blob: 421a6f0ef9ea168e0646bab34dc23f708c593a81 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 "arm_compute/core/NEON/kernels/NEGEMMMatrixAccumulateBiasesKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/NEFixedPoint.h"
31#include "arm_compute/core/Types.h"
Gian Marco900b78f2017-11-20 15:01:12 +000032#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <arm_neon.h>
37#include <cstddef>
38#include <cstdint>
39
40using namespace arm_compute;
41
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000042namespace
43{
44inline Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases)
45{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000047 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000048 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
49 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != accum->dimension(0));
50
51 return Status{};
52}
53
54inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases)
55{
56 constexpr unsigned int num_elems_processed_per_iteration = 16;
57
58 // Configure kernel window
59 Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration));
60
61 bool window_changed = update_window_and_padding(win,
62 AccessWindowHorizontal(accum, 0, num_elems_processed_per_iteration),
63 AccessWindowStatic(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->tensor_shape().y()));
64
65 AccessWindowHorizontal output_access(accum, 0, num_elems_processed_per_iteration);
66
67 // Set the valid region for the accum tensor
68 Coordinates coord;
69 coord.set_num_dimensions(accum->num_dimensions());
70 output_access.set_valid_region(win, ValidRegion(coord, accum->tensor_shape()));
71
72 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
73 return std::make_pair(err, win);
74}
75} // namespace
76
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel()
78 : _accum(nullptr), _biases(nullptr)
79{
80}
81
82void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases)
83{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000084 ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases);
85
86 // Perform validate step
87 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89 _biases = biases;
90 _accum = accum;
91
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 // Configure kernel window
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000093 auto win_config = validate_and_configure_window(accum->info(), biases->info());
94 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
95 INEKernel::configure(win_config.second);
96}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000098Status NEGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases)
99{
100 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases));
101 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000103 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104}
105
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100106void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100108 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
111
112 Window win_biases;
113 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
114 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
115
116 Iterator in0_out(_accum, window);
117 Iterator in1(_biases, win_biases);
118
119 switch(_accum->info()->data_type())
120 {
121 case DataType::F32:
122 {
123 execute_window_loop(window, [&](const Coordinates & id)
124 {
125 const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr()));
126 const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr()));
127 const float32x4x4_t res =
128 {
129 {
130 vaddq_f32(accum.val[0], biases.val[0]),
131 vaddq_f32(accum.val[1], biases.val[1]),
132 vaddq_f32(accum.val[2], biases.val[2]),
133 vaddq_f32(accum.val[3], biases.val[3])
134 }
135 };
136
137 vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res);
138 },
139 in0_out, in1);
140 break;
141 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000142#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100143 case DataType::F16:
144 {
145 execute_window_loop(window, [&](const Coordinates & id)
146 {
147 const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr()));
148 const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr()));
149 const float16x8x2_t res =
150 {
151 {
152 vaddq_f16(accum.val[0], biases.val[0]),
153 vaddq_f16(accum.val[1], biases.val[1])
154 }
155 };
156
157 vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res);
158 },
159 in0_out, in1);
160 break;
161 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000162#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 default:
164 ARM_COMPUTE_ERROR("Data type not supported");
165 break;
166 }
167}