| 39 | { |
| 40 | template <typename T> |
| 41 | SimpleTensor<T> gather(const SimpleTensor<T> &src, const SimpleTensor<uint32_t> &indices, uint32_t actual_axis) |
| 42 | { |
| 43 | const TensorShape dst_shape = |
| 44 | arm_compute::misc::shape_calculator::compute_gather_shape(src.shape(), indices.shape(), actual_axis); |
| 45 | SimpleTensor<T> dst(dst_shape, src.data_type()); |
| 46 | |
| 47 | const auto src_ptr = static_cast<const T *>(src.data()); |
| 48 | const auto indices_ptr = static_cast<const uint32_t *>(indices.data()); |
| 49 | const auto dst_ptr = static_cast<T *>(dst.data()); |
| 50 | |
| 51 | const uint32_t index_limit = src.shape()[actual_axis]; |
| 52 | |
| 53 | Window win; |
| 54 | win.use_tensor_dimensions(dst_shape); |
| 55 | |
| 56 | execute_window_loop(win, |
| 57 | [&](const Coordinates &dst_coords) |
| 58 | { |
| 59 | const auto dst_addr = coords2index(dst.shape(), dst_coords); |
| 60 | |
| 61 | // Calculate the coordinates of the index value. |
| 62 | Coordinates idx_coords; |
| 63 | |
| 64 | for (size_t i = 0; i < indices.shape().num_dimensions(); ++i) |
| 65 | { |
| 66 | idx_coords.set(i, dst_coords[i + actual_axis]); |
| 67 | } |
| 68 | |
| 69 | const auto index = indices_ptr[coords2index(indices.shape(), idx_coords)]; |
| 70 | |
| 71 | if (index < index_limit) |
| 72 | { |
| 73 | // Calculate the coordinates of the source data. |
| 74 | Coordinates src_coords; |
| 75 | |
| 76 | for (size_t i = 0; i < actual_axis; ++i) |
| 77 | { |
| 78 | src_coords.set(i, dst_coords[i]); |
| 79 | } |
| 80 | |
| 81 | src_coords.set(actual_axis, index); |
| 82 | |
| 83 | for (size_t i = actual_axis + 1; i < src.shape().num_dimensions(); ++i) |
| 84 | { |
| 85 | src_coords.set(i, dst_coords[i + indices.shape().num_dimensions() - 1]); |
| 86 | } |
| 87 | |
| 88 | // Copy the data. |
| 89 | const auto src_addr = coords2index(src.shape(), src_coords); |
| 90 | dst_ptr[dst_addr] = src_ptr[src_addr]; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | dst_ptr[dst_addr] = 0; |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | return dst; |
no test coverage detected