AddZeroPass Python Sample Usage Guide【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/geFeature DescriptionThis directory provides two pure PythonPatternFusionPasssamples:src/python_add_zero_pass_const_value_match.pysrc/python_add_zero_pass.pyThe two samples have different purposes:python_add_zero_pass_const_value_match.pyDemonstrates Python V1PatternMatcherConfigBuilder.enable_const_value_match()Front-loadsAdd(x, 0.0f)to matcher phase via strict const-value-matchpatternmethod uses Python expression to describe strictly matchedAdd(x, 0.0f), e.g.,return inputs[0] 0.0CurrentConstantMatcher::IsMatchvalue matching is strict binary match without floating-point tolerance or cross-dtype normalizationBetter suited as matcher_config example than fully equivalent version of C samplepython_add_zero_pass.pyMain logic aligned with C pass samplepatternmethod uses Python expression to describeData Consttopology, e.g.,return inputs[0] 0Multi-input pattern can explicitly declare input count viax, y, z inputs[:3]Multi-pattern pass can declare multiplepatternmethods; legacypatterns(self)returning multiplePattern/Graphstill compatiblepatternmethod cannot be used simultaneously withpatterns(self)to avoid ambiguous pattern declaration sourcesPython pass framework automatically createsGraphBuilder, sets graph outputs, and auto-captures in visited inputs,returnoutputs order; legacy explicitGraphBuildersyntax still compatiblemeet_requirements()explicitly reads matchedConst.valueand judges zero value per same rules as C sampleCurrently supportsDT_FLOAT,DT_DOUBLE,DT_INT32consistent with C sampleRecommended 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 Structurepython/ ├── 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 examplePrerequisitesCANN environment variables configured viasource ${ASCEND_PATH}/set_env.sh, see C sample README environment configuration stepsCANN software package installation per Environment preparationGraph compilation flow Python dependencies installed:attrs,decorator,sympy,numpy,psutil,scipyrun 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.UsageSet 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, replaceatccommand 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 runningpython src/python_add_zero_pass.pyorpython src/python_add_zero_pass_const_value_match.pywont trigger pass executionExpected output prints after GE compilation flow actually loads this Python passExpected LogsAfter successful execution, logs show similar output (actual input_0 name may differ):[PythonAddZeroConstValueMatchPass] matchedPythonAddZeroConstValueMatchPass_add_zero_pattern capturedinput_0:0 [PythonAddZeroPass] matchedPythonAddZeroPass_add_zero_pattern capturedinput_0:0 const_dtypeDT_FLOAT zeroTrue【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考