diff --git a/docs/INTEL_ARC.md b/docs/INTEL_ARC.md index 601a005d1f..b5f4548083 100644 --- a/docs/INTEL_ARC.md +++ b/docs/INTEL_ARC.md @@ -224,11 +224,18 @@ Running on Windows: - Always set `ONEAPI_DEVICE_SELECTOR=level_zero:gpu` so SYCL selects the discrete Arc GPU over Level Zero. - In zero-oneMKL builds on Battlemage, `UR_LOADER_USE_LEVEL_ZERO_V2=1` (immediate command lists) and SYCL `command_graph` decode replay are enabled automatically unless overridden. - For DFlash 2 speculative decoding, set `STRATA_INK_DRAFTER=dflash2` and pass `--draft dflash2` (or `--draft on`). -- Default mode is 100% lossless/exact (`chunk=4096` for single-slot `<=128K`, register-direct `BK=64` XMX FlashAttention-2, `BN=128` contiguous-`h_act` XMX MoE, 2-term BF16 XMX dense/controller GEMMs, 2,048-entry SWA ring, and 0-ULP speculative tree + linear verify parity); pass `--turbo` (`STRATA_INK_INTELARC=1` + `STRATA_INK_TAIL_SKIP=turbo`) to opt into `Q8_ROW` trunk compaction, load-time `MX8 -> SF4` Coda transcoding, Hybrid KV (`40` lossless FP16 SWA visits + `8` `FP8_E4M3` global visits), Quest Block-TopK (`128` prefill / `96` decode blocks), VNNI `joint_matrix` INT8/FP16 XMX prefill & decode, and router tail-skip. +- Default mode is 100% lossless/exact (`chunk=4096` for single-slot `<=128K`, register-direct `BK=64` XMX FlashAttention-2, `BN=128` contiguous-`h_act` XMX MoE including 2-term `BK=16` exact GGUF MoE, 2-term BF16 XMX dense/controller GEMMs, 16B-aligned `SG32` GGUF MoE decode/verify, 2,048-entry SWA ring, and 0-ULP speculative tree + linear verify parity); pass `--turbo` (`STRATA_INK_INTELARC=1` + `STRATA_INK_TAIL_SKIP=turbo`) to opt into `Q8_ROW` trunk/unembed compaction, load-time `MX8 -> SF4` and GGUF `Q8_0 -> SF4` Coda transcoding, Hybrid KV (`40` lossless FP16 SWA visits + `8` `FP8_E4M3` global visits), Quest Block-TopK (`128` prefill / `96` decode blocks), VNNI `joint_matrix` INT8/FP16 XMX prefill & decode, and router tail-skip. - Every prefill and long-context attention launch is sub-chunked well below the ~2 s Windows GPU TDR limit so long contexts run without device resets or host spill. -- **Measured on one Arc Pro B70 32 GB (Windows 11, `Inkling-2-Mini SQ-20 v2`, `--max-context 131072`, `--draft dflash2`, all 3,072 experts in VRAM, zero spill):** - - **Lossless Default (`dflash2`):** `1,812.5 tok/s` prompt / `178.9 tok/s` gen (`96.7` non-repeating prose) at 4K; `1,582.0` / `97.1 tok/s` (`151.8` prose) at 32K; `1,382.2` / `121.5 tok/s` at 64K; `1,097.1` / `97.1 tok/s` (`123.9` prose) at 128K (`0-ULP` `--spec-check 64`, `100%` Top-1 at `pos >= 1024` in `--ring-check 128`, `25.91 GiB` peak VRAM, `0.06 GiB` shared). - - **Opt-in `--turbo` (`dflash2`):** `2,549.6 tok/s` prompt / `211.7 tok/s` gen (`92.7` non-repeating prose) at 4K; `2,333.0` / `199.1 tok/s` (`136.8` synthetic) at 32K; `2,313.7` / `163.8 tok/s` at 64K; `2,293.6` (`2,254.6` prose) / `165.7 tok/s` at 128K (`21.24 GiB` peak VRAM, `9.96 GiB` free headroom, `0.07 GiB` shared). +- **Measured on one Arc Pro B70 32 GB (Windows 11, `--max-context 131072`, `--draft dflash2`, all 3,072 experts in VRAM, zero spill across 4K–128K):** + - **`Inkling-2-Mini SQ-20 v2` (`.strata`):** + - **Lossless Default (`dflash2`):** `1,812.5 tok/s` prompt / `178.9 tok/s` gen (`96.7` non-repeating prose) at 4K; `1,582.0` / `97.1 tok/s` (`151.8` prose) at 32K; `1,382.2` / `121.5 tok/s` at 64K; `1,097.1` / `97.1 tok/s` (`123.9` prose) at 128K (`0-ULP` `--spec-check 64`, `--ring-check 128` `ok`, `25.91 GiB` peak VRAM, `0.06 GiB` shared). + - **Opt-in `--turbo` (`dflash2`):** `2,549.6 tok/s` prompt / `211.7 tok/s` gen (`92.7` prose) at 4K; `2,333.0` / `199.1 tok/s` (`136.8` synthetic) at 32K; `2,313.7` / `163.8 tok/s` at 64K; `2,293.6` (`2,254.6` prose) / `165.7 tok/s` at 128K (`21.24 GiB` peak VRAM, `9.96 GiB` free headroom, `0.07 GiB` shared). + - **`Inkling-2-Mini GSQ-RCO IQ3_S` (`.gguf`, `10.64 GiB`):** + - **Lossless Default (`dflash2`):** `1,382.9 tok/s` prompt (`1,682.9` 1-term) / `157.6–158.8 tok/s` gen at 4K; `1,243.0` (`1,483.5` 1-term) / `134.3 tok/s` prose (`83.7–87.3` synthetic) at 32K; `1,114.6` (`1,303.6` 1-term) / `138.8 tok/s` at 64K; `837.5` (`957.0` 1-term) / `110.6 tok/s` prose (`84.6–85.9` synthetic) at 128K (`0-ULP` `--spec-check 64`, `--ring-check 128` `ok`, `6/7` prompts bit-identical to scalar FP32 including `long4k`, `19.84 GiB` peak VRAM, `0.07 GiB` shared). + - **Opt-in `--turbo` (`dflash2`):** `2,273.5 tok/s` prompt / `175.1 tok/s` gen at 4K; `2,105.5–2,109.1` / `159.8 tok/s` prose (`103.5` synthetic) at 32K; `2,090.3` / `178.9 tok/s` at 64K; `1,823.9` (`1,804.7` prose) / `158.2 tok/s` prose (`133.3` synthetic) at 128K (`16.88 GiB` peak VRAM, `14.3 GiB` free headroom, `0.07 GiB` shared). + - **`Inkling-2-Mini UD-Q4_K_XL` (`.gguf`, `16.42 GiB`):** + - **Lossless Default (`dflash2`):** `1,780.9–1,817.6 tok/s` prompt / `174.4–175.8 tok/s` gen at 4K; `1,546.4–1,588.9` / `151.0 tok/s` prose (`96.1` synthetic) at 32K; `1,383.9` / `149.1 tok/s` at 64K; `994.4–1,006.4` / `122.9 tok/s` prose (`86.2–86.4` synthetic) at 128K (`0-ULP` `--spec-check 64`, `--ring-check 128` `ok`, `25.68 GiB` peak VRAM, `0.07 GiB` shared). + - **Opt-in `--turbo` (`dflash2`):** `2,536.7 tok/s` prompt / `198.9 tok/s` gen at 4K; `2,335.4` (`2,306.0` prose) / `188.1 tok/s` prose (`118.1` synthetic) at 32K; `2,315.9` / `202.2 tok/s` at 64K; `2,035.7` (`1,977.6` prose) / `186.2 tok/s` prose (`92.5` synthetic) at 128K (`20.86 GiB` peak VRAM, `10.3 GiB` free headroom, `0.08 GiB` shared). Note: `setup --backend sycl` and the older `engine/sycl` (`strata`) target still only support Linux. WSL2 with an Arc was used by one tester (the B580 row above), though `setup` cannot detect `/sys/class/drm` inside WSL2. diff --git a/engine/src/inkling2/sycl/ink_sy_b70_gguf.h b/engine/src/inkling2/sycl/ink_sy_b70_gguf.h index 8dbcfac2b6..1641f21b67 100644 --- a/engine/src/inkling2/sycl/ink_sy_b70_gguf.h +++ b/engine/src/inkling2/sycl/ink_sy_b70_gguf.h @@ -1,19 +1,22 @@ // ink_sy_b70_gguf.h - Custom Intel Arc Pro B70 (Xe2 / bmg_g31) GGUF MoE Kernels // Targets: // - IQ3_S & GSQ-RCO: IQ3_S(21), IQ2_S(22), IQ2_XS(17), IQ2_XXS(16), IQ1_S(19), Q2_0(42), IQ3_XXS(18) -// - Unsloth UD-Q4_K_XL: Q4_K(12), Q5_K(13), Q6_K(14), IQ4_XS(23), IQ4_NL(20), Q8_0(8) +// - Unsloth UD-Q4_K_XL: Q4_K(12), Q5_K(13), Q6_K(14), IQ4_XS(23), IQ4_NL(20), Q8_0(8), Q4_0(2), MXFP4(39) // -// Three Xe2-specialized execution tiers: -// 1. Exact Mode (M <= 16): 32-tile Persistent-SLM SG32 Half-Split Small-GRF GEMV with -// cooperative SLM Codebook Preload (0 global USM iq_grids random gathers!) & 0-ULP parity. -// 2. Turbo Mode (M <= 16): Q8_1 (32-block int8x4 + d + sum) activation quantization + -// SG32 Half-Split dp4a_s8 (`sycl::ext::oneapi::dot_acc`) integer dot-product kernels. -// 3. Turbo XMX Prefill (M >= 16): Native SG16 VNNI-2 `joint_matrix` FP16 DPAS (8x16x16, -// stride 136, explicit captures, SLM Codebook Preload) for GGUF_RAW MoE experts. +// Four Xe2-specialized execution tiers: +// 1. Exact Decode/Verify (M <= 16): Zero-spill SG32 Half-Split Small-GRF GEMV with +// 16B-aligned bank-conflict-free STRIDE32=36 SLM, persistent SLM fast path (n<=1 GU / n<=4 DN), +// single-pass K-tiled multi-token verify (unit_step % 16 == 0 -> 0-ULP parity M=1 vs M=2..16!). +// 2. Turbo Decode/Verify (M <= 16): Compact SoA Q8_1 SLM staging (2.81 KiB/tok) + +// single-unpack weight-reuse SG32 dp4a_s8 (`sycl::ext::oneapi::dot_acc`) across all mm<=16 tokens. +// 3. Exact & Turbo BM=128, BN=128 XMX Prefill (M >= 64): Flat-worklist NR2(18, max_xm_tiles, 256) +// unioned-SLM (<= 28.25 KiB <= 32 KiB -> full 4 WGs/Xe2 core across ALL 15 GGUF types!) +// + contiguous sorted h_act[(off+tr)*1152] + optional HALF_X / HALF_INTER / HALF_YP (--turbo). +// 4. Legacy BM=32, BN=64 XMX Prefill fallback (16 < M < 64). // // CRITICAL BATTLEMAGE B70 RULES: // - ZERO reqd_sub_group_size(8) anywhere (strictly SG16 and SG32!). -// - Explicit lambda captures on all #if INK_SY_DEV_JM_SG16 kernels. +// - Explicit lambda captures & flat outer-scope joint_matrix SSA declarations on all XMX kernels. // MIT License - Copyright (c) 2026 Strata contributors #pragma once @@ -41,14 +44,6 @@ inline float b70_gguf_sg32_half_sum16(const sycl::sub_group& sg, float v) { return v; } -// 8-lane sub-block sum inside an SG32 (lanes 0..7, 8..15, 16..23, 24..31 sum independently) -inline int32_t b70_gguf_sg32_sum8_i32(const sycl::sub_group& sg, int32_t v) { - v += sycl::permute_group_by_xor(sg, v, 4); - v += sycl::permute_group_by_xor(sg, v, 2); - v += sycl::permute_group_by_xor(sg, v, 1); - return v; -} - inline int8_t b70_iq4_nl_i8(uint32_t nib) { constexpr int8_t kLut[16] = { -127, -104, -83, -65, -49, -35, -22, -10, @@ -65,23 +60,70 @@ inline int8_t b70_mxfp4_2x_i8(uint32_t nib) { return kLut[nib & 15u]; } +inline constexpr bool b70_gguf_gu_xmx_supported(uint32_t gt) { + return gt == 21u || gt == 22u || gt == 17u || gt == 16u || gt == 19u || + gt == 42u || gt == 18u || gt == 12u || gt == 13u || gt == 14u || + gt == 23u || gt == 20u || gt == 8u || gt == 2u || gt == 39u; +} + +inline constexpr bool b70_gguf_dn_xmx_supported(uint32_t dt) { + return dt == 42u || dt == 20u || dt == 8u || dt == 2u || dt == 39u; +} + // ============================================================================ -// 1. SLM Codebook Sizing & Cooperative Preload Helpers +// 1. Unified Single-Accessor SLM Codebook Sizing & Cooperative Preload Helpers +// IQ1_S (16 KiB) stays in Xe2's 192 KiB L1 cache (0 generic-addrspace branches, +// 0 B SLM -> 8 WGs/core at M=1!), while <= 8 KiB IQ tables preload into SLM. // ============================================================================ +template +constexpr size_t b70_gguf_lut_u32_words() { + if constexpr (T == 16u) return 256 * 2 + 32; // IQ2_XXS: 2048 + 128 = 2176 B + if constexpr (T == 17u) return 512 * 2 + 32; // IQ2_XS : 4096 + 128 = 4224 B + if constexpr (T == 22u) return 1024 * 2; // IQ2_S : 8192 B + if constexpr (T == 21u) return 512; // IQ3_S : 2048 B + if constexpr (T == 18u) return 256 + 32; // IQ3_XXS: 1024 + 128 = 1152 B + return 0; // IQ1_S(19) & non-IQ: 0 B SLM (192 KiB L1!) +} + +template +inline void b70_gguf_preload_lut_ptr(int tid, int wg_size, const InkSyIqGridTable* g, uint32_t* lut_u32) { + if constexpr (T == 21u) { + for (int i = tid; i < 512; i += wg_size) lut_u32[i] = g->iq3s_grid[i]; + } else if constexpr (T == 18u) { + for (int i = tid; i < 256; i += wg_size) lut_u32[i] = g->iq3xxs_grid[i]; + uint8_t* sgn = reinterpret_cast(lut_u32 + 256); + for (int i = tid; i < 128; i += wg_size) sgn[i] = g->ksigns_iq2[i]; + } else if constexpr (T == 16u) { + uint64_t* g64 = reinterpret_cast(lut_u32); + for (int i = tid; i < 256; i += wg_size) g64[i] = g->iq2xxs_grid[i]; + uint8_t* sgn = reinterpret_cast(lut_u32 + 512); + for (int i = tid; i < 128; i += wg_size) sgn[i] = g->ksigns_iq2[i]; + } else if constexpr (T == 17u) { + uint64_t* g64 = reinterpret_cast(lut_u32); + for (int i = tid; i < 512; i += wg_size) g64[i] = g->iq2xs_grid[i]; + uint8_t* sgn = reinterpret_cast(lut_u32 + 1024); + for (int i = tid; i < 128; i += wg_size) sgn[i] = g->ksigns_iq2[i]; + } else if constexpr (T == 22u) { + uint64_t* g64 = reinterpret_cast(lut_u32); + for (int i = tid; i < 1024; i += wg_size) g64[i] = g->iq2s_grid[i]; + } + (void)tid; (void)wg_size; (void)g; (void)lut_u32; +} + +// Legacy 3-accessor helpers preserved for Tier 4 (16 < M < 64) fallback compatibility template constexpr size_t b70_gguf_grid_u64_len() { - if constexpr (T == 16u) return 256; // IQ2_XXS (2 KiB) - if constexpr (T == 17u) return 512; // IQ2_XS (4 KiB) - if constexpr (T == 22u) return 1024; // IQ2_S (8 KiB) - if constexpr (T == 19u) return 2048; // IQ1_S (16 KiB) + if constexpr (T == 16u) return 256; + if constexpr (T == 17u) return 512; + if constexpr (T == 22u) return 1024; return 1; } template constexpr size_t b70_gguf_grid_u32_len() { - if constexpr (T == 21u) return 512; // IQ3_S (2 KiB) - if constexpr (T == 18u) return 256; // IQ3_XXS (1 KiB) + if constexpr (T == 21u) return 512; + if constexpr (T == 18u) return 256; return 1; } @@ -90,32 +132,10 @@ constexpr bool b70_gguf_needs_ksigns() { return (T == 16u || T == 17u || T == 18u); } -template -inline void b70_gguf_preload_slm_lut(int tid, int wg_size, const InkSyIqGridTable* g, - Acc64& s_g64, Acc32& s_g32, AccSign& s_sgn) { - if constexpr (T == 21u) { - for (int i = tid; i < 512; i += wg_size) s_g32[i] = g->iq3s_grid[i]; - } else if constexpr (T == 18u) { - for (int i = tid; i < 256; i += wg_size) s_g32[i] = g->iq3xxs_grid[i]; - if (tid < 128) s_sgn[tid] = g->ksigns_iq2[tid]; - } else if constexpr (T == 16u) { - for (int i = tid; i < 256; i += wg_size) s_g64[i] = g->iq2xxs_grid[i]; - if (tid < 128) s_sgn[tid] = g->ksigns_iq2[tid]; - } else if constexpr (T == 17u) { - for (int i = tid; i < 512; i += wg_size) s_g64[i] = g->iq2xs_grid[i]; - if (tid < 128) s_sgn[tid] = g->ksigns_iq2[tid]; - } else if constexpr (T == 22u) { - for (int i = tid; i < 1024; i += wg_size) s_g64[i] = g->iq2s_grid[i]; - } else if constexpr (T == 19u) { - for (int i = tid; i < 2048; i += wg_size) s_g64[i] = g->iq1s_grid[i]; - } - (void)g; (void)s_g64; (void)s_g32; (void)s_sgn; -} - -// Compile-time specialized, SLM-LUT-backed 8-weight FP32 decoder (0 global LUT gathers, 0 switch!) -template -inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, float* out, - const Acc64& s_g64, const Acc32& s_g32, const AccSign& s_sgn) { +// Compile-time specialized, raw-SLM-pointer 8-weight FP32 decoder (0 global random LUT gathers, 0 switch!) +template +inline void b70_gguf_decode_8_ptr(const uint8_t* row, int col, float ext_sc, float* out, + const uint32_t* lut_u32, const InkSyIqGridTable* iq_grids) { if constexpr (T == 21u) { // IQ3_S (110 B / 256, GSQ-RCO) const uint8_t* b = row + (size_t)(col >> 8) * 110u; const float d = h2f_bits(ld16(b)) * ext_sc; @@ -127,8 +147,8 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo const uint8_t sg = b[74 + sub * 4 + l4]; const uint8_t scb = b[106 + (sub >> 1)]; const float dl = d * (float)(1 + 2 * (int)((scb >> (4 * (sub & 1))) & 0xFu)); - const uint32_t w0 = s_g32[i1]; - const uint32_t w1 = s_g32[i2]; + const uint32_t w0 = lut_u32[i1]; + const uint32_t w1 = lut_u32[i2]; #pragma unroll for (int i = 0; i < 4; ++i) { const float v0 = (float)((w0 >> (i * 8)) & 0xFFu) * dl; @@ -137,6 +157,7 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i + 4] = (sg & (1u << (i + 4))) ? -v1 : v1; } } else if constexpr (T == 22u) { // IQ2_S (82 B / 256, GSQ-RCO) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); const uint8_t* b = row + (size_t)(col >> 8) * 82u; const float d = h2f_bits(ld16(b)) * ext_sc; const int j = col & 255, sub = j >> 5, l4 = (j & 31) >> 3; @@ -152,6 +173,8 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i] = (sg & (1u << i)) ? -v : v; } } else if constexpr (T == 17u) { // IQ2_XS (74 B / 256, GSQ-RCO) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 1024); const uint8_t* b = row + (size_t)(col >> 8) * 74u; const float d = h2f_bits(ld16(b)) * ext_sc; const int j = col & 255, sub = j >> 5, l4 = (j & 31) >> 3; @@ -166,6 +189,8 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i] = (sg & (1u << i)) ? -v : v; } } else if constexpr (T == 16u) { // IQ2_XXS (66 B / 256, GSQ-RCO) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 512); const uint8_t* b = row + (size_t)(col >> 8) * 66u; const float d = h2f_bits(ld16(b)) * ext_sc; const int j = col & 255, sub = j >> 5, l4 = (j & 31) >> 3; @@ -180,7 +205,7 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo const float v = (float)((g >> (i * 8)) & 0xFFu) * dl; out[i] = (sg & (1u << i)) ? -v : v; } - } else if constexpr (T == 19u) { // IQ1_S (50 B / 256, GSQ-RCO) + } else if constexpr (T == 19u) { // IQ1_S (50 B / 256, GSQ-RCO; 16 KiB codebook L1-resident!) const uint8_t* b = row + (size_t)(col >> 8) * 50u; const float d = h2f_bits(ld16(b)) * ext_sc; const int j = col & 255, sub = j >> 5, l4 = (j & 31) >> 3; @@ -188,7 +213,7 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo const float dl = d * (float)(2 * (int)((qh >> 12) & 7u) + 1); const float delta = (qh & 0x8000u) ? -0.125f : 0.125f; const uint32_t gi = (uint32_t)b[2 + sub * 4 + l4] | (((uint32_t)(qh >> (3 * l4)) & 7u) << 8); - const uint64_t g = s_g64[gi]; + const uint64_t g = iq_grids->iq1s_grid[gi]; #pragma unroll for (int i = 0; i < 8; ++i) { const int8_t qv = (int8_t)((g >> (i * 8)) & 0xFFu); @@ -205,14 +230,15 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i] = d * (float)q; } } else if constexpr (T == 18u) { // IQ3_XXS (98 B / 256) + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 256); const uint8_t* b = row + (size_t)(col >> 8) * 98u; const float d = h2f_bits(ld16(b)) * ext_sc; const int j = col & 255, ib = j >> 5, l = (j & 31) >> 3; const uint32_t aux = sy_ld32(b + 66 + ib * 4); const float db = d * (0.5f + (float)(aux >> 28)) * 0.5f; const uint8_t signs = s_sgn[(aux >> (7 * l)) & 127u]; - const uint32_t g0 = s_g32[b[2 + ib * 8 + l * 2 + 0]]; - const uint32_t g1 = s_g32[b[2 + ib * 8 + l * 2 + 1]]; + const uint32_t g0 = lut_u32[b[2 + ib * 8 + l * 2 + 0]]; + const uint32_t g1 = lut_u32[b[2 + ib * 8 + l * 2 + 1]]; #pragma unroll for (int i = 0; i < 4; ++i) { const float v0 = (float)((g0 >> (i * 8)) & 0xFFu) * db; @@ -293,7 +319,7 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i] = dl * sy_iq4_nl((q0 >> (8 * i + shift)) & 0xFu); out[i + 4] = dl * sy_iq4_nl((q1 >> (8 * i + shift)) & 0xFu); } - } else if constexpr (T == 20u) { // IQ4_NL (18 B / 32, UD-Q4_K_XL) + } else if constexpr (T == 20u) { // IQ4_NL (18 B / 32, GSQ-RCO & UD-Q4_K_XL) const uint8_t* b = row + (size_t)(col >> 5) * 18u; const float d = h2f_bits(ld16(b)) * ext_sc; const int l = col & 31, shift = (l >= 16) ? 4 : 0; @@ -337,11 +363,41 @@ inline void b70_gguf_decode_8_slm(const uint8_t* row, int col, float ext_sc, flo out[i + 4] = d * e2m1_lut((q1 >> (8 * i + shift)) & 0xFu); } } - (void)s_g64; (void)s_g32; (void)s_sgn; + (void)lut_u32; (void)iq_grids; +} + +// Decode 16 contiguous weights (col is 16-aligned!) straight into VNNI-2 SLM sB as 8 x sycl::half2 +template +inline void b70_gguf_decode_16_to_sB(const uint8_t* row, int col16, float ext_sc, + half* sB_col_ptr, const uint32_t* lut_u32, + const InkSyIqGridTable* iq_grids) { + float w8[8]; + b70_gguf_decode_8_ptr(row, col16, ext_sc, w8, lut_u32, iq_grids); +#pragma unroll + for (int j = 0; j < 4; ++j) { + *reinterpret_cast(&sB_col_ptr[j * LD_SB]) = + sycl::half2((half)w8[2 * j], (half)w8[2 * j + 1]); + } + b70_gguf_decode_8_ptr(row, col16 + 8, ext_sc, w8, lut_u32, iq_grids); +#pragma unroll + for (int j = 0; j < 4; ++j) { + *reinterpret_cast(&sB_col_ptr[(4 + j) * LD_SB]) = + sycl::half2((half)w8[2 * j], (half)w8[2 * j + 1]); + } +} + +// 16B-aligned SLM dot8 helper (preserves exact left-to-right (((0+w0*x0)+w1*x1)+...+w7*x7) FP32 order!) +inline float b70_dot8_f4x2(const float* w, const float* sx_aligned16) { + const sycl::float4 x0 = *reinterpret_cast(sx_aligned16 + 0); + const sycl::float4 x1 = *reinterpret_cast(sx_aligned16 + 4); + float d = 0.0f; + d += w[0] * x0.x(); d += w[1] * x0.y(); d += w[2] * x0.z(); d += w[3] * x0.w(); + d += w[4] * x1.x(); d += w[5] * x1.y(); d += w[6] * x1.z(); d += w[7] * x1.w(); + return d; } // ============================================================================ -// 2. Tier 1: Exact 32-Tile Persistent-SLM SG32 Half-Split GGUF MoE (0-ULP Exact!) +// 2. Tier 1: Zero-Spill SG32 16B-Aligned STRIDE32=36 GGUF MoE (0-ULP Exact!) // ============================================================================ template @@ -350,19 +406,20 @@ inline void k_b70_moe_gu_gguf_exact_sg32( const int* uexp, const int* uoff, const int* ucnt, const int* pair_list, const int* n_uniq_dev, int max_uniq, float* h_act, int M) { const InkSyIqGridTable* iq_grids = iq_grid_table_dev(); - constexpr size_t G64 = b70_gguf_grid_u64_len(); - constexpr size_t G32 = b70_gguf_grid_u32_len(); - constexpr size_t GSG = b70_gguf_needs_ksigns() ? 128 : 1; - // IQ1_S uses 16 KiB codebook + 9 KiB s_x = 25 KiB/WG -> 32 WGs (4/core = 100 KiB <= 128 KiB SLM/core) fits cleanly! + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words(); const size_t rb_2304 = sy_gguf_row_bytes(GT, 2304); if (M == 1) { + // STRIDE32=36 floats (72 * 36 = 2592 floats = 10.125 KiB) -> 16B-aligned float4 loads & 0.0% SLM bank conflicts! + // When total SLM <= 16.00 KiB, launch NR2(32, max_uniq, 256) (8 WGs/core = 256 WGs in 1 wave!). + // When total SLM > 16.00 KiB (IQ2_S = 18.1 KiB, IQ1_S = 22.1 KiB), launch NR2(16, max_uniq, 256) (4 WGs/core = 128 WGs in 1 wave, 0 tail stall!). + constexpr size_t SX_U32 = 72 * 36; + constexpr size_t TOT_U32 = SX_U32 + LUT_U32; + constexpr size_t R_GROUPS = (TOT_U32 <= 4096) ? 32 : 16; + constexpr int R_STRIDE = (int)R_GROUPS * 8; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_x(2304, h); - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); - h.parallel_for(NR2(32, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { + sycl::local_accessor sRaw(TOT_U32, h); + h.parallel_for(NR2(R_GROUPS, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int y = (int)it.get_group(0); if (y >= n_uniq_dev[0]) return; const int tid = (int)it.get_local_id(1); @@ -371,13 +428,15 @@ inline void k_b70_moe_gu_gguf_exact_sg32( const int half_id = lid >> 4, lane16 = lid & 15; const int r_base = (int)it.get_group(1) * 8 + sgi; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + float* sx_ptr = reinterpret_cast(raw_ptr); + uint32_t* lut_ptr = raw_ptr + SX_U32; const sycl::float4* x4 = reinterpret_cast(x_in); + sycl::float4* sx4 = reinterpret_cast(sx_ptr); for (int idx = tid; idx < 576; idx += 256) { - const sycl::float4 v = x4[idx]; - const int d = idx * 4; - s_x[d + 0] = v.x(); s_x[d + 1] = v.y(); s_x[d + 2] = v.z(); s_x[d + 3] = v.w(); + sx4[(idx >> 3) * 9 + (idx & 7)] = x4[idx]; } - b70_gguf_preload_slm_lut(tid, 256, iq_grids, s_g64, s_g32, s_sgn); + b70_gguf_preload_lut_ptr(tid, 256, iq_grids, lut_ptr); sycl::group_barrier(it.get_group()); const int e = uexp[y], off = uoff[y], n = ucnt[y]; @@ -387,18 +446,15 @@ inline void k_b70_moe_gu_gguf_exact_sg32( const bool ilv = (hdr->flags & 1u) != 0; const float ext_sc = half_id ? hdr->up_scale : hdr->gate_scale; - for (int r = r_base; r < 1152; r += 256) { + for (int r = r_base; r < 1152; r += R_STRIDE) { const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); const uint8_t* rw = body + 32 + (size_t)row_idx * rb_2304; float acc = 0.0f; for (int unit = lane16; unit < 288; unit += 16) { const int col = unit << 3; float w[8]; - b70_gguf_decode_8_slm(rw, col, ext_sc, w, s_g64, s_g32, s_sgn); - float d = 0.0f; -#pragma unroll - for (int t = 0; t < 8; ++t) d += w[t] * s_x[col + t]; - acc += d; + b70_gguf_decode_8_ptr(rw, col, ext_sc, w, lut_ptr, iq_grids); + acc += b70_dot8_f4x2(w, sx_ptr + (unit >> 2) * 36 + (unit & 3) * 8); } const float sum_h = b70_gguf_sg32_half_sum16(sg, acc); const float u_val = sycl::select_from_group(sg, sum_h, 16u); @@ -412,22 +468,28 @@ inline void k_b70_moe_gu_gguf_exact_sg32( return; } + // M in 2..16 (and 17..63 tail fallback): NR2(72, max_uniq, 256) -> 2 row-pairs/SG32, + // 2 * 72 * 36 = 5184 floats (20.25 KiB + <= 8.00 KiB LUT = <= 28.25 KiB <= 32.00 KiB -> 4 WGs/Xe2 core!). + // 1-barrier ZERO-K-tiling persistent SLM fast path for n <= 2 (>85% of M=8 verify experts!), + // outer-K-loop single-pass SLM (unit_step % 16 == 0 -> 0-ULP bit-identical to M==1!) for n >= 3! + constexpr size_t SX_U32 = 2 * 72 * 36; // 5184 floats (20.25 KiB) + constexpr size_t TOT_U32 = SX_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); - h.parallel_for(NR2(144, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { + sycl::local_accessor sRaw(TOT_U32, h); + h.parallel_for(NR2(72, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int y = (int)it.get_group(0); if (y >= n_uniq_dev[0]) return; const int tid = (int)it.get_local_id(1); auto sg = it.get_sub_group(); const int sgi = tid >> 5, lid = tid & 31; const int half_id = lid >> 4, lane16 = lid & 15; - const int r = (int)it.get_group(1) * 8 + sgi; + const int r_base = (int)it.get_group(1) * 8 + sgi; - b70_gguf_preload_slm_lut(tid, 256, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); - if (r >= 1152) return; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + float* sx_ptr = reinterpret_cast(raw_ptr); + sycl::float4* sx4 = reinterpret_cast(sx_ptr); + uint32_t* lut_ptr = raw_ptr + SX_U32; + b70_gguf_preload_lut_ptr(tid, 256, iq_grids, lut_ptr); const int e = uexp[y], off = uoff[y], n = ucnt[y]; const InkSyExpertDev ed = ex_layer[e]; @@ -435,30 +497,94 @@ inline void k_b70_moe_gu_gguf_exact_sg32( const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); const bool ilv = (hdr->flags & 1u) != 0; const float ext_sc = half_id ? hdr->up_scale : hdr->gate_scale; - const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); - const uint8_t* rw = body + 32 + (size_t)row_idx * rb_2304; - for (int t0 = 0; t0 < n; t0 += 4) { - const int mm = sycl::min(4, n - t0); - int pid[4], tk[4]; - for (int i = 0; i < mm; ++i) { pid[i] = pair_list[off + t0 + i]; tk[i] = pid[i] >> 3; } - float acc[4] = {0, 0, 0, 0}; - for (int unit = lane16; unit < 288; unit += 16) { - const int col = unit << 3; - float w[8]; - b70_gguf_decode_8_slm(rw, col, ext_sc, w, s_g64, s_g32, s_sgn); - for (int i = 0; i < mm; ++i) { - const float* xp = x_in + (size_t)tk[i] * 2304 + col; - float d = 0.0f; -#pragma unroll - for (int t = 0; t < 8; ++t) d += w[t] * xp[t]; - acc[i] += d; + if (n <= 2) { + const int p0 = pair_list[off]; + const sycl::float4* gx0 = reinterpret_cast(x_in + (size_t)(p0 >> 3) * 2304); + for (int idx = tid; idx < 576; idx += 256) { + sx4[(idx >> 3) * 9 + (idx & 7)] = gx0[idx]; + } + int p1 = p0; + if (n == 2) { + p1 = pair_list[off + 1]; + const sycl::float4* gx1 = reinterpret_cast(x_in + (size_t)(p1 >> 3) * 2304); + for (int idx = tid; idx < 576; idx += 256) { + sx4[(72 + (idx >> 3)) * 9 + (idx & 7)] = gx1[idx]; } } - for (int i = 0; i < mm; ++i) { - const float sum_h = b70_gguf_sg32_half_sum16(sg, acc[i]); - const float u_val = sycl::select_from_group(sg, sum_h, 16u); - if (lid == 0) h_act[(size_t)pid[i] * 1152 + r] = situ_act(sum_h, u_val); + sycl::group_barrier(it.get_group()); + + for (int r = r_base; r < 1152; r += 576) { + const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); + const uint8_t* rw = body + 32 + (size_t)row_idx * rb_2304; + float acc0 = 0.0f, acc1 = 0.0f; + for (int unit = lane16; unit < 288; unit += 16) { + const int col = unit << 3; + float w[8]; + b70_gguf_decode_8_ptr(rw, col, ext_sc, w, lut_ptr, iq_grids); + const int sub_off = (unit >> 2) * 36 + (unit & 3) * 8; + acc0 += b70_dot8_f4x2(w, sx_ptr + sub_off); + if (n == 2) acc1 += b70_dot8_f4x2(w, sx_ptr + (72 * 36) + sub_off); + } + const float sum0 = b70_gguf_sg32_half_sum16(sg, acc0); + const float u0_val = sycl::select_from_group(sg, sum0, 16u); + if (lid == 0) h_act[(size_t)p0 * 1152 + r] = situ_act(sum0, u0_val); + if (n == 2) { + const float sum1 = b70_gguf_sg32_half_sum16(sg, acc1); + const float u1_val = sycl::select_from_group(sg, sum1, 16u); + if (lid == 0) h_act[(size_t)p1 * 1152 + r] = situ_act(sum1, u1_val); + } + } + return; + } + + if constexpr (LUT_U32 > 0) { + sycl::group_barrier(it.get_group()); + } + + for (int t_base = 0; t_base < n; t_base += 16) { + const int mm = sycl::min(16, n - t_base); + const int unit_step = (mm <= 3) ? 144 + : (mm <= 4) ? 128 + : (mm <= 8) ? 64 + : 32; + const int b32_stride = unit_step >> 2; // 36, 32, 16, or 8 (always exact & %16==0!) + + for (int r = r_base; r < 1152; r += 576) { + const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); + const uint8_t* rw = body + 32 + (size_t)row_idx * rb_2304; + float acc[16] = {}; + for (int u0 = 0; u0 < 288; u0 += unit_step) { + const int u_cnt = sycl::min(unit_step, 288 - u0); + const int b32_cnt = u_cnt >> 2; + const int f4_per_tok = b32_cnt * 8; + const int tot4 = mm * f4_per_tok; + const int col_base = u0 << 3; + for (int idx = tid; idx < tot4; idx += 256) { + const int ti = idx / f4_per_tok; + const int rem = idx - ti * f4_per_tok; + const int tk = pair_list[off + t_base + ti] >> 3; + sx4[(ti * b32_stride + (rem >> 3)) * 9 + (rem & 7)] = + reinterpret_cast(x_in + (size_t)tk * 2304 + col_base)[rem]; + } + sycl::group_barrier(it.get_group()); + + for (int sub_u = lane16; sub_u < u_cnt; sub_u += 16) { + const int col = col_base + (sub_u << 3); + const int sub_off = (sub_u >> 2) * 36 + (sub_u & 3) * 8; + float w[8]; + b70_gguf_decode_8_ptr(rw, col, ext_sc, w, lut_ptr, iq_grids); + for (int i = 0; i < mm; ++i) { + acc[i] += b70_dot8_f4x2(w, sx_ptr + i * (b32_stride * 36) + sub_off); + } + } + sycl::group_barrier(it.get_group()); + } + for (int i = 0; i < mm; ++i) { + const float sum_h = b70_gguf_sg32_half_sum16(sg, acc[i]); + const float u_val = sycl::select_from_group(sg, sum_h, 16u); + if (lid == 0) h_act[(size_t)pair_list[off + t_base + i] * 1152 + r] = situ_act(sum_h, u_val); + } } } }); @@ -471,19 +597,16 @@ inline void k_b70_moe_dn_gguf_exact_sg32( const int* uexp, const int* uoff, const int* ucnt, const int* pair_list, const int* n_uniq_dev, int max_uniq, const float* h_act, float* y_pairs, int M) { const InkSyIqGridTable* iq_grids = iq_grid_table_dev(); - constexpr size_t G64 = b70_gguf_grid_u64_len
(); - constexpr size_t G32 = b70_gguf_grid_u32_len
(); - constexpr size_t GSG = b70_gguf_needs_ksigns
() ? 128 : 1; - // Note: K=1152 is NOT divisible by 256 (1152 = 36*32 = 18*64), so GGUF ffn_down_exps always uses 32/64-block types (Q2_0, IQ4_NL, Q8_0, Q4_0, MXFP4). + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words
(); const size_t rb_1152 = sy_gguf_row_bytes(DT, 1152); if (rb_1152 == 0) return; if (M == 1) { + // 36 * 36 = 1296 floats = 5.06 KiB SLM -> full 8 WGs/Xe2 core = 256 WGs in 1 wave! + constexpr size_t SH_U32 = 36 * 36; + constexpr size_t TOT_U32 = SH_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_h(1152, h); - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); + sycl::local_accessor sRaw(TOT_U32, h); h.parallel_for(NR2(32, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int yy = (int)it.get_group(0); if (yy >= n_uniq_dev[0]) return; @@ -495,33 +618,31 @@ inline void k_b70_moe_dn_gguf_exact_sg32( const int e = uexp[yy], off = uoff[yy], n = ucnt[yy]; const int pid0 = pair_list[off]; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + float* sh_ptr = reinterpret_cast(raw_ptr); + uint32_t* lut_ptr = raw_ptr + SH_U32; const sycl::float4* h4 = reinterpret_cast(h_act + (size_t)pid0 * 1152); + sycl::float4* sh4 = reinterpret_cast(sh_ptr); for (int idx = tid; idx < 288; idx += 256) { - const sycl::float4 v = h4[idx]; - const int d = idx * 4; - s_h[d + 0] = v.x(); s_h[d + 1] = v.y(); s_h[d + 2] = v.z(); s_h[d + 3] = v.w(); + sh4[(idx >> 3) * 9 + (idx & 7)] = h4[idx]; } - b70_gguf_preload_slm_lut
(tid, 256, iq_grids, s_g64, s_g32, s_sgn); + b70_gguf_preload_lut_ptr
(tid, 256, iq_grids, lut_ptr); sycl::group_barrier(it.get_group()); const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(DT, 1152); const float ext_sc = hdr->dn_scale; for (int rp = rp_base; rp < 1152; rp += 256) { const int row = 2 * rp + half_id; - const uint8_t* rw = body + ed.dn_off + (size_t)row * rb; + const uint8_t* rw = body + ed.dn_off + (size_t)row * rb_1152; float acc = 0.0f; for (int unit = lane16; unit < 144; unit += 16) { const int col = unit << 3; float w[8]; - b70_gguf_decode_8_slm
(rw, col, ext_sc, w, s_g64, s_g32, s_sgn); - float d = 0.0f; -#pragma unroll - for (int t = 0; t < 8; ++t) d += w[t] * s_h[col + t]; - acc += d; + b70_gguf_decode_8_ptr
(rw, col, ext_sc, w, lut_ptr, iq_grids); + acc += b70_dot8_f4x2(w, sh_ptr + (unit >> 2) * 36 + (unit & 3) * 8); } const float v = b70_gguf_sg32_half_sum16(sg, acc); if (lane16 == 0) { @@ -536,52 +657,115 @@ inline void k_b70_moe_dn_gguf_exact_sg32( return; } + // M in 2..16: 6 * 36 * 36 = 7776 floats (30.375 KiB <= 32.00 KiB -> 4 WGs/core!). + // Persistent 1-barrier SLM fast path for ALL n <= 6 (>98% of M=8 verify experts!), + // outer-K-loop single-pass SLM (unit_step in {96, 48} -> 0-ULP bit-identical to M==1!) for n=7..16! + constexpr size_t SH_U32 = 6 * 36 * 36; + constexpr size_t TOT_U32 = SH_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); - h.parallel_for(NR2(144, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { + sycl::local_accessor sRaw(TOT_U32, h); + h.parallel_for(NR2(72, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int yy = (int)it.get_group(0); if (yy >= n_uniq_dev[0]) return; const int tid = (int)it.get_local_id(1); auto sg = it.get_sub_group(); const int sgi = tid >> 5, lid = tid & 31; const int half_id = lid >> 4, lane16 = lid & 15; - const int rp = (int)it.get_group(1) * 8 + sgi; + const int rp_base = (int)it.get_group(1) * 8 + sgi; - b70_gguf_preload_slm_lut
(tid, 256, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); - if (rp >= 1152) return; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + float* sh_ptr = reinterpret_cast(raw_ptr); + sycl::float4* sh4 = reinterpret_cast(sh_ptr); + uint32_t* lut_ptr = raw_ptr + SH_U32; + b70_gguf_preload_lut_ptr
(tid, 256, iq_grids, lut_ptr); const int e = uexp[yy], off = uoff[yy], n = ucnt[yy]; const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(DT, 1152); const float ext_sc = hdr->dn_scale; - const int row = 2 * rp + half_id; - const uint8_t* rw = body + ed.dn_off + (size_t)row * rb; - for (int t0 = 0; t0 < n; t0 += 4) { - const int mm = sycl::min(4, n - t0); - int pid[4]; - for (int i = 0; i < mm; ++i) pid[i] = pair_list[off + t0 + i]; - float acc[4] = {0, 0, 0, 0}; - for (int unit = lane16; unit < 144; unit += 16) { - const int col = unit << 3; - float w[8]; - b70_gguf_decode_8_slm
(rw, col, ext_sc, w, s_g64, s_g32, s_sgn); - for (int i = 0; i < mm; ++i) { - const float* xp = h_act + (size_t)pid[i] * 1152 + col; - float d = 0.0f; -#pragma unroll - for (int t = 0; t < 8; ++t) d += w[t] * xp[t]; - acc[i] += d; + if (n <= 6) { + const int tot4 = n * 288; + for (int idx = tid; idx < tot4; idx += 256) { + const int ti = idx / 288; + const int rem = idx - ti * 288; + const int pid = pair_list[off + ti]; + sh4[(ti * 36 + (rem >> 3)) * 9 + (rem & 7)] = + reinterpret_cast(h_act + (size_t)pid * 1152)[rem]; + } + sycl::group_barrier(it.get_group()); + + for (int rp = rp_base; rp < 1152; rp += 576) { + const int row = 2 * rp + half_id; + const uint8_t* rw = body + ed.dn_off + (size_t)row * rb_1152; + float acc[6] = {}; + for (int unit = lane16; unit < 144; unit += 16) { + const int col = unit << 3; + float w[8]; + b70_gguf_decode_8_ptr
(rw, col, ext_sc, w, lut_ptr, iq_grids); + const int sub_off = (unit >> 2) * 36 + (unit & 3) * 8; + for (int i = 0; i < n; ++i) { + acc[i] += b70_dot8_f4x2(w, sh_ptr + i * (36 * 36) + sub_off); + } + } + for (int i = 0; i < n; ++i) { + const float v = b70_gguf_sg32_half_sum16(sg, acc[i]); + if (lane16 == 0) { + const int pid = pair_list[off + i]; + y_pairs[(size_t)pid * 2304 + row] = v * topk_w[pid]; + } } } - for (int i = 0; i < mm; ++i) { - const float v = b70_gguf_sg32_half_sum16(sg, acc[i]); - if (lane16 == 0) y_pairs[(size_t)pid[i] * 2304 + row] = v * topk_w[pid[i]]; + return; + } + + if constexpr (LUT_U32 > 0) { + sycl::group_barrier(it.get_group()); + } + + for (int t_base = 0; t_base < n; t_base += 16) { + const int mm = sycl::min(16, n - t_base); + const int unit_step = (mm <= 8) ? 96 : 48; // 24 or 12 blocks of 32 (both % 16 == 0!) + const int b32_stride = unit_step >> 2; + + for (int rp = rp_base; rp < 1152; rp += 576) { + const int row = 2 * rp + half_id; + const uint8_t* rw = body + ed.dn_off + (size_t)row * rb_1152; + float acc[16] = {}; + for (int u0 = 0; u0 < 144; u0 += unit_step) { + const int u_cnt = sycl::min(unit_step, 144 - u0); + const int b32_cnt = u_cnt >> 2; + const int f4_per_tok = b32_cnt * 8; + const int tot4 = mm * f4_per_tok; + const int col_base = u0 << 3; + for (int idx = tid; idx < tot4; idx += 256) { + const int ti = idx / f4_per_tok; + const int rem = idx - ti * f4_per_tok; + const int pid = pair_list[off + t_base + ti]; + sh4[(ti * b32_stride + (rem >> 3)) * 9 + (rem & 7)] = + reinterpret_cast(h_act + (size_t)pid * 1152 + col_base)[rem]; + } + sycl::group_barrier(it.get_group()); + + for (int sub_u = lane16; sub_u < u_cnt; sub_u += 16) { + const int col = col_base + (sub_u << 3); + const int sub_off = (sub_u >> 2) * 36 + (sub_u & 3) * 8; + float w[8]; + b70_gguf_decode_8_ptr
(rw, col, ext_sc, w, lut_ptr, iq_grids); + for (int i = 0; i < mm; ++i) { + acc[i] += b70_dot8_f4x2(w, sh_ptr + i * (b32_stride * 36) + sub_off); + } + } + sycl::group_barrier(it.get_group()); + } + for (int i = 0; i < mm; ++i) { + const float v = b70_gguf_sg32_half_sum16(sg, acc[i]); + if (lane16 == 0) { + const int pid = pair_list[off + t_base + i]; + y_pairs[(size_t)pid * 2304 + row] = v * topk_w[pid]; + } + } } } }); @@ -589,7 +773,8 @@ inline void k_b70_moe_dn_gguf_exact_sg32( } // ============================================================================ -// 3. Tier 2: Turbo Q8_1 + SG32 Half-Split dp4a_s8 (`dot_acc`) GGUF MoE (M <= 16) +// 3. Tier 2: Compact SoA Q8_1 SLM + Single-Unpack SG32 dp4a_s8 (`dot_acc`) MoE +// Eliminates 48B struct copies (0 register spill!) & unpacks weights ONCE across mm tokens! // ============================================================================ struct alignas(16) InkSyQ8_1_G32 { @@ -601,10 +786,8 @@ struct alignas(16) InkSyQ8_1_G32 { }; static_assert(sizeof(InkSyQ8_1_G32) == 48, "InkSyQ8_1_G32 size"); -// Quantize X [rows, K] (K multiple of 32) -> InkSyQ8_1_G32 [rows * (K/32)] -// 1 SG16 = 2 blocks of 32 floats (8 lanes per 32-block, each lane quantizes 4 contiguous floats -> 1 int32_t!) inline void k_b70_quant_q8_1_g32_sg16(const float* X, int total_blocks32, InkSyQ8_1_G32* out_q8) { - const int n_wg = (total_blocks32 + 31) / 32; // 256 threads = 16 SG16 = 32 blocks/WG + const int n_wg = (total_blocks32 + 31) / 32; Q().parallel_for(NR((size_t)n_wg, 256), [=](sycl::nd_item<1> it) INK_REQD_SG16 { auto sg = it.get_sub_group(); const int lane = (int)it.get_local_id(0) & 15; @@ -639,13 +822,13 @@ inline void k_b70_quant_q8_1_g32_sg16(const float* X, int total_blocks32, InkSyQ }); } -// Helper: unpack 4 weights at (b32, g4 in 0..7) and compute sub-block contribution with dp4a_s8! -// Returns per-lane float partial product (summing across all 16 lanes of half-SG32 yields the full row dot product!). -template -inline float b70_gguf_dp4a_step( - const sycl::sub_group& sg, const uint8_t* row, int b32, int g4, float ext_sc, - int32_t x_qs4, float x_d, float x_s, - const Acc64& s_g64, const Acc32& s_g32, const AccSign& s_sgn) { +// Unpack 4 weights at (b32, g4 in 0..7) ONCE into (w_s8x4, dl, zp_term) for reuse across all mm tokens! +template +inline void b70_gguf_unpack4_s8_ptr( + const uint8_t* row, int b32, int g4, float ext_sc, + int32_t& w_s8x4, float& dl, float& zp_term, + const uint32_t* lut_u32, const InkSyIqGridTable* iq_grids) { + zp_term = 0.0f; if constexpr (T == 21u) { // IQ3_S (110 B / 256) const uint8_t* b = row + (size_t)(b32 >> 3) * 110u; const float d = h2f_bits(ld16(b)) * ext_sc; @@ -654,99 +837,100 @@ inline float b70_gguf_dp4a_step( const uint32_t gi = (uint32_t)b[2 + sub * 8 + g4] | (((uint32_t)qh << (8 - g4)) & 256u); const uint8_t sn = (b[74 + sub * 4 + l4] >> (4 * odd)) & 0xFu; const uint8_t scb = b[106 + (sub >> 1)]; - const float dl = d * (float)(1 + 2 * (int)((scb >> (4 * (sub & 1))) & 0xFu)); - const auto u4 = sycl::bit_cast(s_g32[gi]); + dl = d * (float)(1 + 2 * (int)((scb >> (4 * (sub & 1))) & 0xFu)); + const auto u4 = sycl::bit_cast(lut_u32[gi]); const sycl::char4 w4( (sn & 1u) ? -(int8_t)u4[0] : (int8_t)u4[0], (sn & 2u) ? -(int8_t)u4[1] : (int8_t)u4[1], (sn & 4u) ? -(int8_t)u4[2] : (int8_t)u4[2], (sn & 8u) ? -(int8_t)u4[3] : (int8_t)u4[3]); - const int32_t dot4 = dp4a_s8(sycl::bit_cast(w4), x_qs4, 0); - return (float)dot4 * (dl * x_d); + w_s8x4 = sycl::bit_cast(w4); } else if constexpr (T == 22u) { // IQ2_S (82 B / 256) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); const uint8_t* b = row + (size_t)(b32 >> 3) * 82u; const float d = h2f_bits(ld16(b)) * ext_sc; const int sub = b32 & 7, l4 = g4 >> 1, odd = g4 & 1; const uint8_t qh = b[66 + sub], sc = b[74 + sub]; const uint32_t gi = (uint32_t)b[2 + sub * 4 + l4] | (((uint32_t)qh << (8 - 2 * l4)) & 0x300u); const uint8_t sn = (b[34 + sub * 4 + l4] >> (4 * odd)) & 0xFu; - const float dl = d * (0.5f + (float)((sc >> (4 * (l4 >> 1))) & 0xFu)) * 0.25f; - const uint32_t w32 = (uint32_t)(s_g64[gi] >> (32 * odd)); - const auto u4 = sycl::bit_cast(w32); + dl = d * (0.5f + (float)((sc >> (4 * (l4 >> 1))) & 0xFu)) * 0.25f; + const auto u4 = sycl::bit_cast((uint32_t)(s_g64[gi] >> (32 * odd))); const sycl::char4 w4( (sn & 1u) ? -(int8_t)u4[0] : (int8_t)u4[0], (sn & 2u) ? -(int8_t)u4[1] : (int8_t)u4[1], (sn & 4u) ? -(int8_t)u4[2] : (int8_t)u4[2], (sn & 8u) ? -(int8_t)u4[3] : (int8_t)u4[3]); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (dl * x_d); + w_s8x4 = sycl::bit_cast(w4); } else if constexpr (T == 17u) { // IQ2_XS (74 B / 256) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 1024); const uint8_t* b = row + (size_t)(b32 >> 3) * 74u; const float d = h2f_bits(ld16(b)) * ext_sc; const int sub = b32 & 7, l4 = g4 >> 1, odd = g4 & 1; const uint16_t qv = ld16(b + 2 + sub * 8 + l4 * 2); const uint8_t sc = b[66 + sub]; - const float dl = d * (0.5f + (float)((sc >> (4 * (l4 >> 1))) & 0xFu)) * 0.25f; + dl = d * (0.5f + (float)((sc >> (4 * (l4 >> 1))) & 0xFu)) * 0.25f; const uint8_t sn = (s_sgn[qv >> 9] >> (4 * odd)) & 0xFu; - const uint32_t w32 = (uint32_t)(s_g64[qv & 511u] >> (32 * odd)); - const auto u4 = sycl::bit_cast(w32); + const auto u4 = sycl::bit_cast((uint32_t)(s_g64[qv & 511u] >> (32 * odd))); const sycl::char4 w4( (sn & 1u) ? -(int8_t)u4[0] : (int8_t)u4[0], (sn & 2u) ? -(int8_t)u4[1] : (int8_t)u4[1], (sn & 4u) ? -(int8_t)u4[2] : (int8_t)u4[2], (sn & 8u) ? -(int8_t)u4[3] : (int8_t)u4[3]); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (dl * x_d); + w_s8x4 = sycl::bit_cast(w4); } else if constexpr (T == 16u) { // IQ2_XXS (66 B / 256) + const uint64_t* s_g64 = reinterpret_cast(lut_u32); + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 512); const uint8_t* b = row + (size_t)(b32 >> 3) * 66u; const float d = h2f_bits(ld16(b)) * ext_sc; const int sub = b32 & 7, l4 = g4 >> 1, odd = g4 & 1; const uint8_t* q = b + 2 + sub * 8; const uint32_t gi = sy_ld32(q), sw = sy_ld32(q + 4); - const float dl = d * (0.5f + (float)(sw >> 28)) * 0.25f; + dl = d * (0.5f + (float)(sw >> 28)) * 0.25f; const uint8_t sn = (s_sgn[(sw >> (7 * l4)) & 127u] >> (4 * odd)) & 0xFu; - const uint32_t w32 = (uint32_t)(s_g64[(gi >> (8 * l4)) & 0xFFu] >> (32 * odd)); - const auto u4 = sycl::bit_cast(w32); + const auto u4 = sycl::bit_cast((uint32_t)(s_g64[(gi >> (8 * l4)) & 0xFFu] >> (32 * odd))); const sycl::char4 w4( (sn & 1u) ? -(int8_t)u4[0] : (int8_t)u4[0], (sn & 2u) ? -(int8_t)u4[1] : (int8_t)u4[1], (sn & 4u) ? -(int8_t)u4[2] : (int8_t)u4[2], (sn & 8u) ? -(int8_t)u4[3] : (int8_t)u4[3]); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (dl * x_d); - } else if constexpr (T == 19u) { // IQ1_S (50 B / 256) + w_s8x4 = sycl::bit_cast(w4); + } else if constexpr (T == 19u) { // IQ1_S (50 B / 256; 16 KiB codebook L1-resident!) const uint8_t* b = row + (size_t)(b32 >> 3) * 50u; const float d = h2f_bits(ld16(b)) * ext_sc; const int sub = b32 & 7, l4 = g4 >> 1, odd = g4 & 1; const uint16_t qh = ld16(b + 34 + sub * 2); - const float dl = d * (float)(2 * (int)((qh >> 12) & 7u) + 1); + dl = d * (float)(2 * (int)((qh >> 12) & 7u) + 1); const float delta = (qh & 0x8000u) ? -0.125f : 0.125f; const uint32_t gi = (uint32_t)b[2 + sub * 4 + l4] | (((uint32_t)(qh >> (3 * l4)) & 7u) << 8); - const int32_t w_s8x4 = (int32_t)(uint32_t)(s_g64[gi] >> (32 * odd)); - const int32_t dot4 = dp4a_s8(w_s8x4, x_qs4, 0); - return dl * (x_d * (float)dot4 + ((g4 == 0) ? (delta * x_s) : 0.0f)); - } else if constexpr (T == 42u) { // Q2_0 (18 B / 64, 1 byte per lane = 4 x 2b weights!) + w_s8x4 = (int32_t)(uint32_t)(iq_grids->iq1s_grid[gi] >> (32 * odd)); + zp_term = dl * delta; + } else if constexpr (T == 42u) { // Q2_0 (18 B / 64) const uint8_t* b = row + (size_t)(b32 >> 1) * 18u; - const float d = h2f_bits(ld16(b)) * ext_sc; + dl = h2f_bits(ld16(b)) * ext_sc; const uint8_t qb = b[2 + (b32 & 1) * 8 + g4]; const sycl::char4 w4( (int8_t)((int)((qb >> 0) & 3u) - 1), (int8_t)((int)((qb >> 2) & 3u) - 1), (int8_t)((int)((qb >> 4) & 3u) - 1), (int8_t)((int)((qb >> 6) & 3u) - 1)); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (d * x_d); + w_s8x4 = sycl::bit_cast(w4); } else if constexpr (T == 18u) { // IQ3_XXS (98 B / 256) + const uint8_t* s_sgn = reinterpret_cast(lut_u32 + 256); const uint8_t* b = row + (size_t)(b32 >> 3) * 98u; const float d = h2f_bits(ld16(b)) * ext_sc; const int ib = b32 & 7, l4 = g4 >> 1, odd = g4 & 1; const uint32_t aux = sy_ld32(b + 66 + ib * 4); - const float db = d * (0.5f + (float)(aux >> 28)) * 0.5f; + dl = d * (0.5f + (float)(aux >> 28)) * 0.5f; const uint8_t sn = (s_sgn[(aux >> (7 * l4)) & 127u] >> (4 * odd)) & 0xFu; - const auto u4 = sycl::bit_cast(s_g32[b[2 + ib * 8 + g4]]); + const auto u4 = sycl::bit_cast(lut_u32[b[2 + ib * 8 + g4]]); const sycl::char4 w4( (sn & 1u) ? -(int8_t)u4[0] : (int8_t)u4[0], (sn & 2u) ? -(int8_t)u4[1] : (int8_t)u4[1], (sn & 4u) ? -(int8_t)u4[2] : (int8_t)u4[2], (sn & 8u) ? -(int8_t)u4[3] : (int8_t)u4[3]); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (db * x_d); - } else if constexpr (T == 12u) { // Q4_K (144 B / 256, UD-Q4_K_XL) + w_s8x4 = sycl::bit_cast(w4); + } else if constexpr (T == 12u) { // Q4_K (144 B / 256) const uint8_t* b = row + (size_t)(b32 >> 3) * 144u; const float d = h2f_bits(ld16(b)) * ext_sc; const float dmin = h2f_bits(ld16(b + 2)) * ext_sc; @@ -755,11 +939,10 @@ inline float b70_gguf_dp4a_step( uint32_t s, m; if (is < 4) { s = sc[is] & 63u; m = sc[is + 4] & 63u; } else { s = (sc[is + 4] & 0xFu) | ((sc[is - 4] >> 6) << 4); m = (sc[is + 4] >> 4) | ((sc[is] >> 6) << 4); } - const float d1 = d * (float)s, m1 = dmin * (float)m; - const uint32_t qw = (sy_ld32(b + 16 + g * 32 + g4 * 4) >> (is_hi ? 4 : 0)) & 0x0F0F0F0Fu; - const int32_t dot4 = dp4a_s8((int32_t)qw, x_qs4, 0); - return (float)dot4 * (d1 * x_d) - ((g4 == 0) ? (m1 * x_s) : 0.0f); - } else if constexpr (T == 13u) { // Q5_K (176 B / 256, UD-Q4_K_XL) + dl = d * (float)s; + zp_term = -(dmin * (float)m); + w_s8x4 = (int32_t)((sy_ld32(b + 16 + g * 32 + g4 * 4) >> (is_hi ? 4 : 0)) & 0x0F0F0F0Fu); + } else if constexpr (T == 13u) { // Q5_K (176 B / 256) const uint8_t* b = row + (size_t)(b32 >> 3) * 176u; const float d = h2f_bits(ld16(b)) * ext_sc; const float dmin = h2f_bits(ld16(b + 2)) * ext_sc; @@ -768,12 +951,12 @@ inline float b70_gguf_dp4a_step( uint32_t s, m; if (is < 4) { s = sc[is] & 63u; m = sc[is + 4] & 63u; } else { s = (sc[is + 4] & 0xFu) | ((sc[is - 4] >> 6) << 4); m = (sc[is + 4] >> 4) | ((sc[is] >> 6) << 4); } - const float d1 = d * (float)s, m1 = dmin * (float)m; + dl = d * (float)s; + zp_term = -(dmin * (float)m); const uint32_t ql = (sy_ld32(b + 48 + g * 32 + g4 * 4) >> (is_hi ? 4 : 0)) & 0x0F0F0F0Fu; const uint32_t qh = ((sy_ld32(b + 16 + g4 * 4) >> is) & 0x01010101u) << 4; - const int32_t dot4 = dp4a_s8((int32_t)(ql | qh), x_qs4, 0); - return (float)dot4 * (d1 * x_d) - ((g4 == 0) ? (m1 * x_s) : 0.0f); - } else if constexpr (T == 14u) { // Q6_K (210 B / 256, UD-Q4_K_XL) + w_s8x4 = (int32_t)(ql | qh); + } else if constexpr (T == 14u) { // Q6_K (210 B / 256) const uint8_t* b = row + (size_t)(b32 >> 3) * 210u; const float d = h2f_bits(ld16(b + 208)) * ext_sc; const int sub = b32 & 7, h = sub >> 2, q4 = sub & 3, l0 = g4 * 4; @@ -782,55 +965,55 @@ inline float b70_gguf_dp4a_step( const auto u4 = sycl::bit_cast(ql | qh); const sycl::char4 w4((int8_t)((int)u4[0] - 32), (int8_t)((int)u4[1] - 32), (int8_t)((int)u4[2] - 32), (int8_t)((int)u4[3] - 32)); - const float sc = d * (float)((int8_t)b[192 + h * 8 + q4 * 2 + (g4 >> 2)]); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (sc * x_d); - } else if constexpr (T == 23u) { // IQ4_XS (136 B / 256, UD-Q4_K_XL) + w_s8x4 = sycl::bit_cast(w4); + dl = d * (float)((int8_t)b[192 + h * 8 + q4 * 2 + (g4 >> 2)]); + } else if constexpr (T == 23u) { // IQ4_XS (136 B / 256) const uint8_t* b = row + (size_t)(b32 >> 3) * 136u; const float d = h2f_bits(ld16(b)) * ext_sc; const uint16_t sh = ld16(b + 2); const int ib = b32 & 7, l = g4 * 4; const int ls = (int)(((b[4 + (ib >> 1)] >> ((ib & 1) * 4)) & 0xFu) | (((sh >> (2 * ib)) & 3u) << 4)) - 32; - const float dl = d * (float)ls; + dl = d * (float)ls; const uint32_t qw = sy_ld32(b + 8 + ib * 16 + (l & 15)) >> ((l >= 16) ? 4 : 0); const sycl::char4 w4( b70_iq4_nl_i8(qw >> 0), b70_iq4_nl_i8(qw >> 8), b70_iq4_nl_i8(qw >> 16), b70_iq4_nl_i8(qw >> 24)); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (dl * x_d); - } else if constexpr (T == 20u) { // IQ4_NL (18 B / 32, UD-Q4_K_XL) + w_s8x4 = sycl::bit_cast(w4); + } else if constexpr (T == 20u) { // IQ4_NL (18 B / 32) const uint8_t* b = row + (size_t)b32 * 18u; - const float d = h2f_bits(ld16(b)) * ext_sc; + dl = h2f_bits(ld16(b)) * ext_sc; const int l = g4 * 4; const uint32_t qw = sy_ld32(b + 2 + (l & 15)) >> ((l >= 16) ? 4 : 0); const sycl::char4 w4( b70_iq4_nl_i8(qw >> 0), b70_iq4_nl_i8(qw >> 8), b70_iq4_nl_i8(qw >> 16), b70_iq4_nl_i8(qw >> 24)); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (d * x_d); - } else if constexpr (T == 8u) { // Q8_0 (34 B / 32, UD-Q4_K_XL) + w_s8x4 = sycl::bit_cast(w4); + } else if constexpr (T == 8u) { // Q8_0 (34 B / 32) const uint8_t* b = row + (size_t)b32 * 34u; - const float d = h2f_bits(ld16(b)) * ext_sc; - const int32_t qw = (int32_t)sy_ld32(b + 2 + g4 * 4); - return (float)dp4a_s8(qw, x_qs4, 0) * (d * x_d); + dl = h2f_bits(ld16(b)) * ext_sc; + w_s8x4 = (int32_t)sy_ld32(b + 2 + g4 * 4); } else if constexpr (T == 2u) { // Q4_0 (18 B / 32) const uint8_t* b = row + (size_t)b32 * 18u; - const float d = h2f_bits(ld16(b)) * ext_sc; + dl = h2f_bits(ld16(b)) * ext_sc; const int l = g4 * 4; const uint32_t qw = (sy_ld32(b + 2 + (l & 15)) >> ((l >= 16) ? 4 : 0)) & 0x0F0F0F0Fu; const auto u4 = sycl::bit_cast(qw); const sycl::char4 w4((int8_t)((int)u4[0] - 8), (int8_t)((int)u4[1] - 8), (int8_t)((int)u4[2] - 8), (int8_t)((int)u4[3] - 8)); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (d * x_d); - } else if constexpr (T == 39u) { // MXFP4 (17 B / 32: 2*e2m1 is exact int8!) + w_s8x4 = sycl::bit_cast(w4); + } else if constexpr (T == 39u) { // MXFP4 (17 B / 32) const uint8_t* b = row + (size_t)b32 * 17u; - const float d = ue8m0_to_f32(b[0]) * ext_sc; + dl = ue8m0_to_f32(b[0]) * ext_sc; const int l = g4 * 4; const uint32_t qw = sy_ld32(b + 1 + (l & 15)) >> ((l >= 16) ? 4 : 0); const sycl::char4 w4( b70_mxfp4_2x_i8(qw >> 0), b70_mxfp4_2x_i8(qw >> 8), b70_mxfp4_2x_i8(qw >> 16), b70_mxfp4_2x_i8(qw >> 24)); - return (float)dp4a_s8(sycl::bit_cast(w4), x_qs4, 0) * (d * x_d); + w_s8x4 = sycl::bit_cast(w4); + } else { + w_s8x4 = 0; dl = 0.0f; } - (void)sg; (void)s_g64; (void)s_g32; (void)s_sgn; (void)x_s; - return 0.0f; + (void)lut_u32; (void)iq_grids; } template @@ -839,16 +1022,17 @@ inline void k_b70_moe_gu_gguf_dp4a_sg32( const int* uexp, const int* uoff, const int* ucnt, const int* pair_list, const int* n_uniq_dev, int max_uniq, float* h_act, int M) { const InkSyIqGridTable* iq_grids = iq_grid_table_dev(); - constexpr size_t G64 = b70_gguf_grid_u64_len(); - constexpr size_t G32 = b70_gguf_grid_u32_len(); - constexpr size_t GSG = b70_gguf_needs_ksigns() ? 128 : 1; + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words(); + const size_t rb = sy_gguf_row_bytes(GT, 2304); if (M == 1) { + // SoA s_qs4[72*8] (2304 B) + s_ds[72] (576 B) = 2880 B (720 u32 = 2.81 KiB) + // + <=12.0 KiB LUT = <=14.81 KiB <= 16.00 KiB -> FULL 8 WGs/Xe2 core across ALL 15 GGUF types! + constexpr size_t QS_U32 = 72 * 8; + constexpr size_t DS_U32 = 72 * 2; + constexpr size_t TOT_U32 = QS_U32 + DS_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_xq(72, h); // 3.375 KiB in SLM! - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); + sycl::local_accessor sRaw(TOT_U32, h); h.parallel_for(NR2(32, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int y = (int)it.get_group(0); if (y >= n_uniq_dev[0]) return; @@ -859,15 +1043,23 @@ inline void k_b70_moe_gu_gguf_dp4a_sg32( const int sub_off = lane16 >> 3, g4 = lane16 & 7; const int r_base = (int)it.get_group(1) * 8 + sgi; - if (tid < 72) s_xq[tid] = xq_in[tid]; - b70_gguf_preload_slm_lut(tid, 256, iq_grids, s_g64, s_g32, s_sgn); + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + int32_t* s_qs4 = reinterpret_cast(raw_ptr); + sycl::float2* s_ds = reinterpret_cast(raw_ptr + QS_U32); + uint32_t* lut_ptr = raw_ptr + QS_U32 + DS_U32; + + for (int idx = tid; idx < 72 * 8; idx += 256) { + const int b = idx >> 3, c = idx & 7; + s_qs4[idx] = xq_in[b].qs4[c]; + if (c == 0) s_ds[b] = sycl::float2(xq_in[b].d, xq_in[b].s); + } + b70_gguf_preload_lut_ptr(tid, 256, iq_grids, lut_ptr); sycl::group_barrier(it.get_group()); const int e = uexp[y], off = uoff[y], n = ucnt[y]; const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(GT, 2304); const bool ilv = (hdr->flags & 1u) != 0; const float ext_sc = half_id ? hdr->up_scale : hdr->gate_scale; @@ -875,11 +1067,17 @@ inline void k_b70_moe_gu_gguf_dp4a_sg32( const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); const uint8_t* rw = body + 32 + (size_t)row_idx * rb; float acc = 0.0f; - // 72 blocks of 32 weights -> 36 iterations of 2 blocks (64 weights/step across 16 lanes!) for (int b0 = 0; b0 < 72; b0 += 2) { const int b32 = b0 + sub_off; - const InkSyQ8_1_G32 xb = s_xq[b32]; - acc += b70_gguf_dp4a_step(sg, rw, b32, g4, ext_sc, xb.qs4[g4], xb.d, xb.s, s_g64, s_g32, s_sgn); + int32_t w_s8x4; float dl, zp; + b70_gguf_unpack4_s8_ptr(rw, b32, g4, ext_sc, w_s8x4, dl, zp, lut_ptr, iq_grids); + const sycl::float2 ds = s_ds[b32]; + const int32_t xq = s_qs4[b32 * 8 + g4]; + float v = (float)dp4a_s8(w_s8x4, xq, 0) * (dl * ds.x()); + if constexpr (GT == 12u || GT == 13u || GT == 19u) { + if (g4 == 0) v += zp * ds.y(); + } + acc += v; } const float sum_h = b70_gguf_sg32_half_sum16(sg, acc); const float u_val = sycl::select_from_group(sg, sum_h, 16u); @@ -893,11 +1091,15 @@ inline void k_b70_moe_gu_gguf_dp4a_sg32( return; } + // M in 2..16: 5 * 72 blocks = 360 blocks (14,400 B = 14.06 KiB) + <=12 KiB LUT = <=26.06 KiB <= 32.00 KiB -> 4 WGs/core! + // Persistent 1-barrier SLM for ALL n <= 5 (~93% of verify experts!), 2-chunk K-tiled SLM (blk_step=18/36) for n=6..16! + constexpr size_t MAX_SLM_BLKS = 360; // 5 * 72 (also holds 10 * 36 or 16 * 22 -> 16 * 18 = 288!) + constexpr size_t QS_U32 = MAX_SLM_BLKS * 8; + constexpr size_t DS_U32 = MAX_SLM_BLKS * 2; + constexpr size_t TOT_U32 = QS_U32 + DS_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); - h.parallel_for(NR2(144, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { + sycl::local_accessor sRaw(TOT_U32, h); + h.parallel_for(NR2(72, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int y = (int)it.get_group(0); if (y >= n_uniq_dev[0]) return; const int tid = (int)it.get_local_id(1); @@ -905,38 +1107,107 @@ inline void k_b70_moe_gu_gguf_dp4a_sg32( const int sgi = tid >> 5, lid = tid & 31; const int half_id = lid >> 4, lane16 = lid & 15; const int sub_off = lane16 >> 3, g4 = lane16 & 7; - const int r = (int)it.get_group(1) * 8 + sgi; + const int r_base = (int)it.get_group(1) * 8 + sgi; - b70_gguf_preload_slm_lut(tid, 256, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); - if (r >= 1152) return; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + int32_t* s_qs4 = reinterpret_cast(raw_ptr); + sycl::float2* s_ds = reinterpret_cast(raw_ptr + QS_U32); + uint32_t* lut_ptr = raw_ptr + QS_U32 + DS_U32; + b70_gguf_preload_lut_ptr(tid, 256, iq_grids, lut_ptr); const int e = uexp[y], off = uoff[y], n = ucnt[y]; const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(GT, 2304); const bool ilv = (hdr->flags & 1u) != 0; const float ext_sc = half_id ? hdr->up_scale : hdr->gate_scale; - const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); - const uint8_t* rw = body + 32 + (size_t)row_idx * rb; - for (int t0 = 0; t0 < n; t0 += 4) { - const int mm = sycl::min(4, n - t0); - int pid[4], tk[4]; - for (int i = 0; i < mm; ++i) { pid[i] = pair_list[off + t0 + i]; tk[i] = pid[i] >> 3; } - float acc[4] = {0, 0, 0, 0}; - for (int b0 = 0; b0 < 72; b0 += 2) { - const int b32 = b0 + sub_off; - for (int i = 0; i < mm; ++i) { - const InkSyQ8_1_G32 xb = xq_in[(size_t)tk[i] * 72 + b32]; - acc[i] += b70_gguf_dp4a_step(sg, rw, b32, g4, ext_sc, xb.qs4[g4], xb.d, xb.s, s_g64, s_g32, s_sgn); + if (n <= 5) { + const int tot_q = n * 72 * 8; + for (int idx = tid; idx < tot_q; idx += 256) { + const int ti = idx / 576; + const int rem = idx - ti * 576; + const int b = rem >> 3, c = rem & 7; + const int tk = pair_list[off + ti] >> 3; + const InkSyQ8_1_G32& src = xq_in[(size_t)tk * 72 + b]; + s_qs4[idx] = src.qs4[c]; + if (c == 0) s_ds[ti * 72 + b] = sycl::float2(src.d, src.s); + } + sycl::group_barrier(it.get_group()); + + for (int r = r_base; r < 1152; r += 576) { + const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); + const uint8_t* rw = body + 32 + (size_t)row_idx * rb; + float acc[5] = {}; + for (int b0 = 0; b0 < 72; b0 += 2) { + const int b32 = b0 + sub_off; + int32_t w_s8x4; float dl, zp; + b70_gguf_unpack4_s8_ptr(rw, b32, g4, ext_sc, w_s8x4, dl, zp, lut_ptr, iq_grids); + for (int i = 0; i < n; ++i) { + const int b_slm = i * 72 + b32; + const sycl::float2 ds = s_ds[b_slm]; + const int32_t xq = s_qs4[b_slm * 8 + g4]; + float v = (float)dp4a_s8(w_s8x4, xq, 0) * (dl * ds.x()); + if constexpr (GT == 12u || GT == 13u || GT == 19u) { + if (g4 == 0) v += zp * ds.y(); + } + acc[i] += v; + } } + for (int i = 0; i < n; ++i) { + const float sum_h = b70_gguf_sg32_half_sum16(sg, acc[i]); + const float u_val = sycl::select_from_group(sg, sum_h, 16u); + if (lid == 0) h_act[(size_t)pair_list[off + i] * 1152 + r] = situ_act(sum_h, u_val); + } + } + return; + } + + if constexpr (LUT_U32 > 0) { + sycl::group_barrier(it.get_group()); + } + + const int mm = sycl::min(16, n); + const int blk_step = (mm <= 10) ? 36 : 18; // both even & exact divisors of 72! + for (int r = r_base; r < 1152; r += 576) { + const int row_idx = ilv ? (2 * r + half_id) : (half_id ? (1152 + r) : r); + const uint8_t* rw = body + 32 + (size_t)row_idx * rb; + float acc[16] = {}; + for (int blk_base = 0; blk_base < 72; blk_base += blk_step) { + const int q_per_tok = blk_step * 8; + const int tot_q = mm * q_per_tok; + for (int idx = tid; idx < tot_q; idx += 256) { + const int ti = idx / q_per_tok; + const int rem = idx - ti * q_per_tok; + const int b = rem >> 3, c = rem & 7; + const int tk = pair_list[off + ti] >> 3; + const InkSyQ8_1_G32& src = xq_in[(size_t)tk * 72 + blk_base + b]; + s_qs4[idx] = src.qs4[c]; + if (c == 0) s_ds[ti * blk_step + b] = sycl::float2(src.d, src.s); + } + sycl::group_barrier(it.get_group()); + + for (int sub_b = 0; sub_b < blk_step; sub_b += 2) { + const int b32 = blk_base + sub_b + sub_off; + int32_t w_s8x4; float dl, zp; + b70_gguf_unpack4_s8_ptr(rw, b32, g4, ext_sc, w_s8x4, dl, zp, lut_ptr, iq_grids); + for (int i = 0; i < mm; ++i) { + const int b_slm = i * blk_step + sub_b + sub_off; + const sycl::float2 ds = s_ds[b_slm]; + const int32_t xq = s_qs4[b_slm * 8 + g4]; + float v = (float)dp4a_s8(w_s8x4, xq, 0) * (dl * ds.x()); + if constexpr (GT == 12u || GT == 13u || GT == 19u) { + if (g4 == 0) v += zp * ds.y(); + } + acc[i] += v; + } + } + sycl::group_barrier(it.get_group()); } for (int i = 0; i < mm; ++i) { const float sum_h = b70_gguf_sg32_half_sum16(sg, acc[i]); const float u_val = sycl::select_from_group(sg, sum_h, 16u); - if (lid == 0) h_act[(size_t)pid[i] * 1152 + r] = situ_act(sum_h, u_val); + if (lid == 0) h_act[(size_t)pair_list[off + i] * 1152 + r] = situ_act(sum_h, u_val); } } }); @@ -949,16 +1220,17 @@ inline void k_b70_moe_dn_gguf_dp4a_sg32( const int* uexp, const int* uoff, const int* ucnt, const int* pair_list, const int* n_uniq_dev, int max_uniq, const InkSyQ8_1_G32* hq_in, float* y_pairs, int M) { const InkSyIqGridTable* iq_grids = iq_grid_table_dev(); - constexpr size_t G64 = b70_gguf_grid_u64_len
(); - constexpr size_t G32 = b70_gguf_grid_u32_len
(); - constexpr size_t GSG = b70_gguf_needs_ksigns
() ? 128 : 1; + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words
(); + const size_t rb = sy_gguf_row_bytes(DT, 1152); + if (rb == 0) return; if (M == 1) { + // 36 * 40 B = 1,440 B (1.41 KiB) -> 8 WGs/Xe2 core! + constexpr size_t QS_U32 = 36 * 8; + constexpr size_t DS_U32 = 36 * 2; + constexpr size_t TOT_U32 = QS_U32 + DS_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_hq(36, h); // 1.6875 KiB in SLM! - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); + sycl::local_accessor sRaw(TOT_U32, h); h.parallel_for(NR2(32, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int yy = (int)it.get_group(0); if (yy >= n_uniq_dev[0]) return; @@ -971,25 +1243,36 @@ inline void k_b70_moe_dn_gguf_dp4a_sg32( const int e = uexp[yy], off = uoff[yy], n = ucnt[yy]; const int pid0 = pair_list[off]; - if (tid < 36) s_hq[tid] = hq_in[(size_t)pid0 * 36 + tid]; - b70_gguf_preload_slm_lut
(tid, 256, iq_grids, s_g64, s_g32, s_sgn); + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + int32_t* s_qs4 = reinterpret_cast(raw_ptr); + sycl::float2* s_ds = reinterpret_cast(raw_ptr + QS_U32); + uint32_t* lut_ptr = raw_ptr + QS_U32 + DS_U32; + + for (int idx = tid; idx < 36 * 8; idx += 256) { + const int b = idx >> 3, c = idx & 7; + const InkSyQ8_1_G32& src = hq_in[(size_t)pid0 * 36 + b]; + s_qs4[idx] = src.qs4[c]; + if (c == 0) s_ds[b] = sycl::float2(src.d, src.s); + } + b70_gguf_preload_lut_ptr
(tid, 256, iq_grids, lut_ptr); sycl::group_barrier(it.get_group()); const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(DT, 1152); const float ext_sc = hdr->dn_scale; for (int rp = rp_base; rp < 1152; rp += 256) { const int row = 2 * rp + half_id; const uint8_t* rw = body + ed.dn_off + (size_t)row * rb; float acc = 0.0f; - // 36 blocks of 32 weights -> 18 iterations of 2 blocks! for (int b0 = 0; b0 < 36; b0 += 2) { const int b32 = b0 + sub_off; - const InkSyQ8_1_G32 hb = s_hq[b32]; - acc += b70_gguf_dp4a_step
(sg, rw, b32, g4, ext_sc, hb.qs4[g4], hb.d, hb.s, s_g64, s_g32, s_sgn); + int32_t w_s8x4; float dl, zp; + b70_gguf_unpack4_s8_ptr
(rw, b32, g4, ext_sc, w_s8x4, dl, zp, lut_ptr, iq_grids); + const sycl::float2 ds = s_ds[b32]; + const int32_t xq = s_qs4[b32 * 8 + g4]; + acc += (float)dp4a_s8(w_s8x4, xq, 0) * (dl * ds.x()); } const float v = b70_gguf_sg32_half_sum16(sg, acc); if (lane16 == 0) { @@ -1004,11 +1287,15 @@ inline void k_b70_moe_dn_gguf_dp4a_sg32( return; } + // M in 2..16: ALL 16 tokens * 36 blocks * 40 B = 23,040 B (22.50 KiB <= 32.00 KiB -> 4 WGs/core!) + // ZERO K-tiling needed even at n = 16 -> 1 SINGLE BARRIER TOTAL for ANY n in 1..16! + constexpr size_t MAX_SLM_BLKS = 16 * 36; + constexpr size_t QS_U32 = MAX_SLM_BLKS * 8; + constexpr size_t DS_U32 = MAX_SLM_BLKS * 2; + constexpr size_t TOT_U32 = QS_U32 + DS_U32 + LUT_U32; Q().submit([&](sycl::handler& h) { - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); - h.parallel_for(NR2(144, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { + sycl::local_accessor sRaw(TOT_U32, h); + h.parallel_for(NR2(72, (size_t)max_uniq, 256), [=](sycl::nd_item<2> it) INK_REQD_SG32 { const int yy = (int)it.get_group(0); if (yy >= n_uniq_dev[0]) return; const int tid = (int)it.get_local_id(1); @@ -1016,36 +1303,54 @@ inline void k_b70_moe_dn_gguf_dp4a_sg32( const int sgi = tid >> 5, lid = tid & 31; const int half_id = lid >> 4, lane16 = lid & 15; const int sub_off = lane16 >> 3, g4 = lane16 & 7; - const int rp = (int)it.get_group(1) * 8 + sgi; + const int rp_base = (int)it.get_group(1) * 8 + sgi; - b70_gguf_preload_slm_lut
(tid, 256, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); - if (rp >= 1152) return; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + int32_t* s_qs4 = reinterpret_cast(raw_ptr); + sycl::float2* s_ds = reinterpret_cast(raw_ptr + QS_U32); + uint32_t* lut_ptr = raw_ptr + QS_U32 + DS_U32; + b70_gguf_preload_lut_ptr
(tid, 256, iq_grids, lut_ptr); const int e = uexp[yy], off = uoff[yy], n = ucnt[yy]; const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(DT, 1152); const float ext_sc = hdr->dn_scale; - const int row = 2 * rp + half_id; - const uint8_t* rw = body + ed.dn_off + (size_t)row * rb; - for (int t0 = 0; t0 < n; t0 += 4) { - const int mm = sycl::min(4, n - t0); - int pid[4]; - for (int i = 0; i < mm; ++i) pid[i] = pair_list[off + t0 + i]; - float acc[4] = {0, 0, 0, 0}; + const int mm = sycl::min(16, n); + const int tot_q = mm * 36 * 8; + for (int idx = tid; idx < tot_q; idx += 256) { + const int ti = idx / 288; + const int rem = idx - ti * 288; + const int b = rem >> 3, c = rem & 7; + const int pid = pair_list[off + ti]; + const InkSyQ8_1_G32& src = hq_in[(size_t)pid * 36 + b]; + s_qs4[idx] = src.qs4[c]; + if (c == 0) s_ds[ti * 36 + b] = sycl::float2(src.d, src.s); + } + sycl::group_barrier(it.get_group()); + + for (int rp = rp_base; rp < 1152; rp += 576) { + const int row = 2 * rp + half_id; + const uint8_t* rw = body + ed.dn_off + (size_t)row * rb; + float acc[16] = {}; for (int b0 = 0; b0 < 36; b0 += 2) { const int b32 = b0 + sub_off; + int32_t w_s8x4; float dl, zp; + b70_gguf_unpack4_s8_ptr
(rw, b32, g4, ext_sc, w_s8x4, dl, zp, lut_ptr, iq_grids); for (int i = 0; i < mm; ++i) { - const InkSyQ8_1_G32 hb = hq_in[(size_t)pid[i] * 36 + b32]; - acc[i] += b70_gguf_dp4a_step
(sg, rw, b32, g4, ext_sc, hb.qs4[g4], hb.d, hb.s, s_g64, s_g32, s_sgn); + const int b_slm = i * 36 + b32; + const sycl::float2 ds = s_ds[b_slm]; + const int32_t xq = s_qs4[b_slm * 8 + g4]; + acc[i] += (float)dp4a_s8(w_s8x4, xq, 0) * (dl * ds.x()); } } for (int i = 0; i < mm; ++i) { const float v = b70_gguf_sg32_half_sum16(sg, acc[i]); - if (lane16 == 0) y_pairs[(size_t)pid[i] * 2304 + row] = v * topk_w[pid[i]]; + if (lane16 == 0) { + const int pid = pair_list[off + i]; + y_pairs[(size_t)pid * 2304 + row] = v * topk_w[pid]; + } } } }); @@ -1053,11 +1358,530 @@ inline void k_b70_moe_dn_gguf_dp4a_sg32( } // ============================================================================ -// 4. Tier 3: Native Xe2 VNNI-2 `joint_matrix` FP16 XMX DPAS GGUF Prefill (M >= 16) -// Explicit lambda captures + SLM Codebook Preload + VNNI-2 sB[8 * 136] (stride 136)! +// 4. Tier 3: Flat-Worklist BM=128, BN=128, BK=32 Unioned-SLM XMX GGUF Prefill! +// - NR2(18, max_xm_tiles, 256): ZERO Zipf hot-expert serial t0+=32 stalls! +// - 4x fewer GGUF weight unpacks (128-token reuse vs 32-token) & 2x fewer x_in gathers! +// - Unioned sRaw (16.25 KiB) + LUT (<= 12.00 KiB) = <= 28.25 KiB <= 32.00 KiB -> 4 WGs/Xe2 core! +// - Contiguous sorted h_act[(off + tr) * 1152] + optional HALF_X / HALF_INTER / HALF_YP (--turbo)! // ============================================================================ #if INK_SY_HAS_JOINT_MATRIX +template +inline void k_b70_jm_moe_gu_gguf_bm128_sg16( + sycl::queue& q, + const InkSyExpertDev* ex_layer, const void* x_in_raw, + const InkSyMoeBuckets* bk, const int* pair_list, int max_xm_tiles, + void* h_act_raw, const InkSyIqGridTable* iq_grids) { + namespace jm = sycl::ext::oneapi::experimental::matrix; + constexpr int LD_SB128 = 264; // 528B stride -> 0 SLM bank conflicts! + constexpr size_t BASE_U32 = 4160; // 16,640 B = 16.25 KiB (64B-aligned!) + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words(); + const size_t rb = sy_gguf_row_bytes(GT, 2304); + + q.submit([&](sycl::handler& h) { + sycl::local_accessor sRaw(BASE_U32 + LUT_U32, h); + h.parallel_for( + NR2(18, (size_t)max_xm_tiles, 256), + [bk, ex_layer, pair_list, x_in_raw, h_act_raw, iq_grids, rb, sRaw](sycl::nd_item<2> it) INK_REQD_SG16 { + const int tile_id = (int)it.get_group(0); + if (tile_id >= bk->n_xm_tiles) return; + const int bn = (int)it.get_group(1); // 0..17 -> 128 rows (64 gate+up inter channels) + const int tid = (int)it.get_local_id(1); + const int sgi = tid >> 4, lane = tid & 15; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + half* sA = reinterpret_cast(raw_ptr); + half* sB = sA + 4096; + uint32_t* lut_ptr = raw_ptr + BASE_U32; + auto sA_mp = sycl::address_space_cast(sA); + auto sB_mp = sycl::address_space_cast(sB); + auto sC_mp = sycl::address_space_cast( + reinterpret_cast(raw_ptr)); + (void)sA_mp; (void)sB_mp; (void)sC_mp; (void)lane; + + b70_gguf_preload_lut_ptr(tid, 256, iq_grids, lut_ptr); + if constexpr (LUT_U32 > 0) { + sycl::group_barrier(it.get_group()); + } + +#if INK_SY_DEV_JM_SG16 + auto sg = it.get_sub_group(); + const int sg_m = sgi >> 2, sg_n = sgi & 3; + jm::joint_matrix c0_0, c1_0, c0_1, c1_1, c0_2, c1_2, c0_3, c1_3; + jm::joint_matrix a0, a1; + jm::joint_matrix b0, b1; + jm::joint_matrix_fill(sg, c0_0, 0.0f); jm::joint_matrix_fill(sg, c1_0, 0.0f); + jm::joint_matrix_fill(sg, c0_1, 0.0f); jm::joint_matrix_fill(sg, c1_1, 0.0f); + jm::joint_matrix_fill(sg, c0_2, 0.0f); jm::joint_matrix_fill(sg, c1_2, 0.0f); + jm::joint_matrix_fill(sg, c0_3, 0.0f); jm::joint_matrix_fill(sg, c1_3, 0.0f); +#endif + const int e = bk->xm_tile_exp[tile_id]; + const int off = bk->xm_tile_off[tile_id]; + const int tm = bk->xm_tile_cnt[tile_id]; + const InkSyExpertDev ed = ex_layer[e]; + const uint8_t* body = ed.body_ptr; + const bool is_sk_dn = (ed.dn_code != FMT_SF4 && ed.dn_code != FMT_MX8 && ed.dn_code != FMT_GGUF_RAW); + const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); + const bool ilv = (hdr->flags & 1u) != 0; + + const int nr = tid >> 1; // 0..127 (even = gate, odd = up) + const int ks_w = tid & 1; // 0 or 1 + const int inter_w = bn * 64 + (nr >> 1); + const int is_up = nr & 1; + const int row_idx = ilv ? (2 * inter_w + is_up) : (is_up ? (1152 + inter_w) : inter_w); + const uint8_t* rw = body + 32 + (size_t)row_idx * rb; + + if constexpr (!HALF_X && !HALF_INTER) { + // Lossless Exact 2-Term BK=16 XMX (S_A=4, S_B=64 -> 21-bit mantissa, 0 extra SLM, 0 extra GRFs!) + constexpr float S_A = 4.0f; + constexpr float S_B = 64.0f; + const int hi8 = ks_w * 8; + half* sA_hi = sA; + half* sA_lo = sA + 2048; + half* sB_hi = sB; + half* sB_lo = sB + 8 * LD_SB128; + auto sA_hi_mp = sA_mp; + auto sA_lo_mp = sA_mp + 2048; + auto sB_hi_mp = sB_mp; + auto sB_lo_mp = sB_mp + 8 * LD_SB128; + (void)sA_hi_mp; (void)sA_lo_mp; (void)sB_hi_mp; (void)sB_lo_mp; + + for (int k0 = 0; k0 < 2304; k0 += 16) { +#pragma unroll + for (int p = 0; p < 2; ++p) { + const int idx4 = p * 256 + tid; + const int r = idx4 >> 2, k4 = (idx4 & 3) * 4; + sycl::float4 xv(0.0f); + if (r < tm) { + const int tk = pair_list[off + r] >> 3; + xv = *reinterpret_cast( + reinterpret_cast(x_in_raw) + (size_t)tk * 2304 + k0 + k4) * S_A; + } + const sycl::half4 xh((half)xv.x(), (half)xv.y(), (half)xv.z(), (half)xv.w()); + const sycl::half4 xl( + (sycl::fabs(xv.x()) < 65500.0f) ? (half)(xv.x() - (float)xh.x()) : (half)0.0f, + (sycl::fabs(xv.y()) < 65500.0f) ? (half)(xv.y() - (float)xh.y()) : (half)0.0f, + (sycl::fabs(xv.z()) < 65500.0f) ? (half)(xv.z() - (float)xh.z()) : (half)0.0f, + (sycl::fabs(xv.w()) < 65500.0f) ? (half)(xv.w() - (float)xh.w()) : (half)0.0f); + *reinterpret_cast(&sA_hi[r * 16 + k4]) = xh; + *reinterpret_cast(&sA_lo[r * 16 + k4]) = xl; + } + + float w8[8]; + b70_gguf_decode_8_ptr(rw, k0 + hi8, 1.0f, w8, lut_ptr, iq_grids); +#pragma unroll + for (int j = 0; j < 4; ++j) { + const float wf0 = w8[2 * j] * S_B; + const float wf1 = w8[2 * j + 1] * S_B; + const sycl::half2 wh((half)wf0, (half)wf1); + const sycl::half2 wl( + (sycl::fabs(wf0) < 65500.0f) ? (half)(wf0 - (float)wh.x()) : (half)0.0f, + (sycl::fabs(wf1) < 65500.0f) ? (half)(wf1 - (float)wh.y()) : (half)0.0f); + const int boff = ((hi8 >> 1) + j) * LD_SB128 + nr * 2; + *reinterpret_cast(&sB_hi[boff]) = wh; + *reinterpret_cast(&sB_lo[boff]) = wl; + } + sycl::group_barrier(it.get_group()); + +#if INK_SY_DEV_JM_SG16 + auto pBh = sB_hi_mp + sg_n * 64; + auto pBl = sB_lo_mp + sg_n * 64; + auto pAh = sA_hi_mp + (sg_m * 32) * 16; + auto pAl = sA_lo_mp + (sg_m * 32) * 16; + jm::joint_matrix_load(sg, b0, pBh, LD_SB128); + jm::joint_matrix_load(sg, b1, pBh + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pAh + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pAl + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAl + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + + jm::joint_matrix_load(sg, a0, pAh + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + jm::joint_matrix_load(sg, a0, pAl + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAl + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + + jm::joint_matrix_load(sg, b0, pBl, LD_SB128); + jm::joint_matrix_load(sg, b1, pBl + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pAh + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pAh + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); +#endif + sycl::group_barrier(it.get_group()); + } + } else { + half* sB_col = &sB[ks_w * 8 * LD_SB128 + nr * 2]; + for (int k0 = 0; k0 < 2304; k0 += 32) { +#pragma unroll + for (int p = 0; p < 4; ++p) { + const int idx4 = p * 256 + tid; + const int r = idx4 >> 3, k4 = (idx4 & 7) * 4; + if constexpr (HALF_X) { + sycl::half4 xh((half)0.0f); + if (r < tm) { + const int tk = pair_list[off + r] >> 3; + xh = *reinterpret_cast( + reinterpret_cast(x_in_raw) + (size_t)tk * 2304 + k0 + k4); + } + *reinterpret_cast(&sA[r * 32 + k4]) = xh; + } else { + sycl::float4 xv(0.0f); + if (r < tm) { + const int tk = pair_list[off + r] >> 3; + xv = *reinterpret_cast( + reinterpret_cast(x_in_raw) + (size_t)tk * 2304 + k0 + k4); + } + *reinterpret_cast(&sA[r * 32 + k4]) = + sycl::half4((half)xv.x(), (half)xv.y(), (half)xv.z(), (half)xv.w()); + } + } + + b70_gguf_decode_16_to_sB(rw, k0 + ks_w * 16, 1.0f, sB_col, lut_ptr, iq_grids); + sycl::group_barrier(it.get_group()); + +#if INK_SY_DEV_JM_SG16 +#pragma unroll + for (int ks = 0; ks < 2; ++ks) { + auto pB = sB_mp + ks * 8 * LD_SB128 + sg_n * 64; + auto pA_base = sA_mp + (sg_m * 32) * 32 + ks * 16; + jm::joint_matrix_load(sg, b0, pB, LD_SB128); + jm::joint_matrix_load(sg, b1, pB + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pA_base + 0 * 32, 32); + jm::joint_matrix_load(sg, a1, pA_base + 8 * 32, 32); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pA_base + 16 * 32, 32); + jm::joint_matrix_load(sg, a1, pA_base + 24 * 32, 32); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + } +#endif + sycl::group_barrier(it.get_group()); + } + } + +#if INK_SY_DEV_JM_SG16 + auto pS = sC_mp + sgi * 256; + const int inter = bn * 64 + sg_n * 16 + lane; + const float inv_sq = is_sk_dn ? (1.0f / h2f_bits(ld16(body + 2 * inter))) : 1.0f; + constexpr float POST_SC = (!HALF_X && !HALF_INTER) ? (1.0f / 256.0f) : 1.0f; + const float g_post = hdr->gate_scale * POST_SC; + const float u_post = hdr->up_scale * POST_SC; +#define INK_B70_GGUF_GU_STORE_SUB(MSUB, C0M, C1M) \ + do { \ + jm::joint_matrix_store(sg, C0M, pS, 32, jm::layout::row_major); \ + jm::joint_matrix_store(sg, C1M, pS + 16, 32, jm::layout::row_major); \ + sycl::group_barrier(sg); \ + for (int r = 0; r < 8; ++r) { \ + const int tr = sg_m * 32 + (MSUB) * 8 + r; \ + if (tr < tm) { \ + const float g = pS[r * 32 + 2 * lane] * g_post; \ + const float u = pS[r * 32 + 2 * lane + 1] * u_post; \ + const float act = situ_act(g, u) * inv_sq; \ + if constexpr (HALF_INTER) reinterpret_cast(h_act_raw)[(size_t)(off + tr) * 1152 + inter] = (half)act; \ + else reinterpret_cast(h_act_raw)[(size_t)(off + tr) * 1152 + inter] = act; \ + } \ + } \ + sycl::group_barrier(sg); \ + } while (0) + INK_B70_GGUF_GU_STORE_SUB(0, c0_0, c1_0); + INK_B70_GGUF_GU_STORE_SUB(1, c0_1, c1_1); + INK_B70_GGUF_GU_STORE_SUB(2, c0_2, c1_2); + INK_B70_GGUF_GU_STORE_SUB(3, c0_3, c1_3); +#undef INK_B70_GGUF_GU_STORE_SUB +#else + (void)h_act_raw; (void)is_sk_dn; +#endif + }); + }); +} + +template +inline void k_b70_jm_moe_dn_gguf_bm128_sg16( + sycl::queue& q, + const InkSyExpertDev* ex_layer, const float* topk_w, + const InkSyMoeBuckets* bk, const int* pair_list, int max_xm_tiles, + const void* h_act_raw, void* y_pairs_raw, const InkSyIqGridTable* iq_grids) { + namespace jm = sycl::ext::oneapi::experimental::matrix; + constexpr int LD_SB128 = 264; + constexpr size_t BASE_U32 = 4160; + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words
(); + const size_t rb = sy_gguf_row_bytes(DT, 1152); + + q.submit([&](sycl::handler& h) { + sycl::local_accessor sRaw(BASE_U32 + LUT_U32, h); + h.parallel_for( + NR2(18, (size_t)max_xm_tiles, 256), + [bk, ex_layer, pair_list, topk_w, h_act_raw, y_pairs_raw, iq_grids, rb, sRaw](sycl::nd_item<2> it) INK_REQD_SG16 { + const int tile_id = (int)it.get_group(0); + if (tile_id >= bk->n_xm_tiles) return; + const int bn = (int)it.get_group(1); // 0..17 -> 128 output cols each = 2304 + const int tid = (int)it.get_local_id(1); + const int sgi = tid >> 4, lane = tid & 15; + uint32_t* raw_ptr = sRaw.template get_multi_ptr().get(); + half* sA = reinterpret_cast(raw_ptr); + half* sB = sA + 4096; + uint32_t* lut_ptr = raw_ptr + BASE_U32; + auto sA_mp = sycl::address_space_cast(sA); + auto sB_mp = sycl::address_space_cast(sB); + auto sC_mp = sycl::address_space_cast( + reinterpret_cast(raw_ptr)); + (void)sA_mp; (void)sB_mp; (void)sC_mp; (void)lane; + + b70_gguf_preload_lut_ptr
(tid, 256, iq_grids, lut_ptr); + if constexpr (LUT_U32 > 0) { + sycl::group_barrier(it.get_group()); + } + +#if INK_SY_DEV_JM_SG16 + auto sg = it.get_sub_group(); + const int sg_m = sgi >> 2, sg_n = sgi & 3; + jm::joint_matrix c0_0, c1_0, c0_1, c1_1, c0_2, c1_2, c0_3, c1_3; + jm::joint_matrix a0, a1; + jm::joint_matrix b0, b1; + jm::joint_matrix_fill(sg, c0_0, 0.0f); jm::joint_matrix_fill(sg, c1_0, 0.0f); + jm::joint_matrix_fill(sg, c0_1, 0.0f); jm::joint_matrix_fill(sg, c1_1, 0.0f); + jm::joint_matrix_fill(sg, c0_2, 0.0f); jm::joint_matrix_fill(sg, c1_2, 0.0f); + jm::joint_matrix_fill(sg, c0_3, 0.0f); jm::joint_matrix_fill(sg, c1_3, 0.0f); +#endif + const int e = bk->xm_tile_exp[tile_id]; + const int off = bk->xm_tile_off[tile_id]; + const int tm = bk->xm_tile_cnt[tile_id]; + const InkSyExpertDev ed = ex_layer[e]; + const uint8_t* body = ed.body_ptr; + const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); + const int row0 = bn * 128; + const int nr = tid >> 1; // 0..127 + const int ks_w = tid & 1; // 0 or 1 + const int gr = row0 + nr; + const uint8_t* rw = body + ed.dn_off + (size_t)gr * rb; + + if constexpr (!HALF_INTER && !HALF_YP) { + // Lossless Exact 2-Term BK=16 XMX (S_A=4, S_B=64 -> 21-bit mantissa, 0 extra SLM, 0 extra GRFs!) + constexpr float S_A = 4.0f; + constexpr float S_B = 64.0f; + const int hi8 = ks_w * 8; + half* sA_hi = sA; + half* sA_lo = sA + 2048; + half* sB_hi = sB; + half* sB_lo = sB + 8 * LD_SB128; + auto sA_hi_mp = sA_mp; + auto sA_lo_mp = sA_mp + 2048; + auto sB_hi_mp = sB_mp; + auto sB_lo_mp = sB_mp + 8 * LD_SB128; + (void)sA_hi_mp; (void)sA_lo_mp; (void)sB_hi_mp; (void)sB_lo_mp; + + for (int k0 = 0; k0 < 1152; k0 += 16) { +#pragma unroll + for (int p = 0; p < 2; ++p) { + const int idx4 = p * 256 + tid; + const int r = idx4 >> 2, k4 = (idx4 & 3) * 4; + sycl::float4 xv(0.0f); + if (r < tm) { + xv = *reinterpret_cast( + reinterpret_cast(h_act_raw) + (size_t)(off + r) * 1152 + k0 + k4) * S_A; + } + const sycl::half4 xh((half)xv.x(), (half)xv.y(), (half)xv.z(), (half)xv.w()); + const sycl::half4 xl( + (sycl::fabs(xv.x()) < 65500.0f) ? (half)(xv.x() - (float)xh.x()) : (half)0.0f, + (sycl::fabs(xv.y()) < 65500.0f) ? (half)(xv.y() - (float)xh.y()) : (half)0.0f, + (sycl::fabs(xv.z()) < 65500.0f) ? (half)(xv.z() - (float)xh.z()) : (half)0.0f, + (sycl::fabs(xv.w()) < 65500.0f) ? (half)(xv.w() - (float)xh.w()) : (half)0.0f); + *reinterpret_cast(&sA_hi[r * 16 + k4]) = xh; + *reinterpret_cast(&sA_lo[r * 16 + k4]) = xl; + } + + float w8[8]; + b70_gguf_decode_8_ptr
(rw, k0 + hi8, 1.0f, w8, lut_ptr, iq_grids); +#pragma unroll + for (int j = 0; j < 4; ++j) { + const float wf0 = w8[2 * j] * S_B; + const float wf1 = w8[2 * j + 1] * S_B; + const sycl::half2 wh((half)wf0, (half)wf1); + const sycl::half2 wl( + (sycl::fabs(wf0) < 65500.0f) ? (half)(wf0 - (float)wh.x()) : (half)0.0f, + (sycl::fabs(wf1) < 65500.0f) ? (half)(wf1 - (float)wh.y()) : (half)0.0f); + const int boff = ((hi8 >> 1) + j) * LD_SB128 + nr * 2; + *reinterpret_cast(&sB_hi[boff]) = wh; + *reinterpret_cast(&sB_lo[boff]) = wl; + } + sycl::group_barrier(it.get_group()); + +#if INK_SY_DEV_JM_SG16 + auto pBh = sB_hi_mp + sg_n * 64; + auto pBl = sB_lo_mp + sg_n * 64; + auto pAh = sA_hi_mp + (sg_m * 32) * 16; + auto pAl = sA_lo_mp + (sg_m * 32) * 16; + jm::joint_matrix_load(sg, b0, pBh, LD_SB128); + jm::joint_matrix_load(sg, b1, pBh + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pAh + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pAl + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAl + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + + jm::joint_matrix_load(sg, a0, pAh + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + jm::joint_matrix_load(sg, a0, pAl + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAl + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + + jm::joint_matrix_load(sg, b0, pBl, LD_SB128); + jm::joint_matrix_load(sg, b1, pBl + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pAh + 0 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 8 * 16, 16); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pAh + 16 * 16, 16); + jm::joint_matrix_load(sg, a1, pAh + 24 * 16, 16); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); +#endif + sycl::group_barrier(it.get_group()); + } + } else { + half* sB_col = &sB[ks_w * 8 * LD_SB128 + nr * 2]; + for (int k0 = 0; k0 < 1152; k0 += 32) { +#pragma unroll + for (int p = 0; p < 4; ++p) { + const int idx4 = p * 256 + tid; + const int r = idx4 >> 3, k4 = (idx4 & 7) * 4; + if constexpr (HALF_INTER) { + sycl::half4 hv((half)0.0f); + if (r < tm) { + hv = *reinterpret_cast( + reinterpret_cast(h_act_raw) + (size_t)(off + r) * 1152 + k0 + k4); + } + *reinterpret_cast(&sA[r * 32 + k4]) = hv; + } else { + sycl::float4 xv(0.0f); + if (r < tm) { + xv = *reinterpret_cast( + reinterpret_cast(h_act_raw) + (size_t)(off + r) * 1152 + k0 + k4); + } + *reinterpret_cast(&sA[r * 32 + k4]) = + sycl::half4((half)xv.x(), (half)xv.y(), (half)xv.z(), (half)xv.w()); + } + } + + b70_gguf_decode_16_to_sB(rw, k0 + ks_w * 16, 1.0f, sB_col, lut_ptr, iq_grids); + sycl::group_barrier(it.get_group()); + +#if INK_SY_DEV_JM_SG16 +#pragma unroll + for (int ks = 0; ks < 2; ++ks) { + auto pB = sB_mp + ks * 8 * LD_SB128 + sg_n * 64; + auto pA_base = sA_mp + (sg_m * 32) * 32 + ks * 16; + jm::joint_matrix_load(sg, b0, pB, LD_SB128); + jm::joint_matrix_load(sg, b1, pB + 32, LD_SB128); + jm::joint_matrix_load(sg, a0, pA_base + 0 * 32, 32); + jm::joint_matrix_load(sg, a1, pA_base + 8 * 32, 32); + jm::joint_matrix_mad(sg, c0_0, a0, b0, c0_0); + jm::joint_matrix_mad(sg, c1_0, a0, b1, c1_0); + jm::joint_matrix_mad(sg, c0_1, a1, b0, c0_1); + jm::joint_matrix_mad(sg, c1_1, a1, b1, c1_1); + jm::joint_matrix_load(sg, a0, pA_base + 16 * 32, 32); + jm::joint_matrix_load(sg, a1, pA_base + 24 * 32, 32); + jm::joint_matrix_mad(sg, c0_2, a0, b0, c0_2); + jm::joint_matrix_mad(sg, c1_2, a0, b1, c1_2); + jm::joint_matrix_mad(sg, c0_3, a1, b0, c0_3); + jm::joint_matrix_mad(sg, c1_3, a1, b1, c1_3); + } +#endif + sycl::group_barrier(it.get_group()); + } + } + +#if INK_SY_DEV_JM_SG16 + auto pS = sC_mp + sgi * 256; + const int c0_col = row0 + sg_n * 32 + lane; + const int c1_col = c0_col + 16; + constexpr float POST_SC = (!HALF_INTER && !HALF_YP) ? (1.0f / 256.0f) : 1.0f; + const float d_post = hdr->dn_scale * POST_SC; +#define INK_B70_GGUF_DN_STORE_SUB(MSUB, C0M, C1M) \ + do { \ + jm::joint_matrix_store(sg, C0M, pS, 32, jm::layout::row_major); \ + jm::joint_matrix_store(sg, C1M, pS + 16, 32, jm::layout::row_major); \ + sycl::group_barrier(sg); \ + for (int r = 0; r < 8; ++r) { \ + const int tr = sg_m * 32 + (MSUB) * 8 + r; \ + if (tr < tm) { \ + const int pid = pair_list[off + tr]; \ + const float rw_tok = topk_w[pid] * d_post; \ + const float v0 = pS[r * 32 + lane] * rw_tok; \ + const float v1 = pS[r * 32 + 16 + lane] * rw_tok; \ + if constexpr (HALF_YP) { \ + half* yp = reinterpret_cast(y_pairs_raw) + (size_t)pid * 2304; \ + yp[c0_col] = (half)v0; yp[c1_col] = (half)v1; \ + } else { \ + float* yp = reinterpret_cast(y_pairs_raw) + (size_t)pid * 2304; \ + yp[c0_col] = v0; yp[c1_col] = v1; \ + } \ + } \ + } \ + sycl::group_barrier(sg); \ + } while (0) + INK_B70_GGUF_DN_STORE_SUB(0, c0_0, c1_0); + INK_B70_GGUF_DN_STORE_SUB(1, c0_1, c1_1); + INK_B70_GGUF_DN_STORE_SUB(2, c0_2, c1_2); + INK_B70_GGUF_DN_STORE_SUB(3, c0_3, c1_3); +#undef INK_B70_GGUF_DN_STORE_SUB +#else + (void)y_pairs_raw; +#endif + }); + }); +} + +// ============================================================================ +// 4B. Tier 4: BM=32, BN=64 XMX GGUF Prefill (preserved for 16 < M < 64 tails) +// ============================================================================ + template inline void k_jm_moe_gu_gguf_type_sg16( sycl::queue& q, @@ -1065,31 +1889,29 @@ inline void k_jm_moe_gu_gguf_type_sg16( const InkSyMoeBuckets* bk, const int* pair_list, int max_xm, float* h_act, const InkSyIqGridTable* iq_grids) { namespace jm = sycl::ext::oneapi::experimental::matrix; - constexpr size_t G64 = b70_gguf_grid_u64_len(); - constexpr size_t G32 = b70_gguf_grid_u32_len(); - constexpr size_t GSG = b70_gguf_needs_ksigns() ? 128 : 1; + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words(); + const size_t rb = sy_gguf_row_bytes(GT, 2304); q.submit([&](sycl::handler& h) { sycl::local_accessor sA(32 * 16, h); sycl::local_accessor sB(8 * 136, h); sycl::local_accessor sC(32 * 64, h); - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); + sycl::local_accessor sLut(std::max(1, LUT_U32), h); h.parallel_for(NR2(36, (size_t)max_xm, 128), - [bk, ex_layer, pair_list, x_in, h_act, iq_grids, sA, sB, sC, s_g64, s_g32, s_sgn](sycl::nd_item<2> it) INK_REQD_SG16 { + [bk, ex_layer, pair_list, x_in, h_act, iq_grids, rb, sA, sB, sC, sLut](sycl::nd_item<2> it) INK_REQD_SG16 { const int y = (int)it.get_group(0); if (y >= bk->n_xm) return; - const int bn = (int)it.get_group(1); // 0..35 -> 32 paired (gate, up) channels = 64 rows + const int bn = (int)it.get_group(1); const int tid = (int)it.get_local_id(1); const int sgi = tid >> 4, lane = tid & 15; auto sA_mp = sA.template get_multi_ptr(); auto sB_mp = sB.template get_multi_ptr(); auto sC_mp = sC.template get_multi_ptr(); + uint32_t* lut_ptr = sLut.template get_multi_ptr().get(); (void)sA_mp; (void)sB_mp; (void)sC_mp; (void)lane; - b70_gguf_preload_slm_lut(tid, 128, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); + b70_gguf_preload_lut_ptr(tid, 128, iq_grids, lut_ptr); + if constexpr (LUT_U32 > 0) sycl::group_barrier(it.get_group()); #if INK_SY_DEV_JM_SG16 auto sg = it.get_sub_group(); @@ -1101,7 +1923,6 @@ inline void k_jm_moe_gu_gguf_type_sg16( const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(GT, 2304); const bool ilv = (hdr->flags & 1u) != 0; for (int t0 = 0; t0 < n_tok; t0 += 32) { @@ -1110,9 +1931,6 @@ inline void k_jm_moe_gu_gguf_type_sg16( jm::joint_matrix c0, c1; jm::joint_matrix_fill(sg, c0, 0.0f); jm::joint_matrix_fill(sg, c1, 0.0f); -#else - float acc[4][4] = {}; - const int r0 = sgi * 4, c0_idx = lane * 4; #endif for (int k0 = 0; k0 < 2304; k0 += 16) { for (int idx = tid; idx < 32 * 16; idx += 128) { @@ -1124,7 +1942,6 @@ inline void k_jm_moe_gu_gguf_type_sg16( sA[idx] = (half)0.0f; } } - // 128 threads = 64 cols (nr = tid >> 1: even=gate, odd=up) x 2 groups of 8 K-elems (hi = (tid & 1) * 8) const int nr = tid >> 1; const int hi = (tid & 1) * 8; const int inter = bn * 32 + (nr >> 1); @@ -1132,7 +1949,7 @@ inline void k_jm_moe_gu_gguf_type_sg16( const int row_idx = ilv ? (2 * inter + is_up) : (is_up ? (1152 + inter) : inter); const float ext_sc = is_up ? hdr->up_scale : hdr->gate_scale; float w8[8]; - b70_gguf_decode_8_slm(body + 32 + (size_t)row_idx * rb, k0 + hi, ext_sc, w8, s_g64, s_g32, s_sgn); + b70_gguf_decode_8_ptr(body + 32 + (size_t)row_idx * rb, k0 + hi, ext_sc, w8, lut_ptr, iq_grids); #pragma unroll for (int j = 0; j < 8; ++j) { const int kk = hi + j; @@ -1147,18 +1964,6 @@ inline void k_jm_moe_gu_gguf_type_sg16( jm::joint_matrix_load(sg, b1, sB_mp + sg_n * 64 + 32, 136); jm::joint_matrix_mad(sg, c0, a_m, b0, c0); jm::joint_matrix_mad(sg, c1, a_m, b1, c1); -#else - for (int k = 0; k < 16; ++k) { - const int kp = (k >> 1) * 136 + (k & 1); - float bv[4] = {(float)sB[kp + (c0_idx + 0) * 2], (float)sB[kp + (c0_idx + 1) * 2], - (float)sB[kp + (c0_idx + 2) * 2], (float)sB[kp + (c0_idx + 3) * 2]}; -#pragma unroll - for (int ri = 0; ri < 4; ++ri) { - const float av = (float)sA[(r0 + ri) * 16 + k]; - acc[ri][0] += av * bv[0]; acc[ri][1] += av * bv[1]; - acc[ri][2] += av * bv[2]; acc[ri][3] += av * bv[3]; - } - } #endif sycl::group_barrier(it.get_group()); } @@ -1166,12 +1971,6 @@ inline void k_jm_moe_gu_gguf_type_sg16( auto pS = sC_mp + sg_m * 8 * 64 + sg_n * 32; jm::joint_matrix_store(sg, c0, pS, 64, jm::layout::row_major); jm::joint_matrix_store(sg, c1, pS + 16, 64, jm::layout::row_major); -#else -#pragma unroll - for (int ri = 0; ri < 4; ++ri) { -#pragma unroll - for (int ci = 0; ci < 4; ++ci) sC[(r0 + ri) * 64 + c0_idx + ci] = acc[ri][ci]; - } #endif sycl::group_barrier(it.get_group()); @@ -1195,19 +1994,16 @@ inline void k_jm_moe_dn_gguf_type_sg16( const InkSyMoeBuckets* bk, const int* pair_list, int max_xm, const float* h_act, float* y_pairs, const InkSyIqGridTable* iq_grids) { namespace jm = sycl::ext::oneapi::experimental::matrix; - constexpr size_t G64 = b70_gguf_grid_u64_len
(); - constexpr size_t G32 = b70_gguf_grid_u32_len
(); - constexpr size_t GSG = b70_gguf_needs_ksigns
() ? 128 : 1; + constexpr size_t LUT_U32 = b70_gguf_lut_u32_words
(); + const size_t rb = sy_gguf_row_bytes(DT, 1152); q.submit([&](sycl::handler& h) { sycl::local_accessor sA(32 * 16, h); sycl::local_accessor sB(8 * 136, h); sycl::local_accessor sC(32 * 64, h); - sycl::local_accessor s_g64(G64, h); - sycl::local_accessor s_g32(G32, h); - sycl::local_accessor s_sgn(GSG, h); + sycl::local_accessor sLut(std::max(1, LUT_U32), h); h.parallel_for(NR2(36, (size_t)max_xm, 128), - [bk, ex_layer, pair_list, topk_w, h_act, y_pairs, iq_grids, sA, sB, sC, s_g64, s_g32, s_sgn](sycl::nd_item<2> it) INK_REQD_SG16 { + [bk, ex_layer, pair_list, topk_w, h_act, y_pairs, iq_grids, rb, sA, sB, sC, sLut](sycl::nd_item<2> it) INK_REQD_SG16 { const int y = (int)it.get_group(0); if (y >= bk->n_xm) return; const int bn = (int)it.get_group(1); @@ -1216,10 +2012,11 @@ inline void k_jm_moe_dn_gguf_type_sg16( auto sA_mp = sA.template get_multi_ptr(); auto sB_mp = sB.template get_multi_ptr(); auto sC_mp = sC.template get_multi_ptr(); + uint32_t* lut_ptr = sLut.template get_multi_ptr().get(); (void)sA_mp; (void)sB_mp; (void)sC_mp; (void)lane; - b70_gguf_preload_slm_lut
(tid, 128, iq_grids, s_g64, s_g32, s_sgn); - sycl::group_barrier(it.get_group()); + b70_gguf_preload_lut_ptr
(tid, 128, iq_grids, lut_ptr); + if constexpr (LUT_U32 > 0) sycl::group_barrier(it.get_group()); #if INK_SY_DEV_JM_SG16 auto sg = it.get_sub_group(); @@ -1231,7 +2028,6 @@ inline void k_jm_moe_dn_gguf_type_sg16( const InkSyExpertDev ed = ex_layer[e]; const uint8_t* body = ed.body_ptr; const InkSyGgufExpertHeader* hdr = reinterpret_cast(body); - const size_t rb = sy_gguf_row_bytes(DT, 1152); const float ext_sc = hdr->dn_scale; const int row0 = bn * 64; @@ -1241,9 +2037,6 @@ inline void k_jm_moe_dn_gguf_type_sg16( jm::joint_matrix c0, c1; jm::joint_matrix_fill(sg, c0, 0.0f); jm::joint_matrix_fill(sg, c1, 0.0f); -#else - float acc[4][4] = {}; - const int r0 = sgi * 4, c0_idx = lane * 4; #endif for (int k0 = 0; k0 < 1152; k0 += 16) { for (int idx = tid; idx < 32 * 16; idx += 128) { @@ -1259,7 +2052,7 @@ inline void k_jm_moe_dn_gguf_type_sg16( const int hi = (tid & 1) * 8; const int gr = row0 + nr; float w8[8]; - b70_gguf_decode_8_slm
(body + ed.dn_off + (size_t)gr * rb, k0 + hi, ext_sc, w8, s_g64, s_g32, s_sgn); + b70_gguf_decode_8_ptr
(body + ed.dn_off + (size_t)gr * rb, k0 + hi, ext_sc, w8, lut_ptr, iq_grids); #pragma unroll for (int j = 0; j < 8; ++j) { const int kk = hi + j; @@ -1274,18 +2067,6 @@ inline void k_jm_moe_dn_gguf_type_sg16( jm::joint_matrix_load(sg, b1, sB_mp + sg_n * 64 + 32, 136); jm::joint_matrix_mad(sg, c0, a_m, b0, c0); jm::joint_matrix_mad(sg, c1, a_m, b1, c1); -#else - for (int k = 0; k < 16; ++k) { - const int kp = (k >> 1) * 136 + (k & 1); - float bv[4] = {(float)sB[kp + (c0_idx + 0) * 2], (float)sB[kp + (c0_idx + 1) * 2], - (float)sB[kp + (c0_idx + 2) * 2], (float)sB[kp + (c0_idx + 3) * 2]}; -#pragma unroll - for (int ri = 0; ri < 4; ++ri) { - const float av = (float)sA[(r0 + ri) * 16 + k]; - acc[ri][0] += av * bv[0]; acc[ri][1] += av * bv[1]; - acc[ri][2] += av * bv[2]; acc[ri][3] += av * bv[3]; - } - } #endif sycl::group_barrier(it.get_group()); } @@ -1293,12 +2074,6 @@ inline void k_jm_moe_dn_gguf_type_sg16( auto pS = sC_mp + sg_m * 8 * 64 + sg_n * 32; jm::joint_matrix_store(sg, c0, pS, 64, jm::layout::row_major); jm::joint_matrix_store(sg, c1, pS + 16, 64, jm::layout::row_major); -#else -#pragma unroll - for (int ri = 0; ri < 4; ++ri) { -#pragma unroll - for (int ci = 0; ci < 4; ++ci) sC[(r0 + ri) * 64 + c0_idx + ci] = acc[ri][ci]; - } #endif sycl::group_barrier(it.get_group()); @@ -1342,7 +2117,6 @@ inline bool b70_dispatch_moe_gu_gguf_exact( } } -// Note: K = 1152 (36 * 32 = 18 * 64) is NOT divisible by 256, so GGUF ffn_down_exps strictly uses 32/64-block types! inline bool b70_dispatch_moe_dn_gguf_exact( uint32_t dt, const InkSyExpertDev* ex_layer, const float* topk_w, const int* uexp, const int* uoff, const int* ucnt, const int* pair_list, const int* n_uniq_dev, @@ -1411,6 +2185,48 @@ inline bool b70_dispatch_moe_dn_gguf_dp4a( } #if INK_SY_HAS_JOINT_MATRIX +template +inline bool b70_dispatch_jm_moe_gu_gguf_bm128( + sycl::queue& q, uint32_t gt, + const InkSyExpertDev* ex_layer, const void* x_in_raw, + const InkSyMoeBuckets* bk, const int* pair_list, int max_xm_tiles, + void* h_act_raw, const InkSyIqGridTable* iq_grids) { + switch (gt) { + case 21u: k_b70_jm_moe_gu_gguf_bm128_sg16<21u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 22u: k_b70_jm_moe_gu_gguf_bm128_sg16<22u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 17u: k_b70_jm_moe_gu_gguf_bm128_sg16<17u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 16u: k_b70_jm_moe_gu_gguf_bm128_sg16<16u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 19u: k_b70_jm_moe_gu_gguf_bm128_sg16<19u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 42u: k_b70_jm_moe_gu_gguf_bm128_sg16<42u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 18u: k_b70_jm_moe_gu_gguf_bm128_sg16<18u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 12u: k_b70_jm_moe_gu_gguf_bm128_sg16<12u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 13u: k_b70_jm_moe_gu_gguf_bm128_sg16<13u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 14u: k_b70_jm_moe_gu_gguf_bm128_sg16<14u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 23u: k_b70_jm_moe_gu_gguf_bm128_sg16<23u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 20u: k_b70_jm_moe_gu_gguf_bm128_sg16<20u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 8u: k_b70_jm_moe_gu_gguf_bm128_sg16< 8u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 2u: k_b70_jm_moe_gu_gguf_bm128_sg16< 2u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + case 39u: k_b70_jm_moe_gu_gguf_bm128_sg16<39u, HALF_INTER, HALF_X>(q, ex_layer, x_in_raw, bk, pair_list, max_xm_tiles, h_act_raw, iq_grids); return true; + default: return false; + } +} + +template +inline bool b70_dispatch_jm_moe_dn_gguf_bm128( + sycl::queue& q, uint32_t dt, + const InkSyExpertDev* ex_layer, const float* topk_w, + const InkSyMoeBuckets* bk, const int* pair_list, int max_xm_tiles, + const void* h_act_raw, void* y_pairs_raw, const InkSyIqGridTable* iq_grids) { + switch (dt) { + case 42u: k_b70_jm_moe_dn_gguf_bm128_sg16<42u, HALF_INTER, HALF_YP>(q, ex_layer, topk_w, bk, pair_list, max_xm_tiles, h_act_raw, y_pairs_raw, iq_grids); return true; + case 20u: k_b70_jm_moe_dn_gguf_bm128_sg16<20u, HALF_INTER, HALF_YP>(q, ex_layer, topk_w, bk, pair_list, max_xm_tiles, h_act_raw, y_pairs_raw, iq_grids); return true; + case 8u: k_b70_jm_moe_dn_gguf_bm128_sg16< 8u, HALF_INTER, HALF_YP>(q, ex_layer, topk_w, bk, pair_list, max_xm_tiles, h_act_raw, y_pairs_raw, iq_grids); return true; + case 2u: k_b70_jm_moe_dn_gguf_bm128_sg16< 2u, HALF_INTER, HALF_YP>(q, ex_layer, topk_w, bk, pair_list, max_xm_tiles, h_act_raw, y_pairs_raw, iq_grids); return true; + case 39u: k_b70_jm_moe_dn_gguf_bm128_sg16<39u, HALF_INTER, HALF_YP>(q, ex_layer, topk_w, bk, pair_list, max_xm_tiles, h_act_raw, y_pairs_raw, iq_grids); return true; + default: return false; + } +} + inline bool b70_dispatch_jm_moe_gu_gguf( sycl::queue& q, uint32_t gt, const InkSyExpertDev* ex_layer, const float* x_in, diff --git a/engine/src/inkling2/sycl/ink_sy_common.h b/engine/src/inkling2/sycl/ink_sy_common.h index a6d229a18d..325d46c1ce 100644 --- a/engine/src/inkling2/sycl/ink_sy_common.h +++ b/engine/src/inkling2/sycl/ink_sy_common.h @@ -719,6 +719,94 @@ inline void transcode_mx8_expert_to_sf4(const uint8_t* raw_mx8, uint32_t orig_dn std::memcpy(out_sf4, hdr, 16u); } +// Host-side fast GGUF Q8_0 (type 8) -> SF4 Coda expert transcoder for --turbo mode (UD-Q4_K_XL L20..L23: -1.90 GiB VRAM, 2.0x MoE BW!): +inline float encode_q8_0_mat_to_sf4( + const uint8_t* q8_base, int rows, int cols, bool split_gu_to_ilv, + float scale_even, float scale_odd, uint8_t* sf4_pb, uint8_t* sf4_sb) { + const int n_blk32 = cols >> 5; + const int n_blk16 = cols >> 4; + const size_t rb = (size_t)n_blk32 * 34u; + const int half_rows = rows >> 1; + float gmax = 0.0f; + for (int r = 0; r < rows; ++r) { + const int src_r = split_gu_to_ilv ? (((r & 1) ? half_rows : 0) + (r >> 1)) : r; + const float ext_sc = (r & 1) ? scale_odd : scale_even; + const uint8_t* wr = q8_base + (size_t)src_r * rb; + for (int b32 = 0; b32 < n_blk32; ++b32) { + const uint8_t* blk = wr + (size_t)b32 * 34u; + const float d = std::fabs(h2f_bits(ld16(blk)) * ext_sc); + if (d == 0.0f) continue; + int m_q = 0; + for (int j = 0; j < 32; ++j) { + const int aq = std::abs((int)(int8_t)blk[2 + j]); + if (aq > m_q) m_q = aq; + } + const float bmax = d * (float)m_q; + if (bmax > gmax) gmax = bmax; + } + } + const float outer = (gmax > 0.0f) ? (gmax * (1.0f / (6.0f * 448.0f))) : 1.0f; + const float inv_6outer = (gmax > 0.0f) ? (1.0f / (6.0f * outer)) : 0.0f; + + for (int r = 0; r < rows; ++r) { + const int src_r = split_gu_to_ilv ? (((r & 1) ? half_rows : 0) + (r >> 1)) : r; + const float ext_sc = (r & 1) ? scale_odd : scale_even; + const uint8_t* wr = q8_base + (size_t)src_r * rb; + uint8_t* p_dst = sf4_pb + (size_t)r * (cols >> 1); + uint8_t* s_dst = sf4_sb + (size_t)r * n_blk16; + for (int b16 = 0; b16 < n_blk16; ++b16) { + const uint8_t* blk = wr + (size_t)(b16 >> 1) * 34u; + const float d = h2f_bits(ld16(blk)) * ext_sc; + const int8_t* qp = reinterpret_cast(blk + 2 + (b16 & 1) * 16); + float wf[16]; + float bmax = 0.0f; + for (int j = 0; j < 16; ++j) { + const float v = (float)qp[j] * d; + wf[j] = v; + const float av = std::fabs(v); + if (av > bmax) bmax = av; + } + const uint8_t sc_u8 = (bmax > 0.0f) ? f32_to_e4m3(bmax * inv_6outer) : 0u; + s_dst[b16] = sc_u8; + const float sc_eff = e4m3_to_f32(sc_u8) * outer; + const float inv_sc = (sc_eff > 0.0f) ? (1.0f / sc_eff) : 0.0f; + uint8_t* pb = p_dst + b16 * 8; + for (int j2 = 0; j2 < 8; ++j2) { + const float v0 = wf[2 * j2 + 0] * inv_sc; + const float v1 = wf[2 * j2 + 1] * inv_sc; + const float a0 = std::fabs(v0), a1 = std::fabs(v1); + const uint32_t m0 = (a0 < 0.25f) ? 0u : (a0 < 0.75f) ? 1u : (a0 < 1.25f) ? 2u : (a0 < 1.75f) ? 3u + : (a0 < 2.50f) ? 4u : (a0 < 3.50f) ? 5u : (a0 < 5.00f) ? 6u : 7u; + const uint32_t m1 = (a1 < 0.25f) ? 0u : (a1 < 0.75f) ? 1u : (a1 < 1.25f) ? 2u : (a1 < 1.75f) ? 3u + : (a1 < 2.50f) ? 4u : (a1 < 3.50f) ? 5u : (a1 < 5.00f) ? 6u : 7u; + const uint32_t n0 = ((v0 < 0.0f) ? 8u : 0u) | m0; + const uint32_t n1 = ((v1 < 0.0f) ? 8u : 0u) | m1; + pb[j2] = (uint8_t)(n0 | (n1 << 4)); + } + } + } + return outer; +} + +inline void transcode_gguf_q8_0_expert_to_sf4(const uint8_t* raw_gguf, uint32_t orig_dn_off, uint8_t* out_sf4) { + constexpr uint32_t kSf4DnOff = 2986000u; + uint32_t flags = 0; + float gate_sc = 1.0f, up_sc = 1.0f, dn_sc = 1.0f; + std::memcpy(&flags, raw_gguf + 12, 4); + std::memcpy(&gate_sc, raw_gguf + 16, 4); + std::memcpy(&up_sc, raw_gguf + 20, 4); + std::memcpy(&dn_sc, raw_gguf + 24, 4); + const bool ilv = (flags & 1u) != 0; + const float gu_outer = encode_q8_0_mat_to_sf4( + raw_gguf + 32u, 2304, 2304, !ilv, gate_sc, up_sc, + out_sf4 + 16u, out_sf4 + 16u + 2654208u); + const float dn_outer = encode_q8_0_mat_to_sf4( + raw_gguf + orig_dn_off, 2304, 1152, false, dn_sc, dn_sc, + out_sf4 + kSf4DnOff, out_sf4 + kSf4DnOff + 1327104u); + const float hdr[4] = {gu_outer, 1.0f, dn_outer, 1.0f}; + std::memcpy(out_sf4, hdr, 16u); +} + // ---- Format IDs & Manifest ---- enum InkFmt : int32_t { FMT_S2 = 2, FMT_S3 = 3, FMT_S4 = 4, FMT_S5 = 5, FMT_S6 = 6, FMT_S8 = 8, @@ -1726,17 +1814,35 @@ struct InkSyclModel { const bool force_sk_tc = (std::getenv("INK_SYCL_FORCE_SK_TRANSCODE") && std::atoi(std::getenv("INK_SYCL_FORCE_SK_TRANSCODE")) != 0); std::vector adj_dir = mf.expert_directory(); std::vector native_gguf(adj_dir.size(), 0); + std::vector gguf_q8_to_sf4(adj_dir.size(), 0); + std::vector orig_dn_off_di(adj_dir.size(), 0); size_t n_native_gguf = 0; + size_t n_coda_q8_sf4 = 0; + size_t coda_q8_saved_bytes = 0; if (mf.kind == ink2::ModelSourceKind::Gguf && mf.gguf_adapter) { const size_t h_sz = (size_t)man.c.hidden; const size_t i_sz = (size_t)man.c.inter; + const bool allow_coda_q8_sf4 = acfg.fast && h_sz == 2304 && i_sz == 1152 && + !env_truthy_arc("STRATA_INK_NO_TURBO_CODA_SF4"); + constexpr uint32_t kSf4ExpertBytes = 4478992u; + constexpr uint32_t kSf4DnOff = 2986000u; for (size_t i = 0; i < adj_dir.size(); ++i) { auto& e = adj_dir[i]; if (e.gu_code_id != FMT_GGUF_RAW && e.dn_code_id != FMT_GGUF_RAW) continue; const int il = (int)(i / (size_t)man.c.n_experts); if (il < 0 || il >= (int)mf.gguf_adapter->layers.size()) continue; const auto& lm = mf.gguf_adapter->layers[il]; - if (!force_sk_tc && gguf_type_has_sycl_kernel(lm.gu_type) && gguf_type_has_sycl_kernel(lm.dn_type)) { + if (allow_coda_q8_sf4 && il >= man.c.core_end && lm.gu_type == 8u && lm.dn_type == 8u && + e.body_bytes > kSf4ExpertBytes) { + gguf_q8_to_sf4[i] = 1; + orig_dn_off_di[i] = e.dn_offset; + coda_q8_saved_bytes += (e.body_bytes - kSf4ExpertBytes); + e.gu_code_id = FMT_SF4; + e.dn_code_id = FMT_SF4; + e.dn_offset = kSf4DnOff; + e.body_bytes = kSf4ExpertBytes; + ++n_coda_q8_sf4; + } else if (!force_sk_tc && gguf_type_has_sycl_kernel(lm.gu_type) && gguf_type_has_sycl_kernel(lm.dn_type)) { native_gguf[i] = 1; ++n_native_gguf; } else { @@ -1750,6 +1856,10 @@ struct InkSyclModel { e.body_bytes = body_b; } } + if (n_coda_q8_sf4 > 0) { + fprintf(stderr, "[ink_sycl_intelarc] transcoding %zu GGUF Q8_0 Coda experts -> SF4 in --turbo (saves %.1f MiB VRAM)\n", + n_coda_q8_sf4, (double)coda_q8_saved_bytes / 1048576.0); + } if (n_native_gguf > 0) { fprintf(stderr, "[ink_sycl] native lossless GGUF_RAW SG16+XMX kernels active for %zu/%zu experts (zero S_K transcode)\n", n_native_gguf, adj_dir.size()); @@ -1757,6 +1867,16 @@ struct InkSyclModel { } auto read_expert_sy = [&](size_t di, std::vector& out_b, uint32_t& out_dn, uint8_t& out_gu, uint8_t& out_dnc, std::string& lerr) -> bool { + if (di < gguf_q8_to_sf4.size() && gguf_q8_to_sf4[di]) { + std::vector raw_tmp; + if (!mf.read_expert_body(di, raw_tmp, lerr)) return false; + out_b.resize(adj_dir[di].body_bytes); + transcode_gguf_q8_0_expert_to_sf4(raw_tmp.data(), orig_dn_off_di[di], out_b.data()); + out_dn = adj_dir[di].dn_offset; + out_gu = adj_dir[di].gu_code_id; + out_dnc = adj_dir[di].dn_code_id; + return true; + } if (di < native_gguf.size() && native_gguf[di]) { if (!mf.read_expert_body(di, out_b, lerr)) return false; out_dn = adj_dir[di].dn_offset; diff --git a/engine/src/inkling2/sycl/ink_sy_runner.h b/engine/src/inkling2/sycl/ink_sy_runner.h index 0ebd5b67dd..d256d3c6a3 100644 --- a/engine/src/inkling2/sycl/ink_sy_runner.h +++ b/engine/src/inkling2/sycl/ink_sy_runner.h @@ -1755,13 +1755,18 @@ struct InkSyRunner { const uint8_t gu_f_l = (ip < (int)layer_gu_fmt.size()) ? layer_gu_fmt[ip] : 0; const uint8_t dn_f_l = (ip < (int)layer_dn_fmt.size()) ? layer_dn_fmt[ip] : 0; + const uint32_t g_gt_l = (ip < (int)layer_gguf_gu_type.size()) ? layer_gguf_gu_type[ip] : 0; + const uint32_t g_dt_l = (ip < (int)layer_gguf_dn_type.size()) ? layer_gguf_dn_type[ip] : 0; const bool any_gguf_l = (gu_f_l == FMT_GGUF_RAW || dn_f_l == FMT_GGUF_RAW); #if INK_SY_HAS_JOINT_MATRIX - const bool exact_xmx_moe = !acfg.fast && is_bmg_gpu() && b70_k && M >= 64 && !any_gguf_l && + const bool gguf_bm128_ok = !any_gguf_l || + ((gu_f_l != FMT_GGUF_RAW || b70_gguf_gu_xmx_supported(g_gt_l)) && + (dn_f_l != FMT_GGUF_RAW || b70_gguf_dn_xmx_supported(g_dt_l))); + const bool exact_xmx_moe = !acfg.fast && is_bmg_gpu() && b70_k && M >= 64 && gguf_bm128_ok && !use_bf16_sc && !env_truthy_arc("STRATA_INK_NO_EXACT_XMX"); - const bool exact_bm128 = exact_xmx_moe && M >= 128; - const bool fast_bm128_f16 = acfg.fast && acfg.use_joint_matrix_xmx && is_bmg_gpu() && b70_k && M >= 128 && - !any_gguf_l && !use_bf16_sc; + const bool exact_bm128 = exact_xmx_moe && (M >= 128 || any_gguf_l); + const bool fast_bm128_f16 = acfg.fast && acfg.use_joint_matrix_xmx && is_bmg_gpu() && b70_k && + (M >= 128 || (M >= 64 && any_gguf_l)) && gguf_bm128_ok && !use_bf16_sc; #else const bool exact_xmx_moe = false; const bool exact_bm128 = false; @@ -1887,23 +1892,44 @@ struct InkSyRunner { const InkSyExpertDev* ex_layer = mod->expert_dev + (size_t)ip * c.n_experts; const uint8_t gu_f = gu_f_l; const uint8_t dn_f = dn_f_l; - const uint32_t g_gt = (ip < (int)layer_gguf_gu_type.size()) ? layer_gguf_gu_type[ip] : 0; - const uint32_t g_dt = (ip < (int)layer_gguf_dn_type.size()) ? layer_gguf_dn_type[ip] : 0; - const bool gguf_gu_xm_ok = (g_gt == 21u || g_gt == 22u || g_gt == 17u || g_gt == 16u || g_gt == 19u || - g_gt == 42u || g_gt == 18u || g_gt == 12u || g_gt == 13u || g_gt == 14u || - g_gt == 23u || g_gt == 20u || g_gt == 8u || g_gt == 2u || g_gt == 39u); - const bool gguf_dn_xm_ok = (g_dt == 42u || g_dt == 20u || g_dt == 8u || g_dt == 2u || g_dt == 39u); + const uint32_t g_gt = g_gt_l; + const uint32_t g_dt = g_dt_l; + const bool gguf_gu_xm_ok = b70_gguf_gu_xmx_supported(g_gt); + const bool gguf_dn_xm_ok = b70_gguf_dn_xmx_supported(g_dt); const bool allow_gguf_xm = b70_k && (gu_f == FMT_GGUF_RAW && dn_f == FMT_GGUF_RAW && gguf_gu_xm_ok && gguf_dn_xm_ok); if (fast_bm128_f16) { half* x_in_f16 = reinterpret_cast(b_wo); k_b70_f32_to_f16_sg16(Q(), b_mlp_in, x_in_f16, (size_t)M * D); - k_moe_plan_active_sg16(Q(), b_topk_idx, P, 1, b_moe_bk, b_moe_pairs, ex_layer, false); - k_b70_jm_moe_gu_bm128_sg16(Q(), ex_layer, x_in_f16, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h); - k_b70_jm_moe_dn_bm128_sg16(Q(), ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp); + k_moe_plan_active_sg16(Q(), b_topk_idx, P, 1, b_moe_bk, b_moe_pairs, ex_layer, any_gguf_l); + if (gu_f == FMT_GGUF_RAW) { + b70_dispatch_jm_moe_gu_gguf_bm128( + Q(), g_gt, ex_layer, x_in_f16, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, iq_grid_table_dev()); + } else { + k_b70_jm_moe_gu_bm128_sg16(Q(), ex_layer, x_in_f16, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h); + } + if (dn_f == FMT_GGUF_RAW) { + b70_dispatch_jm_moe_dn_gguf_bm128( + Q(), g_dt, ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp, iq_grid_table_dev()); + } else { + k_b70_jm_moe_dn_bm128_sg16(Q(), ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp); + } } else if (exact_bm128) { static const bool moe_2term = env_truthy_arc("STRATA_INK_MOE_2TERM"); - k_moe_plan_active_sg16(Q(), b_topk_idx, P, 1, b_moe_bk, b_moe_pairs, ex_layer, false); - if (moe_2term) { + k_moe_plan_active_sg16(Q(), b_topk_idx, P, 1, b_moe_bk, b_moe_pairs, ex_layer, any_gguf_l); + if (any_gguf_l) { + if (gu_f == FMT_GGUF_RAW) { + b70_dispatch_jm_moe_gu_gguf_bm128( + Q(), g_gt, ex_layer, b_mlp_in, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, iq_grid_table_dev()); + } else { + k_b70_jm_moe_gu_bm128_sg16(Q(), ex_layer, b_mlp_in, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h); + } + if (dn_f == FMT_GGUF_RAW) { + b70_dispatch_jm_moe_dn_gguf_bm128( + Q(), g_dt, ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp, iq_grid_table_dev()); + } else { + k_b70_jm_moe_dn_bm128_sg16(Q(), ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp); + } + } else if (moe_2term) { k_b70_jm_moe_gu_bm128_sg16(Q(), ex_layer, b_mlp_in, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h); k_b70_jm_moe_dn_bm128_sg16(Q(), ex_layer, b_topk_w, b_moe_bk, b_moe_pairs, max_xm_tiles, b_moe_h, b_moe_yp); } else { @@ -1997,7 +2023,11 @@ struct InkSyRunner { const uint32_t g_gt = (ip < (int)layer_gguf_gu_type.size()) ? layer_gguf_gu_type[ip] : 0; const uint32_t g_dt = (ip < (int)layer_gguf_dn_type.size()) ? layer_gguf_dn_type[ip] : 0; bool gu_done = false; - const bool use_gguf_dp4a = acfg.fast && M <= 16 && env_truthy_arc("STRATA_INK_GGUF_DP4A"); + // On Xe2 (bmg_g31), Exact-SG32 FP32 FMA beats Q8_1 dp4a by 11%-32% on IQ* & Q4_K while avoiding + // 2 extra quant_q8_1 launches per layer; Q6_K (type 14) is 1.17x faster on dp4a at M<=16. + const bool use_gguf_dp4a = acfg.fast && M <= 16 && + (env_truthy_arc("STRATA_INK_GGUF_DP4A") || + (!env_truthy_arc("STRATA_INK_NO_GGUF_DP4A") && g_gt == 14u)); if (gu_f == FMT_GGUF_RAW && g_gt != 0) { if (use_gguf_dp4a) { gu_done = b70_dispatch_moe_gu_gguf_dp4a(g_gt, ex_layer, b_mlp_in, b_gguf_xq,