Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 1 | // |
Tianle Cheng | 7790dc6 | 2023-12-12 13:52:22 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <armnn/INetwork.hpp> |
| 8 | |
| 9 | #include <CommonTestUtils.hpp> |
| 10 | #include <ResolveType.hpp> |
| 11 | |
| 12 | #include <doctest/doctest.h> |
| 13 | |
| 14 | namespace |
| 15 | { |
| 16 | |
| 17 | template<typename armnn::DataType DataType> |
| 18 | armnn::INetworkPtr CreateSliceNetwork(const armnn::TensorShape& inputShape, |
| 19 | const armnn::TensorShape& outputShape, |
| 20 | const armnn::SliceDescriptor& descriptor, |
| 21 | const float qScale = 1.0f, |
| 22 | const int32_t qOffset = 0) |
| 23 | { |
| 24 | using namespace armnn; |
| 25 | |
| 26 | INetworkPtr network(INetwork::Create()); |
| 27 | |
| 28 | TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset, true); |
| 29 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 30 | |
| 31 | |
| 32 | IConnectableLayer* slice = network->AddSliceLayer(descriptor, "slice"); |
| 33 | IConnectableLayer* input = network->AddInputLayer(0, "input"); |
| 34 | IConnectableLayer* output = network->AddOutputLayer(0, "output"); |
| 35 | |
| 36 | Connect(input, slice, inputTensorInfo, 0, 0); |
| 37 | Connect(slice, output, outputTensorInfo, 0, 0); |
| 38 | |
| 39 | return network; |
| 40 | } |
| 41 | |
| 42 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 43 | void SliceEndToEnd(const std::vector<armnn::BackendId>& backends) |
| 44 | { |
| 45 | using namespace armnn; |
| 46 | |
| 47 | const TensorShape& inputShape = { 3, 2, 3 }; |
| 48 | const TensorShape& outputShape = { 2, 1, 3 }; |
| 49 | |
| 50 | SliceDescriptor descriptor; |
| 51 | descriptor.m_Begin = { 1, 0, 0 }; |
| 52 | descriptor.m_Size = { 2, 1, 3 }; |
| 53 | |
| 54 | INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor); |
| 55 | |
| 56 | CHECK(network); |
| 57 | |
| 58 | std::vector<T> inputData{ 1, 1, 1, 2, 2, 2, |
| 59 | 3, 3, 3, 4, 4, 4, |
| 60 | 5, 5, 5, 6, 6, 6 }; |
| 61 | std::vector<T> expectedOutput{ 3, 3, 3, |
| 62 | 5, 5, 5 }; |
| 63 | |
| 64 | std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } }; |
| 65 | std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } }; |
| 66 | |
| 67 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends); |
| 68 | } |
| 69 | |
| 70 | template<armnn::DataType ArmnnType> |
| 71 | void SliceEndToEndFloat16(const std::vector<armnn::BackendId>& backends) |
| 72 | { |
| 73 | using namespace armnn; |
| 74 | using namespace half_float::literal; |
| 75 | using Half = half_float::half; |
| 76 | |
| 77 | const TensorShape& inputShape = { 3, 2, 3 }; |
| 78 | const TensorShape& outputShape = { 2, 1, 3 }; |
| 79 | |
| 80 | SliceDescriptor descriptor; |
| 81 | descriptor.m_Begin = { 1, 0, 0 }; |
| 82 | descriptor.m_Size = { 2, 1, 3 }; |
| 83 | |
| 84 | INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor); |
| 85 | CHECK(network); |
| 86 | |
| 87 | std::vector<Half> inputData{ 1._h, 1._h, 1._h, 2._h, 2._h, 2._h, |
| 88 | 3._h, 3._h, 3._h, 4._h, 4._h, 4._h, |
| 89 | 5._h, 5._h, 5._h, 6._h, 6._h, 6._h }; |
| 90 | std::vector<Half> expectedOutput{ 3._h, 3._h, 3._h, |
| 91 | 5._h, 5._h, 5._h }; |
| 92 | |
| 93 | std::map<int, std::vector<Half>> inputTensorData = { { 0, inputData } }; |
| 94 | std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } }; |
| 95 | |
| 96 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends); |
| 97 | } |
| 98 | |
Tianle Cheng | 7790dc6 | 2023-12-12 13:52:22 +0000 | [diff] [blame] | 99 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 100 | void SliceEndToEnd4Dim(const std::vector<armnn::BackendId>& backends) |
| 101 | { |
| 102 | using namespace armnn; |
| 103 | |
| 104 | const TensorShape& inputShape = { 2, 3, 2, 3 }; |
| 105 | const TensorShape& outputShape = { 1, 3, 2, 1 }; |
| 106 | |
| 107 | SliceDescriptor descriptor; |
| 108 | descriptor.m_Begin = { 1, 0, 0, 0 }; |
| 109 | descriptor.m_Size = { 1, 3, 2, 1 }; |
| 110 | |
| 111 | INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor); |
| 112 | |
| 113 | CHECK(network); |
| 114 | |
| 115 | std::vector<T> inputData{ 1, 1, 1, 2, 2, 2, |
| 116 | 3, 3, 3, 4, 4, 4, |
| 117 | 5, 5, 5, 6, 6, 6, |
| 118 | 1, 1, 1, 2, 2, 2, |
| 119 | 3, 3, 3, 4, 4, 4, |
| 120 | 5, 5, 5, 6, 6, 6 }; |
| 121 | |
| 122 | std::vector<T> expectedOutput{ 1, 2, |
| 123 | 3, 4, |
| 124 | 5, 6 }; |
| 125 | |
| 126 | std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } }; |
| 127 | std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } }; |
| 128 | |
| 129 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends); |
| 130 | } |
| 131 | |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 132 | } // anonymous namespace |