aboutsummaryrefslogtreecommitdiff
path: root/tests/InterfaceTests/OnnxParserTest.cpp
blob: f6dc9a42b265e719f18b5be10698d9c974247b4a (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include <armnn/INetwork.hpp>
#include <armnn/IRuntime.hpp>
#include <armnnOnnxParser/IOnnxParser.hpp>
#include <iostream>

int main()
{
    // Raw protobuf text for a single layer CONV2D model.
    std::string m_Prototext = R"(
                   ir_version: 3
                   producer_name:  "CNTK"
                   producer_version:  "2.5.1"
                   domain:  "ai.cntk"
                   model_version: 1
                   graph {
                     name:  "CNTKGraph"
                     input {
                        name: "Input"
                        type {
                          tensor_type {
                            elem_type: 1
                            shape {
                              dim {
                                dim_value: 1
                              }
                              dim {
                                dim_value: 1
                              }
                              dim {
                                dim_value: 3
                              }
                              dim {
                                dim_value: 3
                              }
                            }
                          }
                        }
                      }
                      input {
                        name: "Weight"
                        type {
                          tensor_type {
                            elem_type: 1
                            shape {
                              dim {
                                dim_value: 1
                              }
                              dim {
                                dim_value: 1
                              }
                              dim {
                                dim_value: 3
                              }
                              dim {
                                dim_value: 3
                              }
                            }
                          }
                        }
                      }
                      initializer {
                          dims: 1
                          dims: 1
                          dims: 3
                          dims: 3
                          data_type: 1
                          float_data: 2
                          float_data: 1
                          float_data: 0
                          float_data: 6
                          float_data: 2
                          float_data: 1
                          float_data: 4
                          float_data: 1
                          float_data: 2
                          name: "Weight"
                        }
                      node {
                         input: "Input"
                         input: "Weight"
                         output: "Output"
                         name: "Convolution"
                         op_type: "Conv"
                         attribute {
                           name: "kernel_shape"
                           ints: 3
                           ints: 3
                           type: INTS
                         }
                         attribute {
                           name: "strides"
                           ints: 1
                           ints: 1
                           type: INTS
                         }
                         attribute {
                           name: "auto_pad"
                           s: "VALID"
                           type: STRING
                         }
                         attribute {
                           name: "group"
                           i: 1
                           type: INT
                         }
                         attribute {
                           name: "dilations"
                           ints: 1
                           ints: 1
                           type: INTS
                         }
                         doc_string: ""
                         domain: ""
                       }
                      output {
                          name: "Output"
                          type {
                             tensor_type {
                               elem_type: 1
                               shape {
                                   dim {
                                       dim_value: 1
                                   }
                                   dim {
                                       dim_value: 1
                                   }
                                   dim {
                                       dim_value: 1
                                   }
                                   dim {
                                       dim_value: 1
                                   }
                               }
                            }
                        }
                        }
                    }
                   opset_import {
                      version: 7
                    })";

    using namespace armnn;

    // Create ArmNN runtime
    IRuntime::CreationOptions options;    // default options
    IRuntimePtr runtime = IRuntime::Create(options);
    // Create the parser.
    armnnOnnxParser::IOnnxParserPtr parser = armnnOnnxParser::IOnnxParser::Create();
    try
    {
        // Parse the proto text.
        armnn::INetworkPtr network = parser->CreateNetworkFromString(m_Prototext);
        auto optimized             = Optimize(*network, { armnn::Compute::CpuRef }, runtime->GetDeviceSpec());
        if (!optimized)
        {
            std::cout << "Error: Failed to optimise the input network." << std::endl;
            return 1;
        }
        armnn::NetworkId networkId;
        std::string errorMsg;
        Status status = runtime->LoadNetwork(networkId, std::move(optimized), errorMsg);
        if (status != Status::Success)
        {
            std::cout << "Error: Failed to load the optimized network." << std::endl;
            return -1;
        }

        // Setup the input and output.
        std::vector<armnnOnnxParser::BindingPointInfo> inputBindings;
        // Coz we know the model we know the input tensor is called Input and output is Output.
        inputBindings.push_back(parser->GetNetworkInputBindingInfo("Input"));
        std::vector<armnnOnnxParser::BindingPointInfo> outputBindings;
        outputBindings.push_back(parser->GetNetworkOutputBindingInfo("Output"));
        // Allocate input tensors
        armnn::InputTensors inputTensors;
        std::vector<float> in_data(inputBindings[0].second.GetNumElements());
        TensorInfo inputTensorInfo(inputBindings[0].second);
        inputTensorInfo.SetConstant(true);
        // Set some kind of values in the input.
        for (int i = 0; i < inputBindings[0].second.GetNumElements(); i++)
        {
            in_data[i] = 1.0f + i;
        }
        inputTensors.push_back({ inputBindings[0].first, armnn::ConstTensor(inputTensorInfo, in_data.data()) });

        // Allocate output tensors
        armnn::OutputTensors outputTensors;
        std::vector<float> out_data(outputBindings[0].second.GetNumElements());
        outputTensors.push_back({ outputBindings[0].first, armnn::Tensor(outputBindings[0].second, out_data.data()) });

        runtime->EnqueueWorkload(networkId, inputTensors, outputTensors);
        runtime->UnloadNetwork(networkId);
        // We're finished with the parser.
        armnnOnnxParser::IOnnxParser::Destroy(parser.get());
        parser.release();
    }
    catch (const std::exception& e)    // Could be an InvalidArgumentException or a ParseException.
    {
        std::cout << "Unable to create parser for the passed protobuf string. Reason: " << e.what() << std::endl;
        return -1;
    }
    return 0;
}