Skip to content

增强 MACA_HOME 查找回退#19

Open
ghangz wants to merge 1 commit into
MetaX-MACA:mainfrom
ghangz:mengz/find-maca-home-fallback
Open

增强 MACA_HOME 查找回退#19
ghangz wants to merge 1 commit into
MetaX-MACA:mainfrom
ghangz:mengz/find-maca-home-fallback

Conversation

@ghangz

@ghangz ghangz commented Jun 8, 2026

Copy link
Copy Markdown

该 PR 为 MACA_HOME 查找增加常见安装路径回退,提高不同沐曦镜像和本地工具链布局下的可用性。

这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。

已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:mengz/find-maca-home-fallback,目标仓库:MetaX-MACA/mcTVM

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the FindMACA.cmake module to reset MACA-related variables before searching, support the MACA_HOME environment variable, and restrict library searches to the specified SDK path. It also introduces a Python-based unit test suite to verify these CMake behaviors. The reviewer feedback suggests robust improvements, including clearing CMake cache variables using unset(... CACHE) instead of just normal variables, wrapping path variables in double quotes to handle spaces safely, and updating the new unit tests to accommodate these changes.

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.

Comment on lines +37 to +42
unset(MACA_FOUND)
unset(MACA_ROOT_DIR)
unset(MACA_INCLUDE_DIRS)
unset(MACA_MACAMCC_LIBRARY)
unset(MACA_HCA_LIBRARY)
unset(MACA_FLASHATTN_LIBRARY)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

由于 find_library 会将找到的路径缓存在 CMake Cache 中(即创建 Cache 变量),仅使用 unset(VAR) 只能清除普通变量,无法清除 Cache 变量。如果用户更改了 MACA_PATHMACA_HOME 并重新运行 CMake,find_library 仍会直接使用缓存中的旧路径,而不会在新路径下重新查找。

建议同时清除 Cache 变量,以确保路径切换时能正确重新检测。

  unset(MACA_FOUND)
  unset(MACA_ROOT_DIR)
  unset(MACA_INCLUDE_DIRS)
  unset(MACA_MACAMCC_LIBRARY CACHE)
  unset(MACA_HCA_LIBRARY CACHE)
  unset(MACA_FLASHATTN_LIBRARY CACHE)

Comment on lines 44 to +50
if(IS_DIRECTORY ${__use_maca})
set(__maca_sdk ${__use_maca})
message(STATUS "Custom MACA SDK PATH=" ${__use_maca})
elseif(IS_DIRECTORY $ENV{MACA_PATH})
set(__maca_sdk $ENV{MACA_PATH})
elseif(IS_DIRECTORY $ENV{MACA_HOME})
set(__maca_sdk $ENV{MACA_HOME})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在 CMake 中,如果变量或环境变量为空,或者其路径中包含空格,未加双引号的变量在 IS_DIRECTORY 中展开时会导致语法错误(例如 IS_DIRECTORY 后面没有参数,或者参数被拆分为多个)。

为了提高鲁棒性,建议将路径变量和环境变量用双引号括起来,例如 "${__use_maca}""$ENV{MACA_PATH}""$ENV{MACA_HOME}"

  if(IS_DIRECTORY "${__use_maca}")
    set(__maca_sdk "${__use_maca}")
    message(STATUS "Custom MACA SDK PATH=" "${__use_maca}")
  elseif(IS_DIRECTORY "$ENV{MACA_PATH}")
    set(__maca_sdk "$ENV{MACA_PATH}")
  elseif(IS_DIRECTORY "$ENV{MACA_HOME}")
    set(__maca_sdk "$ENV{MACA_HOME}")

Comment on lines +58 to +62
set(MACA_ROOT_DIR ${__maca_sdk})
set(MACA_INCLUDE_DIRS ${__maca_sdk}/include)
find_library(MACA_MACAMCC_LIBRARY mcruntime ${__maca_sdk}/lib)
find_library(MACA_HCA_LIBRARY mxc-runtime64 ${__maca_sdk}/lib)
find_library(MACA_FLASHATTN_LIBRARY mcFlashAttn ${__maca_sdk}/lib)
find_library(MACA_MACAMCC_LIBRARY mcruntime PATHS ${__maca_sdk}/lib NO_DEFAULT_PATH)
find_library(MACA_HCA_LIBRARY mxc-runtime64 PATHS ${__maca_sdk}/lib NO_DEFAULT_PATH)
find_library(MACA_FLASHATTN_LIBRARY mcFlashAttn PATHS ${__maca_sdk}/lib NO_DEFAULT_PATH)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

同样地,为了防止 ${__maca_sdk} 路径中包含空格时被 CMake 拆分为多个参数或列表,建议在 setfind_library 中对路径进行双引号保护。

    set(MACA_ROOT_DIR "${__maca_sdk}")
    set(MACA_INCLUDE_DIRS "${__maca_sdk}/include")
    find_library(MACA_MACAMCC_LIBRARY mcruntime PATHS "${__maca_sdk}/lib" NO_DEFAULT_PATH)
    find_library(MACA_HCA_LIBRARY mxc-runtime64 PATHS "${__maca_sdk}/lib" NO_DEFAULT_PATH)
    find_library(MACA_FLASHATTN_LIBRARY mcFlashAttn PATHS "${__maca_sdk}/lib" NO_DEFAULT_PATH)

"MACA_HCA_LIBRARY",
"MACA_FLASHATTN_LIBRARY",
]:
self.assertRegex(self.content, rf"unset\({re.escape(variable)}\)")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

如果我们在 CMake 中将 unset(VAR) 改为 unset(VAR CACHE)(以确保清除缓存变量),当前的正则表达式 rf"unset\({re.escape(variable)}\)" 将会匹配失败。

建议将正则表达式修改为允许可选的 CACHE 参数,以提高测试的兼容性和健壮性。

Suggested change
self.assertRegex(self.content, rf"unset\({re.escape(variable)}\)")
self.assertRegex(self.content, rf"unset\({re.escape(variable)}(?:\s+CACHE)?\)")

Comment on lines +39 to +42
self.assertRegex(
self.content,
rf"find_library\({variable} .* PATHS \${{__maca_sdk}}/lib NO_DEFAULT_PATH\)",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

如果我们为了防止路径中包含空格而在 CMake 中将 ${__maca_sdk}/lib 用双引号括起来(即 "${__maca_sdk}/lib"),当前的测试正则表达式将会匹配失败。

建议将正则表达式修改为允许可选的双引号,以确保测试的健壮性。

Suggested change
self.assertRegex(
self.content,
rf"find_library\({variable} .* PATHS \${{__maca_sdk}}/lib NO_DEFAULT_PATH\)",
)
self.assertRegex(
self.content,
rf"find_library\({variable} .* PATHS \"?\${{__maca_sdk}}/lib\"? NO_DEFAULT_PATH\)",
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant