Debian Codegraph @

src/qd8-f16-qc4w-gemm/gen/qd8-f16-qc4w-gemm-1x8c8-minmax-avxvnni.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx8c8-avxvnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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 <immintrin.h>
13
14 #include "xnnpack/gemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18
19
20 void xnn_qd8_f16_qc4w_gemm_minmax_ukernel_1x8c8__avxvnni(
21 size_t mr,
22 size_t nc,
23 size_t kc,
24 const int8_t* restrict a,
25 size_t a_stride,
26 const void* restrict w,
27 xnn_float16* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 const struct xnn_f16_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
31 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33 assert(mr != 0);
34 assert(mr <= 1);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(kc % sizeof(int8_t) == 0);
38 assert(a != NULL);
39 assert(w != NULL);
40 assert(c != NULL);
41
42 kc = round_up_po2(kc, 8 * sizeof(int8_t));
43 const int8_t* a0 = a;
44 uint16_t* c0 = (uint16_t*) c;
45
46 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
47 XNN_FORCE_REALIZATION(vsign_mask);
48 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
49 const __m256 voutput_min = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
50 const __m256 voutput_max = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
51 // XNN_FORCE_REALIZATION(voutput_min);
52 // XNN_FORCE_REALIZATION(voutput_max);
53 const __m256i vmask = _mm256_set1_epi8(0xF0);
54 XNN_FORCE_REALIZATION(vmask);
55 do {
56 const __m256i vksum01234567 = _mm256_load_si256(w);
57 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
58 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
59 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
60 __m256i vacc1x0x0123 = _mm256_setzero_si256();
61 __m256i vacc1x0x4567 = _mm256_setzero_si256();
62 w = (const int32_t*) w + 8;
63
64 size_t k = kc;
65 while (k >= 16 * sizeof(int8_t)) {
66 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
67 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
68 a0 += 16;
69
70 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
71 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
72 const __m256i vbs01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
73 const __m256i vbs89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
74 const __m256i vb01234567x4567 = _mm256_and_si256(vbb01234567x01234567, vmask);
75 const __m256i vb89ABCDEFx4567 = _mm256_and_si256(vbb89ABCDEFx01234567, vmask);
76 const __m256i vb01234567x0123 = _mm256_and_si256(vbs01234567x0123, vmask);
77 const __m256i vb89ABCDEFx0123 = _mm256_and_si256(vbs89ABCDEFx0123, vmask);
78
79 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
80 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
81 vacc1x0x0123 = _mm256_dpbusd_avx_epi32(vacc1x0x0123, va0x89ABCDEF, vb01234567x4567);
82 vacc1x0x4567 = _mm256_dpbusd_avx_epi32(vacc1x0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
83
84 w = (const int8_t*) w + 64;
85 k -= 16 * sizeof(int8_t);
86 }
87
88 if (k != 0) {
89 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
90 a0 += 8;
91
92 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
93 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
94 const __m256i vb01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
95 const __m256i vb89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
96
97 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
98 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
99
100 w = (const int8_t*) w + 64;
101 k -= 8 * sizeof(int8_t);
102 }
103 vacc0x0123 = _mm256_add_epi32(vacc0x0123, vacc1x0x0123);
104 vacc0x4567 = _mm256_add_epi32(vacc0x4567, vacc1x0x4567);
105
106 // Add adjacent pairs
107 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
108 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
109
110 vacc0x01234567 = _mm256_srai_epi32(vacc0x01234567, 4);
111 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
112
113 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
114
115 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
116 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
117 w = (const float*) w + 16;
118
119 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
120
121 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
122
123 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
124
125 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
126 if XNN_LIKELY(nc >= 8) {
127 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
128 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
129 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
130 nc -= 8;
131 } else {
132 if (nc & 4) {
133 _mm_storel_epi64((__m128i*) c0, vfp16out0x01234567);
134 c0 += 4;
135 vfp16out0x01234567 = _mm_unpackhi_epi64(vfp16out0x01234567, vfp16out0x01234567);
136 }
137 if (nc & 2) {
138 _mm_storeu_si32(c0, vfp16out0x01234567);
139 c0 += 2;
140 vfp16out0x01234567 = _mm_srli_epi64(vfp16out0x01234567, 32);
141 }
142 if (nc & 1) {
143 *c0 = (uint16_t) _mm_extract_epi16(vfp16out0x01234567, 0);
144 }
145 nc = 0;
146 }
147 } while (nc != 0);
148 }