Compute Library
 21.11
CPPPermuteKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
25 
30 
31 #include <cstddef>
32 #include <cstdint>
33 
34 using namespace arm_compute;
35 
36 namespace
37 {
38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
39 {
41  ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() > 4, "Only up to 4D permutation vectors are supported");
42 
44 
45  // Validate configured output
46  if(output->total_size() != 0)
47  {
50  }
51 
52  return Status{};
53 }
54 
55 } // namespace
56 
57 template <typename T>
58 void CPPPermuteKernel::run_permute(const Window &window)
59 {
60  // Permute strides
61  Strides strides = _output->info()->strides_in_bytes();
62  Strides perm_strides = strides;
63  permute_strides(perm_strides, _perm);
64 
65  // Create output window
66  Window window_out(window);
67  const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
68  for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
69  {
70  window_out.set(d, zero_window);
71  }
72 
73  // Create iterators
74  Iterator in(_input, window);
75  Iterator out(_output, window_out);
76 
77  if(_input->info()->num_dimensions() <= 3)
78  {
79  execute_window_loop(window, [&](const Coordinates & id)
80  {
81  const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2];
82  *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
83  },
84  in, out);
85  }
86  else if(_input->info()->num_dimensions() >= 4)
87  {
88  execute_window_loop(window, [&](const Coordinates & id)
89  {
90  const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3];
91  *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
92  },
93  in, out);
94  }
95 }
96 
98  : _func(), _input(nullptr), _output(nullptr), _perm()
99 {
100 }
101 
102 void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
103 {
104  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
105  const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
106  // Output auto inizialitation if not yet initialized
107  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
108 
109  // Perform validation step
110  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
111 
112  _input = input;
113  _output = output;
114  _perm = perm;
115 
116  switch(input->info()->element_size())
117  {
118  case 1:
119  _func = &CPPPermuteKernel::run_permute<uint8_t>;
120  break;
121  case 2:
122  _func = &CPPPermuteKernel::run_permute<uint16_t>;
123  break;
124  case 4:
125  _func = &CPPPermuteKernel::run_permute<uint32_t>;
126  break;
127  default:
128  ARM_COMPUTE_ERROR("Element size not supported");
129  break;
130  }
131 
132  // Configure kernel window
133  Window win = calculate_max_window(*input->info(), Steps());
134 
135  // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped
136  Coordinates coord;
137  coord.set_num_dimensions(output->info()->num_dimensions());
138  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
139 
140  ICPPKernel::configure(win);
141 }
142 
144 {
145  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
146  return Status{};
147 }
148 
149 void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info)
150 {
151  ARM_COMPUTE_UNUSED(info);
154 
155  if(_func != nullptr)
156  {
157  (this->*_func)(window);
158  }
159 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
Calculate the permuted shape of an input given a permutation vector.
void permute_strides(Dimensions< T > &dimensions, const PermutationVector &perm)
Permutes the given dimensions according the permutation vector.
Definition: Utils.h:728
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
CPPPermuteKernel()
Default constructor.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Static function to check if given info will lead to a valid configuration of CPPPermuteKernel.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
void configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
Set the input and output of the kernel.
Strides of an item in bytes.
Definition: Strides.h:37
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:158
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Container for valid region of a window.
Definition: Types.h:184
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201