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

发布时间:2026/11/4 15:49:51

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/10/29 2:52:26

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

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

2026/10/28 15:30:52

如何在 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/11/5 12:29:32

Text to SQL系统第三层实现:复杂查询意图识别与优化

1. 项目概述Text to SQL系统作为自然语言处理与数据库技术交叉领域的重要应用,正在深刻改变着非技术人员与数据库交互的方式。这个看似简单的"输入问题,输出SQL"功能背后,隐藏着复杂的实现逻辑和技术挑战。今天我们就来拆解这个系统…

2026/11/5 12:29:32

Text-to-SQL系统架构解析与性能优化实践

1. 文本到SQL转换系统的核心价值 在数据驱动的时代,SQL作为与数据库交互的标准语言,其重要性不言而喻。但编写SQL查询需要专业技能,这成为了许多非技术用户获取数据的障碍。Text-to-SQL系统正是为解决这一痛点而生,它允许用户用自…

2026/11/5 12:29:32

钉钉办公自动化:OpenClaw Skill原理与应用

1. 项目概述 OpenClaw Skill是一个基于自动化原理设计的办公效率工具,专门针对钉钉平台的操作优化。它能模拟人工操作流程,实现消息自动收发、审批流程处理、考勤数据管理等高频办公场景的自动化。我在实际使用中发现,这个工具特别适合需要频…

2026/11/5 12:29:32

AI编程辅助工具:5个提升代码可用性的Prompt模板

1. 项目背景与核心价值 去年团队引入AI编程辅助工具时,我们经历了从"玩具demo"到"生产级应用"的艰难跨越。最深的体会是:需求沟通的质量直接决定了AI产出的可用性。那些模糊的"做个用户管理系统"之类的需求,AI…

2026/11/5 12:24:32

云原生智能体协同架构在城市轨道交通中的应用

1. 新一代城市轨道交通云原生智能体协同架构概述城市轨道交通作为现代都市的"大动脉",其运营效率和服务质量直接影响着千万市民的日常生活。近年来,随着云计算、人工智能等技术的快速发展,传统的轨道交通智能化系统架构正面临着前所…

2026/11/5 12:14:29

Stimulsoft Reports 2019.2.2 落地指南:老系统兼容与版本陷阱规避

简介:Stimulsoft Reports 2019.2.2 是一款面向.NET开发者的专业报表控件,适用于WinForms、ASP.NET及WPF平台,帮助开发者快速构建数据可视化报表与打印输出功能,尤其适合中高级C#工程师在企业级管理系统、BI看板或财务报表模块中集…

2026/11/4 12:38:23

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/4 21:04:25

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/5 0:02:07

多智能体协作系统架构设计与实践优化

1. 多智能体协作系统的时代背景2017年Transformer架构的诞生彻底改变了人工智能的发展轨迹。当行业还沉浸在单一大模型带来的性能突破时,Anthropic的研究团队已经将目光投向了更前沿的领域——如何让多个智能体通过协作产生超越单体能力的系统智能。这就像交响乐团中…

2026/11/5 0:02:07

大模型系统性入门实战指南:从原理到部署微调的全链路路线

提到大模型的系统性入门,我先说个结论:这事真不难,难的是资料太散、路线太多,今天这个框架明天那个工具,新人很容易在“收藏夹吃灰”和“教程看到一半放弃”之间反复横跳。市面上关于大模型的内容少说有几十万篇&#…

2026/11/5 0:02:07

鸿蒙文件系统沙箱机制解析与开发实践

1. 鸿蒙文件系统沙箱机制深度解析作为一名从Android转向鸿蒙开发的工程师,我深刻体会到文件系统访问是迁移过程中最先遇到的"拦路虎"。在Android上运行良好的文件读写代码,在鸿蒙真机环境却频频出现文件写入后神秘消失或直接抛出权限拒绝错误的…

2026/11/4 12:38:23

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/4 21:04:25

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

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

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

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