| 37 | { |
| 38 | template <typename T> |
| 39 | SimpleTensor<T> reverse(const SimpleTensor<T> &src, const SimpleTensor<int32_t> &axis, bool use_inverted_axis) |
| 40 | { |
| 41 | ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4); |
| 42 | ARM_COMPUTE_ERROR_ON(axis.shape().num_dimensions() > 1); |
| 43 | ARM_COMPUTE_ERROR_ON(axis.shape().x() > 4); |
| 44 | |
| 45 | // Create reference |
| 46 | SimpleTensor<T> dst{src.shape(), src.data_type(), src.num_channels(), src.quantization_info()}; |
| 47 | |
| 48 | const unsigned int width = src.shape()[0]; |
| 49 | const unsigned int height = src.shape()[1]; |
| 50 | const unsigned int depth = src.shape()[2]; |
| 51 | const unsigned int batches = src.shape()[3]; |
| 52 | |
| 53 | const int rank = src.shape().num_dimensions(); |
| 54 | |
| 55 | std::array<bool, 4> to_reverse = {{false, false, false, false}}; |
| 56 | for (int i = 0; i < axis.num_elements(); ++i) |
| 57 | { |
| 58 | int axis_i = axis[i]; |
| 59 | |
| 60 | // The values of axis tensor must be between [-rank, rank-1]. |
| 61 | if ((axis_i < -rank) || (axis_i >= rank)) |
| 62 | { |
| 63 | ARM_COMPUTE_ERROR("the values of the axis tensor must be within [-rank, rank-1]."); |
| 64 | } |
| 65 | |
| 66 | // In case of negative axis value i.e targeted axis(i) = rank + axis(i) |
| 67 | if (axis_i < 0) |
| 68 | { |
| 69 | axis_i = rank + axis_i; |
| 70 | } |
| 71 | |
| 72 | // Reverse ACL axis indices convention i.e. (inverted)axis = (tensor_rank - 1) - axis |
| 73 | if (use_inverted_axis) |
| 74 | { |
| 75 | axis_i = (rank - 1) - axis_i; |
| 76 | } |
| 77 | |
| 78 | to_reverse[axis_i] = true; |
| 79 | } |
| 80 | |
| 81 | const uint32_t num_elements = src.num_elements(); |
| 82 | |
| 83 | #if defined(_OPENMP) |
| 84 | #pragma omp parallel for |
| 85 | #endif /* _OPENMP */ |
| 86 | for (uint32_t i = 0; i < num_elements; ++i) |
| 87 | { |
| 88 | const Coordinates src_coord = index2coord(src.shape(), i); |
| 89 | const unsigned int dst_x = to_reverse[0] ? width - src_coord[0] - 1 : src_coord[0]; |
| 90 | const unsigned int dst_y = to_reverse[1] ? height - src_coord[1] - 1 : src_coord[1]; |
| 91 | const unsigned int dst_z = to_reverse[2] ? depth - src_coord[2] - 1 : src_coord[2]; |
| 92 | const unsigned int dst_w = to_reverse[3] ? batches - src_coord[3] - 1 : src_coord[3]; |
| 93 | |
| 94 | dst[coord2index(src.shape(), Coordinates(dst_x, dst_y, dst_z, dst_w))] = src[i]; |
| 95 | } |
| 96 |
no test coverage detected