Repository navigation
[Vulkan] Enable coopmat for NVIDIA devices and do some tuning. - #22899
jeffbolznv wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22899
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New FailuresAs of commit c79090a with merge base 91a918e ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
This PR needs a
|
|
Thanks for the PR, @jeffbolznv. Could you sign the easyCLA, and fix the CI failures? |
d094ffc to
cafb158
Compare
cafb158 to
c98f813
Compare
|
Hi @jeffbolznv, could you update the branch which resolves some CI failures we noticed across PRs? Do ping us when ready for a review. |
c98f813 to
af2a845
Compare
Enable FP16 Q4GSW cooperative-matrix linear using its established subgroup-32 geometry, and add a subgroup-32 variant to the existing subgroup-64 dynamic-int8 path. Select variants only when exact cooperative-matrix types and shapes, Vulkan memory model, subgroup controls, compatible tensor layout, aligned dimensions, workgroup limits, and shared-memory requirements are satisfied. Use K32 staging for FP16, pre-unpack int4 weights for int8, and accept singleton-batch activations used by LLM prefill. Unsupported configurations continue to use the tiled fallback.
af2a845 to
c79090a
Compare
SS-JIA
left a comment
There was a problem hiding this comment.
AI review (Codex), static analysis. Findings rechecked against the current PR head.
| kDq8caQ4gswCoopmatDims.n, | ||
| kDq8caQ4gswCoopmatDims.k)) { | ||
| const char* variant = pick_dq8ca_q4gsw_coopmat_variant( | ||
| graph, out, fp_input, group_size, resize_args.at(2)); |
There was a problem hiding this comment.
AI review (Codex), static analysis.
[High] Guard cooperative dispatch against floating zero-point textures.
choose_qparams_affine and activation packing support _zpinherit, retaining Half per-row zero points as VK_FORMAT_R16G16B16A16_SFLOAT images. On an otherwise eligible subgroup-32 device with ordinary int8 dot product enabled and force_narrow_int4_tile=false, Half width-packed input/output buffers with M=128, K=N=64, group_size=64 and no bias pass this selector even when input_zp is Half. The selected linear_dq8ca_q4gsw_coopmat_sg32_*_half shader hardcodes binding 5 as int8/isampler3D, with no conversion before binding. The sampled type therefore mismatches the image format. Its zero-point fetch feeds accum_int32 - izp * weight_sum, so this invalid image access can produce wrong linear outputs when zero points and weight sums are nonzero.
The fixed integer binding is inherited; this PR newly exposes it on subgroup-32 devices that previously selected the tiled _zpinherit path. Require graph->dtype_of(input_zp) == vkapi::kChar for the existing cooperative variants, letting floating zero points use that fallback. An aligned Half-zero-point case with nonzero zero points and weight sums would cover this route.
The previous commit enabled VK_KHR_vulkan_memory_model only on devices that match a family, through a policy function that WmmaLinear.cpp registered with vk_api during static initialization. Device creation then depended on an op-layer translation unit being linked and initialized first, and a missing registration silently disabled the family. create_logical_device now enables the extension, vulkanMemoryModel and the device scope whenever the physical device supports them, and the policy hook is gone. Kernel choice stays with the existing dispatch gates (family key, subgroup size, device name). This makes upstream shaders that declare VulkanMemoryModel (linear_coopmat, matmul_coopmat, the q4gsw / dq8ca coopmat kernels) pass the capability gate on devices that support the memory model and cooperative matrices, as pytorch#22899 does. wmma_memory_model_gate now checks that a family device enables the memory model and that every family variant passes the gate. On every device it reports the gate's verdict on the upstream VulkanMemoryModel shaders without asserting it. Checked on an Arc B580 (xe2), a RADV Raphael iGPU and llvmpipe (Mesa 26.2.6): wmma/cases/correctness.json 75/75 on each. The memory model is now enabled on all three; the ten upstream shaders pass the gate on the B580 and llvmpipe and are rejected on RADV (no cooperative matrix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit enabled VK_KHR_vulkan_memory_model only on devices that match a family, through a policy function that WmmaLinear.cpp registered with vk_api during static initialization. Device creation then depended on an op-layer translation unit being linked and initialized first, and a missing registration silently disabled the family. create_logical_device now enables the extension, vulkanMemoryModel and the device scope whenever the physical device supports them, and the policy hook is gone. Kernel choice stays with the existing dispatch gates (family key, subgroup size, device name). This makes upstream shaders that declare VulkanMemoryModel (linear_coopmat, matmul_coopmat, the q4gsw / dq8ca coopmat kernels) pass the capability gate on devices that support the memory model and cooperative matrices, as pytorch#22899 does. wmma_memory_model_gate now checks that a family device enables the memory model and that every family variant passes the gate. On every device it reports the gate's verdict on the upstream VulkanMemoryModel shaders without asserting it. Checked on an Arc B580 (xe2), a RADV Raphael iGPU and llvmpipe (Mesa 26.2.6): wmma/cases/correctness.json 75/75 on each. The memory model is now enabled on all three; the ten upstream shaders pass the gate on the B580 and llvmpipe and are rejected on RADV (no cooperative matrix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit enabled VK_KHR_vulkan_memory_model only on devices that match a family, through a policy function that WmmaLinear.cpp registered with vk_api during static initialization. Device creation then depended on an op-layer translation unit being linked and initialized first, and a missing registration silently disabled the family. create_logical_device now enables the extension, vulkanMemoryModel and the device scope whenever the physical device supports them, and the policy hook is gone. Kernel choice stays with the existing dispatch gates (family key, subgroup size, device name). This makes upstream shaders that declare VulkanMemoryModel (linear_coopmat, matmul_coopmat, the q4gsw / dq8ca coopmat kernels) pass the capability gate on devices that support the memory model and cooperative matrices, as pytorch#22899 does. wmma_memory_model_gate now checks that a family device enables the memory model and that every family variant passes the gate. On every device it reports the gate's verdict on the upstream VulkanMemoryModel shaders without asserting it. Checked on an Arc B580 (xe2), a RADV Raphael iGPU and llvmpipe (Mesa 26.2.6): wmma/cases/correctness.json 75/75 on each. The memory model is now enabled on all three; the ten upstream shaders pass the gate on the B580 and llvmpipe and are rejected on RADV (no cooperative matrix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit enabled VK_KHR_vulkan_memory_model only on devices that match a family, through a policy function that WmmaLinear.cpp registered with vk_api during static initialization. Device creation then depended on an op-layer translation unit being linked and initialized first, and a missing registration silently disabled the family. create_logical_device now enables the extension, vulkanMemoryModel and the device scope whenever the physical device supports them, and the policy hook is gone. Kernel choice stays with the existing dispatch gates (family key, subgroup size, device name). This makes upstream shaders that declare VulkanMemoryModel (linear_coopmat, matmul_coopmat, the q4gsw / dq8ca coopmat kernels) pass the capability gate on devices that support the memory model and cooperative matrices, as pytorch#22899 does. wmma_memory_model_gate now checks that a family device enables the memory model and that every family variant passes the gate. On every device it reports the gate's verdict on the upstream VulkanMemoryModel shaders without asserting it. Checked on an Arc B580 (xe2), a RADV Raphael iGPU and llvmpipe (Mesa 26.2.6): wmma/cases/correctness.json 75/75 on each. The memory model is now enabled on all three; the ten upstream shaders pass the gate on the B580 and llvmpipe and are rejected on RADV (no cooperative matrix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit enabled VK_KHR_vulkan_memory_model only on devices that match a family, through a policy function that WmmaLinear.cpp registered with vk_api during static initialization. Device creation then depended on an op-layer translation unit being linked and initialized first, and a missing registration silently disabled the family. create_logical_device now enables the extension, vulkanMemoryModel and the device scope whenever the physical device supports them, and the policy hook is gone. Kernel choice stays with the existing dispatch gates (family key, subgroup size, device name). This makes upstream shaders that declare VulkanMemoryModel (linear_coopmat, matmul_coopmat, the q4gsw / dq8ca coopmat kernels) pass the capability gate on devices that support the memory model and cooperative matrices, as pytorch#22899 does. wmma_memory_model_gate now checks that a family device enables the memory model and that every family variant passes the gate. On every device it reports the gate's verdict on the upstream VulkanMemoryModel shaders without asserting it. Checked on an Arc B580 (xe2), a RADV Raphael iGPU and llvmpipe (Mesa 26.2.6): wmma/cases/correctness.json 75/75 on each. The memory model is now enabled on all three; the ten upstream shaders pass the gate on the B580 and llvmpipe and are rejected on RADV (no cooperative matrix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…NVIDIA (#24000) ## Summary Today the cooperative-matrix path of `q4gsw` and `dq8ca_q4gsw` linear only runs on devices with subgroup size 64 whose name contains "Radeon" or "Xclipse". On Intel Xe2 (subgroup 16) and NVIDIA (subgroup 32) no cooperative-matrix linear kernel runs, and prefill uses the tiled kernels. This PR adds a cooperative-matrix path for 4-bit weight linear (`q4gsw`, 4w) and 8-bit-activation 4-bit-weight linear (`dq8ca_q4gsw`, 8da4w) on two architecture families: - **Intel Xe2** (Arc B580, Arc Pro B70) on Mesa ANV - **NVIDIA** (RTX 4070 Ti SUPER, Jetson Orin) on the proprietary driver A device matches a family by four things, never by device name: vendor ID, driver ID, the cooperative-matrix shapes it reports and the subgroup sizes it supports. Its logical device must also have what the kernels need enabled: subgroup size control for compute, full subgroups and the Vulkan memory model. This holds for an Adapter that creates its device and for one that borrows a shared device (#23151). **A device that matches no family keeps the kernels it runs today.** ## What changes 1. `vk_api`: - Set `REQUIRE_FULL_SUBGROUPS` on pipelines with a required subgroup size when the device enabled `computeFullSubgroups` and the local size in X is a multiple of it (VUID-RuntimeSpirv-OpTypeCooperativeMatrixKHR-10770, VUID 02757). - Enable `VK_KHR_vulkan_memory_model` (and its device scope) whenever the device supports it, and map the `VulkanMemoryModel` / `VulkanMemoryModelDeviceScope` SPIR-V capabilities to it. - Record the memory-model features in `SharedVulkanDeviceFeatures`. Shared-device Adapters derive full subgroups and the memory model from the device's enabled extensions and features, as the owning Adapter does (`vk_api/EnabledDeviceState.h`). Adapters on an external `VkDevice` stay conservative. - Expose the driver ID (`VkPhysicalDeviceDriverProperties`, only with `VK_KHR_driver_properties`), the cooperative-matrix property list and the maximum compute shared memory. - Add `kReadStorageImage`, a read access that selects `VK_IMAGE_LAYOUT_GENERAL`. The graph dump lists it as a read. 2. `impl/gemm/linear/qw/wmma/CoopmatFamily.{h,cpp}`: the family key and one table of kernel rows per family. - Each row names a shader variant, its tile geometry, the shared memory it declares per IO storage and an optional shape rule. - Selection checks op, layout, subgroup size, shared-memory limit and tile alignment. A shape that no row fits falls back to the existing kernel. - `WmmaLinear.*` builds the key (from an Adapter or a graph) and the shape. 3. `impl/gemm/linear/qw/q4gsw/wmma/Q4gswLinearWmma.*`, `impl/gemm/linear/qw/dq8ca_q4gsw/wmma/Dq8caQ4gswLinearWmma.*`: dispatch, hooked into `Q4gswLinear.cpp` and `Dq8caQ4gswLinear.cpp` (7 lines each). Weight scales are converted to the output dtype, so fp32-scale and floating-zero-point models take the family kernels too. 4. Shaders under `glsl/gemm/linear/qw/{q4gsw,dq8ca_q4gsw}/wmma/`: - Two cooperative-matrix linear templates, `linear_q4gsw_wmma` and `linear_dq8ca_q4gsw_wmma`, each with one `*_body.glslh`. - q4gsw flags: `B_TEXEL`, `ACC_FP32`, `CSH_IN_ASH`, `FRAG_LAYOUT`, `IMG_A`, `CSH_BAND`, `B_COLMAJOR`. - dq8ca flags: `ROWMAJOR_A`, `ORDER_NB_MAX`, `A_MULTI_BLOCK`. - 15 variants. The storage, dtype and zero-point suffixes come from `generate_variant_forall`. The templates define `PRECISION highp` themselves, so `--auto-use-mediump` does not change them. - The existing with-group-sums activation packer, now `quantize_and_pack_with_group_sums.{glsl,yaml}`, gains an `OUTPUT_LAYOUT` axis with two row-major variant names (`quantize_and_pack_4w_with_group_sums_*`). Its 16 existing variants compile to the same SPIR-V as before. 5. Tests: - `test/custom_ops/coopmat_family_test.cpp`: host-only gtest of the key and the selection (14 tests). It covers: - unmatched keys (e.g. llvmpipe); - logical-device prerequisites, and the enabled state a created or shared device yields; - the shared-memory limit of every row; - every row's tile geometry (`WG_TILE_*`, `SG_GRID_*`, subgroup size, `MMA_M`, IO storage, layout flags) against the yaml parameters of each variant it dispatches. It runs in `test-vulkan-operators-linux`. - `vulkan_shared_device_test`: an Adapter that borrows a shared device created by the backend has the owner's pipeline flags, memory model and family. An imported device without declared features has none. - `benchmark_v2/operators/gemm/linear/qw/wmma/cases/correctness.json` (88 cases). It runs on its own in `test-vulkan-operators-linux` (SwiftShader) and `test-vulkan-nvidia`. - Each case is a `wmma_family_route` check: `operator_dynamic_resize` steps that also assert the kernel each step dispatches for the device's family. The family is stated by `ETVK_WMMA_EXPECTED_FAMILY` (`nvidia` on the NVIDIA job, `software` on SwiftShader), not taken from the selector. In strict mode a case that does not run fails. - Coverage: every Xe2 and NVIDIA row, both IO storages, group sizes 32 / 64 / 128 / 256, `[1, M, K]` inputs, `[2, M, K]` (no family kernel), unaligned M, runtime shrink/grow down to decode and back, fp32 / bias graphs, fp32 scales and fp16 zero points. - Long-K q4gsw steps that dispatch a family kernel are validated at that family's measured bound (see Testing). - `wmma_family_shared_memory` checks the row table against the SPIR-V. `wmma_memory_model_gate` checks that family kernels pass the capability gate on family devices. - `performance.json` has the Llama 3.2 1B projections. - The Xe2 rows have no GitHub CI coverage (no runner has an Xe2 GPU). | Family | 4w | 8da4w | |---|---|---| | Xe2 (MMA 8x16x16 fp16, 8x16x32 int8, subgroup 16) | `t128x128k16g82s16m8flib` (texture3d), `t128x128k16g44s16m8fl` (buffer) | `t128x128k64g84s16m8i`; blocked workgroup order `...n13` when K*N >= 20e6 | | NVIDIA (MMA 16x16x16 fp16/fp32, 16x16x32 int8, subgroup 32) | `t256x128k16g42s32bt` for K <= 8192; `t128x128k16g22s32` where the 256-row tile does not fit; `bx_t128x128k32g42s32f32c` (fp32 accumulation) for K > 8192 | `rowmajor_t128x128k64g24s32mk32` | The kernel sets were chosen from per-device tuned kernels by measuring each candidate on every member of its family. The accepted set is within 3 % geomean of each member's own best set, with no projection more than 8 % worse. The K > 8192 fp32-accumulation rule on NVIDIA is a correctness rule: the fp16-accumulate kernel exceeds tolerance at K = 14336 (Llama 3.1 8B down projection). Buffer tensors pad their packed dim to four (#24753). On the family route that dim is always a multiple of the tile, so the padding is empty there. ## Results End-to-end prefill, Llama 3.2 1B, 512-token prompt, tokens/s (median of 3), this PR vs `main`. Measured 2026-10-19, before the rebases. Family kernel times were unchanged on 2026-11-10 (B580, 4070 Ti), and the changes since then leave the family kernels' SPIR-V byte-identical: | Device | 4w | 8da4w | |---|---|---| | Arc B580 | 10667 vs 3066 (3.5x) | 13474 vs 6737 (2.0x) | | RTX 4070 Ti SUPER | 21333 vs 6649 (3.2x) | 32000 vs 7420 (4.3x) | Unaligned prompts (e.g. 511, 300, 129 tokens) take the family kernels through a partial last M tile. On the 4070 Ti, 1B 4w at 511 tokens goes from 6636 to 21292 tok/s. Outside the Llama projections, I tested 30 projection shapes of Qwen2.5 0.5B / 1.5B, Gemma 2 2B, Qwen3 4B / 8B and Phi-3 mini (M = 512 and 2048, group sizes 64, 128 and 256). They pass against the CPU reference on the 4070 Ti and the Orin and run 2.2x to 13.5x faster than the current kernels. ## Testing On the current head (`221c089c33`, base `main` `311d96d60d`), 2026-11-13 (wmma set rerun 2026-11-16), `etvk_ops_bench --validation_only`: | Device | `wmma/correctness` | `q4gsw/correctness` | `coopmat_correctness` | `fpa_correctness` | |---|---|---|---|---| | Arc B580 (Mesa 26.2.6) | 88/88 (`xe2`) | 134/134 | 32/32 | 90/90 | | Ryzen 9600X iGPU (RADV, Mesa 26.2.6, no family) | 88/88 (`none`) | 134/134 | not rerun | not rerun | | llvmpipe (Mesa 26.2.6, no family) | 88/88 (`software`) | 134/134 | not rerun | not rerun | On the same commits before the last rebase (base `main` `4d292c9733`), 2026-11-13: | Device | `wmma/correctness` | `q4gsw/correctness` | `coopmat_correctness` | `fpa_correctness` | |---|---|---|---|---| | Arc Pro B70 (Mesa 26.2.6) | 88/88 (`xe2`) | 130/132 * | 32/32 | 90/90 | | Jetson Orin Nano (595.78) | 88/88 (`nvidia`) | 132/132 | 32/32 | 90/90 | | Ryzen 9600X iGPU (RADV) | 88/88 (`none`) | 132/132 | 32/32 | 90/90 | | llvmpipe | 88/88 (`software` and `none`) | 132/132 | 32/32 | 90/90 | \* Two `linear_dq8ca_q4gsw` buffer cases that no family row takes failed on Intel on that base, as on `main` at the time. They pass on the B580 on the current base. The RTX 4070 Ti SUPER (615.71.09) passed an earlier head's 75 cases on 2026-11-10. It was not reachable for this head. SwiftShader was last run on 2026-11-10 too (75/75, outside GitHub). - **wmma runs.** The wmma rows were run with `ETVK_WMMA_EXPECTED_FAMILY` set to the device's family. Every route case validates its outputs at every step, with one exception: the `[2, M, K]` cases check routing only, because the existing kernels compute only the first batch, on `main` too. - **Host tests.** `vulkan_coopmat_family_test` passes 14/14. `vulkan_shared_device_test` passes 41/41 on the B580, B70 and RADV, and 40/40 with 1 skipped on llvmpipe. - **Kernel lists.** Enabling the memory model wherever it is supported did not change the dispatched kernel of any case in the three q4gsw collections on the B580, RADV or llvmpipe. None of upstream's `VulkanMemoryModel` coopmat shaders is selected on these devices. They need a Radeon/Xclipse name or subgroup size 64. - **Long K.** Max |error| against the CPU reference over 5 seeds (M = 256): | Kernel | K = 9216 | K = 14336 | |---|---|---| | Xe2 fp16 accumulation (B580) | 0.13 | 0.23 | | NVIDIA fp32 accumulation (Orin) | 0.008 | 0.008 | | `main`'s q4gsw GEMM (B580) | 0.51 | 0.77 | The family steps are validated at atol 0.3 (Xe2) and 0.03 (NVIDIA), rtol 0. Other devices keep atol 1.0. ## Known limitations and open questions - **Device reach.** The Xe2 key requires Mesa ANV (the Intel Windows driver is not matched). The NVIDIA key matches any NVIDIA GPU on the proprietary driver that reports these shapes. It was measured on Ada and Ampere only (Turing, Hopper and Blackwell would match unmeasured). Happy to narrow it if you prefer. - **External `VkDevice`.** An Adapter on an external `VkDevice` does not know whether full subgroups and the memory model are enabled, so it keeps the existing kernels. Shared devices carry that state and match. - **Memory model on other devices.** The memory model is enabled wherever it is supported. Upstream's `linear_coopmat*`, `matmul_coopmat*` and Radeon/Xclipse `linear_{,dq8ca_}q4gsw_coopmat_*` shaders therefore pass the capability gate on such devices, as with #22899. Their own dispatch gates decide whether they run. RDNA3 / Xclipse were not run here. - **Xe2 long K.** The Xe2 4w rows keep fp16 accumulation at K > 8192. The error is measured above: within the 0.25 every K <= 8192 case allows, and below `main`'s kernel on the same device. - **Xe2 8da4w threshold.** The blocked order starts at K*N >= 20e6, the midpoint between the largest shape where it is slower (16.8e6) and the smallest where it is faster (21.2e6) on both Xe2 cards. - **Image extents on Intel.** Texture3d tensors at K = 14336 exceed ANV's reported 2048 image extent. This is the case on `main` as well and is not changed here. - **AMD.** RDNA3 under RADV (780M, RX 7600, RX 7900 XTX) and Xclipse are not covered. Their selection is unchanged. Folding the AMD coopmat route into the family table is a possible follow-up. - **Weight layout.** On a family device, decode and small-M prefill fall back to `linear_q4gsw_{coop,tiled}`, which read the legacy layout the family kernels share. - On an Arc B580 those are faster than `main`'s `w_4x8_nc` GEMV/GEMM for most Llama 3.2 1B projections (geomean 0.63–0.78 of `main`'s time on texture3d). They are slower for k/v decode and for gate/up at M = 64 and 100. - On a Jetson Orin, `main`'s kernels are faster from M = 64 on (fallback 1.19x / 1.36x at M = 64 / 100) and on par for decode. - Reading `w_4x8_nc` from the family kernels is a follow-up. - **Upstream issue seen while testing (not changed here).** On `main`, `linear_q4gsw` / `linear_dq8ca_q4gsw` with a `[2, M, K]` input compute only the first batch. - **Teardown crash on NVIDIA x86 (pre-existing, not from this PR).** With a cold driver shader cache, `etvk_ops_bench` on the 4070 Ti (driver 615.71) occasionally aborts at exit after all cases have passed (`ComputePipelineCache::purge` in `~Adapter` racing the driver's background compiler thread). Not in this PR: fused attention, decode kernels, fp16 linear. Authored with Claude Code (AI). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
(Re-?)Enable the coopmat shaders, remove is_amd check and handle subgroup size of 32. Also did some tuning of the int8 shader to remove some local memory indexing. To get the shaders to be used in qwen3, needed to handle single-batch 3D activations.
Perf in the unit tests on RTX 5090:
I also saw about a 2.5x improvement in prefill on Qwen3-4B.
This change is intended to keep the AMD behavior as-is, but I don't have the hardware to test that.
Test plan
Tested test_coopmat_linear_bench, test_q4gsw_linear.exe, and verified correctness in Qwen3-4B.
cc @SS-JIA @manuelcandales @digantdesai @cbilgin