forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscalar.c.in
253 lines (218 loc) · 9.46 KB
/
scalar.c.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2021 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
$assert REQUANTIZATION in ["FP32", "GEMMLOWP", "RNDNU"]
$assert DATATYPE in ["QC8", "QS8", "QU8"]
$assert DATATYPE != "QC8" or REQUANTIZATION == "FP32"
#include <assert.h>
$if REQUANTIZATION == "FP32":
$if VARIANT == "LRINT":
#include <math.h>
$elif VARIANT == "MAGIC":
#include <fp16.h>
#include <xnnpack/math.h>
#include <xnnpack/gemm.h>
$FUNCTION_SUFFIX = "scalar_" + VARIANT.lower() if VARIANT else "scalar"
$PARAMS_UNION = "xnn_qs8_minmax_params" if DATATYPE == "QC8" else "xnn_%s_conv_minmax_params" % DATATYPE.lower()
$PARAMS_STRUCT = ("" if DATATYPE == "QC8" else REQUANTIZATION.lower() + "_") + "scalar" + ("_" + VARIANT.lower() if VARIANT else "")
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
void xnn_${DATATYPE.lower()}_igemm_minmax_${REQUANTIZATION.lower()}_ukernel_${MR}x${NR}__${FUNCTION_SUFFIX}(
size_t mr,
size_t nc,
size_t kc,
size_t ks,
const ${XINT8_T}**restrict a,
const void*restrict w,
${XINT8_T}*restrict c,
size_t cm_stride,
size_t cn_stride,
size_t a_offset,
const ${XINT8_T}* zero,
const union ${PARAMS_UNION} params[restrict XNN_MIN_ELEMENTS(1)])
{
assert(mr != 0);
assert(mr <= ${MR});
assert(nc != 0);
assert(kc != 0);
assert(ks != 0);
assert(ks % (${MR} * sizeof(void*)) == 0);
assert(a != NULL);
assert(w != NULL);
assert(c != NULL);
${XINT8_T}* c0 = c;
$for M in range(1, MR):
${XINT8_T}* c${M} = (${XINT8_T}*) ((uintptr_t) c${M-1} + cm_stride);
$if M % 2 == 0:
if XNN_UNPREDICTABLE(mr <= ${M}) {
c${M} = c${M-1};
}
$elif M + 1 == MR:
if XNN_UNPREDICTABLE(mr != ${M+1}) {
c${M} = c${M-1};
}
$else:
if XNN_UNPREDICTABLE(mr < ${M+1}) {
c${M} = c${M-1};
}
$if DATATYPE == "QU8":
const int32_t vb_zero_point = params->${PARAMS_STRUCT}.kernel_zero_point;
do {
$for N in range(NR):
int32_t vacc0x${N} = ((const int32_t*) w)[${N}];
$for M in range(1, MR):
$for N in range(NR):
int32_t vacc${M}x${N} = vacc0x${N};
w = (const void*) ((const int32_t*) w + ${NR});
size_t p = ks;
do {
$for M in range(MR):
const ${XINT8_T}* restrict a${M} = a[${M}];
assert(a${M} != NULL);
if XNN_UNPREDICTABLE(a${M} != zero) {
a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} + a_offset);
}
a += ${MR};
size_t k = kc;
do {
$for M in range(MR):
$if DATATYPE == "QU8":
const int32_t va${M} = (int32_t) (uint32_t) *a${M}++;
$else:
const int32_t va${M} = (int32_t) *a${M}++;
$for N in range(NR):
$if DATATYPE == "QU8":
const int32_t vb${N} = (int32_t) (uint32_t) ((const uint8_t*) w)[${N}] - vb_zero_point;
$else:
const int32_t vb${N} = (int32_t) ((const int8_t*) w)[${N}];
w = (const void*) ((const ${XINT8_T}*) w + ${NR});
$for M in range(MR):
$for N in range(NR):
vacc${M}x${N} += va${M} * vb${N};
k -= sizeof(${XINT8_T});
} while (k != 0);
p -= ${MR} * sizeof(void*);
} while (p != 0);
$if REQUANTIZATION == "GEMMLOWP":
const int32_t vmultiplier = params->${PARAMS_STRUCT}.multiplier;
$for M in range(MR):
$for N in range(NR):
const int64_t vproduct${M}x${N} = (int64_t) vacc${M}x${N} * (int64_t) vmultiplier;
const int64_t vq31rounding = INT64_C(0x40000000);
$for M in range(MR):
$for N in range(NR):
const int32_t vq31product${M}x${N} = (int32_t) (uint32_t) ((uint64_t) (vproduct${M}x${N} + vq31rounding) >> 31);
const int32_t vremainder_mask = params->${PARAMS_STRUCT}.remainder_mask;
$for M in range(MR):
$for N in range(NR):
const int32_t vremainder${M}x${N} = (vq31product${M}x${N} & vremainder_mask) - (int32_t) (vq31product${M}x${N} < 0);
const uint32_t vshift = params->${PARAMS_STRUCT}.shift;
const int32_t vremainder_threshold = params->${PARAMS_STRUCT}.remainder_threshold;
$for M in range(MR):
$for N in range(NR):
int32_t vout${M}x${N} = asr_s32(vq31product${M}x${N}, vshift) + (int32_t) (vremainder${M}x${N} > vremainder_threshold);
const int32_t voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} = math_max_s32(vout${M}x${N}, voutput_min_less_zero_point);
const int32_t voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} = math_min_s32(vout${M}x${N}, voutput_max_less_zero_point);
const int32_t voutput_zero_point = params->${PARAMS_STRUCT}.output_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} += voutput_zero_point;
$elif REQUANTIZATION == "RNDNU":
const int32_t vmultiplier = params->${PARAMS_STRUCT}.multiplier;
$for M in range(MR):
$for N in range(NR):
const int64_t vproduct${M}x${N} = (int64_t) vacc${M}x${N} * (int64_t) vmultiplier;
const uint32_t shift = params->${PARAMS_STRUCT}.shift;
const int64_t rounding = params->${PARAMS_STRUCT}.rounding;
$for M in range(MR):
$for N in range(NR):
int32_t vout${M}x${N} = (int32_t) asr_s64(vproduct${M}x${N} + rounding, shift);
const int32_t voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} = math_max_s32(vout${M}x${N}, voutput_min_less_zero_point);
const int32_t voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} = math_min_s32(vout${M}x${N}, voutput_max_less_zero_point);
const int32_t voutput_zero_point = params->${PARAMS_STRUCT}.output_zero_point;
$for M in range(MR):
$for N in range(NR):
vout${M}x${N} += voutput_zero_point;
$elif REQUANTIZATION == "FP32":
$for M in range(MR):
$for N in range(NR):
float vfpacc${M}x${N} = (float) vacc${M}x${N};
$if DATATYPE == "QC8":
$for N in range(NR):
const float vscale${N} = ((const float*) w)[${N}];
$for M in range(MR):
vfpacc${M}x${N} *= vscale${N};
w = (const void*) ((const float*) w + ${NR});
$else:
const float vscale = params->${PARAMS_STRUCT}.scale;
$for M in range(MR):
$for N in range(NR):
vfpacc${M}x${N} *= vscale;
$if VARIANT == "MAGIC":
const float voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vfpacc${M}x${N} = math_max_f32(vfpacc${M}x${N}, voutput_min_less_zero_point);
const float voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vfpacc${M}x${N} = math_min_f32(vfpacc${M}x${N}, voutput_max_less_zero_point);
const float vmagic_bias = params->${PARAMS_STRUCT}.magic_bias;
$for M in range(MR):
$for N in range(NR):
vfpacc${M}x${N} += vmagic_bias;
const int32_t vmagic_bias_less_output_zero_point = params->${PARAMS_STRUCT}.magic_bias_less_output_zero_point;
$for M in range(MR):
$for N in range(NR):
int32_t vout${M}x${N} = (int32_t) fp32_to_bits(vfpacc${M}x${N}) - vmagic_bias_less_output_zero_point;
$elif VARIANT == "LRINT":
$for M in range(MR):
$for N in range(NR):
long vrndacc${M}x${N} = lrintf(vfpacc${M}x${N});
const long voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vrndacc${M}x${N} = XNN_UNPREDICTABLE(vrndacc${M}x${N} < voutput_min_less_zero_point) ? voutput_min_less_zero_point : vrndacc${M}x${N};
const long voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point;
$for M in range(MR):
$for N in range(NR):
vrndacc${M}x${N} = XNN_UNPREDICTABLE(vrndacc${M}x${N} > voutput_max_less_zero_point) ? voutput_max_less_zero_point : vrndacc${M}x${N};
const int32_t voutput_zero_point = params->${PARAMS_STRUCT}.output_zero_point;
$for M in range(MR):
$for N in range(NR):
int32_t vout${M}x${N} = (int32_t) vrndacc${M}x${N} + voutput_zero_point;
if XNN_LIKELY(nc >= ${NR}) {
$for M in reversed(range(MR)):
$for N in range(NR):
c${M}[${N}] = (${XINT8_T}) vout${M}x${N};
$for M in reversed(range(MR)):
c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride);
a = (const ${XINT8_T}**restrict) ((uintptr_t) a - ks);
nc -= ${NR};
} else {
$for LOG2N in reversed(range(NR.bit_length() - 1)):
if (nc & ${1 << LOG2N}) {
$for M in reversed(range(MR)):
$for N in range(1 << LOG2N):
c${M}[${N}] = (${XINT8_T}) vout${M}x${N};
$if LOG2N != 0:
$for N in range(1 << (LOG2N - 1)):
vout${M}x${N} = vout${M}x${N + (1 << LOG2N)};
c${M} += ${1 << LOG2N};
}
nc = 0;
}
} while (nc != 0);
}