编译失败时报告 mxcc 命令#26
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves error reporting for MACA compilation failures in mxcc.py by introducing a helper function _format_mxcc_error that formats the failure with command, compiler output, and source code context. It also adds a unit test to verify the formatted error message. The review feedback suggests improving the robustness of the error formatting function by safely decoding compiler output and converting command arguments to strings to prevent runtime errors. Additionally, it recommends simplifying the unit test by directly importing tvm.contrib.mxcc instead of manually mocking the TVM package structure.
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.
| def _format_mxcc_error(cmd, compiler_output, code): | ||
| """Format an mxcc failure with enough context for reproduction.""" | ||
| return "\n".join( | ||
| [ | ||
| "MACA compilation failed.", | ||
| "Command:", | ||
| shlex.join(cmd), | ||
| "Compiler output:", | ||
| py_str(compiler_output), | ||
| "Source:", | ||
| code, | ||
| ] | ||
| ) |
There was a problem hiding this comment.
在编译失败时,如果编译器输出(compiler_output)包含非 UTF-8 字符(例如在某些本地化环境下,或者输出中包含二进制垃圾字符),调用 py_str(compiler_output) 会抛出 UnicodeDecodeError,从而掩盖了真正的编译错误。此外,如果 cmd 中包含 pathlib.Path 等非字符串对象,shlex.join 会抛出 TypeError。
建议对 compiler_output 进行安全的解码(使用 errors="replace"),并将 cmd 中的所有元素转换为字符串,以提高错误处理的鲁棒性。
def _format_mxcc_error(cmd, compiler_output, code):
"""Format an mxcc failure with enough context for reproduction."""
if isinstance(compiler_output, bytes):
out_str = compiler_output.decode("utf-8", errors="replace")
else:
out_str = str(compiler_output)
return "\n".join(
[
"MACA compilation failed.",
"Command:",
shlex.join(str(x) for x in cmd),
"Compiler output:",
out_str,
"Source:",
code,
]
)| import importlib.util | ||
| import sys | ||
| import types | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[3] | ||
| MXCC_PATH = REPO_ROOT / "python" / "tvm" / "contrib" / "mxcc.py" | ||
|
|
||
| tvm_ffi = types.ModuleType("tvm_ffi") | ||
| tvm_ffi.register_global_func = lambda *args, **kwargs: ( | ||
| (lambda func: func) if args and callable(args[0]) else (lambda func: func) | ||
| ) | ||
| tvm_ffi.get_global_func = lambda _name: None | ||
| sys.modules["tvm_ffi"] = tvm_ffi | ||
|
|
||
| tvm = types.ModuleType("tvm") | ||
| tvm.maca = lambda *_args, **_kwargs: types.SimpleNamespace(exist=False) | ||
| tvm.target = types.ModuleType("tvm.target") | ||
| tvm.target.Target = object | ||
| sys.modules["tvm"] = tvm | ||
| sys.modules["tvm.target"] = tvm.target | ||
|
|
||
| tvm_base = types.ModuleType("tvm.base") | ||
| tvm_base.py_str = lambda value: value.decode("utf-8") if isinstance(value, bytes) else str(value) | ||
| sys.modules["tvm.base"] = tvm_base | ||
|
|
||
| tvm_contrib = types.ModuleType("tvm.contrib") | ||
| tvm_contrib.__path__ = [] | ||
| sys.modules["tvm.contrib"] = tvm_contrib | ||
|
|
||
| tvm_contrib_utils = types.ModuleType("tvm.contrib.utils") | ||
| tvm_contrib_utils.tempdir = lambda: None | ||
| sys.modules["tvm.contrib.utils"] = tvm_contrib_utils | ||
|
|
||
| spec = importlib.util.spec_from_file_location("tvm.contrib.mxcc", MXCC_PATH) | ||
| mxcc = importlib.util.module_from_spec(spec) | ||
| mxcc.__package__ = "tvm.contrib" | ||
| sys.modules["tvm.contrib.mxcc"] = mxcc | ||
| assert spec.loader is not None | ||
| spec.loader.exec_module(mxcc) | ||
|
|
There was a problem hiding this comment.
在测试文件中手动模拟(mock)整个 tvm 包及其子模块是非常脆弱且非标准的做法。TVM 的测试套件通常在完整的构建环境下运行,因此可以直接导入 tvm.contrib.mxcc。
建议简化测试文件,直接导入 tvm.contrib.mxcc,避免复杂的 mock 逻辑,提高代码的可维护性。
| import importlib.util | |
| import sys | |
| import types | |
| import unittest | |
| from pathlib import Path | |
| REPO_ROOT = Path(__file__).resolve().parents[3] | |
| MXCC_PATH = REPO_ROOT / "python" / "tvm" / "contrib" / "mxcc.py" | |
| tvm_ffi = types.ModuleType("tvm_ffi") | |
| tvm_ffi.register_global_func = lambda *args, **kwargs: ( | |
| (lambda func: func) if args and callable(args[0]) else (lambda func: func) | |
| ) | |
| tvm_ffi.get_global_func = lambda _name: None | |
| sys.modules["tvm_ffi"] = tvm_ffi | |
| tvm = types.ModuleType("tvm") | |
| tvm.maca = lambda *_args, **_kwargs: types.SimpleNamespace(exist=False) | |
| tvm.target = types.ModuleType("tvm.target") | |
| tvm.target.Target = object | |
| sys.modules["tvm"] = tvm | |
| sys.modules["tvm.target"] = tvm.target | |
| tvm_base = types.ModuleType("tvm.base") | |
| tvm_base.py_str = lambda value: value.decode("utf-8") if isinstance(value, bytes) else str(value) | |
| sys.modules["tvm.base"] = tvm_base | |
| tvm_contrib = types.ModuleType("tvm.contrib") | |
| tvm_contrib.__path__ = [] | |
| sys.modules["tvm.contrib"] = tvm_contrib | |
| tvm_contrib_utils = types.ModuleType("tvm.contrib.utils") | |
| tvm_contrib_utils.tempdir = lambda: None | |
| sys.modules["tvm.contrib.utils"] = tvm_contrib_utils | |
| spec = importlib.util.spec_from_file_location("tvm.contrib.mxcc", MXCC_PATH) | |
| mxcc = importlib.util.module_from_spec(spec) | |
| mxcc.__package__ = "tvm.contrib" | |
| sys.modules["tvm.contrib.mxcc"] = mxcc | |
| assert spec.loader is not None | |
| spec.loader.exec_module(mxcc) | |
| import unittest | |
| from tvm.contrib import mxcc | |
该 PR 在 mxcc 编译失败时输出完整命令和关键上下文,让维护者可以直接复现失败场景。
这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。
已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:
mengz/report-mxcc-command-on-error,目标仓库:MetaX-MACA/mcTVM。