[PR #4228] [Engine] dsv41: read Engram tables stored as ggml Q5_K #4963

Open
opened 2026-11-07 16:31:39 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4228
Author: @melchisedekko
Created: 10/26/2026
Status: 🔄 Open

Base: main ← Head: feat/dsv41-engram-q5k


📝 Commits (1)

  • b24c6a1 [Engine] dsv41: read Engram tables stored as ggml Q5_K

📊 Changes

8 files changed (+224 additions, -9 deletions)

View changed files

📝 CMakeLists.txt (+19 -0)
📝 docs/DEEPSEEK41.md (+2 -0)
📝 src/dsv41/ds4_engram_io.hpp (+1 -1)
📝 src/dsv41/ds4_kernels.cu (+11 -0)
📝 src/dsv41/ds4_kernels.cuh (+3 -1)
📝 src/dsv41/ds4_model.cu (+11 -7)
➕ src/dsv41/ds4_q5k.cuh (+38 -0)
➕ tests/dsv41/q5k_engram_test.cpp (+139 -0)

📄 Description

Problem and result

Unsloth's lower DeepSeek-V4.1-Flash-1017 GGUFs (seen on UD-IQ2_XXS) store the Engram table (per_layer_token_embd.weight, 135 GB) as Q5_K. pack_gguf.py packs them without complaint (it already knows Q5_K), but strata-dsv41 stops at load (observed on 4b9e05a2, exit code 2):

ds4: Engram layer 1: engram.embed.weight type Q5_K is not one the Engram reader handles (MXFP8, Q6_K, IQ4_NL)

With this change those packs load and answer. Every other Engram format reads exactly as before.

None found. I searched the open and closed issues and PRs for Q5_K / Engram / UD-IQ2_XXS. It is a small, self-contained format addition next to Q6_K, so I am opening it as a focused PR. It is a draft because I could not build the CUDA backend (see "Not run").

Approach

Q5_K becomes Engram row format 4 (3 is already the old two-tensor FP8 pack). It follows the Q6_K path step by step:

  • ds4_model.cu, format pick:
    • dtype == "Q5_K" selects format 4;
    • the same whole-superblock check as Q6_K (eg_hd % 256);
    • the refusal message now lists Q5_K.
  • ds4_model.cu, eg_issue: a Q5_K row is hd / 256 * 176 bytes, read into the row's code bytes like Q6_K and IQ4_NL. The row slot (hd + hd/32) is big enough.
  • ds4_q5k.cuh (new): q5k_value(block, i), one value of a ggml block_q5_K, computed the same way as dequantize_row_q5_K and get_scale_min_k4. Each product is rounded on its own (__fmul_rn, then __fsub_rn), as ds4_q6k.cuh does for Q6_K.
  • ds4_kernels.cu: a fmt == 4 branch in engram_dequant_kernel and engram_dequant_tok_kernel.
  • tests/dsv41/q5k_engram_test.cpp and the ds4_q5k_test target: random valid superblocks go through both kernels and are compared bit for bit with a copy of ggml's dequantize_row_q5_K. The test needs a GPU and returns 77 without one. It sits outside the STRATA_NATIVE_EXPERTS block because it needs no native experts.
  • docs/DEEPSEEK41.md: one sentence naming the formats the Engram reader accepts.

Compatibility: the existing formats' code paths are not changed. This was checked by reading the diff, which only adds else if / if (fmt == 4) branches. I did not run an end-to-end check on an MXFP8, Q6_K or IQ4_NL pack; see "Not run".

AI assistance: the code was drafted by DeepSeek V4.1 (via an agent harness). I reviewed it and wrote the validation tooling with Claude. I have read every changed line and can answer for it. All numbers below are from runs on my machine.

Validation

Environment: AMD Radeon RX 6800 XT (gfx1030, RDNA2; HIP, system ROCm 7.1, clang 21), Intel Core Ultra 9 285HX, 256 GB RAM, Ubuntu, Linux 7.0. Base revision 4b9e05a2. Model: unsloth/DeepSeek-V4.1-Flash-1017-GGUF UD-IQ2_XXS, 6 shards, packed with tools/dsv41/pack_gguf.py --model <dir> --out <pack> (no other options; no DSpark drafter, no vision).

Passed

  1. Builds, HIP gfx1030:

    • -DSTRATA_ENABLE_HIP=ON -DSTRATA_BUILD_DSV41=ON -DSTRATA_BUILD_TESTS=ON -DCMAKE_HIP_ARCHITECTURES=gfx1030
    • the same with -DSTRATA_NATIVE_EXPERTS=ON

    Targets strata-dsv41, ds4_q5k_test, ds4_tq_test and ds4_pick_rule_test build and link in both configurations.

  2. ds4_q5k_test: engram_dequant: 2048 values, 0 differ from ggml, engram_dequant_tok: 2048 values, 0 differ from ggml. Same result in both builds and through ctest -R ds4.

  3. ds4_tq_test: ALL OK. ds4_pick_rule_test: ok. python -m unittest serve.test_dsv41: 60 tests OK (1 skipped).

  4. strata-dsv41 --pack <pack> --pack-info: every tensor has a device type, and all files are present and long enough. Engram layers 1 and 14, Q5_K.

  5. Correctness against the reference implementation of Q5_K (llama.cpp reading the same GGUF).

    • Data: 5 × 1000 tokens, the texts in data/quant_quality/v1 (code, docs, english, multilingual, agent), tokenized with this GGUF's tokenizer. Note: set.ids.json itself holds Qwen ids; on DeepSeek it fails with token 188912 out of range.
    • Strata side: strata-dsv41 --score.
    • llama.cpp side: a small tool that writes the same rows (teacher-forced, fresh context per segment, no BOS, top 64), on llama.cpp with DeepSeek-V4.1 support (ggml-org/llama.cpp PR #30817, b72050d03), Vulkan, all 6 experts.
    strata-dsv41 run ppl vs llama.cpp same top-1 KL (llama.cpp's top-64)
    this PR, default (5 experts a token) ×1.025 91.3% 0.059
    this PR, --experts 6 ×1.015 94.2% 0.027
    ablation: Engram rows zeroed (throwaway build) ×1.231 80.9% 0.315
    ablation: each Engram row read from row id^1 (throwaway build) ×1.108 81.9% 0.288

    The ablations show that the Engram matters on this model: a wrong-row or wrong-decode bug would sit about 10× further from the reference than this PR does. Per set, with --experts 6, the ppl ratios are 1.072 / 0.983 / 0.985 / 1.028 / 1.011, so Strata is lower on two of the five.

  6. Server (serve.server --engine strata, arch: deepseek41, --max-context 262144). Measurements are from this machine only, not a claim for other hardware:

    • 16/16 on a small graded set (12 integer-answer math questions and 4 Python functions run against asserts; thinking on).
    • Thinking, OpenAI tool calls and streaming work.
    • Needle test: three invented facts at 10%, 50% and 90% of a prompt; 3/3 found at 111,889 tokens and at 173,551 tokens.
    • Speed, read / write t/s, one run each: 4.2k prompt 165.7 / 18.5; 24.7k prompt 267.8 / 17.1; 127.4k prompt 279.9 / 17.4.
    • For context, the same requests on llama.cpp PR #30817 (Vulkan, --n-cpu-moe 40), same files, same day: 19.0 / 8.0 at 4.2k and 18.6 / 7.5 at 24.7k. That comparison is between engines, not a before/after of this PR, which has no "before" since the pack was refused.

Failed

None observed with the final code. During development, the first version of the test laid out the chunked buffer with a per-row stride where the kernel takes a per-token stride. Only the test was wrong; the kernel was not changed.

Not run

  • CUDA build: there is no NVIDIA GPU or CUDA toolkit on this machine. ds4_kernels.cu and the new header are shared CUDA/HIP code with no HIP-only constructs, but that is unconfirmed static analysis. I would appreciate a CUDA build and ds4_q5k_test run from someone with a card.
  • SYCL / Metal: the dsv41 engine is not built there.
  • End-to-end runs on MXFP8, Q6_K or IQ4_NL Engram packs (the IST-DAS sets, UD-Q8_K_XL), to show their output is unchanged byte for byte: I do not have those files (100+ GB downloads). The claim that they are unchanged rests on the diff only.
  • Other Unsloth quants with Q5_K Engram tables: only UD-IQ2_XXS was tested.

Limits

  • Only the Engram table gains Q5_K. Other tensors' formats are untouched; this pack's dense and expert types were already supported.
  • gfx1030 is listed as unvalidated for HIP in CMakeLists.txt; all GPU results above are from that architecture.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GoernqqT5SefEMknYTo8Xx


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4228 **Author:** [@melchisedekko](https://github.com/melchisedekko) **Created:** 10/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/dsv41-engram-q5k` --- ### 📝 Commits (1) - [`b24c6a1`](https://github.com/Niko1221/Strata/commit/b24c6a1d8f2d12f9fc7221be57ee9bdc5811e8a0) [Engine] dsv41: read Engram tables stored as ggml Q5_K ### 📊 Changes **8 files changed** (+224 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+19 -0) 📝 `docs/DEEPSEEK41.md` (+2 -0) 📝 `src/dsv41/ds4_engram_io.hpp` (+1 -1) 📝 `src/dsv41/ds4_kernels.cu` (+11 -0) 📝 `src/dsv41/ds4_kernels.cuh` (+3 -1) 📝 `src/dsv41/ds4_model.cu` (+11 -7) ➕ `src/dsv41/ds4_q5k.cuh` (+38 -0) ➕ `tests/dsv41/q5k_engram_test.cpp` (+139 -0) </details> ### 📄 Description ## Problem and result Unsloth's lower DeepSeek-V4.1-Flash-1017 GGUFs (seen on `UD-IQ2_XXS`) store the Engram table (`per_layer_token_embd.weight`, 135 GB) as **Q5_K**. `pack_gguf.py` packs them without complaint (it already knows Q5_K), but `strata-dsv41` stops at load (observed on `4b9e05a2`, exit code 2): ``` ds4: Engram layer 1: engram.embed.weight type Q5_K is not one the Engram reader handles (MXFP8, Q6_K, IQ4_NL) ``` With this change those packs load and answer. Every other Engram format reads exactly as before. ## Related discussion None found. I searched the open and closed issues and PRs for Q5_K / Engram / UD-IQ2_XXS. It is a small, self-contained format addition next to Q6_K, so I am opening it as a focused PR. It is a **draft** because I could not build the CUDA backend (see "Not run"). ## Approach Q5_K becomes Engram row format `4` (`3` is already the old two-tensor FP8 pack). It follows the Q6_K path step by step: - `ds4_model.cu`, format pick: - `dtype == "Q5_K"` selects format 4; - the same whole-superblock check as Q6_K (`eg_hd % 256`); - the refusal message now lists Q5_K. - `ds4_model.cu`, `eg_issue`: a Q5_K row is `hd / 256 * 176` bytes, read into the row's code bytes like Q6_K and IQ4_NL. The row slot (`hd + hd/32`) is big enough. - `ds4_q5k.cuh` (new): `q5k_value(block, i)`, one value of a ggml `block_q5_K`, computed the same way as `dequantize_row_q5_K` and `get_scale_min_k4`. Each product is rounded on its own (`__fmul_rn`, then `__fsub_rn`), as `ds4_q6k.cuh` does for Q6_K. - `ds4_kernels.cu`: a `fmt == 4` branch in `engram_dequant_kernel` and `engram_dequant_tok_kernel`. - `tests/dsv41/q5k_engram_test.cpp` and the `ds4_q5k_test` target: random valid superblocks go through both kernels and are compared bit for bit with a copy of ggml's `dequantize_row_q5_K`. The test needs a GPU and returns 77 without one. It sits outside the `STRATA_NATIVE_EXPERTS` block because it needs no native experts. - `docs/DEEPSEEK41.md`: one sentence naming the formats the Engram reader accepts. Compatibility: the existing formats' code paths are not changed. This was checked by reading the diff, which only adds `else if` / `if (fmt == 4)` branches. I did not run an end-to-end check on an MXFP8, Q6_K or IQ4_NL pack; see "Not run". AI assistance: the code was drafted by DeepSeek V4.1 (via an agent harness). I reviewed it and wrote the validation tooling with Claude. I have read every changed line and can answer for it. All numbers below are from runs on my machine. ## Validation Environment: AMD Radeon RX 6800 XT (gfx1030, RDNA2; HIP, system ROCm 7.1, clang 21), Intel Core Ultra 9 285HX, 256 GB RAM, Ubuntu, Linux 7.0. Base revision `4b9e05a2`. Model: `unsloth/DeepSeek-V4.1-Flash-1017-GGUF` `UD-IQ2_XXS`, 6 shards, packed with `tools/dsv41/pack_gguf.py --model <dir> --out <pack>` (no other options; no DSpark drafter, no vision). **Passed** 1. Builds, HIP gfx1030: - `-DSTRATA_ENABLE_HIP=ON -DSTRATA_BUILD_DSV41=ON -DSTRATA_BUILD_TESTS=ON -DCMAKE_HIP_ARCHITECTURES=gfx1030` - the same with `-DSTRATA_NATIVE_EXPERTS=ON` Targets `strata-dsv41`, `ds4_q5k_test`, `ds4_tq_test` and `ds4_pick_rule_test` build and link in both configurations. 2. `ds4_q5k_test`: `engram_dequant: 2048 values, 0 differ from ggml`, `engram_dequant_tok: 2048 values, 0 differ from ggml`. Same result in both builds and through `ctest -R ds4`. 3. `ds4_tq_test`: `ALL OK`. `ds4_pick_rule_test`: ok. `python -m unittest serve.test_dsv41`: 60 tests OK (1 skipped). 4. `strata-dsv41 --pack <pack> --pack-info`: every tensor has a device type, and all files are present and long enough. Engram layers 1 and 14, Q5_K. 5. Correctness against the reference implementation of Q5_K (llama.cpp reading the same GGUF). - Data: 5 × 1000 tokens, the texts in `data/quant_quality/v1` (code, docs, english, multilingual, agent), tokenized with this GGUF's tokenizer. Note: `set.ids.json` itself holds Qwen ids; on DeepSeek it fails with `token 188912 out of range`. - Strata side: `strata-dsv41 --score`. - llama.cpp side: a small tool that writes the same rows (teacher-forced, fresh context per segment, no BOS, top 64), on llama.cpp with DeepSeek-V4.1 support (ggml-org/llama.cpp PR #30817, `b72050d03`), Vulkan, all 6 experts. | strata-dsv41 run | ppl vs llama.cpp | same top-1 | KL (llama.cpp's top-64) | |---|---|---|---| | this PR, default (5 experts a token) | ×1.025 | 91.3% | 0.059 | | this PR, `--experts 6` | ×1.015 | 94.2% | 0.027 | | ablation: Engram rows zeroed (throwaway build) | ×1.231 | 80.9% | 0.315 | | ablation: each Engram row read from row `id^1` (throwaway build) | ×1.108 | 81.9% | 0.288 | The ablations show that the Engram matters on this model: a wrong-row or wrong-decode bug would sit about 10× further from the reference than this PR does. Per set, with `--experts 6`, the ppl ratios are 1.072 / 0.983 / 0.985 / 1.028 / 1.011, so Strata is lower on two of the five. 6. Server (`serve.server --engine strata`, `arch: deepseek41`, `--max-context 262144`). Measurements are from this machine only, not a claim for other hardware: - 16/16 on a small graded set (12 integer-answer math questions and 4 Python functions run against asserts; thinking on). - Thinking, OpenAI tool calls and streaming work. - Needle test: three invented facts at 10%, 50% and 90% of a prompt; 3/3 found at 111,889 tokens and at 173,551 tokens. - Speed, read / write t/s, one run each: 4.2k prompt 165.7 / 18.5; 24.7k prompt 267.8 / 17.1; 127.4k prompt 279.9 / 17.4. - For context, the same requests on llama.cpp PR #30817 (Vulkan, `--n-cpu-moe 40`), same files, same day: 19.0 / 8.0 at 4.2k and 18.6 / 7.5 at 24.7k. That comparison is between engines, not a before/after of this PR, which has no "before" since the pack was refused. **Failed** None observed with the final code. During development, the first version of the test laid out the chunked buffer with a per-row stride where the kernel takes a per-token stride. Only the test was wrong; the kernel was not changed. **Not run** - CUDA build: there is no NVIDIA GPU or CUDA toolkit on this machine. `ds4_kernels.cu` and the new header are shared CUDA/HIP code with no HIP-only constructs, but that is unconfirmed static analysis. I would appreciate a CUDA build and `ds4_q5k_test` run from someone with a card. - SYCL / Metal: the dsv41 engine is not built there. - End-to-end runs on MXFP8, Q6_K or IQ4_NL Engram packs (the IST-DAS sets, UD-Q8_K_XL), to show their output is unchanged byte for byte: I do not have those files (100+ GB downloads). The claim that they are unchanged rests on the diff only. - Other Unsloth quants with Q5_K Engram tables: only UD-IQ2_XXS was tested. ## Limits - Only the Engram table gains Q5_K. Other tensors' formats are untouched; this pack's dense and expert types were already supported. - gfx1030 is listed as unvalidated for HIP in `CMakeLists.txt`; all GPU results above are from that architecture. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01GoernqqT5SefEMknYTo8Xx --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4963
No description provided.