b92130cece48f89aa4c1702333843bcac1e4ea6f
[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), vmulq_f32(vcvtq_f32_s32(iv.val[1]), vscale),
127       vmulq_f32(vcvtq_f32_s32(iv.val[2]), vscale), vmulq_f32(vcvtq_f32_s32(iv.val[3]), vscale),
128   }};
129   return ret;
130 }
131 } // namespace
132
133 NEMultiplyScaleFactorKernel::NEMultiplyScaleFactorKernel()
134     : _input(nullptr), _scale_factor(nullptr), _output(nullptr), _multiplier(1.f)
135 {
136 }
137
138 void NEMultiplyScaleFactorKernel::configure(const ITensor *input, const ITensor *scale_factor,
139                                             ITensor *output, float multiplier)
140 {
141   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
142   ARM_COMPUTE_ERROR_THROW_ON(
143       validate_arguments(input->info(), scale_factor->info(), output->info()));
144
145   _input = input;
146   _scale_factor = scale_factor;
147   _output = output;
148   _multiplier = multiplier;
149
150   // Configure kernel window
151   Window win_config = calculate_max_window(*input->info(), Steps());
152
153   Coordinates coord;
154   coord.set_num_dimensions(output->info()->num_dimensions());
155   output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
156
157   INEKernel::configure(win_config);
158 }
159
160 Status NEMultiplyScaleFactorKernel::validate(const ITensorInfo *input,
161                                              const ITensorInfo *scale_factor,
162                                              const ITensorInfo *output, float multiplier)
163 {
164   ARM_COMPUTE_UNUSED(multiplier);
165   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, scale_factor, output));
166
167   return Status{};
168 }
169
170 template <typename T> void NEMultiplyScaleFactorKernel::multiply(const Window &window)
171 {
172   constexpr auto window_step = 16;
173   const auto window_start_x = static_cast<int>(window.x().start());
174   const auto window_end_x = static_cast<int>(window.x().end());
175
176   // Collapse window and reset first dimension to handle tail calculations manually
177   // Support Only 2D input
178   Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
179   Iterator input(_input, win_collapsed);
180   Iterator output(_output, win_collapsed);
181   win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
182   execute_window_loop(
183       win_collapsed,
184       [&](const Coordinates &id) {
185         auto scale = *reinterpret_cast<T *>(_scale_factor->ptr_to_element({id.y()}));
186         scale *= _multiplier;
187
188         const auto input_ptr = reinterpret_cast<const int32_t *>(input.ptr());
189         auto output_ptr = reinterpret_cast<T *>(output.ptr());
190         int x = window_start_x;
191         for (; x <= (window_end_x - window_step); x += window_step)
192         {
193           store_result<float>(&output_ptr[x], multiply_scale_vec(load_value(&input_ptr[x]), scale));
194         }
195         // Compute left-over elements
196         for (; x < window_end_x; ++x)
197         {
198           output_ptr[x] = input_ptr[x] * scale;
199         }
200       },
201       input, output);
202 }
203
204 void NEMultiplyScaleFactorKernel::run(const Window &window, const ThreadInfo &info)
205 {
206   ARM_COMPUTE_UNUSED(info);
207   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
208   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
209
210   switch (_output->info()->data_type())
211   {
212     case DataType::F32:
213       NEMultiplyScaleFactorKernel::multiply<float>(window);
214       break;
215 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
216     case DataType::F16:
217       NEMultiplyScaleFactorKernel::multiply<float16_t>(window);
218       break;
219 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
220     default:
221       ARM_COMPUTE_ERROR("Unsupported data type.");
222   }
223 }