Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, 2022-2023 Arm Limited. |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [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 | |
| 25 | #include "Gather.h" |
| 26 | |
| 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 29 | #include "tests/validation/Helpers.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | template <typename T> |
| 40 | SimpleTensor<T> gather(const SimpleTensor<T> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis) |
| 41 | { |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 42 | const TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_gather_shape(src.shape(), indices.shape(), actual_axis); |
| 43 | SimpleTensor<T> dst(dst_shape, src.data_type()); |
| 44 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 45 | const auto src_ptr = static_cast<const T *>(src.data()); |
| 46 | const auto indices_ptr = static_cast<const uint32_t *>(indices.data()); |
| 47 | const auto dst_ptr = static_cast<T *>(dst.data()); |
| 48 | |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 49 | const uint32_t index_limit = src.shape()[actual_axis]; |
| 50 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 51 | Window win; |
| 52 | win.use_tensor_dimensions(dst_shape); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 53 | |
| 54 | execute_window_loop(win, [&](const Coordinates &dst_coords) { |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 55 | const auto dst_addr = coords2index(dst.shape(), dst_coords); |
| 56 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 57 | // Calculate the coordinates of the index value. |
| 58 | Coordinates idx_coords; |
| 59 | |
| 60 | for(size_t i = 0; i < indices.shape().num_dimensions(); ++i) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 61 | { |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 62 | idx_coords.set(i, dst_coords[i + actual_axis]); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 63 | } |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 64 | |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 65 | const auto index = indices_ptr[coords2index(indices.shape(), idx_coords)]; |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 66 | |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 67 | if(index < index_limit) |
Pablo Marquez Tello | 894659a | 2022-05-13 12:20:16 +0100 | [diff] [blame] | 68 | { |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 69 | // Calculate the coordinates of the source data. |
| 70 | Coordinates src_coords; |
| 71 | |
| 72 | for(size_t i = 0; i < actual_axis; ++i) |
| 73 | { |
| 74 | src_coords.set(i, dst_coords[i]); |
| 75 | } |
| 76 | |
| 77 | src_coords.set(actual_axis, index); |
| 78 | |
| 79 | for(size_t i = actual_axis + 1; i < src.shape().num_dimensions(); ++i) |
| 80 | { |
| 81 | src_coords.set(i, dst_coords[i + indices.shape().num_dimensions() - 1]); |
| 82 | } |
| 83 | |
| 84 | // Copy the data. |
| 85 | const auto src_addr = coords2index(src.shape(), src_coords); |
| 86 | dst_ptr[dst_addr] = src_ptr[src_addr]; |
Pablo Marquez Tello | 894659a | 2022-05-13 12:20:16 +0100 | [diff] [blame] | 87 | } |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 88 | else |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 89 | { |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 90 | dst_ptr[dst_addr] = 0; |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 91 | } |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 92 | }); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 93 | |
| 94 | return dst; |
| 95 | } |
| 96 | |
| 97 | template SimpleTensor<float> gather(const SimpleTensor<float> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 98 | template SimpleTensor<half> gather(const SimpleTensor<half> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 99 | template SimpleTensor<uint16_t> gather(const SimpleTensor<uint16_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 100 | template SimpleTensor<uint8_t> gather(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 101 | } // namespace reference |
| 102 | } // namespace validation |
| 103 | } // namespace test |
Pablo Marquez Tello | 894659a | 2022-05-13 12:20:16 +0100 | [diff] [blame] | 104 | } // namespace arm_compute |