[PR #41] [MERGED] GLM KV cache for 128K context: indexer key/gate ring, INT8 latent cache #295

Closed
opened 2026-10-29 21:23:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/41
Author: @merbanan
Created: 10/9/2026
Status: ✅ Merged
Merged: 10/9/2026
Merged by: @mw00

Base: main ← Head: kv-128k


📝 Commits (1)

  • 3cba358 GLM KV cache for 128K context: indexer key/gate ring, INT8 latent cache

📊 Changes

10 files changed (+264 additions, -77 deletions)

View changed files

📝 README.md (+2 -0)
📝 include/strata/core/glm_model.hpp (+11 -0)
📝 include/strata/kernels/glm_batch.hpp (+6 -4)
📝 include/strata/kernels/glm_fast.hpp (+7 -1)
📝 src/core/glm_fast_path.cu (+71 -10)
📝 src/core/glm_model.cu (+17 -5)
📝 src/core/glm_prefill.cu (+25 -10)
📝 src/kernels/cuda/glm_batch.cu (+66 -26)
📝 src/kernels/cuda/glm_fast.cu (+44 -20)
📝 src/program/generate.cpp (+15 -1)

📄 Description

Makes 128K context cheaper in VRAM on the GLM fast path, which leaves more room for experts. There are two parts and one debug option:

  • Indexer key/gate ring (always on). The DSA indexer's ik/ig caches (FP32, 2 x 128 per token and layer) are only read to pool each 4-token block. After that they are dead. The fast path now keeps a ring of 8192 + 64 positions instead of the whole context. That covers a prompt sub-batch (at most 8192) plus one open pool, and the NextN block's prompt cache fill writes in pieces of the ring. Kernels index the caches modulo the ring.
    • A snapshot keeps the open pool's rows, because later positions overwrite their ring slots.
    • Slot files carry those rows instead of full ik/ig runs. The slot magic changes to MAYALST2, so old slot files are refused and the conversation is read again.
    • The reference path (STRATA_GLM_SLOW) still keeps every position.
  • INT8 latent cache (opt-in: STRATA_GLM_KV_INT8=1, or --kv int8). The MLA latent is stored as 512 int8 codes plus one FP16 scale per 32 values: 544 bytes per token and layer instead of 1024.
    • Both dsa_prep kernels quantize (warp absmax).
    • Every reader dequantizes where it loads, so the attention arithmetic is unchanged. That covers decode mla / mla_attn / mla_split, and prompt mla_attn F32 / tensor-core / tensor-core-register / mma.sync (sm_80+) / rocWMMA (HIP).
  • STRATA_GLM_SCORE_PREFILL=1 (debug). It reads the unscored context of STRATA_GLM_SCORE through the prompt path, so a long context is scored in minutes.

The README's Tuning table gets rows for both new settings.

Measurements

All on 1x Tesla V100-PCIE-32GB (PCIe 3.0 x16), Ryzen 9 7950X, 62 GB RAM, Maya-S v2 IQ2_XXS, --max-context 131072, v1.0.16 vs v1.0.16 + this PR.

Memory, from the engine's own glm fast: lines:

state/KV expert pool VRAM expert slots
v1.0.16 3.06 GB 20.32 GB 3066
+ ring 1.78 GB 21.71 GB 3276
+ ring + STRATA_GLM_KV_INT8=1 1.13 GB 22.27 GB 3360

Long-context recall. Three facts are placed at 5 / 50 / 90% of a 60K-token prompt (README and docs text, repeated). They are asked in three turns, so the 2nd and 3rd go through conversation reuse; one run also sets STRATA_GLM_SLOT_ROUNDTRIP=1. All 3 were found in every run: stock x4, ring x2, ring + INT8 x4.

Speed, same harness.

  • Prefill (60K-token prompt): the first run after the GPU is freed is consistently the fastest, whichever binary it is.
    • First run: stock 626 / 607, patched 622 tok/s.
    • Later runs: stock 566 / 527, patched 557 / 548 / 547 / 534 / 521 / 519.
    • I see no difference beyond that ordering effect.
  • Decode (the three --bench questions, 256 tokens, greedy): stock 23.0 / 22.6 / 21.1 / 20.6, patched 22.7 / 22.4 / 21.8 / 21.8 / 21.4 tok/s. Same within noise.

INT8 quality. I scored 512 tokens after a 30K-token context (STRATA_GLM_SCORE) and compared KL against an FP16 log-softmax dump.

  • A second FP16 run gives KL 0.0276; INT8 gives 0.0277. With the CPU lane off, 0.0862 vs 0.0895.
  • NLL: FP16 1.702 (mean of 4 runs), INT8 1.708 (mean of 2).
  • INT8 is inside the engine's own run-to-run spread. These were measured on v1.0.11 with the same kernels; INT8 is opt-in, so default output is unchanged.

Output: with the defaults, only the ring changes, and it computes the same values (the same pooled keys, read from a ring instead of the full array). Runs differ in their last bits the usual way, depending on which experts run on the CPU or the GPU.

Tests run

  • glm_dsa_parity, glm_ffn_parity, glm_hc_parity, glm_kda_parity --selftest: PASS.
  • glm_layer_parity could not run here: no data/glm5-synth-dumps.
  • python -m unittest serve.test_server serve.test_tools serve.test_mcp serve.test_detok: 102 tests OK (3 skipped).

Not tested

  • Two or more GPUs.
  • Other NVIDIA generations: the sm_80+ mma.sync kernel's INT8 load path is compiled but not run.
  • AMD: the rocWMMA kernel's INT8 path only builds behind STRATA_USE_HIP; the NVIDIA code path is as above.

🔄 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/mw00/project-maya/pull/41 **Author:** [@merbanan](https://github.com/merbanan) **Created:** 10/9/2026 **Status:** ✅ Merged **Merged:** 10/9/2026 **Merged by:** [@mw00](https://github.com/mw00) **Base:** `main` ← **Head:** `kv-128k` --- ### 📝 Commits (1) - [`3cba358`](https://github.com/mw00/project-maya/commit/3cba358baaa1232749d751de542d58da4b0d3e61) GLM KV cache for 128K context: indexer key/gate ring, INT8 latent cache ### 📊 Changes **10 files changed** (+264 additions, -77 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+2 -0) 📝 `include/strata/core/glm_model.hpp` (+11 -0) 📝 `include/strata/kernels/glm_batch.hpp` (+6 -4) 📝 `include/strata/kernels/glm_fast.hpp` (+7 -1) 📝 `src/core/glm_fast_path.cu` (+71 -10) 📝 `src/core/glm_model.cu` (+17 -5) 📝 `src/core/glm_prefill.cu` (+25 -10) 📝 `src/kernels/cuda/glm_batch.cu` (+66 -26) 📝 `src/kernels/cuda/glm_fast.cu` (+44 -20) 📝 `src/program/generate.cpp` (+15 -1) </details> ### 📄 Description Makes 128K context cheaper in VRAM on the GLM fast path, which leaves more room for experts. There are two parts and one debug option: - **Indexer key/gate ring (always on).** The DSA indexer's `ik`/`ig` caches (FP32, 2 x 128 per token and layer) are only read to pool each 4-token block. After that they are dead. The fast path now keeps a ring of 8192 + 64 positions instead of the whole context. That covers a prompt sub-batch (at most 8192) plus one open pool, and the NextN block's prompt cache fill writes in pieces of the ring. Kernels index the caches modulo the ring. - A snapshot keeps the open pool's rows, because later positions overwrite their ring slots. - Slot files carry those rows instead of full `ik`/`ig` runs. The slot magic changes to `MAYALST2`, so old slot files are refused and the conversation is read again. - The reference path (`STRATA_GLM_SLOW`) still keeps every position. - **INT8 latent cache (opt-in: `STRATA_GLM_KV_INT8=1`, or `--kv int8`).** The MLA latent is stored as 512 int8 codes plus one FP16 scale per 32 values: 544 bytes per token and layer instead of 1024. - Both `dsa_prep` kernels quantize (warp absmax). - Every reader dequantizes where it loads, so the attention arithmetic is unchanged. That covers decode `mla` / `mla_attn` / `mla_split`, and prompt `mla_attn` F32 / tensor-core / tensor-core-register / mma.sync (sm_80+) / rocWMMA (HIP). - **`STRATA_GLM_SCORE_PREFILL=1` (debug).** It reads the unscored context of `STRATA_GLM_SCORE` through the prompt path, so a long context is scored in minutes. The README's Tuning table gets rows for both new settings. ## Measurements All on 1x Tesla V100-PCIE-32GB (PCIe 3.0 x16), Ryzen 9 7950X, 62 GB RAM, Maya-S v2 IQ2_XXS, `--max-context 131072`, v1.0.16 vs v1.0.16 + this PR. **Memory**, from the engine's own `glm fast:` lines: | | state/KV | expert pool | VRAM expert slots | |---|---:|---:|---:| | v1.0.16 | 3.06 GB | 20.32 GB | 3066 | | + ring | 1.78 GB | 21.71 GB | 3276 | | + ring + `STRATA_GLM_KV_INT8=1` | 1.13 GB | 22.27 GB | 3360 | **Long-context recall.** Three facts are placed at 5 / 50 / 90% of a 60K-token prompt (README and docs text, repeated). They are asked in three turns, so the 2nd and 3rd go through conversation reuse; one run also sets `STRATA_GLM_SLOT_ROUNDTRIP=1`. All 3 were found in every run: stock x4, ring x2, ring + INT8 x4. **Speed, same harness.** - **Prefill (60K-token prompt):** the first run after the GPU is freed is consistently the fastest, whichever binary it is. - First run: stock 626 / 607, patched 622 tok/s. - Later runs: stock 566 / 527, patched 557 / 548 / 547 / 534 / 521 / 519. - I see no difference beyond that ordering effect. - **Decode** (the three `--bench` questions, 256 tokens, greedy): stock 23.0 / 22.6 / 21.1 / 20.6, patched 22.7 / 22.4 / 21.8 / 21.8 / 21.4 tok/s. Same within noise. **INT8 quality.** I scored 512 tokens after a 30K-token context (`STRATA_GLM_SCORE`) and compared KL against an FP16 log-softmax dump. - A second FP16 run gives KL 0.0276; INT8 gives 0.0277. With the CPU lane off, 0.0862 vs 0.0895. - NLL: FP16 1.702 (mean of 4 runs), INT8 1.708 (mean of 2). - INT8 is inside the engine's own run-to-run spread. These were measured on v1.0.11 with the same kernels; INT8 is opt-in, so default output is unchanged. **Output:** with the defaults, only the ring changes, and it computes the same values (the same pooled keys, read from a ring instead of the full array). Runs differ in their last bits the usual way, depending on which experts run on the CPU or the GPU. ## Tests run - `glm_dsa_parity`, `glm_ffn_parity`, `glm_hc_parity`, `glm_kda_parity --selftest`: PASS. - `glm_layer_parity` could not run here: no `data/glm5-synth-dumps`. - `python -m unittest serve.test_server serve.test_tools serve.test_mcp serve.test_detok`: 102 tests OK (3 skipped). ## Not tested - Two or more GPUs. - Other NVIDIA generations: the sm_80+ mma.sync kernel's INT8 load path is compiled but not run. - AMD: the rocWMMA kernel's INT8 path only builds behind `STRATA_USE_HIP`; the NVIDIA code path is as above. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:23:54 +07:00
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#295
No description provided.