• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 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 "src/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/IAccessWindow.h"
29 #include "arm_compute/core/ITensor.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Utils.h"
32 #include "arm_compute/core/Validate.h"
33 #include "arm_compute/core/Window.h"
34 #include "src/core/NEON/NEAsymm.h"
35 #include "src/core/NEON/NEFixedPoint.h"
36 #include "src/core/NEON/wrapper/wrapper.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
39 
40 #include <cstdint>
41 
42 namespace arm_compute
43 {
44 namespace
45 {
46 template <typename T>
depth_concat(const ITensor * in,ITensor * out,unsigned int depth_offset,const Window & window)47 void depth_concat(const ITensor *in, ITensor *out, unsigned int depth_offset, const Window &window)
48 {
49     // Offset input
50     uint8_t *input_ptr = in->buffer() + in->info()->offset_first_element_in_bytes();
51 
52     // Offset output
53     uint8_t *output_ptr = out->buffer() + out->info()->offset_first_element_in_bytes() + depth_offset * out->info()->strides_in_bytes()[2];
54 
55     const auto window_start_x = static_cast<int>(window.x().start());
56     const auto window_end_x   = static_cast<int>(window.x().end());
57     const int  window_step_x  = 16 / out->info()->element_size();
58 
59     Window win{ window };
60     win.set(Window::DimX, Window::Dimension(0, 1, 1));
61     win.set(Window::DimZ, Window::Dimension(0, in->info()->tensor_shape().z(), 1));
62 
63     Iterator input(in, win);
64     Iterator output(out, win);
65 
66     const DataType                dt           = in->info()->data_type();
67     const UniformQuantizationInfo input_qinfo  = in->info()->quantization_info().uniform();
68     const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform();
69     if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
70     {
71         execute_window_loop(win, [&](const Coordinates &)
72         {
73             const auto in_ptr  = reinterpret_cast<const uint8_t *>(input_ptr + input.offset());
74             const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset());
75             int        x       = window_start_x;
76             for(; x <= (window_end_x - window_step_x); x += window_step_x)
77             {
78                 wrapper::vstore(out_ptr + x, vquantize(vdequantize(wrapper::vloadq(in_ptr + x), input_qinfo), output_qinfo));
79             }
80 
81             // Compute left-over elements
82             for(; x < window_end_x; ++x)
83             {
84                 *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), input_qinfo), output_qinfo);
85             }
86         },
87         input, output);
88     }
89     else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
90     {
91         execute_window_loop(win, [&](const Coordinates &)
92         {
93             const auto in_ptr  = reinterpret_cast<const int8_t *>(input_ptr + input.offset());
94             const auto out_ptr = reinterpret_cast<int8_t *>(output_ptr + output.offset());
95             int        x       = window_start_x;
96             for(; x <= (window_end_x - window_step_x); x += window_step_x)
97             {
98                 wrapper::vstore(out_ptr + x, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr + x), input_qinfo), output_qinfo));
99             }
100 
101             // Compute left-over elements
102             for(; x < window_end_x; ++x)
103             {
104                 *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), input_qinfo), output_qinfo);
105             }
106         },
107         input, output);
108     }
109     else
110     {
111         execute_window_loop(win, [&](const Coordinates &)
112         {
113             const auto in_ptr  = reinterpret_cast<const T *>(input_ptr + input.offset());
114             const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
115             int        x       = window_start_x;
116             for(; x <= (window_end_x - window_step_x); x += window_step_x)
117             {
118                 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
119             }
120             // Compute left-over elements
121             for(; x < window_end_x; ++x)
122             {
123                 *(out_ptr + x) = *(in_ptr + x);
124             }
125         },
126         input, output);
127     }
128 }
129 
validate_arguments(const ITensorInfo * input,unsigned int depth_offset,const ITensorInfo * output)130 Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
131 {
132     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
133     //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
134     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
135     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
136 
137     ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
138     ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
139     ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
140     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
141 
142     return Status{};
143 }
144 } // namespace
145 
NEDepthConcatenateLayerKernel()146 NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
147     : _func(nullptr), _depth_offset(0)
148 {
149 }
150 
configure(const ITensorInfo * input,unsigned int depth_offset,ITensorInfo * output)151 void NEDepthConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int depth_offset, ITensorInfo *output)
152 {
153     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
154     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, depth_offset, output));
155 
156     _func         = nullptr;
157     _depth_offset = depth_offset;
158 
159     switch(input->data_type())
160     {
161         case DataType::QASYMM8:
162             _func = &depth_concat<uint8_t>;
163             break;
164         case DataType::QASYMM8_SIGNED:
165             _func = &depth_concat<int8_t>;
166             break;
167         case DataType::F16:
168             _func = &depth_concat<uint16_t>;
169             break;
170         case DataType::F32:
171             _func = &depth_concat<uint32_t>;
172             break;
173         default:
174             ARM_COMPUTE_ERROR("Unsupported data type.");
175     }
176 
177     // Configure kernel window
178     Window      win = calculate_max_window(*output, Steps());
179     Coordinates coord;
180     coord.set_num_dimensions(output->num_dimensions());
181 
182     output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
183     INEKernel::configure(win);
184 }
185 
validate(const arm_compute::ITensorInfo * input,unsigned int depth_offset,const arm_compute::ITensorInfo * output)186 Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
187                                                unsigned int                    depth_offset,
188                                                const arm_compute::ITensorInfo *output)
189 {
190     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output));
191     return Status{};
192 }
193 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)194 void NEDepthConcatenateLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
195 {
196     ARM_COMPUTE_UNUSED(info);
197     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
198     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
199     ARM_COMPUTE_ERROR_ON(_func == nullptr);
200 
201     (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC),
202              tensors.get_tensor(TensorType::ACL_DST),
203              _depth_offset,
204              window);
205 }
206 } // namespace arm_compute
207