Compute Library
 21.02
NEConvolution.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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  */
25 
26 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Utils.h"
37 
38 #include <array>
39 #include <utility>
40 
41 namespace arm_compute
42 {
44 
45 void NEConvolution3x3::configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
46 {
47  auto k = std::make_unique<NEConvolution3x3Kernel>();
48  k->configure(input, output, conv, scale, border_mode == BorderMode::UNDEFINED);
49  _kernel = std::move(k);
50 
51  auto b = std::make_unique<NEFillBorderKernel>();
52  b->configure(input, _kernel->border_size(), border_mode, PixelValue(constant_border_value));
53  _border_handler = std::move(b);
54 }
55 
56 template <unsigned int matrix_size>
58 
59 template <unsigned int matrix_size>
60 NEConvolutionSquare<matrix_size>::NEConvolutionSquare(std::shared_ptr<IMemoryManager> memory_manager)
61  : _memory_group(std::move(memory_manager)), _tmp(), _is_separable(false), _kernel_hor(), _kernel_vert(), _kernel(), _border_handler()
62 {
63 }
64 
65 template <unsigned int matrix_size>
66 void NEConvolutionSquare<matrix_size>::configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode,
67  uint8_t constant_border_value)
68 {
69  ARM_COMPUTE_ERROR_ON(conv == nullptr);
72 
73  std::array<int16_t, matrix_size> conv_col{ { 0 } };
74  std::array<int16_t, matrix_size> conv_row{ { 0 } };
75 
76  _is_separable = separate_matrix(conv, conv_col.data(), conv_row.data(), matrix_size);
77 
78  auto b = std::make_unique<NEFillBorderKernel>();
79  if(_is_separable)
80  {
81  DataType intermediate_type = DataType::UNKNOWN;
82  std::tie(std::ignore, intermediate_type) = data_type_for_convolution(conv_col.data(), conv_row.data(), matrix_size);
83 
84  _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), 1, intermediate_type));
85 
86  // Manage intermediate buffers
87  _memory_group.manage(&_tmp);
88 
89  // Calculate scale
90  if(scale == 0)
91  {
92  scale = calculate_matrix_scale(conv, matrix_size);
93  }
94 
95  _kernel_hor = std::make_unique<NESeparableConvolutionHorKernel<matrix_size>>();
96  _kernel_vert = std::make_unique<NESeparableConvolutionVertKernel<matrix_size>>();
97 
98  _kernel_hor->configure(input, &_tmp, conv_row.data(), border_mode == BorderMode::UNDEFINED);
99  _kernel_vert->configure(&_tmp, output, conv_col.data(), scale, border_mode == BorderMode::UNDEFINED);
100 
101  _tmp.allocator()->allocate();
102 
103  b->configure(input, _kernel_hor->border_size(), border_mode, PixelValue(constant_border_value));
104  }
105  else
106  {
107  _kernel = std::make_unique<NEConvolutionKernel<matrix_size>>();
108  _kernel->configure(input, output, conv, scale, border_mode == BorderMode::UNDEFINED);
109  b->configure(input, _kernel->border_size(), border_mode, PixelValue(constant_border_value));
110  }
111  _border_handler = std::move(b);
112 }
113 
114 template <unsigned int matrix_size>
116 {
117  NEScheduler::get().schedule(_border_handler.get(), Window::DimZ);
118 
119  if(_is_separable)
120  {
121  MemoryGroupResourceScope scope_mg(_memory_group);
122 
123  NEScheduler::get().schedule(_kernel_hor.get(), Window::DimY);
124  NEScheduler::get().schedule(_kernel_vert.get(), Window::DimY);
125  }
126  else
127  {
128  NEScheduler::get().schedule(_kernel.get(), Window::DimY);
129  }
130 }
131 
135 
137 
138 void NEConvolutionRectangle::configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t rows, uint32_t cols, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
139 {
140  border_mode = (border_mode == BorderMode::UNDEFINED) ? BorderMode::CONSTANT : border_mode;
141  auto k = std::make_unique<NEConvolutionRectangleKernel>();
142  k->configure(input, output, conv, rows, cols, scale, false);
143  _kernel = std::move(k);
144 
145  auto b = std::make_unique<NEFillBorderKernel>();
146  b->configure(input, _kernel->border_size(), border_mode, PixelValue(constant_border_value));
147  _border_handler = std::move(b);
148 }
149 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:265
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
SimpleTensor< float > b
Definition: DFT.cpp:157
~NEConvolutionRectangle()
Default destructor.
1 channel, 1 U8 per channel
NEConvolutionSquare(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
std::pair< DataType, DataType > data_type_for_convolution(const int16_t *conv_col, const int16_t *conv_row, size_t size)
Calculate accurary required by the horizontal and vertical convolution computations.
Definition: Utils.h:806
~NEConvolution3x3()
Default destructor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size)
Calculate the scale of the given square matrix.
Definition: Utils.h:727
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
TensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: Tensor.cpp:48
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value=0)
Initialize the function&#39;s source, destination, conv and border_mode.
void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t rows, uint32_t cols, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value=0)
Initialize the function&#39;s source, destination, conv and border_mode.
void run() override
Run the kernels contained in the function.
Basic function to execute convolution of size 5x5, 7x7, 9x9.
Definition: NEConvolution.h:93
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
~NEConvolutionSquare()
Default destructor.
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value=0)
Initialize the function&#39;s source, destination, conv and border_mode.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Borders are left undefined.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
bool separate_matrix(const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size)
Separate a 2D convolution into two 1D convolutions.
Definition: Utils.h:667
DataType
Available data types.
Definition: Types.h:77
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94