Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / NEON / kernels / NEMultiplyScaleFactorKernel.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2017-2019 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40
41 #include "arm_compute/core/NEON/kernels/NEMuliplyScaleFactorKernel.h"
42
43 #include "arm_compute/core/Error.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/core/NEON/NEAsymm.h"
46 #include "arm_compute/core/NEON/wrapper/wrapper.h"
47 #include "arm_compute/core/Utils.h"
48 #include "arm_compute/core/Validate.h"
49 #include "arm_compute/core/Window.h"
50
51 #include "arm_compute/core/CPP/Validate.h"
52
53 #include <arm_neon.h>
54
55 using namespace arm_compute;
56
57 namespace
58 {
59 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *scale_factor,
60                           const ITensorInfo *output)
61 {
62   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
63   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
64   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
65   ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(output);
66   ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0);
67   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
68   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
69   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scale_factor, 1, DataType::F16,
70                                                        DataType::F32);
71   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->tensor_shape().total_size() == 0);
72   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->num_dimensions() > 1);
73   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->dimension(0) != input->dimension(1));
74
75   // Checks performed when output is configured
76   if ((output->total_size() != 0))
77   {
78     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
79   }
80
81   return Status{};
82 }
83
84 inline int32x4x4_t load_value(const int32_t *input_ptr)
85 {
86   return {wrapper::vloadq(input_ptr), wrapper::vloadq(input_ptr + 4),
87           wrapper::vloadq(input_ptr + 8), wrapper::vloadq(input_ptr + 12)};
88 }
89
90 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
91 inline const float32x4x4_t load_value(const float16_t *input_ptr)
92 {
93   return {vcvt_f32_f16(wrapper::vload(input_ptr)), vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
94           vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
95           vcvt_f32_f16(wrapper::vload(input_ptr + 12))};
96 }
97
98 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
99
100 template <typename T> inline void store_result(T *ptr, const float32x4x4_t &v)
101 {
102   ARM_COMPUTE_UNUSED(ptr, v);
103 }
104
105 template <> inline void store_result<float>(float *ptr, const float32x4x4_t &v)
106 {
107   wrapper::vstore(ptr, v.val[0]);
108   wrapper::vstore(ptr + 4, v.val[1]);
109   wrapper::vstore(ptr + 8, v.val[2]);
110   wrapper::vstore(ptr + 12, v.val[3]);
111 }
112
113 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
114 template <> inline void store_result<float16_t>(float16_t *ptr, const float32x4x4_t &v)
115 {
116   wrapper::vstore(ptr, vcombine_f16(vcvt_f16_f32(v.val[0]), vcvt_f16_f32(v.val[1])));
117   wrapper::vstore(ptr + 8, vcombine_f16(vcvt_f16_f32(v.val[2]), vcvt_f16_f32(v.val[3])));
118 }
119 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
120
121 inline float32x4x4_t multiply_scale_vec(const int32x4x4_t &iv, float scale)
122 {
123   const float32x4_t vscale = vdupq_n_f32(scale);
124
125   const float32x4x4_t ret = {{
126     vmulq_f32(vcvtq_f32_s32(iv.val[0]), vscale),
127     vmulq_f32(vcvtq_f32_s32(iv.val[1]), vscale),
128     vmulq_f32(vcvtq_f32_s32(iv.val[2]), vscale),
129     vmulq_f32(vcvtq_f32_s32(iv.val[3]), vscale),
130   }};
131   return ret;
132 }
133 } // namespace
134
135 NEMultiplyScaleFactorKernel::NEMultiplyScaleFactorKernel()
136   : _input(nullptr), _scale_factor(nullptr), _output(nullptr), _multiplier(1.f)
137 {
138 }
139
140 void NEMultiplyScaleFactorKernel::configure(const ITensor *input, const ITensor *scale_factor,
141                                             ITensor *output, float multiplier)
142 {
143   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
144   ARM_COMPUTE_ERROR_THROW_ON(
145     validate_arguments(input->info(), scale_factor->info(), output->info()));
146
147   _input = input;
148   _scale_factor = scale_factor;
149   _output = output;
150   _multiplier = multiplier;
151
152   // Configure kernel window
153   Window win_config = calculate_max_window(*input->info(), Steps());
154
155   Coordinates coord;
156   coord.set_num_dimensions(output->info()->num_dimensions());
157   output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
158
159   INEKernel::configure(win_config);
160 }
161
162 Status NEMultiplyScaleFactorKernel::validate(const ITensorInfo *input,
163                                              const ITensorInfo *scale_factor,
164                                              const ITensorInfo *output, float multiplier)
165 {
166   ARM_COMPUTE_UNUSED(multiplier);
167   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, scale_factor, output));
168
169   return Status{};
170 }
171
172 template <typename T> void NEMultiplyScaleFactorKernel::multiply(const Window &window)
173 {
174   constexpr auto window_step = 16;
175   const auto window_start_x = static_cast<int>(window.x().start());
176   const auto window_end_x = static_cast<int>(window.x().end());
177
178   // Collapse window and reset first dimension to handle tail calculations manually
179   // Support Only 2D input
180   Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
181   Iterator input(_input, win_collapsed);
182   Iterator output(_output, win_collapsed);
183   win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
184   execute_window_loop(
185     win_collapsed,
186     [&](const Coordinates &id) {
187       auto scale = *reinterpret_cast<T *>(_scale_factor->ptr_to_element({id.y()}));
188       scale *= _multiplier;
189
190       const auto input_ptr = reinterpret_cast<const int32_t *>(input.ptr());
191       auto output_ptr = reinterpret_cast<T *>(output.ptr());
192       int x = window_start_x;
193       for (; x <= (window_end_x - window_step); x += window_step)
194       {
195         store_result<float>(&output_ptr[x], multiply_scale_vec(load_value(&input_ptr[x]), scale));
196       }
197       // Compute left-over elements
198       for (; x < window_end_x; ++x)
199       {
200         output_ptr[x] = input_ptr[x] * scale;
201       }
202     },
203     input, output);
204 }
205
206 void NEMultiplyScaleFactorKernel::run(const Window &window, const ThreadInfo &info)
207 {
208   ARM_COMPUTE_UNUSED(info);
209   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
210   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
211
212   switch (_output->info()->data_type())
213   {
214     case DataType::F32:
215       NEMultiplyScaleFactorKernel::multiply<float>(window);
216       break;
217 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
218     case DataType::F16:
219       NEMultiplyScaleFactorKernel::multiply<float16_t>(window);
220       break;
221 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
222     default:
223       ARM_COMPUTE_ERROR("Unsupported data type.");
224   }
225 }