Graph Build to an .om Offline Model

This section describes the APIs and calling examples for building a graph into an OM offline model.

Overview

The following figure illustrates the API call sequence for building a constructed graph into an offline model.

  1. aclgrphBuildInitialize: initializes the system and allocates resources after a graph is defined.
  2. aclgrphBuildModel: builds a graph into an offline model adapted to the Ascend AI Processor, during which the built-in OPP and custom OPP are loaded. The model is stored in the memory buffer.
  3. (Optional) aclgrphSaveModel: serializes the offline model in the memory buffer to an offline model file (*.om).
  4. aclgrphBuildFinalize: ends the process and destroys allocations.

The APIs for model building and saving can be called repeatedly in a process, enabling building and saving of multiple offline models.

Example

  1. Include the header files.
    1
    2
    #include "ge_ir_build.h" 
    #include "ge_api_types.h"
    
  2. Allocate resources.

    After constructing a graph, call aclgrphBuildInitialize to initialize the system and allocate resources. See the following code snippet.

    1
    2
    3
    4
    std::map<AscendString, AscendString> global_options = {
            {ge::ir_option::SOC_VERSION, "${soc_version}"},
        };
    auto status = aclgrphBuildInitialize(global_options);
    

    Pass the global_options parameter to set the build configuration for offline model generation. For details about the supported parameters, see aclgrphBuildInitialize Configuration Parameters.

    If Ascend AI Processor does not exist in the current environment, SOC_VERSION is a required parameter used to specify the target SoC version. Replace ${soc_version} with the actual value. Set the rest parameters as required.

  3. Call aclgrphBuildModel to build the graph into an offline model. See the following code snippet.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    ModelBufferData model;
    std::map<AscendString, AscendString> options;
    PrepareOptions(options);
    
    ge::Graph graph;
    status = aclgrphBuildModel(graph, options, model);
    if(status == GRAPH_SUCCESS) {
        cout << "Build Model SUCCESS!" << endl;
    }
    else {
        cout << "Build Model Failed!" << endl;
    }
    

    Pass the options parameter to set the build configuration for offline model generation. For details about the supported parameters, see aclgrphBuildModel Configuration Parameters. Configuration example:

    1
    2
    3
    4
    5
    void PrepareOptions(std::map<AscendString, AscendString>& options) {
        options.insert({
            {ge::ir_option::EXEC_DISABLE_REUSED_MEMORY, "1"} // close reuse memory
            });
    }
    

    When the aclgrphBuildModel API is used to pass the options parameter in the multi-graph scenario, and the input parameter is ge::ir_option::PRECISION_MODE or ge::ir_option::PRECISION_MODE_V2, the parameter values of these graphs must be the same.

  4. (Optional) Call aclgrphSaveModel to save the model in the memory buffer as an offline model file. See the following code snippet.
    1
    2
    3
    4
    5
    6
    7
    status = aclgrphSaveModel("ir_build_sample", model);
    if(status == GRAPH_SUCCESS) {
            cout << "Save Offline Model SUCCESS!" << endl;
    }
    else {
            cout << "Save Offline Model Failed!" << endl;
    }
    
  5. Call aclgrphBuildFinalize to destroy associated allocations. See the following code snippet.
    1
    aclgrphBuildFinalize();
    

Follow-up Procedure

  • If an OM offline model is generated after the preceding build operations:

    If acl APIs are to be used for inference, use the APIs that load a model from a file, for example, aclmdlLoadFromFile, and then use the aclmdlExecute API to perform inference.

  • If the offline model generated after the preceding build operations is stored in the memory buffer:

    If acl APIs are to be used for inference, use the APIs that load a model from the memory, for example, aclmdlLoadFromMem, and then use the aclmdlExecute API to perform inference.

For details about the APIs, see "Model Management" in Application Development Guide (C&C++).