Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-8x4v-minmax-rvv.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/rvv.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 SiFive, Inc.
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 <riscv_vector.h>
13
14 #include "xnnpack/gemm.h"
15 #include "xnnpack/math.h"
16
17 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_8x4v__rvv(
18 size_t mr,
19 size_t nc,
20 size_t kc,
21 const int8_t* restrict a,
22 size_t a_stride,
23 const void* restrict w,
24 float* restrict c,
25 size_t cm_stride,
26 size_t cn_stride,
27 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
28 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30 assert(mr != 0);
31 assert(mr <= 8);
32 assert(nc != 0);
33 assert(kc != 0);
34
35 const int8_t* a0 = a;
36 float* c0 = c;
37 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
38 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
39 if XNN_UNPREDICTABLE(mr < 2) {
40 a1 = a0;
41 c1 = c0;
42 }
43 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
44 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
45 if XNN_UNPREDICTABLE(mr <= 2) {
46 a2 = a1;
47 c2 = c1;
48 }
49 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
50 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
51 if XNN_UNPREDICTABLE(mr < 4) {
52 a3 = a2;
53 c3 = c2;
54 }
55 const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
56 float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
57 if XNN_UNPREDICTABLE(mr <= 4) {
58 a4 = a3;
59 c4 = c3;
60 }
61 const int8_t* a5 = (const int8_t*) ((uintptr_t) a4 + a_stride);
62 float* c5 = (float*) ((uintptr_t) c4 + cm_stride);
63 if XNN_UNPREDICTABLE(mr < 6) {
64 a5 = a4;
65 c5 = c4;
66 }
67 const int8_t* a6 = (const int8_t*) ((uintptr_t) a5 + a_stride);
68 float* c6 = (float*) ((uintptr_t) c5 + cm_stride);
69 if XNN_UNPREDICTABLE(mr <= 6) {
70 a6 = a5;
71 c6 = c5;
72 }
73 const int8_t* a7 = (const int8_t*) ((uintptr_t) a6 + a_stride);
74 float* c7 = (float*) ((uintptr_t) c6 + cm_stride);
75 if XNN_UNPREDICTABLE(mr != 8) {
76 a7 = a6;
77 c7 = c6;
78 }
79
80 const size_t nr = __riscv_vsetvlmax_e32m4();
81 size_t vl = nr;
82 kc = round_up_po2(kc, 2);
83 do {
84 if XNN_UNLIKELY(nc < nr) {
85 vl = __riscv_vsetvl_e32m4(nc);
86 }
87 nc = nc - vl;
88
89 vint32m4_t vksum = __riscv_vle32_v_i32m4((const int32_t*)w, vl);
90 w = (const int32_t*) w + nr;
91 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
92 const int32_t vinput_zero_point1 = quantization_params[1].zero_point;
93 const int32_t vinput_zero_point2 = quantization_params[2].zero_point;
94 const int32_t vinput_zero_point3 = quantization_params[3].zero_point;
95 const int32_t vinput_zero_point4 = quantization_params[4].zero_point;
96 const int32_t vinput_zero_point5 = quantization_params[5].zero_point;
97 const int32_t vinput_zero_point6 = quantization_params[6].zero_point;
98 const int32_t vinput_zero_point7 = quantization_params[7].zero_point;
99 vint32m4_t vacc0 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point0, vl);
100 vint32m4_t vacc1 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point1, vl);
101 vint32m4_t vacc2 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point2, vl);
102 vint32m4_t vacc3 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point3, vl);
103 vint32m4_t vacc4 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point4, vl);
104 vint32m4_t vacc5 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point5, vl);
105 vint32m4_t vacc6 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point6, vl);
106 vint32m4_t vacc7 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point7, vl);
107
108 size_t k = kc;
109 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
110 const int8_t va0c0 = a0[0];
111 const int8_t va0c1 = a0[1];
112 a0 += 2;
113 const int8_t va1c0 = a1[0];
114 const int8_t va1c1 = a1[1];
115 a1 += 2;
116 const int8_t va2c0 = a2[0];
117 const int8_t va2c1 = a2[1];
118 a2 += 2;
119 const int8_t va3c0 = a3[0];
120 const int8_t va3c1 = a3[1];
121 a3 += 2;
122 const int8_t va4c0 = a4[0];
123 const int8_t va4c1 = a4[1];
124 a4 += 2;
125 const int8_t va5c0 = a5[0];
126 const int8_t va5c1 = a5[1];
127 a5 += 2;
128 const int8_t va6c0 = a6[0];
129 const int8_t va6c1 = a6[1];
130 a6 += 2;
131 const int8_t va7c0 = a7[0];
132 const int8_t va7c1 = a7[1];
133 a7 += 2;
134 const vint8m1_t vbi = __riscv_vle8_v_i8m1((const int8_t*) w, vl);
135 w = (const uint8_t*) w + nr;
136 const vint8m1_t vbc0 = __riscv_vsll_vx_i8m1(vbi, 4, vl);
137 const vint8m1_t vbc1 = __riscv_vand_vx_i8m1(vbi, 0xF0, vl);
138
139 vint16m2_t va0bc0 = __riscv_vwmul_vx_i16m2(vbc0, va0c0, vl);
140 vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0bc0, vl);
141 vint16m2_t va0bc1 = __riscv_vwmul_vx_i16m2(vbc1, va0c1, vl);
142 vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0bc1, vl);
143 vint16m2_t va1bc0 = __riscv_vwmul_vx_i16m2(vbc0, va1c0, vl);
144 vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1bc0, vl);
145 vint16m2_t va1bc1 = __riscv_vwmul_vx_i16m2(vbc1, va1c1, vl);
146 vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1bc1, vl);
147 vint16m2_t va2bc0 = __riscv_vwmul_vx_i16m2(vbc0, va2c0, vl);
148 vacc2 = __riscv_vwadd_wv_i32m4(vacc2, va2bc0, vl);
149 vint16m2_t va2bc1 = __riscv_vwmul_vx_i16m2(vbc1, va2c1, vl);
150 vacc2 = __riscv_vwadd_wv_i32m4(vacc2, va2bc1, vl);
151 vint16m2_t va3bc0 = __riscv_vwmul_vx_i16m2(vbc0, va3c0, vl);
152 vacc3 = __riscv_vwadd_wv_i32m4(vacc3, va3bc0, vl);
153 vint16m2_t va3bc1 = __riscv_vwmul_vx_i16m2(vbc1, va3c1, vl);
154 vacc3 = __riscv_vwadd_wv_i32m4(vacc3, va3bc1, vl);
155 vint16m2_t va4bc0 = __riscv_vwmul_vx_i16m2(vbc0, va4c0, vl);
156 vacc4 = __riscv_vwadd_wv_i32m4(vacc4, va4bc0, vl);
157 vint16m2_t va4bc1 = __riscv_vwmul_vx_i16m2(vbc1, va4c1, vl);
158 vacc4 = __riscv_vwadd_wv_i32m4(vacc4, va4bc1, vl);
159 vint16m2_t va5bc0 = __riscv_vwmul_vx_i16m2(vbc0, va5c0, vl);
160 vacc5 = __riscv_vwadd_wv_i32m4(vacc5, va5bc0, vl);
161 vint16m2_t va5bc1 = __riscv_vwmul_vx_i16m2(vbc1, va5c1, vl);
162 vacc5 = __riscv_vwadd_wv_i32m4(vacc5, va5bc1, vl);
163 vint16m2_t va6bc0 = __riscv_vwmul_vx_i16m2(vbc0, va6c0, vl);
164 vacc6 = __riscv_vwadd_wv_i32m4(vacc6, va6bc0, vl);
165 vint16m2_t va6bc1 = __riscv_vwmul_vx_i16m2(vbc1, va6c1, vl);
166 vacc6 = __riscv_vwadd_wv_i32m4(vacc6, va6bc1, vl);
167 vint16m2_t va7bc0 = __riscv_vwmul_vx_i16m2(vbc0, va7c0, vl);
168 vacc7 = __riscv_vwadd_wv_i32m4(vacc7, va7bc0, vl);
169 vint16m2_t va7bc1 = __riscv_vwmul_vx_i16m2(vbc1, va7c1, vl);
170 vacc7 = __riscv_vwadd_wv_i32m4(vacc7, va7bc1, vl);
171 }
172 vacc0 = __riscv_vsra_vx_i32m4(vacc0, 4, vl);
173 vacc1 = __riscv_vsra_vx_i32m4(vacc1, 4, vl);
174 vacc2 = __riscv_vsra_vx_i32m4(vacc2, 4, vl);
175 vacc3 = __riscv_vsra_vx_i32m4(vacc3, 4, vl);
176 vacc4 = __riscv_vsra_vx_i32m4(vacc4, 4, vl);
177 vacc5 = __riscv_vsra_vx_i32m4(vacc5, 4, vl);
178 vacc6 = __riscv_vsra_vx_i32m4(vacc6, 4, vl);
179 vacc7 = __riscv_vsra_vx_i32m4(vacc7, 4, vl);
180 // i32 -> f32
181 vfloat32m4_t vout0 = __riscv_vfcvt_f_x_v_f32m4(vacc0, vl);
182 vfloat32m4_t vout1 = __riscv_vfcvt_f_x_v_f32m4(vacc1, vl);
183 vfloat32m4_t vout2 = __riscv_vfcvt_f_x_v_f32m4(vacc2, vl);
184 vfloat32m4_t vout3 = __riscv_vfcvt_f_x_v_f32m4(vacc3, vl);
185 vfloat32m4_t vout4 = __riscv_vfcvt_f_x_v_f32m4(vacc4, vl);
186 vfloat32m4_t vout5 = __riscv_vfcvt_f_x_v_f32m4(vacc5, vl);
187 vfloat32m4_t vout6 = __riscv_vfcvt_f_x_v_f32m4(vacc6, vl);
188 vfloat32m4_t vout7 = __riscv_vfcvt_f_x_v_f32m4(vacc7, vl);
189
190 // vout * input_scale
191 const float vinput_scale0 = quantization_params[0].inv_scale;
192 const float vinput_scale1 = quantization_params[1].inv_scale;
193 const float vinput_scale2 = quantization_params[2].inv_scale;
194 const float vinput_scale3 = quantization_params[3].inv_scale;
195 const float vinput_scale4 = quantization_params[4].inv_scale;
196 const float vinput_scale5 = quantization_params[5].inv_scale;
197 const float vinput_scale6 = quantization_params[6].inv_scale;
198 const float vinput_scale7 = quantization_params[7].inv_scale;
199 vout0 = __riscv_vfmul_vf_f32m4(vout0, vinput_scale0, vl);
200 vout1 = __riscv_vfmul_vf_f32m4(vout1, vinput_scale1, vl);
201 vout2 = __riscv_vfmul_vf_f32m4(vout2, vinput_scale2, vl);
202 vout3 = __riscv_vfmul_vf_f32m4(vout3, vinput_scale3, vl);
203 vout4 = __riscv_vfmul_vf_f32m4(vout4, vinput_scale4, vl);
204 vout5 = __riscv_vfmul_vf_f32m4(vout5, vinput_scale5, vl);
205 vout6 = __riscv_vfmul_vf_f32m4(vout6, vinput_scale6, vl);
206 vout7 = __riscv_vfmul_vf_f32m4(vout7, vinput_scale7, vl);
207
208 const vfloat32m4_t vfilter_output_scale = __riscv_vle32_v_f32m4((const float*) w, vl);
209 w = (const float*) w + nr;
210 vout0 = __riscv_vfmul_vv_f32m4(vout0, vfilter_output_scale, vl);
211 vout1 = __riscv_vfmul_vv_f32m4(vout1, vfilter_output_scale, vl);
212 vout2 = __riscv_vfmul_vv_f32m4(vout2, vfilter_output_scale, vl);
213 vout3 = __riscv_vfmul_vv_f32m4(vout3, vfilter_output_scale, vl);
214 vout4 = __riscv_vfmul_vv_f32m4(vout4, vfilter_output_scale, vl);
215 vout5 = __riscv_vfmul_vv_f32m4(vout5, vfilter_output_scale, vl);
216 vout6 = __riscv_vfmul_vv_f32m4(vout6, vfilter_output_scale, vl);
217 vout7 = __riscv_vfmul_vv_f32m4(vout7, vfilter_output_scale, vl);
218
219 const vfloat32m4_t vbias = __riscv_vle32_v_f32m4((const float*) w, vl);
220 w = (const float*) w + nr;
221 vout0 = __riscv_vfadd_vv_f32m4(vout0, vbias, vl);
222 vout1 = __riscv_vfadd_vv_f32m4(vout1, vbias, vl);
223 vout2 = __riscv_vfadd_vv_f32m4(vout2, vbias, vl);
224 vout3 = __riscv_vfadd_vv_f32m4(vout3, vbias, vl);
225 vout4 = __riscv_vfadd_vv_f32m4(vout4, vbias, vl);
226 vout5 = __riscv_vfadd_vv_f32m4(vout5, vbias, vl);
227 vout6 = __riscv_vfadd_vv_f32m4(vout6, vbias, vl);
228 vout7 = __riscv_vfadd_vv_f32m4(vout7, vbias, vl);
229
230 const float vmin = params->scalar.min;
231 vout0 = __riscv_vfmax_vf_f32m4(vout0, vmin, vl);
232 vout1 = __riscv_vfmax_vf_f32m4(vout1, vmin, vl);
233 vout2 = __riscv_vfmax_vf_f32m4(vout2, vmin, vl);
234 vout3 = __riscv_vfmax_vf_f32m4(vout3, vmin, vl);
235 vout4 = __riscv_vfmax_vf_f32m4(vout4, vmin, vl);
236 vout5 = __riscv_vfmax_vf_f32m4(vout5, vmin, vl);
237 vout6 = __riscv_vfmax_vf_f32m4(vout6, vmin, vl);
238 vout7 = __riscv_vfmax_vf_f32m4(vout7, vmin, vl);
239 const float vmax = params->scalar.max;
240 vout0 = __riscv_vfmin_vf_f32m4(vout0, vmax, vl);
241 vout1 = __riscv_vfmin_vf_f32m4(vout1, vmax, vl);
242 vout2 = __riscv_vfmin_vf_f32m4(vout2, vmax, vl);
243 vout3 = __riscv_vfmin_vf_f32m4(vout3, vmax, vl);
244 vout4 = __riscv_vfmin_vf_f32m4(vout4, vmax, vl);
245 vout5 = __riscv_vfmin_vf_f32m4(vout5, vmax, vl);
246 vout6 = __riscv_vfmin_vf_f32m4(vout6, vmax, vl);
247 vout7 = __riscv_vfmin_vf_f32m4(vout7, vmax, vl);
248
249 // store 8 x vl results to c
250 __riscv_vse32_v_f32m4(c0, vout0, vl);
251 c0 = (float*) ((uintptr_t) c0 + cn_stride);
252 __riscv_vse32_v_f32m4(c1, vout1, vl);
253 c1 = (float*) ((uintptr_t) c1 + cn_stride);
254 __riscv_vse32_v_f32m4(c2, vout2, vl);
255 c2 = (float*) ((uintptr_t) c2 + cn_stride);
256 __riscv_vse32_v_f32m4(c3, vout3, vl);
257 c3 = (float*) ((uintptr_t) c3 + cn_stride);
258 __riscv_vse32_v_f32m4(c4, vout4, vl);
259 c4 = (float*) ((uintptr_t) c4 + cn_stride);
260 __riscv_vse32_v_f32m4(c5, vout5, vl);
261 c5 = (float*) ((uintptr_t) c5 + cn_stride);
262 __riscv_vse32_v_f32m4(c6, vout6, vl);
263 c6 = (float*) ((uintptr_t) c6 + cn_stride);
264 __riscv_vse32_v_f32m4(c7, vout7, vl);
265 c7 = (float*) ((uintptr_t) c7 + cn_stride);
266
267 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
268 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
269 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
270 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
271 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
272 a5 = (const int8_t*) ((uintptr_t) a5 - kc);
273 a6 = (const int8_t*) ((uintptr_t) a6 - kc);
274 a7 = (const int8_t*) ((uintptr_t) a7 - kc);
275 } while (nc != 0);
276 }