forked from deepseek-ai/FlashMLA
-
Notifications
You must be signed in to change notification settings - Fork 6
增强 Python 输入校验 #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghangz
wants to merge
2
commits into
MetaX-MACA:main
Choose a base branch
from
ghangz:mengz/validate-python-inputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增强 Python 输入校验 #23
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,138 @@ | ||||||||||||||||||||||||||||
| import importlib | ||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||
| import types | ||||||||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class FakeFlashMla(types.SimpleNamespace): | ||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||
| super().__init__( | ||||||||||||||||||||||||||||
| get_mla_metadata=self.get_mla_metadata, | ||||||||||||||||||||||||||||
| fwd_kvcache_mla=self.fwd_kvcache_mla, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| self.metadata_calls = 0 | ||||||||||||||||||||||||||||
| self.kvcache_calls = 0 | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_mla_metadata(self, cache_seqlens, num_heads_per_head_k, num_heads_k): | ||||||||||||||||||||||||||||
| self.metadata_calls += 1 | ||||||||||||||||||||||||||||
| metadata = torch.empty((1, 16), dtype=torch.int32, device=cache_seqlens.device) | ||||||||||||||||||||||||||||
| num_splits = torch.empty((cache_seqlens.shape[0] + 1,), dtype=torch.int32, device=cache_seqlens.device) | ||||||||||||||||||||||||||||
| return metadata, num_splits | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def fwd_kvcache_mla( | ||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||
| q, | ||||||||||||||||||||||||||||
| k_cache, | ||||||||||||||||||||||||||||
| _v_cache, | ||||||||||||||||||||||||||||
| head_dim_v, | ||||||||||||||||||||||||||||
| cache_seqlens, | ||||||||||||||||||||||||||||
| block_table, | ||||||||||||||||||||||||||||
| softmax_scale, | ||||||||||||||||||||||||||||
| causal, | ||||||||||||||||||||||||||||
| tile_scheduler_metadata, | ||||||||||||||||||||||||||||
| num_splits, | ||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||
| self.kvcache_calls += 1 | ||||||||||||||||||||||||||||
| out = torch.empty((*q.shape[:-1], head_dim_v), dtype=q.dtype, device=q.device) | ||||||||||||||||||||||||||||
| lse = torch.empty((q.shape[0], q.shape[2], q.shape[1]), dtype=torch.float32, device=q.device) | ||||||||||||||||||||||||||||
| return out, lse | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class PythonInputValidationTest(unittest.TestCase): | ||||||||||||||||||||||||||||
| def setUp(self): | ||||||||||||||||||||||||||||
| self.fake_extension = FakeFlashMla() | ||||||||||||||||||||||||||||
| self._saved_modules = {} | ||||||||||||||||||||||||||||
| for name in ("flash_mla_cuda", "flash_mla", "flash_mla.flash_mla_interface"): | ||||||||||||||||||||||||||||
| if name in sys.modules: | ||||||||||||||||||||||||||||
| self._saved_modules[name] = sys.modules[name] | ||||||||||||||||||||||||||||
| sys.modules["flash_mla_cuda"] = self.fake_extension | ||||||||||||||||||||||||||||
| sys.modules.pop("flash_mla", None) | ||||||||||||||||||||||||||||
| sys.modules.pop("flash_mla.flash_mla_interface", None) | ||||||||||||||||||||||||||||
| self.interface = importlib.import_module("flash_mla.flash_mla_interface") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def tearDown(self): | ||||||||||||||||||||||||||||
| for name in ("flash_mla", "flash_mla.flash_mla_interface", "flash_mla_cuda"): | ||||||||||||||||||||||||||||
| sys.modules.pop(name, None) | ||||||||||||||||||||||||||||
| sys.modules.update(self._saved_modules) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _valid_kvcache_inputs(self): | ||||||||||||||||||||||||||||
| batch_size = 2 | ||||||||||||||||||||||||||||
| q = torch.randn(batch_size, 1, 4, 8) | ||||||||||||||||||||||||||||
| k_cache = torch.randn(3, 16, 2, 8) | ||||||||||||||||||||||||||||
| block_table = torch.zeros((batch_size, 1), dtype=torch.int32) | ||||||||||||||||||||||||||||
| cache_seqlens = torch.full((batch_size,), 8, dtype=torch.int32) | ||||||||||||||||||||||||||||
| tile_scheduler_metadata = torch.zeros((1, 16), dtype=torch.int32) | ||||||||||||||||||||||||||||
| num_splits = torch.zeros((batch_size + 1,), dtype=torch.int32) | ||||||||||||||||||||||||||||
| return q, k_cache, block_table, cache_seqlens, 4, tile_scheduler_metadata, num_splits | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_metadata_rejects_wrong_cache_seqlens_dtype_before_extension(self): | ||||||||||||||||||||||||||||
| cache_seqlens = torch.ones((2,), dtype=torch.int64) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(TypeError, "cache_seqlens"): | ||||||||||||||||||||||||||||
| self.interface.get_mla_metadata(cache_seqlens, 4, 2) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.metadata_calls, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_metadata_rejects_non_positive_head_counts_before_extension(self): | ||||||||||||||||||||||||||||
| cache_seqlens = torch.ones((2,), dtype=torch.int32) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "num_heads_per_head_k"): | ||||||||||||||||||||||||||||
| self.interface.get_mla_metadata(cache_seqlens, 0, 2) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "num_heads_k"): | ||||||||||||||||||||||||||||
| self.interface.get_mla_metadata(cache_seqlens, 4, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.metadata_calls, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_kvcache_rejects_incompatible_heads_before_extension(self): | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits = self._valid_kvcache_inputs() | ||||||||||||||||||||||||||||
| q = torch.randn(q.shape[0], q.shape[1], 3, q.shape[3]) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "divisible"): | ||||||||||||||||||||||||||||
| self.interface.flash_mla_with_kvcache( | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.kvcache_calls, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+101
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在添加了
Suggested change
|
||||||||||||||||||||||||||||
| def test_kvcache_rejects_bad_num_splits_length_before_extension(self): | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, _num_splits = self._valid_kvcache_inputs() | ||||||||||||||||||||||||||||
| num_splits = torch.zeros((q.shape[0],), dtype=torch.int32) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "batch_size \\+ 1"): | ||||||||||||||||||||||||||||
| self.interface.flash_mla_with_kvcache( | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.kvcache_calls, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_kvcache_rejects_mismatched_q_and_k_cache_dtype_before_extension(self): | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits = self._valid_kvcache_inputs() | ||||||||||||||||||||||||||||
| k_cache = k_cache.to(torch.float16) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with self.assertRaisesRegex(TypeError, "same dtype"): | ||||||||||||||||||||||||||||
| self.interface.flash_mla_with_kvcache( | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.kvcache_calls, 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_kvcache_accepts_valid_inputs_and_uses_default_scale(self): | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits = self._valid_kvcache_inputs() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| out, lse = self.interface.flash_mla_with_kvcache( | ||||||||||||||||||||||||||||
| q, k_cache, block_table, cache_seqlens, head_dim_v, metadata, num_splits | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.assertEqual(out.shape, (2, 1, 4, head_dim_v)) | ||||||||||||||||||||||||||||
| self.assertEqual(lse.shape, (2, 4, 1)) | ||||||||||||||||||||||||||||
| self.assertEqual(self.fake_extension.kvcache_calls, 1) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议增加对
q和k_cache的数据类型(dtype)校验。由于 FlashMLA 核心算子要求输入的 Query 和 Key/Value 缓存具有相同的数据类型(通常为bfloat16或float16),并且必须是浮点类型,在 Python 侧提前校验可以避免底层 C++ 算子因类型不匹配或非浮点类型而导致未定义行为或崩溃。