blob: adda78f1640dd41a6ecb16d2acf992b581fe4de2 [file] [log] [blame]
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001/*
Michael Tyler7d9a6262023-02-01 16:37:07 +00002 * Copyright (c) 2021, 2023 Arm Limited.
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00003 *
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
25#if defined(__aarch64__)
26
27#include "arm_gemm.hpp"
28#include "src/core/NEON/kernels/arm_gemm/utils.hpp"
29#include "src/core/NEON/kernels/assembly/depthwise.hpp"
30#include <cstdint>
31
32namespace arm_conv {
33namespace depthwise {
34
35struct interleave_a64_s8q_3x3_dot
36{
37 static size_t get_packed_size(const DepthwiseArgs &);
38 static void pack_parameters(unsigned int n_channels, void *outptr, const int32_t *bias, const int8_t *weights, const arm_gemm::Requantize32 &qp, size_t ld_weight_col, size_t ld_weight_row);
39};
40
41size_t interleave_a64_s8q_3x3_dot::get_packed_size(const DepthwiseArgs &args)
42{
43 // We store 7 vectors for every <vector_of_ints> of channels.
44 const unsigned int n = arm_gemm::roundup(
45 arm_gemm::iceildiv((long unsigned int) args.input_channels,
46 get_vector_length<int32_t>(arm_gemm::VLType::None)), 4lu
47 );
48 return n * 7 * get_vector_length<int8_t>(arm_gemm::VLType::None);
49}
50
51void interleave_a64_s8q_3x3_dot::pack_parameters(unsigned int n_channels, void *outptr, const int32_t *bias, const int8_t *weights, const arm_gemm::Requantize32 &qp, size_t ld_weight_col, size_t ld_weight_row)
52{
53 __asm__ __volatile__(
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000054 "cmp %x[ld_weight_col], XZR\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000055 "csel %x[ld_weight_col], %x[ld_weight_col], %x[n_channels], NE\n"
56 "movi v16.4s, #0x9\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000057 "movi v0.16b, #0x0\n"
58 "mov x21, #0x3\n"
59 "mul x21, %x[ld_weight_col], x21\n"
60 "add x20, %x[qp], %[offsetof_input_offset]\n"
61 "ld1r { v31.4s }, [x20]\n"
62 "add x20, %x[qp], %[offsetof_weights_offset]\n"
63 "ld1r { v30.4s }, [x20]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000064 "cmp %x[ld_weight_row], XZR\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000065 "mul v30.4s, v30.4s, v31.4s\n"
66 "csel %x[ld_weight_row], %x[ld_weight_row], x21, NE\n"
67 "lsr x21, %x[n_channels], #0x2\n"
68 "movi v29.16b, #0x1\n"
69 "mul v30.4s, v30.4s, v16.4s\n"
70 "add x25, %x[weights], %x[ld_weight_row]\n"
71 "add x20, %x[qp], %[offsetof_per_layer_mul]\n"
72 "ld1r { v28.4s }, [x20]\n"
73 "add x20, %x[qp], %[offsetof_per_layer_right_shift]\n"
74 "ld1r { v27.4s }, [x20]\n"
75 "add x24, x25, %x[ld_weight_row]\n"
76 "add x23, %x[ld_weight_col], %x[ld_weight_col]\n"
77 "mov x22, #0x0\n"
78 "cbz x21, 4f\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000079 "1:" // Loop
80 "movi v26.4s, #0x0\n"
81 "cbz %x[bias], 2f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000082 "ldr q26, [%x[bias], x22]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000083 "2:" // Loop: Skip bias load
Michael Tyler7d9a6262023-02-01 16:37:07 +000084 "ldr s25, [%x[weights], #0x0]\n"
85 "ldr s22, [%x[weights], %x[ld_weight_col]]\n"
86 "zip1 v22.16b, v22.16b, v0.16b\n"
87 "movi v24.4s, #0x0\n"
88 "ldr s20, [%x[weights], x23]\n"
89 "ldr s23, [x25, #0x0]\n"
90 "zip1 v20.16b, v25.16b, v20.16b\n"
91 "zip1 v22.16b, v20.16b, v22.16b\n"
92 "ldr s21, [x25, %x[ld_weight_col]]\n"
93 "ldr s18, [x25, x23]\n"
94 "zip1 v20.16b, v23.16b, v18.16b\n"
95 "zip1 v18.16b, v21.16b, v0.16b\n"
96 "ldr s17, [x24, #0x0]\n"
97 "ldr s19, [x24, %x[ld_weight_col]]\n"
98 ".inst 0x4e9697b8 // sdot v24.4s, v29.16b, v22.16b\n"
99 "zip1 v18.16b, v20.16b, v18.16b\n"
100 "ldr s16, [x24, x23]\n"
101 "zip1 v17.16b, v17.16b, v16.16b\n"
102 "zip1 v16.16b, v19.16b, v0.16b\n"
103 ".inst 0x4e9297b8 // sdot v24.4s, v29.16b, v18.16b\n"
104 "zip1 v16.16b, v17.16b, v16.16b\n"
105 ".inst 0x4e9097b8 // sdot v24.4s, v29.16b, v16.16b\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000106 "add %x[weights], %x[weights], #0x4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000107 "add x25, x25, #0x4\n"
108 "mls v26.4s, v24.4s, v31.4s\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000109 "add x24, x24, #0x4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000110 "add v26.4s, v26.4s, v30.4s\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000111 "str q26, [%x[outptr], #0x0]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000112 "str q22, [%x[outptr], #0x10]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000113 "str q18, [%x[outptr], #0x20]\n"
114 "str q16, [%x[outptr], #0x30]\n"
115 "add %x[outptr], %x[outptr], #0x40\n"
116 "cbz %x[rq_mul_perchannel], 3f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000117 "ldr q28, [%x[rq_mul_perchannel], x22]\n"
118 "ldr q27, [%x[rq_shift_perchannel], x22]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000119 "3:" // Loop: Quantisation parameters: Store
Michael Tyler7d9a6262023-02-01 16:37:07 +0000120 "subs x21, x21, #0x1\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000121 "str q28, [%x[outptr], #0x0]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000122 "add x22, x22, #0x10\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000123 "str q27, [%x[outptr], #0x10]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000124 "add %x[outptr], %x[outptr], #0x20\n"
125 "bgt 1b\n"
126 "tst %x[n_channels], #0x3\n"
127 "beq 13f\n"
128 "4:" // Oddments
129 "movi v26.4s, #0x0\n"
130 "cbz %x[bias], 7f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000131 "add %x[bias], %x[bias], x22\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000132 "tbz %x[n_channels], #1, 5f\n"
133 "ld1 { v26.d }[0], [%x[bias]], #0x8\n"
134 "tbz %x[n_channels], #0, 6f\n"
135 "ld1 { v26.s }[2], [%x[bias]], #0x4\n"
136 "b 6f\n"
137 "5:" // Oddments: Load bias: Bit 1: Unset
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000138 "ld1 { v26.s }[0], [%x[bias]], #0x4\n"
139 "6:" // Oddments: Load bias: Bit 1: End
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000140 "7:" // Oddments: Skip bias load
141 "tbz %x[n_channels], #1, 8f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000142 "ld1 { v25.h }[0], [%x[weights]]\n"
143 "ld1 { v23.h }[0], [x25]\n"
144 "add x21, %x[weights], %x[ld_weight_col]\n"
145 "add x20, %x[weights], x23\n"
146 "ld1 { v22.h }[0], [x21]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000147 "ld1 { v20.h }[0], [x20]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000148 "add x21, x25, %x[ld_weight_col]\n"
149 "add x20, x25, x23\n"
150 "ld1 { v21.h }[0], [x21]\n"
151 "ld1 { v18.h }[0], [x20]\n"
152 "add x21, x24, %x[ld_weight_col]\n"
153 "add x20, x24, x23\n"
154 "ld1 { v17.h }[0], [x24]\n"
155 "ld1 { v19.h }[0], [x21]\n"
156 "add %x[weights], %x[weights], #0x2\n"
157 "add x25, x25, #0x2\n"
158 "ld1 { v16.h }[0], [x20]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000159 "add x24, x24, #0x2\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000160 "tbz %x[n_channels], #0, 9f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000161 "ld1 { v25.b }[2], [%x[weights]]\n"
162 "ld1 { v23.b }[2], [x25]\n"
163 "add x21, %x[weights], %x[ld_weight_col]\n"
164 "add x20, %x[weights], x23\n"
165 "ld1 { v22.b }[2], [x21]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000166 "ld1 { v20.b }[2], [x20]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000167 "add x21, x25, %x[ld_weight_col]\n"
168 "add x20, x25, x23\n"
169 "ld1 { v21.b }[2], [x21]\n"
170 "ld1 { v18.b }[2], [x20]\n"
171 "add x21, x24, %x[ld_weight_col]\n"
172 "add x20, x24, x23\n"
173 "ld1 { v17.b }[2], [x24]\n"
174 "ld1 { v19.b }[2], [x21]\n"
175 "add %x[weights], %x[weights], #0x1\n"
176 "ld1 { v16.b }[2], [x20]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000177 "b 9f\n"
178 "8:" // Oddments: Load weights: Bit 1: Unset
Michael Tyler7d9a6262023-02-01 16:37:07 +0000179 "ld1 { v25.b }[0], [%x[weights]]\n"
180 "ld1 { v23.b }[0], [x25]\n"
181 "add x21, %x[weights], %x[ld_weight_col]\n"
182 "add x20, %x[weights], x23\n"
183 "ld1 { v22.b }[0], [x21]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000184 "ld1 { v20.b }[0], [x20]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000185 "add x21, x25, %x[ld_weight_col]\n"
186 "add x20, x25, x23\n"
187 "ld1 { v21.b }[0], [x21]\n"
188 "ld1 { v18.b }[0], [x20]\n"
189 "add x21, x24, %x[ld_weight_col]\n"
190 "add x20, x24, x23\n"
191 "ld1 { v17.b }[0], [x24]\n"
192 "ld1 { v19.b }[0], [x21]\n"
193 "add %x[weights], %x[weights], #0x1\n"
194 "ld1 { v16.b }[0], [x20]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000195 "9:" // Oddments: Load weights: Bit 1: End
Michael Tyler7d9a6262023-02-01 16:37:07 +0000196 "zip1 v20.16b, v25.16b, v20.16b\n"
197 "zip1 v22.16b, v22.16b, v0.16b\n"
198 "zip1 v22.16b, v20.16b, v22.16b\n"
199 "zip1 v20.16b, v23.16b, v18.16b\n"
200 "zip1 v18.16b, v21.16b, v0.16b\n"
201 "movi v24.4s, #0x0\n"
202 ".inst 0x4e9697b8 // sdot v24.4s, v29.16b, v22.16b\n"
203 "zip1 v18.16b, v20.16b, v18.16b\n"
204 "zip1 v17.16b, v17.16b, v16.16b\n"
205 ".inst 0x4e9297b8 // sdot v24.4s, v29.16b, v18.16b\n"
206 "zip1 v16.16b, v19.16b, v0.16b\n"
207 "zip1 v16.16b, v17.16b, v16.16b\n"
208 ".inst 0x4e9097b8 // sdot v24.4s, v29.16b, v16.16b\n"
209 "mls v26.4s, v24.4s, v31.4s\n"
210 "add v26.4s, v26.4s, v30.4s\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000211 "str q26, [%x[outptr], #0x0]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000212 "str q22, [%x[outptr], #0x10]\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000213 "str q18, [%x[outptr], #0x20]\n"
214 "str q16, [%x[outptr], #0x30]\n"
215 "add %x[outptr], %x[outptr], #0x40\n"
216 "cbz %x[rq_mul_perchannel], 12f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000217 "add x21, %x[rq_mul_perchannel], x22\n"
218 "add x20, %x[rq_shift_perchannel], x22\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000219 "tbz %x[n_channels], #1, 10f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000220 "ld1 { v28.d }[0], [x21], #0x8\n"
221 "ld1 { v27.d }[0], [x20], #0x8\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000222 "tbz %x[n_channels], #0, 11f\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000223 "ld1 { v28.s }[2], [x21], #0x4\n"
224 "ld1 { v27.s }[2], [x20], #0x4\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000225 "b 11f\n"
226 "10:" // Oddments: Quantisation parameters: Load quant params: Bit 1: Unset
Michael Tyler7d9a6262023-02-01 16:37:07 +0000227 "ld1 { v28.s }[0], [x21], #0x4\n"
228 "ld1 { v27.s }[0], [x20], #0x4\n"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000229 "11:" // Oddments: Quantisation parameters: Load quant params: Bit 1: End
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000230 "12:" // Oddments: Quantisation parameters: Store
231 "str q28, [%x[outptr], #0x0]\n"
232 "str q27, [%x[outptr], #0x10]\n"
233 "add %x[outptr], %x[outptr], #0x20\n"
234 "13:" // End
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000235 : [bias] "+&r" (bias), [ld_weight_col] "+&r" (ld_weight_col), [ld_weight_row] "+&r" (ld_weight_row), [outptr] "+&r" (outptr), [weights] "+&r" (weights)
236 : [n_channels] "r" (n_channels), [offsetof_input_offset] "I" (offsetof(arm_gemm::Requantize32, a_offset)), [offsetof_per_layer_mul] "I" (offsetof(arm_gemm::Requantize32, per_layer_mul)), [offsetof_per_layer_right_shift] "I" (offsetof(arm_gemm::Requantize32, per_layer_right_shift)), [offsetof_weights_offset] "I" (offsetof(arm_gemm::Requantize32, b_offset)), [qp] "r" (&qp), [rq_mul_perchannel] "r" (qp.per_channel_muls), [rq_shift_perchannel] "r" (qp.per_channel_right_shifts)
Michael Tyler7d9a6262023-02-01 16:37:07 +0000237 : "cc", "memory", "v0", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "x20", "x21", "x22", "x23", "x24", "x25"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000238 );
239}
240
241} // namespace depthwise
242} // namespace arm_conv
243
244#endif // defined(__aarch64__)