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:
- Traverse
Conv2D/Conv2DV2in graph, filter nodes withdata_format == NCHW; - Change
data_formattoNHWC; - BFS from Conv output, match
Transposewithperm == [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
- Full Graph Rollback: C++ uses backup graph to restore when
SetAttror 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++. - Reading Transpose perm: C++ uses
GNode::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 filePrerequisites
- Completed CANN environment variable setup via
source ${ASCEND_PATH}/set_env.sh. For more guidance, refer to C++ Example README environment variable configuration step - Can import GE Python package (contains
ge.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
Let GE load this Python pass during compilation via environment variable (when in
3_modify_conv_data_format_passdirectory):export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_modify_conv_data_format_pass.pyRefer 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 completedWhen 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),仅供参考