-
Notifications
You must be signed in to change notification settings - Fork 4
增加算子库算子复杂度报告 #40
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/mcoplib-op-complexity-report
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.
+86
−0
Open
增加算子库算子复杂度报告 #40
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,57 @@ | ||
| #!/usr/bin/env python3 | ||
| """Summarize CUDA/MACA operator source complexity for validation planning.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| SOURCE_SUFFIXES = {".cu", ".cuh", ".cpp", ".h"} | ||
|
|
||
|
|
||
| def analyze_file(path: Path, root: Path) -> dict[str, object]: | ||
| text = path.read_text(encoding="utf-8", errors="replace") | ||
| return { | ||
| "path": path.relative_to(root).as_posix(), | ||
| "lines": len(text.splitlines()), | ||
| "kernel_launches": text.count("<<<"), | ||
| "templates": text.count("template"), | ||
| "torch_bindings": text.count("PYBIND11_MODULE"), | ||
| } | ||
|
|
||
|
|
||
| def build_report(root: Path) -> dict[str, object]: | ||
| op_dir = root / "op" | ||
| if not op_dir.is_dir(): | ||
| return {"file_count": 0, "total_lines": 0, "top_by_lines": []} | ||
|
|
||
| files = [ | ||
| analyze_file(path, root) | ||
| for path in sorted(op_dir.rglob("*")) | ||
| if path.is_file() and path.suffix in SOURCE_SUFFIXES | ||
| ] | ||
| return { | ||
| "file_count": len(files), | ||
| "total_lines": sum(item["lines"] for item in files), | ||
| "top_by_lines": sorted(files, key=lambda item: item["lines"], reverse=True)[:20], | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--root", type=Path, default=Path.cwd(), help="repository root") | ||
| parser.add_argument("--output", type=Path, help="write JSON report to this path") | ||
| args = parser.parse_args() | ||
|
|
||
| text = json.dumps(build_report(args.root), indent=2, ensure_ascii=False) | ||
| if args.output: | ||
| args.output.write_text(text + "\n", encoding="utf-8") | ||
| else: | ||
| print(text) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
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,29 @@ | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| from tools.op_complexity_report import build_report | ||
|
|
||
|
|
||
| class OpComplexityReportTest(unittest.TestCase): | ||
| def test_counts_operator_sources(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| root = Path(tmpdir) | ||
| op = root / "op" | ||
| op.mkdir() | ||
| (op / "kernel.cu").write_text("template <typename T>\nvoid f(){ k<<<1,1>>>(); }\n", encoding="utf-8") | ||
|
|
||
| report = build_report(root) | ||
|
|
||
| self.assertEqual(report["file_count"], 1) | ||
| self.assertEqual(report["top_by_lines"][0]["kernel_launches"], 1) | ||
|
|
||
| def test_returns_empty_report_when_op_directory_is_missing(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| report = build_report(Path(tmpdir)) | ||
|
|
||
| self.assertEqual(report, {"file_count": 0, "total_lines": 0, "top_by_lines": []}) | ||
|
|
||
|
|
||
| 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.
当指定的
root目录下不存在op子目录时,对rglob结果进行排序和迭代会抛出FileNotFoundError异常。为了提高脚本的健壮性,建议在构建报告前先检查op目录是否存在。如果不存在,可以直接返回一个空的报告结构,避免程序崩溃。此外,item["lines"]本身已经是整型,无需在sum和sorted中重复调用int()进行类型转换。