diff --git a/ggml/src/ggml-cpu/arch/x86/quants.c b/ggml/src/ggml-cpu/arch/x86/quants.c index 594da2683..032005c65 100644 --- a/ggml/src/ggml-cpu/arch/x86/quants.c +++ b/ggml/src/ggml-cpu/arch/x86/quants.c @@ -1083,6 +1083,29 @@ void ggml_vec_dot_mxfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const vo *s = sumf; } +#if defined(__AVX2__) +// The 8 UE4M3 scales of two NVFP4 blocks as fp32, equal to GGML_CPU_UE4M3_TO_FP32 (x0.5, 0x7F -> 0, sign bit +// ignored, subnormals), in the lane order of the reduction in ggml_vec_dot_nvfp4_q8_0: +// [b0s0 b0s2 b1s0 b1s2 | b0s1 b0s3 b1s1 b1s3] +static inline __m256 ue4m3x8_to_fp32_half(const uint8_t * d0, const uint8_t * d1) { + uint32_t a, b; + memcpy(&a, d0, sizeof(a)); + memcpy(&b, d1, sizeof(b)); + const __m256i v = _mm256_set1_epi64x((int64_t) (((uint64_t) b << 32) | a)); + const __m256i x = _mm256_shuffle_epi8(v, _mm256_setr_epi8( + 0, -1, -1, -1, 2, -1, -1, -1, 4, -1, -1, -1, 6, -1, -1, -1, + 1, -1, -1, -1, 3, -1, -1, -1, 5, -1, -1, -1, 7, -1, -1, -1)); + const __m256i nan = _mm256_cmpeq_epi32(x, _mm256_set1_epi32(0x7F)); + const __m256i xm = _mm256_and_si256(x, _mm256_set1_epi32(0x7F)); + // normal: fp32 exponent e + 119 (bias 7, times 0.5), mantissa bits in place + const __m256 fn = _mm256_castsi256_ps(_mm256_add_epi32(_mm256_slli_epi32(xm, 20), _mm256_set1_epi32(119 << 23))); + // subnormal (e == 0): m * 2^-10 + const __m256 fs = _mm256_mul_ps(_mm256_cvtepi32_ps(xm), _mm256_set1_ps(1.0f/1024.0f)); + const __m256 f = _mm256_blendv_ps(fn, fs, _mm256_castsi256_ps(_mm256_cmpgt_epi32(_mm256_set1_epi32(8), xm))); + return _mm256_andnot_ps(_mm256_castsi256_ps(nan), f); +} +#endif + void ggml_vec_dot_nvfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { assert(nrc == 1); UNUSED(nrc); @@ -1100,47 +1123,40 @@ void ggml_vec_dot_nvfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const vo #if defined(__AVX2__) - const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4); - const __m128i m4b = _mm_set1_epi8(0x0f); - const __m256i mone = _mm256_set1_epi16(1); + const __m256i values = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *) kvalues_fp4)); + const __m256i m4b = _mm256_set1_epi8(0x0f); + const __m256i mone = _mm256_set1_epi16(1); + // two blocks per iteration: 8 sub-block sums, 8 scales and one FMA __m256 accum = _mm256_setzero_ps(); - for(; ib < nb; ib++){ + for (; ib + 1 < nb; ib += 2) { + __m256i p[4]; + for (int k = 0; k < 2; ++k) { + // 8 bytes per sub-block: low nibbles are elements 0-7, high nibbles 8-15 + const __m256i q = _mm256_loadu_si256((const __m256i *) x[ib + k].qs); + const __m256i lo = _mm256_shuffle_epi8(values, _mm256_and_si256(q, m4b)); + const __m256i hi = _mm256_shuffle_epi8(values, _mm256_and_si256(_mm256_srli_epi16(q, 4), m4b)); + const __m256i a = _mm256_unpacklo_epi64(lo, hi); // [sub0 | sub2] + const __m256i b = _mm256_unpackhi_epi64(lo, hi); // [sub1 | sub3] + p[2*k + 0] = mul_add_epi8(_mm256_permute2x128_si256(a, b, 0x20), _mm256_loadu_si256((const __m256i *) y[2*(ib + k) + 0].qs)); + p[2*k + 1] = mul_add_epi8(_mm256_permute2x128_si256(a, b, 0x31), _mm256_loadu_si256((const __m256i *) y[2*(ib + k) + 1].qs)); + } + // a sub-block sum is at most 16*12*127, so it fits in int16: + // lanes [b0s0 b0s2 b1s0 b1s2 | b0s1 b0s3 b1s1 b1s3] + const __m256i h = _mm256_hadd_epi16(_mm256_hadd_epi16(p[0], p[1]), _mm256_hadd_epi16(p[2], p[3])); + const __m256 sums = _mm256_cvtepi32_ps(_mm256_madd_epi16(h, mone)); - const __m128i q4bits_01 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 0)); - const __m128i q4bits_23 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 16)); +#if defined(__F16C__) + // assembled in a GPR: cheaper than four inserts + const uint64_t dyb = (uint64_t) y[2*ib + 0].d | ((uint64_t) y[2*ib + 1].d << 16) | + ((uint64_t) y[2*ib + 2].d << 32) | ((uint64_t) y[2*ib + 3].d << 48); + const __m128 dy = _mm_cvtph_ps(_mm_cvtsi64_si128((int64_t) dyb)); +#else + const __m128 dy = _mm_setr_ps(GGML_CPU_FP16_TO_FP32(y[2*ib + 0].d), GGML_CPU_FP16_TO_FP32(y[2*ib + 1].d), + GGML_CPU_FP16_TO_FP32(y[2*ib + 2].d), GGML_CPU_FP16_TO_FP32(y[2*ib + 3].d)); +#endif - const __m256i q8_01 = _mm256_loadu_si256((const __m256i *)y[2*ib + 0].qs); - const __m256i q8_23 = _mm256_loadu_si256((const __m256i *)y[2*ib + 1].qs); - - const __m128i q4_01_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_01, m4b)); - const __m128i q4_01_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_01, 4), m4b)); - const __m128i q4_23_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_23, m4b)); - const __m128i q4_23_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_23, 4), m4b)); - - //reordering - const __m256i q4_01 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_01_lo,q4_01_hi), _mm_unpacklo_epi64(q4_01_lo,q4_01_hi)); - const __m256i q4_23 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_23_lo,q4_23_hi),_mm_unpacklo_epi64(q4_23_lo,q4_23_hi)); - - const __m256i p01 = mul_add_epi8(q4_01,q8_01); - const __m256i p_1 = _mm256_madd_epi16(p01, mone); - - const __m256i p23 = mul_add_epi8(q4_23,q8_23); - const __m256i p_2 = _mm256_madd_epi16(p23, mone); - - const float dy0 = GGML_CPU_FP16_TO_FP32(y[2*ib].d); - const float dy1 = GGML_CPU_FP16_TO_FP32(y[2*ib+1].d); - - const float s0 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[0]) * dy0; - const float s1 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[1]) * dy0; - const float s2 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[2]) * dy1; - const float s3 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[3]) * dy1; - - const __m256 scales01 = _mm256_set_m128(_mm_set1_ps(s1), _mm_set1_ps(s0)); - const __m256 scales23 = _mm256_set_m128(_mm_set1_ps(s3), _mm_set1_ps(s2)); - - accum = _mm256_fmadd_ps(scales01, _mm256_cvtepi32_ps(p_1), accum); - accum = _mm256_fmadd_ps(scales23, _mm256_cvtepi32_ps(p_2), accum); + accum = _mm256_fmadd_ps(_mm256_mul_ps(ue4m3x8_to_fp32_half(x[ib].d, x[ib + 1].d), _mm256_set_m128(dy, dy)), sums, accum); } sumf = hsum_float_8(accum); diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 91ff4c86a..38fbcb35d 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -14226,6 +14226,16 @@ static std::vector> make_test_cases_perf() { } } + // Inkling-2-Mini experts (NVFP4 in UD-Q4_K_XL): 128 experts, 8 used, n_embd 2304, gate_up 2x1152 fused, down 1152 + for (int bs : {1, 8, 16, 32}) { + for (ggml_type type_a : {GGML_TYPE_NVFP4, GGML_TYPE_MXFP4, GGML_TYPE_Q4_0}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 2304, bs, 2304, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 2304, bs, 1152, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 128, 8, false, 2304, bs, 2304)); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 128, 8, false, 2304, bs, 1152)); + } + } + for (int K : {3, 5}) { for (int IC : {256, 2560}) { for (int IW_IH : {32, 64, 256}) {