Constructing a Graph Using ES
Overview
The process of constructing a graph using ES is as follows.

- Create a graph builder. Initialize the graph constructor instance to provide the context, workspace, and construction methods required for image construction.
- Add a start node. The start node refers to a node without input dependencies, typically including graph inputs (for example, the Data node) and weight constants (for example, the Const node).
- Add intermediate nodes. Intermediate nodes are compute nodes that have input dependencies. They are usually generated according to your graph construction logic and depend on the inputs from existing nodes.
- Set the graph output. Specify the output node of the graph as the end point of the computation result.
The graph construction process mainly involves two objects. The following uses C++ as an example:
- Graph: indicates the final computational graph, which is the target product of graph construction.
- EsGraphBuilder: facilitates graph construction by providing methods for adding and connecting nodes, setting properties, and recording the intermediate status. EsGraphBuilder exists only during the graph construction. It is used to carry intermediate construction information and is the object that is directly operated by the application during the construction. After the graph construction is complete, its internal status is encapsulated into a graph instance and returned. EsGraphBuilder and its related resources are released.
Constructing a Graph Instance
The following are examples of using C, C++, and Python to implement ES graph construction in the scenario of calculating the sum of two inputs:
- C code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "es_math_ops_c.h" // Aggregation header file where the Add operator is located, including the header file set of all operators and basic structures such as the graph builder. // 1. Create a graph builder (EsCGraphBuilder). EsCGraphBuilder *builder = EsCreateGraphBuilder("graph_name"); // 2. Add a start node. EsCTensorHolder *data0 = EsCreateGraphInput(builder, 0); // Add the first input node. EsCTensorHolder *data1 = EsCreateInput(builder, 1); // Add the second input node. // 3. Add an intermediate node. EsCTensorHolder *add = EsAdd(data0, data1); // Add addition calculation nodes (explicitly passing the builder is no longer required). // 4. Set the graph output. EsSetOutput(add, 0); // Set the add node to the first output of the graph. // 5. Complete the graph construction and return the final graph object. EsGraph *graph = EsBuildGraphAndReset(builder); // Obtain the constructed graph. // 6. Release the builder and its managed process resources. EsDestroyGraphBuilder(builder);
- CPP code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "es_math_ops.h" // Aggregation header file where the Add operator is located, including the header file set of all operators and the header files of basic structures such as the graph builder. namespace ge { namespace es { //1. Create a graph builder (EsGraphBuilder). EsGraphBuilder builder("graph_name"); // 2. Add two input nodes. EsTensorHolder [data0, data1] = builder.CreateInputs<2>(); // 3. Add an intermediate node. In C++, common operators such as addition, subtraction, multiplication, and division have been overloaded for direct use. EsTensorHolder add = data0 + data1; // 4. Set the graph output. builder.SetOutput(add, 0); // 5. Build a graph and obtain the built graph object. The resources in the builder are destroyed along with the destructor. std::unique_ptr<ge::Graph> graph = builder.BuildAndReset(); } }
- Python code example:
Before using Python code to construct a graph, you need to install the .whl package generated in (Optional) Generating ES Graph Construction APIs. The following is an installation example:
1pip3 install {OUTPUT_PATH}/whl/es_math-1.0.0-py3-none-any.whl
The sample code for graph construction is as follows:1 2 3 4 5 6 7 8 9 10 11 12
from ge.es import GraphBuilder from ge.es.math import Add # 1. Create a graph builder (EsBuilder). builder = GraphBuilder("graph_name") # 2. Add two input nodes. data0, data1 = builder.create_inputs(2) # 3. Add an intermediate node. In Python, common operators such as addition, subtraction, multiplication, and division have been overloaded for direct use. add = data0 + data1 # 4. Set the graph output. builder.set_output(add, 0) # 5. Complete the graph construction and return the final graph object. graph = builder.build_and_reset()
The source repository also provides the following ES graph construction samples for reference in various scenarios:
Parent topic: Using the ES for Simplified Graph Construction