Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019, 2022 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 | { |
| 42 | const auto *indices_ptr = static_cast<const uint32_t *>(indices.data()); |
| 43 | const TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_gather_shape(src.shape(), indices.shape(), actual_axis); |
| 44 | SimpleTensor<T> dst(dst_shape, src.data_type()); |
| 45 | |
| 46 | Window win; |
| 47 | win.use_tensor_dimensions(dst_shape); |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 48 | if(indices.shape().num_dimensions() == 1u) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 49 | { |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 50 | execute_window_loop(win, [&](const Coordinates & id) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 51 | { |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 52 | Coordinates offset; |
| 53 | for(unsigned int dim = 0; dim < id.num_dimensions(); ++dim) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 54 | { |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 55 | if(dim == actual_axis) |
| 56 | { |
| 57 | offset.set(dim, indices_ptr[id[dim]]); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | offset.set(dim, id[dim]); |
| 62 | } |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 63 | } |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 64 | *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset)); |
| 65 | }); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | if(actual_axis == 0) |
| 70 | { |
| 71 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 72 | uint32_t index = 0; |
| 73 | execute_window_loop(win, [&](const Coordinates & id) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 74 | { |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 75 | auto *dst_ptr = reinterpret_cast<T *>(dst(id)); |
| 76 | const int row_to_copy = indices[index++]; |
| 77 | std::copy_n(src.data() + row_to_copy * src.shape()[0], src.shape()[0], dst_ptr); |
| 78 | }); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 79 | } |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 80 | } |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 81 | |
| 82 | return dst; |
| 83 | } |
| 84 | |
| 85 | template SimpleTensor<float> gather(const SimpleTensor<float> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 86 | template SimpleTensor<half> gather(const SimpleTensor<half> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 87 | template SimpleTensor<uint16_t> gather(const SimpleTensor<uint16_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 88 | template SimpleTensor<uint8_t> gather(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis); |
| 89 | } // namespace reference |
| 90 | } // namespace validation |
| 91 | } // namespace test |
Pablo Marquez Tello | 0db8b8b | 2022-04-27 11:46:31 +0100 | [diff] [blame^] | 92 | } // namespace arm_compute |