记录 mxcc 编译命令#22
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to log compilation commands for the MACA backend in mxcc.py to a JSONL file specified by the TVM_MACA_COMPILE_COMMAND_LOG environment variable, along with a new test suite. The reviewer feedback highlights two key improvement opportunities: first, replacing the manual directory existence check with os.makedirs(log_dir, exist_ok=True) to prevent race conditions in concurrent environments; second, replacing the fragile AST-based dynamic loading mechanism in the test file with a direct import of the function under test.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if log_dir and not os.path.isdir(log_dir): | ||
| os.makedirs(log_dir) |
There was a problem hiding this comment.
| import ast | ||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
|
|
||
| def _load_mxcc_logging_helper(): | ||
| source_path = ( | ||
| Path(__file__).resolve().parents[3] / "python" / "tvm" / "contrib" / "mxcc.py" | ||
| ) | ||
| tree = ast.parse(source_path.read_text(encoding="utf-8")) | ||
| allowed_imports = {"json", "os"} | ||
| nodes = [ | ||
| node | ||
| for node in tree.body | ||
| if ( | ||
| isinstance(node, ast.Import) | ||
| and all(alias.name in allowed_imports for alias in node.names) | ||
| ) | ||
| or (isinstance(node, ast.FunctionDef) and node.name == "_write_compile_command_log") | ||
| ] | ||
| module = ast.Module(body=nodes, type_ignores=[]) | ||
| ast.fix_missing_locations(module) | ||
| namespace = {} | ||
| exec(compile(module, str(source_path), "exec"), namespace) | ||
| return namespace["_write_compile_command_log"] | ||
|
|
||
|
|
||
| def test_mxcc_compile_command_log_is_jsonl(tmp_path): | ||
| write_log = _load_mxcc_logging_helper() |
There was a problem hiding this comment.
在测试代码中使用 ast.parse 和 exec 动态解析并执行 _write_compile_command_log 的做法非常脆弱且难以维护。如果未来 mxcc.py 发生变更(例如引入了其他辅助函数、修改了导入模块或使用了模块级变量),这种基于 AST 的加载方式极易崩溃。由于这是标准的 TVM 测试套件,可以直接从 tvm.contrib.mxcc 导入该函数,这样更加简单、健壮且符合常规测试规范。
import json
import os
from pathlib import Path
from unittest.mock import patch
from tvm.contrib.mxcc import _write_compile_command_log
def test_mxcc_compile_command_log_is_jsonl(tmp_path):
write_log = _write_compile_command_log
该 PR 在 mxcc 编译流程中记录实际执行命令,便于复现失败、分析编译参数和提交问题报告。
这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。
已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:
mengz/log-mxcc-compile-command,目标仓库:MetaX-MACA/mcTVM。