Compute Library
 19.08
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...
 
- 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 41 of file NEConvertFullyConnectedWeightsKernel.h.

Constructor & Destructor Documentation

◆ NEConvertFullyConnectedWeightsKernel() [1/3]

Default constructor.

Definition at line 31 of file NEConvertFullyConnectedWeightsKernel.cpp.

32  : _input(nullptr), _output(nullptr), _factor1(0), _factor2(0)
33 {
34 }

◆ 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()

Default destructor.

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: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32.
[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 36 of file NEConvertFullyConnectedWeightsKernel.cpp.

38 {
39  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
40 
41  // Output tensor auto initialisation if not yet initialized
42  auto_init_if_empty(*output->info(), *input->info()->clone());
43 
45 
46  _input = input;
47  _output = output;
48 
49  const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
50 
51  const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
52  const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
53  const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
54 
55  const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
56  const unsigned int num_channels = original_input_shape[channel_idx];
57 
58  _factor1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
59  _factor2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
60 
61  // Configure kernel window
62  Window win = calculate_max_window(*input->info(), Steps());
63  INEKernel::configure(win);
64 }
const DataLayout data_layout
Definition: Im2Col.cpp:146
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
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...
Definition: Helpers.inl:201
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...
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
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's metadata.
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Num samples, height, width, channels.
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:326
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
Describe a multidimensional execution window.
Definition: Window.h:39

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::NCHW, arm_compute::NHWC, NEConvertFullyConnectedWeightsKernel::validate(), and arm_compute::WIDTH.

Referenced by NEConvertFullyConnectedWeights::configure().

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 44 of file NEConvertFullyConnectedWeightsKernel.h.

45  {
46  return "NEConvertFullyConnectedWeightsKernel";
47  }

◆ operator=() [1/2]

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

◆ 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.

Implements ICPPKernel.

Definition at line 105 of file NEConvertFullyConnectedWeightsKernel.cpp.

106 {
110 
111  switch(_input->info()->element_size())
112  {
113  case 1:
114  run_convert_fc_weights<uint8_t>(window);
115  break;
116  case 2:
117  run_convert_fc_weights<uint16_t>(window);
118  break;
119  case 4:
120  run_convert_fc_weights<uint32_t>(window);
121  break;
122  default:
123  ARM_COMPUTE_ERROR("Data type not supported.");
124  break;
125  }
126 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940

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

◆ 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: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32.
[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 66 of file NEConvertFullyConnectedWeightsKernel.cpp.

68 {
69  //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
76  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
78 
79  // Checks performed when output is configured
80  if((output != nullptr) && (output->total_size() != 0))
81  {
84  }
85 
86  return Status{};
87 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
const DataLayout data_layout
Definition: Im2Col.cpp:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
1 channel, 1 U8 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
1 channel, 1 F32 per channel
1 channel, 1 U16 per channel
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:244
size_t total_size_lower(size_t dimension) const
Compute size of dimensions lower than the given one.
Definition: TensorShape.h:193
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
1 channel, 1 U32 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
quantized, asymmetric fixed-point 8-bit number
1 channel, 1 S16 per channel
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
signed 8-bit number

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES, arm_compute::test::validation::data_layout, ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, ITensorInfo::num_dimensions(), arm_compute::QASYMM8, arm_compute::S16, arm_compute::S32, arm_compute::S8, ITensorInfo::total_size(), TensorShape::total_size_lower(), arm_compute::U16, arm_compute::U32, arm_compute::U8, and arm_compute::UNKNOWN.

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


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