Skip to content

[feat](rmsnorm): support pad args for rmsnorm kernel - #3265

Merged
valarLip merged 2 commits into
mainfrom
rmsnorm
Jun 2, 2026
Merged

valarLip merged 2 commits into
mainfrom
rmsnorm

Conversation

@PerryZhang01

@PerryZhang01 PerryZhang01 commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

  • Add x_pad_to_multiple to the fused all-reduce RMSNorm API so callers can request padded output widths while keeping the residual path on the original hidden dimension.
  • Introduce a padded custom all-reduce RMSNorm kernel path that distinguishes input width, valid hidden width, and output width, zero-fills the padded tail, and validates shape constraints more explicitly.
  • Improve the communicator fallback logic to handle weak-contiguous and supported strided layouts more robustly, including packing eligible strided 2D inputs into the registered buffer instead of forcing a Python-side contiguous copy.
  • Add multi-GPU coverage for padded outputs, padded input storage, and strided input/residual layouts.

@PerryZhang01
PerryZhang01 requested a review from a team May 19, 2026 11:22
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3265 --add-label <label>

@PerryZhang01
PerryZhang01 marked this pull request as draft May 19, 2026 12:03
@PerryZhang01
PerryZhang01 force-pushed the rmsnorm branch 2 times, most recently from 782309b to 1bce207 Compare May 20, 2026 06:07
@PerryZhang01
PerryZhang01 marked this pull request as ready for review May 20, 2026 06:29
@PerryZhang01
PerryZhang01 force-pushed the rmsnorm branch 2 times, most recently from 65375fd to e8859ee Compare May 20, 2026 06:37
valarLip
valarLip previously approved these changes May 27, 2026
@valarLip
valarLip requested a review from TennyWang1223 May 27, 2026 08:35
Comment thread csrc/include/custom_all_reduce.cuh Outdated
*(reinterpret_cast<P*>(residual_out) + read_idx) = rmsnorm_inp;
}
}
if(tail_pack_count > 0 && threadIdx.x < tail_pack_count)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each block only lets threads with threadIdx.x < tail_pack_count write one pack each, with no loop. The dispatcher computes n_loop based on n_packs (valid hidden), not out_n / pack_size:

int n_loop = (n_packs + 511) / 512;
launch_fused_allreduce_rmsnorm_pad((local_device_load_rmsnorm<T, 512, 1>));

If the tail_pack_count corresponding to out_n - n exceeds tnum (block size 512 or 256), some tail positions are never written and the output retains stale memory.

Example: bf16, n=4096 (n_packs=256, dispatched as tnum=512 / n_loop=1), caller requests x_pad_to_multiple=16384 → out_n=16384, tail = 12288 elements = 1536 packs. Only threadIdx.x < 512 write, leaving 1024 pack slots with garbage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, this was a real bug.

The padded-tail zero-fill in local_device_load_rmsnorm only wrote at most one pack per thread, so large out_n - n tails could leave stale values in the padded region. I fixed it by striding the tail zero-fill loop across the whole tail (tail_offset += blockDim.x) instead of writing a single pack per thread.

I also added a multi-GPU regression that covers a large padded output case (shape=(13, 4096), x_pad_to_multiple=16384) so we exercise tails much larger than the block size.

assert res_out is not None
return out, res_out

if (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only checks input_.stride(-1) == 1 and does not constrain dim, but the C++ helper _can_pack_2d_last_dim_slice only matches dim() == 2. A 3D (or higher) strided input falls through to:
HIP_CALL(hipMemcpyAsync((void*)reg_ptr, inp.data_ptr(), data_bytes,
hipMemcpyDeviceToDevice, stream));

Here data_bytes = numel * element_size, but the underlying storage is non-contiguous, so this copies the wrong bytes and reads out of bounds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this in two parts:

  1. aligned the Python predicate with the C++ packable-layout check, so only safe 2D last-dim slices use the packed-input fast path;
  2. updated the general fallback path to flatten leading dims to 2D for Triton RMSNorm and then reshape back, so strided N-D inputs still work correctly without relying on the packed-input path.

I also added a regression covering a strided 3D input.

@TennyWang1223 TennyWang1223 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@valarLip
valarLip merged commit d380c2c into main Jun 2, 2026
39 checks passed
@valarLip
valarLip deleted the rmsnorm branch June 2, 2026 02:53
amirumoAMD pushed a commit that referenced this pull request Jun 2, 2026
* [feat](rmsnorm): Support padded outputs and strided inputs in fused all-reduce RMSNorm

* [fix](allreduce): fix tail and stride bug

---------

Co-authored-by: perzhang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants