forked from deepseek-ai/FlashMLA
-
Notifications
You must be signed in to change notification settings - Fork 6
检测真实 cucc 版本 #17
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/detect-real-cucc-version
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
检测真实 cucc 版本 #17
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Build-time helpers for FlashMLA.""" |
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,48 @@ | ||
| import re | ||
| import subprocess | ||
| from pathlib import Path | ||
| from typing import Callable, List, Tuple, Union | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
|
|
||
| def parse_cuda_release_version(output: str) -> Version: | ||
| release_match = re.search(r"release\s+(\d+\.\d+)", output) | ||
| if release_match: | ||
| return Version(release_match.group(1)) | ||
|
|
||
| version_match = re.search(r"\bV(\d+\.\d+)(?:\.\d+)?\b", output) | ||
| if version_match: | ||
| return Version(version_match.group(1)) | ||
|
|
||
| raise ValueError(f"Cannot parse compiler release version from output: {output!r}") | ||
|
|
||
|
|
||
| def _compiler_candidates(cuda_dir: Union[str, Path]) -> List[Path]: | ||
| bin_dir = Path(cuda_dir) / "bin" | ||
| return [bin_dir / "cucc", bin_dir / "nvcc"] | ||
|
|
||
|
|
||
| def get_cuda_bare_metal_version( | ||
| cuda_dir: Union[str, Path], | ||
| check_output: Callable[..., str] = subprocess.check_output, | ||
| ) -> Tuple[str, Version]: | ||
| errors: List[str] = [] | ||
| for compiler in _compiler_candidates(cuda_dir): | ||
| if not compiler.is_file(): | ||
| errors.append(f"{compiler} does not exist") | ||
| continue | ||
| try: | ||
| raw_output = check_output( | ||
| [str(compiler), "-V"], | ||
| stderr=subprocess.STDOUT, | ||
| universal_newlines=True, | ||
| ) | ||
| return raw_output, parse_cuda_release_version(raw_output) | ||
| except Exception as err: | ||
| errors.append(f"{compiler}: {err}") | ||
|
|
||
| details = "\n".join(f"- {error}" for error in errors) | ||
| raise RuntimeError( | ||
| f"Cannot detect CUDA-compatible compiler version under {cuda_dir}.\n{details}" | ||
| ) |
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,50 @@ | ||
| import unittest | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
| from build_tools.compiler_version import ( | ||
| get_cuda_bare_metal_version, | ||
| parse_cuda_release_version, | ||
| ) | ||
|
|
||
|
|
||
| class CompilerVersionTest(unittest.TestCase): | ||
| def test_parse_nvcc_release_version(self): | ||
| output = "Cuda compilation tools, release 12.1, V12.1.105" | ||
|
|
||
| self.assertEqual(parse_cuda_release_version(output), Version("12.1")) | ||
|
|
||
| def test_parse_cucc_version_without_release_token(self): | ||
| output = "cucc compiler driver V12.2.91" | ||
|
|
||
| self.assertEqual(parse_cuda_release_version(output), Version("12.2")) | ||
|
|
||
| def test_get_cuda_bare_metal_version_prefers_cucc(self): | ||
| with TemporaryDirectory() as tmp_dir: | ||
| bin_dir = Path(tmp_dir) / "bin" | ||
| bin_dir.mkdir() | ||
| cucc = bin_dir / "cucc" | ||
| nvcc = bin_dir / "nvcc" | ||
| cucc.write_text("#!/bin/sh\n", encoding="utf-8") | ||
| nvcc.write_text("#!/bin/sh\n", encoding="utf-8") | ||
|
|
||
| commands: list[list[str]] = [] | ||
|
|
||
| def fake_check_output(command, **_kwargs): | ||
| commands.append(command) | ||
| return "cucc compiler driver V12.3.0" | ||
|
|
||
| raw_output, version = get_cuda_bare_metal_version( | ||
| tmp_dir, | ||
| check_output=fake_check_output, | ||
| ) | ||
|
|
||
| self.assertEqual(commands, [[str(cucc), "-V"]]) | ||
| self.assertIn("cucc", raw_output) | ||
| self.assertEqual(version, Version("12.3")) | ||
|
|
||
|
|
||
| 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.
新引入的
build_tools目录中包含了__init__.py,这使其被识别为一个 Python 包。由于setup.py中的find_packages调用没有将"build_tools"排除,这会导致在构建和安装flash_mla时,build_tools会作为一个顶级的包被安装到用户的site-packages目录中。这会污染用户的 Python 环境,并且由于
build_tools是一个非常通用的名称,极易与其他库产生命名冲突。建议在
setup.py的find_packages(exclude=(...))中将"build_tools"排除。例如:由于
setup.py在运行时处于项目根目录下,即使排除了该包,构建时依然可以正常导入并使用它。