Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | a9c3267 | 2023-12-04 17:23:09 +0000 | [diff] [blame] | 2 | // Copyright © 2017,2019-2021,2023 Arm Ltd and Contributors. All rights reserved. |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <ResolveType.hpp> |
| 8 | |
| 9 | #include <armnn/INetwork.hpp> |
| 10 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 11 | #include <CommonTestUtils.hpp> |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 12 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 13 | #include <doctest/doctest.h> |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 14 | |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | template<typename armnn::DataType DataType> |
| 21 | INetworkPtr CreateBatchToSpaceNdNetwork(const armnn::TensorShape& inputShape, |
| 22 | const armnn::TensorShape& outputShape, |
| 23 | std::vector<unsigned int>& blockShape, |
| 24 | std::vector<std::pair<unsigned int, unsigned int>>& crops, |
| 25 | armnn::DataLayout dataLayout, |
| 26 | const float qScale = 1.0f, |
| 27 | const int32_t qOffset = 0) |
| 28 | { |
| 29 | using namespace armnn; |
| 30 | // Builds up the structure of the network. |
| 31 | INetworkPtr net(INetwork::Create()); |
| 32 | |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 33 | TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset, true); |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 34 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 35 | |
| 36 | BatchToSpaceNdDescriptor batchToSpaceNdDesc(blockShape, crops); |
| 37 | batchToSpaceNdDesc.m_DataLayout = dataLayout; |
| 38 | |
| 39 | IConnectableLayer* batchToSpaceNd = net->AddBatchToSpaceNdLayer(batchToSpaceNdDesc, "batchToSpaceNd"); |
| 40 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 41 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 42 | |
| 43 | Connect(batchToSpaceNd, output, outputTensorInfo, 0, 0); |
| 44 | Connect(input, batchToSpaceNd, inputTensorInfo, 0, 0); |
| 45 | |
| 46 | return net; |
| 47 | } |
| 48 | |
| 49 | template<armnn::DataType ArmnnType> |
| 50 | void BatchToSpaceNdEndToEnd(const std::vector<BackendId>& backends, armnn::DataLayout dataLayout) |
| 51 | { |
| 52 | using namespace armnn; |
| 53 | using T = ResolveType<ArmnnType>; |
| 54 | |
| 55 | std::vector<unsigned int> blockShape {2, 2}; |
| 56 | std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}}; |
| 57 | const TensorShape& inputShape = { 4, 1, 1, 1 }; |
| 58 | const TensorShape& outputShape = (dataLayout == DataLayout::NCHW) |
| 59 | ? std::initializer_list<unsigned int>({ 1, 1, 2, 2 }) |
| 60 | : std::initializer_list<unsigned int>({ 1, 2, 2, 1 }); |
| 61 | |
| 62 | // Builds up the structure of the network |
| 63 | INetworkPtr net = CreateBatchToSpaceNdNetwork<ArmnnType>(inputShape, outputShape, blockShape, crops, dataLayout); |
| 64 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 65 | CHECK(net); |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 66 | |
| 67 | // Creates structures for input & output. |
| 68 | std::vector<T> inputData{ 1, 2, 3, 4 }; |
| 69 | |
| 70 | std::vector<T> expectedOutput{ 1, 2, 3, 4 }; |
| 71 | |
| 72 | std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } }; |
| 73 | std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } }; |
| 74 | |
Mike Kelly | a9c3267 | 2023-12-04 17:23:09 +0000 | [diff] [blame] | 75 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(net), inputTensorData, expectedOutputData, backends); |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | template<armnn::DataType ArmnnType> |
| 79 | void BatchToSpaceNdComplexEndToEnd(const std::vector<BackendId>& backends, armnn::DataLayout dataLayout) |
| 80 | { |
| 81 | using namespace armnn; |
| 82 | using T = ResolveType<ArmnnType>; |
| 83 | |
| 84 | std::vector<unsigned int> blockShape {2, 2}; |
| 85 | std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {2, 0}}; |
| 86 | const TensorShape& inputShape = (dataLayout == DataLayout::NCHW) |
| 87 | ? std::initializer_list<unsigned int>({ 8, 1, 1, 3 }) |
| 88 | : std::initializer_list<unsigned int>({ 8, 1, 3, 1 }); |
| 89 | const TensorShape& outputShape = (dataLayout == DataLayout::NCHW) |
| 90 | ? std::initializer_list<unsigned int>({ 2, 1, 2, 4 }) |
| 91 | : std::initializer_list<unsigned int>({ 2, 2, 4, 1 }); |
| 92 | |
| 93 | // Builds up the structure of the network |
| 94 | INetworkPtr net = CreateBatchToSpaceNdNetwork<ArmnnType>(inputShape, outputShape, blockShape, crops, dataLayout); |
| 95 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 96 | CHECK(net); |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 97 | |
| 98 | // Creates structures for input & output. |
| 99 | std::vector<T> inputData{ |
| 100 | 0, 1, 3, 0, 9, 11, |
| 101 | 0, 2, 4, 0, 10, 12, |
| 102 | 0, 5, 7, 0, 13, 15, |
| 103 | 0, 6, 8, 0, 14, 16 |
| 104 | }; |
| 105 | |
| 106 | std::vector<T> expectedOutput{ |
| 107 | 1, 2, 3, 4, |
| 108 | 5, 6, 7, 8, |
| 109 | 9, 10, 11, 12, |
| 110 | 13, 14, 15, 16 |
| 111 | }; |
| 112 | |
| 113 | std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } }; |
| 114 | std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } }; |
| 115 | |
Mike Kelly | a9c3267 | 2023-12-04 17:23:09 +0000 | [diff] [blame] | 116 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(net), inputTensorData, expectedOutputData, backends); |
Francis Murtagh | e24e3cd | 2019-06-25 14:41:55 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // anonymous namespace |