Merged
feat(machine): Support offline compilation and packaging #4974
W4termyAdd on Jul 21
feat(machine): Support offline compilation and packaging #4974
Merged
W4termyAdd on Jul 21
W4termy
W4termyCollaborators
Jul 21
支持离线打包二进制
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for W4termy's contribution.)
W4termyW4termyCollaborators
Jul 21 Created pull request, commit 64a3242d
atomgit-bot
atomgit-bot
Jul 21 Comments:

变更摘要

本 PR 为 PyPTO 框架引入了**内核离线编译与打包(Kernel Bundle)**功能,定义了一种 .pyptokb 打包格式,将编译好的算子程序、AiCpu 后端、控制流缓存、动态工作空间符号元数据等全部序列化为一个自包含文件。同时提供了基于路径和基于内存的两套两接口 C ABI(PyptoWorkspace/PyptoLaunch 及其 FromMemory 变体),使得离线部署场景下无需完整框架运行时即可加载并执行打包内核。整个子系统包含打包、加载、缓存注册、设备端缓存管理、动态工作空间评估、以及离线启动等模块,并集成到现有编译与启动流程中。

主要改动

  • 新增 .pyptokb 打包格式定义与 CRC32 校验:kernel_bundle_format.h 定义了 TLV 分段的磁盘格式(含 magic、版本号、4KB 对齐),kernel_bundle_crc32.h 提供 IEEE-802.3 CRC32 实现,用于文件头和各分段值的完整性校验。
  • 新增打包/加载器及全局注册表:KernelBundlePacker(kernel_bundle_packer.h/.cpp)将五个 TLV 分段(AicoreKernel、AicpuSo、DevProgram、CtrlFlowCache、SymbolMeta)序列化为 .pyptokb;KernelBundleLoader(kernel_bundle_loader.h/.cpp)从文件或内存解析并验证该格式;KernelBundleRegistry(kernel_bundle_registry.h/.cpp)按 hashKey 缓存已加载的 bundle 并管理内核二进制句柄。
  • 新增两接口 C ABI 与离线启动链路:pypto_bundle_api.h/.cpp 导出 PyptoWorkspace/PyptoLaunch(路径版本)和 PyptoWorkspaceFromMemory/PyptoLaunchFromMemory(内存版本)四个 C 函数;kernel_bundle_launch.h/.cpp 实现自包含的离线启动逻辑,从打包的 DevAscendProgram 读取拓扑并分别启动 AiCpu 和 AiCore 内核。
  • 新增动态工作空间评估与设备端缓存:kernel_bundle_workspace.h/.cpp 将符号化工作空间树(SymbolicScalar)序列化为 JSON 存入 SymbolMeta 分段,加载时根据实际张量形状动态评估工作空间大小;KernelBundleDevCache(kernel_bundle_dev_cache.h/.cpp)按 bundle 缓存控制流和设备端 cell-match 元数据池,避免重复拷贝。
  • 集成打包钩子到现有编译与启动流程:在 emulation_launcher.cpp、ctrl_flow_cache_manager.cpp、kernel_binary.cpp 中插入 bundle 打包调用点(受 PYPTO_ENABLE_KERNEL_BUNDLE 环境变量控制);在 device_runner.cpp 中支持从 bundle 的 AICPU_SO 分段加载后端 .so(SetAiCpuSoOverride);在 cell_match_dynamic.cpp 中新增无 Function 场景的重载以支持 bundle 加载路径。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 21 Comments:

代码审查

审查总结

本次审查覆盖了全部 26 个变更文件。以下为各文件的审查结果及发现的问题统计:

问题统计

  • P1: 1 个(整数溢出导致越界读取)
  • P2: 2 个(设备内存分配失败被永久缓存、TLV count 乘法溢出)
  • P3: 3 个(CRC32 多项式拼写错误、AlignedCopy 对齐注释误导、config 参数被丢弃)

各文件审查结果

文件 结果
examples/00_hello_world/st_kernel_bundle.py 无问题
examples/03_advanced/advanced_nn/attention/st_kernel_bundle_attn.py 无问题
framework/src/machine/runtime/bundle/kernel_bundle_crc32.h P3: CRC32 多项式 0xEDB88420 非标准值
framework/src/machine/runtime/bundle/kernel_bundle_dev_cache.cpp P2: GetOrCopy 将失败的 CopyToDev 返回的 nullptr 永久缓存
framework/src/machine/runtime/bundle/kernel_bundle_dev_cache.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_format.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_launch.cpp P3: config 参数的 blockdim 未被使用
framework/src/machine/runtime/bundle/kernel_bundle_launch.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_loader.cpp P1: TLV valueOffset 加法溢出绕过越界检查;P2: tlvCount 乘法溢出;P3: AlignedCopy 未实现 4KB 对齐
framework/src/machine/runtime/bundle/kernel_bundle_loader.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_packer.cpp 无问题
framework/src/machine/runtime/bundle/kernel_bundle_packer.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_registry.cpp 无问题
framework/src/machine/runtime/bundle/kernel_bundle_registry.h 无问题
framework/src/machine/runtime/bundle/kernel_bundle_workspace.cpp 无问题
framework/src/machine/runtime/bundle/kernel_bundle_workspace.h 无问题
framework/src/machine/runtime/bundle/pypto_bundle_api.cpp 无问题
framework/src/machine/runtime/bundle/pypto_bundle_api.h 无问题
framework/src/machine/runtime/launcher/cell_match_dynamic.cpp 无问题
framework/src/machine/runtime/launcher/cell_match_dynamic.h 无问题
framework/src/machine/runtime/launcher/ctrl_flow_cache_manager.cpp 无问题
framework/src/machine/runtime/launcher/emulation_launcher.cpp 无问题
framework/src/machine/runtime/runner/device_runner.cpp 无问题
framework/src/machine/runtime/runner/device_runner.h 无问题
framework/src/machine/runtime/runner/kernel_binary.cpp 无问题
framework/src/machine/runtime/runner/kernel_binary.h 无问题

整体风险评估

中等风险。最严重的问题是 kernel_bundle_loader.cpp 中的整数溢出漏洞(P1),恶意构造的 .pyptokb 文件可利用此漏洞导致越界内存读取。由于 bundle 格式目前为内部使用(通过受信任的工具链生成),实际利用概率较低,但建议在上线前修复。P2 的设备内存缓存问题在内存紧张场景下可能导致永久性 launch 失败,需关注。其余 P3 问题为代码卫生问题,不影响当前功能的正确运行。

类型 数量
🔴 阻塞 1
🟡 建议 1

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 21 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:

CLA Signature Pass

W4termy, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
129 messages are collapsed here。 See more
W4termyW4termyCollaborators
Aug 5 Resolved final issue
gaoxingwang
gaoxingwangCollaborators
Aug 5 Comments:

/lgtm

likedislike
袁野
袁野Collaborators
Aug 5 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 5 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 5 Merged pull request