narpra01 | b9546cf | 2018-11-20 15:21:28 +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 CreateMergerNetwork(const std::vector<TensorShape>& inputShapes, |
| 20 | const TensorShape& outputShape, |
| 21 | unsigned int concatAxis, |
| 22 | const float qScale = 1.0f, |
| 23 | const int32_t qOffset = 0) |
| 24 | { |
| 25 | using namespace armnn; |
| 26 | // Builds up the structure of the network. |
| 27 | INetworkPtr net(INetwork::Create()); |
| 28 | |
| 29 | OriginsDescriptor descriptor; |
| 30 | |
| 31 | descriptor = CreateMergerDescriptorForConcatenation(inputShapes.begin(), |
| 32 | inputShapes.end(), |
| 33 | concatAxis); |
| 34 | IConnectableLayer* merger = net->AddMergerLayer(descriptor, "merger"); |
| 35 | |
| 36 | for (unsigned int i = 0; i < inputShapes.size(); ++i) |
| 37 | { |
| 38 | TensorInfo inputTensorInfo(inputShapes[i], DataType, qScale, qOffset); |
| 39 | IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i)); |
| 40 | Connect(input, merger, inputTensorInfo, 0, i); |
| 41 | } |
| 42 | |
| 43 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 44 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 45 | Connect(merger, output, outputTensorInfo, 0, 0); |
| 46 | |
| 47 | return net; |
| 48 | } |
| 49 | |
| 50 | template<typename T> |
| 51 | void MergerDim0EndToEnd(const std::vector<BackendId>& backends) |
| 52 | { |
| 53 | using namespace armnn; |
| 54 | |
| 55 | unsigned int concatAxis = 0; |
| 56 | const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 57 | const TensorShape& outputShape = { 4, 3, 2, 2 }; |
| 58 | |
| 59 | // Builds up the structure of the network |
| 60 | INetworkPtr net = CreateMergerNetwork<GetDataType<T>()>(inputShapes, outputShape, concatAxis); |
| 61 | |
| 62 | BOOST_TEST_CHECKPOINT("create a network"); |
| 63 | |
| 64 | // Creates structures for input & output. |
| 65 | std::vector<T> inputData{ |
| 66 | 1, 2, |
| 67 | 3, 4, |
| 68 | 5, 6, |
| 69 | 7, 8, |
| 70 | 9, 10, |
| 71 | 11, 12, |
| 72 | 1, 2, |
| 73 | 3, 4, |
| 74 | 5, 6, |
| 75 | 7, 8, |
| 76 | 9, 10, |
| 77 | 11, 12 |
| 78 | }; |
| 79 | |
| 80 | std::vector<T> expectedOutput{ |
| 81 | 1, 2, |
| 82 | 3, 4, |
| 83 | 5, 6, |
| 84 | 7, 8, |
| 85 | 9, 10, |
| 86 | 11, 12, |
| 87 | 1, 2, |
| 88 | 3, 4, |
| 89 | 5, 6, |
| 90 | 7, 8, |
| 91 | 9, 10, |
| 92 | 11, 12, |
| 93 | 1, 2, |
| 94 | 3, 4, |
| 95 | 5, 6, |
| 96 | 7, 8, |
| 97 | 9, 10, |
| 98 | 11, 12, |
| 99 | 1, 2, |
| 100 | 3, 4, |
| 101 | 5, 6, |
| 102 | 7, 8, |
| 103 | 9, 10, |
| 104 | 11, 12 |
| 105 | }; |
| 106 | |
| 107 | std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }}; |
| 108 | std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }}; |
| 109 | |
| 110 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | void MergerDim1EndToEnd(const std::vector<BackendId>& backends) |
| 115 | { |
| 116 | using namespace armnn; |
| 117 | |
| 118 | unsigned int concatAxis = 1; |
| 119 | const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 120 | const TensorShape& outputShape = { 2, 6, 2, 2 }; |
| 121 | |
| 122 | // Builds up the structure of the network |
| 123 | INetworkPtr net = CreateMergerNetwork<GetDataType<T>()>(inputShapes, outputShape, concatAxis); |
| 124 | |
| 125 | BOOST_TEST_CHECKPOINT("create a network"); |
| 126 | |
| 127 | // Creates structures for input & output. |
| 128 | std::vector<T> inputData{ |
| 129 | 1, 2, |
| 130 | 3, 4, |
| 131 | 5, 6, |
| 132 | 7, 8, |
| 133 | 9, 10, |
| 134 | 11, 12, |
| 135 | 1, 2, |
| 136 | 3, 4, |
| 137 | 5, 6, |
| 138 | 7, 8, |
| 139 | 9, 10, |
| 140 | 11, 12 |
| 141 | }; |
| 142 | |
| 143 | std::vector<T> expectedOutput{ |
| 144 | 1, 2, |
| 145 | 3, 4, |
| 146 | 5, 6, |
| 147 | 7, 8, |
| 148 | 9, 10, |
| 149 | 11, 12, |
| 150 | 1, 2, |
| 151 | 3, 4, |
| 152 | 5, 6, |
| 153 | 7, 8, |
| 154 | 9, 10, |
| 155 | 11, 12, |
| 156 | 1, 2, |
| 157 | 3, 4, |
| 158 | 5, 6, |
| 159 | 7, 8, |
| 160 | 9, 10, |
| 161 | 11, 12, |
| 162 | 1, 2, |
| 163 | 3, 4, |
| 164 | 5, 6, |
| 165 | 7, 8, |
| 166 | 9, 10, |
| 167 | 11, 12 |
| 168 | }; |
| 169 | |
| 170 | std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }}; |
| 171 | std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }}; |
| 172 | |
| 173 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 174 | } |
| 175 | |
| 176 | template<typename T> |
| 177 | void MergerDim2EndToEnd(const std::vector<BackendId>& backends) |
| 178 | { |
| 179 | using namespace armnn; |
| 180 | |
| 181 | unsigned int concatAxis = 2; |
| 182 | const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 183 | const TensorShape& outputShape = { 2, 3, 4, 2 }; |
| 184 | |
| 185 | // Builds up the structure of the network |
| 186 | INetworkPtr net = CreateMergerNetwork<GetDataType<T>()>(inputShapes, outputShape, concatAxis); |
| 187 | |
| 188 | BOOST_TEST_CHECKPOINT("create a network"); |
| 189 | |
| 190 | // Creates structures for input & output. |
| 191 | std::vector<T> inputData{ |
| 192 | 1, 2, |
| 193 | 3, 4, |
| 194 | 5, 6, |
| 195 | 7, 8, |
| 196 | 9, 10, |
| 197 | 11, 12, |
| 198 | 1, 2, |
| 199 | 3, 4, |
| 200 | 5, 6, |
| 201 | 7, 8, |
| 202 | 9, 10, |
| 203 | 11, 12 |
| 204 | }; |
| 205 | |
| 206 | std::vector<T> expectedOutput{ |
| 207 | 1, 2, |
| 208 | 3, 4, |
| 209 | 1, 2, |
| 210 | 3, 4, |
| 211 | 5, 6, |
| 212 | 7, 8, |
| 213 | 5, 6, |
| 214 | 7, 8, |
| 215 | 9, 10, |
| 216 | 11, 12, |
| 217 | 9, 10, |
| 218 | 11, 12, |
| 219 | 1, 2, |
| 220 | 3, 4, |
| 221 | 1, 2, |
| 222 | 3, 4, |
| 223 | 5, 6, |
| 224 | 7, 8, |
| 225 | 5, 6, |
| 226 | 7, 8, |
| 227 | 9, 10, |
| 228 | 11, 12, |
| 229 | 9, 10, |
| 230 | 11, 12 |
| 231 | }; |
| 232 | |
| 233 | std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }}; |
| 234 | std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }}; |
| 235 | |
| 236 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 237 | } |
| 238 | |
| 239 | template<typename T> |
| 240 | void MergerDim3EndToEnd(const std::vector<BackendId>& backends) |
| 241 | { |
| 242 | using namespace armnn; |
| 243 | |
| 244 | unsigned int concatAxis = 3; |
| 245 | const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 246 | const TensorShape& outputShape = { 2, 3, 2, 4 }; |
| 247 | |
| 248 | // Builds up the structure of the network |
| 249 | INetworkPtr net = CreateMergerNetwork<GetDataType<T>()>(inputShapes, outputShape, concatAxis); |
| 250 | |
| 251 | BOOST_TEST_CHECKPOINT("create a network"); |
| 252 | |
| 253 | // Creates structures for input & output. |
| 254 | std::vector<T> inputData{ |
| 255 | 1, 2, |
| 256 | 3, 4, |
| 257 | 5, 6, |
| 258 | 7, 8, |
| 259 | 9, 10, |
| 260 | 11, 12, |
| 261 | 1, 2, |
| 262 | 3, 4, |
| 263 | 5, 6, |
| 264 | 7, 8, |
| 265 | 9, 10, |
| 266 | 11, 12 |
| 267 | }; |
| 268 | |
| 269 | std::vector<T> expectedOutput{ |
| 270 | 1, 2, |
| 271 | 1, 2, |
| 272 | 3, 4, |
| 273 | 3, 4, |
| 274 | 5, 6, |
| 275 | 5, 6, |
| 276 | 7, 8, |
| 277 | 7, 8, |
| 278 | 9, 10, |
| 279 | 9, 10, |
| 280 | 11, 12, |
| 281 | 11, 12, |
| 282 | 1, 2, |
| 283 | 1, 2, |
| 284 | 3, 4, |
| 285 | 3, 4, |
| 286 | 5, 6, |
| 287 | 5, 6, |
| 288 | 7, 8, |
| 289 | 7, 8, |
| 290 | 9, 10, |
| 291 | 9, 10, |
| 292 | 11, 12, |
| 293 | 11, 12 |
| 294 | }; |
| 295 | |
| 296 | std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }}; |
| 297 | std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }}; |
| 298 | |
| 299 | EndToEndLayerTestImpl<T>(move(net), inputTensorData, expectedOutputData, backends); |
| 300 | } |
| 301 | |
| 302 | } // anonymous namespace |