Debian Codegraph @

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