Compute Library
 22.11
Gather.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, 2022 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 
25 #include "Gather.h"
26 
27 #include "arm_compute/core/Types.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());
44  SimpleTensor<T> dst(dst_shape, src.data_type());
45 
46  Window win;
47  win.use_tensor_dimensions(dst_shape);
48  if(indices.shape().num_dimensions() == 1u)
49  {
50  execute_window_loop(win, [&](const Coordinates & id)
51  {
53  for(unsigned int dim = 0; dim < id.num_dimensions(); ++dim)
54  {
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  }
63  }
64  *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset));
65  });
66  }
67  else
68  {
69  if(actual_axis == 1)
70  {
71  win.set(Window::DimX, Window::Dimension(0, 1, 1));
72  execute_window_loop(win, [&](const Coordinates & id)
73  {
74  auto *dst_ptr = dst(id);
75  Coordinates index_offset;
76  for(uint32_t k = 0; k < indices.shape().num_dimensions(); ++k)
77  {
78  index_offset.set(k, id[k + 1]);
79  }
80  const uint32_t row = *reinterpret_cast<const uint32_t *>(indices(index_offset));
81  Coordinates src_offset;
82  src_offset.set(0, 0);
83  src_offset.set(1, row);
84  for(uint32_t j = 2; j < src.shape().num_dimensions(); ++j)
85  {
86  src_offset.set(j, id[1 + indices.shape().num_dimensions() + (j - 2)]);
87  }
88  const auto in_ptr_row = src(src_offset);
89  memcpy(dst_ptr, in_ptr_row, src.shape()[0] * src.element_size());
90  });
91  }
92  else
93  {
94  ARM_COMPUTE_ERROR("Not implemented.");
95  }
96  }
97 
98  return dst;
99 }
100 
101 template SimpleTensor<float> gather(const SimpleTensor<float> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis);
102 template SimpleTensor<half> gather(const SimpleTensor<half> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis);
103 template SimpleTensor<uint16_t> gather(const SimpleTensor<uint16_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis);
104 template SimpleTensor<uint8_t> gather(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis);
105 } // namespace reference
106 } // namespace validation
107 } // namespace test
108 } // namespace arm_compute
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1084
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: SimpleTensor.h:326
Shape of a tensor.
Definition: TensorShape.h:39
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
SimpleTensor< float > src
Definition: DFT.cpp:155
SimpleTensor< T > gather(const SimpleTensor< T > &src, const SimpleTensor< uint32_t > &indices, uint32_t actual_axis)
Definition: Gather.cpp:40
Copyright (c) 2017-2022 Arm Limited.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Coordinates of an item.
Definition: Coordinates.h:37
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Describe a multidimensional execution window.
Definition: Window.h:39
TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
Calculate the gather output shape of a tensor.
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:418