Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 1 | // |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved. |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "AvgPool2DIgnoreValueChecker.hpp" |
Teresa Charlin | ca5a23a | 2023-12-15 14:20:47 +0000 | [diff] [blame] | 7 | #include "QuantizeChecker.hpp" |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 8 | #include "SplitChecker.hpp" |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 9 | #include <backendsCommon/test/ActivationEndToEndTestImpl.hpp> |
Cathal Corbett | 6d2647d | 2022-12-13 11:37:57 +0000 | [diff] [blame] | 10 | #include <armnn/IRuntime.hpp> |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 11 | |
| 12 | using namespace armnn; |
| 13 | using namespace tosa; |
| 14 | |
| 15 | TEST_SUITE("TosaOperatorMappingOneToManyTests") |
| 16 | { |
| 17 | TEST_CASE("GetTosaMapping_AvgPool2DIgnoreValueLayer") |
| 18 | { |
| 19 | armnn::Pooling2dDescriptor descriptor; |
| 20 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 21 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 22 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 23 | descriptor.m_PadLeft = 1; |
| 24 | descriptor.m_PadRight = 1; |
| 25 | descriptor.m_PadTop = 1; |
| 26 | descriptor.m_PadBottom = 1; |
| 27 | descriptor.m_PaddingMethod = armnn::PaddingMethod::IgnoreValue; |
| 28 | |
| 29 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 30 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 31 | |
| 32 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 33 | std::vector<std::vector<int32_t>> intermediateShape = {{ 1, 1, 6, 6 }}; |
| 34 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 35 | |
| 36 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 37 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 38 | VerifyAvgPool2DIgnoreValue(basicBlock, |
| 39 | inputShape, |
| 40 | outputShape, |
| 41 | intermediateShape, |
| 42 | descriptor); |
| 43 | } |
| 44 | |
| 45 | TEST_CASE("GetTosaMappingFromLayer_AvgPool2DIgnoreValueLayer") |
| 46 | { |
| 47 | IRuntime::CreationOptions options; |
| 48 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 49 | |
| 50 | // Builds up the structure of the network. |
| 51 | INetworkPtr net(INetwork::Create()); |
| 52 | |
| 53 | armnn::Pooling2dDescriptor descriptor; |
| 54 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 55 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 56 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 57 | descriptor.m_PadLeft = 1; |
| 58 | descriptor.m_PadRight = 1; |
| 59 | descriptor.m_PadTop = 1; |
| 60 | descriptor.m_PadBottom = 1; |
| 61 | descriptor.m_PaddingMethod = armnn::PaddingMethod::IgnoreValue; |
| 62 | |
| 63 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 64 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 65 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 66 | |
| 67 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 68 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 69 | |
| 70 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 71 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 72 | |
| 73 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 74 | std::vector<std::vector<int32_t>> intermediateShape = {{ 1, 1, 6, 6 }}; |
| 75 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 76 | |
| 77 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 78 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 79 | |
| 80 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 81 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 82 | VerifyAvgPool2DIgnoreValue(basicBlock, |
| 83 | inputShape, |
| 84 | outputShape, |
| 85 | intermediateShape, |
| 86 | descriptor); |
| 87 | } |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 88 | |
Teresa Charlin | ca5a23a | 2023-12-15 14:20:47 +0000 | [diff] [blame] | 89 | TEST_CASE("GetTosaMapping_QuantizeLayer") |
| 90 | { |
| 91 | NullDescriptor descriptor; |
| 92 | DataType outputDataType = DataType::Signed32; |
| 93 | |
| 94 | TensorInfo inputTensorInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 95 | TensorInfo outputTensorInfo({ 1, 3, 3, 1 }, outputDataType); |
| 96 | std::vector<int32_t> shape = { 1, 3, 3, 1 }; |
| 97 | |
| 98 | TosaSerializationBasicBlock* basicBlock = |
| 99 | GetTosaMapping(nullptr, LayerType::Quantize, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
| 100 | VerifyQuantize(basicBlock, shape, ArmNNToDType(DataType::Float32), ArmNNToDType(outputDataType)); |
| 101 | } |
| 102 | TEST_CASE("GetTosaMappingFromLayer_QuantizeLayer") |
| 103 | { |
| 104 | IRuntime::CreationOptions options; |
| 105 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 106 | // Builds up the structure of the network. |
| 107 | INetworkPtr net(INetwork::Create()); |
| 108 | NullDescriptor descriptor; |
| 109 | DataType outputDataType = DataType::Signed32; |
| 110 | |
| 111 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 112 | IConnectableLayer* quantize = net->AddQuantizeLayer("quantize"); |
| 113 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 114 | |
| 115 | input0->GetOutputSlot(0).Connect(quantize->GetInputSlot(0)); |
| 116 | quantize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 117 | |
| 118 | armnn::TensorInfo inputTensorInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 119 | armnn::TensorInfo outputTensorInfo({ 1, 3, 3, 1 }, outputDataType); |
| 120 | std::vector<int32_t> shape = { 1, 3, 3, 1 }; |
| 121 | |
| 122 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 123 | quantize->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 124 | |
| 125 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(quantize)); |
| 126 | VerifyQuantize(basicBlock, shape, ArmNNToDType(DataType::Float32), ArmNNToDType(outputDataType)); |
| 127 | } |
| 128 | |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 129 | TEST_CASE("GetTosaMapping_SplitLayer") |
| 130 | { |
| 131 | const unsigned int numViews = 3; |
| 132 | const unsigned int numDimensions = 4; |
| 133 | armnn::ViewsDescriptor descriptor(numViews, numDimensions); |
| 134 | descriptor.SetAxis(static_cast<int32_t>(1)); |
| 135 | |
| 136 | std::vector<std::vector<int32_t>> inShape = {{ 1, 18, 4, 4 }}; |
| 137 | std::vector<std::vector<int32_t>> outShape = {{ 1, 6, 4, 4 },{ 1, 6, 4, 4 },{ 1, 6, 4, 4 }}; |
| 138 | |
| 139 | armnn::TensorInfo inputTensorInfo({1, 18, 4, 4}, DataType::Float32); |
| 140 | armnn::TensorInfo outputTensorInfo({1, 6, 4, 4}, DataType::Float32); |
| 141 | |
Teresa Charlin | 7db7089 | 2024-04-23 13:43:03 +0100 | [diff] [blame^] | 142 | TosaSerializationBasicBlock* basicBlock = GetTosaMapping(nullptr, |
| 143 | LayerType::Splitter, |
| 144 | {&inputTensorInfo}, |
| 145 | {&outputTensorInfo, &outputTensorInfo, &outputTensorInfo}, |
| 146 | descriptor); |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 147 | |
| 148 | VerifySplit(basicBlock, |
| 149 | inShape, |
| 150 | outShape, |
| 151 | descriptor); |
| 152 | } |
| 153 | |
| 154 | TEST_CASE("GetTosaMappingFromLayer_SplitLayer") |
| 155 | { |
| 156 | IRuntime::CreationOptions options; |
| 157 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 158 | |
| 159 | // Builds up the structure of the network. |
| 160 | INetworkPtr net(INetwork::Create()); |
| 161 | |
| 162 | const unsigned int numViews = 3; |
| 163 | const unsigned int numDimensions = 4; |
| 164 | armnn::ViewsDescriptor descriptor(numViews, numDimensions); |
| 165 | descriptor.SetAxis(static_cast<int32_t>(1)); |
| 166 | |
| 167 | std::vector<std::vector<int32_t>> inShape = {{ 1, 18, 4, 4 }}; |
| 168 | std::vector<std::vector<int32_t>> outShape = {{ 1, 6, 4, 4 },{ 1, 6, 4, 4 },{ 1, 6, 4, 4 }}; |
| 169 | |
| 170 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 171 | IConnectableLayer* split = net->AddSplitterLayer(descriptor, "split"); |
| 172 | IConnectableLayer* output0 = net->AddOutputLayer(0, "output0"); |
| 173 | IConnectableLayer* output1 = net->AddOutputLayer(1, "output1"); |
| 174 | IConnectableLayer* output2 = net->AddOutputLayer(2, "output2"); |
| 175 | |
| 176 | input0->GetOutputSlot(0).Connect(split->GetInputSlot(0)); |
| 177 | split->GetOutputSlot(0).Connect(output0->GetInputSlot(0)); |
| 178 | split->GetOutputSlot(1).Connect(output1->GetInputSlot(0)); |
| 179 | split->GetOutputSlot(2).Connect(output2->GetInputSlot(0)); |
| 180 | |
| 181 | armnn::TensorInfo inputTensorInfo({1, 18, 4, 4}, DataType::Float32); |
| 182 | armnn::TensorInfo outputTensorInfo({1, 6, 4, 4}, DataType::Float32); |
| 183 | |
| 184 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 185 | split->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 186 | split->GetOutputSlot(1).SetTensorInfo(outputTensorInfo); |
| 187 | split->GetOutputSlot(2).SetTensorInfo(outputTensorInfo); |
| 188 | |
| 189 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(split)); |
| 190 | |
| 191 | VerifySplit(basicBlock, |
| 192 | inShape, |
| 193 | outShape, |
| 194 | descriptor); |
| 195 | } |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 196 | |
| 197 | // Activation |
| 198 | |
| 199 | static std::vector<BackendId> tosaDefaultBackends = { "TosaRef" }; |
| 200 | |
| 201 | TEST_CASE("GetTosaMapping_ActivationFloat32") |
| 202 | { |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 203 | ActivationEndToEndTest<DataType::Float32>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 1.f, 0, 0.01f); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Teresa Charlin | a42e006 | 2024-04-23 13:03:40 +0100 | [diff] [blame] | 206 | TEST_CASE("GetTosaMapping_ActivationFloat16") |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 207 | { |
Teresa Charlin | a42e006 | 2024-04-23 13:03:40 +0100 | [diff] [blame] | 208 | ActivationEndToEndTest<DataType::Float16>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 1.f, 0, 0.01f); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_CASE("GetTosaMapping_ActivationInt32") |
| 212 | { |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 213 | ActivationEndToEndTest<DataType::Signed32>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 0.15f, 0, 0.01f); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST_CASE("GetTosaMapping_ActivationInt16") |
| 217 | { |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 218 | ActivationEndToEndTest<DataType::QSymmS16>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 0.35f, 0, 0.01f); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST_CASE("GetTosaMapping_ActivationInt8") |
| 222 | { |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 223 | ActivationEndToEndTest<DataType::QSymmS8>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 0.75f, 0, 0.01f); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 226 | TEST_CASE("UNSUPPORTED_GetTosaMapping_ActivationUInt8") |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 227 | { |
| 228 | try |
| 229 | { |
Teresa Charlin | a4b6090 | 2024-02-07 20:55:53 +0000 | [diff] [blame] | 230 | ActivationEndToEndTest<DataType::QAsymmU8>(tosaDefaultBackends, ActivationFunction::LeakyReLu, 1.f, 0, 0.01f); |
| 231 | FAIL("An exception should have been thrown"); |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 232 | } |
| 233 | catch (armnn::Exception& e) |
| 234 | { |
| 235 | CHECK_EQ(std::string(e.what()), "Failed to assign a backend to each layer"); |
| 236 | } |
| 237 | } |
| 238 | } |