FuseMatMulAndAddPass Python Sample Usage Guide
【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge
This directory provides apure Pythonversion ofpattern_base_pass/1_fuse_matmul_add_pass, with logic consistent with C++FuseMatMulAndAddPass:
- Pattern 0:
MatMul(a, b)→Add(..., c), graph inputs0/1/2correspond toa/b/c - Pattern 1:
BatchMatMulV2(a, b)→Add(..., c), same three-input topology - Replacement:
GEMM(r_a, r_b, r_c, alpha=1, beta=1)(scalar1.0aligns with C++CreateScalar(1)) - Inherits
PatternFusionPass, implementspatterns()/meet_requirements()/replacement(); phase isBeforeInferShape
No additional matcher configuration likeenable_const_value_match()enabled, consistent with C++ default matching behavior.
Directory Structure
python/ ├── README.md // Python sample documentation ├── CMakeLists.txt // Build script for generating es_all Python ES API ├── src │ ├── python_fuse_matmul_add_pass.py // Python pass implementation filePrerequisites
- CANN environment variables configured via
source ${ASCEND_PATH}/set_env.sh, see C++ sample README environment configuration steps - GE Python package importable (includes
ge.passesand pass loading chain)
run package typically includesge_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. If execution reports missingBatchMatMulV2,GEMMetc. ES API, follow "ES API Missing Handling (Optional)" below to generate and loades_all.
Usage
Commands below default to execution in1_fuse_matmul_add_pass/pythondirectory.
- Set environment variable for GE to load this Python pass during compilation:
export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_fuse_matmul_add_pass.pyFollow C++ sample README program execution section for verification.
Notes:
- This sample is not a standalone execution script, directly running
python src/python_fuse_matmul_add_pass.pywon't trigger pass execution - If execution reports missing
BatchMatMulV2,GEMMetc. ES API, generate and loades_allfirst per below, then rerun
- This sample is not a standalone execution script, directly running
ES API Missing Handling (Optional)
If execution reports missingBatchMatMulV2,GEMMetc. ES API, generatees_allvia this Python directory'sCMakeLists.txt:
cmake -S . -B build cmake --build build --target build_es_all -j$(nproc)Install generated Python package and ensure current Python process can find the package and corresponding dynamic library:
pip install --force-reinstall --upgrade --target ./build/whl_package ./build/es_output/whl/es_all-1.0.0-py3-none-any.whl export PYTHONPATH="$PWD/build/whl_package:${PYTHONPATH:-}" export LD_LIBRARY_PATH="$PWD/build/es_output/lib64:${LD_LIBRARY_PATH:-}"Expected Behavior
Similar to C++ sample, when pattern/replacement invoked:
Define pattern for FuseMatMulAndAddPass Define replacement for FuseMatMulAndAddPass【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考