2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/INetwork.hpp>
6 #include <armnn/IRuntime.hpp>
7 #include <armnn/Utils.hpp>
8 #include <armnn/Descriptors.hpp>
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.
16 using namespace armnn;
18 // Construct ArmNN network
19 armnn::NetworkId networkIdentifier;
20 INetworkPtr myNetwork = INetwork::Create();
22 IConnectableLayer* input0 = myNetwork->AddInputLayer(0);
23 IConnectableLayer* input1 = myNetwork->AddInputLayer(1);
24 IConnectableLayer* add = myNetwork->AddAdditionLayer();
25 IConnectableLayer* output = myNetwork->AddOutputLayer(0);
27 input0->GetOutputSlot(0).Connect(add->GetInputSlot(0));
28 input1->GetOutputSlot(0).Connect(add->GetInputSlot(1));
29 add->GetOutputSlot(0).Connect(output->GetInputSlot(0));
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);
36 // Create ArmNN runtime
37 IRuntime::CreationOptions options; // default options
38 armnn::IRuntimePtr run(armnn::IRuntime::Create(options));
40 // Optimise ArmNN network
41 armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {"SampleDynamic"}, run->GetDeviceSpec());
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;
51 // Load graph into runtime
52 run->LoadNetwork(networkIdentifier, std::move(optNet));
55 std::vector<float> input0Data
59 std::vector<float> input1Data
63 std::vector<float> outputData(2);
65 InputTensors inputTensors
67 {0,armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0), input0Data.data())},
68 {1,armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0), input1Data.data())}
70 OutputTensors outputTensors
72 {0,armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), outputData.data())}
76 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
78 std::cout << "Addition operator result is {" << outputData[0] << "," << outputData[1] << "}" << std::endl;