DeepSpeed icon indicating copy to clipboard operation
DeepSpeed copied to clipboard

[BUG] inference v2 layer_norm / rms_norm kernels read and write past the last row for hidden sizes <= 128 (reachable via CUDARMSNorm, e.g. EXAONE4 Q/K norm)

Open jayden1711 opened this issue 1 month ago • 3 comments

[BUG] inference v2 layer_norm / rms_norm kernels read and write past the last row for hidden sizes <= 128 (reachable via CUDARMSNorm, e.g. EXAONE4 Q/K norm)

Describe the bug

The inference-v2 core-op norm kernels have the same sub-block launch problem as the v1 kernels in #8828 (fixed for v1 in #8830): for elems_per_row <= 128 several rows are packed into one thread block, the kernels have no row bound, and when rows is larger than the rows per block and not a multiple of it, the last block loads and stores rows past the end of the tensors.

Files (master 5f266e26, deepspeed/inference/v2/kernels/core_ops/):

  • cuda_rms_norm/rms_norm_cuda.cu: kernels rms_norm (:19, block_offset at :27) and pre_rms_norm (:84, :98); launcher launch_rms_norm (:182), launch math :196-211, sub-block dispatch :218-229.
  • cuda_layer_norm/layer_norm_cuda.cu: kernels fused_ln (:31, :44) and fused_residual_ln (:220, :235); launchers launch_fused_ln (:124, math :141-155), launch_fused_post_ln (:332, :350-364), launch_fused_pre_ln (:404, :423-437).
  • Bindings: cuda_rms_norm/rms_norm.cpp (rows = norm_input.size(0), :49, :87), cuda_layer_norm/layer_norm.cpp.

Which shapes

From the launch code (T_per_load = 4 for fp32, 8 for fp16/bf16):

threads_per_group    = min(next_pow2(ceil(elems_per_row / T_per_load)), 256)
rows_per_block (R)   = 256 / threads_per_group          (sub-block schedule, elems_per_row <= 128)
grid                 = ceil(rows / min(rows, R)),  block = (threads_per_group, min(rows, R))
block_offset         = blockIdx.x * R * elems_per_row + threadIdx.y * elems_per_row   (no row check)

So for rows > R and rows % R != 0 the last block touches R - rows % R rows past the end:

threads per row R fp16/bf16 hidden fp32 hidden
16 16 65-128 33-64
8 32 33-64 17-32
4 / 2 / 1 64 / 128 / 256 17-32 / 9-16 / 1-8 9-16 / 5-8 / 1-4

Where it is reached

CUDARMSNorm (cuda_rms_norm/rms_norm.py) with a small channels. EXAONE4 (and EXAONE 4.5, see below) does this for its per-head Q/K RMSNorm (deepspeed/inference/v2/model_implementations/exaone4/model.py):

  • :101-105 creates CUDARMSNorm(channels=self.head_size, fp_dtype=fp16 or bf16);
  • :107-133 _apply_qk_norm views Q and K as [tokens * n_heads_local, head_size] (:125, :129) and normalizes them in place (:126, :130); called from :164.

So rows = tokens * local heads, elems_per_row = head_size:

  • EXAONE-4.0-32B (head_dim 128, bf16, 40 Q heads, 8 KV heads): 16 threads per row, R = 16. Without tensor parallelism Q has tokens * 40 rows and K tokens * 8 rows, so for an odd number of tokens (K: and at least 3) the last block overruns by 8 rows.
  • EXAONE-4.0-1.2B (head_dim 64, 32 Q / 8 KV heads): 8 threads per row, R = 32. Q (tokens * 32) is always a multiple; K (tokens * 8) overruns when tokens % 4 != 0 and tokens >= 5.
  • EXAONE 4.5 (#8121): Exaone4_5InferenceModel subclasses Exaone4InferenceModel (exaone4_5/model.py:18,86) and inherits _apply_qk_norm unchanged. LGAI-EXAONE/EXAONE-4.5-33B has no head_dim in its text_config (hidden_size 5120, 40 Q / 8 KV heads, bf16), so head_size = 5120 / 40 = 128, the same case as EXAONE-4.0-32B: Q and K overrun by 8 rows for an odd number of tokens (K: and at least 3).

Q and K are fresh clone()s of exactly that size, so the overrun reads past them and writes into whatever the caching allocator placed next. (Other CUDARMSNorm users normalize over model_dim, which is normally > 128.)

To Reproduce

import torch
from deepspeed.inference.v2.kernels.core_ops.cuda_rms_norm.rms_norm import CUDARMSNorm

rows, H, dtype = 17, 128, torch.float16          # 16 threads per row, 16 rows per block
norm = CUDARMSNorm(channels=H, fp_dtype=dtype)
x = torch.randn(rows, H, device="cuda", dtype=dtype)
gamma = torch.randn(H, device="cuda", dtype=dtype)
buf = torch.full((rows + 16, H), 1000.0, device="cuda", dtype=dtype)   # output + 16 guard rows
out = buf[:rows]
norm(out, x, gamma)
torch.cuda.synchronize()
print("guard rows overwritten:", (buf[rows:] != 1000.0).any(-1).sum().item())   # expected 0

or run it with the output as its own allocation under PYTORCH_NO_CUDA_MEMORY_CACHING=1 compute-sanitizer --tool memcheck --padding 65536 python repro.py.

memcheck on master (CUDARMSNorm, output as its own allocation; number of invalid accesses in the first faulting launch, all in rms_norm<T, 1, 16 or 8, 256>):

dtype, hidden rows 16 rows 17 rows 33 rows 40
fp16, 128 0 244 244 132
bf16, 128 0 244 244 132
fp16, 64 0 0 252 196
bf16, 64 0 0 252 196

(rows 17 at hidden 64 is within one 32-row block, so it is clean, as expected.) The v2 layer-norm launchers have the same structure but I haven't run them. v2 also appears to have no 32-thread branch, so fp32 hidden sizes 65-128 launch no kernel there (same gap as #8826 in v1) — from code reading only, not run.

Possible fix

The same change as #8830 for v1: pass rows to fused_ln, fused_residual_ln, rms_norm and pre_rms_norm, compute the row index from the expression already used for block_offset, and mask the global loads/stores of rows past the end while keeping every thread in the reductions (no grid/block change). Happy to send a PR.

System info: 1x NVIDIA GeForce RTX 5080 Laptop GPU (sm_120), driver 595.99.02, CUDA 12.8, compute-sanitizer 2025.1.0, torch 2.11.0+cu128, DeepSpeed master 5f266e26 with inference_core_ops JIT-built for sm_120.

Related: #8828 / #8830 (same bug in the v1 kernels), #8826 / #8827, #8121 (EXAONE 4.5, same Q/K norm path), #8762 (proposed dense Qwen3 support with per-head Q/K RMSNorm through the same module, which would add another small-channels caller).

jayden1711 avatar Oct 15 '26 20:10 jayden1711

I opened #8886 with a fix for the v2 kernels, done the same way as #8830 for v1: rows is passed to rms_norm, pre_rms_norm, fused_ln and fused_residual_ln, the row index is derived from the existing block_offset expression, and loads/stores of rows past the end are masked while those threads still join the block reductions, so the launch shape is unchanged.

Tests put every kernel output in front of canary rows so the overrun fails under plain pytest (26 new cases fail on master, all pass on the branch), and compute-sanitizer memcheck on exactly sized tensors shows 16 invalid 16-byte reads in rms_norm on master and 0 errors on the branch. Validated on 1x NVIDIA GB10, driver 580.126.09, CUDA 13.0, torch 2.14.1+cu130.

@jayden1711 since you opened both #8830 and this issue: if you already have a v2 fix in progress, say so and I will close #8886 in favour of yours.

rapsealk avatar Oct 22 '26 10:10 rapsealk

Confirmed a second v2 defect in these launchers on master f32e164b, separate from the row bound tracked here. For fp32, elems_per_row <= 128 takes the sub-block schedule with threadsPerGroup = next_pow2(ceil(H / 4)), which is 32 for H=65-128, but all four ladders (launch_fused_ln, launch_fused_post_ln, launch_fused_pre_ln, launch_rms_norm) stop at 16. No kernel is launched and the empty_like output is returned unwritten. This is the v2 sibling of #8826/#8827.

I have a 4-branch patch ready that adds the 32-thread case and launches one row per block for it, so it is independent of the row-bound change. Tests are fp32 H=96/128 for post-LN, pre-LN, plain LN, and RMS norm with/without residual. On a Tesla T4 (torch 2.14.1, CUDA 13.0), the new tests fail with NaN on f32e164b and pass with the patch; excluding the pre-existing bf16-on-sm_75 failures, the three test files are 38 passed.

@jayden1711 @rapsealk would you prefer this folded into #8886 or a separate PR? I would rather not create a three-way conflict in these four ladders and in test_ln.py.

YidaWeng avatar Nov 09 '26 16:11 YidaWeng

@YidaWeng Hi! Folding into #8886 would be fine with me.

rapsealk avatar Nov 09 '26 16:11 rapsealk