Skip to content

hip dsv41: the indexer top-512 over fp32 score rows, under the kernel contract - #4914

Merged
ashhart merged 2 commits into
ashhart:mainfrom
Shoestring-dev:hip-dsv41-topk512
Nov 25, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
Shoestring-dev:hip-dsv41-topk512

Conversation

@Shoestring-dev

Copy link
Copy Markdown
Contributor

Part of #2065: the next single-box kernel PR on deepseek_v41 for HIP, after #3314 (EXL3 decode).

What it adds. A new HIP group, deepseek_v41/topk512.hip: tf_dsv41_topk512, the indexer's top-512 over fp32 score rows. It is the arithmetic of Metal's topk512.metal on a 1,024-thread block a row: an 11/11/10-bit radix select of order-preserving keys over 2,048 LDS bins, then a stable pass that writes the kept indices ascending. Wave32 shuffles stand where Metal has simdgroup scans. The rule is Metal's: NaN ranks as -inf, -0 as +0, ties go to the lower index, and row r keeps min(512, visible[r]) indices, then -1. Nothing launches it outside its tests yet.

One question on the rule. HIP follows Metal's stated rule. CUDA's decode top-k (topk_cuda.cu) maps NaN to the image 0x7FC00000, which ranks it above +inf. The two agree on every finite row, on ±0 and on ties. If HIP should follow CUDA's NaN instead, for the token-exact gate against CUDA's serial path, it is one line in tf_topk512_key plus the control.

Kernel tests (kernel_tests/dsv41.zig):

  • each row against a stable sort at widths 1 to 131,072, over spread values, heavy ties, masked -inf rows and NaN, ±0 and ±inf;
  • visible counts whole, none, half and one, 512, 513, negative and past the width;
  • a row's output the same alone as among other rows, and the scores left as they were.

Contract (contract_dsv41_topk512.zig): Metal's served shapes, rows and rule, each row against the CPU's stable sort. Its four controls go red where they do on Metal: NaN above +inf at nonfinite; ties to the higher index and the last score dropped at shapes; a write into its input at repeat. Gate-5 records come from ROCm 10.1's hipcc (HIP 7.16).

Receipts (Radeon 8060S gfx1151, Linux, ROCm 10.1 hipcc; one commit on 683c66fe3, which merges clean on 00e0e9878). The contract and limits ran at 7d533820f; 347940f9d only narrows the launcher's width check to an int, and the kernel and host tests were rerun there:

  • zig build hip-kernel-test: pass (with hip-host-test)
  • zig build hip-kernel-contract: SUMMARY gfx1151 210 kernels: 210 pass gates 1-4, 0 fail, 2 known findings; 74 controls: 74 red as planted, 0 missed; 1 limit problems (main alone: 209 kernels, 70 controls, the same 1 limit problem, below)
  • the new kernel's gates: nonfinite PASS (the stated rule holds), repeat PASS (five runs byte-identical, inputs and guards untouched), shapes PASS (43 shapes exact), sums N/A (none declared); gate 5 live: 1,024 threads, 47 VGPRs, 8,584 LDS bytes, as recorded
  • zig build hip-kernel-contract-limits: the new record passes on gfx1030, gfx1100, gfx1151 and gfx1201 (47 VGPRs, 56 SGPRs, 8,584 LDS bytes each)
  • Planted faults in the served kernel, each alone: all four fail the kernel tests. They are ties to the higher index, NaN ranked high, -0 ranked below +0, and the -1 fill dropped. Each was caught at width 513, 513, 1024 and 1 respectively. These ran before the last comment-only edits; the kernel's code is the same.
  • tools/pr_check.py --base main: 0 errors, lean 165 vs 165. The one warning is the changelog line, which is yours.

On main with this hipcc. These findings come out the same on unmodified 683c66fe3, in files this PR doesn't touch:

  • hip-kernel-contract-limits reports 7 stale records: tf_moe_combine_gate on gfx1030/1100/1151, and four relu2 stream tiles on gfx1100.
  • hip-kernel-contract's live limits fail tf_moe_combine_gate ("256 threads launched, the runtime allows 1024; 9 VGPRs, 0 + 0 LDS bytes (recorded 9, 0)"), the run's one limit problem with or without this PR.

@AIFlowML

Copy link
Copy Markdown
Contributor

The clean trial merge of 347940f9d1 into 4c79cfe346 passed 316 host registry tests on the M5 Max. The HIP selector passed 7 tests, and the complete HIP runner compiled with inert kernels.

zig build test-kernel-contract -Dfixed-cpu -j2 --summary all
zig build test-hip-contract-select-host check-hip-kernel-contract-host -Dhip=true -Dfixed-cpu -j2 --summary all

For the NaN question, I compiled the production CUDA image and HIP tf_topk512_key routines into a host comparison. On the 513-wide row [NaN, +inf, 511, 510, ..., 1] with 512 kept, sorting by CUDA's keys drops index 512, while sorting by HIP's drops index 0. That gives a small device comparison once the intended nonfinite rule is decided. This probe checked the ordering keys on the CPU. The posted gfx1151 run remains the device evidence.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @Shoestring-dev. The selector follows its declared Metal rule, with stable ties, signed-zero handling, ascending indices and padding. The gfx1151 tests and resource results support landing this standalone kernel; the unrelated baseline limit findings remain visible.

The NaN difference from CUDA is real: the 513-score example selects different indices. Before this feeds a CUDA-referenced serving path, the intended non-finite rule needs an explicit decision and comparison. This kernel-only result does not yet qualify that serving integration.

  • Synthena

Shoestring-dev and others added 2 commits November 25, 2026 07:57
… contract

A new HIP group, deepseek_v41/topk512.hip: tf_dsv41_topk512, the arithmetic of Metal's topk512.metal on a 1,024-thread
block a row: an 11/11/10-bit radix select of order-preserving keys over 2,048 LDS bins, then a stable pass that writes
the kept indices ascending (wave32 shuffles where Metal has simdgroup scans). NaN ranks as -inf, -0 as +0, ties go to
the lower index, and row r keeps min(512, visible[r]) indices, then -1. Nothing launches it outside its tests yet.

Kernel tests: each row against a stable sort at widths 1 to 131,072 over spread values, heavy ties, masked -inf rows and
NaN, +-0 and +-inf, with visible counts whole, none, half and one, 512, 513, negative and past the width; a row's output
the same alone as among other rows, and the scores left as they were.
Contract: Metal's served shapes, rows and rule, each row against the CPU's stable sort; its four controls (NaN above
+inf red at nonfinite, ties to the higher index and the last score dropped red at shapes, a write into its input red at
repeat); gate-5 records from ROCm 10.1's hipcc.
@ashhart
ashhart merged commit 4beda42 into ashhart:main Nov 25, 2026
15 of 37 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @Shoestring-dev. 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.

3 participants