Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2024 Arm Limited. |
| 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 ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H |
| 25 | #define ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H |
| 26 | |
| 27 | #include "arm_compute/core/Utils.h" |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 29 | #include "tests/Globals.h" |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 30 | #include "tests/framework/Asserts.h" |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 31 | #include "tests/framework/Fixture.h" |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 32 | #include "tests/validation/Helpers.h" |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 33 | #include "tests/validation/Validation.h" |
| 34 | #include "tests/validation/reference/ScatterLayer.h" |
| 35 | #include "tests/SimpleTensor.h" |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 36 | |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 37 | #include <random> |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 38 | #include <cstdint> |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
| 46 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 47 | class ScatterGenericValidationFixture : public framework::Fixture |
| 48 | { |
| 49 | public: |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 50 | void setup(TensorShape src_shape, TensorShape updates_shape, TensorShape indices_shape, |
| 51 | TensorShape out_shape, DataType data_type, ScatterInfo scatter_info, bool inplace, |
| 52 | QuantizationInfo src_qinfo = QuantizationInfo(), QuantizationInfo o_qinfo = QuantizationInfo()) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 53 | { |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 54 | // this is for improving randomness across tests |
| 55 | _hash = src_shape[0] + src_shape[1] + src_shape[2] + src_shape[3] + src_shape[4] + src_shape[5] |
| 56 | + updates_shape[0] + updates_shape[1] + updates_shape[2] + updates_shape[3] |
| 57 | + updates_shape[4] + updates_shape[5] |
| 58 | + indices_shape[0] + indices_shape[1] + indices_shape[2] + indices_shape[3]; |
| 59 | |
| 60 | _target = compute_target(src_shape, updates_shape, indices_shape, out_shape, data_type, scatter_info, inplace, src_qinfo, o_qinfo); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 61 | _reference = compute_reference(src_shape, updates_shape, indices_shape, out_shape, data_type,scatter_info, src_qinfo , o_qinfo); |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | template <typename U> |
Mohammed Suhail Munshi | 0e21236 | 2024-04-08 14:38:31 +0100 | [diff] [blame] | 66 | void fill(U &&tensor, int i, float lo = -10.f, float hi = 10.f) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 67 | { |
| 68 | switch(tensor.data_type()) |
| 69 | { |
| 70 | case DataType::F32: |
| 71 | { |
| 72 | std::uniform_real_distribution<float> distribution(lo, hi); |
| 73 | library->fill(tensor, distribution, i); |
| 74 | break; |
| 75 | } |
| 76 | default: |
| 77 | { |
| 78 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 83 | // This is used to fill indices tensor with S32 datatype. |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 84 | // Used to prevent ONLY having values that are out of bounds. |
| 85 | template <typename U> |
| 86 | void fill_indices(U &&tensor, int i, const TensorShape &shape) |
| 87 | { |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 88 | // Calculate max indices the shape should contain. Add an arbitrary value to allow testing for some out of bounds values (In this case min dimension) |
| 89 | const int32_t max = std::max({shape[0] , shape[1], shape[2]}); |
| 90 | library->fill_tensor_uniform(tensor, i, static_cast<int32_t>(-2), static_cast<int32_t>(max)); |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 93 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, |
| 94 | const TensorShape &out_shape, DataType data_type, const ScatterInfo info, bool inplace, |
| 95 | QuantizationInfo a_qinfo, QuantizationInfo o_qinfo) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 96 | { |
| 97 | // 1. Create relevant tensors using ScatterInfo data structure. |
| 98 | // ---------------------------------------------------- |
| 99 | // In order - src, updates, indices, output. |
| 100 | TensorType src = create_tensor<TensorType>(shape_a, data_type, 1, a_qinfo); |
| 101 | TensorType updates = create_tensor<TensorType>(shape_b, data_type, 1, a_qinfo); |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 102 | TensorType indices = create_tensor<TensorType>(shape_c, DataType::S32, 1, QuantizationInfo()); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 103 | TensorType dst = create_tensor<TensorType>(out_shape, data_type, 1, o_qinfo); |
| 104 | |
| 105 | FunctionType scatter; |
| 106 | |
| 107 | // Configure operator |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 108 | // When scatter_info.zero_initialization is true, pass nullptr for src |
| 109 | // because dst does not need to be initialized with src values. |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 110 | if(info.zero_initialization) |
| 111 | { |
| 112 | scatter.configure(nullptr, &updates, &indices, &dst, info); |
| 113 | } |
| 114 | else |
| 115 | { |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 116 | if(inplace) |
| 117 | { |
| 118 | scatter.configure(&src, &updates, &indices, &src, info); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | scatter.configure(&src, &updates, &indices, &dst, info); |
| 123 | } |
Mohammed Suhail Munshi | 473b829 | 2024-03-18 12:13:30 +0000 | [diff] [blame] | 124 | } |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 125 | |
| 126 | // Assertions |
| 127 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 128 | ARM_COMPUTE_ASSERT(updates.info()->is_resizable()); |
| 129 | ARM_COMPUTE_ASSERT(indices.info()->is_resizable()); |
| 130 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
| 131 | |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 132 | add_padding_x({ &src, &updates, &indices}); |
| 133 | |
| 134 | if(!inplace) |
| 135 | { |
| 136 | add_padding_x({ &dst }); |
| 137 | } |
| 138 | |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 139 | // Allocate tensors |
| 140 | src.allocator()->allocate(); |
| 141 | updates.allocator()->allocate(); |
| 142 | indices.allocator()->allocate(); |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 143 | |
| 144 | if(!inplace) |
| 145 | { |
| 146 | dst.allocator()->allocate(); |
| 147 | } |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 148 | |
| 149 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 150 | ARM_COMPUTE_ASSERT(!updates.info()->is_resizable()); |
| 151 | ARM_COMPUTE_ASSERT(!indices.info()->is_resizable()); |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 152 | |
| 153 | if(!inplace) |
| 154 | { |
| 155 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
| 156 | } |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 157 | |
| 158 | // Fill update (a) and indices (b) tensors. |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 159 | fill(AccessorType(src), 0 + _hash); |
| 160 | fill(AccessorType(updates), 1+ _hash); |
| 161 | fill_indices(AccessorType(indices), 2 + _hash, out_shape); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 162 | |
| 163 | scatter.run(); |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 164 | |
| 165 | if(inplace) |
| 166 | { |
| 167 | return src; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | return dst; |
| 172 | } |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 175 | SimpleTensor<T> compute_reference(const TensorShape &a_shape, const TensorShape &b_shape, const TensorShape &c_shape, |
| 176 | const TensorShape &out_shape, DataType data_type, ScatterInfo info, QuantizationInfo a_qinfo, QuantizationInfo o_qinfo) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 177 | { |
| 178 | // Output Quantization not currently in use - fixture should be extended to support this. |
| 179 | ARM_COMPUTE_UNUSED(o_qinfo); |
Mohammed Suhail Munshi | 0e21236 | 2024-04-08 14:38:31 +0100 | [diff] [blame] | 180 | TensorShape src_shape = a_shape; |
| 181 | TensorShape updates_shape = b_shape; |
| 182 | TensorShape indices_shape = c_shape; |
| 183 | |
| 184 | // 1. Collapse batch index into a single dim if necessary for update tensor and indices tensor. |
| 185 | if(c_shape.num_dimensions() >= 3) |
| 186 | { |
| 187 | indices_shape = indices_shape.collapsed_from(1); |
| 188 | updates_shape = updates_shape.collapsed_from(updates_shape.num_dimensions() - 2); // Collapses from last 2 dims |
| 189 | } |
| 190 | |
| 191 | // 2. Collapse data dims into a single dim. |
| 192 | // Collapse all src dims into 2 dims. First one holding data, the other being the index we iterate over. |
| 193 | src_shape.collapse(updates_shape.num_dimensions() - 1); // Collapse all data dims into single dim. |
| 194 | src_shape = src_shape.collapsed_from(1); // Collapse all index dims into a single dim |
| 195 | updates_shape.collapse(updates_shape.num_dimensions() - 1); // Collapse data dims (all except last dim which is batch dim) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 196 | |
| 197 | // Create reference tensors |
| 198 | SimpleTensor<T> src{ a_shape, data_type, 1, a_qinfo }; |
| 199 | SimpleTensor<T> updates{b_shape, data_type, 1, QuantizationInfo() }; |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 200 | SimpleTensor<int32_t> indices{ c_shape, DataType::S32, 1, QuantizationInfo() }; |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 201 | |
| 202 | // Fill reference |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 203 | fill(src, 0 + _hash); |
| 204 | fill(updates, 1 + _hash); |
| 205 | fill_indices(indices, 2 + _hash, out_shape); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 206 | |
| 207 | // Calculate individual reference. |
Mohammed Suhail Munshi | 7377107 | 2024-03-25 15:55:42 +0000 | [diff] [blame] | 208 | return reference::scatter_layer<T>(src, updates, indices, out_shape, info); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TensorType _target{}; |
| 212 | SimpleTensor<T> _reference{}; |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 213 | int32_t _hash{}; |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | // This fixture will use the same shape for updates as indices. |
| 217 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 218 | class ScatterValidationFixture : public ScatterGenericValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 219 | { |
| 220 | public: |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 221 | void setup(TensorShape src_shape, TensorShape update_shape, TensorShape indices_shape, |
| 222 | TensorShape out_shape, DataType data_type, ScatterFunction func, bool zero_init, bool inplace) |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 223 | { |
Gunes Bayir | ada3200 | 2024-04-24 10:27:13 +0100 | [diff] [blame^] | 224 | ScatterGenericValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, update_shape, |
| 225 | indices_shape, out_shape, data_type, ScatterInfo(func, zero_init), inplace, |
| 226 | QuantizationInfo(), QuantizationInfo()); |
Mohammed Suhail Munshi | 8609ca0 | 2024-02-29 17:00:07 +0000 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| 230 | } // namespace validation |
| 231 | } // namespace test |
| 232 | } // namespace arm_compute |
| 233 | #endif // ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H |