CANN/GE:Ascend C自定义算子图集成示例

发布时间:2026/9/10 4:50:28
CANN/GE:Ascend C自定义算子图集成示例 Ascend C Operator Graph Integration Sample via TorchAir【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/geSample OverviewGraph construction entry:PyTorch TorchAirOperator programming language:Ascend CCompilation method:Pre-compiled/Direct invocationModel sinking capability:Not applicableCore pipeline:Ascend C kernel - GE deliverables - TorchAir graph integration - Graph mode executionDifference from other samples: This sample focuses onPyTorch TorchAirscenario, does not involveATC offline compilation - om offline modelmodel sinking, nor Triton kernel.This sample demonstrates how to register Ascend C custom operators and integrate them into GE graph viaTorchAir. Using Add operator as an example, the deliverable side completes GE graph integration execution viaEagerExecuteOp, while the Python side covers both eager invocation and graph mode compiled invocation.Applicable ScenariosWant to see the minimum graph integration pipeline for Ascend C custom operators inPyTorch TorchAirscenario.Want to reference the collaboration betweenTORCH_LIBRARYregistration, custom converter and GE custom operator.Want to verify numerical results of Ascend C kernel in eager and graph modes.PrerequisitesCANNFollow installation guide to complete toolkit and ops package installation.Framework and PluginsAlready installedPyTorch,torch_npuandTorchAir.TorchAirdependent Python version needs to meet its release requirements.Reference:Ascend Extension for PyTorchAscend Extension for PyTorch Ascend Community DocumentationEnvironment VariablesASCEND_HOME_PATHLD_LIBRARY_PATHand other CANN runtime related variablesAdditional Dependenciespip3 install expecttestQuick RunExecute inexamples/custom_op/ascendc_add_customdirectory:Recommended Methodmkdir -p build cd build cmake .. make -j$(nproc) export ASCEND_CUSTOM_OPP_PATH$(pwd):$ASCEND_CUSTOM_OPP_PATH python3 ../script/add_custom_test.pyIf the terminal showsRan 1 testandOK, the sample runs successfully.Step-by-step MethodFirst executecmake ..andmake -j$(nproc)to generatebuild/libcust_opapi.so.Then executeexport ASCEND_CUSTOM_OPP_PATH$(pwd):$ASCEND_CUSTOM_OPP_PATHto add the directory wherelibcust_opapi.sois located to environment variable.Finally executepython3 ../script/add_custom_test.pyto run eager and TorchAir graph mode tests.If you need to execute installation target, you can additionally runcmake --install build.Directory Structure and Key Filesascendc_add_custom ├── CMakeLists.txt ├── README.md ├── add_custom_kernel │ ├── add_custom.asc // Ascend C kernel implementation and PyTorch registration code │ ├── add_custom_kernel.h // kernel declaration │ └── custom_op.cpp // GE deliverables, implementing Execute graph integration logic └── script └── add_custom_test.py // Python test script, covering eager and graph modesKey files:add_custom_kernel/add_custom.ascAscend C Add kernel implementation file, also contains PyTorch custom operator registration logic.add_custom_kernel/custom_op.cppCustom operator GE deliverables, implementingExecutepath and completing GE graph integration.script/add_custom_test.pyPython test script, covering eager execution and TorchAir graph mode compiled execution.CMakeLists.txtCompileslibcust_opapi.soand configures PyTorch, TorchAir, CANN dependencies.Core PipelineImplement Ascend C Add kernel inadd_custom_kernel/add_custom.ascand complete PyTorch custom operator registration.ImplementEagerExecuteOpinadd_custom_kernel/custom_op.cppto register custom operator to GE.Loadlibcust_opapi.soviascript/add_custom_test.py, execute and verify results in eager and TorchAir graph modes.Build Artifactsbuild/libcust_opapi.soCustom operator deliverables used by GE/TorchAir.Result VerificationWhen successful, you can observe:Terminal output containsRan 1 test.Terminal output containsOK.NoNumerical values do not match within toleranceerror during testing.If failed, priority checks:WhetherASCEND_HOME_PATH,LD_LIBRARY_PATHand other environment variables are correct.Whethertorch,torch_npu,TorchAirversions match.Whetherbuild/libcust_opapi.sohas been successfully generated and can be loaded by Python script.Whether current environment has available NPU, script will directly verifytorch.npu.is_available().Notes / LimitationsSupported products:Atlas A2 training series products / Atlas A2 inference series products.Sample currently usesfloat168 x 2048input for result verification.script/add_custom_test.pydepends on available NPU environment, cannot pass in CPU environment.This sample demonstratesPyTorch TorchAirgraph integration path, does not involve model sinking to om offline model.AppendixOperator SpecificationItemContentOperator typeAddCustomInputx,yOutputzInput shape8 x 2048Output shape8 x 2048Data typefloat16FormatNDKernel nameadd_customOperator Implementation SupplementAscend C Add kernel execution process can be summarized in three steps:CopyIn: Move input from Global Memory to on-chip Local Memory.Compute: Perform addition operation on two LocalTensors.CopyOut: Move computation result back to Global Memory corresponding to output Tensor.Registration Mechanism SupplementTORCH_LIBRARY_FRAGMENTis used to define Python-side visible operator signatures.TORCH_LIBRARY_IMPLis used to bind operator implementation toPrivateUse1,XLA,Metaand otherDispatchKeys.torchair.register_fx_node_ge_converter(...)inscript/add_custom_test.pyis responsible for mapping PyTorch nodes to GE custom operatorAddCustom.【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询