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
hid22900163Add on Aug 21
hid22900163
Aug 21

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:

  • Append the function hash to the LiteNPU kernel entry name so distinct kernel bodies cannot share an entry symbol
  • Add a Kirin9030 test that builds the same-named kernel in two fresh subprocesses and asserts identical bodies keep one name while different bodies get different names

Revision notes

  • Since the previous revision: the duplicated LiteNPU entry-name formula is now a single member, Function::GetMagicNameWithHash(), called from both codegen sites and the launcher. The interim framework/src/interface/utils/kernel_entry_name.{h,cpp} pair introduced by an earlier revision is deleted — Function already lives under interface/, 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.
  • Review feedback: answers all three comments on this PR — reuse rather than a second formula, a copied driver rather than a generated one, and the helper as a member of Function.
  • Against custom_pypto_20260717_rebased, the monolithic branch this series was split from: adds test_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) flipped new_ir from False to True, 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:

magic name kernels sharing it
add_kernel_PATH0_hiddenfunc_5 add, add_softmax_pipeline, mixed_ascendc_pypto
softmax_kernel_PATH0_hiddenfunc_5 softmax, add_softmax_pipeline
pypto_add_kernel_PATH0_hiddenfunc_5 mixed_ascendc_pypto, mixed_torch_pypto

The function id is _5 in 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_hash asserts the collision directly: two same-named kernels with different bodies share a magic name and are separated by the content hash alone.

new_ir=False also remains a supported argument, and restores the fully generic name where the hash does all of the work.

Related Issues: #2869, #5158

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for hid22900163's contribution.)
hid22900163hid22900163
Aug 21 Created pull request, commit 6d913851
atomgit-bot
atomgit-bot
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)中跳过。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 21 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 21 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
68 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 31 Added label: approved
Jie YinJie YinCollaborators
Aug 31 Resolved final issue
袁野
袁野Collaborators
Sep 1 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 1 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 1 Merged pull request