Sample Usage Guide

1. Functional Description

This sample uses dynamic input operators for graph construction, aiming to help graph construction developers quickly understand the definition and usage of dynamic input

2. Directory Structure

python/
├── src/
|   └── make_concatv2_graph.py    // sample file
├── CMakeLists.txt                // Compilation script
├── README.md                     // README file
├── run_sample.sh                 // Execution script

3. Usage

3.1. Prepare CANN Package

  • Correctly install toolkit and ops packages through installation guide Environment Preparation
  • Set environment variables (assuming packages are installed in /usr/local/Ascend/)
source /usr/local/Ascend/cann/set_env.sh

3.2. Compilation and Execution

  • Note: Compared with C/C++ graph construction, Python graph construction requires additional LD_LIBRARY_PATH and PYTHONPATH (refer to sample configuration)
bash run_sample.sh -t sample_and_run_python

This command will:

  1. Automatically generate ES interfaces
  2. Compile sample program
  3. Generate dump graph and run the graph

After successful execution you will see:

[Success] sample executed successfully, pbtxt dump generated in current directory. This 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 the current directory:

  • ge_onnx_*.pbtxt - Graph structure protobuf text format, 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 is debug level

3.4. DUMP Graph During Graph Compilation Process

During executable program execution, if you need to DUMP graph to assist debugging graph compilation process, you can set the following environment variable before bash run_sample.sh -t sample_and_run_python to DUMP graph to execution path

export DUMP_GE_GRAPH=2

4. Core Concept Introduction

4.1. Graph Construction Steps

  • Create graph builder (used to provide context, workspace and construction-related methods needed for graph construction)
  • Add start nodes (start 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 through existing nodes as inputs)
  • Set graph output (explicitly specify graph output nodes as endpoints of computation results)

4.2. Dynamic Input

Concept Description: Dynamic input refers to operators whose input count is not fixed; for example, ConcatV2 operator, which is a dynamic multi-input operator

The prototype of ConcatV2 operator is shown below, the ES graph construction generated API is ConcatV2(), supports usage in Python

    REG_OP(ConcatV2)
      .DYNAMIC_INPUT(x, TensorType({BasicType(), DT_BOOL, DT_STRING}))
      .INPUT(concat_dim, TensorType::IndexNumberType())
      .OUTPUT(y, TensorType({BasicType(), DT_BOOL, DT_STRING}))
      .ATTR(N, Int, 1)
      .OP_END_FACTORY_REG(ConcatV2)

Its corresponding function prototype is:

  • Function name: ConcatV2
  • Parameters: 3 in total, namely x, concat_dim, N
  • Return value: output y

In Python API:

ConcatV2(x: List[Union[TensorHolder, TensorLike]], concat_dim: Union[TensorHolder, TensorLike], *, N: int =1) -> TensorHolder:

Note: 1. Use TensorLike type to express input, to support the case where actual parameters can directly pass values