Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-1x4-minmax-scalar.c

raw
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_qb4w_gemm_minmax_ukernel_1x4__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_qb4w_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 <= 1);
31 assert(nc != 0);
32 assert(kc != 0);
33 size_t bl = params->scalar.blocksize;
34 assert(bl <= round_up_po2(kc, 2));
35 assert(bl != 0);
36 assert(bl % 32 == 0);
37
38 const int8_t* a0 = a;
39 float* c0 = c;
40
41
42 kc = round_up_po2(kc, 2);
43 do {
44 const float vksum0 = ((const float*) w)[0];
45 const float vksum1 = ((const float*) w)[1];
46 const float vksum2 = ((const float*) w)[2];
47 const float vksum3 = ((const float*) w)[3];
48 const float vinput_zero_point0 = (const float) quantization_params[0].zero_point;
49 float vout0x0 = vksum0 * vinput_zero_point0;
50 float vout0x1 = vksum1 * vinput_zero_point0;
51 float vout0x2 = vksum2 * vinput_zero_point0;
52 float vout0x3 = vksum3 * vinput_zero_point0;
53 w = (const float*) w + 4;
54
55 for (size_t kb=0; kb < kc; kb += bl) {
56 int32_t vacc0x0 = 0;
57 int32_t vacc0x1 = 0;
58 int32_t vacc0x2 = 0;
59 int32_t vacc0x3 = 0;
60 size_t k = bl;
61 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
62 const int32_t va0c0 = (int32_t) a0[0];
63 const int32_t va0c1 = (int32_t) a0[1];
64 a0 += 2;
65
66 const uint8_t vbi0 = ((const uint8_t*) w)[0];
67 const uint8_t vbi1 = ((const uint8_t*) w)[1];
68 const uint8_t vbi2 = ((const uint8_t*) w)[2];
69 const uint8_t vbi3 = ((const uint8_t*) w)[3];
70 w = (const uint8_t*) w + 4;
71 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
72 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
73 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
74 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
75 const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4);
76 const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0);
77 const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4);
78 const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0);
79
80 vacc0x0 += va0c0 * vb0c0;
81 vacc0x1 += va0c0 * vb1c0;
82 vacc0x2 += va0c0 * vb2c0;
83 vacc0x3 += va0c0 * vb3c0;
84 vacc0x0 += va0c1 * vb0c1;
85 vacc0x1 += va0c1 * vb1c1;
86 vacc0x2 += va0c1 * vb2c1;
87 vacc0x3 += va0c1 * vb3c1;
88 }
89 // accumulate in float
90 float vf0x0 = vacc0x0;
91 const float vfilter_output_scale0 = math_cvt_fp32_bf16(((const uint16_t*) w)[0]);
92 float vf0x1 = vacc0x1;
93 const float vfilter_output_scale1 = math_cvt_fp32_bf16(((const uint16_t*) w)[1]);
94 float vf0x2 = vacc0x2;
95 const float vfilter_output_scale2 = math_cvt_fp32_bf16(((const uint16_t*) w)[2]);
96 float vf0x3 = vacc0x3;
97 const float vfilter_output_scale3 = math_cvt_fp32_bf16(((const uint16_t*) w)[3]);
98
99 vf0x0 *= vfilter_output_scale0;
100 vout0x0 += vf0x0;
101 vf0x1 *= vfilter_output_scale1;
102 vout0x1 += vf0x1;
103 vf0x2 *= vfilter_output_scale2;
104 vout0x2 += vf0x2;
105 vf0x3 *= vfilter_output_scale3;
106 vout0x3 += vf0x3;
107 w = (const uint16_t*) w + 4;
108 }
109
110
111 const float vinput_scale0 = quantization_params[0].inv_scale;
112 vout0x0 *= vinput_scale0;
113 vout0x1 *= vinput_scale0;
114 vout0x2 *= vinput_scale0;
115 vout0x3 *= vinput_scale0;
116
117
118 const float vbias0 = ((const float*) w)[0];
119 vout0x0 += vbias0;
120 const float vbias1 = ((const float*) w)[1];
121 vout0x1 += vbias1;
122 const float vbias2 = ((const float*) w)[2];
123 vout0x2 += vbias2;
124 const float vbias3 = ((const float*) w)[3];
125 vout0x3 += vbias3;
126
127 w = (const float*) w + 4;
128
129 const float voutput_min = params->scalar.min;
130 vout0x0 = math_max_f32(vout0x0, voutput_min);
131 vout0x1 = math_max_f32(vout0x1, voutput_min);
132 vout0x2 = math_max_f32(vout0x2, voutput_min);
133 vout0x3 = math_max_f32(vout0x3, voutput_min);
134
135 const float voutput_max = params->scalar.max;
136 vout0x0 = math_min_f32(vout0x0, voutput_max);
137 vout0x1 = math_min_f32(vout0x1, voutput_max);
138 vout0x2 = math_min_f32(vout0x2, voutput_max);
139 vout0x3 = math_min_f32(vout0x3, voutput_max);
140
141 if XNN_LIKELY(nc >= 4) {
142 c0[0] = vout0x0;
143 c0[1] = vout0x1;
144 c0[2] = vout0x2;
145 c0[3] = vout0x3;
146
147 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
148
149 c0 = (float*) ((uintptr_t) c0 + cn_stride);
150
151 nc -= 4;
152 } else {
153 if (nc & 2) {
154 c0[0] = vout0x0;
155 c0[1] = vout0x1;
156 vout0x0 = vout0x2;
157 c0 += 2;
158 }
159 if (nc & 1) {
160 c0[0] = vout0x0;
161 }
162
163 nc = 0;
164 }
165 } while (nc != 0);
166 }