Converting a Model
This section uses Switch_v1.pb as an example to demonstrate how to convert a network with control flow operators into that with function operators, which you can convert into an offline model adapted to the Ascend AI Processor by using ATC. You can obtain Switch_v1.pb by referring to How Do I Obtain Switch_v1.pb?
- Obtain the output of the network with control flow operators.
Run the following command in the directory of the executable file summarize_graph.
./summarize_graph --in_graph=/home/test/module/Switch_v1.pb
If you see the following information displayed, your command execution is successful.
Found 3 possible inputs: (name=x, type=float(1), shape=<unknown>) (name=y, type=float(1), shape=<unknown>) (name=z, type=float(1), shape=<unknown>) No variables spotted. Found 1 possible output: (name=add, op=AddV2) Found 0 (0) const parameters, 0 (0) variable parameters, and 0 control_edges Op types used: 3 Placeholder, 3 Switch, 2 AddV2, 2 Mul, 2 Square, 1 Identity, 1 Less, 1 Merge To use with tensorflow/tools/benchmark:benchmark_model try these arguments: bazel run tensorflow/tools/benchmark:benchmark_model -- --graph=/home/test/module/Switch_v1.pb --show_flops --input_layer=x,y,z --input_layer_type=float,float,float --input_layer_shape=:: --output_layer=add
- Construct the output configuration file config.pbtxt.
Run vim config.pbtxt in any directory to create the config.pbtxt file. The following assumes that the configuration file is created in $HOME/module.
According to 1, the network has an output with which you can create a config.pbtxt file as follows. In this example, fetch indicates the output and node_name indicates the output operator name. Edit the name as required.
fetch { id { node_name: "add" } }After configuration, run the exit command :wq!.
- Generate the configuration file of the network with function operators.
Run the following commands in any directory to set the log information displayed with the xlacompile command.
export TF_CPP_MIN_LOG_LEVEL=0 export TF_CPP_MIN_VLOG_LEVEL=1
Run the following command in the directory of the executable file xlacompile../xlacompile --graph=/home/test/module/Switch_v1.pb --config=/home/test/module/config.pbtxt --output=/home/test/module/Switch_v1_v2
If you see the message "Successfully convert ..." displayed, your conversion is successful. You can view the following output files by accessing the path specified by --output.
-rw-rw-r-- 1 test test 1236 Jun 20 17:13 Switch_v1_v2.pb -rw-rw-r-- 1 test test 4803 Jun 20 17:13 Switch_v1_v2.pbtxt
- Generate subgraphs of the network with function operators. To obtain subgraphs, use the configuration file when converting your model with ATC.
Run the following command in any directory to generate subgraphs for the network with function operators.
python3.7.5 ${INSTALL_DIR}/compiler/python/func2graph/func2graph.py -m /home/test/module/Switch_v1_v2.pbIf you see the following message displayed, they are successfully generated.
graph_def_file: /home/test/module/graph_def_library.pbtxt INFO: Convert to subgraphs successfully.
- Convert the network with function operators into an offline model adapted to the Ascend AI Processor.
Set environment variables required for executing ATC by referring to Setting Environment Variables, and run the following command to convert the model.
atc --model=/home/test/module/Switch_v1_v2.pb --framework=3 --output=/home/test/module/out/Switch_v1_v2_to_om --soc_version=<soc_version>If you see the following message displayed, your conversion is successful.ATC run success
After successful execution, you can view the model file in the path specified by --output.