Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 945b8da | 2023-07-12 11:54:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/Tensor.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/SoftmaxLayer.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false> |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 46 | class SoftmaxValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 49 | void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 50 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 51 | _quantization_info = quantization_info; |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 52 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 53 | _reference = compute_reference(shape, data_type, quantization_info, beta, axis); |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 54 | _target = compute_target(shape, data_type, quantization_info, beta, axis); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | protected: |
| 58 | template <typename U> |
| 59 | void fill(U &&tensor) |
| 60 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 61 | if(tensor.data_type() == DataType::F32) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 62 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 63 | std::uniform_real_distribution<float> distribution(-10.0f, 10.0f); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 64 | library->fill(tensor, distribution, 0); |
| 65 | } |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 66 | else if(tensor.data_type() == DataType::F16) |
| 67 | { |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 68 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -10.0f, 10.0f }; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 69 | library->fill(tensor, distribution, 0); |
| 70 | } |
| 71 | else if(!is_data_type_quantized(tensor.data_type())) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 72 | { |
| 73 | std::uniform_int_distribution<> distribution(0, 100); |
| 74 | library->fill(tensor, distribution, 0); |
| 75 | } |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 76 | else |
| 77 | { |
| 78 | library->fill_tensor_uniform(tensor, 0); |
| 79 | } |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 82 | TensorType compute_target(const TensorShape &shape, DataType data_type, |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 83 | QuantizationInfo quantization_info, float beta, int32_t axis) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 84 | { |
| 85 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 86 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 87 | TensorType dst = create_tensor<TensorType>(shape, data_type, 1, get_softmax_output_quantization_info(data_type, IS_LOG)); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 88 | |
| 89 | // Create and configure function |
| 90 | FunctionType smx_layer; |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 91 | smx_layer.configure(&src, &dst, beta, axis); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 92 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 93 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 94 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 95 | |
| 96 | // Allocate tensors |
| 97 | src.allocator()->allocate(); |
| 98 | dst.allocator()->allocate(); |
| 99 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 101 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 102 | |
| 103 | // Fill tensors |
| 104 | fill(AccessorType(src)); |
| 105 | |
| 106 | // Compute function |
| 107 | smx_layer.run(); |
| 108 | |
| 109 | return dst; |
| 110 | } |
| 111 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 112 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 113 | QuantizationInfo quantization_info, float beta, int32_t axis) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 114 | { |
| 115 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 116 | SimpleTensor<T> src{ shape, data_type, 1, quantization_info }; |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 117 | |
| 118 | // Fill reference |
| 119 | fill(src); |
| 120 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 121 | return reference::softmax_layer<T>(src, beta, axis, IS_LOG); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 124 | TensorType _target{}; |
| 125 | SimpleTensor<T> _reference{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 126 | QuantizationInfo _quantization_info{}; |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 129 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false> |
| 130 | class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG> |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 131 | { |
| 132 | public: |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 133 | void setup(TensorShape shape, DataType data_type, float beta, size_t axis) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 134 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 135 | SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape, |
| 136 | data_type, |
| 137 | QuantizationInfo(), |
| 138 | beta, |
| 139 | axis); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 143 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false> |
| 144 | class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG> |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 145 | { |
| 146 | public: |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 147 | void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 148 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 149 | SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape, |
| 150 | data_type, |
| 151 | quantization_info, |
| 152 | beta, |
| 153 | axis); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 154 | } |
| 155 | }; |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 156 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 157 | } // namespace validation |
| 158 | } // namespace test |
| 159 | } // namespace arm_compute |
| 160 | #endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */ |