Sample Usage Guide
1. Function Description
This sample demonstrates graph construction using MatMul operator normal attributes, aimed at helping graph developers quickly understand normal attribute definition and usage.
2. Directory Structure
cpp/
├── src/
| └── CMakeLists.txt // CMake build file
| └── es_showcase.h // Header file
| └── make_matmul_graph.cpp // sample file
├── CMakeLists.txt // CMake build file
├── main.cpp // Program main entry
├── README.md // README file
├── run_sample.sh // Execution script
├── utils.h // Utility file
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
3.2.1 Generate ES Interfaces and Build Graph for DUMP
Simply run the following command to clean, generate interfaces, construct graph and DUMP graph:
bash run_sample.sh
Current run_sample.sh behavior: automatically clean old build, build sample and default execute sample dump. When you see the following message, it indicates successful execution:
[Success] sample execution successful, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be opened in netron for display
3.2.2 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.2.3 Build Graph and Execute
Besides basic graph construction and dump functionality, esb_sample supports building graph and actually executing computation.
bash run_sample.sh -t sample_and_run
This command will:
- Automatically generate ES interfaces
- Compile sample program
- Generate dump graph, run graph and output computation results
After successful execution, you will see:
[Success] sample_and_run execution successful, pbtxt and data output dump generated in current directory
You can view computation results through data file
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 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
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 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. Normal Attributes
Concept Explanation: Normal attributes are operator configuration parameters used to control operator behavior; attribute values are determined during graph construction and do not change at runtime.
Graph Construction API Features:
- Attribute values must be determined during graph construction, cannot be runtime variables
- If using default values, corresponding parameters can be omitted (use default values) or explicitly passed
- ES API will perform type checking based on attribute type, ensuring passed value types are correct
For example, MatMul operator prototype is shown below, ES graph construction provides API method MatMul(), ATTR attribute in method is whether to transpose matrix, supporting use in C++ and C
REG_OP(MatMul)
.INPUT(x1, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_BF16, DT_HIFLOAT8}))
.INPUT(x2, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_BF16, DT_HIFLOAT8}))
.OPTIONAL_INPUT(bias, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_BF16}))
.OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_BF16, DT_HIFLOAT8}))
.ATTR(transpose_x1, Bool, false)
.ATTR(transpose_x2, Bool, false)
.OP_END_FACTORY_REG(MatMul)
Its corresponding function prototype is:
- Function name: MatMul
- Parameters: 5 in total, in order: x1, x2, bias, transpose_x1, transpose_x2
- Return value: output y
C API:
EsCTensorHolder* EsMatMul(EsCTensorHolder *x1, EsCTensorHolder *x2, EsCTensorHolder *bias, bool transpose_x1, bool transpose_x2);
C++ API:
EsTensorHolder EsMatMul(const EsTensorLike &x1, const EsTensorLike &x2, const EsTensorLike &bias=nullptr, transpose_x1=false, transpose_x2=false);
Note: Use TensorLike type to express input, to support case where actual parameter can directly pass numeric values