Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-5x4v-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_5x4v__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 <= 5);
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
62 const size_t nr = __riscv_vsetvlmax_e32m4();
63 size_t vl = nr;
64 kc = round_up_po2(kc, 2);
65 do {
66 if XNN_UNLIKELY(nc < nr) {
67 vl = __riscv_vsetvl_e32m4(nc);
68 }
69 nc = nc - vl;
70
71 vint32m4_t vksum = __riscv_vle32_v_i32m4((const int32_t*)w, vl);
72 w = (const int32_t*) w + nr;
73 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
74 const int32_t vinput_zero_point1 = quantization_params[1].zero_point;
75 const int32_t vinput_zero_point2 = quantization_params[2].zero_point;
76 const int32_t vinput_zero_point3 = quantization_params[3].zero_point;
77 const int32_t vinput_zero_point4 = quantization_params[4].zero_point;
78 vint32m4_t vacc0 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point0, vl);
79 vint32m4_t vacc1 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point1, vl);
80 vint32m4_t vacc2 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point2, vl);
81 vint32m4_t vacc3 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point3, vl);
82 vint32m4_t vacc4 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point4, vl);
83
84 size_t k = kc;
85 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
86 const int8_t va0c0 = a0[0];
87 const int8_t va0c1 = a0[1];
88 a0 += 2;
89 const int8_t va1c0 = a1[0];
90 const int8_t va1c1 = a1[1];
91 a1 += 2;
92 const int8_t va2c0 = a2[0];
93 const int8_t va2c1 = a2[1];
94 a2 += 2;
95 const int8_t va3c0 = a3[0];
96 const int8_t va3c1 = a3[1];
97 a3 += 2;
98 const int8_t va4c0 = a4[0];
99 const int8_t va4c1 = a4[1];
100 a4 += 2;
101 const vint8m1_t vbi = __riscv_vle8_v_i8m1((const int8_t*) w, vl);
102 w = (const uint8_t*) w + nr;
103 const vint8m1_t vbc0 = __riscv_vsll_vx_i8m1(vbi, 4, vl);
104 const vint8m1_t vbc1 = __riscv_vand_vx_i8m1(vbi, 0xF0, vl);
105
106 vint16m2_t va0bc0 = __riscv_vwmul_vx_i16m2(vbc0, va0c0, vl);
107 vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0bc0, vl);
108 vint16m2_t va0bc1 = __riscv_vwmul_vx_i16m2(vbc1, va0c1, vl);
109 vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0bc1, vl);
110 vint16m2_t va1bc0 = __riscv_vwmul_vx_i16m2(vbc0, va1c0, vl);
111 vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1bc0, vl);
112 vint16m2_t va1bc1 = __riscv_vwmul_vx_i16m2(vbc1, va1c1, vl);
113 vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1bc1, vl);
114 vint16m2_t va2bc0 = __riscv_vwmul_vx_i16m2(vbc0, va2c0, vl);
115 vacc2 = __riscv_vwadd_wv_i32m4(vacc2, va2bc0, vl);
116 vint16m2_t va2bc1 = __riscv_vwmul_vx_i16m2(vbc1, va2c1, vl);
117 vacc2 = __riscv_vwadd_wv_i32m4(vacc2, va2bc1, vl);
118 vint16m2_t va3bc0 = __riscv_vwmul_vx_i16m2(vbc0, va3c0, vl);
119 vacc3 = __riscv_vwadd_wv_i32m4(vacc3, va3bc0, vl);
120 vint16m2_t va3bc1 = __riscv_vwmul_vx_i16m2(vbc1, va3c1, vl);
121 vacc3 = __riscv_vwadd_wv_i32m4(vacc3, va3bc1, vl);
122 vint16m2_t va4bc0 = __riscv_vwmul_vx_i16m2(vbc0, va4c0, vl);
123 vacc4 = __riscv_vwadd_wv_i32m4(vacc4, va4bc0, vl);
124 vint16m2_t va4bc1 = __riscv_vwmul_vx_i16m2(vbc1, va4c1, vl);
125 vacc4 = __riscv_vwadd_wv_i32m4(vacc4, va4bc1, vl);
126 }
127 vacc0 = __riscv_vsra_vx_i32m4(vacc0, 4, vl);
128 vacc1 = __riscv_vsra_vx_i32m4(vacc1, 4, vl);
129 vacc2 = __riscv_vsra_vx_i32m4(vacc2, 4, vl);
130 vacc3 = __riscv_vsra_vx_i32m4(vacc3, 4, vl);
131 vacc4 = __riscv_vsra_vx_i32m4(vacc4, 4, vl);
132 // i32 -> f32
133 vfloat32m4_t vout0 = __riscv_vfcvt_f_x_v_f32m4(vacc0, vl);
134 vfloat32m4_t vout1 = __riscv_vfcvt_f_x_v_f32m4(vacc1, vl);
135 vfloat32m4_t vout2 = __riscv_vfcvt_f_x_v_f32m4(vacc2, vl);
136 vfloat32m4_t vout3 = __riscv_vfcvt_f_x_v_f32m4(vacc3, vl);
137 vfloat32m4_t vout4 = __riscv_vfcvt_f_x_v_f32m4(vacc4, vl);
138
139 // vout * input_scale
140 const float vinput_scale0 = quantization_params[0].inv_scale;
141 const float vinput_scale1 = quantization_params[1].inv_scale;
142 const float vinput_scale2 = quantization_params[2].inv_scale;
143 const float vinput_scale3 = quantization_params[3].inv_scale;
144 const float vinput_scale4 = quantization_params[4].inv_scale;
145 vout0 = __riscv_vfmul_vf_f32m4(vout0, vinput_scale0, vl);
146 vout1 = __riscv_vfmul_vf_f32m4(vout1, vinput_scale1, vl);
147 vout2 = __riscv_vfmul_vf_f32m4(vout2, vinput_scale2, vl);
148 vout3 = __riscv_vfmul_vf_f32m4(vout3, vinput_scale3, vl);
149 vout4 = __riscv_vfmul_vf_f32m4(vout4, vinput_scale4, vl);
150
151 const vfloat32m4_t vfilter_output_scale = __riscv_vle32_v_f32m4((const float*) w, vl);
152 w = (const float*) w + nr;
153 vout0 = __riscv_vfmul_vv_f32m4(vout0, vfilter_output_scale, vl);
154 vout1 = __riscv_vfmul_vv_f32m4(vout1, vfilter_output_scale, vl);
155 vout2 = __riscv_vfmul_vv_f32m4(vout2, vfilter_output_scale, vl);
156 vout3 = __riscv_vfmul_vv_f32m4(vout3, vfilter_output_scale, vl);
157 vout4 = __riscv_vfmul_vv_f32m4(vout4, vfilter_output_scale, vl);
158
159 const vfloat32m4_t vbias = __riscv_vle32_v_f32m4((const float*) w, vl);
160 w = (const float*) w + nr;
161 vout0 = __riscv_vfadd_vv_f32m4(vout0, vbias, vl);
162 vout1 = __riscv_vfadd_vv_f32m4(vout1, vbias, vl);
163 vout2 = __riscv_vfadd_vv_f32m4(vout2, vbias, vl);
164 vout3 = __riscv_vfadd_vv_f32m4(vout3, vbias, vl);
165 vout4 = __riscv_vfadd_vv_f32m4(vout4, vbias, vl);
166
167 const float vmin = params->scalar.min;
168 vout0 = __riscv_vfmax_vf_f32m4(vout0, vmin, vl);
169 vout1 = __riscv_vfmax_vf_f32m4(vout1, vmin, vl);
170 vout2 = __riscv_vfmax_vf_f32m4(vout2, vmin, vl);
171 vout3 = __riscv_vfmax_vf_f32m4(vout3, vmin, vl);
172 vout4 = __riscv_vfmax_vf_f32m4(vout4, vmin, vl);
173 const float vmax = params->scalar.max;
174 vout0 = __riscv_vfmin_vf_f32m4(vout0, vmax, vl);
175 vout1 = __riscv_vfmin_vf_f32m4(vout1, vmax, vl);
176 vout2 = __riscv_vfmin_vf_f32m4(vout2, vmax, vl);
177 vout3 = __riscv_vfmin_vf_f32m4(vout3, vmax, vl);
178 vout4 = __riscv_vfmin_vf_f32m4(vout4, vmax, vl);
179
180 // store 5 x vl results to c
181 __riscv_vse32_v_f32m4(c0, vout0, vl);
182 c0 = (float*) ((uintptr_t) c0 + cn_stride);
183 __riscv_vse32_v_f32m4(c1, vout1, vl);
184 c1 = (float*) ((uintptr_t) c1 + cn_stride);
185 __riscv_vse32_v_f32m4(c2, vout2, vl);
186 c2 = (float*) ((uintptr_t) c2 + cn_stride);
187 __riscv_vse32_v_f32m4(c3, vout3, vl);
188 c3 = (float*) ((uintptr_t) c3 + cn_stride);
189 __riscv_vse32_v_f32m4(c4, vout4, vl);
190 c4 = (float*) ((uintptr_t) c4 + cn_stride);
191
192 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
193 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
194 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
195 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
196 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
197 } while (nc != 0);
198 }