Merged
fix(codegen): Uniquify the LiteNPU kernel entry name by function hash #5598
hid22900163Add on Aug 21
fix(codegen): Uniquify the LiteNPU kernel entry name by function hash #5598
Merged
Merged fromNikita17/pyptov3_01_litenpuintocann/pyptomaster
Pull Request has been successfully merged by CANN-robot.
(Thank you for hid22900163's contribution.)Aug 21 Created pull request, commit 6d913851
atomgit-bot
Aug 21 Comments:
Aug 21 Comments:
变更摘要
本 PR 旨在修复 LiteNPU 代码生成中内核入口符号名可能冲突的问题:由于生成 magic name 所用的函数 id 计数器按进程重置,不同进程构建的多个内核可能得到相同的入口符号,合并为离线模型时后一个内核会静默覆盖前一个。此改动将函数内容哈希(GetFunctionHash().GetHash())追加到内核入口名中,使符号名由内核体决定而非进程本地状态,并新增 Kirin9030 测试验证该行为。
主要改动
- 新增
UniqueKernelBase辅助函数(codegen_litenpu.cpp):将f.GetMagicName()与f.GetFunctionHash().GetHash()拼接为唯一内核基名,并在GenCode中传给GenConfigJson、在模板替换表中用作FunctionName,使 JSON sidecar 名称与生成的入口符号都包含内容哈希。 - 更新运行时内核注册名(
eslmodel_launcher.cpp):EslModelLauncher::LiteRegisterKernel中构造的kernelName由GetMagicName() + "_main"改为GetMagicName() + "_" + 哈希 + "_main",与代码生成侧保持一致。 - 新增 Kirin9030 单测(
test_kirin9030_kernel_entry_uniqueness.py):通过subprocess在两个全新解释器中分别构建同名(add_kernel)内核,断言相同内核体得到相同入口名(控制用例,证明哈希仅依赖内核体)、不同内核体得到不同入口名(守卫用例),并进一步校验两个不同入口名仅在内容哈希部分存在差异;在无法产出 JSON sidecar 的环境(未设置ASCEND_HOME_PATH)中跳过。


Incorrect?
atomgit-bot
Aug 21 Comments:
Aug 21 Comments:
Aug 21 Added label: cann-cla/yes
CANN-robot
Aug 21 Comments:
Aug 21 Comments:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| framework/src/codegen/npu/litenpu | ✅ 尹杰, make_pto, jason_yuan_ye (3/2) | ✅ 尹杰, make_pto (2/1) |
| framework/src/interface/function | ✅ jason_yuan_ye, 尹杰, make_pto (3/2) | ✅ 尹杰, make_pto (2/1) |
| framework/src/machine | ✅ 尹杰, jason_yuan_ye (2/2) | ✅ 尹杰 (1/1) |
| repo-cann/pypto | ✅ 尹杰, make_pto, jason_yuan_ye (3/2) | ✅ 尹杰, make_pto (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
Nikita17, thanks for your pull request. All authors of the commits have signed the CLA. 👍


68 messages are collapsed here。 See more
Aug 31 Added label: approved
Aug 31 Resolved final issue
袁野
Sep 1 Comments:
Sep 1 Comments:
/lgtm


Sep 1 Added label: lgtm
Sep 1 Merged pull request
fix(codegen): Uniquify the LiteNPU kernel entry name by function hash
Split out of https://gitcode.com/cann/pypto/merge_requests/5158 for reviewability. This is the only part of that PR that changes framework code, and it is independent of the rest of the series.
Two kernels built in separate processes can receive the same generated entry symbol, because the function-id counter that feeds the magic name restarts per process. When their artifacts are combined into one offline model the second kernel silently overwrites the first. Appending the function hash to the entry name makes the symbol depend on the kernel body rather than on process-local state.
Changes:
Revision notes
Function::GetMagicNameWithHash(), called from both codegen sites and the launcher. The interimframework/src/interface/utils/kernel_entry_name.{h,cpp}pair introduced by an earlier revision is deleted —Functionalready lives underinterface/, so a member has the same layering and link properties as a free function there and needs no new file. The test's build driver is now a real module,kernel_entry_uniqueness_driver.py, copied per build with the operation passed as an argument, rather than a templated source string.Function.custom_pypto_20260717_rebased, the monolithic branch this series was split from: addstest_kirin9030_kernel_entry_uniqueness.py(4 tests), absent there.Why this is needed at all
Before this change the emitted LiteNPU entry symbol was
TENSOR_default_loop_1_Unroll1_PATH0_hiddenfunc0_5_main— byte-identical across unrelated kernels. On device that mis-dispatched: a two-kernel pipeline failed precision at cosine 0.924405, because one kernel's binary shadowed the other inside the combined offline model.An upstream change complicates the picture and is worth naming, because it invites the wrong conclusion.
51ff0e528"feat(ir): Switch new_ir config to true" (2026-08-12) flippednew_irfromFalsetoTrue, moving kernels from the AST parser onto the PIL pipeline. The two pipelines name functions differently, so magic names acquired a per-kernel component they did not have before. That was a feature switch, not a naming fix.It does not make this change unnecessary. On the current pipeline, across the shipped demo set, 16 distinct magic names include 3 that are each shared by two or three different kernels:
add_kernel_PATH0_hiddenfunc_5add,add_softmax_pipeline,mixed_ascendc_pyptosoftmax_kernel_PATH0_hiddenfunc_5softmax,add_softmax_pipelinepypto_add_kernel_PATH0_hiddenfunc_5mixed_ascendc_pypto,mixed_torch_pyptoThe function id is
_5in every one —Program::Reset()rewinds the counter per process, so the id never disambiguates. Each operator of a multi-operator offline model is exported and built by its own process and the artifacts are combined afterwards, which is exactly the regime where those names meet.test_distinct_entry_names_differ_only_in_the_content_hashasserts the collision directly: two same-named kernels with different bodies share a magic name and are separated by the content hash alone.new_ir=Falsealso remains a supported argument, and restores the fully generic name where the hash does all of the work.Related Issues: #2869, #5158