forked from deepseek-ai/FlashMLA
-
Notifications
You must be signed in to change notification settings - Fork 3
延迟导入 FlashMLA 扩展 #20
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
1
commit into
MetaX-MACA:main
Choose a base branch
from
ghangz:mengz/lazy-extension-import
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
延迟导入 FlashMLA 扩展 #20
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| # Adapted from deepseek-ai/FlashMLA(https://github.com/deepseek-ai/FlashMLA) | ||
| __version__ = "1.0.1" | ||
|
|
||
| from flash_mla.flash_mla_interface import( | ||
| get_mla_metadata, | ||
| flash_mla_with_kvcache | ||
| ) | ||
| __all__ = ["__version__", "get_mla_metadata", "flash_mla_with_kvcache"] | ||
|
|
||
|
|
||
| def _load_interface(): | ||
| try: | ||
| from flash_mla.flash_mla_interface import flash_mla_with_kvcache, get_mla_metadata | ||
| except ImportError as exc: | ||
| raise ImportError( | ||
| "flash_mla_cuda is not available. Build and install FlashMLA from source " | ||
| "in a configured MACA environment before calling FlashMLA kernels." | ||
| ) from exc | ||
| return get_mla_metadata, flash_mla_with_kvcache | ||
|
|
||
|
|
||
| def get_mla_metadata(*args, **kwargs): | ||
| metadata_func, _ = _load_interface() | ||
| return metadata_func(*args, **kwargs) | ||
|
|
||
|
|
||
| def flash_mla_with_kvcache(*args, **kwargs): | ||
| _, flash_func = _load_interface() | ||
| return flash_func(*args, **kwargs) | ||
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,14 @@ | ||
| import importlib | ||
| import unittest | ||
|
|
||
|
|
||
| class LazyImportTest(unittest.TestCase): | ||
| def test_import_package_without_compiled_extension(self): | ||
| module = importlib.import_module("flash_mla") | ||
|
|
||
| self.assertEqual(module.__version__, "1.0.1") | ||
| self.assertTrue(callable(module.get_mla_metadata)) | ||
|
|
||
|
|
||
| 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.
问题分析
get_mla_metadata或flash_mla_with_kvcache都会执行_load_interface()。虽然 Python 会缓存已导入的模块,但在高频调用的 CUDA 算子核心路径(hot path)中,频繁执行try-except和import语句仍会带来不必要的 CPU 额外开销。*args, **kwargs包装函数会导致 IDE(如 VS Code、PyCharm)和静态类型检查工具(如 mypy)无法获取原始函数的参数签名和 Docstring,降低了开发体验。解决方案
建议使用模块级缓存来避免重复导入的开销。同时,为了保留参数签名和文档,可以直接在包装函数中显式声明参数。