| 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_qc8w_gemm_minmax_ukernel_2x4v__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 union xnn_f32_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 <= 2); |
| 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 |
|
| 44 |
const size_t nr = __riscv_vsetvlmax_e32m4(); |
| 45 |
size_t vl = nr; |
| 46 |
do { |
| 47 |
if XNN_UNLIKELY(nc < nr) { |
| 48 |
vl = __riscv_vsetvl_e32m4(nc); |
| 49 |
} |
| 50 |
nc = nc - vl; |
| 51 |
|
| 52 |
vint32m4_t vksum = __riscv_vle32_v_i32m4((const int32_t*)w, vl); |
| 53 |
w = (const int32_t*) w + nr; |
| 54 |
const int32_t vinput_zero_point0 = quantization_params[0].zero_point; |
| 55 |
const int32_t vinput_zero_point1 = quantization_params[1].zero_point; |
| 56 |
vint32m4_t vacc0 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point0, vl); |
| 57 |
vint32m4_t vacc1 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point1, vl); |
| 58 |
|
| 59 |
size_t k = kc; |
| 60 |
do { |
| 61 |
const int8_t va0 = *a0++; |
| 62 |
const int8_t va1 = *a1++; |
| 63 |
const vint8m1_t vb = __riscv_vle8_v_i8m1((const int8_t*) w, vl); |
| 64 |
w = (const int8_t*) w + nr; |
| 65 |
|
| 66 |
vint16m2_t va0b = __riscv_vwmul_vx_i16m2(vb, va0, vl); |
| 67 |
vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0b, vl); |
| 68 |
vint16m2_t va1b = __riscv_vwmul_vx_i16m2(vb, va1, vl); |
| 69 |
vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1b, vl); |
| 70 |
|
| 71 |
k -= sizeof(int8_t); |
| 72 |
} while (k != 0); |
| 73 |
// i32 -> f32 |
| 74 |
vfloat32m4_t vout0 = __riscv_vfcvt_f_x_v_f32m4(vacc0, vl); |
| 75 |
vfloat32m4_t vout1 = __riscv_vfcvt_f_x_v_f32m4(vacc1, vl); |
| 76 |
|
| 77 |
// vout * input_scale |
| 78 |
const float vinput_scale0 = quantization_params[0].inv_scale; |
| 79 |
const float vinput_scale1 = quantization_params[1].inv_scale; |
| 80 |
vout0 = __riscv_vfmul_vf_f32m4(vout0, vinput_scale0, vl); |
| 81 |
vout1 = __riscv_vfmul_vf_f32m4(vout1, vinput_scale1, vl); |
| 82 |
|
| 83 |
const vfloat32m4_t vfilter_output_scale = __riscv_vle32_v_f32m4((const float*) w, vl); |
| 84 |
w = (const float*) w + nr; |
| 85 |
vout0 = __riscv_vfmul_vv_f32m4(vout0, vfilter_output_scale, vl); |
| 86 |
vout1 = __riscv_vfmul_vv_f32m4(vout1, vfilter_output_scale, vl); |
| 87 |
|
| 88 |
const vfloat32m4_t vbias = __riscv_vle32_v_f32m4((const float*) w, vl); |
| 89 |
w = (const float*) w + nr; |
| 90 |
vout0 = __riscv_vfadd_vv_f32m4(vout0, vbias, vl); |
| 91 |
vout1 = __riscv_vfadd_vv_f32m4(vout1, vbias, vl); |
| 92 |
|
| 93 |
const float vmin = params->scalar.min; |
| 94 |
vout0 = __riscv_vfmax_vf_f32m4(vout0, vmin, vl); |
| 95 |
vout1 = __riscv_vfmax_vf_f32m4(vout1, vmin, vl); |
| 96 |
const float vmax = params->scalar.max; |
| 97 |
vout0 = __riscv_vfmin_vf_f32m4(vout0, vmax, vl); |
| 98 |
vout1 = __riscv_vfmin_vf_f32m4(vout1, vmax, vl); |
| 99 |
|
| 100 |
// store 2 x vl results to c |
| 101 |
__riscv_vse32_v_f32m4(c0, vout0, vl); |
| 102 |
c0 = (float*) ((uintptr_t) c0 + cn_stride); |
| 103 |
__riscv_vse32_v_f32m4(c1, vout1, vl); |
| 104 |
c1 = (float*) ((uintptr_t) c1 + cn_stride); |
| 105 |
|
| 106 |
a0 = (const int8_t*) ((uintptr_t) a0 - kc); |
| 107 |
a1 = (const int8_t*) ((uintptr_t) a1 - kc); |
| 108 |
} while (nc != 0); |
| 109 |
} |