Narumol Prangnawarat | 867eba5 | 2020-02-03 12:29:56 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include <armnn/INetwork.hpp> |
| 6 | #include <armnn/IRuntime.hpp> |
| 7 | #include <armnn/Utils.hpp> |
| 8 | #include <armnn/Descriptors.hpp> |
| 9 | |
| 10 | #include <iostream> |
| 11 | |
| 12 | /// A simple example of using the ArmNN SDK API. In this sample, the users single input number is multiplied by 1.0f |
| 13 | /// using a fully connected layer with a single neuron to produce an output number that is the same as the input. |
| 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->AddAdditionLayer(); |
| 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 | InputTensors inputTensors |
| 66 | { |
| 67 | {0,armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0), input0Data.data())}, |
| 68 | {1,armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0), input1Data.data())} |
| 69 | }; |
| 70 | OutputTensors outputTensors |
| 71 | { |
| 72 | {0,armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), outputData.data())} |
| 73 | }; |
| 74 | |
| 75 | // Execute network |
| 76 | run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors); |
| 77 | |
| 78 | std::cout << "Addition operator result is {" << outputData[0] << "," << outputData[1] << "}" << std::endl; |
| 79 | return 0; |
| 80 | } |