Skip to content

hip flashnext: stage each tile's q rows once in LDS in tf_fn_idx_scores - #3601

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:strix/hip-idx-scores-lds
Nov 17, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:strix/hip-idx-scores-lds

Conversation

@gilby

@gilby gilby commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Problem

tf_fn_idx_scores (zig/kernels/hip/flashnext/indexer.hpp) walks up to four kMost (32-wide) pooled-key slots per wave for eight rows (r0..r1) a block. Because each lane read q[(r * heads + h) * dims + d] directly from global memory on every block tile along x (blocks / 8 grid columns), the same q tile was re-read from memory across every block wave and kMost step.

Change

  • Stage qs[8 * kStage] (16,384 bytes LDS, kStage = 512) once per (blockIdx.x, blockIdx.y) tile when heads * dims <= kStage, after a block-uniform early exit (tile over b0 = blockIdx.x * kFnWaves) so inactive decode-graph blocks exit before reading q or hitting __syncthreads(), then keep the per-wave wanted check before reading pooled.
  • Register tf_fn_idx_scores under Gate 1–4 kernel contracts (zig/tests/hip/contract_idx_scores.zig, zig/tests/hip/contract_main.zig) including a planted one_head_short control caught by shapes, plus Gate 5 RDNA records (zig/src/core/contract/hip_records_sparse.zig, ROCm 10.1 counts: 24 VGPRs / 39 SGPRs on gfx1030/gfx1100/gfx1151, 23 VGPRs / 40 SGPRs on gfx1201, 16,384 LDS, 0 scratch, bound 256, wave32) and unit coverage in zig/src/hip/kernel_tests/flashnext_attn.zig + docs/kernel-contract.md.

rocprofv3 --kernel-trace (Qwen3.8-Flash-Next-oQ4e-tf-hip2, gfx1151, --context 131072 --parallel 4 --prompt-cache-gib 0)

Request (tf_fn_idx_scores) Dispatches origin/main (5987b9b5...) This PR (6a57c62b...) Speedup
Graph decode warmup (4 prompt tok, 4 gen tok) 24 0.238 ms 0.209 ms 1.14x (-0.029 ms)
Cold 8K (7,377 prompt tok, max_tokens=1) 1,093 276.003 ms 141.524 ms 1.95x (-134.479 ms, -48.7%)
Cold 32K (29,364 prompt tok, max_tokens=1) 5,552 2,873.855 ms 2,091.387 ms 1.37x (-782.468 ms, -27.2%)

Without the block-uniform tile check ahead of __syncthreads(), captured HIP graphs over --context 131072 (grid.x = 1,049,088 in warmup) paid 0.467 ms on that 4-token graph replay instead of 0.209 ms.

Serving benchmarks (main / this / this / main, Strix Halo gfx1151, Qwen3.8-Flash-Next-oQ4e-tf-hip2)

Flags: TENSORFOLD_MEMORY_RESERVE_GIB=4 --host 127.0.0.1 --port 28191 --context 131072 --parallel 4 --prompt-cache-gib 0, page cache dropped (sync; echo 3 > /proc/sys/vm/drop_caches) before every server, performance power mode throughout.

  • Idle GTT across all 4 arms: 86,736,945,152 bytes identically on arm1-main, arm2-this, arm3-this, arm4-main (out of 123,480,309,760 bytes total).
  • bench_cache.py --require-cold --tokens 16 --repeats 1:
Prompt Metric arm1-main arm2-this arm3-this arm4-main Verdict (mean)
cold-2k (2,048t) cold_first TTFT 3.190 s 3.197 s 3.172 s 3.196 s +0.26% (tie)
cold-2k (2,048t) repeated TTFT 2.514 s 2.520 s 2.525 s 2.527 s -0.11% (tie)
cold-2k (2,048t) conc makespan 6.309 s 6.313 s 6.303 s 6.312 s +0.04% (tie)
cold-8k (7,377t) cold_first TTFT 9.485 s 9.365 s 9.367 s 9.494 s +1.31% (WIN, -124 ms)
cold-8k (7,377t) repeated TTFT 8.844 s 8.741 s 8.749 s 8.853 s +1.17% (WIN, -104 ms)
cold-8k (7,377t) conc makespan 19.999 s 19.771 s 19.767 s 19.994 s +1.14% (WIN, -228 ms)
cold-8k (7,377t) conc fast / slow TTFT 8.851 / 19.563 s 8.739 / 19.334 s 8.752 / 19.330 s 8.848 / 19.557 s +1.18% / +1.17% (WIN)
cold-32k (29,364t) cold_first TTFT 39.203 s 41.716 s 38.479 s 39.141 s -2.36% (1-arm first-fault spike; tie)
cold-32k (29,364t) repeated TTFT 38.374 s 37.939 s 37.879 s 38.430 s +1.28% (WIN, -493 ms)
cold-32k (29,364t) conc makespan 79.592 s 79.307 s 79.353 s 80.494 s +0.89% (-713 ms)
cold-32k (29,364t) conc fast TTFT 38.407 s 38.034 s 38.159 s 38.667 s +1.14% (WIN, -441 ms)
  • bench_concurrent.py --levels 1,2,4 --tokens 256 --reps 3 --alone --serial --temperatures 1.0,0 --prompts tools/bench_prompts.json:
T Streams agg_tps (m1 / t1 / t2 / m2) steady_tps (m1 / t1 / t2 / m2) req_decode_tps agg_prompt_ttft_tps
1.0 1 46.4 / 46.4 / 46.4 / 46.4 (+0.00%) — 46.36 / 46.41 / 46.40 / 46.38 78.86 / 78.56 / 78.93 / 78.62
1.0 2 65.9 / 65.4 / 65.0 / 64.8 (-0.23%) 69.2 / 69.2 / 69.2 / 69.1 34.71 / 34.76 / 34.61 / 34.84 86.88 / 87.01 / 86.95 / 86.81
1.0 4 74.1 / 74.1 / 74.3 / 74.0 (+0.20%) 80.8 / 80.1 / 80.3 / 80.1 19.67 / 19.71 / 19.74 / 19.68 83.90 / 84.61 / 84.55 / 84.47
0.0 1 52.9 / 52.9 / 52.9 / 52.9 (+0.00%) — 52.85 / 52.93 / 52.94 / 52.88 79.01 / 79.09 / 79.07 / 78.85
0.0 2 72.3 / 71.5 / 71.6 / 72.2 (-0.97%) 75.5 / 75.5 / 75.7 / 75.5 37.10 / 36.85 / 37.21 / 37.03 87.17 / 87.19 / 87.11 / 87.09
0.0 4 75.6 / 75.8 / 76.1 / 75.7 (+0.40%) 85.2 / 85.6 / 85.2 / 85.0 21.38 / 21.32 / 21.81 / 21.48 85.11 / 85.16 / 85.12 / 85.10
  • bench_openai.py --tokens 256 --reps 3 (decode_tps_median / ttft_s_median):
    • fibonacci-raw (T=1.0): 49.76 / 49.82 / 49.86 / 49.71 (+0.21%), TTFT 0.194 / 0.193 / 0.192 / 0.193 s
    • gpu-chat-no-think (T=1.0): 38.26 / 38.48 / 38.36 / 38.45 (+0.17%), TTFT 0.338 / 0.337 / 0.339 / 0.338 s
    • fibonacci-raw (T=0.0): 50.18 / 50.25 / 50.27 / 50.19 (+0.15%), TTFT 0.193 / 0.192 / 0.193 / 0.194 s
    • gpu-chat-no-think (T=0.0): 43.47 / 43.52 / 43.52 / 43.47 (+0.12%), TTFT 0.339 / 0.338 / 0.338 / 0.339 s

Token identity & checks

  • Token hashes 100% identical across all 4 arms:
    • bench_cache: 12/12 rows equal (4/4 per prompt length: cold-2k 7e13a80e2db1, cold-8k 7106730d9614, cold-32k cc64c3fb3853; all qualified: true).
    • bench_concurrent: 42/42 cross-arm token_sha256 equal; 168/168 alone equal; 96/96 serial ("draft": false) equal.
    • bench_openai: 12/12 token_sha equal under entry["evidence"].
  • Verification runs on 9cf47b894 (gfx1151, Strix Halo):
    • ROCm 10.1 zig build hip-kernel-contract: PASS limits live flashnext tf_fn_idx_scores (24 VGPRs, 16,384 LDS); 162 kernels pass gates 1–4 (0 fail, 2 pre-existing known findings), 43/43 planted controls red, 2 pre-existing limit problems (tf_moe_combine_gate, tf_vl_resize).
    • ROCm 10.1 zig build hip-kernel-contract-limits -Dhip-relax-branches=true: PASS (record current) for tf_fn_idx_scores across gfx1030, gfx1100, gfx1151, gfx1201.
    • HIP 7.1 zig build hip-kernel-test: 22/22 steps succeeded; 219/226 passed (7 skipped). (PASS on unit test and planted-control fault caught by flashnext_attn.zig:232.)
    • HIP 7.1 zig build hip-kernel-contract: 162/162 pass gates 1–4, 43/43 controls red.
    • Host zig build check-hip-kernel-contract-host hip-host-test test-kernel-contract -Dhip=true --summary all: 47/47 steps succeeded; 312/312 passed.
    • Ubuntu zig build test -Dhip=true (umask 022): 771/771 steps succeeded; 6177/6228 passed (51 skipped).
  • Full sanitized receipts, contract logs, and rocprofv3 summary: https://gist.github.com/gilby/f290b865992369db4d39ab4e96657480

Checklist

  • zig fmt --check zig/ clean
  • Gate 1–4 kernel contract + planted control (contract_idx_scores.zig) and Gate 5 ROCm 10.1 record (hip_records_sparse.zig) added and verified on gfx1151
  • Host test suite (312/312 on macOS, 6177/6228 with 51 skipped on Ubuntu -Dhip=true) and hip-kernel-test (219/226, 7 skipped) pass
  • Interleaved main / this / this / main serving benchmarks pass with zero #2984 regressions and 100% token-hash identity
  • tools/pr_check.py, unwrap_md.py --check, and publish_check.py --upstream clean

@gilby
gilby force-pushed the strix/hip-idx-scores-lds branch from 9cf47b8 to 04b2741 Compare November 16, 2026 19:24
@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. Staging the block's q rows in LDS once gives the waves the same values they read from global memory, in the same order, so the scores stay bit for bit. The early exit is uniform across the block, so every thread reaches the barrier, and widths over 512 keep the direct reads. With the new contract, the two-tile test and your gfx1151 receipts, this is ready.

The only conflict is a changelog line, which we'll resolve when it lands. Nothing more is needed from you; it's in our landing queue.

  • Synthena

@ashhart
ashhart force-pushed the strix/hip-idx-scores-lds branch from 04b2741 to 0969ecc Compare November 17, 2026 01:38
@ashhart
ashhart merged commit fa2bfd6 into ashhart:main Nov 17, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

2 participants