blob: f0c153d4f4de4b798c8009ee36f6b572c76c364d [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Georgios Pinitasddd79f52021-01-15 09:42:26 +00002 * Copyright (c) 2017-2021 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 */
Georgios Pinitas9fb11592018-04-26 20:34:58 +010024#include "arm_compute/runtime/NEON/functions/NEWinogradConvolutionLayer.h"
Pablo Tello89519332017-11-17 11:52:36 +000025
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026#include "arm_compute/core/Error.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010027#include "arm_compute/core/ITensorPack.h"
Pablo Tello89519332017-11-17 11:52:36 +000028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010032#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010033#include "src/cpu/kernels/CpuWinogradConv2dKernel.h"
34#include "src/cpu/operators/CpuWinogradConv2d.h"
Pablo Tello89519332017-11-17 11:52:36 +000035
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/NEON/kernels/convolution/common/utils.hpp"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010037#include "src/core/NEON/kernels/convolution/winograd/winograd.hpp"
Pablo Tellod6ca4782018-01-23 09:36:04 +000038
Pablo Tello89519332017-11-17 11:52:36 +000039namespace arm_compute
40{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010041using namespace arm_compute::experimental;
42
43struct NEWinogradConvolutionLayer::Impl
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000044{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010045 MemoryGroup memory_group{};
46 std::unique_ptr<cpu::CpuWinogradConv2d> op{ nullptr };
47 ITensorPack run_pack{};
48 ITensorPack prep_pack{};
49 WorkspaceData<Tensor> workspace{};
50 experimental::MemoryRequirements aux_mem_req{};
51 const ITensor *original_weights{ nullptr };
52 bool is_prepared{ false };
53 bool is_activationlayer_enabled{ false };
54 DataLayout data_layout{};
55};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000056
Michalis Spyroua4f378d2019-04-26 14:54:54 +010057NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Michalis Spyrou96f977e2021-07-01 12:20:56 +010058 : _impl(std::make_unique<Impl>())
Pablo Tello89519332017-11-17 11:52:36 +000059{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010060 _impl->memory_group = MemoryGroup(std::move(memory_manager));
Pablo Tello8f43d742019-03-27 09:28:32 +000061}
Pablo Tello89519332017-11-17 11:52:36 +000062
Michalis Spyrou96f977e2021-07-01 12:20:56 +010063NEWinogradConvolutionLayer::~NEWinogradConvolutionLayer() = default;
64
Giorgio Arenaa3221e62018-05-03 15:57:48 +010065void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
66 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +000067{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010068 _impl->original_weights = weights;
69 _impl->op = std::make_unique<cpu::CpuWinogradConv2d>();
70 _impl->op->configure(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), conv_info, act_info, enable_fast_math);
Pablo Tello89519332017-11-17 11:52:36 +000071
Michalis Spyrou96f977e2021-07-01 12:20:56 +010072 _impl->aux_mem_req = _impl->op->workspace();
73 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
74 _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
75 _impl->workspace = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Pablo Tello89519332017-11-17 11:52:36 +000076}
77
Georgios Pinitas9fb11592018-04-26 20:34:58 +010078void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +000079{
Georgios Pinitas72219332018-06-05 14:56:06 +010080 prepare();
81
Michalis Spyrou96f977e2021-07-01 12:20:56 +010082 MemoryGroupResourceScope scope_mg(_impl->memory_group);
83 _impl->op->run(_impl->run_pack);
Pablo Tello89519332017-11-17 11:52:36 +000084}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000085
Georgios Pinitas9fb11592018-04-26 20:34:58 +010086Status NEWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaa3221e62018-05-03 15:57:48 +010087 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000088{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010089 return cpu::CpuWinogradConv2d::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000090}
91
Georgios Pinitas72219332018-06-05 14:56:06 +010092void NEWinogradConvolutionLayer::prepare()
93{
Michalis Spyrou96f977e2021-07-01 12:20:56 +010094 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +010095 {
Michalis Spyrou96f977e2021-07-01 12:20:56 +010096 _impl->op->prepare(_impl->prep_pack);
97 _impl->original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +010098
Michalis Spyrou96f977e2021-07-01 12:20:56 +010099 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100100 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
Georgios Pinitasddd79f52021-01-15 09:42:26 +0000101
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100102 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100103 }
104}
Pablo Tello89519332017-11-17 11:52:36 +0000105} // namespace arm_compute