Fix ROCm __syncthreads deadlock in compute_amax_and_quantize_kernel#5894
Open
q10 wants to merge 1 commit into
Open
Fix ROCm __syncthreads deadlock in compute_amax_and_quantize_kernel#5894q10 wants to merge 1 commit into
q10 wants to merge 1 commit into
Conversation
Summary:
Fixes a ROCm/AMD GPU deadlock in the FP4 fused amax+quantize kernel, found by
auditing fbgemm_gpu for the same barrier-divergence pattern fixed in D107554507.
## The bug
In compute_amax_and_quantize_kernel (quantize.cu), threads whose idx >= n hit an
early `return` (quantize.cu:1958-1959) before reaching a block-wide reduction.
The launch fp4_fused_amax_quantize uses block = dim3(blocksize, blocks_per_cta=4)
and blocks = ceil_div(numel, blocksize*4), so when numel is not a multiple of
blocksize*4 the tail block has some threads that return early while the rest
enter the block reduction. The surviving threads stall forever on a block-wide
barrier -> deadlock on ROCm (latent UB on NVIDIA).
## Where the hanging barrier actually is (not visible in the diff)
The blocking barriers are NOT in the kernel body -- they are reached transitively
through a device helper, which is why the early return was easy to miss. Call
chain:
1. Kernel calls compute_max<THREAD_X, THREAD_Y>() quantize.cu:1964
2. compute_max() branches on THREAD_X: quantize.cu:1864-1872
- THREAD_X == 32 -> compute_max_warp() (warp-only, no block barrier)
- else -> compute_max_block() quantize.cu:1870
The sole instantiation is <__nv_bfloat16, 16, 4> (quantize.cu:1993), so
THREAD_X == 16 and it ALWAYS takes the compute_max_block() path.
3. compute_max_block() (quantize.cu:1825-1846) has two block-wide barriers:
- implicit __syncthreads() inside cub::BlockReduce::Reduce() quantize.cu:1837
- explicit __syncthreads() quantize.cu:1843
Both gate the whole physical 64-thread block (dim3(blocksize, 4)), even though
cub is declared BlockReduce<float, 16> with per-row temp_storage[threadIdx.y].
So a thread that returns early in ANY row stalls the entire block.
## The fix
Remove the early return and mask the work with `active = idx < n`:
- Inactive lanes load a neutral 0.0f. The reduction is a fabsf-max, and
fabsf(0.0f) = 0 <= any real |x|, so inactive lanes cannot perturb block_amax.
- compute_max() is called by ALL threads -> full block participation, both
__syncthreads() are reached by every thread.
- Only active lanes (idx < n) store y[idx]; no OOB read or write.
Behavior is bit-identical for active lanes. blocks = ceil_div(...) guarantees the
tail block always has >= 1 active lane, so block_amax is never degenerate.
## Caveat
This deadlock exists only because the sole launch uses THREAD_X = 16. With
THREAD_X = 32, compute_max() would take the compute_max_warp() path (no block
__syncthreads()) and the early return would have been safe. The fix is correct
for the code as it exists today and remains correct if a non-32 THREAD_X is added.
Reviewed By: henrylhtsang
Differential Revision: D107946896
Contributor
|
@q10 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D107946896. |
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.
Summary:
Fixes a ROCm/AMD GPU deadlock in the FP4 fused amax+quantize kernel, found by
auditing fbgemm_gpu for the same barrier-divergence pattern fixed in D107554507.
The bug
In compute_amax_and_quantize_kernel (quantize.cu), threads whose idx >= n hit an
early
return(quantize.cu:1958-1959) before reaching a block-wide reduction.The launch fp4_fused_amax_quantize uses block = dim3(blocksize, blocks_per_cta=4)
and blocks = ceil_div(numel, blocksize4), so when numel is not a multiple of
blocksize4 the tail block has some threads that return early while the rest
enter the block reduction. The surviving threads stall forever on a block-wide
barrier -> deadlock on ROCm (latent UB on NVIDIA).
Where the hanging barrier actually is (not visible in the diff)
The blocking barriers are NOT in the kernel body -- they are reached transitively
through a device helper, which is why the early return was easy to miss. Call
chain:
The sole instantiation is <__nv_bfloat16, 16, 4> (quantize.cu:1993), so
THREAD_X == 16 and it ALWAYS takes the compute_max_block() path.
Both gate the whole physical 64-thread block (dim3(blocksize, 4)), even though
cub is declared BlockReduce<float, 16> with per-row temp_storage[threadIdx.y].
So a thread that returns early in ANY row stalls the entire block.
The fix
Remove the early return and mask the work with
active = idx < n:fabsf(0.0f) = 0 <= any real |x|, so inactive lanes cannot perturb block_amax.
__syncthreads() are reached by every thread.
Behavior is bit-identical for active lanes. blocks = ceil_div(...) guarantees the
tail block always has >= 1 active lane, so block_amax is never degenerate.
Caveat
This deadlock exists only because the sole launch uses THREAD_X = 16. With
THREAD_X = 32, compute_max() would take the compute_max_warp() path (no block
__syncthreads()) and the early return would have been safe. The fix is correct
for the code as it exists today and remains correct if a non-32 THREAD_X is added.
Reviewed By: henrylhtsang
Differential Revision: D107946896