AddZeroPass Python Sample Usage Guide
【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge
Feature Description
This directory provides two pure PythonPatternFusionPasssamples:
- src/python_add_zero_pass_const_value_match.py
- src/python_add_zero_pass.py
The two samples have different purposes:
python_add_zero_pass_const_value_match.py- Demonstrates Python V1
PatternMatcherConfigBuilder.enable_const_value_match() - Front-loads
Add(x, 0.0f)to matcher phase via strict const-value-match @patternmethod uses Python expression to describe strictly matchedAdd(x, 0.0f), e.g.,return inputs[0] + 0.0- Current
ConstantMatcher::IsMatchvalue matching is strict binary match without floating-point tolerance or cross-dtype normalization - Better suited as matcher_config example than fully equivalent version of C++ sample
- Demonstrates Python V1
python_add_zero_pass.py- Main logic aligned with C++ pass sample
@patternmethod uses Python expression to describeData + Consttopology, e.g.,return inputs[0] + 0- Multi-input pattern can explicitly declare input count via
x, y, z = inputs[:3] - Multi-pattern pass can declare multiple
@patternmethods; legacypatterns(self)returning multiplePattern/Graphstill compatible @patternmethod cannot be used simultaneously withpatterns(self)to avoid ambiguous pattern declaration sources- Python pass framework automatically creates
GraphBuilder, sets graph outputs, and auto-captures in "visited inputs,returnoutputs" order; legacy explicitGraphBuildersyntax still compatible meet_requirements()explicitly reads matchedConst.valueand judges zero value per same rules as C++ sample- Currently supports
DT_FLOAT,DT_DOUBLE,DT_INT32consistent with C++ sample
Recommended expression pattern syntax:
from ge.passes import PatternFusionPass, pattern class PythonAddZeroPass(PatternFusionPass): @pattern def add_zero(self, inputs): return inputs[0] + 0 def replacement(self, inputs): return inputs[0]Directory Structure
python/ ├── README.md // Python sample documentation ├── CMakeLists.txt // Build script for generating es_all Python ES API ├── src │ ├── python_add_zero_pass.py // Python pass implementation file │ ├── python_add_zero_pass_const_value_match.py // const value match examplePrerequisites
- CANN environment variables configured via
source ${ASCEND_PATH}/set_env.sh, see C++ sample README environment configuration steps - CANN software package installation per Environment preparation
- Graph compilation flow Python dependencies installed:
attrs,decorator,sympy,numpy,psutil,scipy
run package includes GE Python runtimege_pywheel, no need to install separatege_py-*.whl. Python pass runtime loadspybind11-based precompiled binary components. CANN package provides artifacts matching current Python version; if no match, fallback compilation occurs automatically. Fallback compilation requirespybind11installed in current Python environment.
Usage
Set Python pass plugin path:
export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_add_zero_pass_const_value_match.pyFor version aligned with C++ main logic:
export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_add_zero_pass.pyFollow C++ sample README program execution section for verification. For offline scenarios, replace
atccommand in C++ README withpyatc; both have identical command-line parameters,pyatcruns in current Python interpreter process.Notes:
- These two samples are not standalone execution scripts, directly running
python src/python_add_zero_pass.pyorpython src/python_add_zero_pass_const_value_match.pywon't trigger pass execution - Expected output prints after GE compilation flow actually loads this Python pass
- These two samples are not standalone execution scripts, directly running
Expected Logs
After successful execution, logs show similar output (actual input_0 name may differ):
[PythonAddZeroConstValueMatchPass] matched=PythonAddZeroConstValueMatchPass_add_zero_pattern captured=input_0:0 [PythonAddZeroPass] matched=PythonAddZeroPass_add_zero_pattern captured=input_0:0 const_dtype=DT_FLOAT zero=True【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考