Skip to content

[Engine] Inkling: resume multi-turn prompts from a prompt checkpoint on HIP (#6627) - #6665

Draft
jbox5 wants to merge 1 commit into
Niko1221:mainfrom
jbox5:fix/ink6627-hip-ckpt
Draft

jbox5 wants to merge 1 commit into
Niko1221:mainfrom
jbox5:fix/ink6627-hip-ckpt

Conversation

@jbox5

@jbox5 jbox5 commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem and result

Fixes #6627 on AMD (HIP). main now has the prompt checkpoint hooks (save_prompt_checkpoint / restore_prompt_checkpoint in ink_backend.h) and the serve logic that uses them, with CPU and CUDA implementations. The HIP backend still has the default return false. So on AMD, a follow-up turn whose previous reply is not a prefix of the new prompt reads the whole conversation again, and so does every exact repeat or regeneration.

This PR implements the two hooks for HIP. On a Radeon AI PRO R9700 (gfx1201), with a chat of about 19,700 tokens, prompt read time:

main This PR
Follow-up turn, previous reply changed by the client 18,051 ms 208 ms
Exact repeat of the same request 17,865 ms 30 ms

Related discussion

#6627. Earlier versions of this PR added their own hooks and serve change, starting from the patch martinshane posted in #6627. main now has its own interface and serve logic, so this version is rebased onto main and only adds the HIP side. None of the earlier serve or interface code is left.

#6737 fixes a HIP race (#6736) that makes identical greedy requests differ. It is not needed for this PR. The greedy exactness checks below ran with it applied, because without it the reference runs don't repeat.

Approach

One commit, engine/src/inkling2/hip/ink_hip_backend.hip.cpp only. main's serve saves the checkpoint after reading prompt_len - 1 prompt tokens and restores it when a later prompt starts with exactly those tokens. seq_len is that token count both times. The HIP side:

  • SWA visits keep a ring of 1024 positions in 64-token pages. Save copies the slot's mapped ring pages to pinned host memory, one hipMemcpyAsync per run of consecutive physical pages, queued on the backend stream with no host sync. This uses about 160 MiB of host RAM per slot at BF16 KV and no VRAM. FP8 KV copies the per-token scales too. The copies use hipMemcpyDefault, because the KV pools can be host-mapped (UMA, or the fallback in grow_pool when VRAM runs out).
  • ShortConv state goes to small device buffers through the existing k_sconv_snapshot_pair and comes back through k_sconv_rollback_pair at keep_steps = 0.
  • Global-visit KV and the DSpark context are indexed by absolute position, so a restore sets seq_len back and caps dspark_len. Nothing is copied.
  • Checks:
    • Save returns false unless the slot's seq_len equals seq_len, so the ring holds exactly the positions before it.
    • Restore returns false unless the checkpoint is valid for the same seq_len and the slot still maps the same physical SWA pages. The serve then reads the whole prompt, as before.
    • reset_slot and free_slot drop the checkpoint, as in the CPU and CUDA backends. forward_batch also drops it if a position below its seq_len is written again.
    • Restore resets DSpark's adaptive draft state, as reset_slot does.
  • Slots: state is per slot, so --parallel 2 to 8 works. Off above 8 slots, to bound pinned host RAM, as in the CUDA backend.

The default path changes on purpose: follow-up turns and repeats now resume. Every prompt of 16 tokens or more pays for the checkpoint copy at its end. On cold reads of about 19,700 tokens with --draft off, the first turn took 17,883 to 18,092 ms on main and 17,895 to 18,005 ms with this PR, so the copy cost is within the run-to-run spread.

Validation

Environment: Strata main at 73d4f4196 plus this branch; strata-inkling built for gfx1201 only, standalone CMake of engine/src/inkling2 with the clang in the Strata venv's ROCm SDK (-DSTRATA_ENABLE_HIP=ON -DSTRATA_HIP_ARCHS=gfx1201), the same flags for both builds; Radeon AI PRO R9700 32 GB; Linux x86_64; Inkling-2-Mini SQ-20; served by main's own serve (terminal/ at 73d4f4196) with --ctx-size 131072 --parallel 1 unless noted. Requests are greedy (temperature 0).

Passed:

  • HIP build, gfx1201: no warnings, the same as main (0 in both builds).

  • Multi-turn chat, about 19,700-token first prompt, prompt read per turn:

    main This PR
    Turn 2, reply sent back as written, --draft off 17,927 ms 177 ms
    Turn 2, reply sent back as written, DSpark 17,978 ms 177 ms
    Turn 2, reply changed, --draft off 18,063 ms 207 ms
    Turn 2, reply changed, DSpark 18,051 ms 208 ms
    Turn 3, reply changed, DSpark 18,072 ms 203 ms
    Turn 2, reply changed, --kv fp8 18,838 ms (turn 1, cold) 274 ms
    Turn 2 usage.prompt_tokens_details.cached_tokens, reply changed, DSpark 0 19,767

    On this card main read the whole conversation again on the "sent back as written" turns too (cached_tokens 0, no RESUME line), so the checkpoint covers those turns as well.

  • Exact repeat, the same request sent twice:

    Prompt tokens Second request, main Second request, this PR
    about 5,000 4,422 ms 29 ms
    about 19,700 17,865 ms 30 ms
  • Resume position: in every checkpoint resume above, the engine's RESUME line equals the previous prompt length minus 1, and cached_tokens equals it.

  • --parallel 2, two conversations sent at the same time: all four follow-up turns resumed, with prompt reads of 190 to 195 ms against 8,899 and 19,448 ms for the two concurrent first turns.

  • First requests, with [Engine] strata-inkling HIP: fix the k1_loop_ctrl_up3 race on SWA visits (#6736) #6737 applied to both builds: token-identical to main on 9 prompts, with --draft off and with DSpark (200 tokens).

  • Greedy output, with [Engine] strata-inkling HIP: fix the k1_loop_ctrl_up3 race on SWA visits (#6736) #6737 applied: an exact repeat, which resumes from the checkpoint, gives the same token ids as the first request at about 5,000 and about 19,700 prompt tokens, with --draft off and with DSpark (200 tokens). Without [Engine] strata-inkling HIP: fix the k1_loop_ctrl_up3 race on SWA visits (#6736) #6737 the repeat differs on both builds (from token 7 to 163), with or without the checkpoint.

Failed: none.

Not identical, and why I don't count it against this PR: after a changed previous reply (about 5,000 and 19,700 prompt tokens, both modes, #6737 applied), three ways of reaching the same prompt give three different replies, first differing at token 4 to 33: the checkpoint resume, main's existing exact-prefix resume, and a cold read. The exact-prefix resume and the cold read also differ from each other, from token 4, and neither uses this PR's code. Each of the three repeats exactly when run again. The three paths read the last prompt tokens in different chunks (the checkpoint resume continues from prompt_len - 1 of the previous prompt), and chunking changes rounding. In the earlier version of this PR, with the checkpoint at prompt_len, the checkpoint resume was token-identical to the exact-prefix resume.

Not run:

  • CUDA and Metal builds: this PR changes only the HIP file.
  • The state hash check from the earlier version (debug build hashing SWA KV, ShortConv, global KV and DSpark rows after a restore). It passed at 5K, 20K and 80K tokens on 0.2.19. The copy code here is the same apart from hipMemcpyDefault and the seq_len checks, and the greedy checks above cover the result on main.
  • The 26-step agent replay from the earlier version, on main.

Limits

🤖 Generated with Claude Code

https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU

@jbox5

jbox5 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Agent replay with this branch (same card and settings as above; 26-step agent session with tool calls, history growing to 82K tokens; temperature 0.7; one run per build):

llama.cpp b12613 strata-inkling unpatched strata-inkling with this PR
Median time to first token 3.17 s 103.1 s 1.11 s
Model time for the session 399 s 2,447 s 220 s
Decode, ms per step 34.7 21.2 21.4

Decode speed doesn't change; the gain is all in not re-reading the history. The engine logged 25 checkpoint resumes in 25 follow-up turns.

One note: 26 turns with this branch hit max_tokens, because the model spends its budget in the hidden thinking span (#6612). So the token counts aren't comparable with llama.cpp's, but the times above don't depend on them.

@kurtmj93

Copy link
Copy Markdown

Independently verified on Windows 11 with an RX 7900 XTX (gfx1100, 24 GB), Ryzen 9 7900X, and 64 GB RAM. I applied this PR to Strata v0.2.19 on top of the #6594 NaN fix. Both builds used Inkling 2 Mini SQ-15, FP8 KV, --draft off, --chunk 256, a 262,144-token context limit, and one slot.

I replayed the same three-turn sequence through the OpenAI-compatible API, passing back the tool call and result without replaying hidden reasoning.

Request | Before | With this PR | Cached tokens with this PR
Initial tool call | 12.04 s | 12.23 s | 0 / 16,802
Tool result | 12.09 s | 0.32 s | 16,802 / 16,848
Text follow-up | 12.21 s | 0.27 s | 16,848 / 16,877

Times are server-reported prefill. Both builds called the tool with the expected arguments and returned the correct answers. The combined build also passed the 64-token NaN regression check. I tested normal follow-ups here. I didn’t test exact retries or regeneration.

Thanks for the patch!

@jbox5

jbox5 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Greedy output is now shown to be token-exact. The run-to-run variation I couldn't get around was a race in the HIP kernel k1_loop_ctrl_up3 (#6736, fixed in #6737). With that fix applied on top of this branch, on the same R9700:

Prompt tokens --draft off DSpark default
5,002 identical identical
19,741 identical identical

"Identical" compares the token ids of the reply after a checkpoint resume (changed previous reply) with the reply after the existing exact-prefix resume of the same prompt: 200 tokens, temperature 0. The engine logged a checkpoint resume for each. With the fix, the existing exact-prefix path gives the same tokens with and without this branch.

I've moved the greedy comparison from "Failed" to "Passed" in the description.

main added save_prompt_checkpoint / restore_prompt_checkpoint to
InkBackend and the serve logic that uses them, with CPU and CUDA
implementations. HIP kept the default (return false), so on AMD every
follow-up turn whose previous reply is not a prefix of the new prompt
still reads the whole conversation again.

What the HIP side saves for the slot, at seq_len = prompt_len - 1:

- SWA visits keep a ring of swa_win positions in 64-token pages. The
  slot's mapped ring pages are copied to pinned host memory, one
  hipMemcpyAsync per run of consecutive physical pages, queued on the
  backend stream with no host sync. About 160 MiB of host RAM per slot
  at BF16 KV and no VRAM. FP8 KV copies the per-token scales too.
  hipMemcpyDefault, because the pools can be host-mapped.
- ShortConv state goes to small device buffers through
  k_sconv_snapshot_pair and comes back through k_sconv_rollback_pair at
  keep_steps = 0.
- Global-visit KV and the DSpark context are indexed by absolute
  position, so a restore sets seq_len back and caps dspark_len; nothing
  is copied.

Safety:

- save returns false unless the slot's seq_len equals seq_len, so the
  ring holds exactly the positions before it.
- restore returns false unless the checkpoint is valid for the same
  seq_len and the slot still maps the same physical SWA pages; the serve
  then falls back to a full read.
- reset_slot and free_slot drop the checkpoint, as the CPU and CUDA
  backends do. forward_batch also drops it if a position below its
  seq_len is written again.
- restore resets DSpark's adaptive draft state as reset_slot does.
- Off above 8 slots, to bound pinned host RAM, as in the CUDA backend.
  State is per slot, so --parallel 2..8 works.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU
@jbox5
jbox5 force-pushed the fix/ink6627-hip-ckpt branch from a6babf9 to fe2ab3b Compare November 23, 2026 04:13
@jbox5

jbox5 commented Nov 23, 2026

Copy link
Copy Markdown
Author

I've rebased this branch onto current main (73d4f4196).

main now has its own prompt checkpoint interface (save_prompt_checkpoint / restore_prompt_checkpoint), the serve logic that uses it, and the 9-field DONE line, with CPU and CUDA implementations. HIP still had the default return false, so AMD cards didn't get the resume.

What changed in this PR:

  • It now implements main's two hooks in ink_hip_backend.hip.cpp. That is the only file it touches.
  • The HIP design is the same as before: SWA ring pages copied to pinned host memory, ShortConv through the existing snapshot kernels, global KV and the DSpark context restored by setting the position back.
  • It follows main's contract: the checkpoint is at prompt_len - 1, so exact repeats and regenerations resume too. reset_slot drops the checkpoint, as in the CPU and CUDA backends. The slot limit is 8, as in CUDA.

What I dropped, because main has it now:

I've updated the description. Its numbers are new runs on main with main's own serve: on a 19,700-token chat, a follow-up turn with a changed reply reads in 208 ms instead of 18.1 s, and an exact repeat in 30 ms instead of 17.9 s. With #6737 applied, an exact repeat gives the same tokens as the first request, with --draft off and with DSpark. One comparison isn't identical, and the description explains why.

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

Labels

None yet

Projects

None yet

2 participants