Add grid-stride loops and ROCm cap to reorder_batched_ad_{lengths,indices,sequence_embeddings}_kernel#5938
Open
q10 wants to merge 2 commits into
Open
Add grid-stride loops and ROCm cap to reorder_batched_ad_{lengths,indices,sequence_embeddings}_kernel#5938q10 wants to merge 2 commits into
q10 wants to merge 2 commits into
Conversation
…_kernel (pytorch#5934) Summary: X-link: facebookresearch/FBGEMM#2852 Tier-2 fix for HIP grid-overflow in `sparse_ops/sparse_index_add.cu`. `index_add_2d_with_unique_indices_kernel` previously used `blockIdx.x` directly to index unique indices. Capping the host-side grid without first adding a grid-stride loop would silently drop work. Changes: - Add `const int num_unique_indices` as a new kernel parameter. - Convert kernel to a grid-stride loop over `u = blockIdx.x; u < num_unique_indices; u += gridDim.x` (Pattern C). All `blockIdx.x` references replaced with `u`. Hoist `start_D` and `has_remainder` outside the loop since they depend only on `blockIdx.y` / `threadIdx.x`. - RESET per-iteration register state at the top of each iteration: `sum[MAX_ELEMENTS_PER_THREAD]` re-zeroed and `sum_remainder = 0`. - Apply standard `#ifdef USE_ROCM min(blocks_x_uncapped, get_max_thread_blocks(stream)) #else blocks_x_uncapped #endif` cap to the x-dim of the launch grid. y dim is bounded by D/stride_D and needs no cap. Stacked on top of D105029028 (Tier-2 Diff 5/7). Plan: `/home/bensonma415/.llms/plans/sparse_ops_rocm_grid_overflow_tier2_fix.plan.md` (Diff 6/7). Reviewed By: henrylhtsang Differential Revision: D105029511
…ices,sequence_embeddings}_kernel Summary: Tier-2 fix for HIP grid-overflow in `sparse_ops/sparse_reorder_batched_ad.cu`. Three saturating kernels — all using direct `b_t = blockIdx.x * blockDim.y + threadIdx.y` with early-returns. Changes: - `reorder_batched_ad_lengths_kernel`: Pattern B grid-stride loop over `b_t`. The `t >= T` early-return becomes the implicit loop bound. - `reorder_batched_ad_indices_kernel` and `reorder_batched_ad_indices_kernel_vec`: Pattern B grid-stride loop. All per-iteration locals (`output_segment_*`, `input_segment_*`, `num_elements`, `dst_ptr`, `src_ptr`) reset naturally. Inner `if (num_elements <= 64) / else if ... <= 128 / else > 128` branch dispatch is also per-iteration. - `reorder_batched_sequence_embeddings_kernel`: Pattern B grid-stride loop. Inner per-row and per-D loops are intra-block and unchanged. Apply standard `#ifdef USE_ROCM min(grid_uncapped, get_max_thread_blocks(stream)) #else grid_uncapped #endif` cap to all three launch sites: - `reorder_batched_ad_lengths_gpu`: `grid_size = (B*T+31)/32` (manual ceil-div). - `reorder_batched_ad_indices_gpu`: `cuda_calc_xblock_count(B*T, NUM_WARPS)` cap is applied once outside the `#if defined __HIP_PLATFORM_AMD__ / #else / #endif` block, after `NUM_WARPS` is determined. - `reorder_batched_sequence_embeddings_gpu`: `(B*T+31)/32` (manual ceil-div). Stacked on top of D105029511 (Tier-2 Diff 6/7). Plan: `/home/bensonma415/.llms/plans/sparse_ops_rocm_grid_overflow_tier2_fix.plan.md` (Diff 7/7) — final diff in the Tier-2 stack. Differential Revision: D105030655
Contributor
|
@q10 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105030655. |
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:
Tier-2 fix for HIP grid-overflow in
sparse_ops/sparse_reorder_batched_ad.cu. Three saturating kernels — all using directb_t = blockIdx.x * blockDim.y + threadIdx.ywith early-returns.Changes:
reorder_batched_ad_lengths_kernel: Pattern B grid-stride loop overb_t. Thet >= Tearly-return becomes the implicit loop bound.reorder_batched_ad_indices_kernelandreorder_batched_ad_indices_kernel_vec: Pattern B grid-stride loop. All per-iteration locals (output_segment_*,input_segment_*,num_elements,dst_ptr,src_ptr) reset naturally. Innerif (num_elements <= 64) / else if ... <= 128 / else > 128branch dispatch is also per-iteration.reorder_batched_sequence_embeddings_kernel: Pattern B grid-stride loop. Inner per-row and per-D loops are intra-block and unchanged.Apply standard
#ifdef USE_ROCM min(grid_uncapped, get_max_thread_blocks(stream)) #else grid_uncapped #endifcap to all three launch sites:reorder_batched_ad_lengths_gpu:grid_size = (B*T+31)/32(manual ceil-div).reorder_batched_ad_indices_gpu:cuda_calc_xblock_count(B*T, NUM_WARPS)cap is applied once outside the#if defined __HIP_PLATFORM_AMD__ / #else / #endifblock, afterNUM_WARPSis determined.reorder_batched_sequence_embeddings_gpu:(B*T+31)/32(manual ceil-div).Stacked on top of D105029511 (Tier-2 Diff 6/7). Plan:
/home/bensonma415/.llms/plans/sparse_ops_rocm_grid_overflow_tier2_fix.plan.md(Diff 7/7) — final diff in the Tier-2 stack.Differential Revision: D105030655