Compute Library
CpuWeightsReshapeKernel Class Reference

Kernel to perform reshaping on the weights used by convolution and locally connected layer. More...

#include <CpuWeightsReshapeKernel.h>

Collaboration diagram for CpuWeightsReshapeKernel:

Public Member Functions

 CpuWeightsReshapeKernel ()=default
 Default constructor. More...
void configure (const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst)
 Set the input and output of the kernel. More...
void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
const char * name () const override
 Name of the kernel. More...
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
virtual void run (const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
virtual size_t get_mws (const CPUInfo &platform, size_t thread_count) const
 Return minimum workload size of the relevant kernel. More...
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
virtual ~IKernel ()=default
 Destructor. More...
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...

Static Public Member Functions

static Status validate (const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst)
 Static function to check if given info will lead to a valid configuration. More...
- Static Public Member Functions inherited from ICpuKernel< CpuWeightsReshapeKernel >
static const auto * get_implementation (const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
 Micro-kernel selector. More...

Additional Inherited Members

- Static Public Attributes inherited from ICPPKernel
static constexpr size_t default_mws = 1

Detailed Description

Kernel to perform reshaping on the weights used by convolution and locally connected layer.

Rearranges each 3-dimensional kernel to a single row leading to a matrix with linearized kernels. In combination with the cpu::kernels::CpuIm2ColKernel can transform a convolution to a matrix multiplication.

For example assuming a 3D weight kernel of 3x3 dimensions and depth of 2 we have:

\[ \left( \begin{array}{ccc} a000 & a001 & a002 \\ a010 & a011 & a012 \\ a020 & a021 & a022 \\ \end{array} \right) \left( \begin{array}{ccc} a100 & a101 & a102 \\ a110 & a111 & a112 \\ a120 & a121 & a122 \\ \end{array} \right) \rightarrow \left( \begin{array}{ccccccccc} a000 & a001 & a002 & a010 & a011 & a012 & a020 & a021 & a022 & a100 & a101 & a102 & a110 & a111 & a112 & a120 & a121 & a122 \\ \end{array} \right) \]

Definition at line 59 of file CpuWeightsReshapeKernel.h.

Constructor & Destructor Documentation

◆ CpuWeightsReshapeKernel()

Default constructor.

Member Function Documentation



◆ configure()

void configure ( const ITensorInfo src,
const ITensorInfo biases,
ITensorInfo dst 

Set the input and output of the kernel.

[in]srcThe input tensor info to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared, and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches] if unshared. Data types supported: All
[in]biasesThe shared biases tensor info to append. Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with dimensions [OFM, num_patches] if unshared. Data types supported: Same as input
Appending biases to weights reshaped matrix is not supported for quantized asymmetric types.
[out]dstThe output tensor info. Data types supported: Same as src

Definition at line 79 of file CpuWeightsReshapeKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), ICloneable< T >::clone(), ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, Window::set(), and IKernel::window().

80 {
83  // Output tensor auto inizialitation if not yet initialized
84  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(get_output_shape(src, (biases != nullptr))));
86  // Perform validation step
87  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
88  biases,
89  dst));
91  // Configure kernel
92  Window window = calculate_max_window(*src, Steps());
93  window.set(Window::DimX, Window::Dimension(0, src->dimension(0), src->dimension(0)));
94  window.set(Window::DimY, Window::Dimension(0, src->dimension(1), src->dimension(1)));
95  window.set(Window::DimZ, Window::Dimension(0, src->dimension(2), src->dimension(2)));
96  ICpuKernel::configure(window);
97 }
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
Definition: Error.h:455
SimpleTensor< float > src
Definition: DFT.cpp:155
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
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...
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Definition: Validate.h:157

◆ name()

const char * name ( ) const

Name of the kernel.

Kernel name

Implements ICPPKernel.

Definition at line 164 of file CpuWeightsReshapeKernel.cpp.

165 {
166  return "CpuWeightsReshapeKernel";
167 }

◆ run_op()

void run_op ( ITensorPack tensors,
const Window window,
const ThreadInfo info 

Execute the kernel on the passed window.

If is_parallelisable() returns false then the passed window must be equal to window()
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
[in]tensorsA vector containing the tensors to operate on.
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 105 of file CpuWeightsReshapeKernel.cpp.

References arm_compute::ACL_BIAS, arm_compute::ACL_DST, arm_compute::ACL_SRC, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorInfo::dimension(), arm_compute::execute_window_loop(), ITensorPack::get_const_tensor(), ITensorPack::get_tensor(), ITensor::info(), input_stride_y, input_stride_z, Iterator::ptr(), and IKernel::window().

106 {
111  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
112  auto biases = tensors.get_const_tensor(TensorType::ACL_BIAS);
113  auto dst = tensors.get_tensor(TensorType::ACL_DST);
115  const unsigned int kernel_size_x = src->info()->dimension(0);
116  const unsigned int kernel_size_y = src->info()->dimension(1);
117  const unsigned int kernel_depth = src->info()->dimension(2);
118  const unsigned int input_stride_x = src->info()->strides_in_bytes().x();
119  const unsigned int input_stride_y = src->info()->strides_in_bytes().y();
120  const unsigned int input_stride_z = src->info()->strides_in_bytes().z();
121  const unsigned int output_stride_y = dst->info()->strides_in_bytes().y();
123  // Create iterators
124  Iterator in(src, window);
125  execute_window_loop(window, [&](const Coordinates & id)
126  {
127  // Get column index
128  const int kernel_idx = id[3];
129  const int kernel_idz = id[4];
131  // Setup pointers
132  const uint8_t *tmp_input_ptr = in.ptr();
133  uint8_t *tmp_output_ptr = dst->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz));
134  const uint8_t *curr_input_row_ptr = tmp_input_ptr;
135  const uint8_t *curr_input_depth_ptr = tmp_input_ptr;
137  // Linearize volume
138  for(unsigned int d = 0; d < kernel_depth; ++d)
139  {
140  for(unsigned int j = 0; j < kernel_size_y; ++j)
141  {
142  for(unsigned int i = 0; i < kernel_size_x; ++i)
143  {
144  std::memcpy(tmp_output_ptr, tmp_input_ptr, src->info()->element_size());
145  tmp_input_ptr += input_stride_x;
146  tmp_output_ptr += output_stride_y;
147  }
148  curr_input_row_ptr += input_stride_y;
149  tmp_input_ptr = curr_input_row_ptr;
150  }
151  curr_input_depth_ptr += input_stride_z;
152  curr_input_row_ptr = curr_input_depth_ptr;
153  tmp_input_ptr = curr_input_depth_ptr;
154  }
156  // Add bias
157  if(biases != nullptr)
158  {
159  std::memcpy(tmp_output_ptr, biases->ptr_to_element(Coordinates(kernel_idx, kernel_idz)), src->info()->element_size());
160  }
161  },
162  in);
163 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
const size_t input_stride_y
const size_t input_stride_z
SimpleTensor< float > src
Definition: DFT.cpp:155
To avoid unused variables warnings.
Definition: Error.h:152
Definition: Validate.h:915
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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
Definition: Validate.h:201

◆ validate()

Status validate ( const ITensorInfo src,
const ITensorInfo biases,
const ITensorInfo dst 

Static function to check if given info will lead to a valid configuration.

Similar to CpuWeightsReshapeKernel::configure()

a status

Definition at line 99 of file CpuWeightsReshapeKernel.cpp.


100 {
101  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, biases, dst));
102  return Status{};
103 }
Checks if a status contains an error and returns it.
Definition: Error.h:204
SimpleTensor< float > src
Definition: DFT.cpp:155

The documentation for this class was generated from the following files: