A simple example of using the ArmNN SDK API with the standalone sample dynamic backend. In this example, an addition layer is used to add 2 input tensors to produce a result output tensor.
| 12 | /// A simple example of using the ArmNN SDK API with the standalone sample dynamic backend. |
| 13 | /// In this example, an addition layer is used to add 2 input tensors to produce a result output tensor. |
| 14 | int main() |
| 15 | { |
| 16 | using namespace armnn; |
| 17 | |
| 18 | // Construct ArmNN network |
| 19 | armnn::NetworkId networkIdentifier; |
| 20 | INetworkPtr myNetwork = INetwork::Create(); |
| 21 | |
| 22 | IConnectableLayer* input0 = myNetwork->AddInputLayer(0); |
| 23 | IConnectableLayer* input1 = myNetwork->AddInputLayer(1); |
| 24 | IConnectableLayer* add = myNetwork->AddElementwiseBinaryLayer(BinaryOperation::Add); |
| 25 | IConnectableLayer* output = myNetwork->AddOutputLayer(0); |
| 26 | |
| 27 | input0->GetOutputSlot(0).Connect(add->GetInputSlot(0)); |
| 28 | input1->GetOutputSlot(0).Connect(add->GetInputSlot(1)); |
| 29 | add->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 30 | |
| 31 | TensorInfo tensorInfo(TensorShape({2, 1}), DataType::Float32); |
| 32 | input0->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 33 | input1->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 34 | add->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 35 | |
| 36 | // Create ArmNN runtime |
| 37 | IRuntime::CreationOptions options; // default options |
| 38 | armnn::IRuntimePtr run(armnn::IRuntime::Create(options)); |
| 39 | |
| 40 | // Optimise ArmNN network |
| 41 | armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {"SampleDynamic"}, run->GetDeviceSpec()); |
| 42 | if (!optNet) |
| 43 | { |
| 44 | // This shouldn't happen for this simple sample, with reference backend. |
| 45 | // But in general usage Optimize could fail if the hardware at runtime cannot |
| 46 | // support the model that has been provided. |
| 47 | std::cerr << "Error: Failed to optimise the input network." << std::endl; |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | // Load graph into runtime |
| 52 | run->LoadNetwork(networkIdentifier, std::move(optNet)); |
| 53 | |
| 54 | // input data |
| 55 | std::vector<float> input0Data |
| 56 | { |
| 57 | 5.0f, 3.0f |
| 58 | }; |
| 59 | std::vector<float> input1Data |
| 60 | { |
| 61 | 10.0f, 8.0f |
| 62 | }; |
| 63 | std::vector<float> outputData(2); |
| 64 | |
| 65 | TensorInfo inputTensorInfo = run->GetInputTensorInfo(networkIdentifier, 0); |
| 66 | inputTensorInfo.SetConstant(true); |
| 67 | InputTensors inputTensors |
| 68 | { |
| 69 | {0,armnn::ConstTensor(inputTensorInfo, input0Data.data())}, |
| 70 | {1,armnn::ConstTensor(inputTensorInfo, input1Data.data())} |
| 71 | }; |
nothing calls this directly
no test coverage detected