blob: 42353ed0eb58d8a0ac6495d9b739f5c1619bdbf3 [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"
Anthony Barbiereaefd002018-07-20 17:49:35 +010027#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/NEFixedPoint.h"
32#include "arm_compute/core/Types.h"
Gian Marco900b78f2017-11-20 15:01:12 +000033#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <arm_neon.h>
38#include <cstddef>
39#include <cstdint>
40
41using namespace arm_compute;
42
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000043namespace
44{
45inline Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases)
46{
Anthony Barbiereaefd002018-07-20 17:49:35 +010047 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(accum);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000050 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
51 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != accum->dimension(0));
52
53 return Status{};
54}
55
56inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases)
57{
58 constexpr unsigned int num_elems_processed_per_iteration = 16;
59
60 // Configure kernel window
61 Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration));
62
63 bool window_changed = update_window_and_padding(win,
64 AccessWindowHorizontal(accum, 0, num_elems_processed_per_iteration),
65 AccessWindowStatic(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->tensor_shape().y()));
66
67 AccessWindowHorizontal output_access(accum, 0, num_elems_processed_per_iteration);
68
69 // Set the valid region for the accum tensor
70 Coordinates coord;
71 coord.set_num_dimensions(accum->num_dimensions());
72 output_access.set_valid_region(win, ValidRegion(coord, accum->tensor_shape()));
73
74 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
75 return std::make_pair(err, win);
76}
77} // namespace
78
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel()
80 : _accum(nullptr), _biases(nullptr)
81{
82}
83
84void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases)
85{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000086 ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases);
87
88 // Perform validate step
89 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 _biases = biases;
92 _accum = accum;
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 // Configure kernel window
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000095 auto win_config = validate_and_configure_window(accum->info(), biases->info());
96 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
97 INEKernel::configure(win_config.second);
98}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000100Status NEGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases)
101{
102 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases));
103 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000105 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106}
107
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100108void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100110 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
112 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
113
114 Window win_biases;
115 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
116 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
117
118 Iterator in0_out(_accum, window);
119 Iterator in1(_biases, win_biases);
120
121 switch(_accum->info()->data_type())
122 {
123 case DataType::F32:
124 {
125 execute_window_loop(window, [&](const Coordinates & id)
126 {
127 const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr()));
128 const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr()));
129 const float32x4x4_t res =
130 {
131 {
132 vaddq_f32(accum.val[0], biases.val[0]),
133 vaddq_f32(accum.val[1], biases.val[1]),
134 vaddq_f32(accum.val[2], biases.val[2]),
135 vaddq_f32(accum.val[3], biases.val[3])
136 }
137 };
138
139 vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res);
140 },
141 in0_out, in1);
142 break;
143 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000144#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100145 case DataType::F16:
146 {
147 execute_window_loop(window, [&](const Coordinates & id)
148 {
149 const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr()));
150 const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr()));
151 const float16x8x2_t res =
152 {
153 {
154 vaddq_f16(accum.val[0], biases.val[0]),
155 vaddq_f16(accum.val[1], biases.val[1])
156 }
157 };
158
159 vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res);
160 },
161 in0_out, in1);
162 break;
163 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000164#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 default:
166 ARM_COMPUTE_ERROR("Data type not supported");
167 break;
168 }
169}