2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include "TypeUtils.hpp"
9 #include <armnn/INetwork.hpp>
11 #include <backendsCommon/test/CommonTestUtils.hpp>
13 #include <boost/test/unit_test.hpp>
20 template<typename armnn::DataType DataType>
21 INetworkPtr CreateArithmeticNetwork(const std::vector<TensorShape>& inputShapes,
22 const TensorShape& outputShape,
24 const float qScale = 1.0f,
25 const int32_t qOffset = 0)
27 using namespace armnn;
29 // Builds up the structure of the network.
30 INetworkPtr net(INetwork::Create());
32 IConnectableLayer* arithmeticLayer = nullptr;
35 case LayerType::Equal: arithmeticLayer = net->AddEqualLayer("equal"); break;
36 case LayerType::Greater: arithmeticLayer = net->AddGreaterLayer("greater"); break;
37 default: BOOST_TEST_FAIL("Non-Arithmetic layer type called.");
40 for (unsigned int i = 0; i < inputShapes.size(); ++i)
42 TensorInfo inputTensorInfo(inputShapes[i], DataType, qScale, qOffset);
43 IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i));
44 Connect(input, arithmeticLayer, inputTensorInfo, 0, i);
47 TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
48 IConnectableLayer* output = net->AddOutputLayer(0, "output");
49 Connect(arithmeticLayer, output, outputTensorInfo, 0, 0);
54 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
55 void ArithmeticSimpleEndToEnd(const std::vector<BackendId>& backends,
57 const std::vector<T> expectedOutput)
59 using namespace armnn;
61 const std::vector<TensorShape> inputShapes{{ 2, 2, 2, 2 }, { 2, 2, 2, 2 }};
62 const TensorShape& outputShape = { 2, 2, 2, 2 };
64 // Builds up the structure of the network
65 INetworkPtr net = CreateArithmeticNetwork<ArmnnType>(inputShapes, outputShape, type);
67 BOOST_TEST_CHECKPOINT("create a network");
69 const std::vector<T> input0({ 1, 1, 1, 1, 5, 5, 5, 5,
70 3, 3, 3, 3, 4, 4, 4, 4 });
72 const std::vector<T> input1({ 1, 1, 1, 1, 3, 3, 3, 3,
73 5, 5, 5, 5, 4, 4, 4, 4 });
75 std::map<int, std::vector<T>> inputTensorData = {{ 0, input0 }, { 1, input1 }};
76 std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }};
78 EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends);
81 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
82 void ArithmeticBroadcastEndToEnd(const std::vector<BackendId>& backends,
84 const std::vector<T> expectedOutput)
86 using namespace armnn;
88 const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }};
89 const TensorShape& outputShape = { 1, 2, 2, 3 };
91 // Builds up the structure of the network
92 INetworkPtr net = CreateArithmeticNetwork<ArmnnType>(inputShapes, outputShape, type);
94 BOOST_TEST_CHECKPOINT("create a network");
96 const std::vector<T> input0({ 1, 2, 3, 1, 0, 6,
97 7, 8, 9, 10, 11, 12 });
99 const std::vector<T> input1({ 1, 1, 3 });
101 std::map<int, std::vector<T>> inputTensorData = {{ 0, input0 }, { 1, input1 }};
102 std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }};
104 EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends);
107 } // anonymous namespace