Repository navigation
[CUDA] Fix grid overflow in gemm conv unfold kernels for >= 65,536 output positions - #3893
Merged
Merged
Conversation
The naive unfold kernels launched with num_blocks.z = mat_M, where mat_M = N * out_pixels can exceed CUDA's 65,535 grid.y/z limit (a 256x256 output already has 65,536 positions), failing with cudaGraphAddKernelNode / cuLaunchKernel "invalid argument". Swap the mapping so the matrix-row index rides grid.x (limit 2^31-1) and the filter-spatial blocks ride grid.z; per-block thread layout and memory access patterns are unchanged.
AdamDLuz
force-pushed
the
fix-gemm-conv-grid-overflow
branch
from
July 22, 2026 21:50
21eff5c to
bafa6a8
Compare
nastya236
approved these changes
Jul 23, 2026
nastya236
left a comment
Collaborator
There was a problem hiding this comment.
Looks great! THanks for the fix.
1 task done
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.
The naive unfold kernels in
backend/cuda/conv/gemm_conv.cuandgemm_grouped_conv.culaunch withnum_blocks.x = cuda::ceil_div(wt_spatial_size, block_dims.x); num_blocks.y = params.C; num_blocks.z = mat_M; // mat_M = N * out_pixelsgridDim.zis limited to 65,535, so any convolution with ≥ 65,536 output positions — e.g. a batch-1 256×256 image, common in VAE decoders — fails withcudaGraphAddKernelNode(...): invalid argument(orcudaLaunchKernelExC(...): invalid argumentwithMLX_USE_CUDA_GRAPHS=0). Meanwhilegrid.x(limit 2³¹−1) carries onlyceil_div(wt_spatial_size, block_dims.x), which is 1 for typical filters.This PR swaps the mapping so the unbounded matrix-row index rides
grid.xand the filter-spatial blocks ridegrid.z, updating the twotid.x/tid.zconsumers in each kernel accordingly. Per-block thread layout and memory access patterns are unchanged.Same bug class as #3858 (batched matvec, batch > 65,535) and the merged #3666 (binary ops).
Repro (before):
Found while debugging ollama/ollama#14843 (FLUX.2-klein image generation on Linux/CUDA): the VAE decoder's upsample-to-256 conv is the first launch to cross the limit. Verified indirectly on an NVIDIA L4 (CUDA 13) via an equivalent host-side chunking workaround producing correct 512×512 and 1024×1024 images; the patch itself is a pure grid-axis remap (not compile-tested here — missing local dep tree).
Note:
num_blocks.y = params.Cretains a theoretical >65,535-channel limit, untouched here.