Compute Library
 20.05
GCConvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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  */
24 
26 
29 #include "arm_compute/core/Utils.h"
32 
33 #include <cmath>
34 #include <memory>
35 #include <tuple>
36 
37 using namespace arm_compute;
38 
40  : _weights_reshape_kernel()
41 {
42 }
43 
45 {
49 
50  if(biases != nullptr)
51  {
54  ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
55  ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
56  }
57 
58  const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
59  const IGCTensor *biases_to_use = (append_biases) ? biases : nullptr;
60 
61  _weights_reshape_kernel.configure(weights, biases_to_use, output);
62 }
63 
65 {
66  GCScheduler::get().dispatch(_weights_reshape_kernel);
67 }
68 
69 GCConvolutionLayer::GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
70  : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _mm_gemm(), _output_col2im_kernel(), _fill_border(), _activationlayer_function(), _original_weights(nullptr),
71  _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _is_activationlayer_enabled(false), _is_prepared(false)
72 {
73 }
74 
75 void GCConvolutionLayer::configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output)
76 {
78  ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info()));
79 
80  _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
81 }
82 
83 Status GCConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output)
84 {
85  // Perform validation step on Matrix multiply function
86  GCGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
87  return Status{};
88 }
89 
91  const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
92 {
96  ARM_COMPUTE_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
97  ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2));
101 
102  _is_prepared = false;
103  _original_weights = weights;
104 
105  if(biases != nullptr)
106  {
108  ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
109  ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
110  }
111 
112  const DataType dt = input->info()->data_type();
113 
114  // Set the GPU target for im2col and col2im
115  _input_im2col_kernel.set_target(GCScheduler::get().get_target());
116  _output_col2im_kernel.set_target(GCScheduler::get().get_target());
117 
118  const bool append_bias = (biases != nullptr);
119  const unsigned bias_element = (append_bias) ? 1 : 0;
120  const IGCTensor *biases_to_use = (append_bias) ? biases : nullptr;
121 
122  // Get parameters from conv_info
123  unsigned int stride_x = 0;
124  unsigned int stride_y = 0;
125  std::tie(stride_x, stride_y) = conv_info.stride();
126 
127  // Get convolved dimensions
128  unsigned int conv_w = 0;
129  unsigned int conv_h = 0;
130 
131  const unsigned int kernel_width = weights->info()->dimension(0);
132  const unsigned int kernel_height = weights->info()->dimension(1);
133  std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
135 
136  unsigned int mat_weights_cols = weights->info()->dimension(3);
137  unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
138 
139  // _weights_reshaped will be auto configured in the kernel.
140  // Just append biases and do not transpose 1xW as it will be reshaped in GCGEMM
141  _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
142 
143  weights = &_weights_reshaped;
144 
145  // Create tensor to store im2col reshaped inputs
146  const unsigned int mat_input_cols = mat_weights_rows;
147  const unsigned int mat_input_rows = conv_w * conv_h;
148  TensorShape shape_im2col = input->info()->tensor_shape();
149  shape_im2col.set(0, mat_input_cols);
150  shape_im2col.set(1, mat_input_rows);
151  shape_im2col.set(2, 1);
152 
153  // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
154  TensorInfo im2col_reshaped_info(shape_im2col, 1, dt);
155  _input_im2col_reshaped.allocator()->init(im2col_reshaped_info);
156  _memory_group.manage(&_input_im2col_reshaped);
157 
158  // Create GEMM output tensor
159  TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
160  shape_gemm.set(0, mat_weights_cols);
161  shape_gemm.set(1, mat_input_rows);
162  const DataType gemm_data_type = dt;
163 
164  // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
165  TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
166  _gemm_output.allocator()->init(info_gemm);
167  _memory_group.manage(&_gemm_output);
168 
169  if(dt == DataType::F16)
170  {
171  BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
172  input->info()->extend_padding(border_size);
173  _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue()); // for PAD of im2col fp16: consider it as border
174  }
175  // Configure im2col
176  _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation);
177 
178  // Configure GEMM
179  configure_mm(&_input_im2col_reshaped, weights, &_gemm_output);
180 
181  _input_im2col_reshaped.allocator()->allocate();
182 
183  // Configure Col2Im
184  _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
185  _gemm_output.allocator()->allocate();
186 
187  ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one");
188 
189  //Configure Activation Layer
190  _is_activationlayer_enabled = act_info.enabled();
191 
192  if(_is_activationlayer_enabled)
193  {
194  _activationlayer_function.configure(output, nullptr, act_info);
195  }
196 
198 }
199 
201 {
202  prepare();
203 
204  MemoryGroupResourceScope scope_mg(_memory_group);
205 
206  // Run im2col
207  GCScheduler::get().dispatch(_fill_border);
209  GCScheduler::get().dispatch(_input_im2col_kernel);
210 
211  // Run gemm on reshaped matrices
212  _mm_gemm.run();
214 
215  // Reshape output matrix
216  GCScheduler::get().dispatch(_output_col2im_kernel, false);
218 
219  // Run Activation Layer
220  if(_is_activationlayer_enabled)
221  {
222  _activationlayer_function.run();
223  }
224 }
225 
227 {
228  if(!_is_prepared)
229  {
230  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
231 
232  // Run weights reshaping and mark as unused
233  _weights_reshaped.allocator()->allocate();
234  _reshape_weights.run();
235 
236  // Mark original weights tensor as unused
237  _original_weights->mark_as_unused();
238 
239  _is_prepared = true;
240  }
241 }
void configure(const IGCTensor *input, IGCTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U))
Set the input and output of the kernel.
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
Shape of a tensor.
Definition: TensorShape.h:39
bool enabled() const
Check if initialised.
Definition: Types.h:1567
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void prepare() override
Prepare the function for executing.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
void dispatch(IGCKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: GCScheduler.cpp:77
Container for 2D border size.
Definition: Types.h:272
void run() override
Run the kernels contained in the function.
Definition: GCGEMM.cpp:161
void run() override final
Run the kernels contained in the function.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
1 channel, 1 F32 per channel
void memory_barrier()
Defines a barrier ordering memory transactions.
Definition: GCScheduler.cpp:86
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
void configure(const IGCTensor *input, const IGCTensor *biases, IGCTensor *output)
Set the input and output of the kernel.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1517
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:395
void set_target(GPUTarget target)
Set the targeted GPU architecture.
Definition: IGCKernel.h:113
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
Convolution Layer Weights Information class.
Definition: Types.h:1694
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:70
void configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output)
Set the input and output tensors.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
const unsigned int num_groups
Definition: Im2Col.cpp:148
void run() override
Run the kernels contained in the function.
void configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the kernel's input, output and border mode.
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of GCGEMM.
Definition: GCGEMM.cpp:155
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
Padding and stride information class.
Definition: Types.h:689
virtual void allocate()=0
Interface to be implemented by the child class to allocate the tensor.
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1153
void configure(IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info)
Set the input and output tensor.
GCConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: GCTensor.cpp:39
void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), unsigned int num_groups=1)
Set the input and output tensors.
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
Store the tensor's metadata.
Definition: TensorInfo.h:45
GEMM information class.
Definition: Types.h:1931
void configure(const IGCTensor *a, const IGCTensor *b, const IGCTensor *c, IGCTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel's inputs and output.
Definition: GCGEMM.cpp:81
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:261
DataType
Available data types.
Definition: Types.h:77
ITensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: GCTensor.cpp:34
void configure(const IGCTensor *input, IGCTensor *output, std::pair< unsigned int, unsigned int > convolved_dims)
Set the input and output of the kernel.