blob: 24d72eddd82744671bb6b00a440733dd687af26f [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Pablo Tello9ceebbe2018-01-10 16:44:13 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tello89519332017-11-17 11:52:36 +00003 *
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/core/NEON/kernels/NEWinogradLayerKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
Pablo Tello3d4968a2017-12-04 15:03:35 +000030#include "support/ToolchainSupport.h"
31
Pablo Tello9ceebbe2018-01-10 16:44:13 +000032#include "arm_compute/core/NEON/kernels/winograd/winograd_layer.hpp"
Pablo Tello3d4968a2017-12-04 15:03:35 +000033
Pablo Tello4e2c1392018-01-09 10:30:27 +000034namespace
35{
Pablo Tello9ceebbe2018-01-10 16:44:13 +000036using T = WinogradConvolutionLayer<2, 2, 3, 3, float, float>;
Pablo Tello4e2c1392018-01-09 10:30:27 +000037} // namespace
Pablo Tello89519332017-11-17 11:52:36 +000038
39namespace arm_compute
40{
Pablo Tello3d4968a2017-12-04 15:03:35 +000041class Winograd3x3F32::Private
42{
43public:
Pablo Tello9ceebbe2018-01-10 16:44:13 +000044 Private(
45 const int n_batches, /** Number of batches in the input and output tensors. */
46 const int n_input_channels, /** Number of feature maps in a batch of the input tensor. */
47 const int n_input_rows, /** Number of rows in a feature map of the input tensor. */
48 const int n_input_cols, /** Number of columns in a feature map of the input tensor. */
49 const int n_output_channels, /** Number of feature maps in the output tensor. */
50 const bool same_padding, /** Use "SAME" padding, otherwise use "VALID". */
51 const float *const weights, /** Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. */
52 float *const weights_storage, /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */
53 const float *const input, /** Pointer to NHWC ordered input tensor, in the spatial domain. */
54 float *const winograd_input, /** Pointer to working space for the input tensor in the Winograd domain. Must be at least the size returned by `get_input_storage_size`. */
55 float *const output, /** Pointer to NHWC ordered output tensor, in the spatial domain. */
56 float *const winograd_output /** Pointer to working space for the output tensor in the Winograd domain. Must be at least the size returned by `get_output_storage_size`. */
57 )
58 : convolver(n_batches, n_input_channels, n_input_rows, n_input_cols, n_output_channels, same_padding, weights, weights_storage, input, winograd_input, output, winograd_output)
Pablo Tello3d4968a2017-12-04 15:03:35 +000059 {
60 }
Pablo Tello3d4968a2017-12-04 15:03:35 +000061 T convolver;
62};
63
64Winograd3x3F32::~Winograd3x3F32()
65{
66}
67
Pablo Tello9ceebbe2018-01-10 16:44:13 +000068void Winograd3x3F32::transform_output()
Pablo Tello3d4968a2017-12-04 15:03:35 +000069{
Pablo Tello9ceebbe2018-01-10 16:44:13 +000070 auto win = _pimpl->convolver.output_transform.get_window();
71 _pimpl->convolver.output_transform.run(0, win);
Pablo Tello3d4968a2017-12-04 15:03:35 +000072}
73
Pablo Tello9ceebbe2018-01-10 16:44:13 +000074void Winograd3x3F32::transform_input()
Pablo Tello3d4968a2017-12-04 15:03:35 +000075{
Pablo Tello9ceebbe2018-01-10 16:44:13 +000076 auto win = _pimpl->convolver.input_transform.get_window();
77 _pimpl->convolver.input_transform.run(0, win);
Pablo Tello3d4968a2017-12-04 15:03:35 +000078}
79
Pablo Tello9ceebbe2018-01-10 16:44:13 +000080void Winograd3x3F32::transform_weights()
Pablo Tello3d4968a2017-12-04 15:03:35 +000081{
Pablo Tello9ceebbe2018-01-10 16:44:13 +000082 auto win = _pimpl->convolver.weights_transform.get_window();
83 _pimpl->convolver.weights_transform.run(0, win);
Pablo Tello3d4968a2017-12-04 15:03:35 +000084}
85
Pablo Tello9ceebbe2018-01-10 16:44:13 +000086Winograd3x3F32::Winograd3x3F32(
87 const int n_batches, /** Number of batches in the input and output tensors. */
88 const int n_input_channels, /** Number of feature maps in a batch of the input tensor. */
89 const int n_input_rows, /** Number of rows in a feature map of the input tensor. */
90 const int n_input_cols, /** Number of columns in a feature map of the input tensor. */
91 const int n_output_channels, /** Number of feature maps in the output tensor. */
92 const bool same_padding, /** Use "SAME" padding, otherwise use "VALID". */
93 const float *const weights, /** Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. */
94 float *const weights_storage, /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */
95 const float *const input, /** Pointer to NHWC ordered input tensor, in the spatial domain. */
96 float *const winograd_input, /** Pointer to working space for the input tensor in the Winograd domain. Must be at least the size returned by `get_input_storage_size`. */
97 float *const output, /** Pointer to NHWC ordered output tensor, in the spatial domain. */
98 float *const winograd_output /** Pointer to working space for the output tensor in the Winograd domain. Must be at least the size returned by `get_output_storage_size`. */
99)
100 : _pimpl(support::cpp14::make_unique<Private>(n_batches, n_input_channels, n_input_rows, n_input_cols, n_output_channels, same_padding, weights, weights_storage, input, winograd_input, output,
101 winograd_output))
Pablo Tello3d4968a2017-12-04 15:03:35 +0000102{
103}
104
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000105unsigned int NEWinogradLayerKernel::get_input_storage_size(const int n_batches, const int n_channels, const int n_rows, const int n_cols, const bool same_padding)
Pablo Tello3d4968a2017-12-04 15:03:35 +0000106{
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000107 return T::get_input_storage_size(n_batches, n_channels, n_rows, n_cols, same_padding);
Pablo Tello3d4968a2017-12-04 15:03:35 +0000108}
109
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000110unsigned int NEWinogradLayerKernel::get_output_storage_size(
111 const int n_batches, /** Number of batches in the output tensor. */
112 const int n_rows, /** Number of rows in each feature map of the input tensor. */
113 const int n_cols, /** Number of columns in each feature map of the input tensor. */
114 const int n_output_channels, /** Number of feature maps in the output tensor. */
115 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
116)
Pablo Tello3d4968a2017-12-04 15:03:35 +0000117{
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000118 return T::get_output_storage_size(n_batches, n_rows, n_cols, n_output_channels, same_padding);
Pablo Tello3d4968a2017-12-04 15:03:35 +0000119}
120
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000121size_t NEWinogradLayerKernel::get_weight_storage_size(const int n_output_channels, const int n_input_channels)
Pablo Tello3d4968a2017-12-04 15:03:35 +0000122{
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000123 return T::get_weight_storage_size(n_output_channels, n_input_channels);
Pablo Tello3d4968a2017-12-04 15:03:35 +0000124}
125
Pablo Tello89519332017-11-17 11:52:36 +0000126NEWinogradLayerKernel::NEWinogradLayerKernel()
Pablo Tello02541fb2017-12-15 09:48:59 +0000127 : _convolver(nullptr)
Pablo Tello89519332017-11-17 11:52:36 +0000128{
129}
130
Pablo Tello02541fb2017-12-15 09:48:59 +0000131void NEWinogradLayerKernel::configure(Winograd3x3F32 *convolver)
Pablo Tello89519332017-11-17 11:52:36 +0000132{
Pablo Tello02541fb2017-12-15 09:48:59 +0000133 ARM_COMPUTE_ERROR_ON_NULLPTR(convolver);
Pablo Tello89519332017-11-17 11:52:36 +0000134 _convolver = convolver;
Pablo Tello02541fb2017-12-15 09:48:59 +0000135 Window win;
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000136 auto win_last = _convolver->_pimpl->convolver.gemms.get_window();
137 win.set(Window::DimX, Window::Dimension(0, win_last, 1));
Pablo Tello89519332017-11-17 11:52:36 +0000138 INEKernel::configure(win);
139}
140
141void NEWinogradLayerKernel::run(const Window &window, const ThreadInfo &info)
142{
Pablo Tello89519332017-11-17 11:52:36 +0000143 ARM_COMPUTE_UNUSED(info);
144 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Pablo Tello02541fb2017-12-15 09:48:59 +0000145 const size_t first_gemm = window.x().start();
146 const size_t last_gemm = window.x().end();
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000147 _convolver->_pimpl->convolver.gemms.run(first_gemm, last_gemm);
Pablo Tello89519332017-11-17 11:52:36 +0000148}
149} // namespace arm_compute