新闻详情

新闻详情

首页 / 资讯中心 / 详情

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

发布时间:2026/9/10 4:41:24来源:尧图网络
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),仅供参考
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

2026茶艺师自学Go编程实战|从零开发抖音直播拣货工具降本增效 2026/9/10 5:26:30

2026茶艺师自学Go编程实战|从零开发抖音直播拣货工具降本增效

非技术岗位完全可以通过自学编程解决职场实操痛点,无需专业编程背景、无需大额投入,结合岗位场景轻量化开发,就能实现流程自动化、大幅降低人工内耗。本文以一线茶艺师跨界编程的真实落地案例,完整拆解从职场痛点挖掘、零基础开发…

阅读更多 →
E4980A精密LCR表深度解析:原理详解到校准实操与故障排除 2026/9/10 5:26:30

E4980A精密LCR表深度解析:原理详解到校准实操与故障排除

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

阅读更多 →
轻量黄金看盘工具实测:低占用多窗口高效盯盘 2026/9/10 5:26:30

轻量黄金看盘工具实测:低占用多窗口高效盯盘

打开电脑第一件事,就是把它挂上:一款轻量黄金看盘工具的真实使用记录 做黄金交易的朋友应该都有同一种体验:行情波动最激烈的时候,往往是下午到凌晨这段时间。手机端虽然方便,但屏幕一亮一暗之间很容易错过突破信号&am…

阅读更多 →
2026内容创作持续产出方法论:借鉴DevOps三步法稳定输出高质量内容 2026/9/10 5:26:30

2026内容创作持续产出方法论:借鉴DevOps三步法稳定输出高质量内容

想要长期做好内容创作,核心痛点从来不是缺乏灵感,而是产出不稳定、质量参差不齐、量产就丢风格。套用通用写作模板只会产出呆板同质化内容,随性创作又会频繁断更、质量失控。2026年高效可持续的创作核心解法,是借鉴软件开发DevOps…

阅读更多 →
Hugging Face被英伟达收购背后的中立性技术真相 2026/9/10 5:26:30

Hugging Face被英伟达收购背后的中立性技术真相

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

阅读更多 →
Better Auth 发布说明 AI 修复流水线:深入解析 repair.prompt.md 的设计与工程实现 2026/9/10 5:23:29

Better Auth 发布说明 AI 修复流水线:深入解析 repair.prompt.md 的设计与工程实现

Better Auth 发布说明 AI 修复流水线:深入解析 repair.prompt.md 的设计与工程实现 【免费下载链接】better-auth The most comprehensive authentication framework 项目地址: https://gitcode.com/GitHub_Trending/be/better-auth better-auth 是面向 Type…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞