Skip to content

cuda: FWHT kernels for block widths above 2048 (to prism) - #153

Merged
khosravipasha merged 1 commit into
prismfrom
fwht-large-blocks-to-prism
Sep 5, 2026
Merged

khosravipasha merged 1 commit into
prismfrom
fwht-large-blocks-to-prism

Conversation

@khosravipasha

Copy link
Copy Markdown
Collaborator

Cherry-pick of #152 (merged into prism-v7) onto prism, commit kept as authored. prism-v7 is historical; the release branch is prism.

Adds the shared-memory CUDA FWHT path for block widths 4096 and 8192 (the register kernel stops at 2048) and backend-op coverage for 512, 2048, 4096 and 8192. This is what the 9B preview model needs: its Hadamard block is 4096 and it has been running the GEMM fallback on CUDA. Metal still caps at 2048 (separate change).

Merge with Rebase and merge to keep the single commit.

The register kernel keeps N/warp_size floats per thread, so it runs out of
registers long before the arithmetic runs out: at N=4096 that is 128 floats per
thread, which spills. The switch therefore stopped at 2048 and returned false
for anything larger, and the whole op fell back.

The fallback is not free. On an L40S, the two f32 cases at block 4096 fail the
existing backend-op tolerance through the fallback path:

  MUL_MAT_HADAMARD(blk=4096,width=4096,n_tokens=7)   ERR 1.37e-07 > 1.00e-07
  MUL_MAT_HADAMARD(blk=4096,width=8192,n_tokens=3)   ERR 1.34e-07 > 1.00e-07

Add a shared-memory path for the widths the register kernel cannot reach: one
row per block, the row staged in shared memory, all log2(N) butterfly stages run
there, same sign convention and same scale-and-signs-on-load as the register
path. 16 KiB of shared at 4096 and 32 KiB at 8192, both inside the 48 KiB
default, so no opt-in carveout is needed. Blocks up to 2048 keep the existing
register kernel untouched.

Adds backend-op coverage at 512, 2048, 4096 and 8192 (the previous FWHT cases
only exercised 1024). On an L40S: 24/26 before, 26/26 after, and the two cases
that flip are exactly the block-4096 f32 ones above, which also confirms the new
kernel is what runs rather than the test passing through a CPU fallback.
@khosravipasha
khosravipasha merged commit 6f1c690 into prism Sep 5, 2026
8 checks passed
@khosravipasha
khosravipasha deleted the fwht-large-blocks-to-prism branch September 23, 2026 23:04
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Load explicit sign vectors inside the F16/F32 transform and select block-per-row kernels for wide decode shapes. Keep the shared-memory fallback and its dispatch controls. Exclude 8192-wide shared-memory instantiations on MUSA.

Source: PrismML-Eng#121
Source: PrismML-Eng#153
Source: PrismML-Eng#166

Source commits (PrismML-Eng/llama.cpp):
9146baa
14d8bb5
db4c6ee
2f2ddf0
ddc5ed9
fe88d0c
f37f047
610ddad
e76b304
4edee06
c49590b
2e85ea4
e71a350
e9e4ea3
34c9b5c
8362c89
deb4dfd
52ad701
e0c828b
6f1c690
1bc46a8
b6f1bb5

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Load explicit sign vectors inside the F16/F32 transform and select block-per-row kernels for wide decode shapes. Keep the shared-memory fallback and its dispatch controls. Exclude 8192-wide shared-memory instantiations on MUSA.

Source: PrismML-Eng#121
Source: PrismML-Eng#153
Source: PrismML-Eng#166

Source commits (PrismML-Eng/llama.cpp):
9146baa
14d8bb5
db4c6ee
2f2ddf0
ddc5ed9
fe88d0c
f37f047
610ddad
e76b304
4edee06
c49590b
2e85ea4
e71a350
e9e4ea3
34c9b5c
8362c89
deb4dfd
52ad701
e0c828b
6f1c690
1bc46a8
b6f1bb5

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Load explicit sign vectors inside the F16/F32 transform and select block-per-row kernels for wide decode shapes. Keep the shared-memory fallback and its dispatch controls. Exclude 8192-wide shared-memory instantiations on MUSA.

Source: PrismML-Eng#121
Source: PrismML-Eng#153
Source: PrismML-Eng#166

Source commits (PrismML-Eng/llama.cpp):
9146baa
14d8bb5
db4c6ee
2f2ddf0
ddc5ed9
fe88d0c
f37f047
610ddad
e76b304
4edee06
c49590b
2e85ea4
e71a350
e9e4ea3
34c9b5c
8362c89
deb4dfd
52ad701
e0c828b
6f1c690
1bc46a8
b6f1bb5

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Load explicit sign vectors inside the F16/F32 transform and select block-per-row kernels for wide decode shapes. Keep the shared-memory fallback and its dispatch controls. Exclude 8192-wide shared-memory instantiations on MUSA.

Source: PrismML-Eng#121
Source: PrismML-Eng#153
Source: PrismML-Eng#166

Source commits (PrismML-Eng/llama.cpp):
9146baa
14d8bb5
db4c6ee
2f2ddf0
ddc5ed9
fe88d0c
f37f047
610ddad
e76b304
4edee06
c49590b
2e85ea4
e71a350
e9e4ea3
34c9b5c
8362c89
deb4dfd
52ad701
e0c828b
6f1c690
1bc46a8
b6f1bb5

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants