Running a Graph Synchronously

This section describes the main APIs involved and calling examples.

Overview

This feature is not supported by the Atlas 200/300/500 Inference Product .

This section describes how to build and run a constructed graph to generate a result. The following figure illustrates the API call sequence.

  1. GEInitialize: initializes the system and allocates resources. (This API can be called before graph construction.)
  2. Session constructor: creates a Session object and allocates Session resources.
  3. AddGraph: adds a graph to the Session object.
  4. RunGraph: runs the graph.
  5. GEFinalize: destroys system allocations.

Example

  1. Include the header file.
    1
    #include "ge_api.h"
    
  2. Allocate system resources.

    After a graph is defined, call GEInitialize to initialize the system (or call it before defining a graph) and allocate system resources. See the following code snippet.

    1
    2
    3
    std::map<AscendString, AscendString>config = {{"ge.exec.deviceId", "0"},
                                                  {"ge.graphRunMode", "1"}};
    Status ret = ge::GEInitialize(config);
    

    Set the GE initialization configuration by using config. Configure ge.exec.deviceId and ge.graphRunMode in the sample: one for specifying the device where a GE instance runs, and the other for specifying the graph run mode (set to 0 for online inference and 1 for training). For more configurations, see Command-Line Options.

  3. Add a graph object and run the graph.
    To run a graph, create a Session object, call the AddGraph API to add the graph, and then call the RunGraph API to run the graph. See the following code snippet.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    std::map <AscendString, AscendString> options;
    ge::Session *session = new Session(options);
    if(session == nullptr) {
      std::cout << "Create session failed." << std::endl;
      ...
      ...
    //Destroy allocations.
      ge::GEFinalize();  
      return FAILED;
    }
    uint32_t conv_graph_id = 0;
    ge::Graph conv_graph;
    Status ret = session->AddGraph(conv_graph_id, conv_graph);
    if(ret != SUCCESS) {
      ...
      ...
    //Destroy allocations.
      ge::GEFinalize();
      delete session;
      return FAILED;
    }
    std::vector<ge::Tensor> input_cov;
    std::vector<ge::Tensor> output_cov;
    ret = session->RunGraph(conv_graph_id, input_cov, output_cov);
    if(ret != SUCCESS) {
      ...
      ...
    //Destroy allocations.
      ge::GEFinalize();
      delete session;
      return FAILED;
    }
    

    Set the run configuration by using options. For details, see the Session constructor.

    The graph execution result will be saved to the output_cov tensor.

  4. Call GEFinalize to destroy the allocations.
    1
    ret = ge::GEFinalize();