Merged
feat(exported_custom_op): Add kernel-source introspection and the runtime compile contract #5599
hid22900163Add on Aug 21
feat(exported_custom_op): Add kernel-source introspection and the runtime compile contract #5599
Merged
hid22900163Add on Aug 21
hid22900163
Aug 21

Split out of https://gitcode.com/cann/pypto/merge_requests/5158 for reviewability. First functional piece of the exported custom-op subsystem: everything downstream needs a way to recover a kernel's source and to compile it under a stable contract.

API surface map for this whole series — every def with its signature, introducing PR and callers: https://claude.ai/code/artifact/a42e70ca-5cf9-4a67-8eb3-b7d3c8e17f1a

Changes:

  • Add kernel-source introspection for decorated kernel functions: source recovery, annotation-span editing, scope classification, and the authoring helpers that read a DIRECT kernel's declared shape/dtype constants and classify a factory's signature
  • Define the runtime compile contract: CompileEntry(jit_kernel=… | factory=…, num_inputs=, num_outputs=, factory_signature=), called as entry(shapes, dtypes, attrs, soc_version) -> compiled-kernel path, result cached per key. Construction accepts exactly one kernel form and rejects an unknown factory_signature or a non-positive input/output count; each call requires soc_version and checks the shape/dtype count against num_inputs
  • infer_shape / infer_dtype are read/write properties, rejecting a non-callable at assignment rather than deep in dispatch; without a hook every output takes the first input's shape and dtype
  • Enforce author-declared annotations at compile: a pinned dtype that disagrees with the runtime dtype raises, a pinned shape warns
  • Wire build_kernel through the frontend parser (single-object contract stated in its error), register the package in pyproject.toml
  • Add the kernel compile samples and the unit tests over the contract, the declared annotations and the warning paths

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 7455d16b
atomgit-bot
atomgit-bot
Aug 21 Comments:

变更摘要

本 PR 是导出自定义算子(exported custom-op)子系统的第一个功能片段:为被装饰的 kernel 函数增加内核源码自省(source introspection)能力,并定义声明/导出路径依赖的运行时编译契约。核心是新增 pypto.extensions.torch_custom_op 扩展包(在 pyproject.toml 中注册、并在 pypto/__init__.py 中以 pypto.torch_custom_op_compile 顶层别名暴露),其中 common/compile.py 提供可调用的运行时编译入口 CompileEntry,common/authoring.py / common/source_utils.py 提供 kernel 形式识别、工厂签名分类与源码改写工具;同时在 LiteNPU 代码生成侧把内核入口/侧车名称改为“magic name + 内容哈希”组合以保证跨进程构建的入口符号唯一,并为 JitCallableWrapper 新增 build_kernel 编译入口(以进程级 _BUILD_LOCK 串行化)。配套新增共享的 kernel 编译样例包 sample_helper_pkg、kernel_compile_samples.py 及多组单元/盒子测试。

主要改动

  • LiteNPU 内核入口符号唯一化:codegen_litenpu.cpp 新增 UniqueKernelBase(GetMagicName() + "_" + GetFunctionHash().GetHash()),用于 JSON 侧车 kernelName 与 _main 内核模板的符号名;eslmodel_launcher.cpp 的 LiteRegisterKernel 同步拼接函数内容哈希,使不同进程中结构相同、共享 magic name 的 kernel 获得互不冲突的入口符号/侧车名称。

  • 新增 pypto.extensions.torch_custom_op 扩展包:在 pyproject.toml 的 packages.find.include 注册 pypto.extensions 及其子包,pypto/__init__.py 将其编译契约以 pypto.torch_custom_op_compile 顶层别名暴露;common/authoring.py 提供 _is_jit_kernel、_is_factory_function、_factory_inner_jit_name、_detect_factory_takes 等 kernel= 形式识别与校验,common/source_utils.py 提供源码捕获/去装饰器/函数改名与基于 symtable 的全局加载作用域分析。

  • 定义运行时编译契约 CompileEntry:common/compile.py 实现抽象可调用 (shapes, dtypes, attrs, soc_version) -> 编译产物路径,支持 DIRECT(from_jit_kernel,经进程全局 set_codegen_options 设置 soc)与工厂(from_factory,按 single/lists/full 签名分派)两种 kernel 供给形式,含 infer_shape/infer_dtype 钩子、按 key 缓存、v3/v4 参数风格,以及声明注解一致性校验(shape 不匹配告警、dtype 不匹配抛错)。

  • 新增 JitCallableWrapper.build_kernel 编译入口并串行化构建:entry.py 定义进程级 _BUILD_LOCK(RLock),build_kernel(*args, **kwargs) 复用 _parse_call_args 解析参数、在 jit_scope 下编译,并从 LogTopFolder()/kernel_aicore 定位唯一 .o 文件返回其路径(数量非 1 时抛 RuntimeError)。

  • 新增共享样例与测试:新增 kernel_compile_samples.py 及多模块辅助包 sample_helper_pkg(含跨文件 helper/常量、同名冲突、PEP 563、函数内 import 等样例),配套 test_op_compile_selftest.py(mock 后端覆盖 CompileEntry/from_factory 各模式)、test_op_compile_warnings.py(声明注解不匹配守卫)、test_from_jit_kernel.py(DIRECT 真实构建)、test_declared_annotations.py、test_export_helpers.py,以及 test_kirin9030_kernel_entry_uniqueness.py 盒子测试(跨解释器验证入口名仅由内容哈希区分)。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 21 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 21 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Aug 21 Added label: cann-cla/yes
137 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 15 Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Sep 15 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 15 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 15 Added label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
Sep 15 Merged pull request