fix: flash attention CUDA kernel truncating head dimensions > 32#39
Merged
MankyDanky merged 2 commits intomainfrom Apr 13, 2026
Merged
fix: flash attention CUDA kernel truncating head dimensions > 32#39MankyDanky merged 2 commits intomainfrom
MankyDanky merged 2 commits intomainfrom
Conversation
The CUDA launch config capped blockDim.x at min(D, 32), but the kernel assigns threadIdx.x as the head-dimension index. For headDim=64 (the common case with nEmbd=384, nHead=6), only the first 32 of 64 elements were computed — the rest stayed zero from store.zeros(). This broke both forward (half the attention output was zeros) and backward (half the Q/K/V gradients were zero, so those weights never updated). Fix: use the full head dimension as blockDim.x and adjust blockDim.y to stay within CUDA's 1024 threads-per-block limit. Replace the hardcoded BLOCK_SIZE macro with blockDim.y so the kernel adapts to the actual launch configuration. Made-with: Cursor
fix: flash attention CUDA kernel truncating head dimensions > 32
r-chong
approved these changes
Apr 12, 2026
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 CUDA launch config capped blockDim.x at min(D, 32), but the kernel assigns threadIdx.x as the head-dimension index. For headDim=64 (the common case with nEmbd=384, nHead=6), only the first 32 of 64 elements were computed — the rest stayed zero from store.zeros(). This broke both forward (half the attention output was zeros) and backward (half the Q/K/V gradients were zero, so those weights never updated).
Fix: use the full head dimension as blockDim.x and adjust blockDim.y to stay within CUDA's 1024 threads-per-block limit. Replace the hardcoded BLOCK_SIZE macro with blockDim.y so the kernel adapts to the actual launch configuration.