2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2019-2020 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 #include "arm_compute/runtime/CL/functions/CLDirectTransposeConvLayer.h"
42 #include "arm_compute/core/Helpers.h"
43 #include "arm_compute/core/UtilsEx.h"
44 #include "arm_compute/core/Validate.h"
45 #include "arm_compute/core/utils/misc/ShapeCalculatorEx.h"
46 #include "arm_compute/runtime/CL/CLScheduler.h"
53 using namespace arm_compute::misc::shape_calculator;
55 CLDirectTransposeConvLayer::CLDirectTransposeConvLayer(
56 std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
57 : _memory_group(std::move(memory_manager)), _scale_f(), _conv_f(), _flip_weights(),
58 _scaled_output(), _original_weights(nullptr), _weights_flipped(), _flip_axis(),
63 Status CLDirectTransposeConvLayer::validate(const ITensorInfo *input, const ITensorInfo *weights,
64 const ITensorInfo *bias, ITensorInfo *output,
65 const PadStrideInfo &info, unsigned int invalid_right,
66 unsigned int invalid_bottom,
67 const WeightsInfo &weights_info)
69 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
70 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
71 input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
73 const DataLayout data_layout = input->data_layout();
75 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
76 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
77 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
79 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h));
80 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
82 auto out_dims = transposeconv_output_dimensions(
83 input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w),
84 weights->dimension(idx_h), info, invalid_right, invalid_bottom);
86 const TensorShape output_shape = compute_transposeconv_output_shape(out_dims, *input, *weights);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
92 if (is_data_type_quantized_asymmetric(input->data_type()))
94 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias);
103 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w],
104 "Output's width is invalid.");
105 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h],
106 "Output's height is invalid.");
107 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c],
108 "Output's depth is invalid.");
110 unsigned int pad_left = 0;
111 unsigned int pad_right = 0;
112 unsigned int pad_top = 0;
113 unsigned int pad_bottom = 0;
114 const TensorShape scale_out_shape =
115 compute_transposeconv_upsampled_shape(*input, *weights, info, out_dims, invalid_right,
116 invalid_bottom, pad_left, pad_right, pad_top, pad_bottom);
117 TensorInfo scale_out_info(input->clone()
118 ->set_is_resizable(true)
120 .set_tensor_shape(scale_out_shape)
121 .set_data_layout(data_layout));
122 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
124 ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, info));
125 ARM_COMPUTE_RETURN_ON_ERROR(
126 CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
131 void CLDirectTransposeConvLayer::configure(ICLTensor *input, ICLTensor *weights,
132 const ICLTensor *bias, ICLTensor *output,
133 const PadStrideInfo &info, unsigned int invalid_right,
134 unsigned int invalid_bottom,
135 const WeightsInfo &weights_info)
137 configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, info,
138 invalid_right, invalid_bottom, weights_info);
141 void CLDirectTransposeConvLayer::configure(const CLCompileContext &compile_context,
142 ICLTensor *input, ICLTensor *weights,
143 const ICLTensor *bias, ICLTensor *output,
144 const PadStrideInfo &info, unsigned int invalid_right,
145 unsigned int invalid_bottom,
146 const WeightsInfo &weights_info)
148 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
150 unsigned int pad_left = 0;
151 unsigned int pad_right = 0;
152 unsigned int pad_top = 0;
153 unsigned int pad_bottom = 0;
154 const unsigned int stride_x = info.stride().first;
155 const unsigned int stride_y = info.stride().second;
157 const DataLayout data_layout = input->info()->data_layout();
159 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
160 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
162 _original_weights = weights;
163 _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
164 _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
165 _flip_weights.configure(compile_context, weights, &_weights_flipped, &_flip_axis);
167 auto out_dims = transposeconv_output_dimensions(
168 input->info()->dimension(idx_w), input->info()->dimension(idx_h),
169 weights->info()->dimension(idx_w), weights->info()->dimension(idx_h), info, invalid_right,
172 const TensorShape output_shape =
173 compute_transposeconv_output_shape(out_dims, *input->info(), *weights->info());
175 // Output auto initialization if not yet initialized
178 input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
180 // Perform validation step
181 ARM_COMPUTE_ERROR_THROW_ON(CLDirectTransposeConvLayer::validate(
182 input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info,
183 invalid_right, invalid_bottom));
185 _is_prepared = weights_info.retain_internal_weights();
187 _memory_group.manage(&_scaled_output);
189 // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order
190 // to match output shape
191 const TensorShape scale_out_shape = compute_transposeconv_upsampled_shape(
192 *input->info(), *weights->info(), info, out_dims, invalid_right, invalid_bottom, pad_left,
193 pad_right, pad_top, pad_bottom);
195 TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(),
196 input->info()->quantization_info());
197 scale_out_info.set_data_layout(data_layout);
198 _scaled_output.allocator()->init(scale_out_info);
200 // configure scale function
201 const PadStrideInfo upsample_info(stride_x, stride_y, pad_left, pad_right, pad_top, pad_bottom,
202 DimensionRoundingType::FLOOR);
203 _scale_f.configure(input, &_scaled_output, upsample_info);
205 // Setup the function to convolve the upscaled output
206 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
207 _conv_f.configure(compile_context, &_scaled_output, &_weights_flipped, bias, output, conv_info,
209 _scaled_output.allocator()->allocate();
211 // Setup flip axis data
212 _flip_axis.allocator()->allocate();
213 _flip_axis.map(true);
214 auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
215 if (weights->info()->data_layout() == DataLayout::NHWC)
228 void CLDirectTransposeConvLayer::run()
232 MemoryGroupResourceScope scope_mg(_memory_group);
238 void CLDirectTransposeConvLayer::prepare()
242 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
244 // Run weights flipping and mark original weights tensor as unused
245 _weights_flipped.allocator()->allocate();
247 _original_weights->mark_as_unused();
249 // Prepare convolution
252 // Free flipped weights
253 if (!_weights_flipped.is_used())
255 _weights_flipped.allocator()->free();
261 } // namespace arm_compute