Skip to content

metal, vulkan: FWHT kernels for wide block widths (to prism) - #155

Merged
khosravipasha merged 3 commits into
prismfrom
perf/fwht-metal-vulkan-prism
Sep 5, 2026
Merged

khosravipasha merged 3 commits into
prismfrom
perf/fwht-metal-vulkan-prism

Conversation

@bri-prism

@bri-prism bri-prism commented Sep 5, 2026 •

Copy link
Copy Markdown
Collaborator

Port of #154 to prism, the same way #153 carried #152 over. #153 brought the CUDA FWHT kernels for wide block widths to this branch; this brings Metal and Vulkan so the three backends cover the same widths here too.

What

Metal gains block widths 4096 and 8192. Vulkan gains 1024, 2048, 4096 and 8192, having previously stopped at 512.

Why

Both backends hit the same wall for the same reason. The existing kernels are register resident, holding N/subgroup_size values per lane, so the wide widths stop fitting. Any width without a kernel silently falls back to a dense mul_mat, which is O(N^2) work where the transform is O(N log N).

How

Metal adds kernel_fwht_tg, a threadgroup staged variant that maps one row to a whole threadgroup instead of one simdgroup. Butterflies below the simdgroup width still use simd_shuffle_xor, butterflies up to the threadgroup size exchange through threadgroup memory, and the remainder stay in registers. It is instantiated at 4096 and 8192 under the existing kernel_fwht_<type>_<n> names, so the host side lookup is unchanged. The threshold and thread count live in ggml-metal-impl.h, which both the host and the shader include, so the dispatch and the kernel cannot disagree.

Vulkan needed no new shader. fwht.comp was already parameterised by specialization constants, so the change is to pick the shape per width: the subgroup shader keeps the narrow widths, and wider blocks use the shared memory shader with one row per workgroup, which keeps the register block small and the staging buffer at N floats rather than 4N. Rows per workgroup becomes a specialization constant instead of a hardcoded 4, and the value chosen when the pipeline is built is recorded on the device so ggml_vk_fwht dispatches a matching workgroup count. Widths that would exceed maxComputeSharedMemorySize or maxComputeWorkGroupInvocations leave a null pipeline, which already makes ggml_vk_can_use_fwht fall back.

The Vulkan cutoff keys on n / subgroup_size and not on n alone. The subgroup shader pins its block to the subgroup width, so a device reporting a narrow subgroup reaches the same register wall at a smaller width: at subgroup_size 16 a 2048 block is already 128 values per invocation. The ratio test is a no-op on the 32 and 64 wide subgroups and routes the narrow ones to the shared memory shader.

The divisor is clamped with std::max(device->subgroup_size, 1u). That also closes a pre-existing path: the same value already fed std::min(device->subgroup_size, n) for the shared memory block size, so a device reporting a subgroup size of zero would have given a zero block size and divided by zero in the shader's EL_W = N / BLOCK_SIZE.

Testing

A pass on its own does not prove much here, because an unsupported width falls back to a generic mul_mat that produces the same answer, so I checked that the new kernel is the thing running.

Rebuilt and retested on this branch rather than relying on the results from #154. Metal on an M5 Pro: 10/10 runnable MUL_MAT_HADAMARD cases pass, and the pipeline compile log shows kernel_fwht_f32_4096 and kernel_fwht_f32_8192 being built and dispatched. Vulkan on MoltenVK: 13/13 pass.

Limitations worth knowing

Same two caveats as #154, both still open here.

The Vulkan side is verified on MoltenVK, a translation layer over Metal, not a native desktop driver. Worth a run on native hardware before this is leaned on. Note also that an Intel box whose driver falls inside the existing crash gate in ggml_vk_load_shaders creates no FWHT pipelines at all, so the whole suite passes there purely by fallback and looks identical to an unmodified tree. A green result on Intel means nothing without checking the driver version first.

The f16 MUL_MAT_HADAMARD cases report "not supported" on Metal at every width, including the ones that worked before this change. That is not related to FWHT: the test graph applies the sign vector as ggml_mul(x_f16, s_f32) and the Metal elementwise path requires matching operand types, so the graph falls back. Vulkan accepts the mixed types and runs those cases.

The register-resident kernel keeps N/32 values per thread, so it stops
fitting at N=4096. Add a threadgroup-staged variant that maps one row to a
whole threadgroup: butterflies below the simdgroup width still shuffle,
those up to the threadgroup size exchange through threadgroup memory, and
the remainder stay in registers. Instantiate it at 4096 and 8192 under the
existing kernel_fwht_<type>_<n> names so the host lookup is unchanged.
Vulkan stopped at 512 while CUDA and Metal now go to 8192. Extend the width
list to 8192 and pick the shader shape per width: the subgroup shader keeps
n/subgroup_size values per invocation so it stays on widths up to 2048, and
wider blocks use the shared-memory shader with one row per workgroup, which
keeps the register block small and the staging buffer at n floats. Rows per
workgroup becomes a specialization constant instead of a hardcoded 4, and the
value chosen at build time is recorded so the dispatch agrees with it. Widths
that would exceed maxComputeSharedMemorySize or maxComputeWorkGroupInvocations
leave a null pipeline, which already makes ggml_vk_can_use_fwht fall back.
The subgroup shader pins BLOCK_SIZE to the subgroup width, so its register
block is n/subgroup_size rather than n/32. A width cutoff alone therefore
still admits the register wall on a device reporting a narrow subgroup: at
subgroup_size 16 a 2048 block is 128 values per invocation. Cut over on the
ratio as well as the width, which is a no-op on the 32 and 64 wide subgroups
and routes the narrow ones to the shared-memory shader.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Device resource limits are not fully enforced, and the new Metal F16 kernels lack executable coverage.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Extends Metal and Vulkan FWHT acceleration to wider block sizes, avoiding dense matrix multiplication fallback.

Changes:

  • Adds Metal threadgroup-staged kernels for widths 4096 and 8192.
  • Extends Vulkan pipelines through width 8192 with configurable workgroup rows.
  • Adds an F16 width-8192 test case.
File summaries
File Description
tests/test-backend-ops.cpp Adds wide F16 coverage.
ggml/src/ggml-vulkan/vulkan-shaders/fwht.comp Parameterizes rows per workgroup.
ggml/src/ggml-vulkan/ggml-vulkan.cpp Creates and dispatches wide FWHT pipelines.
ggml/src/ggml-metal/kernels/misc.metal Implements threadgroup-staged FWHT.
ggml/src/ggml-metal/ggml-metal-ops.cpp Dispatches wide Metal kernels.
ggml/src/ggml-metal/ggml-metal-impl.h Defines wide-kernel thresholds.
ggml/src/ggml-metal/ggml-metal-device.h Advertises new supported widths.
Review details

Suppressed comments (1)

ggml/src/ggml-vulkan/ggml-vulkan.cpp:5814

  • This checks only the total invocation count, but Vulkan also requires each local-size dimension to stay within maxComputeWorkGroupSize. A conforming device may allow 256 total invocations while limiting X to 128, so the wide pipeline's local_size_x = 256 can make pipeline creation throw instead of leaving a null pipeline for fallback.
                if ((uint64_t)rows * n * sizeof(float) <= device->properties.limits.maxComputeSharedMemorySize &&
                    block_size * rows <= device->properties.limits.maxComputeWorkGroupInvocations) {
  • Files reviewed: 7/7 changed files
  • Comments generated: 5
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

// falls back to a plain mul_mat for other widths, which has no F16-input pipeline.
static inline bool ggml_metal_fwht_supported_size(int64_t n) {
return n == 64 || n == 128 || n == 256 || n == 512 || n == 1024 || n == 2048;
return n == 64 || n == 128 || n == 256 || n == 512 || n == 1024 || n == 2048 || n == 4096 || n == 8192;
Comment thread ggml/src/ggml-metal/ggml-metal-impl.h Resolved
Comment thread ggml/src/ggml-metal/kernels/misc.metal Resolved
Comment thread ggml/src/ggml-vulkan/ggml-vulkan.cpp Resolved
test_cases.emplace_back(new test_fwht_signed(4096, 8192, 3));
test_cases.emplace_back(new test_fwht_signed(4096, 8192, 1, GGML_TYPE_F16));
test_cases.emplace_back(new test_fwht_signed(8192, 8192, 2));
test_cases.emplace_back(new test_fwht_signed(8192, 8192, 1, GGML_TYPE_F16));
@khosravipasha
khosravipasha merged commit ca02f9b into prism Sep 5, 2026
9 checks passed
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add F16 inputs and transform widths through 8192. Use shared memory when
the register block grows beyond the subgroup path, and check workgroup
and shared-memory limits before creating each pipeline.

Based on bri-prism's Vulkan Hadamard implementation. Keep the Intel
Windows driver gate.

Source: PrismML-Eng#121
Source: PrismML-Eng#155
Original-commit: 6d25d20
Original-commit: 09ca0fd
Original-commit: ca02f9b
Assisted-by: GitHub Copilot
Assisted-by: Codex (porting and rebasing)

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add F16 inputs and transform widths through 8192. Use shared memory when
the register block grows beyond the subgroup path, and check workgroup
and shared-memory limits before creating each pipeline.

Based on bri-prism's Vulkan Hadamard implementation. Keep the Intel
Windows driver gate.

Source: PrismML-Eng#121
Source: PrismML-Eng#155
Original-commit: 6d25d20
Original-commit: 09ca0fd
Original-commit: ca02f9b
Assisted-by: GitHub Copilot
Assisted-by: Codex (porting and rebasing)

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Add F16 inputs and transform widths through 8192. Use shared memory when
the register block grows beyond the subgroup path, and check workgroup
and shared-memory limits before creating each pipeline.

Based on bri-prism's Vulkan Hadamard implementation. Keep the Intel
Windows driver gate.

Source: PrismML-Eng#121
Source: PrismML-Eng#155
Original-commit: 6d25d20
Original-commit: 09ca0fd
Original-commit: ca02f9b
Assisted-by: GitHub Copilot
Assisted-by: Codex (porting and rebasing)

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Add F16 inputs and transform widths through 8192. Use shared memory when
the register block grows beyond the subgroup path, and check workgroup
and shared-memory limits before creating each pipeline.

Based on bri-prism's Vulkan Hadamard implementation. Keep the Intel
Windows driver gate.

Source: PrismML-Eng#121
Source: PrismML-Eng#155
Original-commit: 6d25d20
Original-commit: 09ca0fd
Original-commit: ca02f9b
Assisted-by: GitHub Copilot
Assisted-by: Codex (porting and rebasing)

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.

3 participants