☰
CANN/ge ES图构建HCCL TP示例
2026/10/15 15:54:39 网站建设 项目流程

Sample Usage Guide

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

1. Function Description

This sample demonstrates how to use HcomAllReduce collective communication operator for graph construction, aimed at helping graph developers quickly understand collective communication definition and usage of this type of operators in graph construction.

2. Directory Structure

python/ ├── src/ | └── make_pfa_hcom_graph.py // sample file ├── rank_table/ | ├── a2/ | | └── rank_table_2p.json // A2(d802) 2-card rank table configuration (v1.0) ├── CMakeLists.txt // Build script ├── README.md // README file ├── run_sample.sh // Execution script

3. Usage Instructions

3.1. Prepare CANN Package

  • Installtoolkitandopspackages correctly following Environment Preparation
  • Set environment variables (assuming package is installed at /usr/local/Ascend/)
source /usr/local/Ascend/cann/set_env.sh

3.2. Build and Execute

Important Prerequisite: Ensure your system has at least 2 available NPU devices

Platform Support Description:

  • A2 Platform:lspci | grep d802has output, script automatically usesrank_table/a2/rank_table_2p.json
  • A5 Platform:lspci | grep d806has output, script will exit with error (this form is not supported)
  • Other platforms: Current version does not support, will exit with error directly in script

Note: Compared with C/C++ graph construction, Python graph construction requires additional LD_LIBRARY_PATH and PYTHONPATH settings (refer to configuration in sample)

Usage:

bash run_sample.sh -t sample_and_run_python

This command will:

  1. Automatically generate ES interfaces
  2. Compile sample program
  3. Dump graph to current directory
  4. Run TP graph in parallel on 2 NPU devices (device ID automatically read from rank_table)

Note:

  • Script will automatically identify hardware throughlspciand select corresponding rank table (currently only A2 usesrank_table/a2/rank_table_2p.json; A5 does not support this form)
  • If you need to use other devices (like 2,3 or 4,5), please modifydevice_idin rank table file under corresponding platform directory

After successful execution, you will see:

[Success] sample execution successful, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be opened in netron for display
Output File Description

After successful execution, the following files will be generated in current directory:

  • ge_onnx_*.pbtxt- protobuf text format of graph structure, can be viewed with netron

3.3. Log Printing

If you need log printing to assist debugging during executable program execution, you can set the following environment variables beforebash run_sample.sh -t sample_and_run_pythonto print logs to screen:

export ASCEND_SLOG_PRINT_TO_STDOUT=1 # Print logs to screen export ASCEND_GLOBAL_LOG_LEVEL=0 # Log level set to debug level

3.4. DUMP Graph During Graph Compilation Process

If you need to DUMP graph to assist debugging graph compilation process during executable program execution, you can set the following environment variables beforebash run_sample.sh -t sample_and_run_pythonto DUMP graph to execution path:

export DUMP_GE_GRAPH=2

4. Core Concepts Introduction

4.1. Graph Construction Steps

  • Create graph builder (provides context, workspace and construction-related methods needed for graph construction)
  • Add starting nodes (starting nodes refer to nodes without input dependencies, usually including graph inputs (like Data nodes) and weight constants (like Const nodes))
  • Add intermediate nodes (intermediate nodes are computation nodes with input dependencies, usually generated by user graph construction logic, and connected using existing nodes as inputs)
  • Set graph output (explicitly specify graph output nodes as computation result endpoints)

4.2. Multi-card Running Key Concepts

Environment Variable Description:When running multi-card sample, script will automatically set the following environment variables:

  • RANK_ID: Logical process number (0 or 1 in this sample)
  • DEVICE_ID: Physical device ID (0 or 1 in this sample)
  • RANK_TABLE_FILE: Rank table configuration file path (currently only A2:rank_table/a2/rank_table_2p.json; A5 does not support this form)
  • For detailed introduction ofRANK_TABLE_FILE,RANK_ID,DEVICE_ID, please refer to Example a2: rank table configuration resource information

GE Initialization Configuration:

config = { "ge.exec.deviceId": str(device_id), # From environment variable DEVICE_ID "ge.graphRunMode": "0", "ge.exec.rankTableFile": rank_table_file, # From environment variable RANK_TABLE_FILE "ge.exec.rankId": rank_id # From environment variable RANK_ID }

4.3. TP Graph Construction

Concept Explanation:TP (Tensor Parallel) graph refers to graph structure running on multiple cards through tensor parallel method. This sample demonstrates how to use ES operators to build TP graph containing collective communication operators, achieving inter-card data synchronization and parallel computation.

HcomAllReduce operator prototype is shown below, ES graph construction generated API isHcomAllReduce(), supporting use in Python

REG_OP(HcomAllReduce) .INPUT(x, TensorType({DT_FLOAT, DT_INT32, DT_INT8, DT_INT16, DT_FLOAT16, DT_INT64})) .OUTPUT(y, TensorType({DT_FLOAT, DT_INT32, DT_INT8, DT_INT16, DT_FLOAT16, DT_INT64})) .REQUIRED_ATTR(reduction, String) .REQUIRED_ATTR(group, String) .ATTR(fusion, Int, 1) .ATTR(fusion_id, Int, -1) .OP_END_FACTORY_REG(HcomAllReduce)

Its corresponding function prototype is:

  • Function name: HcomAllReduce
  • Parameters: 5 in total, in order: x, reduction, group, fusion (optional, default 1), fusion_id (optional, default -1)
  • Return value: output y

Python API:

HcomAllReduce(x: TensorHolder, *, reduction: str, group: str, fusion: int = 1, fusion_id: int = -1) -> TensorHolder

Note: reduction and group are required keyword parameters, fusion and fusion_id are optional parameters with default values

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

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

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

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

立即咨询