尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
昇腾CANN/GE数据依赖形状自定义算子示例
Data Dependent Shape Custom Operator (Type III Operator) Sample【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/geSample OverviewGraph construction entry:GEOperator programming language:Ascend CCompilation method:.ascand.cppcompiled in same targetModel sinking capability:Not applicableCore pipeline:Ascend C kernel and host-side custom op compiled in same library - GE deliverables - In-process graph construction - Session::RunGraph - hybrid/RT2 dynamic executionDifference from other samples: This sample focuses on shape buffer usage in unknown graph dynamic execution scenarios for type III custom operators.This sample demonstrates a minimum runnable type III custom operator execution pipeline: after in-process graph construction, directly callSession::RunGraph, compile Ascend C kernel and host-side custom op to samelibwhere_like_custom_op.soby CMake, thenWhereLikeCustom::Execute()allocates maximum output and shape buffer, finally writes back actual output shape on device side.WhereLikeCustominput isbool[N], used to return indices of elements with valuetruein input, output isint64[true_count, rank]. Sincetrue_countdepends on runtime input data, only output upper bound can be determined at compile time, actual shape needs to be passed back through type III custom op shape buffer protocol after execution.Applicable ScenariosWant to see minimum implementation of type III custom operators inSession::RunGraphdynamic execution pipeline.Want to reference how user synchronizes stream, reads back shape buffer and updates output shape inExecute.Want to see complete process of.ascand.cppcompiled directly together and initiating kernel calls inExecute.Not suitable for understandingATC offline compilation - om offline modelsinking pipeline.PrerequisitesCANNCANN environment has been correctly installed and configured, e.g., executedsource ${ASCEND_HOME_PATH}/set_env.sh.Current environment hasACL,GE,Graph,Ascend Crelated headers and libraries.Follow installation guide to complete toolkit and ops package installation.Framework and PluginsThis sample does not depend on PyTorch, TensorFlow or TorchAir.Environment VariablesASCEND_HOME_PATHASCEND_CUSTOM_OPP_PATHwill be automatically appended as current samplesoutput/inrun.shAdditional DependenciescmakegQuick RunExecute inexamples/custom_op/data_dependent_shape_customdirectory:Recommended Methodsource ${ASCEND_HOME_PATH}/set_env.sh bash run.shrun.shwill automatically complete configure, build, install and appendoutput/toASCEND_CUSTOM_OPP_PATH. If successful, terminal will print:output shape: [4, 1] output values: 0 2 4 7Step-by-step Methodsource ${ASCEND_HOME_PATH}/set_env.sh cmake -S . -B build -DCMAKE_BUILD_TYPERelease cmake --build build -j$(nproc) cmake --install build export ASCEND_CUSTOM_OPP_PATH$(pwd)/output:$ASCEND_CUSTOM_OPP_PATH cd build ./data_dependent_shape_custom_session_run cd ..Whereexport ASCEND_CUSTOM_OPP_PATH$(pwd)/output:$ASCEND_CUSTOM_OPP_PATHis used to add custom operator package root directory to environment variable, then GE will load deliverables according tooutput/op_graph/lib/os/arch/libwhere_like_custom_op.sorule.Directory Structure and Key Filesdata_dependent_shape_custom ├── CMakeLists.txt ├── README.md ├── run.sh ├── ge │ ├── custom_op.cpp // EagerExecuteOp ShapeInferOp main process implementation │ ├── where_like_custom.h // WhereLikeCustom proto definition │ └── where_like_custom_kernel.asc // Ascend C kernel source code └── session_run └── main.cc // In-process graph construction and direct Session::RunGraph callKey files:ge/custom_op.cppCustom operator core main process, implementsExecute,InferShapeandInferDataType; whereExecuteis responsible for allocating maximum output, shape buffer, calling.ascexported launch wrapper, and reading back shape buffer to update output shape after kernel completion,InferShape/InferDataTypeis responsible for compile-time output shape / dtype derivation.ge/where_like_custom_kernel.ascDevice kernel and host-side launch wrapper implementation, responsible for writing output data and shape buffer.session_run/main.ccBuild minimum graph and execute directly viaSession::AddGraph Session::RunGraph.run.shChain configure, build, install and run process.Core Pipelinesession_run/main.ccbuilds minimum graph containingData - WhereLikeCustomand sets input/output description to dynamic shape, making the whole graph gounknown graphexecution pipeline.InferShape/InferDataTypeinge/custom_op.cppgives output shape / dtype at graph construction phase; this sample does not depend on framework lowering to insert shape write-back nodes.CMakeLists.txtcompilesge/custom_op.cppandge/where_like_custom_kernel.asctogether tolibwhere_like_custom_op.so.ge/custom_op.cppallocates output viactx-MallocOutputTensor(...)according to maximum shape inExecutecallback, then allocates shape buffer via firstctx-MallocWorkSpace(...).ge/where_like_custom_kernel.ascwrites output indices and shape buffer on device side, where shape buffer is used to pass back actual output shape.Executeinge/custom_op.cppsynchronizes current stream after launch, copies shape buffer back to host, parses real shape and updates output tensors logical shape and valid size.After execution,session_run/main.ccreads output tensor and prints actual shape and output values.Build Artifactsoutput/op_graph/lib/linux/x86_64/libwhere_like_custom_op.soCustom operator deliverables used by GE in Linux x86_64 environment; aarch64 environment corresponds tooutput/op_graph/lib/linux/aarch64/libwhere_like_custom_op.so.output/op_graph/include/where_like_custom.hOperator proto header file that can be directly used by graph construction side.build/data_dependent_shape_custom_session_runDirectSession::RunGraphexecution program.Result VerificationWhen successful, you can observe:output/op_graph/lib/os/arch/libwhere_like_custom_op.sohas been generated.output/op_graph/include/where_like_custom.hhas been generated.Terminal output containsoutput shape: [4, 1].Terminal output containsoutput values: 0 2 4 7.If failed, priority checks:WhetherASCEND_HOME_PATHhas been set and CANN environment has been correctlysourced.WhetherASCEND_CUSTOM_OPP_PATHalready contains current samplesoutput/.Whetheroutput/op_graph/lib/os/arch/libwhere_like_custom_op.soandoutput/op_graph/include/where_like_custom.hhave been generated.Whether current environment has available NPU and available Ascend C compilation environment.Notes / LimitationsWhereLikeCustomcurrent sample input is one-dimensionalbool[8], so actual output is matching position indices, shape is[true_count, 1]..asccompilation parameter currently fixed as--npu-archdav-2201, if target chip is different need to adjustCMakeLists.txt.AppendixOperator SpecificationItemContentOperator typeWhereLikeCustomInputxOutputyInput shapeNOutput shape upper bound[N, rank(x)]Output actual shape[true_count, rank(x)]Input data typeboolOutput data typeint64FormatNDKernel namewhere_like_customShape Buffer ConventionThis sample uses user-defined shape buffer protocol and user parses and writes back actual output shape inExecute. Current kernel will write:shape[0] 2Ushape[1] true_countshape[2] rankFor current one-dimensional input scenario,rank 1, so input[true, false, true, false, true, false, false, true]actual output shape is[4, 1], output data is0 2 4 7.【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED

相关推荐

CANN/ge SetAttrValue API文档

CANN/ge SetAttrValue API文档

SetAttrValue 【免费下载链接】ge GE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前…

📅 2026/11/23 22:53:18
Ruff Ty 类型检查器 while 循环类型收窄(Narrowing)机制深度解析

Ruff Ty 类型检查器 while 循环类型收窄(Narrowing)机制深度解析

Ruff Ty 类型检查器 while 循环类型收窄(Narrowing)机制深度解析 【免费下载链接】ruff An extremely fast Python linter and code formatter, written in Rust. 项目地址: https://gitcode.com/GitHub_Trending/ru/ruff while 循环是 Python 控…

📅 2026/11/23 22:53:16
在 Next.js 中集成 Mongoose:连接管理、模型注册与双路由体系实战指南

在 Next.js 中集成 Mongoose:连接管理、模型注册与双路由体系实战指南

在 Next.js 中集成 Mongoose:连接管理、模型注册与双路由体系实战指南 【免费下载链接】mongoose MongoDB object modeling designed to work in an asynchronous environment. 项目地址: https://gitcode.com/GitHub_Trending/mo/mongoose 本文是一份面向 N…

📅 2026/11/23 23:05:05
MORE NEWS

更多资讯

📰

嘉定区哪里考电工证在哪考?实操考试心里没底怕挂科怎么办?

嘉定区哪里考电工证在哪考?实操考试心里没底怕挂科怎么办? 你是不是也这样,一听说要考电工证,脑子里就闪过“实操考试心里没底怕挂科”这句台词?别急,今天咱就来聊聊 嘉定区哪里考电工证…

📰

荣成低压电工证考试怎么考?2026河南最新政策你必须知道

荣成低压电工证考试怎么考?2026河南最新政策你必须知道 找工作没证寸步难行,特种作业证就是你的敲门砖!尤其像低压电工证,不仅关乎你能不能上岗,还直接影响你未来的薪资和发展。2026年河南最新政策一出,很多正在备考的小伙伴都在问,荣成低压电…

📰

高级电工证掉了这么补 考前押题帮你搞定

高级电工证掉了这么补 考前押题帮你搞定 你是不是也担心自己学历不高,报不了电工证?别急,今天我就来给你讲清楚, 高级电工证掉了这么补 ,到底该怎么操作,还有 考前押题 怎么用,让你少走弯路,顺利拿到证。…

📰

深圳哪有高级电工证考怎么报名一次通过不踩坑

深圳哪有高级电工证考怎么报名一次通过不踩坑 怕考不过白交培训费?深圳哪有高级电工证考?很多人在报名前最大的担忧就是花了钱,最后却因为考试没过而白白浪费时间和金钱。尤其是电工证这类高门槛的特种作业证,考试难度不低,流程复杂,稍有不慎就可能“竹…

📰

高压电工作业错题库怎么用?郑州报考避坑指南帮你拿证更顺利

高压电工作业错题库怎么用?郑州报考避坑指南帮你拿证更顺利 证书过期了不知道怎么复审延期,这事儿真的让人头疼。尤其是像你这样在信阳浉河区从事建筑行业的工人,高压电工作业证是刚需,一旦过期,不仅影响工作,还可能面临停工甚至罚款。别急,我今天就带…

📰

电工证培训补助千万别踩坑:三门峡劳务班组怎么选培训和考证

电工证培训补助千万别踩坑:三门峡劳务班组怎么选培训和考证 你是不是正被“急需特种作业证才能上岗找工作”这句话压得喘不过气?别急,今天我就从三门峡劳务班组负责人的角度,手把手教你搞懂 电工证培训补助…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬