昇腾CANN GE卷积格式转换Python融合通道示例
2026/9/10 3:04:25 网站建设 项目流程

ConvTransFormatPass Python Example Usage Guide

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

This directory provides apure Pythonversion example ofgraph_base_pass/3_modify_conv_data_format_pass, main flow identical to C++ConvTransFormatPass:

  • TraverseConv2D/Conv2DV2in graph, filter nodes withdata_format == NCHW;
  • Changedata_formattoNHWC;
  • BFS from Conv output, matchTransposewithperm == [0,2,3,1]and[0,3,1,2]in order, delete correspondingTransposeand perm constant output nodes and reconnect data edges.

This example inheritsFusionBasePassand overridesrun(), completes modification viaGraph.remove_edge/add_data_edge/remove_nodeandNode.set_attr,does not useSubgraphRewriter.

Differences from C++ Version

  1. Full Graph Rollback: C++ uses backup graph to restore whenSetAttror edge deletion fails. Current PythonGraphdoes not support full graph deep copy. This example throws exception on failure,does not guaranteesame atomic rollback semantics as C++.
  2. Reading Transpose perm: C++ usesGNode::GetInputConstData. Python side reads viavalueattribute ofConst/Constantnode at perm input (same as Const validation inpattern_base_pass/4_add_zero_pass). If perm in graph does not appear in this form, may not recognize and deleteTranspose, coverage may differ slightly from C++.

Directory Structure

python/ ├── README.md // Python example description ├── CMakeLists.txt // Build script for generating es_all Python ES API ├── src │ ├── python_modify_conv_data_format_pass.py // Python pass implementation file

Prerequisites

  • Completed CANN environment variable setup viasource ${ASCEND_PATH}/set_env.sh. For more guidance, refer to C++ Example README environment variable configuration step
  • Can import GE Python package (containsge.graph,ge.passesand pass loading chain)

Python pass runtime loads precompiled binary components based onpybind11. CANN package prioritizes providing artifacts matching current Python version; if no matching artifact, automatically enters fallback compilation flow. Fallback compilation requirespybind11installed in current Python environment.

Usage

  1. Let GE load this Python pass during compilation via environment variable (when in3_modify_conv_data_format_passdirectory):

    export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_modify_conv_data_format_pass.py
  2. Refer to C++ Example README program execution chapter for validation.

Expected Result

Similar log output:

PythonConvTransFormatPass is starting Remove output edges success Remove output edges success PythonConvTransFormatPass completed

When comparing pbtxt exported byDUMP_GE_GRAPH, should see Convdata_formatisNHWCand targetTransposeremoved (consistent with C++ example description).

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询