Debian Codegraph @

src/qd8-f16-qc8w-igemm/gen/qd8-f16-qc8w-igemm-1x8c8-minmax-avx256vnni.c

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