Pull Request has been successfully merged by CANN-robot.
(Thank you for hid22900163's contribution.)变更摘要
本 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盒子测试(跨解释器验证入口名仅由内容哈希区分)。


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:
CompileEntry(jit_kernel=… | factory=…, num_inputs=, num_outputs=, factory_signature=), called asentry(shapes, dtypes, attrs, soc_version) -> compiled-kernel path, result cached per key. Construction accepts exactly one kernel form and rejects an unknownfactory_signatureor a non-positive input/output count; each call requiressoc_versionand checks the shape/dtype count againstnum_inputsinfer_shape/infer_dtypeare 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 dtypebuild_kernelthrough the frontend parser (single-object contract stated in its error), register the package in pyproject.tomlRelated Issues: #2869, #5158