Model Build
After graph definition is complete as shown in Figure 4-3, this section describes how to build a model.
Model build is briefly described as follows:
- After the graph is defined, call aclgrphBuildInitialize to initialize the system and allocate resources.
- Call aclgrphBuildModel to build the graph into an offline model adapted to the Ascend AI Processor. During build, the TBE built-in operator library and custom operator library are loaded. In this case, the model is stored in the buffer.
- To save the buffered model into a file, call aclgrphSaveModel to serialize the model into an .om file.
- Call aclgrphBuildFinalize to end the process and release resources.
Currently, the model build and model saving interfaces can be called repeatedly in a process, for building and saving multiple offline models.
Related Interfaces
- aclgrphBuildInitialize: initializes the system.
- aclgrphBuildModel: builds an offline model.
- aclgrphSaveModel: (optional) saves a model.
- aclgrphBuildFinalize: ends the build process and releases resource.
Including Header Files
#include "ge_ir_build.h" #include "ge_api_types.h"
The header files are stored in /atc/include/ge in the ATC installation path.
Allocating Resources
After creating a graph, Call aclgrphBuildInitialize to initialize the system and allocate resources. A code sample is provided as follows.
std::map<std::string, std::string> global_options = { {ge::ir_option::SOC_VERSION, "Ascend310"}, }; auto status = aclgrphBuildInitialize(global_options);
Pass the global_options arguments to set the build initialization information of the offline model. For details about the supported parameters, see aclgrphBuildInitialize Configuration Parameters. SOC_VERSION must be set, and other parameters can be set as required.
Building an Offline Model
Call aclgrphBuildModel to build the graph into an offline model. A code sample is provided as follows.
ModelBufferData model; std::map<std::string, std::string> options; PrepareOptions(options); status = aclgrphBuildModel(graph, options, model); if (status == GRAPH_SUCCESS) { cout << "Build Model SUCCESS!" << endl; } else { cout << "Build Model Failed!" << endl; }
Pass the options arguments to set the build configuration of the offline model. For details about the supported parameters, see aclgrphBuildModel Configuration Parameters. The configuration code is as follows.
void PrepareOptions(std::map<std::string, std::string>& options) { options.insert({ {ge::ir_option::EXEC_DISABLE_REUSED_MEMORY, "1"} // close resue memory }); }
Saving the Model
Model saving is optional. Call aclgrphSaveModel to save the buffered model into an offline model file (for example, ir_build_sample.om). A code sample is provided as follows.
status = aclgrphSaveModel("ir_build_sample", model); if (status == GRAPH_SUCCESS) { cout << "Save Offline Model SUCCESS!" << endl; } else { cout << "Save Offline Model Failed!" << endl; }
Releasing Resources
Call aclgrphBuildInitialize to release resources when the graph construction process ends. A code sample is provided as follows.
aclgrphBuildFinalize();