1 /*
2 * Copyright (c) 2017-2022 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/NEON/functions/NEWinogradConvolutionLayer.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/ITensorPack.h"
28 #include "arm_compute/core/Utils.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
31 #include "src/core/CPP/Validate.h"
32 #include "src/core/helpers/MemoryHelpers.h"
33 #include "src/cpu/kernels/CpuWinogradConv2dKernel.h"
34 #include "src/cpu/operators/CpuWinogradConv2d.h"
35
36 #include "src/core/NEON/kernels/convolution/common/utils.hpp"
37
38 namespace arm_compute
39 {
40 using namespace arm_compute::experimental;
41
42 struct NEWinogradConvolutionLayer::Impl
43 {
44 MemoryGroup memory_group{};
45 std::unique_ptr<cpu::CpuWinogradConv2d> op{ nullptr };
46 ITensorPack run_pack{};
47 ITensorPack prep_pack{};
48 WorkspaceData<Tensor> workspace{};
49 experimental::MemoryRequirements aux_mem_req{};
50 const ITensor *original_weights{ nullptr };
51 bool is_prepared{ false };
52 bool is_activationlayer_enabled{ false };
53 DataLayout data_layout{};
54 };
55
NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> & memory_manager)56 NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
57 : _impl(std::make_unique<Impl>())
58 {
59 _impl->memory_group = MemoryGroup(std::move(memory_manager));
60 }
61
62 NEWinogradConvolutionLayer::~NEWinogradConvolutionLayer() = default;
63
configure(const ITensor * input,const ITensor * weights,const ITensor * biases,ITensor * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)64 void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
65 bool enable_fast_math)
66 {
67 _impl->original_weights = weights;
68 _impl->op = std::make_unique<cpu::CpuWinogradConv2d>();
69 _impl->op->configure(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), conv_info, act_info, enable_fast_math);
70
71 _impl->aux_mem_req = _impl->op->workspace();
72 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
73 _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
74 _impl->workspace = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
75 }
76
run()77 void NEWinogradConvolutionLayer::run()
78 {
79 prepare();
80
81 MemoryGroupResourceScope scope_mg(_impl->memory_group);
82 _impl->op->run(_impl->run_pack);
83 }
84
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)85 Status NEWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
86 const ActivationLayerInfo &act_info, bool enable_fast_math)
87 {
88 return cpu::CpuWinogradConv2d::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math);
89 }
90
prepare()91 void NEWinogradConvolutionLayer::prepare()
92 {
93 if(!_impl->is_prepared)
94 {
95 _impl->op->prepare(_impl->prep_pack);
96 _impl->original_weights->mark_as_unused();
97
98 // Release temporary tensors that are only used in prepare stage
99 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
100
101 _impl->is_prepared = true;
102 }
103 }
104 } // namespace arm_compute
105