aclgrphBundleSaveModel

Applicability

Product

Supported or Not

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference products

Atlas training products

Header File/Library File

  • Header file: #include <ge/ge_ir_build.h>
  • Library file: libge_compiler.so

Function Usage

Serializes an offline model to a file.

  • Compared with aclgrphSaveModel, this API applies to the weight update scenario. If acl is used to perform inference on the model generated by this API, you need to use the aclmdlBundleLoadFromFile or aclmdlBundleLoadFromMem API to load the model. For details about the APIs, see ""Model Loading and Unloading"".
  • After the offline model cache is generated using aclgrphBundleBuildModel, aclgrphBundleSaveModel can be called to flush the cache to disks.

Prototype

1
graphStatus aclgrphBundleSaveModel(const char_t *output_file, const ModelBufferData &model)

Parameters

Parameter

Input/Output

Description

model

Input

Buffer of the offline model generated using aclgrphBundleBuildModel. For details, see ModelBufferData.

1
2
3
4
5
struct ModelBufferData
{
  std::shared_ptr<uint8_t> data = nullptr;
  uint32_t length;
};

data points to the generated model data, and length indicates the actual model size.

output_file

Input

Name of the offline model file. The name of the generated offline model file automatically ends with .om, for example, ir_build_sample.om or ir_build_sample_linux_x86_64.om.

If the name of an .om file contains the OS and architecture, the file can be used only in the operating environment with the OS and architecture.

Returns

Parameter

Type

Description

-

graphStatus

GRAPH_SUCCESS(0): success.

Other values: failure. For details, see ge::graphStatus.

Restrictions

If the name of the generated .om model file contains the OS and architecture, but the OS and architecture are inconsistent with those of the model operating environment, this API must be used together with OPTION_HOST_ENV_OS and OPTION_HOST_ENV_CPU to set the OS type and architecture of the model operating environment. For details about the parameters, see aclgrphBuildInitialize Configuration Parameters.

Example

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Included header files:
#include "ge_ir_build.h" 
#include "ge_api_types.h"

// 1. Generate a graph.
Graph origin_graph("Irorigin_graph");
GenGraph(origin_graph);

// 2. After the graph is created, call aclgrphBuildInitialize to initialize the system and allocate resources.
std::map<AscendString, AscendString> global_options;
auto status = aclgrphBuildInitialize(global_options);

// 3. Generate graphs whose weights can be updated, including the weight initialization graph, weight update graph, and inference graph.
// Weight initialization is optional. You can determine whether to include the weight initialization graph based on the service requirements. If the weight initialization graph is not included, the device memory required for model loading can be saved.
WeightRefreshableGraphs weight_refreshable_graphs;
std::vector<AscendString> const_names;
const_names.emplace_back(AscendString("const_1"));
const_names.emplace_back(AscendString("const_2"));
status = aclgrphConvertToWeightRefreshableGraphs(origin_graph, const_names, weight_refreshable_graphs);
if (status != GRAPH_SUCCESS) {
    cout << "aclgrphConvertToWeightRefreshableGraphs failed!" << endl;
    aclgrphBuildFinalize();
    return -1;
}

// 4. Build the graph whose weight can be updated into an offline model and save it in the memory buffer.
std::map<AscendString, AscendString> options;
std::vector<ge::GraphWithOptions> graph_and_options;
// Inference graph
graph_and_options.push_back(GraphWithOptions{weight_refreshable_graphs.infer_graph, options});
// Weight initialization graph
graph_and_options.push_back(GraphWithOptions{weight_refreshable_graphs.var_init_graph, options});
// Weight update graph
graph_and_options.push_back(GraphWithOptions{weight_refreshable_graphs.var_update_graph, options});
ge::ModelBufferData model;
status = aclgrphBundleBuildModel(graph_and_options, model);
if (status != GRAPH_SUCCESS) {
    cout << "aclgrphBundleBuildModel failed" << endl;
    aclgrphBuildFinalize();
    return -1;
}

// 5. Save the model in the memory buffer as an offline model file.
const char *file="./xxx" ;
status = aclgrphBundleSaveModel(file, model);
if (status != GRAPH_SUCCESS) {
    cout << "aclgrphBundleSaveModel failed" << endl;
    aclgrphBuildFinalize();
    return -1;
}
// 6. End the build process and destroy allocations.
aclgrphBuildFinalize();