blob: 7af36bf06b01078fd6c9806b87cbd5e3576428f8 [file] [log] [blame]
Gian Marco Iodiced2fab732018-03-02 11:18:12 +00001/*
2 * Copyright (c) 2018 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#include "arm_compute/runtime/CL/functions/CLWinogradConvolutionLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31
32using namespace arm_compute;
33
34CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000035 : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _activationlayer_function(), _input0(), _input1(), _batched_mm_output(),
36 _is_first_run(true), _is_activationlayer_enabled(false)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000037{
38}
39
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000040void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000041{
42 // TODO(COMPMID-1013): This part will be removed
43 // Get indeces for the width and height
44 const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
45 const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
46
47 // Kernel size
48 const unsigned int kernel_w = weights->info()->tensor_shape()[idx_width];
49 const unsigned int kernel_h = weights->info()->tensor_shape()[idx_height];
50
51 // Number of tiles along the X and Y direction
52 const unsigned int num_tiles_x = std::ceil((input->info()->tensor_shape().x() - (kernel_w - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f);
53 const unsigned int num_tiles_y = std::ceil((input->info()->tensor_shape().y() - (kernel_h - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f);
54
55 // Compute output shape
56 const TensorShape output_convolved_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info);
57
58 // Manage intermediate tensors
59 _memory_group.manage(&_input0);
60 _memory_group.manage(&_batched_mm_output);
61
62 // Do not manage _input1 as it contains the weights
63
64 // Configure input transform
65 _input_transform.configure(input, &_input0, conv_info, Size2D(kernel_w, kernel_h));
66
67 // Configure filter transform
Giorgio Arena2d9de0a2018-03-15 17:58:20 +000068 _filter_transform.configure(weights, &_input1, Size2D(2U, 2U));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000069
70 // Configure batched matrix multiply
71 _batched_mm.configure(&_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
72
73 // Configure output transform
74 _output_transform.configure(&_batched_mm_output, biases, output, Size2D(kernel_w, kernel_h), Size2D(output_convolved_shape[idx_width], output_convolved_shape[idx_height]), Size2D(num_tiles_x,
75 num_tiles_y));
76
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000077 // Configure activation layer
78 _is_activationlayer_enabled = act_info.enabled();
79 if(_is_activationlayer_enabled)
80 {
81 _activationlayer_function.configure(output, nullptr, act_info);
82 }
83
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000084 // Allocate temporary tensors
85 _input0.allocator()->allocate();
86 _input1.allocator()->allocate();
87 _batched_mm_output.allocator()->allocate();
88}
89
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000090Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
91 const ActivationLayerInfo &act_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000092{
93 // TODO(COMPMID-1013): This part will be removed
94 // Get indeces for the width and height
95 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
96 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
97
98 // Kernel size
99 const unsigned int kernel_w = weights->tensor_shape()[idx_width];
100 const unsigned int kernel_h = weights->tensor_shape()[idx_height];
101
102 // Number of tiles along the X and Y direction
103 const unsigned int num_tiles_x = std::ceil((input->tensor_shape().x() - (kernel_w - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f);
104 const unsigned int num_tiles_y = std::ceil((input->tensor_shape().y() - (kernel_h - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f);
105
106 // Compute output shape
107 const TensorShape output_convolved_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info);
108
109 // Validate input transform
110 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, conv_info, Size2D(kernel_w, kernel_h));
111 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
112 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, conv_info, Size2D(kernel_w, kernel_h)));
113
114 // Validate filter transform
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000115 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, Size2D(2U, 2U));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000116 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000117 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1, Size2D(2U, 2U)));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000118
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000119 // Validate batched matrix multiply
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000120 TensorShape batched_mm_output_shape = input0.tensor_shape();
121 batched_mm_output_shape[0] = input1.tensor_shape()[0];
122 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
123 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input0, &input1, nullptr, &batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)));
124
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000125 // Validate output transform
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000126 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, Size2D(kernel_w, kernel_h), Size2D(output_convolved_shape[idx_width],
127 output_convolved_shape[idx_height]),
128 Size2D(num_tiles_x, num_tiles_y)));
129
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000130 // Validate Activation Layer
131 if(act_info.enabled())
132 {
133 CLActivationLayer::validate(output, nullptr, act_info);
134 }
135
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000136 return Status{};
137}
138
139void CLWinogradConvolutionLayer::run()
140{
141 if(_is_first_run)
142 {
143 // Run filter transform
144 CLScheduler::get().enqueue(_filter_transform, false);
145
146 _is_first_run = false;
147 }
148
149 _memory_group.acquire();
150
151 // Run input transform
152 _input_transform.run();
153
154 // Run batched matrix multiplication
155 _batched_mm.run();
156
157 // Run output transform
158 CLScheduler::get().enqueue(_output_transform);
159
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000160 if(_is_activationlayer_enabled)
161 {
162 _activationlayer_function.run();
163 }
164
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000165 _memory_group.release();
166}