Sample Usage Guide
1. Function Description
This sample demonstrates how to use HcomAllGather and HcomReduceScatter collective communication operators 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_ep_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
- Install
toolkitandopspackages 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:
- Automatically generate ES interfaces
- Compile sample program
- Dump graph to current directory
- Run EP graph in parallel on 2 NPU devices (device ID automatically read from rank_table)
Note:
- Script will automatically identify hardware through
lspciand 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 modify
device_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 before bash run_sample.sh -t sample_and_run_python to 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 before bash run_sample.sh -t sample_and_run_python to 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 of
RANK_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. EP Graph Construction
Concept Explanation: EP (Expert Parallel) graph refers to graph structure running on multiple cards through expert parallel method. This sample demonstrates how to use ES operators to build EP graph containing collective communication operators, achieving inter-card data synchronization and parallel computation.
HcomAllGather operator prototype is shown below, ES graph construction generated API is HcomAllGather(), supporting use in Python
REG_OP(HcomAllGather)
.INPUT(x, TensorType({DT_FLOAT, DT_INT32, DT_INT8, DT_INT16, DT_FLOAT16, DT_BFLOAT16, DT_INT64, DT_UINT64,
DT_UINT8, DT_UINT16, DT_UINT32, DT_FLOAT64}))
.OUTPUT(y, TensorType({DT_FLOAT, DT_INT32, DT_INT8, DT_INT16, DT_FLOAT16, DT_BFLOAT16, DT_INT64, DT_UINT64,
DT_UINT8, DT_UINT16, DT_UINT32, DT_FLOAT64}))
.REQUIRED_ATTR(rank_size, Int)
.REQUIRED_ATTR(group, String)
.ATTR(fusion, Int, 0)
.ATTR(fusion_id, Int, -1)
.OP_END_FACTORY_REG(HcomAllGather)
Its corresponding function prototype is:
- Function name: HcomAllGather
- Parameters: 5 in total, in order: x, rank_size, group, fusion (optional, default 0), fusion_id (optional, default -1)
- Return value: output y
Python API:
HcomAllGather(x: TensorHolder, *, rank_size: int, group: str, fusion: int = 0, fusion_id: int = -1) -> TensorHolder
Note: rank_size and group are required keyword parameters, fusion and fusion_id are optional parameters with default values
HcomReduceScatter operator prototype is shown below, ES graph construction generated API is HcomReduceScatter(), supporting use in Python
REG_OP(HcomReduceScatter)
.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)
.ATTR(fusion, Int, 0)
.ATTR(fusion_id, Int, -1)
.REQUIRED_ATTR(group, String)
.REQUIRED_ATTR(rank_size, Int)
.OP_END_FACTORY_REG(HcomReduceScatter)
Its corresponding function prototype is:
- Function name: HcomReduceScatter
- Parameters: 6 in total, in order: x, reduction, group, rank_size, fusion (optional, default 0), fusion_id (optional, default -1)
- Return value: output y
Python API:
HcomReduceScatter(x: TensorHolder, *, reduction: str, group: str, rank_size: int, fusion: int = 0, fusion_id: int = -1) -> TensorHolder
Note: reduction, group and rank_size are required keyword parameters, fusion and fusion_id are optional parameters with default values