Skip to content

cuda, llama, server : end-of-prompt context checkpoint inside the last prompt pass (exact GDN rollback snapshots) - #247

Open
gaetan-puleo wants to merge 3 commits into
masterfrom
strix/ckpt-rs-exact
Open

gaetan-puleo wants to merge 3 commits into
masterfrom
strix/ckpt-rs-exact

Conversation

@gaetan-puleo

Copy link
Copy Markdown
Collaborator

Overview

For recurrent / hybrid models (Qwen3.8 Flash Next: Gated DeltaNet layers) llama-server creates a context checkpoint
4 tokens before the end of every prompt, so that the next turn can restore it instead of re-processing the prompt. To do
that it decodes the last 4 tokens in a separate batch: one more full read of the weights per prompt, about 250 ms on
Strix Halo. In chat, the checkpoint at the start of the last user message added a second such pass, right after the
template's system prefix (position 42 on a single-turn prompt).

This PR takes the end-of-prompt checkpoint inside the last prompt pass instead:

  1. cuda (HIP RDNA3.5): GATED_DELTA_NET with K > 1 rollback snapshots used the per-token scan. On a prefill-sized
    ubatch of one sequence it now runs the chunked path over all the tokens exactly as with K = 1 (same outputs, same final
    state), and recomputes the K - 1 other snapshots beside it from a state the scan dumps before one chunk (their outputs
    are discarded). The dump is a template parameter, so the kernels without it are unchanged. The device advertises this
    through the backend registry (ggml_backend_gdn_rs_exact).
  2. llama: LLAMA_STATE_SEQ_FLAGS_RS_BACK(k) writes the recurrent state of a sequence as it was k tokens before the
    end of its last ubatch, from rollback snapshot plane k.
  3. server: where every GPU advertises exact snapshots, the context keeps 4 of them, the prompt is not split, and the
    checkpoint is read with RS_BACK(4) after the first token is out (before the next decode, which overwrites the
    snapshots). Also with MTP and the n-gram drafts; EAGLE3 / DFlash / DSpark / standalone drafts keep the separate pass.
    A user-message checkpoint closer to the start of the prompt than one ubatch is no longer split out either: restoring
    it saves less than the pass costs on every prompt. --no-ctx-checkpoint-snapshots restores the previous behaviour.

Other backends (Vulkan, CUDA, CPU) advertise nothing and keep the current behaviour: there K > 1 would switch the prefill
to the per-token scan.

The K > 1 plumbing of the chunked path (token ranges, conv history at a token, the K = 2/4 test cases) comes from #243,
closed: its tail tokens produced their outputs from separate runs, which changed the logits.

Measurements

Device:     Ryzen AI Max+ 395 (strix-halo-002)
Memory:     128 GB LPDDR5X-8000
Power:      stock
BIOS:       UMA split default
Kernel:     7.2.4-200.fc44.x86_64, amdgpu params: ttm.pages_limit=32505856
Backend:    ROCm 10.1 (HIP 7.16.26385), HIP_LAUNCH_BLOCKING=0
Build:      -DGGML_HIP=ON -DGPU_TARGETS=gfx1151 -DCMAKE_BUILD_TYPE=Release -DGGML_HIP_NO_VMM=ON
Baseline:   f5c0a7a34 (master), built and run in this session
Change:     this branch
Model:      unsloth/Qwen3.8-Flash-Next-GGUF, UD-Q4_K_XL (+ MTP: mtp-Qwen3.8-Flash-Next-shared-Q8_0)

llama-server -c 262144 -b 4096 -ub 4096 -np 1 -fa on --cache-ram 0 -lm mmap -lzm on-direct --jinja, MTP runs add
--spec-type draft-mtp,ngram-mod --spec-draft-n-max 3 --spec-draft-n-ubatch 512. One server at a time, nothing else
on the machine. uvx llama-benchy --pp 512 --tg 32 --runs 5 --no-cache (chat completions), prefill t/s (at depth 10k
it counts the 10512 prompt tokens):

master this PR change
depth 0, no MTP 605 (549-636) 938 (908-958) +55 %
depth 0, MTP 541 (522-566) 852 (791-887) +57 %
depth 10k, no MTP 1668 (1546-1750) 1655 (1579-1728) = (noise)
depth 10k, MTP 1263 (1184-1345) 1517 (1437-1631) +20 %

Depth 0 without MTP, master --ctx-checkpoints 0 (no checkpoint at all) gives 979 (912-1067): the PR is 4 % under it.
At depth 10k the saved pass is about 4 % of the request and within the run-to-run noise; with MTP the gain there comes
from the chunked path replacing the per-token scan. For reference, #243 alone measured 619 / 568 / 1689 / 1521 on the
same four rows.

Time-to-first-token at depth 0: 851 -> 548 ms (no MTP), 950 -> 604 ms (MTP).

Correctness:

  • Logits: on raw /completion prompts of 512 and 3000 tokens, the top-10 log-probabilities of the first 8 generated
    tokens are bit-identical to master --ctx-checkpoints 0 (one pass, no split). master with its default checkpoints
    differs from both by up to 0.1-0.3 nat: that is its split (4-token pass) against one pass.
  • A prompt whose last 2 tokens change restores the checkpoint (cache_n = n - 4, 4 tokens re-processed), at 3000 and
    10000 tokens. That continuation is not bit-identical to the same prompt processed cold in one pass (restore + 4 tokens
    rounds like master's split, not like one pass); on master both paths are split and match.
  • With the capability off (GGML_CUDA_DISABLE_GDN_RS_SPLIT=1), the server splits 508 + 4 as on master.
  • test-backend-ops test -b ROCm0 -o GATED_DELTA_NET: 75/75.
  • HIP quality check: build with -DCMAKE_HIP_FLAGS="-Werror -Wno-tautological-compare" clean;
    gcn-cdna-vgpr-check.py against gfx1151-vgpr-baseline.json: zero VGPR spills, complete baseline coverage.

Requirements

  • I have read and agree with the contributing guidelines
  • This change is Strix Halo specific, or justified by measurements on Strix Halo. General llama.cpp improvements
    belong in halo-box/llama.cpp instead
  • AI usage disclosure: AGENT-AUTHORED - written by Claude (Opus 5.5) in Claude Code at my request: the analysis, the
    change, and the measurements above.

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

Changes requested. The GDN kernel part is clean, but the server part can store a checkpoint from a snapshot plane that the last pass did not write, and restoring it changes the output. It also turns on 4 rollback snapshots for every rollback-capable model on ROCm, which costs memory and decode speed for all users.

Direction

Accept after work. We want the extra 4-token pass gone, and the one-pass snapshot kernel does that: TTFT at 512 tokens drops by 19 % on ROCm. The gain only exists for short uncached prompts, so the cost must stay with the cases that gain. If you prefer to land the kernel first, a PR with only commit 1 (plus the test changes in ask 5 and without the env var) is welcome on its own.

Asks

  1. Blocking. tools/server/server-context.cpp:4427: n_prompt_in > 4 checks the whole prompt, not the tokens of this slot in the last ubatch. After a last pass of 1 to 4 tokens, RS_BACK(4) reads a plane that pass did not write, and the server stores it as a checkpoint at n-5 or uses it to replace the good n-4 checkpoint. Take the in-pass checkpoint only when the slot had at least n_rs_seq + 1 tokens in the last ubatch, else keep the split. Also make state_write (src/llama-memory-recurrent.cpp:800-805) fail when k is larger than the token count of that sequence in its last ubatch.
  2. Blocking. tools/server/server-context.cpp:1386-1388 with common/common.cpp:1716: n_rs_seq_ckpt = 4 applies to every rollback-capable model (LFM2 too) on any RDNA3.5 device, and it overrides --spec-n-rs-seq 0. On ROCm we measured recurrent state x5 per slot (62.8 to 314.1 MiB), server decode -8.3 %, and TTFT +7 % at 3000 tokens. Turn it on only when the model and the devices it runs on take the chunked rollback path, respect the user cap, and post decode numbers.
  3. Should fix. :4259 checks llama_n_rs_seq(ctx_tgt) >= 4, so with MTP and --spec-draft-n-max 4 the in-pass path also runs on Vulkan, where ask 1 shows too. worth_a_pass (:4367) drops the user-message checkpoint on every backend; Granite chat output changed on ROCm and Vulkan. Gate both on the exact-snapshot capability, or move the worth_a_pass change into its own measured commit.
  4. Should fix. Move LLAMA_STATE_SEQ_FLAGS_RS_BACK from include/llama.h:969-970 to src/llama-ext.h with its preconditions documented. Drop GGML_CUDA_DISABLE_GDN_RS_SPLIT (gated_delta_net.cu:1582, :1591): master just removed the HIP env gates, and --no-ctx-checkpoint-snapshots covers the user need.
  5. Should fix. tests/test-backend-ops.cpp:6120-6125 and :6342-6344 loosen the K > 1 tolerance to 1e-6 on every backend; gate it to HIP through the backend overload. Add a K = 5 case (the value the server uses), a case where n_tokens - (K - 1) < 256, and an RS_BACK case in tests/llama-context/cases-rs.cpp. For prompts with images (has_mtmd, :4384/:4427) keep the split, so that multimodal chats keep their end-of-prompt checkpoint.

Nits: loop over sequences in the rs_split branch instead of requiring n_seqs == 1 (concurrent 2-slot prefill is about 10 % slower); add the new flag to tools/server/README.md; the COMPLETION check at :4707 is dead; the state-dump block is copied in two kernels; the number 4 repeats in three places; name the device in the benchmark block in the CONTRIBUTING.md form.

Correctness

FAIL. test-backend-ops GATED_DELTA_NET passes 75/75 on ROCm (with HIP_LAUNCH_BLOCKING unset and =1) and on Vulkan, the full suites have no new failure, and the -Werror build and the VGPR spill gate pass. f32 logits and tokens, KLD/PPL at -c 2048, and 256-token greedy decode are byte-identical to master on all four corpora on both backends, but that path does not use the feature. The server restore test fails: after a 3-token request, the next restore gives different output from a cold run of the same prompt (first-token top-1 flips on ROCm; 8 of 12 cases differ on Vulkan with MTP against 1 of 12 on master; all 4 slots differ at -np 4 on ROCm against none on master). --no-ctx-checkpoint-snapshots gives the master behavior in 39 of 39 cases. Dense, Mamba2 and LFM2 models do not abort.

Performance

ROCm, Qwen3.5-35B-A3B, server master this PR change
TTFT, 512-token prompt (ms, median of 10) 293.0 237.3 -19 %
TTFT, 3000-token prompt (ms, median of 10) 1522.3 1632.9 +7 %
decode after 512 tokens (t/s, 4 runs) 72.4-73.3 66.4-67.1 -8.3 %
recurrent state per slot (MiB) 62.81 314.06 x5

llama-bench PP2048 at depths 0 to 64000 and tg128 do not change on either backend (it does not set n_rs_seq). The TTFT gain at 512 tokens matches the PR; at 3000 tokens all 10 paired runs are slower. Part of the gain moves to the second token (+16 to +31 ms).

Verification

Strix Halo (gfx1151), ROCm and Vulkan. The PR was 57 commits behind master and conflicted in gated_delta_net.cu, because master removed GGML_GDN_CHUNK and GGML_GDN_CHUNK_MIN. We rebased it onto master 0c1021e1f (resolution below) and compared it against master. We did not push the rebase; please apply the same resolution when you rebase.

Not tested

Long KLD (-c 16384), the server pytest suite, multimodal prompts, MTP at 10k depth, and bench-spec-prefill.sh acceptance.

Thanks for the careful kernel work and the measurements.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, Radeon 8060S (gfx1151).
  • ROCm 10.1, hipconfig 7.16.26385, AMD clang 24.0.0git d6f6cb691863.
  • Mesa RADV 26.0.8, Vulkan loader 1.4.341.0, shaderc 2026.1.
  • cmake 4.2.3, gcc 15.2.0.

Compared

  • master tip (base): 0c1021e1f685531fcdcc7f28d81b7b534adc508f
  • PR head: 9b7c19212547796a4e89a2082973c49e0f70dfc7
  • merge base: f5c0a7a3433fbebc0babced326664e461991a6b4, 57 commits behind master
  • head under test: the PR rebased onto master, 1e56462f46f0c655189c7514d73d8897c2140dc7 (commits 0688fdd11, 68ec91137, 1e56462f4; authors kept)

Rebase

git merge conflicts in ggml/src/ggml-cuda/gated_delta_net.cu (2 hunks: gdn_chunk_eligible_raw and ggml_cuda_gdn_z16_ok). Master commit d8f3d1df1 removed gdn_chunk_enabled() and gdn_chunk_min_tokens() and added static constexpr int64_t GDN_CHUNK_MIN_TOKENS = 256. Two more PR lines that call the removed functions merge without a conflict and then fail to build. Resolution: keep the PR side of both hunks, use GDN_CHUNK_MIN_TOKENS, and drop gdn_chunk_enabled() (the chunked path is always on in master). Commits 2 and 3 apply unchanged. The rebase was not pushed (maintainer edits are off). Patch on top of the rebased first commit, against the PR version of the file:

 bool ggml_cuda_gdn_rs_exact(const int device) {
 #if defined(GGML_USE_HIP)
     static const bool off = getenv("GGML_CUDA_DISABLE_GDN_RS_SPLIT") != nullptr;
-    return !off && gdn_chunk_enabled() && GGML_CUDA_CC_IS_RDNA3_5(ggml_cuda_info().devices[device].cc);
+    return !off && GGML_CUDA_CC_IS_RDNA3_5(ggml_cuda_info().devices[device].cc);
@@
 static bool gdn_rs_split_ok(const int K, const int64_t n_tokens, const int64_t n_seqs) {
     static const bool off = getenv("GGML_CUDA_DISABLE_GDN_RS_SPLIT") != nullptr;
-    return K <= 1 || (!off && n_seqs == 1 && n_tokens > K && n_tokens - (K - 1) >= gdn_chunk_min_tokens());
+    return K <= 1 || (!off && n_seqs == 1 && n_tokens > K && n_tokens - (K - 1) >= GDN_CHUNK_MIN_TOKENS);
@@ gdn_chunk_eligible_raw
     return !kda && gdn_rs_split_ok(K, n_tokens, n_seqs) && GGML_CUDA_CC_IS_RDNA3_5(cc) && S_v == 128 && n_seqs >= 1 &&
-        n_tokens >= gdn_chunk_min_tokens() && aligned && Hk > 0 && H % Hk == 0 && H / Hk <= 4 && gdn_chunk_enabled();
+        n_tokens >= GDN_CHUNK_MIN_TOKENS && aligned && Hk > 0 && H % Hk == 0 && H / Hk <= 4;
@@ ggml_cuda_gdn_z16_ok
-    return GGML_CUDA_CC_IS_RDNA3_5(cc) && !kda && gdn_rs_split_ok(K, v->ne[2], v->ne[3]) && v->ne[0] == 128 && v->ne[2] >= gdn_chunk_min_tokens() &&
+    return GGML_CUDA_CC_IS_RDNA3_5(cc) && !kda && gdn_rs_split_ok(K, v->ne[2], v->ne[3]) && v->ne[0] == 128 && v->ne[2] >= GDN_CHUNK_MIN_TOKENS &&

The two gdn_chunk_enabled()/gdn_chunk_min_tokens() definitions are dropped in favor of master's constant; the early return of ggml_cuda_gdn_z16_ok keeps master's form (no !gdn_chunk_enabled()).

Build

cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
# head only: the HIP line plus -DCMAKE_HIP_FLAGS="-Werror -Wno-tautological-compare", target ggml-hip
# spill gate, both trees: the HIP line plus -DGGML_HIP_EXPORT_METRICS=On -DCMAKE_HIP_FLAGS="", then
python3 <base>/scripts/hip/gcn-cdna-vgpr-check.py <tree>/build-hip-metrics --baseline <base>/scripts/hip/gfx1151-vgpr-baseline.json --compiler-version "<clang --version | head -n1>"

All 8 builds rc=0. -Werror build clean (13 ld.lld: warning: loop not unrolled linker remarks). Spill gate PASS on base (6288 kernels) and head (6294 kernels), 0 VGPR spills.

Models

  • GDN target: Qwen3.5-35B-A3B UD-IQ2_XXS GGUF (unsloth), sha256 2a809de317cfd49ac9130b95619ee6ac039a855e467015b3e996eb61af84718b
  • GDN with MTP head: Qwen3.8-27B UD-Q4_K_XL GGUF, sha256 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e
  • dense: Qwen2.5-1.5B-Instruct q4_k_m (Qwen), sha256 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e
  • Mamba2 hybrid: granite-4.0-h-1b Q4_K_M, sha256 da3d737121a96f3c9a316685212376257a7f167b74380855666dd488d6af3bcb
  • LFM2-700M Q4_K_M, sha256 684e8406dc13321452b3f6aeca432776e2a6a7e1ad6c23f7887b8fe3efbe2efa; LFM2-350M Q4_K_M for test-llama-context

Commands

# ops
test-backend-ops test -b <dev> -o GATED_DELTA_NET              # ROCm also with HIP_LAUNCH_BLOCKING=1 and with GGML_CUDA_DISABLE_GDN_RS_SPLIT=1
test-backend-ops test -b <dev> -j 4                            # ROCm also with HIP_LAUNCH_BLOCKING=1
ctest --test-dir <build> -L main -E 'test-opt|test-llama-archs'
test-llama-archs -b <dev> -a qwen35moe ; test-llama-archs -b <dev> -a qwen35
test-llama-context -m <qwen2.5-1.5b | lfm2-350m | qwen3.5-35b-a3b>
test-vk-exact
# parity, 4 corpora, base and head
llama-debug -m <35b> -f <corpus> -c 16384 -b 16384 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --save-logits --logits-output-dir <dir>   # cmp logits and tokens
llama-perplexity -m <35b> -f <corpus> -c 2048 -b 2048 -ub 2048 -ngl 99 -fa on -ctk f16 -ctv f16 --kl-divergence-base <file>          # cmp, then head --kl-divergence against base
llama-completion -m <35b> -f <corpus> -n 256 --temp 0 --seed 1 -no-cnv --ignore-eos -ngl 99 -fa on -c 16384                         # base twice, then head
# server
llama-server -m <35b> -dev <dev> -ngl 99 -fa on -c 32768 -b 2048 -ub 512 --cache-ram 0 -lv 4 -np {1,2,4}
#   variants: master; PR; PR --no-ctx-checkpoint-snapshots; master --ctx-checkpoints 0; PR --spec-n-rs-seq 0
#   MTP: -m <27b> -np 1 --spec-type draft-mtp --spec-draft-n-max 4
#   client: POST /completion, prompt as token array, temperature 0, n_probs 5, return_tokens, cache_prompt true;
#   cold reference: POST /slots/0?action=erase, then cache_prompt false
# restore cases, P = n corpus tokens (n = 300, 700, 1500), S = 40 other tokens
#   b: P, then P[:n-4]+S
#   c (k = 3,4,5,6): P, then P[:n-1], then P[:n-k]+S ; control: same without the middle request
#   g: P three times, then P[:n-3]+S
# disk: --slot-save-path <dir>, POST /slots/0?action=save, /proc/<pid>/io write_bytes before and after
# bench, 2 rounds, base and head alternated
llama-bench -m <35b> -p 2048 -d 0,12000,32000,64000 -b 2048 -ub 2048 -n 0 -r 5 -ngl 99 -fa on -ctk f16 -ctv f16 --load-mode none
llama-bench -m <35b> -p 0 -n 128 -r 5 -ngl 99 -fa on
GGML_CUDA_DISABLE_GRAPHS=1 test-backend-ops perf -b ROCm0 -o GATED_DELTA_NET    # Vulkan without the env var
# server-test scripts from master, ROCm, --jinja, -np 1

Results

Op tests:

test ROCm master ROCm PR Vulkan master Vulkan PR
GATED_DELTA_NET 69/69 75/75 69/69 75/75
GATED_DELTA_NET, HIP_LAUNCH_BLOCKING=1 69/69 75/75 - -
GATED_DELTA_NET, GGML_CUDA_DISABLE_GDN_RS_SPLIT=1 69/69 72/75 - -
full suite 40417/40417 40426/40426 43847/43847 43856/43856
full suite, HIP_LAUNCH_BLOCKING=1 40417/40417 40426/40426 - -

With GGML_CUDA_DISABLE_GDN_RS_SPLIT=1, 3 of the new cases fail on ROCm: head_count=16, n_seq_tokens=2048|4096, v_repeat=3, K=4 (ERR 1.05 to 1.80 against 1e-6). They pass without the env var and on Vulkan. With the env var set the op should take master's per-token K > 1 path, so this may be an existing master issue that the new test exposes; we did not confirm it on master.

Other tests: ctest -L main, test-llama-archs, test-llama-context and test-vk-exact give the same results on master and the PR (the failures that exist are on master too: test-tokenizers-ggml-vocabs; on Vulkan a test-llama-context subprocess abort; Qwen3.5-35B-A3B test-llama-context replay multi-seq max diff 1.377484 on both; test-vk-exact 11 failing checks on both).

Model parity (Qwen3.5-35B-A3B), both backends: llama-debug logits and tokens identical 4/4; KLD files identical 4/4, mean KLD 0.000000; PPL ROCm prose 7.5440, code 2.1911, structured 1.7398, numeric 1.4837 (Vulkan 7.5426, 2.1891, 1.7309, 1.4800) on both sides; greedy decode A/A identical and master = PR for prose and code.

Server TTFT (prompt_ms, 10 runs = 2 rounds x 5 prompts; median [min-max]; token-2 gap median in ms):

backend variant 512 tokens 3000 tokens token-2 gap 512 / 3000
ROCm master 293.0 [284.2-319.9] 1522.3 [1387.5-1632.2] 16.1 / 20.6
ROCm PR 237.3 [231.8-252.7] 1632.9 [1482.5-1748.2] 32.0 / 51.4
ROCm PR, --no-ctx-checkpoint-snapshots 296.9 [286.8-318.4] 1544.2 [1399.9-1631.7] 16.1 / 20.5
ROCm master, --ctx-checkpoints 0 221.9 [217.0-232.6] 1458.6 [1357.7-1503.6] 16.7 / 22.1
Vulkan master 323.0 [317.0-330.4] 1852.9 [1734.2-1916.8] 14.9 / 21.4
Vulkan PR 322.1 [316.8-330.9] 1879.4 [1794.2-1918.9] 14.8 / 21.3
Vulkan PR, --no-ctx-checkpoint-snapshots 322.8 [317.1-329.7] 1864.9 [1804.6-1922.3] 14.8 / 21.2
Vulkan master, --ctx-checkpoints 0 294.3 [290.3-304.3] 1815.5 [1749.9-1860.5] 15.5 / 23.0

ROCm 3000 tokens per run: master 1387 1445 1448 1541 1514 1420 1632 1544 1582 1531; PR 1483 1742 1630 1636 1602 1500 1748 1637 1650 1607 (PR slower in 10/10 pairs, paired median +94 ms).

MTP (Qwen3.8-27B, --spec-draft-n-max 4), prompt_ms per run:

512 tokens 3000 tokens token-2 gap at 512
ROCm master 1455, 1339, 1338, 1442, 1498 8060, 8216, 7978, 8033, 8049 87-127
ROCm PR 1178, 1218, 1255, 1294, 1303 7918, 8028, 7863, 7854, 7869 117-197
Vulkan master 1186, 1129, 1126, 1173, 1232 6963, 6917, 6907, 6895, 6905 83-92
Vulkan PR 1062, 1013, 1048, 1089, 1098 6985, 6901, 6913, 6907, 6897 128-158

Restore correctness: number of cases whose tokens differ from a cold run of the same final prompt:

server c, master c, PR control, master control, PR b master / PR g master / PR
ROCm, 35B 6/12 10/12 4/12 4/12 1/3 / 1/3 3/3 / 2/3
Vulkan, 27B + MTP 1/12 8/12 0/12 0/12 0/3 / 0/3 0/3 / 2/3
ROCm, 27B + MTP 5/12 10/12 2/12 3/12 2/3 / 0/3 2/3 / 2/3
Vulkan, 35B, no MTP (feature off) 6/12 6/12 (identical to master) 2/12 2/12 1/3 / 1/3 2/3 / 2/3

Trace for c, n=700, k=4 (ROCm, and Vulkan with MTP): the middle request restores the checkpoint at 696 and processes 3 tokens. On master it creates no new checkpoint. On the PR it also logs created context checkpoint 3 (pos_min = 694, pos_max = 694, n_tokens = 695), and the next request restores 695. The same happens at n=300 (295) and n=1500 (1495). The first request on the PR also logs a superseding checkpoint at 696 after a 4-token pass. ROCm 35B first-token max |delta logprob| against the cold run: master, 30 restores, at most 0.406; PR, n=700 k=4: 1.440 with a top-1 flip. On the 27B, master restores agree with the cold run to at most 1.6e-2; the PR reaches 7.9e-2. PR vs PR --no-ctx-checkpoint-snapshots: identical in 39/39 cases.

Multi-slot (1500-token prompts, 32 tokens; c4 = P[:n-3]+S against cold), first diverging token per slot: -np 2 master [0, =], PR [6, 5], PR no-snapshots [=, =]; -np 4 master [=, =, =, =], PR [6, 5, 0, 0], PR no-snapshots [0, =, 0, =]. Concurrent prefill of 2000-token prompts, wall ms for 3 runs: -np 2 master 2187/2355/2287, PR 2479/2382/2776, PR no-snapshots 2158/2258/2235; -np 4 master 5108/5531/5107, PR 5248/5810/5075 (inside noise).

Memory (llama_memory_recurrent: size): ROCm -np 1 master 62.81 MiB (n_rs_seq 0), PR 314.06 MiB (n_rs_seq 4), PR --spec-n-rs-seq 0 314.06 MiB; -np 4 251.25 vs 1256.25 MiB. Vulkan unchanged (n_rs_seq 0). LFM2-700M on ROCm: n_rs_seq 4 on the PR (0.12 to 0.59 MiB) and the same n-5 checkpoint after a 3-token pass. Granite-4.0-H stays at 0.

Server decode (predicted_per_second, 128 tokens after 512, 4 runs): ROCm master 73.3, 72.4, 72.7, 72.6; PR 66.4, 67.1, 66.6, 66.9; PR no-snapshots 73.2, 71.0, 72.4, 72.1; PR --spec-n-rs-seq 0 67.1, 67.9, 68.0, 67.8. Vulkan master 84.1, 83.8, 83.2, 83.9; PR 84.1, 83.7, 83.5, 83.4.

Other models, 3-turn chat (PR = master) and 39 restore cases: Qwen2.5-1.5B 3/3 and 39/39 on both backends; granite-4.0-h-1b ROCm 0/3 (turn 3 differs), Vulkan 2/3, restores 39/39; LFM2-700M ROCm 1/3, restores 2/39, Vulkan 2/3, 39/39. No abort on any model.

server-test scripts (ROCm, master / PR): function-call 10/10 / 10/10, parallel-tc 12/12 / 12/12, structured 12/12 / 12/12, model 2 PASS / 2 PASS.

Slot save (--slot-save-path, one save after 2 turns): 278859636 bytes written on both sides (/proc write_bytes delta 278859776, cancelled 0). No change in disk writes.

Correctness

check method result
changed op, both backends test-backend-ops -o GATED_DELTA_NET PASS
HIP_LAUNCH_BLOCKING unset and =1 op and full suite PASS, no case passes only with =1
full backend suites test-backend-ops -j 4 PASS, no new failure
-Werror build, VGPR spill gate build, gcn-cdna-vgpr-check.py PASS
test-vk-exact binary same 11 failures as master
f32 logits and tokens, 4 corpora llama-debug + cmp identical, both backends
KLD/PPL short llama-perplexity -c 2048 identical, both backends
KLD/PPL long llama-perplexity -c 16384 not run
greedy decode with A/A llama-completion -n 256 identical, both backends
test-llama-context 3 models same as master
context reuse, -np > 1 server, 4 slots FAIL on ROCm
restored continuation server restore cases FAIL on ROCm, and on Vulkan with MTP
memory per slot server log x5 on ROCm
server-test scripts ROCm same pass counts
server pytest - not run, pytest not available
disk writes slot save bytes no change

Performance

llama-bench PP2048 (-b 2048 -ub 2048), t/s avg +- stddev, round 1 and round 2:

depth ROCm master ROCm PR Vulkan master Vulkan PR
0 3051.21+-75.32, 2989.38+-65.78 3004.17+-77.56, 2995.97+-62.67 2440.33+-81.25, 2401.91+-59.20 2413.68+-61.38, 2396.16+-52.78
12000 2190.08+-32.41, 2178.53+-33.41 2179.56+-33.29, 2177.16+-33.68 2019.45+-9.24, 2023.41+-8.38 2022.54+-5.99, 2019.79+-7.53
32000 1524.55+-15.92, 1521.34+-13.59 1523.18+-15.67, 1522.12+-15.76 1601.23+-6.30, 1598.45+-6.87 1600.75+-4.90, 1599.15+-2.72
64000 1024.89+-8.20, 1024.82+-7.19 1023.84+-6.33, 1022.42+-7.16 1184.66+-4.73, 1184.68+-5.52 1188.00+-6.21, 1186.05+-6.14
tg128 74.92+-0.23, 74.83+-0.22 74.82+-0.23, 74.79+-0.25 87.93+-0.21, 87.97+-0.19 87.99+-0.23, 87.95+-0.22

All within stddev. GATED_DELTA_NET op perf (K = 1 cases only; the perf list has no K > 1 case; ROCm with graphs off): long cases within 0.6 % on both backends; short cases +2 to +8 % on ROCm and +5 to +6 % on Vulkan, which the PR does not touch, so noise.

Findings in full

  1. Stale snapshot checkpoint (blocking): server-context.cpp:4427 sets ckpt_tail_pending from the whole prompt length. The op writes only the last min(n_seq_tokens, K) snapshot planes, so after a pass of 1 to 4 tokens plane 4 holds state from an earlier decode. The server stores it at n-5 or supersedes the n-4 checkpoint with it. Reproduced, see Results.
  2. Forced rollback snapshots (blocking): server-context.cpp:1386-1388 sets n_rs_seq_ckpt = 4 when every GPU is RDNA3.5, before the model loads; common.cpp:1716 takes the max with the speculative need, so --spec-n-rs-seq 0 cannot turn it off. Every rollback-capable model gets 5 state planes; every decode does the gather copy and the K = 5 path; multi-sequence prefill ubatches fail gdn_rs_split_ok (n_seqs == 1) and take the per-token scan. The check ignores -dev, -ngl 0 and the model shape (S_v, H/Hk), so a GDN model outside the chunked path also pays.
  3. In-pass path outside HIP: :4259 checks llama_n_rs_seq(ctx_tgt) >= 4, which MTP with --spec-draft-n-max 4 meets on any backend. worth_a_pass (:4367) depends only on ctx_checkpoint_snapshots (on by default).
  4. Multimodal prompts: when this batch held an image chunk, the offset-4 break is skipped and ckpt_tail_pending is false (!has_mtmd), so no end-of-prompt checkpoint is created (not tested).
  5. Gain is limited to short uncached prompts: the 4 + n_ubatch break stays, so for an uncached part longer than one ubatch the pass count is the same as on master. Measured: -19 % TTFT at 512 tokens, +7 % at 3000 tokens.
  6. API: RS_BACK(k) takes bits 8-15 of the upstream flags word in the public header, is ignored by non-recurrent memories, aborts when n_rs_seq == 0, and without PARTIAL_ONLY on a hybrid model writes attention KV at n-1 with recurrent state at n-1-k. src/llama-ext.h is the place for fork-only staging API.
  7. Tests: the tolerance change applies to all backends; K = 5 is not tested; the fused conv, q/k-norm, out-norm and z16 paths with the dump run only inside model graphs.
  8. Env var: GGML_CUDA_DISABLE_GDN_RS_SPLIT is read in two places; any value, including 0, turns it off; and with it set, 3 new op cases fail (see Results).
  9. Kernel: no bug found. The dump layout matches the final state write, the dump always runs (n_pre / 32 < nch), t0 > 2 holds because n_pre >= 256, and kernels without the dump only gain two unused arguments.

Claims

claim result
end-of-prompt checkpoint inside the last pass, extra pass removed reproduced (ROCm, 512 tokens: 293 to 237 ms; master without checkpoints 222 ms)
TTFT 851 to 548 ms at depth 0 reproduced in direction at 512 tokens (-19 %); not at 3000 tokens (+7 %)
prefill +55 % at depth 0 reproduced in direction at 512 tokens (server 1492-1665 to 1991-2182 t/s)
depth 10k unchanged consistent (llama-bench unchanged; server slower at 3000 tokens)
MTP gains partly: TTFT at 512 tokens -13 % ROCm, -9 % Vulkan; at 3000 tokens -2 % ROCm, 0 % Vulkan; depth 10k not tested
top-10 logprobs identical to master --ctx-checkpoints 0 reproduced for the cold path (greedy tokens equal 4/4)
restored continuation not identical to a cold run (stated) reproduced; the stale checkpoint adds wrong restores beyond that
GATED_DELTA_NET 75/75 reproduced on ROCm and Vulkan
-Werror clean, no VGPR spills reproduced
other backends keep the current behavior not reproduced: Vulkan with MTP takes the in-pass path; the user-message break changes on all backends

Not tested

Long KLD (-c 16384, corpora joined); server pytest suite (not available); multimodal prompts with images; a byte comparison of the in-pass checkpoint against a separate-pass checkpoint; bench-spec-prefill.sh acceptance and spec depths; MTP at 10k depth; whether the 3 env-var op failures also happen on master.

@dzannotti

Copy link
Copy Markdown
Collaborator

We rebased this branch onto master f2230a8e4 (it was 1258 commits behind) and pushed it as ede503b34. Commit authorship is unchanged.

Conflicts and how we resolved them:

  • ggml/src/ggml-cuda/gated_delta_net.cu, 2 hunks (gdn_chunk_eligible_raw, ggml_cuda_gdn_z16_ok): master replaced gdn_chunk_min_tokens() with GDN_CHUNK_MIN_TOKENS and removed gdn_chunk_enabled(). We kept your side, used the constant, and dropped the gdn_chunk_enabled() calls. Two more calls in ggml_cuda_gdn_rs_exact and gdn_rs_split_ok merged without a conflict but did not build, so we changed them the same way. This is the resolution from our earlier review.
  • tools/server/server-context.cpp, 1 hunk (the worth_a_pass check): master now stores checkpoints as shared pointers. We kept your worth_a_pass logic and changed checkpoints.back().n_tokens to checkpoints.back()->n_tokens.

Commit 2 applied unchanged. The range-diff shows only the lines above.

Checks on Strix Halo, master vs the rebased branch: both backends build (ROCm with -Werror). test-backend-ops -o GATED_DELTA_NET passes 75/75 on ROCm (HIP_LAUNCH_BLOCKING unset and =1) and on Vulkan. SSM_CONV passes. test-llama-context gives the same result on both sides with a hybrid and a dense model. The only failure is a Vulkan replay case, and master fails it in the same way. A 64-token greedy decode is byte-identical on two corpora. A server smoke test (cold prompt, two cache restores, a short request between them) is identical to master on Vulkan. On ROCm with --no-ctx-checkpoint-snapshots it is also identical to master. On ROCm with the default settings, the first-token log-probs differ by 0.03 to 0.08, between cold and restored runs and against master, and the greedy text differs after the first token. Asks 1 to 3 of our earlier review already cover this. We found no failure that comes from the rebase.

Before you push again, run git fetch && git reset --hard origin/strix/ckpt-rs-exact. Our earlier asks still stand. For ask 1, master now has upstream ab3768cdc ("memory: refuse rollbacks past the snapshots of the last ubatch"). It records in rs_max the deepest rollback that the last ubatch guarantees, and seq_rm refuses anything past it. The RS_BACK check in state_write can probably use the same field, but note that it treats a ubatch longer than n_rs_seq + 1 as final-state only, which does not match the chunked snapshot path in this PR.

Review made with AI on behalf of the strix-llama.cpp maintainers.

@dzannotti

Copy link
Copy Markdown
Collaborator

Rebased onto current master (2c3dc19), no conflicts; HIP and Vulkan build and ctest pass, test-backend-ops GATED_DELTA_NET passes on ROCm0. New head 1d2e44f.
Before you push again: git fetch && git reset --hard origin/strix/ckpt-rs-exact
Still waiting on the asks in the last review.

GATED_DELTA_NET with K > 1 rollback snapshots (speculative decoding, context checkpoints) took the per-token scan on HIP,
which is slower and gives other outputs than the chunked path. On a prefill-sized ubatch of one sequence the chunked path
now runs over all the tokens exactly as with K = 1 (same outputs, same final state in slot 0) and also dumps the state
before one chunk; slots 1..K-1 are recomputed from that dump beside the pass, with their outputs discarded. The dump is a
template parameter of the scan kernels, so the instantiations without it keep their code.

The device advertises it through the backend registry (ggml_backend_gdn_rs_exact: HIP RDNA3.5 with the chunked path);
elsewhere K > 1 keeps the per-token scan. GGML_CUDA_DISABLE_GDN_RS_SPLIT=1 turns it off.

The K > 1 plumbing of the chunked path (token ranges, conv history at a token, test cases) comes from the closed #243.

Assisted-by: Claude (Opus 5.5)
…_FLAGS_RS_BACK)

LLAMA_STATE_SEQ_FLAGS_RS_BACK(k) writes the recurrent state of a sequence as it was k tokens before the end of its last
ubatch, read from rollback snapshot plane k, with the cell position moved back by k. It lets a context checkpoint before
the end of a prompt be taken after the prompt was processed in one pass.

Assisted-by: Claude (Opus 5.5)
…ompt pass

For recurrent / hybrid models the server processed the last 4 tokens of every prompt in a separate batch, only to save a
context checkpoint before them: a full read of the weights per prompt (about 250 ms on Strix Halo). Where every GPU has
exact rollback snapshots (ggml_backend_gdn_rs_exact), the context keeps 4 of them, the prompt is not split, and the
checkpoint is read from the snapshots (LLAMA_STATE_SEQ_FLAGS_RS_BACK(4)) after the first token is out, before the next
decode. Also with MTP and the n-gram drafts; EAGLE3 and the standalone drafts keep the separate pass.

A checkpoint at the start of the last user message closer to the start of the prompt than one ubatch is no longer split
out either: restoring it saves less prefill than the separate pass costs on every prompt (chat prompts were split right
after the template's system prefix).

--no-ctx-checkpoint-snapshots restores the previous behaviour.

Assisted-by: Claude (Opus 5.5)
@dzannotti

Copy link
Copy Markdown
Collaborator

We rebased this branch onto master 1da4f8e50 (it was 28 commits behind and conflicted) and pushed it as 6cc02d3b2. Commit authorship is unchanged.

Conflict and how we resolved it:

Commits 1 and 2 applied unchanged. The range-diff of commit 3 shows only the hunk above. Three master changes near this PR merged without a conflict, and we checked each one:

Checks on Strix Halo, master vs the rebased branch: both backends build (ROCm with -Werror). test-backend-ops -o GATED_DELTA_NET passes 75/75 on ROCm (HIP_LAUNCH_BLOCKING unset and =1) and on Vulkan (master: 69/69). test-llama-context passes 14 of 19 cases (5 skipped, 0 failed) with a hybrid and a dense model on both backends, which is the same result as master. In a server smoke test (a cold prompt, a cache restore, a longer prompt with the same prefix, and a second restore), the output is identical to master on Vulkan, and also on ROCm with --no-ctx-checkpoint-snapshots. On ROCm with the default settings, the first-token log-probs differ from master by 0.014 to 0.03, and the greedy text differs on the cold and restored runs. The 11-18 rebase gave the same result, and asks 1 to 3 of our earlier review already cover it. We found no failure that comes from the rebase.

Before you push again, run git fetch && git reset --hard origin/strix/ckpt-rs-exact. Our earlier asks still stand.

Review made with AI on behalf of the strix-llama.cpp maintainers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA ggml risk:high Risk: high server testing waiting-user We asked the author for something.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants