blob: 826a386557f91c3ce295486a945adad5d82a3dbf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include <arm_neon.h>
36#include <cstddef>
37#include <cstdint>
38
39using namespace arm_compute;
40
41NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel()
42 : _accum(nullptr), _biases(nullptr)
43{
44}
45
46void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases)
47{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010048 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::QS8, DataType::QS16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010050 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(biases, accum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() != 1);
52
53 _biases = biases;
54 _accum = accum;
55
56 constexpr unsigned int num_elems_processed_per_iteration = 16;
57
58 // Configure kernel window
59 Window win = calculate_max_window(*accum->info(), Steps(num_elems_processed_per_iteration));
60
61 AccessWindowStatic biases_access(biases->info(), 0, 0, biases->info()->dimension(0), biases->info()->dimension(1));
62
63 update_window_and_padding(win,
64 AccessWindowHorizontal(accum->info(), 0, num_elems_processed_per_iteration),
65 biases_access);
66
67 AccessWindowHorizontal output_access(accum->info(), 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->info()->num_dimensions());
72 output_access.set_valid_region(win, ValidRegion(coord, accum->info()->tensor_shape()));
73
74 INEKernel::configure(win);
75}
76
77void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window)
78{
79 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
80 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
81
82 Window win_biases;
83 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
84 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
85
86 Iterator in0_out(_accum, window);
87 Iterator in1(_biases, win_biases);
88
89 switch(_accum->info()->data_type())
90 {
91 case DataType::F32:
92 {
93 execute_window_loop(window, [&](const Coordinates & id)
94 {
95 const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr()));
96 const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr()));
97 const float32x4x4_t res =
98 {
99 {
100 vaddq_f32(accum.val[0], biases.val[0]),
101 vaddq_f32(accum.val[1], biases.val[1]),
102 vaddq_f32(accum.val[2], biases.val[2]),
103 vaddq_f32(accum.val[3], biases.val[3])
104 }
105 };
106
107 vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res);
108 },
109 in0_out, in1);
110 break;
111 }
112 case DataType::QS8:
113 {
114 execute_window_loop(window, [&](const Coordinates & id)
115 {
116 const qint8x16_t accum = vld1q_qs8(reinterpret_cast<const qint8_t *>(in0_out.ptr()));
117 const qint8x16_t biases = vld1q_qs8(reinterpret_cast<const qint8_t *>(in1.ptr()));
118
119 vst1q_qs8(reinterpret_cast<qint8_t *>(in0_out.ptr()), vqaddq_qs8(accum, biases));
120 },
121 in0_out, in1);
122 break;
123 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100124 case DataType::QS16:
125 {
126 execute_window_loop(window, [&](const Coordinates & id)
127 {
128 qint16x8x2_t accum = vld2q_s16(reinterpret_cast<const qint16_t *>(in0_out.ptr()));
129 const qint16x8x2_t biases = vld2q_s16(reinterpret_cast<const qint16_t *>(in1.ptr()));
130
131 accum.val[0] = vqaddq_qs16(accum.val[0], biases.val[0]);
132 accum.val[1] = vqaddq_qs16(accum.val[1], biases.val[1]);
133
134 vst2q_s16(reinterpret_cast<qint16_t *>(in0_out.ptr()), accum);
135 },
136 in0_out, in1);
137 break;
138 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 default:
140 ARM_COMPUTE_ERROR("Data type not supported");
141 break;
142 }
143}