FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <armnn/INetwork.hpp> |
| 8 | |
| 9 | #include <backendsCommon/test/CommonTestUtils.hpp> |
| 10 | |
| 11 | #include <boost/test/unit_test.hpp> |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | template<typename armnn::DataType DataType> |
| 19 | INetworkPtr CreateArithmeticNetwork(const std::vector<TensorShape>& inputShapes, |
| 20 | const TensorShape& outputShape, |
| 21 | const LayerType type, |
| 22 | const float qScale = 1.0f, |
| 23 | const int32_t qOffset = 0) |
| 24 | { |
| 25 | using namespace armnn; |
| 26 | |
| 27 | // Builds up the structure of the network. |
| 28 | INetworkPtr net(INetwork::Create()); |
| 29 | |
| 30 | IConnectableLayer* arithmeticLayer = nullptr; |
| 31 | |
| 32 | switch(type){ |
| 33 | case LayerType::Equal: arithmeticLayer = net->AddEqualLayer("equal"); break; |
| 34 | case LayerType::Greater: arithmeticLayer = net->AddGreaterLayer("greater"); break; |
| 35 | default: BOOST_TEST_FAIL("Non-Arithmetic layer type called."); |
| 36 | } |
| 37 | |
| 38 | for (unsigned int i = 0; i < inputShapes.size(); ++i) |
| 39 | { |
| 40 | TensorInfo inputTensorInfo(inputShapes[i], DataType, qScale, qOffset); |
| 41 | IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i)); |
| 42 | Connect(input, arithmeticLayer, inputTensorInfo, 0, i); |
| 43 | } |
| 44 | |
| 45 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 46 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 47 | Connect(arithmeticLayer, output, outputTensorInfo, 0, 0); |
| 48 | |
| 49 | return net; |
| 50 | } |
| 51 | |
| 52 | template<typename T> |
| 53 | void ArithmeticSimpleEndToEnd(const std::vector<BackendId>& backends, |
| 54 | const LayerType type, |
| 55 | const std::vector<T> expectedOutput) |
| 56 | { |
| 57 | using namespace armnn; |
| 58 | |
| 59 | const std::vector<TensorShape> inputShapes{{ 2, 2, 2, 2 }, { 2, 2, 2, 2 }}; |
| 60 | const TensorShape& outputShape = { 2, 2, 2, 2 }; |
| 61 | |
| 62 | // Builds up the structure of the network |
| 63 | INetworkPtr net = CreateArithmeticNetwork<GetDataType<T>()>(inputShapes, outputShape, type); |
| 64 | |
| 65 | BOOST_TEST_CHECKPOINT("create a network"); |
| 66 | |
| 67 | const std::vector<T> input0({ 1, 1, 1, 1, 5, 5, 5, 5, |
| 68 | 3, 3, 3, 3, 4, 4, 4, 4 }); |
| 69 | |
| 70 | const std::vector<T> input1({ 1, 1, 1, 1, 3, 3, 3, 3, |
| 71 | 5, 5, 5, 5, 4, 4, 4, 4 }); |
| 72 | |
| 73 | std::map<int, std::vector<T>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 74 | std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }}; |
| 75 | |
| 76 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 77 | } |
| 78 | |
| 79 | template<typename T> |
| 80 | void ArithmeticBroadcastEndToEnd(const std::vector<BackendId>& backends, |
| 81 | const LayerType type, |
| 82 | const std::vector<T> expectedOutput) |
| 83 | { |
| 84 | using namespace armnn; |
| 85 | |
| 86 | const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }}; |
| 87 | const TensorShape& outputShape = { 1, 2, 2, 3 }; |
| 88 | |
| 89 | // Builds up the structure of the network |
| 90 | INetworkPtr net = CreateArithmeticNetwork<GetDataType<T>()>(inputShapes, outputShape, type); |
| 91 | |
| 92 | BOOST_TEST_CHECKPOINT("create a network"); |
| 93 | |
| 94 | const std::vector<T> input0({ 1, 2, 3, 1, 0, 6, |
| 95 | 7, 8, 9, 10, 11, 12 }); |
| 96 | |
| 97 | const std::vector<T> input1({ 1, 1, 3 }); |
| 98 | |
| 99 | std::map<int, std::vector<T>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 100 | std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }}; |
| 101 | |
| 102 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 103 | } |
| 104 | |
| 105 | } // anonymous namespace |