| 1 |
// Auto-generated file. Do not edit! |
| 2 |
// Template: src/qs8-gemm/scalar.c.in |
| 3 |
// Generator: tools/xngen |
| 4 |
// |
| 5 |
// Copyright 2021 Google LLC |
| 6 |
// |
| 7 |
// This source code is licensed under the BSD-style license found in the |
| 8 |
// LICENSE file in the root directory of this source tree. |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
|
| 12 |
#include "xnnpack/gemm.h" |
| 13 |
#include "xnnpack/math.h" |
| 14 |
|
| 15 |
|
| 16 |
void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_4x4__scalar( |
| 17 |
size_t mr, |
| 18 |
size_t nc, |
| 19 |
size_t kc, |
| 20 |
const int8_t* restrict a, |
| 21 |
size_t a_stride, |
| 22 |
const void* restrict w, |
| 23 |
float* restrict c, |
| 24 |
size_t cm_stride, |
| 25 |
size_t cn_stride, |
| 26 |
const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)], |
| 27 |
const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) |
| 28 |
{ |
| 29 |
assert(mr != 0); |
| 30 |
assert(mr <= 4); |
| 31 |
assert(nc != 0); |
| 32 |
assert(kc != 0); |
| 33 |
|
| 34 |
const int8_t* a0 = a; |
| 35 |
float* c0 = c; |
| 36 |
const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride); |
| 37 |
float* c1 = (float*) ((uintptr_t) c0 + cm_stride); |
| 38 |
if XNN_UNPREDICTABLE(mr < 2) { |
| 39 |
a1 = a0; |
| 40 |
c1 = c0; |
| 41 |
} |
| 42 |
const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride); |
| 43 |
float* c2 = (float*) ((uintptr_t) c1 + cm_stride); |
| 44 |
if XNN_UNPREDICTABLE(mr <= 2) { |
| 45 |
a2 = a1; |
| 46 |
c2 = c1; |
| 47 |
} |
| 48 |
const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride); |
| 49 |
float* c3 = (float*) ((uintptr_t) c2 + cm_stride); |
| 50 |
if XNN_UNPREDICTABLE(mr != 4) { |
| 51 |
a3 = a2; |
| 52 |
c3 = c2; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
kc = round_up_po2(kc, 2); |
| 57 |
do { |
| 58 |
const int32_t vksum0 = ((const int32_t*) w)[0]; |
| 59 |
const int32_t vksum1 = ((const int32_t*) w)[1]; |
| 60 |
const int32_t vksum2 = ((const int32_t*) w)[2]; |
| 61 |
const int32_t vksum3 = ((const int32_t*) w)[3]; |
| 62 |
const int32_t vinput_zero_point0 = quantization_params[0].zero_point; |
| 63 |
int32_t vacc0x0 = vksum0 * vinput_zero_point0; |
| 64 |
int32_t vacc0x1 = vksum1 * vinput_zero_point0; |
| 65 |
int32_t vacc0x2 = vksum2 * vinput_zero_point0; |
| 66 |
int32_t vacc0x3 = vksum3 * vinput_zero_point0; |
| 67 |
const int32_t vinput_zero_point1 = quantization_params[1].zero_point; |
| 68 |
int32_t vacc1x0 = vksum0 * vinput_zero_point1; |
| 69 |
int32_t vacc1x1 = vksum1 * vinput_zero_point1; |
| 70 |
int32_t vacc1x2 = vksum2 * vinput_zero_point1; |
| 71 |
int32_t vacc1x3 = vksum3 * vinput_zero_point1; |
| 72 |
const int32_t vinput_zero_point2 = quantization_params[2].zero_point; |
| 73 |
int32_t vacc2x0 = vksum0 * vinput_zero_point2; |
| 74 |
int32_t vacc2x1 = vksum1 * vinput_zero_point2; |
| 75 |
int32_t vacc2x2 = vksum2 * vinput_zero_point2; |
| 76 |
int32_t vacc2x3 = vksum3 * vinput_zero_point2; |
| 77 |
const int32_t vinput_zero_point3 = quantization_params[3].zero_point; |
| 78 |
int32_t vacc3x0 = vksum0 * vinput_zero_point3; |
| 79 |
int32_t vacc3x1 = vksum1 * vinput_zero_point3; |
| 80 |
int32_t vacc3x2 = vksum2 * vinput_zero_point3; |
| 81 |
int32_t vacc3x3 = vksum3 * vinput_zero_point3; |
| 82 |
w = (const int32_t*) w + 4; |
| 83 |
|
| 84 |
size_t k = kc; |
| 85 |
for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) { |
| 86 |
const int32_t va0c0 = (int32_t) a0[0]; |
| 87 |
const int32_t va0c1 = (int32_t) a0[1]; |
| 88 |
a0 += 2; |
| 89 |
const int32_t va1c0 = (int32_t) a1[0]; |
| 90 |
const int32_t va1c1 = (int32_t) a1[1]; |
| 91 |
a1 += 2; |
| 92 |
const int32_t va2c0 = (int32_t) a2[0]; |
| 93 |
const int32_t va2c1 = (int32_t) a2[1]; |
| 94 |
a2 += 2; |
| 95 |
const int32_t va3c0 = (int32_t) a3[0]; |
| 96 |
const int32_t va3c1 = (int32_t) a3[1]; |
| 97 |
a3 += 2; |
| 98 |
|
| 99 |
const uint8_t vbi0 = ((const uint8_t*) w)[0]; |
| 100 |
const uint8_t vbi1 = ((const uint8_t*) w)[1]; |
| 101 |
const uint8_t vbi2 = ((const uint8_t*) w)[2]; |
| 102 |
const uint8_t vbi3 = ((const uint8_t*) w)[3]; |
| 103 |
w = (const uint8_t*) w + 4; |
| 104 |
const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4); |
| 105 |
const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0); |
| 106 |
const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4); |
| 107 |
const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0); |
| 108 |
const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4); |
| 109 |
const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0); |
| 110 |
const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4); |
| 111 |
const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0); |
| 112 |
|
| 113 |
vacc0x0 += va0c0 * vb0c0; |
| 114 |
vacc0x1 += va0c0 * vb1c0; |
| 115 |
vacc0x2 += va0c0 * vb2c0; |
| 116 |
vacc0x3 += va0c0 * vb3c0; |
| 117 |
vacc1x0 += va1c0 * vb0c0; |
| 118 |
vacc1x1 += va1c0 * vb1c0; |
| 119 |
vacc1x2 += va1c0 * vb2c0; |
| 120 |
vacc1x3 += va1c0 * vb3c0; |
| 121 |
vacc2x0 += va2c0 * vb0c0; |
| 122 |
vacc2x1 += va2c0 * vb1c0; |
| 123 |
vacc2x2 += va2c0 * vb2c0; |
| 124 |
vacc2x3 += va2c0 * vb3c0; |
| 125 |
vacc3x0 += va3c0 * vb0c0; |
| 126 |
vacc3x1 += va3c0 * vb1c0; |
| 127 |
vacc3x2 += va3c0 * vb2c0; |
| 128 |
vacc3x3 += va3c0 * vb3c0; |
| 129 |
vacc0x0 += va0c1 * vb0c1; |
| 130 |
vacc0x1 += va0c1 * vb1c1; |
| 131 |
vacc0x2 += va0c1 * vb2c1; |
| 132 |
vacc0x3 += va0c1 * vb3c1; |
| 133 |
vacc1x0 += va1c1 * vb0c1; |
| 134 |
vacc1x1 += va1c1 * vb1c1; |
| 135 |
vacc1x2 += va1c1 * vb2c1; |
| 136 |
vacc1x3 += va1c1 * vb3c1; |
| 137 |
vacc2x0 += va2c1 * vb0c1; |
| 138 |
vacc2x1 += va2c1 * vb1c1; |
| 139 |
vacc2x2 += va2c1 * vb2c1; |
| 140 |
vacc2x3 += va2c1 * vb3c1; |
| 141 |
vacc3x0 += va3c1 * vb0c1; |
| 142 |
vacc3x1 += va3c1 * vb1c1; |
| 143 |
vacc3x2 += va3c1 * vb2c1; |
| 144 |
vacc3x3 += va3c1 * vb3c1; |
| 145 |
} |
| 146 |
|
| 147 |
float vout0x0 = (float) math_asr_s32(vacc0x0, 4); |
| 148 |
float vout0x1 = (float) math_asr_s32(vacc0x1, 4); |
| 149 |
float vout0x2 = (float) math_asr_s32(vacc0x2, 4); |
| 150 |
float vout0x3 = (float) math_asr_s32(vacc0x3, 4); |
| 151 |
float vout1x0 = (float) math_asr_s32(vacc1x0, 4); |
| 152 |
float vout1x1 = (float) math_asr_s32(vacc1x1, 4); |
| 153 |
float vout1x2 = (float) math_asr_s32(vacc1x2, 4); |
| 154 |
float vout1x3 = (float) math_asr_s32(vacc1x3, 4); |
| 155 |
float vout2x0 = (float) math_asr_s32(vacc2x0, 4); |
| 156 |
float vout2x1 = (float) math_asr_s32(vacc2x1, 4); |
| 157 |
float vout2x2 = (float) math_asr_s32(vacc2x2, 4); |
| 158 |
float vout2x3 = (float) math_asr_s32(vacc2x3, 4); |
| 159 |
float vout3x0 = (float) math_asr_s32(vacc3x0, 4); |
| 160 |
float vout3x1 = (float) math_asr_s32(vacc3x1, 4); |
| 161 |
float vout3x2 = (float) math_asr_s32(vacc3x2, 4); |
| 162 |
float vout3x3 = (float) math_asr_s32(vacc3x3, 4); |
| 163 |
|
| 164 |
const float vinput_scale0 = quantization_params[0].inv_scale; |
| 165 |
vout0x0 *= vinput_scale0; |
| 166 |
vout0x1 *= vinput_scale0; |
| 167 |
vout0x2 *= vinput_scale0; |
| 168 |
vout0x3 *= vinput_scale0; |
| 169 |
const float vinput_scale1 = quantization_params[1].inv_scale; |
| 170 |
vout1x0 *= vinput_scale1; |
| 171 |
vout1x1 *= vinput_scale1; |
| 172 |
vout1x2 *= vinput_scale1; |
| 173 |
vout1x3 *= vinput_scale1; |
| 174 |
const float vinput_scale2 = quantization_params[2].inv_scale; |
| 175 |
vout2x0 *= vinput_scale2; |
| 176 |
vout2x1 *= vinput_scale2; |
| 177 |
vout2x2 *= vinput_scale2; |
| 178 |
vout2x3 *= vinput_scale2; |
| 179 |
const float vinput_scale3 = quantization_params[3].inv_scale; |
| 180 |
vout3x0 *= vinput_scale3; |
| 181 |
vout3x1 *= vinput_scale3; |
| 182 |
vout3x2 *= vinput_scale3; |
| 183 |
vout3x3 *= vinput_scale3; |
| 184 |
|
| 185 |
const float vfilter_output_scale0 = ((const float*) w)[0]; |
| 186 |
vout0x0 *= vfilter_output_scale0; |
| 187 |
vout1x0 *= vfilter_output_scale0; |
| 188 |
vout2x0 *= vfilter_output_scale0; |
| 189 |
vout3x0 *= vfilter_output_scale0; |
| 190 |
const float vfilter_output_scale1 = ((const float*) w)[1]; |
| 191 |
vout0x1 *= vfilter_output_scale1; |
| 192 |
vout1x1 *= vfilter_output_scale1; |
| 193 |
vout2x1 *= vfilter_output_scale1; |
| 194 |
vout3x1 *= vfilter_output_scale1; |
| 195 |
const float vfilter_output_scale2 = ((const float*) w)[2]; |
| 196 |
vout0x2 *= vfilter_output_scale2; |
| 197 |
vout1x2 *= vfilter_output_scale2; |
| 198 |
vout2x2 *= vfilter_output_scale2; |
| 199 |
vout3x2 *= vfilter_output_scale2; |
| 200 |
const float vfilter_output_scale3 = ((const float*) w)[3]; |
| 201 |
vout0x3 *= vfilter_output_scale3; |
| 202 |
vout1x3 *= vfilter_output_scale3; |
| 203 |
vout2x3 *= vfilter_output_scale3; |
| 204 |
vout3x3 *= vfilter_output_scale3; |
| 205 |
|
| 206 |
const float vbias0 = ((const float*) w)[4]; |
| 207 |
vout0x0 += vbias0; |
| 208 |
vout1x0 += vbias0; |
| 209 |
vout2x0 += vbias0; |
| 210 |
vout3x0 += vbias0; |
| 211 |
const float vbias1 = ((const float*) w)[5]; |
| 212 |
vout0x1 += vbias1; |
| 213 |
vout1x1 += vbias1; |
| 214 |
vout2x1 += vbias1; |
| 215 |
vout3x1 += vbias1; |
| 216 |
const float vbias2 = ((const float*) w)[6]; |
| 217 |
vout0x2 += vbias2; |
| 218 |
vout1x2 += vbias2; |
| 219 |
vout2x2 += vbias2; |
| 220 |
vout3x2 += vbias2; |
| 221 |
const float vbias3 = ((const float*) w)[7]; |
| 222 |
vout0x3 += vbias3; |
| 223 |
vout1x3 += vbias3; |
| 224 |
vout2x3 += vbias3; |
| 225 |
vout3x3 += vbias3; |
| 226 |
|
| 227 |
w = (const float*) w + 8; |
| 228 |
|
| 229 |
const float voutput_min = params->scalar.min; |
| 230 |
vout0x0 = math_max_f32(vout0x0, voutput_min); |
| 231 |
vout1x0 = math_max_f32(vout1x0, voutput_min); |
| 232 |
vout2x0 = math_max_f32(vout2x0, voutput_min); |
| 233 |
vout3x0 = math_max_f32(vout3x0, voutput_min); |
| 234 |
vout0x1 = math_max_f32(vout0x1, voutput_min); |
| 235 |
vout1x1 = math_max_f32(vout1x1, voutput_min); |
| 236 |
vout2x1 = math_max_f32(vout2x1, voutput_min); |
| 237 |
vout3x1 = math_max_f32(vout3x1, voutput_min); |
| 238 |
vout0x2 = math_max_f32(vout0x2, voutput_min); |
| 239 |
vout1x2 = math_max_f32(vout1x2, voutput_min); |
| 240 |
vout2x2 = math_max_f32(vout2x2, voutput_min); |
| 241 |
vout3x2 = math_max_f32(vout3x2, voutput_min); |
| 242 |
vout0x3 = math_max_f32(vout0x3, voutput_min); |
| 243 |
vout1x3 = math_max_f32(vout1x3, voutput_min); |
| 244 |
vout2x3 = math_max_f32(vout2x3, voutput_min); |
| 245 |
vout3x3 = math_max_f32(vout3x3, voutput_min); |
| 246 |
|
| 247 |
const float voutput_max = params->scalar.max; |
| 248 |
vout0x0 = math_min_f32(vout0x0, voutput_max); |
| 249 |
vout1x0 = math_min_f32(vout1x0, voutput_max); |
| 250 |
vout2x0 = math_min_f32(vout2x0, voutput_max); |
| 251 |
vout3x0 = math_min_f32(vout3x0, voutput_max); |
| 252 |
vout0x1 = math_min_f32(vout0x1, voutput_max); |
| 253 |
vout1x1 = math_min_f32(vout1x1, voutput_max); |
| 254 |
vout2x1 = math_min_f32(vout2x1, voutput_max); |
| 255 |
vout3x1 = math_min_f32(vout3x1, voutput_max); |
| 256 |
vout0x2 = math_min_f32(vout0x2, voutput_max); |
| 257 |
vout1x2 = math_min_f32(vout1x2, voutput_max); |
| 258 |
vout2x2 = math_min_f32(vout2x2, voutput_max); |
| 259 |
vout3x2 = math_min_f32(vout3x2, voutput_max); |
| 260 |
vout0x3 = math_min_f32(vout0x3, voutput_max); |
| 261 |
vout1x3 = math_min_f32(vout1x3, voutput_max); |
| 262 |
vout2x3 = math_min_f32(vout2x3, voutput_max); |
| 263 |
vout3x3 = math_min_f32(vout3x3, voutput_max); |
| 264 |
|
| 265 |
if XNN_LIKELY(nc >= 4) { |
| 266 |
c0[0] = vout0x0; |
| 267 |
c0[1] = vout0x1; |
| 268 |
c0[2] = vout0x2; |
| 269 |
c0[3] = vout0x3; |
| 270 |
c1[0] = vout1x0; |
| 271 |
c1[1] = vout1x1; |
| 272 |
c1[2] = vout1x2; |
| 273 |
c1[3] = vout1x3; |
| 274 |
c2[0] = vout2x0; |
| 275 |
c2[1] = vout2x1; |
| 276 |
c2[2] = vout2x2; |
| 277 |
c2[3] = vout2x3; |
| 278 |
c3[0] = vout3x0; |
| 279 |
c3[1] = vout3x1; |
| 280 |
c3[2] = vout3x2; |
| 281 |
c3[3] = vout3x3; |
| 282 |
|
| 283 |
a0 = (const int8_t*) ((uintptr_t) a0 - kc); |
| 284 |
a1 = (const int8_t*) ((uintptr_t) a1 - kc); |
| 285 |
a2 = (const int8_t*) ((uintptr_t) a2 - kc); |
| 286 |
a3 = (const int8_t*) ((uintptr_t) a3 - kc); |
| 287 |
|
| 288 |
c0 = (float*) ((uintptr_t) c0 + cn_stride); |
| 289 |
c1 = (float*) ((uintptr_t) c1 + cn_stride); |
| 290 |
c2 = (float*) ((uintptr_t) c2 + cn_stride); |
| 291 |
c3 = (float*) ((uintptr_t) c3 + cn_stride); |
| 292 |
|
| 293 |
nc -= 4; |
| 294 |
} else { |
| 295 |
if (nc & 2) { |
| 296 |
c0[0] = vout0x0; |
| 297 |
c0[1] = vout0x1; |
| 298 |
vout0x0 = vout0x2; |
| 299 |
c0 += 2; |
| 300 |
c1[0] = vout1x0; |
| 301 |
c1[1] = vout1x1; |
| 302 |
vout1x0 = vout1x2; |
| 303 |
c1 += 2; |
| 304 |
c2[0] = vout2x0; |
| 305 |
c2[1] = vout2x1; |
| 306 |
vout2x0 = vout2x2; |
| 307 |
c2 += 2; |
| 308 |
c3[0] = vout3x0; |
| 309 |
c3[1] = vout3x1; |
| 310 |
vout3x0 = vout3x2; |
| 311 |
c3 += 2; |
| 312 |
} |
| 313 |
if (nc & 1) { |
| 314 |
c0[0] = vout0x0; |
| 315 |
c1[0] = vout1x0; |
| 316 |
c2[0] = vout2x0; |
| 317 |
c3[0] = vout3x0; |
| 318 |
} |
| 319 |
|
| 320 |
nc = 0; |
| 321 |
} |
| 322 |
} while (nc != 0); |
| 323 |
} |