Compute Library
 21.02
NEConvertFullyConnectedWeightsKernel Class Reference

Interface to convert the 2D Fully Connected weights from NCHW to NHWC or vice versa. More...

#include <NEConvertFullyConnectedWeightsKernel.h>

Collaboration diagram for NEConvertFullyConnectedWeightsKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEConvertFullyConnectedWeightsKernel ()
 Default constructor. More...
 
 NEConvertFullyConnectedWeightsKernel (const NEConvertFullyConnectedWeightsKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEConvertFullyConnectedWeightsKerneloperator= (const NEConvertFullyConnectedWeightsKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEConvertFullyConnectedWeightsKernel (NEConvertFullyConnectedWeightsKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEConvertFullyConnectedWeightsKerneloperator= (NEConvertFullyConnectedWeightsKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEConvertFullyConnectedWeightsKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, ITensor *output, const TensorShape &original_input_shape, DataLayout data_layout)
 Set the input and output tensor. More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. 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 void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. 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...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout)
 Static function to check if given info will lead to a valid configuration of NEConvertFullyConnectedWeightsKernel. More...
 

Detailed Description

Interface to convert the 2D Fully Connected weights from NCHW to NHWC or vice versa.

Note
This function can be applied to the 2D weights used by a Fully Connected layer if:
  • It follows a Convolution layer
  • The data layout used by the network does not match the one the model has been trained in.
This function assumes the weights are already reshaped (transposed)

Definition at line 42 of file NEConvertFullyConnectedWeightsKernel.h.

Constructor & Destructor Documentation

◆ NEConvertFullyConnectedWeightsKernel() [1/3]

Default constructor.

Definition at line 33 of file NEConvertFullyConnectedWeightsKernel.cpp.

Referenced by NEConvertFullyConnectedWeightsKernel::name().

34  : _input(nullptr), _output(nullptr), _factor1(0), _factor2(0)
35 {
36 }

◆ NEConvertFullyConnectedWeightsKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEConvertFullyConnectedWeightsKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NEConvertFullyConnectedWeightsKernel()

Member Function Documentation

◆ configure()

void configure ( const ITensor input,
ITensor output,
const TensorShape original_input_shape,
DataLayout  data_layout 
)

Set the input and output tensor.

Parameters
[in]inputSource weights tensor to convert. Must be 2 dimensional. Data types supported: All.
[out]outputThe converted weights tensor. Shape and Data Type: Same as input.
[in]original_input_shapeShape of the original input tensor (the one entering fully connected layer).
[in]data_layoutThe data layout the weights have been trained in.

Definition at line 38 of file NEConvertFullyConnectedWeightsKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), arm_compute::CHANNEL, ICloneable< T >::clone(), arm_compute::test::validation::data_layout, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, ITensor::info(), arm_compute::test::validation::input, arm_compute::NCHW, arm_compute::NHWC, NEConvertFullyConnectedWeightsKernel::validate(), and arm_compute::WIDTH.

Referenced by NEConvertFullyConnectedWeightsKernel::name().

40 {
42 
43  // Output tensor auto initialisation if not yet initialized
44  auto_init_if_empty(*output->info(), *input->info()->clone());
45 
46  ARM_COMPUTE_ERROR_THROW_ON(NEConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout));
47 
48  _input = input;
49  _output = output;
50 
51  const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
52 
53  const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
54  const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
55  const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
56 
57  const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
58  const unsigned int num_channels = original_input_shape[channel_idx];
59 
60  _factor1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
61  _factor2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
62 
63  // Configure kernel window
64  Window win = calculate_max_window(*input->info(), Steps());
65  INEKernel::configure(win);
66 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const DataLayout data_layout
Definition: Im2Col.cpp:151
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout)
Static function to check if given info will lead to a valid configuration of NEConvertFullyConnectedW...
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...
Num samples, channels, height, width.
Num samples, height, width, channels.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
DataLayout
[DataLayout enum definition]
Definition: Types.h:120

◆ name()

◆ operator=() [1/2]

Prevent instances of this class from being copied (As this class contains pointers)

Referenced by NEConvertFullyConnectedWeightsKernel::name().

◆ operator=() [2/2]

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
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().
Parameters
[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 104 of file NEConvertFullyConnectedWeightsKernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorInfo::element_size(), ITensor::info(), and IKernel::window().

Referenced by NEConvertFullyConnectedWeightsKernel::name().

105 {
109 
110  switch(_input->info()->element_size())
111  {
112  case 1:
113  run_convert_fc_weights<uint8_t>(window);
114  break;
115  case 2:
116  run_convert_fc_weights<uint16_t>(window);
117  break;
118  case 4:
119  run_convert_fc_weights<uint32_t>(window);
120  break;
121  default:
122  ARM_COMPUTE_ERROR("Data type not supported.");
123  break;
124  }
125 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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:941
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const TensorShape original_input_shape,
DataLayout  data_layout 
)
static

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

Parameters
[in]inputSource weights tensor info to convert. Must be 2 dimensional. Data types supported: All.
[in]outputThe converted weights tensor info. Shape and Data Type: Same as input.
[in]original_input_shapeShape of the original input tensor (the one entering fully connected layer).
[in]data_layoutThe data layout the weights have been trained in.

Definition at line 68 of file NEConvertFullyConnectedWeightsKernel.cpp.

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::execute_window_loop(), ITensor::info(), arm_compute::test::validation::input, ITensorInfo::num_dimensions(), Iterator::ptr(), ITensorInfo::strides_in_bytes(), ITensorInfo::total_size(), TensorShape::total_size_lower(), arm_compute::UNKNOWN, IKernel::window(), Dimensions< T >::x(), and Dimensions< T >::y().

Referenced by NEConvertFullyConnectedWeightsKernel::configure(), NEConvertFullyConnectedWeightsKernel::name(), and NEConvertFullyConnectedWeights::validate().

70 {
72  //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions.
74  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2);
75  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
77 
78  // Checks performed when output is configured
79  if((output != nullptr) && (output->total_size() != 0))
80  {
83  }
84 
85  return Status{};
86 }
const DataLayout data_layout
Definition: Im2Col.cpp:151
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545

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