blob: 5081cbac4e1b3692a5a18acdd7270ebe0102d13e [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)
35 : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _input0(), _input1(), _batched_mm_output(), _is_first_run(true)
36{
37}
38
39void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
40{
41 // TODO(COMPMID-1013): This part will be removed
42 // Get indeces for the width and height
43 const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
44 const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
45
46 // Kernel size
47 const unsigned int kernel_w = weights->info()->tensor_shape()[idx_width];
48 const unsigned int kernel_h = weights->info()->tensor_shape()[idx_height];
49
50 // Number of tiles along the X and Y direction
51 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);
52 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);
53
54 // Compute output shape
55 const TensorShape output_convolved_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info);
56
57 // Manage intermediate tensors
58 _memory_group.manage(&_input0);
59 _memory_group.manage(&_batched_mm_output);
60
61 // Do not manage _input1 as it contains the weights
62
63 // Configure input transform
64 _input_transform.configure(input, &_input0, conv_info, Size2D(kernel_w, kernel_h));
65
66 // Configure filter transform
67 _filter_transform.configure(weights, &_input1);
68
69 // Configure batched matrix multiply
70 _batched_mm.configure(&_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
71
72 // Configure output transform
73 _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,
74 num_tiles_y));
75
76 // Allocate temporary tensors
77 _input0.allocator()->allocate();
78 _input1.allocator()->allocate();
79 _batched_mm_output.allocator()->allocate();
80}
81
82Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
83{
84 // TODO(COMPMID-1013): This part will be removed
85 // Get indeces for the width and height
86 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
87 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
88
89 // Kernel size
90 const unsigned int kernel_w = weights->tensor_shape()[idx_width];
91 const unsigned int kernel_h = weights->tensor_shape()[idx_height];
92
93 // Number of tiles along the X and Y direction
94 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);
95 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);
96
97 // Compute output shape
98 const TensorShape output_convolved_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info);
99
100 // Validate input transform
101 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, conv_info, Size2D(kernel_w, kernel_h));
102 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
103 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, conv_info, Size2D(kernel_w, kernel_h)));
104
105 // Validate filter transform
106 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights);
107 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
108 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1));
109
110 // Configure batched matrix multiply
111 TensorShape batched_mm_output_shape = input0.tensor_shape();
112 batched_mm_output_shape[0] = input1.tensor_shape()[0];
113 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
114 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*/)));
115
116 // Configure output transform
117 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, Size2D(kernel_w, kernel_h), Size2D(output_convolved_shape[idx_width],
118 output_convolved_shape[idx_height]),
119 Size2D(num_tiles_x, num_tiles_y)));
120
121 return Status{};
122}
123
124void CLWinogradConvolutionLayer::run()
125{
126 if(_is_first_run)
127 {
128 // Run filter transform
129 CLScheduler::get().enqueue(_filter_transform, false);
130
131 _is_first_run = false;
132 }
133
134 _memory_group.acquire();
135
136 // Run input transform
137 _input_transform.run();
138
139 // Run batched matrix multiplication
140 _batched_mm.run();
141
142 // Run output transform
143 CLScheduler::get().enqueue(_output_transform);
144
145 _memory_group.release();
146}