From f2f4121a70161d953541ff9a0943d45003f7bdbb Mon Sep 17 00:00:00 2001 From: maxfridbe Date: Thu, 1 Oct 2026 22:49:04 -0500 Subject: [PATCH] sycl: read streamed experts from the right shard; fix IQ2_XS products (Swift 1.5 runs) - gguf_expert_source: ExpertLayout::gguf_file is per layer and role (3 * layer + role) since upstream 0.1.31; the port indexed it by layer, so experts in shard 2 were read from shard 1 at shard-2 offsets (Swift 1.5: layers 13-47, NaN logits from layer 13). One fd per layer and role now. - iq_kernels: vec_dot_iq2_xs and Split<17> took their 16-bit codes through a uint16_t* to an int2 (type punning the device compiler does not honour); by shifts now. iq_multi_parity checks IQ2_XS against a CPU decode from ggml's tables (iq2xs_host_tables.inc) and passes. - prefill: STRATA_DBG_NAN=1 also reports the experts' fp16 GEMM inputs. Tested on the B70: Swift 1.5 IQ2_XS answers correctly (48 tok/s raw, short thinking); the original IQ2_XS output is unchanged (65109a, 58 tok/s). --- docs/INTEL.md | 19 ++- .../strata/core/gguf_expert_source.hpp | 4 +- sycl/src/core/gguf_expert_source.cpp | 25 ++-- sycl/src/kernels/cuda/iq_kernels.dp.cpp | 14 +- sycl/src/kernels/iq2xs_host_tables.inc | 138 ++++++++++++++++++ sycl/src/kernels/iq_multi_parity.cpp | 49 +++++++ sycl/src/prefill/prefill.cpp | 22 +++ 7 files changed, 251 insertions(+), 20 deletions(-) create mode 100644 sycl/src/kernels/iq2xs_host_tables.inc diff --git a/docs/INTEL.md b/docs/INTEL.md index 9e8c9e67..046f441c 100644 --- a/docs/INTEL.md +++ b/docs/INTEL.md @@ -486,8 +486,7 @@ part of the procedure now: it caught a dropped mirror hook and four doorbell wai gate/up 8 or 16 x down 4 or 8 all decode in 37.7-38.6 ms per verify round (2 runs each, Coder, both prompts); tok/s differences between them are draft acceptance (each split rounds differently). The default (8 / 8) stays. - **Speed after the merge:** Coder 78.2 / 75.7 tok/s (as before), IQ2_XS 58.6 / 64.2 (from 50.8 / 60.6). -- **Still failing:** `iq_multi_parity` on IQ2_XS (type 17: the port's mmvq disagrees with the reference, old and new - kernel alike; no model here uses it), `s2_expert_grouped_parity` (the s2 path), `kv_hybrid_parity`'s last step +- **Still failing:** (`iq_multi_parity` on IQ2_XS: fixed later, see "Two model-specific bugs"), `s2_expert_grouped_parity` (the s2 path), `kv_hybrid_parity`'s last step (needs the unported tensor-core prompt kernel). `ple_parity` and `native_expert_parity` need model files. **The 0.1.32 merge (2026-10-01).** Upstream 0.1.31 -> 0.1.32 (89 commits) by the same steps; the hash-only @@ -527,6 +526,22 @@ after every phase (`STRATA_PREFILL_SYNC=1`, kept as a debug switch). The fix: th number into page-locked host memory after each DMA and the stager polls it - no host thread waits on a queue's event. Output is bit-identical to the run without borrowing; borrowing is the default above 32K again. +**Two model-specific bugs found with Swift 1.5 (fixed 2026-10-01).** UkisAI's Swift 1.5 IQ2_XS (setup's `swift` +family, the same GSQ-RCO layout) decoded token 0 forever: NaN logits from layer 13 on. + +- **`--stream-experts` read the wrong shard.** Since upstream 0.1.31, `ExpertLayout::gguf_file` is per layer AND role + (`3 * layer + role`; a shard boundary can fall inside a layer). The port's `GgufExpertSource` still indexed it by + layer, so a layer in shard 2 was read from shard 1 at shard 2's offsets: garbage IQ1_M scales, infinities after the + fp16 dequantization. The original model keeps every expert in shard 1 and never noticed; Swift's layers 13-47 are + in shard 2. `STRATA_DBG_NAN=1` now also reports the experts' fp16 GEMM inputs (activations, dequantized gate/up). +- **IQ2_XS products were garbage** (the long-failing `iq_multi_parity` IQ2_XS case): ggml's `vec_dot_iq2_xs` reads + its four 16-bit codes through a `uint16_t*` to an `int2`, type punning the SYCL device compiler does not honour; + the dequantizer, which reads the codes directly, was exact. The codes are extracted by shifts now (the dot and the + expert kernels' `Split<17>`); the test checks against a CPU decode from ggml's tables too and passes. No model here + uses IQ2_XS tensors, Swift included - this was not Swift's bug, only found on the way. + +Swift 1.5 IQ2_XS after both: correct answers, 51-118 words of thinking on short questions, 48 tok/s raw decode. + **Not ported yet (2026-10-01).** - Three kernels carry inline PTX (`mma.sync` tensor-core matrix ops, `ldmatrix`, `cp.async`): diff --git a/sycl/include/strata/core/gguf_expert_source.hpp b/sycl/include/strata/core/gguf_expert_source.hpp index 7ae36b53..678a754e 100644 --- a/sycl/include/strata/core/gguf_expert_source.hpp +++ b/sycl/include/strata/core/gguf_expert_source.hpp @@ -55,13 +55,13 @@ public: uint64_t mirrored_bytes() const { return mirror_bytes_; } private: - int fd_of(int64_t layer, std::string& err); + int fd_of(int64_t layer, int role, std::string& err); std::string shard_; std::string dir_; std::vector fds_; ///< per distinct file name, opened once std::vector names_; - std::vector layer_fd_; ///< per layer: index into fds_ + std::vector layer_fd_; ///< per layer and role (3 * layer + role): index into fds_ std::vector> ring_; std::vector ring_key_; ///< (layer << 20 | expert) held by each slot, -1 = empty std::unordered_map where_; ///< key -> slot diff --git a/sycl/src/core/gguf_expert_source.cpp b/sycl/src/core/gguf_expert_source.cpp index 70ba763c..8c228f3c 100644 --- a/sycl/src/core/gguf_expert_source.cpp +++ b/sycl/src/core/gguf_expert_source.cpp @@ -44,27 +44,32 @@ bool GgufExpertSource::open(const std::string& shard1, int64_t n_layers, int64_t dir_ = cut == std::string::npos ? std::string() : shard1.substr(0, cut + 1); n_layers_ = n_layers; n_expert_ = n_expert; - layer_fd_.assign((size_t) n_layers, -1); + layer_fd_.assign((size_t) (3 * n_layers), -1); for (int64_t l = 0; l < n_layers; ++l) - if (fd_of(l, err) < 0) { close(); return false; } + for (int r = 0; r < 3; ++r) + if (fd_of(l, r, err) < 0) { close(); return false; } ring_.resize(kRing); for (auto& b : ring_) b.resize((size_t) lay.max_blob); ring_key_.assign(kRing, -1); return true; } -int GgufExpertSource::fd_of(int64_t layer, std::string& err) { - if (layer_fd_[(size_t) layer] >= 0) return fds_[(size_t) layer_fd_[(size_t) layer]]; +// The file of role `role` (0 gate, 1 up, 2 down) of `layer`. ExpertLayout::gguf_file is per layer AND role +// (`3 * layer + role`) since upstream 0.1.31; indexing it by layer alone read a shard-2 layer from shard 1 (Swift 1.5, +// whose layers 13-47 are in shard 2: garbage IQ1_M scales, NaN logits - 2026-10-01). +int GgufExpertSource::fd_of(int64_t layer, int role, std::string& err) { + const size_t i = (size_t) (3 * layer + role); + if (layer_fd_[i] >= 0) return fds_[(size_t) layer_fd_[i]]; const auto& lay = strata::kernels::cpu::expert_layout(); std::string name = shard_; - if (!lay.gguf_file.empty() && !lay.gguf_file[(size_t) layer].empty()) name = dir_ + lay.gguf_file[(size_t) layer]; - for (size_t i = 0; i < names_.size(); ++i) - if (names_[i] == name) { layer_fd_[(size_t) layer] = (int) i; return fds_[i]; } + if (lay.gguf_file.size() > i && !lay.gguf_file[i].empty()) name = dir_ + lay.gguf_file[i]; + for (size_t k = 0; k < names_.size(); ++k) + if (names_[k] == name) { layer_fd_[i] = (int) k; return fds_[k]; } const int fd = ::open(name.c_str(), O_RDONLY | O_CLOEXEC); if (fd < 0) { err = "--stream-experts: cannot open " + name; return -1; } names_.push_back(name); fds_.push_back(fd); - layer_fd_[(size_t) layer] = (int) fds_.size() - 1; + layer_fd_[i] = (int) fds_.size() - 1; return fd; } @@ -80,7 +85,6 @@ const uint8_t* GgufExpertSource::blob(int64_t layer, int64_t expert) { // the arena loader's gather, for one expert: [gate | up | down] from the three tensors const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off}; const uint64_t at[3] = {0, fm.up_off, fm.down_off}; - const int fd = fds_[(size_t) layer_fd_[(size_t) layer]]; const int64_t key = ((int64_t) layer << 20) | expert; size_t slot; { @@ -95,6 +99,7 @@ const uint8_t* GgufExpertSource::blob(int64_t layer, int64_t expert) { } std::vector& buf = ring_[slot]; for (int r = 0; r < 3; ++r) { + const int fd = fds_[(size_t) layer_fd_[(size_t) (3 * layer + r)]]; const uint64_t src = lay.gguf_off[(size_t) (3 * layer + r)] + per[r] * (uint64_t) expert; uint64_t done = 0; while (done < per[r]) { @@ -184,8 +189,8 @@ bool GgufExpertSource::read_into(int64_t layer, int64_t expert, uint8_t* dst, si if (bytes < blob) return false; const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off}; const uint64_t at[3] = {0, fm.up_off, fm.down_off}; - const int fd = fds_[(size_t) layer_fd_[(size_t) layer]]; for (int r = 0; r < 3; ++r) { + const int fd = fds_[(size_t) layer_fd_[(size_t) (3 * layer + r)]]; const uint64_t src = lay.gguf_off[(size_t) (3 * layer + r)] + per[r] * (uint64_t) expert; uint64_t done = 0; while (done < per[r]) { diff --git a/sycl/src/kernels/cuda/iq_kernels.dp.cpp b/sycl/src/kernels/cuda/iq_kernels.dp.cpp index 2d035a7f..ac071497 100644 --- a/sycl/src/kernels/cuda/iq_kernels.dp.cpp +++ b/sycl/src/kernels/cuda/iq_kernels.dp.cpp @@ -156,9 +156,11 @@ __dpct_inline__ float vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq, const block_q8_1 *__restrict__ bq8_1, const int &kbx, const int &iqs) { const block_iq2_xs* bq2 = (const block_iq2_xs*) vbq + kbx; - const sycl::int2 q2_packed = - sycl::int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1)); - const uint16_t* q2 = (const uint16_t*) &q2_packed; + // SYCL port: the four 16-bit codes by shifts. ggml reads them through a uint16_t* to an int2, which is type + // punning the SYCL device compiler does not honour (non-char aliasing): IQ2_XS products came out garbage + // (iq_multi_parity, Swift 1.5 IQ2_XS) while the dequantizer, reading the codes directly, was exact. + const uint32_t q2_lo = (uint32_t) get_int_b2(bq2->qs, iqs + 0), q2_hi = (uint32_t) get_int_b2(bq2->qs, iqs + 1); + const uint16_t q2[4] = {(uint16_t) q2_lo, (uint16_t) (q2_lo >> 16), (uint16_t) q2_hi, (uint16_t) (q2_hi >> 16)}; const int ls0 = bq2->scales[iqs / 2] & 0x0F; const int ls1 = bq2->scales[iqs / 2] >> 4; int sumi0 = 0, sumi1 = 0; @@ -766,9 +768,9 @@ template<> inline constexpr bool kSplit<17> = true; template<> struct Split<17> : SplitLs2 { // IQ2_XS static W load(const void* __restrict__ vbq, int kbx, int iqs) { const block_iq2_xs* bq2 = (const block_iq2_xs*) vbq + kbx; - const sycl::int2 q2_packed = sycl::int2(get_int_b2(bq2->qs, iqs + 0), - get_int_b2(bq2->qs, iqs + 1)); - const uint16_t* q2 = (const uint16_t*) &q2_packed; + // SYCL port: the codes by shifts, not through a uint16_t* to an int2 (see vec_dot_iq2_xs_q8_1) + const uint32_t q2_lo = (uint32_t) get_int_b2(bq2->qs, iqs + 0), q2_hi = (uint32_t) get_int_b2(bq2->qs, iqs + 1); + const uint16_t q2[4] = {(uint16_t) q2_lo, (uint16_t) (q2_lo >> 16), (uint16_t) q2_hi, (uint16_t) (q2_hi >> 16)}; W r; r.ls0 = bq2->scales[iqs / 2] & 0x0F; r.ls1 = bq2->scales[iqs / 2] >> 4; diff --git a/sycl/src/kernels/iq2xs_host_tables.inc b/sycl/src/kernels/iq2xs_host_tables.inc new file mode 100644 index 00000000..30b9bb34 --- /dev/null +++ b/sycl/src/kernels/iq2xs_host_tables.inc @@ -0,0 +1,138 @@ +// iq2xs_host_tables.inc - IQ2_XS's grid and sign tables as plain host arrays (from ggml-common.h, MIT), for the +// CPU decode in iq_multi_parity (the SYCL ggml-common.h holds them as device globals only) +static const uint64_t host_iq2xs_grid[512] = {0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08, + 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b, + 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919, + 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b, + 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919, + 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808, + 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819, + 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819, + 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, + 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b, + 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b, + 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908, + 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908, + 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919, + 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808, + 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919, + 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908, + 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, + 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, + 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08, + 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808, + 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808, + 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819, + 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908, + 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819, + 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808, + 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b, + 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819, + 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819, + 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808, + 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908, + 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19, + 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b, + 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b, + 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919, + 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808, + 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819, + 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819, + 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b, + 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908, + 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808, + 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819, + 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808, + 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, + 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808, + 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808, + 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908, + 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908, + 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808, + 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b, + 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819, + 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, + 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908, + 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808, + 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908, + 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919, + 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08, + 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19, + 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b, + 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b, + 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808, + 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08, + 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b, + 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908, + 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b, + 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908, + 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, + 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808, + 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808, + 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08, + 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819, + 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919, + 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808, + 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808, + 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819, + 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819, + 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908, + 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908, + 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b, + 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908, + 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908, + 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908, + 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808, + 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, + 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819, + 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819, + 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808, + 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b, + 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819, + 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819, + 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08, + 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808, + 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19, + 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919, + 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, + 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19, + 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b, + 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808, + 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b, + 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b, + 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, + 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b, + 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808, + 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819, + 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808, + 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808, + 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08, + 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b, + 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19, + 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08, + 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919, + 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08, + 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08, + 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908, + 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908, + 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b, + 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908, + 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808, + 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b, + 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808, + 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808, + 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19, + 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08, + 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808, + 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b, + 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808, + 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b, + 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b}; +static const uint8_t host_ksigns_iq2xs[128] = {0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15, + 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159, + 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175, + 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63, + 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207, + 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95, + 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111, + 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255}; diff --git a/sycl/src/kernels/iq_multi_parity.cpp b/sycl/src/kernels/iq_multi_parity.cpp index 8707d4de..466d3f67 100644 --- a/sycl/src/kernels/iq_multi_parity.cpp +++ b/sycl/src/kernels/iq_multi_parity.cpp @@ -106,6 +106,32 @@ std::vector dequant_q8_1(const std::vector& q, size_t n) { return v; } +#include "iq2xs_host_tables.inc" +// IQ2_XS decoded on the CPU from ggml's tables (dequantize_row_iq2_xs), a reference independent of the GPU dequantizer +std::vector host_iq2_xs(const std::vector& w, size_t n) { + std::vector y(n); + const size_t nb = n / 256; + for (size_t i = 0; i < nb; ++i) { + const uint8_t* b = w.data() + i * 74; // fp16 d, 32 x uint16 qs, 8 scales + uint16_t dh; std::memcpy(&dh, b, 2); + const float d = k::f32_from_f16(dh); + const uint8_t* sc = b + 66; + for (int ib = 0; ib < 8; ++ib) { + const float db[2] = {d * (0.5f + (sc[ib] & 0xf)) * 0.25f, d * (0.5f + (sc[ib] >> 4)) * 0.25f}; + for (int l = 0; l < 4; ++l) { + uint16_t q; std::memcpy(&q, b + 2 + 2 * (4 * ib + l), 2); + const uint64_t g = host_iq2xs_grid[q & 511]; + const uint8_t signs = host_ksigns_iq2xs[q >> 9]; + for (int j = 0; j < 8; ++j) { + const float v = (float) ((g >> (8 * j)) & 0xff); + y[i * 256 + ib * 32 + l * 8 + j] = db[l / 2] * v * ((signs >> j) & 1 ? -1.f : 1.f); + } + } + } + } + return y; +} + // ------------------------------------------------------------------------------------------------ (1) iq_mmvq void check_mmvq(int t, int n_in, int n_out, dpct::queue_ptr s, std::mt19937 &rng) { @@ -148,6 +174,19 @@ void check_mmvq(int t, int n_in, int n_out, dpct::queue_ptr s, (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(xq.data(), dxq, xq_bytes).wait()), "xq"); const auto xd = dequant_q8_1(xq, (size_t) max_cols * n_in); + std::vector hw; + if (t == 17) { // IQ2_XS: the GPU dequantizer against the CPU decode + hw = host_iq2_xs(w, (size_t) n_out * n_in); + double md = 0, mx = 0; + size_t nd = 0; + for (size_t i = 0; i < hw.size(); ++i) { + md = std::max(md, (double) std::fabs(hw[i] - wf[i])); + mx = std::max(mx, (double) std::fabs(hw[i])); + nd += hw[i] != wf[i]; + } + std::printf(" IQ2_XS GPU dequant vs CPU decode: %zu of %zu differ, worst %.3g (max |w| %.3g); w[0..3] cpu %g %g %g %g gpu %g %g %g %g\n", + nd, hw.size(), md, mx, hw[0], hw[1], hw[2], hw[3], wf[0], wf[1], wf[2], wf[3]); + } float* dy_old = dalloc((size_t) max_cols * n_out); float* dy_new = dalloc((size_t) max_cols * n_out); std::vector y_old((size_t) max_cols * n_out), y_new(y_old.size()); @@ -190,6 +229,16 @@ void check_mmvq(int t, int n_in, int n_out, dpct::queue_ptr s, den += std::fabs(acc); } const double rel = num / (den + 1e-30); + if (t == 17 && nc == 1) { + double hn = 0, hd = 0; + for (int r = 0; r < n_out; ++r) { + double acc = 0; + for (int i = 0; i < n_in; ++i) acc += (double) hw[(size_t) r * n_in + i] * xd[(size_t) i]; + hn += std::fabs(y_new[(size_t) r] - acc); hd += std::fabs(acc); + } + std::printf(" IQ2_XS mmvq vs the CPU-decode reference: rel %.2e; y[0..2] gpu %g %g %g\n", hn / (hd + 1e-30), + y_new[0], y_new[1], y_new[2]); + } worst = std::max(worst, rel); if (diff != 0 || !finite || !(rel < 1e-2)) { std::printf(" %-8s %5d x %5d ncols %2d: %zu outputs differ from the old kernel, ref rel %.2e%s FAIL\n", diff --git a/sycl/src/prefill/prefill.cpp b/sycl/src/prefill/prefill.cpp index c0322e96..3a169817 100644 --- a/sycl/src/prefill/prefill.cpp +++ b/sycl/src/prefill/prefill.cpp @@ -2276,7 +2276,29 @@ bool Prefill::run(const int64_t *tokens, int64_t n, int64_t pos0, }; const int64_t bgu = bad(m.GU, T * K * 1280), bdm = bad(m.Dm, T * K * N), bbo = bad(m.bo, T * N); const int64_t bh = m.H ? bad(m.H, T * K * 640) : -1; + // the fp16 inputs of the experts' GEMMs: the gathered activations and the last dequantized + // gate/up (non-finite count, largest finite magnitude) + auto bad16 = [&](const uint16_t* d, int64_t n, double& mx) { + std::vector h((size_t) n); + dpct::get_in_order_queue().memcpy(h.data(), d, (size_t) n * 2).wait(); + int64_t c = 0; + mx = 0; + for (uint16_t v : h) { + if ((v & 0x7c00) == 0x7c00) { ++c; continue; } + const int e = (v >> 10) & 0x1f; + const double a = e ? std::ldexp(1.0 + (v & 0x3ff) / 1024.0, e - 15) : std::ldexp((v & 0x3ff) / 1024.0, -14); + mx = std::max(mx, a); + } + return c; + }; + double mxs = 0, mxq = 0; + const int64_t bxs = (!use_mmq && m.Xs) ? bad16(m.Xs, T * K * N, mxs) : -1; + const int64_t bdq = (!use_mmq && m.dq_gu[0]) ? bad16(m.dq_gu[0], (int64_t) 1280 * N, mxq) : -1; static int64_t reported = -1; + if ((bgu || bdm || bbo || bh > 0) && reported != stats_.chunks) + std::fprintf(stderr, "strata dbg: layer %lld fp16 inputs: activations %lld non-finite (max %.3g), " + "dequantized gate/up %lld non-finite (max %.3g)\n", (long long) l, (long long) bxs, mxs, + (long long) bdq, mxq); if ((bgu || bdm || bbo || bh > 0) && reported != stats_.chunks) { reported = stats_.chunks; std::fprintf(stderr, "strata dbg: layer %lld (mmq %d, types %d/%d, %zu experts): non-finite GU %lld "