Repository navigation
Conversation
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.
There was a problem hiding this comment.
🟡 Changes recommended
Metal must gate wide kernels by device resources, and its new F16 paths need executable coverage.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds wide-block FWHT support to Metal and Vulkan, avoiding dense matrix multiplication fallbacks.
Changes:
- Adds 4096/8192 Metal threadgroup-staged kernels.
- Extends Vulkan pipelines through width 8192.
- Adds F16 coverage for width 8192.
File summaries
| File | Description |
|---|---|
tests/test-backend-ops.cpp |
Adds an F16 wide-block case. |
ggml/src/ggml-vulkan/vulkan-shaders/fwht.comp |
Makes rows per workgroup configurable. |
ggml/src/ggml-vulkan/ggml-vulkan.cpp |
Builds and dispatches width-specific pipelines. |
ggml/src/ggml-metal/kernels/misc.metal |
Implements wide threadgroup FWHT kernels. |
ggml/src/ggml-metal/ggml-metal-ops.cpp |
Dispatches wide kernels with 256 threads. |
ggml/src/ggml-metal/ggml-metal-impl.h |
Defines the wide-kernel threshold and size. |
ggml/src/ggml-metal/ggml-metal-device.h |
Advertises the additional supported widths. |
Review details
- 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 on lines
+1207
to
+1208
| // Block widths at or above this run the threadgroup-staged FWHT kernel: the | ||
| // register-resident one keeps N/32 values per thread, which stops fitting here. |
Comment on lines
+432
to
+434
| // Wide blocks: one row per threadgroup instead of per simdgroup, so each thread | ||
| // keeps N/NT values rather than N/32. Butterflies below the simdgroup width still | ||
| // shuffle; those up to NT go through threadgroup memory; the rest stay in registers. |
Comment on lines
+771
to
+773
| // FWHT block widths with a dedicated Vulkan pipeline. Widths above | ||
| // GGML_VK_FWHT_MAX_SUBGROUP_N would keep n/subgroup_size values per invocation in | ||
| // the register-resident shader, so those stage through shared memory instead. |
| 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)); |
Collaborator
|
closing this one I guess. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #152, which added the CUDA FWHT kernels for block widths above 2048. This does the same for Metal and Vulkan so the three backends cover the same widths.
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: each thread or invocation holds N/32 values, so at N=4096 that is 128 floats per lane and it stops 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 usesimd_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 existingkernel_fwht_<type>_<n>names, so the host side lookup is unchanged. The threshold and thread count live inggml-metal-impl.h, which both the host and the shader include, so the dispatch and the kernel cannot disagree about them.Vulkan needed no new shader.
fwht.compwas already parameterised by specialization constants, so the change is to pick the shape per width rather than per device: the subgroup shader stays on widths up to 2048, and wider blocks use the shared memory shader with one row per workgroup, which keeps the register block at N/256 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 soggml_vk_fwhtdispatches a matching workgroup count. Widths that would exceedmaxComputeSharedMemorySizeormaxComputeWorkGroupInvocationssimply leave a null pipeline, which already makesggml_vk_can_use_fwhtfall back.The Vulkan cutoff keys on
n / subgroup_sizeand not onnalone. 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. Thanks to the reviewer who caught that the width alone does not determine the register block.While clamping that divisor the code now takes
std::max(device->subgroup_size, 1u). That is not cosmetic: the same value already fedstd::min(device->subgroup_size, n)for the shared memory block size before either change, so a device reporting a subgroup size of zero would have produced a zero block size and a division by zero when the shader computedEL_W = N / BLOCK_SIZE. The clamp closes that pre-existing path as well.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 in both cases I checked that the new kernel is actually the thing running.
Metal, on an M5 Pro: all 13
MUL_MAT_HADAMARDf32 cases pass, and the pipeline compile log showskernel_fwht_f32_4096andkernel_fwht_f32_8192being built and dispatched rather than skipped. 8192 in f32 needs 32768 bytes of threadgroup memory, which is exactly the limit reported by the device, and it does fit.Vulkan, on MoltenVK: 13/13 pass. I temporarily instrumented
ggml_vk_fwhtto print the width and the workgroup shape it selected, and confirmed it fires at 1024 and 2048 with 4 rows per workgroup and at 4096 and 8192 with 1, which matches the intended shape and can only come from the new wide path. The instrumentation was removed before the final run.Limitations worth knowing
The Vulkan side is verified on MoltenVK, which is a translation layer over Metal, not a native desktop driver. It exercises the shared memory path and the specialization constant plumbing, but it is not a substitute for a run on AMD, NVIDIA or Intel, and the shared memory shader was previously only reachable on devices without subgroup shuffle so it has had less exposure than the subgroup one. Worth a run on native hardware before this is leaned on.
There is a trap for anyone who does try this on Intel. If the driver version falls inside the crash gate already present in
ggml_vk_load_shaders,can_use_fwhtis false, no FWHT pipeline of any width is created, and every case passes purely by fallback. The result is a clean looking full pass that is indistinguishable from an unmodified tree, including at the widths that have worked for ages. This was confirmed on an Arc B390 at driver 101.8724, which sits inside that range. Check the driver version before believing a green run on Intel.The f16
MUL_MAT_HADAMARDcases report "not supported" on Metal at every width, including 1024 and 2048 which were already supported before this change. That is not related to FWHT. The test graph applies the sign vector asggml_mul(x_f16, s_f32), and the Metal elementwise path requiressrc0->type == src1->type, so the whole graph falls back. Vulkan accepts the mixed types and does run those cases.No performance numbers here.
make_test_cases_perfhas no FWHT cases at all, so there was nothing to measure against without adding some, which felt like it belonged in a separate change. The argument for these widths is that they previously had no kernel and fell back to a dense matmul, not that the kernel got faster.