okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c143c403
main
1// x86_act.inc - ggml's x86 Q8_0 and Q8_1 activation quantizers (arch/x86/quants.c at the pin; MIT, The ggml authors), tier namespace as x86_dots.inc. 2// Round to nearest EVEN (_mm256_round_ps NEAREST), unlike the Generic quantizers' roundf. Needs hsum_i32_8 / hsum_i32_4 from x86_dots.inc. 3 4void quantize_q8_0(const float* STRATA_RESTRICT x, void* STRATA_RESTRICT vy, int64_t k) { 5 assert(QK8_0 == 32); 6 assert(k % QK8_0 == 0); 7 const int nb = k / QK8_0; 8 9 block_q8_0* STRATA_RESTRICT y = (block_q8_0*) vy; 10 11#if defined(__AVX2__) || defined(__AVX__) 12 for (int i = 0; i < nb; i++) { 13 // Load elements into 4 AVX vectors 14 __m256 v0 = _mm256_loadu_ps( x ); 15 __m256 v1 = _mm256_loadu_ps( x + 8 ); 16 __m256 v2 = _mm256_loadu_ps( x + 16 ); 17 __m256 v3 = _mm256_loadu_ps( x + 24 ); 18 x += 32; 19 20 // Compute max(abs(e)) for the block 21 const __m256 signBit = _mm256_set1_ps( -0.0f ); 22 __m256 maxAbs = _mm256_andnot_ps( signBit, v0 ); 23 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) ); 24 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) ); 25 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) ); 26 27 __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) ); 28 max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) ); 29 max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) ); 30 const float maxScalar = _mm_cvtss_f32( max4 ); 31 32 // Quantize these floats 33 const float d = maxScalar / 127.f; 34 y[i].d = f2h(d); 35 const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f; 36 const __m256 mul = _mm256_set1_ps( id ); 37 38 // Apply the multiplier 39 v0 = _mm256_mul_ps( v0, mul ); 40 v1 = _mm256_mul_ps( v1, mul ); 41 v2 = _mm256_mul_ps( v2, mul ); 42 v3 = _mm256_mul_ps( v3, mul ); 43 44 // Round to nearest integer 45 v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST ); 46 v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST ); 47 v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST ); 48 v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST ); 49 50 // Convert floats to integers 51 __m256i i0 = _mm256_cvtps_epi32( v0 ); 52 __m256i i1 = _mm256_cvtps_epi32( v1 ); 53 __m256i i2 = _mm256_cvtps_epi32( v2 ); 54 __m256i i3 = _mm256_cvtps_epi32( v3 ); 55 56#if defined(__AVX2__) 57 // Convert int32 to int16 58 i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15 59 i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31 60 // Convert int16 to int8 61 i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 62 63 // We got our precious signed bytes, but the order is now wrong 64 // These AVX2 pack instructions process 16-byte pieces independently 65 // The following instruction is fixing the order 66 const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 ); 67 i0 = _mm256_permutevar8x32_epi32( i0, perm ); 68 69 _mm256_storeu_si256((__m256i *)y[i].qs, i0); 70#else 71 // Since we don't have in AVX some necessary functions, 72 // we split the registers in half and call AVX2 analogs from SSE 73 __m128i ni0 = _mm256_castsi256_si128( i0 ); 74 __m128i ni1 = _mm256_extractf128_si256( i0, 1); 75 __m128i ni2 = _mm256_castsi256_si128( i1 ); 76 __m128i ni3 = _mm256_extractf128_si256( i1, 1); 77 __m128i ni4 = _mm256_castsi256_si128( i2 ); 78 __m128i ni5 = _mm256_extractf128_si256( i2, 1); 79 __m128i ni6 = _mm256_castsi256_si128( i3 ); 80 __m128i ni7 = _mm256_extractf128_si256( i3, 1); 81 82 // Convert int32 to int16 83 ni0 = _mm_packs_epi32( ni0, ni1 ); 84 ni2 = _mm_packs_epi32( ni2, ni3 ); 85 ni4 = _mm_packs_epi32( ni4, ni5 ); 86 ni6 = _mm_packs_epi32( ni6, ni7 ); 87 // Convert int16 to int8 88 ni0 = _mm_packs_epi16( ni0, ni2 ); 89 ni4 = _mm_packs_epi16( ni4, ni6 ); 90 91 _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0); 92 _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4); 93#endif 94 } 95#else 96 UNUSED(nb); 97 // scalar 98 quantize_row_q8_0_ref(x, y, k); 99#endif 100} 101 102void quantize_q8_1(const float* STRATA_RESTRICT x, void* STRATA_RESTRICT vy, int64_t k) { 103 assert(k % QK8_1 == 0); 104 const int nb = k / QK8_1; 105 106 block_q8_1* STRATA_RESTRICT y = (block_q8_1*) vy; 107#if defined(__AVX2__) || defined(__AVX__) 108 for (int i = 0; i < nb; i++) { 109 // Load elements into 4 AVX vectors 110 __m256 v0 = _mm256_loadu_ps( x ); 111 __m256 v1 = _mm256_loadu_ps( x + 8 ); 112 __m256 v2 = _mm256_loadu_ps( x + 16 ); 113 __m256 v3 = _mm256_loadu_ps( x + 24 ); 114 x += 32; 115 116 // Compute max(abs(e)) for the block 117 const __m256 signBit = _mm256_set1_ps( -0.0f ); 118 __m256 maxAbs = _mm256_andnot_ps( signBit, v0 ); 119 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) ); 120 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) ); 121 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) ); 122 123 __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) ); 124 max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) ); 125 max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) ); 126 const float max_scalar = _mm_cvtss_f32( max4 ); 127 128 // Quantize these floats 129 const float d = max_scalar / 127.f; 130 y[i].d = f2h(d); 131 const float id = ( max_scalar != 0.0f ) ? 127.f / max_scalar : 0.0f; 132 const __m256 mul = _mm256_set1_ps( id ); 133 134 // Apply the multiplier 135 v0 = _mm256_mul_ps( v0, mul ); 136 v1 = _mm256_mul_ps( v1, mul ); 137 v2 = _mm256_mul_ps( v2, mul ); 138 v3 = _mm256_mul_ps( v3, mul ); 139 140 // Round to nearest integer 141 v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST ); 142 v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST ); 143 v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST ); 144 v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST ); 145 146 // Convert floats to integers 147 __m256i i0 = _mm256_cvtps_epi32( v0 ); 148 __m256i i1 = _mm256_cvtps_epi32( v1 ); 149 __m256i i2 = _mm256_cvtps_epi32( v2 ); 150 __m256i i3 = _mm256_cvtps_epi32( v3 ); 151 152#if defined(__AVX2__) 153 // Compute the sum of the quants and set y[i].s 154 y[i].s = f2h(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)))); 155 156 // Convert int32 to int16 157 i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15 158 i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31 159 // Convert int16 to int8 160 i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 161 162 // We got our precious signed bytes, but the order is now wrong 163 // These AVX2 pack instructions process 16-byte pieces independently 164 // The following instruction is fixing the order 165 const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 ); 166 i0 = _mm256_permutevar8x32_epi32( i0, perm ); 167 168 _mm256_storeu_si256((__m256i *)y[i].qs, i0); 169#else 170 // Since we don't have in AVX some necessary functions, 171 // we split the registers in half and call AVX2 analogs from SSE 172 __m128i ni0 = _mm256_castsi256_si128( i0 ); 173 __m128i ni1 = _mm256_extractf128_si256( i0, 1); 174 __m128i ni2 = _mm256_castsi256_si128( i1 ); 175 __m128i ni3 = _mm256_extractf128_si256( i1, 1); 176 __m128i ni4 = _mm256_castsi256_si128( i2 ); 177 __m128i ni5 = _mm256_extractf128_si256( i2, 1); 178 __m128i ni6 = _mm256_castsi256_si128( i3 ); 179 __m128i ni7 = _mm256_extractf128_si256( i3, 1); 180 181 // Compute the sum of the quants and set y[i].s 182 const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3)); 183 const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7)); 184 y[i].s = f2h(d * hsum_i32_4(_mm_add_epi32(s0, s1))); 185 186 // Convert int32 to int16 187 ni0 = _mm_packs_epi32( ni0, ni1 ); 188 ni2 = _mm_packs_epi32( ni2, ni3 ); 189 ni4 = _mm_packs_epi32( ni4, ni5 ); 190 ni6 = _mm_packs_epi32( ni6, ni7 ); 191 // Convert int16 to int8 192 ni0 = _mm_packs_epi16( ni0, ni2 ); 193 ni4 = _mm_packs_epi16( ni4, ni6 ); 194 195 _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0); 196 _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4); 197#endif 198 } 199#else 200 UNUSED(nb); 201 // scalar 202 quantize_row_q8_1_ref(x, y, k); 203#endif 204}