Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 1 | // |
Keith Davis | 69e653f | 2020-07-02 11:49:26 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "LogSoftmaxTestImpl.hpp" |
| 7 | |
| 8 | #include <Half.hpp> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 9 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 10 | #include <ResolveType.hpp> |
| 11 | |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 12 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 13 | #include <armnn/backends/TensorHandle.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 14 | #include <armnn/backends/IBackendInternal.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnn/backends/WorkloadFactory.hpp> |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 16 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 17 | #include <armnnTestUtils/TensorCopyUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 18 | #include <armnnTestUtils/WorkloadTestUtils.hpp> |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 19 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 20 | #include <TensorHelpers.hpp> |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | template<armnn::DataType ArmnnType, |
| 26 | std::size_t NumDims, |
| 27 | typename T = armnn::ResolveType<ArmnnType>> |
| 28 | LayerTestResult<T, NumDims> LogSoftmaxTestImpl( |
| 29 | armnn::IWorkloadFactory& workloadFactory, |
| 30 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 31 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 32 | const armnn::TensorInfo& inputInfo, |
| 33 | const armnn::TensorInfo& outputInfo, |
| 34 | const std::vector<float>& inputValues, |
| 35 | const std::vector<float>& expectedOutputValues, |
| 36 | armnn::LogSoftmaxQueueDescriptor descriptor, |
| 37 | float qScale = 1.0f, |
| 38 | int32_t qOffset = 0) |
| 39 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 40 | IgnoreUnused(memoryManager); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 41 | |
| 42 | auto inputTensor = armnnUtils::QuantizedVector<T>(inputValues, qScale, qOffset); |
| 43 | |
| 44 | std::vector<T> actualOutput(outputInfo.GetNumElements()); |
| 45 | std::vector<T> expectedOutput = armnnUtils::QuantizedVector<T>(expectedOutputValues, qScale, qOffset); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 46 | |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 47 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputInfo); |
| 48 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputInfo); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 49 | |
| 50 | armnn::WorkloadInfo info; |
| 51 | |
| 52 | AddInputToWorkload(descriptor, info, inputInfo, inputHandle.get()); |
| 53 | AddOutputToWorkload(descriptor, info, outputInfo, outputHandle.get()); |
| 54 | |
| 55 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateLogSoftmax(descriptor, info); |
| 56 | |
| 57 | inputHandle->Allocate(); |
| 58 | outputHandle->Allocate(); |
| 59 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 60 | CopyDataToITensorHandle(inputHandle.get(), inputTensor.data()); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 61 | |
Keith Davis | 69e653f | 2020-07-02 11:49:26 +0100 | [diff] [blame] | 62 | ExecuteWorkload(*workload, memoryManager); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 63 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 64 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 65 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 66 | return LayerTestResult<T, NumDims>(actualOutput, |
| 67 | expectedOutput, |
| 68 | outputHandle->GetShape(), |
| 69 | outputInfo.GetShape()); |
| 70 | |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | template<armnn::DataType ArmnnType, typename T> |
| 76 | LayerTestResult<T, 4> LogSoftmaxTest1( |
| 77 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 78 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 79 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 80 | { |
| 81 | const armnn::TensorShape inputOutputShape{1, 1, 2, 4}; |
| 82 | |
| 83 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 84 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 85 | |
| 86 | std::vector<float> inputValues |
| 87 | { |
| 88 | 0.f, -6.f, 2.f, 4.f, |
| 89 | 3.f, -2.f, 10.f, 1.f |
| 90 | }; |
| 91 | |
| 92 | std::vector<float> expectedOutputValues |
| 93 | { |
| 94 | -4.14297f, -10.14297f, -2.14297f, -0.14297f, |
| 95 | -7.00104f, -12.00104f, -0.00105f, -9.00104f |
| 96 | }; |
| 97 | |
| 98 | armnn::LogSoftmaxQueueDescriptor descriptor; |
| 99 | descriptor.m_Parameters.m_Beta = 1.0f; // default beta |
| 100 | descriptor.m_Parameters.m_Axis = -1; // default axis |
| 101 | |
| 102 | return LogSoftmaxTestImpl<ArmnnType, 4>( |
| 103 | workloadFactory, |
| 104 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 105 | tensorHandleFactory, |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 106 | inputTensorInfo, |
| 107 | outputTensorInfo, |
| 108 | inputValues, |
| 109 | expectedOutputValues, |
| 110 | descriptor); |
| 111 | } |
| 112 | |
| 113 | template<armnn::DataType ArmnnType, typename T> |
| 114 | LayerTestResult<T, 4> LogSoftmaxTest2( |
| 115 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 116 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 117 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 118 | { |
| 119 | const armnn::TensorShape inputOutputShape{1, 1, 2, 4}; |
| 120 | |
| 121 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 122 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 123 | |
| 124 | std::vector<float> inputValues |
| 125 | { |
| 126 | 0.f, -6.f, 2.f, 4.f, |
| 127 | 3.f, -2.f, 10.f, 1.f |
| 128 | }; |
| 129 | |
| 130 | std::vector<float> expectedOutputValues |
| 131 | { |
| 132 | -4.14297f, -10.14297f, -2.14297f, -0.14297f, |
| 133 | -7.00104f, -12.00104f, -0.00105f, -9.00104f |
| 134 | }; |
| 135 | |
| 136 | armnn::LogSoftmaxQueueDescriptor descriptor; |
| 137 | descriptor.m_Parameters.m_Beta = 1.0f; // default beta |
| 138 | descriptor.m_Parameters.m_Axis = 3; // positive axis |
| 139 | |
| 140 | return LogSoftmaxTestImpl<ArmnnType, 4>( |
| 141 | workloadFactory, |
| 142 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 143 | tensorHandleFactory, |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 144 | inputTensorInfo, |
| 145 | outputTensorInfo, |
| 146 | inputValues, |
| 147 | expectedOutputValues, |
| 148 | descriptor); |
| 149 | } |
| 150 | |
| 151 | template<armnn::DataType ArmnnType, typename T> |
| 152 | LayerTestResult<T, 4> LogSoftmaxTest3( |
| 153 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 154 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 155 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 156 | { |
| 157 | const armnn::TensorShape inputOutputShape{1, 1, 2, 4}; |
| 158 | |
| 159 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 160 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 161 | |
| 162 | std::vector<float> inputValues |
| 163 | { |
| 164 | 0.0f, -0.6f, 0.2f, 0.4f, |
| 165 | 0.3f, -0.2f, 1.0f, 0.1f |
| 166 | }; |
| 167 | |
| 168 | std::vector<float> expectedOutputValues |
| 169 | { |
| 170 | -4.14297f, -10.14297f, -2.14297f, -0.14297f, |
| 171 | -7.00104f, -12.00104f, -0.00105f, -9.00104f |
| 172 | }; |
| 173 | |
| 174 | armnn::LogSoftmaxQueueDescriptor descriptor; |
| 175 | descriptor.m_Parameters.m_Beta = 10.0f; // non-default beta |
| 176 | descriptor.m_Parameters.m_Axis = 3; // positive axis |
| 177 | |
| 178 | return LogSoftmaxTestImpl<ArmnnType, 4>( |
| 179 | workloadFactory, |
| 180 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 181 | tensorHandleFactory, |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 182 | inputTensorInfo, |
| 183 | outputTensorInfo, |
| 184 | inputValues, |
| 185 | expectedOutputValues, |
| 186 | descriptor); |
| 187 | } |
| 188 | |
| 189 | template<armnn::DataType ArmnnType, typename T> |
| 190 | LayerTestResult<T, 4> LogSoftmaxTest4( |
| 191 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 192 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 193 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 194 | { |
| 195 | const armnn::TensorShape inputOutputShape{1, 1, 2, 4}; |
| 196 | |
| 197 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 198 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 199 | |
| 200 | std::vector<float> inputValues |
| 201 | { |
| 202 | 0.f, -6.f, 2.f, 4.f, |
| 203 | 3.f, -2.f, 10.f, 1.f |
| 204 | }; |
| 205 | |
| 206 | std::vector<float> expectedOutputValues |
| 207 | { |
| 208 | -3.048587f, -4.018149f, -8.000336f, -0.048587f, |
| 209 | -0.048587f, -0.018149f, -0.000335f, -3.048587f |
| 210 | }; |
| 211 | |
| 212 | armnn::LogSoftmaxQueueDescriptor descriptor; |
| 213 | descriptor.m_Parameters.m_Beta = 1.0f; // default beta |
| 214 | descriptor.m_Parameters.m_Axis = -2; // negative axis |
| 215 | |
| 216 | return LogSoftmaxTestImpl<ArmnnType, 4>( |
| 217 | workloadFactory, |
| 218 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 219 | tensorHandleFactory, |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 220 | inputTensorInfo, |
| 221 | outputTensorInfo, |
| 222 | inputValues, |
| 223 | expectedOutputValues, |
| 224 | descriptor); |
| 225 | } |
| 226 | |
| 227 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 4> |
| 228 | LogSoftmaxTest1<armnn::DataType::Float32>( |
| 229 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 230 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 231 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 232 | |
| 233 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 4> |
| 234 | LogSoftmaxTest2<armnn::DataType::Float32>( |
| 235 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 236 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 237 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 238 | |
| 239 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 4> |
| 240 | LogSoftmaxTest3<armnn::DataType::Float32>( |
| 241 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 242 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 243 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 244 | |
| 245 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 4> |
| 246 | LogSoftmaxTest4<armnn::DataType::Float32>( |
| 247 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 248 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 249 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 250 | |
| 251 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 4> |
| 252 | LogSoftmaxTest1<armnn::DataType::Float16>( |
| 253 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 254 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 255 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 256 | |
| 257 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 4> |
| 258 | LogSoftmaxTest2<armnn::DataType::Float16>( |
| 259 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 260 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 261 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 262 | |
| 263 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 4> |
| 264 | LogSoftmaxTest3<armnn::DataType::Float16>( |
| 265 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 266 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 267 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 268 | |
| 269 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 4> |
| 270 | LogSoftmaxTest4<armnn::DataType::Float16>( |
| 271 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 272 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 273 | const armnn::ITensorHandleFactory& tensorHandleFactory); |