2 * Copyright (c) 2019 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 * @file TransposeConv benchmark with various algorithms
21 #include <nonius/nonius.h++>
23 #include <arm_compute/core/Types.h>
24 #include <arm_compute/runtime/CL/CLScheduler.h>
25 #include <arm_compute/runtime/CL/CLFunctions.h>
26 #include <arm_compute/runtime/CL/CLFunctionsEx.h>
32 #include "acl_common/Utils.h"
34 using namespace arm_compute;
35 using namespace kbenchmark::kernels::acl_common;
51 Initializer() { CLScheduler::get().default_init(); }
54 Initializer initializer;
56 TensorInfo make_info(uint32_t N)
59 return TensorInfo{shape, 1, DataType::F32};
62 template <enum Layout> TensorInfo make_info(uint32_t N, uint32_t C, uint32_t H, uint32_t W);
64 template <> TensorInfo make_info<NCHW>(uint32_t N, uint32_t C, uint32_t H, uint32_t W)
66 TensorShape shape{W, H, C, N};
67 TensorInfo info{shape, 1, DataType::F32};
68 info.set_data_layout(DataLayout::NCHW);
72 template <> TensorInfo make_info<NHWC>(uint32_t N, uint32_t C, uint32_t H, uint32_t W)
74 TensorShape shape{C, W, H, N};
75 TensorInfo info{shape, 1, DataType::F32};
76 info.set_data_layout(DataLayout::NHWC);
80 inline void check(const Status &status)
84 std::cerr << status.error_description() << std::endl;
85 throw std::runtime_error{"ERROR"};
89 inline bool is_odd(uint32_t n) { return (n % 2 != 0) ? true : false; }
94 // Benchmark Parameters
96 NONIUS_PARAM(BATCH, 1);
98 NONIUS_PARAM(IFM_C, 3);
99 NONIUS_PARAM(IFM_H, 244);
100 NONIUS_PARAM(IFM_W, 244);
102 NONIUS_PARAM(OFM_C, 3);
103 NONIUS_PARAM(OFM_H, 244);
104 NONIUS_PARAM(OFM_W, 244);
106 NONIUS_PARAM(KER_H, 3);
107 NONIUS_PARAM(KER_W, 3);
109 NONIUS_PARAM(STRIDE_H, 1);
110 NONIUS_PARAM(STRIDE_W, 1);
112 NONIUS_PARAM(PADDING, std::string{"SAME"})
115 // Configuration Helpers
137 uint32_t vertical_stride;
138 uint32_t horizontal_stride;
140 PadStrideInfo deconv_info;
142 uint32_t inner_border_right;
143 uint32_t inner_border_top;
145 Configuration(nonius::chronometer meter)
147 ifm_N = meter.param<BATCH>();
148 ifm_C = meter.param<IFM_C>();
149 ifm_H = meter.param<IFM_H>();
150 ifm_W = meter.param<IFM_W>();
152 ofm_N = meter.param<BATCH>();
153 ofm_C = meter.param<OFM_C>();
154 ofm_H = meter.param<OFM_H>();
155 ofm_W = meter.param<OFM_W>();
157 ker_N = meter.param<OFM_C>();
158 ker_C = meter.param<IFM_C>();
159 ker_H = meter.param<KER_H>();
160 ker_W = meter.param<KER_W>();
162 vertical_stride = meter.param<STRIDE_H>();
163 horizontal_stride = meter.param<STRIDE_W>();
165 // NOTE The padding calculation formula of TransposeConv is opposite to Conv.
166 // So the location of ifm and ofm is changed.
167 auto padding_info = calculatePadding(meter.param<PADDING>(), ofm_H, ofm_W, ifm_H, ifm_W,
168 vertical_stride, horizontal_stride, ker_H, ker_W);
170 inner_border_right = padding_info.right - padding_info.left;
171 inner_border_top = padding_info.bottom - padding_info.top;
173 padding_info.left = padding_info.right;
174 padding_info.top = padding_info.bottom;
176 deconv_info = asPadStrideInfo(padding_info, vertical_stride, horizontal_stride);
179 template <Layout L> TensorInfo src_info() const
181 return make_info<L>(ifm_N, ifm_C, ifm_H, ifm_W);
183 template <Layout L> TensorInfo dst_info() const
185 return make_info<L>(ofm_N, ofm_C, ofm_H, ofm_W);
187 template <Layout L> TensorInfo ker_info() const
189 return make_info<L>(ker_N, ker_C, ker_H, ker_W);
191 TensorInfo bias_info(void) const { return make_info(ker_N); }
197 // Benchmark Implementations
202 inline nonius::benchmark_registry &local_benchmark_registry()
204 static nonius::benchmark_registry registry;
210 #define NONIUS_LOCAL_BENCHMARK(name, ...) \
213 static ::nonius::benchmark_registrar \
214 NONIUS_DETAIL_UNIQUE_NAME(benchmark_registrar)(local_benchmark_registry(), name, __VA_ARGS__); \
217 NONIUS_LOCAL_BENCHMARK("CLDeconvolutionLayer_NCHW", [](nonius::chronometer meter) {
218 CLDeconvolutionLayer deconv;
221 Configuration p{meter};
223 CLTensor src_tensor{};
224 CLTensor dst_tensor{};
225 CLTensor ker_tensor{};
227 src_tensor.allocator()->init(p.src_info<NCHW>());
228 dst_tensor.allocator()->init(p.dst_info<NCHW>());
229 ker_tensor.allocator()->init(p.ker_info<NCHW>());
233 check(deconv.validate(src_tensor.info(), ker_tensor.info(), nullptr, dst_tensor.info(),
234 p.deconv_info, p.inner_border_right, p.inner_border_top));
238 meter.measure([&](int) {
246 deconv.configure(&src_tensor, &ker_tensor, nullptr, &dst_tensor, p.deconv_info,
247 p.inner_border_right, p.inner_border_top);
249 src_tensor.allocator()->allocate();
250 ker_tensor.allocator()->allocate();
251 dst_tensor.allocator()->allocate();
254 meter.measure([&](int) {
256 CLScheduler::get().sync();
260 NONIUS_LOCAL_BENCHMARK("CLDeconvolutionLayer_NHWC", [](nonius::chronometer meter) {
261 CLDeconvolutionLayer deconv;
264 Configuration p{meter};
266 CLTensor src_tensor{};
267 CLTensor dst_tensor{};
268 CLTensor ker_tensor{};
270 src_tensor.allocator()->init(p.src_info<NHWC>());
271 dst_tensor.allocator()->init(p.dst_info<NHWC>());
272 ker_tensor.allocator()->init(p.ker_info<NHWC>());
276 check(deconv.validate(src_tensor.info(), ker_tensor.info(), nullptr, dst_tensor.info(),
277 p.deconv_info, p.inner_border_right, p.inner_border_top));
281 meter.measure([&](int) {
289 deconv.configure(&src_tensor, &ker_tensor, nullptr, &dst_tensor, p.deconv_info,
290 p.inner_border_right, p.inner_border_top);
292 src_tensor.allocator()->allocate();
293 ker_tensor.allocator()->allocate();
294 dst_tensor.allocator()->allocate();
297 meter.measure([&](int) {
299 CLScheduler::get().sync();
303 NONIUS_LOCAL_BENCHMARK("CLDeconvolutionLayerEx_NCHW", [](nonius::chronometer meter) {
304 CLDeconvolutionLayerEx deconv;
307 Configuration p{meter};
309 CLTensor src_tensor{};
310 CLTensor dst_tensor{};
311 CLTensor ker_tensor{};
313 src_tensor.allocator()->init(p.src_info<NCHW>());
314 dst_tensor.allocator()->init(p.dst_info<NCHW>());
315 ker_tensor.allocator()->init(p.ker_info<NCHW>());
319 check(deconv.validate(src_tensor.info(), ker_tensor.info(), nullptr, dst_tensor.info(),
320 p.deconv_info, p.inner_border_right, p.inner_border_top));
324 meter.measure([&](int) {
332 deconv.configure(&src_tensor, &ker_tensor, nullptr, &dst_tensor, p.deconv_info,
333 p.inner_border_right, p.inner_border_top);
335 src_tensor.allocator()->allocate();
336 ker_tensor.allocator()->allocate();
337 dst_tensor.allocator()->allocate();
340 meter.measure([&](int) {
342 CLScheduler::get().sync();
346 NONIUS_LOCAL_BENCHMARK("CLDeconvolutionLayerEx_NHWC", [](nonius::chronometer meter) {
347 CLDeconvolutionLayerEx deconv;
350 Configuration p{meter};
352 CLTensor src_tensor{};
353 CLTensor dst_tensor{};
354 CLTensor ker_tensor{};
356 src_tensor.allocator()->init(p.src_info<NHWC>());
357 dst_tensor.allocator()->init(p.dst_info<NHWC>());
358 ker_tensor.allocator()->init(p.ker_info<NHWC>());
362 check(deconv.validate(src_tensor.info(), ker_tensor.info(), nullptr, dst_tensor.info(),
363 p.deconv_info, p.inner_border_right, p.inner_border_top));
367 meter.measure([&](int) {
375 deconv.configure(&src_tensor, &ker_tensor, nullptr, &dst_tensor, p.deconv_info,
376 p.inner_border_right, p.inner_border_top);
378 src_tensor.allocator()->allocate();
379 ker_tensor.allocator()->allocate();
380 dst_tensor.allocator()->allocate();
383 meter.measure([&](int) {
385 CLScheduler::get().sync();
389 extern "C" nonius::benchmark_registry &benchmark_functions(void)
391 return local_benchmark_registry();