CANN/ge融合Pass捕获张量示例

发布时间:2026/9/10 4:41:26

CANN/ge融合Pass捕获张量示例 Sample Usage Guide【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/geFeature DescriptionThis sample demonstrates capture tensor functionality using MatMulAdd fusion to GEMM pass, providing two verification methods: online inference and ATC offline model compilation. The pass uses eager style API and fusion interface.Directory Structure├── src │ ├──fuse_matmul_add_pass.cpp // pass implementation file ├── CMakeLists.txt // build script ├── data | ├──torch_gen_onnx.py // torch script for exporting onnx | ├──torch_forward_1.py // torch script for online inference, pass executed successfully | ├──torch_forward_2.py // torch script for online inference, pass blocked |—— gen_es_api | |——CMakeLists.txt // build script for generating eager style apiEnvironment RequirementsCompiler: GCC 7.3.xPython and dependencies: python3.9, pytorch2.1Environment preparation completed.Implementation StepsDefine classFuseMatMulAndAddPassinheriting fromPatternFusionPass.Override three functions from base classPatternFusionPass:Patternsdefines matching templates for identifying topologies matching the template in the graph.pattern-CaptureTensor()captures tensor, tensor structure:{NodeIo,index}.MeetRequirementsfilters topologies matched by template.match_result-GetCapturedTensor(kAddCaptureIdx,add_node);reads captured NodeIo for checking.Replacementdefines replacement part.match_result-GetCapturedTensor(kMatMulCaptureIdx, matmul_node);reads captured NodeIo to extract attribute values.RegisterFuseMatMulAndAddPassas custom fusion pass with execution phase BeforeInferShape.Program CompilationConfigure environment variables.Run environment setup script from software package:source ${ASCEND_PATH}/set_env.sh${ASCEND_PATH}is cann path under CANN software package installation directory. Replace with actual installation path, e.g.,${INSTALL_PATH}/cann.ModifyCMakeLists.txtas needed.ASCEND_PATH: Default software package path. If$ASCEND_HOME_PATHset via set_env.sh, no modification needed.target_include_directories: Required header files. For this sample, no modification needed. For custom development, add header files below the example without deleting existing items. If network has custom operators, add custom operator prototype definition headers.target_link_libraries: Required libraries. For this sample, no modification needed. For custom development, add libraries below the example without deleting existing items.Do not link other SOs from software package to avoid compatibility issues during future upgrades.Execute sequentially:mkdir build cd build cmake ..Run make to compile custom pass so, then install dynamic library libfuse_matmul_add_for_capture_tensor_sample_pass.so to custom fusion pass directory via make install. Optional parameter-j$(nproc)can be added after make for parallel build tasks,$(nproc)dynamically gets CPU core count.make -j$(nproc) fuse_matmul_add_for_capture_tensor_sample_pass make installAfter sample verification, run the following command to clean custom pass so installed under CANN package to avoid affecting subsequent UT/ST:make clean_custom_passProgram ExecutionConfigure environment variables (if already done, skip).Run environment setup script:source ${ASCEND_PATH}/set_env.shReplace${ASCEND_PATH}with actual software package installation path.Use ATC offline inference.Set environment variable to dump model graph during compilation:export DUMP_GE_GRAPH1Enter data directory and execute .py file to export onnx (uses torch onnx exporter, depends on additional Python package onnx, ensure installed before running. ATC tool currently supports onnx opset_version up to 18, if torch exports higher version by default, specify explicitly, see script comments):python torch_gen_onnx.pyAfter execution, .onnx format model file named model.onnx generated in data directory.Execute ATC tool command (for detailed ATC tool instructions, visit Ascend Documentation and search for ATC Offline Model Compilation Tool), modifysoc_versionbased on actual environment:atc --model./model.onnx --framework5 --soc_versionxxx --output./modelLog shows:Define pattern for FuseMatMulAndAddPass in capture tensor sample Define MeetRequirements for FuseMatMulAndAddPass in capture tensor sample Define replacement for FuseMatMulAndAddPass in capture tensor sampleOnline inferenceSet environment variable to dump model graph during compilation:export DUMP_GE_GRAPH1Enter data directory and execute .py file for online inference (ensure torch_npu plugin installed for online inference), executetorch_forward_1.py:python torch_forward_1.pyFor torch_forward_1.py, log shows:Define pattern for FuseMatMulAndAddPass in capture tensor sample Define MeetRequirements for FuseMatMulAndAddPass in capture tensor sample Define replacement for FuseMatMulAndAddPass in capture tensor sampleExecutetorch_forward_2.py:python torch_forward_2.pyFor torch_forward_2.py, log shows:Define pattern for FuseMatMulAndAddPass in capture tensor sample Define MeetRequirements for FuseMatMulAndAddPass in capture tensor sample Only support Add inputs are fp32View resultsAfter execution, series of .pbtxt files generated in directory. Compare the following dump graphs:ge_onnx_xxxxx_PreRunBegin.pbtxtdump graph before executionge_onnx_xxxxx_RunCustomPassBeforeInferShape.pbtxtcustom pass dump graph before InferShape executionModel optimized as expected, i.e., MatMul and Add replaced by GEMM.If results not as expected, set following environment variables (for atc command, also add parameter--logdebug) to print logs to screen for troubleshooting.export ASCEND_SLOG_PRINT_TO_STDOUT1 #print logs to screen export ASCEND_GLOBAL_LOG_LEVEL0 #log level debug【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
延伸阅读

更多相关文章

2026/9/10 4:41:26

AB编码器测速全解析:原理、定时器配置与工程实战

作为一个常年跟电机、小车、自动化设备打交道的嵌入式工程师,我对 AB 编码器测速这个需求再熟悉不过了。不管你是做平衡车、AGV、机械臂关节还是简单的循迹小车,只要涉及到闭环控制,速度反馈就绕不开编码器。而增量式 AB 编码器,基…

2026/9/10 4:36:26

如何在 ESP-IDF 中快速获取 WiFi TSF 时间戳:一份完整指南

如何在 ESP-IDF 中快速获取 WiFi TSF 时间戳:一份完整指南 【免费下载链接】esp-idf Espressif IoT Development Framework. Official development framework for Espressif SoCs. 项目地址: https://gitcode.com/GitHub_Trending/es/esp-idf 想在 ESP-IDF 项…

2026/9/10 5:41:32

CANN/ge GE图引擎设置符号形状API

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

2026/9/10 5:41:32

Arm-2D静态工程评测:嵌入式GUI落地前的关键可行性验证

1. 项目概述:为什么一个静态工程评测能决定嵌入式GUI项目的生死? Arm-2D 是 ARM 官方开源的、专为 Cortex-M 系列微控制器设计的轻量级 2D 图形加速库。它不是那种“跑个 demo 就完事”的玩具库,而是真正面向量产级嵌入式设备——比如智能手表…

2026/9/10 5:41:32

MicroPython轻量日志模块uLogLite设计与实战

1. 为什么 MicroPython 项目里,日志不能只是 print? 在 MicroPython 项目里,我见过太多人把 print("debug: x", x) 当成日志——直到某天设备在野外连续跑三天后突然卡死,串口连上去只看到一堆乱序的 "led on&q…

2026/9/10 5:36:31

RPA高级认证B卷:影刀与Alien RPA工程实战能力深度解析

简介:本资源为RPA高级认证最新B卷标准答案解析资料,面向正在备考RPA专业认证的技术人员、自动化工程师及企业流程优化从业者,旨在帮助考生精准把握考试重点、厘清高阶考点逻辑、提升应试策略与实操能力。压缩包共39.32MB,虽未提供…

2026/9/9 13:11:35

超人会飞不算本事:系统稳定依赖清晰规则与边界设计

开头先不绕弯子。“#斯坦李吐槽dc 所以超人是无缘无故会飞的嘛哈哈哈哈哈哈哈锤哥真是技术人才啊!#雷神 #复联”这类调侃式短标题,第一波冲击力在于它把两个宇宙的角色塞进同一个吐槽箱里,但细想一下就能发现,它真正碰到的根本不是…

2026/9/8 7:15:15

超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论

把“蜘蛛侠 vs 超人”放在 CSDN 上聊,可能很多人第一反应是走错片场了。但如果把这两个角色看成“两个持续运营了 80 多年的文化产品”,你会发现,这场比较本质上是两个不同 IP 策略的长期结果对比:超人赢在定义了整个超级英雄题材…

2026/9/9 16:31:09

基于CNN的调制信号识别:MATLAB实现时频图分类实战

简介:本资源是一套面向通信工程与信号处理方向学习者、研究者的深度学习实践方案,聚焦调制信号自动检测与识别这一典型无线通信任务,解决传统方法依赖人工特征、低信噪比下性能下降等痛点。压缩包共12个文件(10.73MB)&…

2026/9/10 0:00:55

目录对比去重实战:用哈希算法精准清理重复文件

我电脑里现在还有一块换了三次机的“数据墓地”硬盘,里面存着2016年以前所有旧笔记本的完整备份。平时不觉得有什么,直到前阵子想把它整理归档,发现同一个安装包、同一批照片、同一份论文草稿,在几个不同的备份目录里反复出现。更…

2026/9/10 0:00:55

Leaflet离线地图完整Demo合集:内网部署与坐标纠偏实战

简介:这是一份面向Web GIS开发者的LeafLet离线地图示例合集,帮助开发者快速掌握离线地图从搭建到交互的完整流程。压缩包共723个文件,大小14.06MB,以319个js脚本、175个html页面和29个css样式文件为主体,配合png/svg图…

2026/9/10 0:00:55

MATLAB读取Rinex 3.02观测文件:多系统GNSS数据解析实战

简介:基于MATLAB开发的Rinex3.02版观测文件(o文件)读取代码包,面向卫星定位导航方向的学习者与研究人员,用于解决新版观测文件的数据解析、历元提取与时间转换问题。压缩包共4个文件,包含两个m脚本、一个19…

2026/9/7 16:23:03

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/7 22:46:00

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/9 10:21:54

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码