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 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 7 | #include "CommonTestUtils.hpp" |
| 8 | |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 9 | #include <ResolveType.hpp> |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 10 | |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 11 | #include <armnn/INetwork.hpp> |
| 12 | |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 13 | #include <boost/test/unit_test.hpp> |
| 14 | |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 20 | template<armnn::DataType ArmnnTypeInput, armnn::DataType ArmnnTypeOutput> |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 21 | INetworkPtr CreateArithmeticNetwork(const std::vector<TensorShape>& inputShapes, |
| 22 | const TensorShape& outputShape, |
| 23 | const LayerType type, |
| 24 | const float qScale = 1.0f, |
| 25 | const int32_t qOffset = 0) |
| 26 | { |
| 27 | using namespace armnn; |
| 28 | |
| 29 | // Builds up the structure of the network. |
| 30 | INetworkPtr net(INetwork::Create()); |
| 31 | |
| 32 | IConnectableLayer* arithmeticLayer = nullptr; |
| 33 | |
| 34 | switch(type){ |
| 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."); |
| 38 | } |
| 39 | |
| 40 | for (unsigned int i = 0; i < inputShapes.size(); ++i) |
| 41 | { |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 42 | TensorInfo inputTensorInfo(inputShapes[i], ArmnnTypeInput, qScale, qOffset); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 43 | IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i)); |
| 44 | Connect(input, arithmeticLayer, inputTensorInfo, 0, i); |
| 45 | } |
| 46 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 47 | TensorInfo outputTensorInfo(outputShape, ArmnnTypeOutput, qScale, qOffset); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 48 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 49 | Connect(arithmeticLayer, output, outputTensorInfo, 0, 0); |
| 50 | |
| 51 | return net; |
| 52 | } |
| 53 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 54 | template<armnn::DataType ArmnnInputType, |
| 55 | armnn::DataType ArmnnOutputType, |
| 56 | typename TInput = armnn::ResolveType<ArmnnInputType>, |
| 57 | typename TOutput = armnn::ResolveType<ArmnnOutputType>> |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 58 | void ArithmeticSimpleEndToEnd(const std::vector<BackendId>& backends, |
| 59 | const LayerType type, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 60 | const std::vector<TOutput> expectedOutput) |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 61 | { |
| 62 | using namespace armnn; |
| 63 | |
| 64 | const std::vector<TensorShape> inputShapes{{ 2, 2, 2, 2 }, { 2, 2, 2, 2 }}; |
| 65 | const TensorShape& outputShape = { 2, 2, 2, 2 }; |
| 66 | |
| 67 | // Builds up the structure of the network |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 68 | INetworkPtr net = CreateArithmeticNetwork<ArmnnInputType, ArmnnOutputType>(inputShapes, outputShape, type); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 69 | |
| 70 | BOOST_TEST_CHECKPOINT("create a network"); |
| 71 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 72 | const std::vector<TInput> input0({ 1, 1, 1, 1, 5, 5, 5, 5, |
| 73 | 3, 3, 3, 3, 4, 4, 4, 4 }); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 74 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 75 | const std::vector<TInput> input1({ 1, 1, 1, 1, 3, 3, 3, 3, |
| 76 | 5, 5, 5, 5, 4, 4, 4, 4 }); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 77 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 78 | std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 79 | std::map<int, std::vector<TOutput>> expectedOutputData = {{ 0, expectedOutput }}; |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 80 | |
Nattapat Chaimanowong | 1fcb4ff | 2019-01-24 15:25:26 +0000 | [diff] [blame] | 81 | EndToEndLayerTestImpl<ArmnnInputType, ArmnnOutputType>(move(net), inputTensorData, expectedOutputData, backends); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 82 | } |
| 83 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 84 | template<armnn::DataType ArmnnInputType, |
| 85 | armnn::DataType ArmnnOutputType, |
| 86 | typename TInput = armnn::ResolveType<ArmnnInputType>, |
| 87 | typename TOutput = armnn::ResolveType<ArmnnOutputType>> |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 88 | void ArithmeticBroadcastEndToEnd(const std::vector<BackendId>& backends, |
| 89 | const LayerType type, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 90 | const std::vector<TOutput> expectedOutput) |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 91 | { |
| 92 | using namespace armnn; |
| 93 | |
| 94 | const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }}; |
| 95 | const TensorShape& outputShape = { 1, 2, 2, 3 }; |
| 96 | |
| 97 | // Builds up the structure of the network |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 98 | INetworkPtr net = CreateArithmeticNetwork<ArmnnInputType, ArmnnOutputType>(inputShapes, outputShape, type); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 99 | |
| 100 | BOOST_TEST_CHECKPOINT("create a network"); |
| 101 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 102 | const std::vector<TInput> input0({ 1, 2, 3, 1, 0, 6, |
| 103 | 7, 8, 9, 10, 11, 12 }); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 104 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 105 | const std::vector<TInput> input1({ 1, 1, 3 }); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 106 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 107 | std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 108 | std::map<int, std::vector<TOutput>> expectedOutputData = {{ 0, expectedOutput }}; |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 109 | |
Nattapat Chaimanowong | 1fcb4ff | 2019-01-24 15:25:26 +0000 | [diff] [blame] | 110 | EndToEndLayerTestImpl<ArmnnInputType, ArmnnOutputType>(move(net), inputTensorData, expectedOutputData, backends); |
FrancisMurtagh | 2262bbd | 2018-12-20 16:09:45 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // anonymous namespace |