2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright (c) 2017-2019 ARM Limited.
20 * SPDX-License-Identifier: MIT
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:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
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
41 #include "arm_compute/core/NEON/kernels/NEMuliplyScaleFactorKernel.h"
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"
51 #include "arm_compute/core/CPP/Validate.h"
55 using namespace arm_compute;
59 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *scale_factor,
60 const ITensorInfo *output)
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,
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));
75 // Checks performed when output is configured
76 if ((output->total_size() != 0))
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
84 inline int32x4x4_t load_value(const int32_t *input_ptr)
86 return {wrapper::vloadq(input_ptr), wrapper::vloadq(input_ptr + 4),
87 wrapper::vloadq(input_ptr + 8), wrapper::vloadq(input_ptr + 12)};
90 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
91 inline const float32x4x4_t load_value(const float16_t *input_ptr)
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))};
98 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
100 template <typename T> inline void store_result(T *ptr, const float32x4x4_t &v)
102 ARM_COMPUTE_UNUSED(ptr, v);
105 template <> inline void store_result<float>(float *ptr, const float32x4x4_t &v)
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]);
113 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
114 template <> inline void store_result<float16_t>(float16_t *ptr, const float32x4x4_t &v)
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])));
119 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
121 inline float32x4x4_t multiply_scale_vec(const int32x4x4_t &iv, float scale)
123 const float32x4_t vscale = vdupq_n_f32(scale);
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),
135 NEMultiplyScaleFactorKernel::NEMultiplyScaleFactorKernel()
136 : _input(nullptr), _scale_factor(nullptr), _output(nullptr), _multiplier(1.f)
140 void NEMultiplyScaleFactorKernel::configure(const ITensor *input, const ITensor *scale_factor,
141 ITensor *output, float multiplier)
143 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
144 ARM_COMPUTE_ERROR_THROW_ON(
145 validate_arguments(input->info(), scale_factor->info(), output->info()));
148 _scale_factor = scale_factor;
150 _multiplier = multiplier;
152 // Configure kernel window
153 Window win_config = calculate_max_window(*input->info(), Steps());
156 coord.set_num_dimensions(output->info()->num_dimensions());
157 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
159 INEKernel::configure(win_config);
162 Status NEMultiplyScaleFactorKernel::validate(const ITensorInfo *input,
163 const ITensorInfo *scale_factor,
164 const ITensorInfo *output, float multiplier)
166 ARM_COMPUTE_UNUSED(multiplier);
167 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, scale_factor, output));
172 template <typename T> void NEMultiplyScaleFactorKernel::multiply(const Window &window)
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());
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));
186 [&](const Coordinates &id) {
187 auto scale = *reinterpret_cast<T *>(_scale_factor->ptr_to_element({id.y()}));
188 scale *= _multiplier;
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)
195 store_result<float>(&output_ptr[x], multiply_scale_vec(load_value(&input_ptr[x]), scale));
197 // Compute left-over elements
198 for (; x < window_end_x; ++x)
200 output_ptr[x] = input_ptr[x] * scale;
206 void NEMultiplyScaleFactorKernel::run(const Window &window, const ThreadInfo &info)
208 ARM_COMPUTE_UNUSED(info);
209 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
210 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
212 switch (_output->info()->data_type())
215 NEMultiplyScaleFactorKernel::multiply<float>(window);
217 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
219 NEMultiplyScaleFactorKernel::multiply<float16_t>(window);
221 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
223 ARM_COMPUTE_ERROR("Unsupported data type.");