Skip to content

[Engine] strata-inkling HIP: fix the k1_loop_ctrl_up3 race on SWA visits (#6736) - #6737

Open
jbox5 wants to merge 1 commit into
Niko1221:mainfrom
jbox5:fix/ink-hip-up3-race
Open

jbox5 wants to merge 1 commit into
Niko1221:mainfrom
jbox5:fix/ink-hip-up3-race

Conversation

@jbox5

@jbox5 jbox5 commented Nov 23, 2026

Copy link
Copy Markdown

Problem and result

Fixes #6736. On HIP, k1_loop_ctrl_up3 updates in0 in place for channel 0 while the k and v channels of the same launch still read in0 on sliding-window visits. Which value k and v see depends on block timing, so greedy decode with strata-inkling doesn't repeat, and some steps compute k and v from the wrong input.

With this PR, on a Radeon AI PRO R9700 (gfx1201), greedy output repeats exactly (9/9 prompts, against 1/9 before), with --draft off and with DSpark, on current main and on 0.2.19, also across separately started servers. Decode speed doesn't change.

Related discussion

#6736 has the measurements that locate the race (per-step and per-layer state hashes). #6665 depended on this to show that its checkpoint resume is token-exact; the results are below.

Approach

On SWA visits all three loop controllers read in0 (src_p = {in0, in0, in0}), and channel 0 used to write its result back to in0. Now channel 0 writes to mx3, and q_in points there, so in0 stays unchanged until every channel has read it. mx3 holds B × 3 × D floats and is only written on global visits (the attnres mix), so it's free on SWA visits. The q, r, gate and sink projections already read q_in, so the change is the destination pointer and the input of the 3-channel norm. This is done at both call sites: the forward pass and the DSpark tree verify.

I chose this over the alternatives:

  • Copying in0 to in1/in2 first, as the B > 12 path does. It's correct but adds 2 copies of B × D × 4 bytes on each of the 20 affected visits per token.
  • Launching channel 0 after channels 1 and 2. That's 20 extra launches per token.

Visits with B > 12, global visits and the CPU backend don't change. The CUDA, Metal and SYCL backends don't have this kernel.

Validation

Environment: I wrote and tested the change on Strata 0.2.19 (c6f6427d9). This PR's commit is the same 12 changed lines re-applied on current main (73d4f4196); it didn't cherry-pick because the file is now LF instead of CRLF. On main I repeated the build and the repeat test (marked "main" below); everything else ran on 0.2.19. Both: 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 with and without the commit; Radeon AI PRO R9700 32 GB; Linux x86_64; Inkling-2-Mini SQ-20; served by the 0.2.19 serve in both cases, --parallel 1, BF16 KV. Outputs are compared by token id from the engine's proto log.

Passed:

  • HIP build, gfx1201, main and 0.2.19: no new warnings.

  • Repeat, same request twice, 9 prompts (code, SQL, bash, a translation, a word problem, an explanation, 2K- and 8K-token documents), 200 tokens:

    Before After
    --draft off, main 1/9 identical 9/9
    DSpark default, main – 9/9
    --draft off, 0.2.19 1/9 identical 9/9
    DSpark default, 0.2.19 0/9 to 1/9 9/9
    Two separately started servers, 7 prompts × 400 tokens + one ~16K-token prompt – 8/8, both modes
  • Same output as copying in0 to in1/in2 before the kernel (a debug build): 9/9 token-identical in both modes.

  • State hashes (debug-only build, not part of the PR) after every decode step, covering logits, final hidden row, the SWA and global KV rows written that step, and ShortConv rows: identical over 96 steps on every case. Before, they differed from step 1.

  • Speed, --draft off, alternating before/after/after/before, 7 prompts × 400 tokens per run:

    Before After
    Decode 13.533 ms/token 13.540 ms/token (+0.06%; run-to-run noise 0.08%)
    ~16K-token prompt read 14,478 ms 14,474 ms

    With DSpark the numbers depend on how many drafts are accepted, which changes with the generated text, so they aren't comparable before and after.

  • With [Engine] Inkling: resume multi-turn prompts from a prompt checkpoint on HIP (#6627) #6665 on top: its checkpoint resume gives the same tokens as the existing exact-prefix resume at 5,002 and 19,741 prompt tokens, with --draft off and with DSpark. Its exact-prefix path is also token-identical to this commit alone.

Failed: none.

Not run:

  • Other AMD cards (RDNA2/RDNA3).
  • --parallel above 1.
  • CUDA, Metal, SYCL: the commit touches only hip/ink_hip_backend.hip.cpp.

Limits

🤖 Generated with Claude Code

https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU

On SWA visits with loop messages and B <= 12 (every single-token decode
step and every tree verify), the three loop controllers (attn, k, v) all
take the mixed input in0 as their source, and k1_loop_ctrl_up3 got
dst = {in0, in1, in2}. The kernel runs the three channels as separate
blocks of one launch (blockIdx.z) with no ordering between them, so the
channel 0 blocks overwrote in0 while the k and v blocks of the same
launch were still reading it. k and v then saw either the mixed input or
the attn controller's output, depending on block order, and identical
greedy requests gave different tokens from run to run. Prompt reads
(B > 12) were not affected: that path copies in0 to in1 / in2 first, and
the CPU backend gives every channel its own copy as well.

Channel 0 now writes to mx3, which SWA visits do not use (only global
visits mix three attnres queries into it, before the controllers), and
q_in and the 3-channel input RMSNorm follow it. k and v keep reading the
unchanged in0. No extra copy or kernel launch; global visits and the
B > 12 path are unchanged. Both call sites are fixed: the forward pass
and the tree verify.

This changes greedy output on HIP for any prompt that reaches decode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Engine] strata-inkling HIP: race in k1_loop_ctrl_up3 gives wrong k/v inputs on every decode step, so greedy output differs run to run

1 participant