[BUG] Inference `layer_norm` / `rms_norm` return uninitialized memory for float32 inputs with hidden size 65-128
Describe the bug
With float32 inputs whose last dimension is between 65 and 128, LayerNormOp and RMSNormOp (InferenceBuilder layer_norm / rms_norm) return values unrelated to the input (errors of 3 to 4e19 on unit-scale data). float16 and bfloat16 inputs, and float32 inputs with other hidden sizes, are correct.
In launch_fused_ln (csrc/transformer/inference/csrc/layer_norm.cu) and launch_rms_norm (rms_norm.cu), rows with elems_per_row <= 128 use the sub-block schedule with h_per_step = T_per_load, which is 4 for float. For 65-128 elements this gives threadsPerGroup = next_pow2(ceil(elems_per_row / 4)) = 32, but the sub-block branch only launches for 1, 2, 4, 8 or 16 threads, so no kernel runs and the at::empty_like output is returned as is. The residual variants (launch_fused_residual_ln, launch_fused_residual_ln_store_pre_ln_res) have the same branch.
To Reproduce
# DeepSpeed inference LayerNormOp / RMSNormOp with float32 input and hidden size 65..128:
# the CUDA kernel is never launched, so the returned tensor is uninitialized memory.
import torch
import torch.nn.functional as F
from deepspeed.ops.transformer.inference.op_binding.layer_norm import LayerNormOp
from deepspeed.ops.transformer.inference.op_binding.rms_norm import RMSNormOp
dev = "cuda"
eps = 1e-5
ln, rms = LayerNormOp(), RMSNormOp()
U32 = 2.0 ** -24
print("op H | non-finite lib max|err| torch max|err| ratio max|err| / max(torch, 2u*max|ref|)")
for H in (64, 96, 128, 256):
torch.manual_seed(0)
x = torch.randn(1, 4, H, device=dev)
w = 1 + 0.1 * torch.randn(H, device=dev)
b = 0.1 * torch.randn(H, device=dev)
xd = x.double()
cases = (
("layer_norm", ln(x, w, b, eps), F.layer_norm(x, (H,), w, b, eps),
F.layer_norm(xd, (H,), w.double(), b.double(), eps)),
("rms_norm", rms(x, w, eps), x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps) * w,
xd * torch.rsqrt(xd.pow(2).mean(-1, keepdim=True) + eps) * w.double()),
)
torch.cuda.synchronize()
for name, y, yt, ref in cases:
nf = int((~torch.isfinite(y)).sum())
e_lib = (y.double() - ref).abs().nan_to_num(float("inf")).max().item()
e_t = (yt.double() - ref).abs().max().item()
q = e_lib / max(e_t, 2 * U32 * ref.abs().max().item())
print("%-10s %4d | %10d %12.3e %14.3e %10.3e" % (name, H, nf, e_lib, e_t, q))
Output:
op H | non-finite lib max|err| torch max|err| ratio max|err| / max(torch, 2u*max|ref|)
layer_norm 64 | 0 3.766e-07 3.146e-07 9.867e-01
rms_norm 64 | 0 2.304e-07 2.304e-07 6.206e-01
layer_norm 96 | 0 2.928e+00 4.895e-07 5.981e+06
rms_norm 96 | 0 3.405e+00 4.772e-07 7.137e+06
layer_norm 128 | 0 3.689e+19 4.348e-07 7.882e+25
rms_norm 128 | 0 5.360e+00 3.273e-07 1.243e+07
layer_norm 256 | 0 3.682e-07 3.554e-07 9.714e-01
rms_norm 256 | 0 3.200e-07 3.200e-07 8.422e-01
Real model impact. google/bert_uncased_L-2_H-128_A-2 (BERT-tiny) has hidden size 128, so it's affected. I loaded it in fp32 and ran deepspeed.init_inference(model, dtype=torch.float32, replace_with_kernel_inject=True) (injected modules: DeepSpeedBERTInference, DeepSpeedSelfAttention, DeepSpeedMLP), on two short English sentences padded to the same length, comparing last_hidden_state on non-padding tokens. Largest output magnitude is 4.70.
| max abs error | min per-token cosine similarity | |
|---|---|---|
| HF model, fp32 (vs fp64) | 2.7e-06 | - |
| DeepSpeed kernel injection, fp32 | 4.7–4.8 (varies per run) | between −0.22 and 0.17 (varies per run) |
| DeepSpeed kernel injection, fp32, with the fix below | 4.7e-03 | 0.999999 |
So for some tokens the encoder output is essentially unrelated to the model's real output. The numbers change from run to run because the output memory is never written. End-to-end script (loads the model by id, compares against HF fp32): https://gist.github.com/jayden1711/c81e4ba679caa804ab6cf1a91a9019b9
Control: the same comparison on google/bert_uncased_L-2_H-256_A-4 (hidden 256), unpatched, gives max error 4.3e-03 and min cosine 0.999999, the normal level for fp32 kernel injection, which the patched hidden-128 model matches.
Expected behavior The same accuracy as for H = 64 or 256 (errors around 3e-7), or an error if this size isn't supported for float32.
ds_report output
DeepSpeed 0.19.7 (pip), ops JIT-built with InferenceBuilder().load() (CUDA 12.8, TORCH_CUDA_ARCH_LIST=12.0).
ds_report
--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
runtime if needed. Op compatibility means that your system
meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. [OKAY]
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
[WARNING] async_io requires the dev libaio .so object and headers but these were not found.
[WARNING] async_io: please install the libaio-dev package with apt
[WARNING] If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found.
async_io ............... [NO] ....... [NO]
fused_adam ............. [NO] ....... [OKAY]
cpu_adam ............... [NO] ....... [OKAY]
pin_memory ............. [NO] ....... [OKAY]
cpu_adagrad ............ [NO] ....... [OKAY]
cpu_lion ............... [NO] ....... [OKAY]
dc ..................... [NO] ....... [OKAY]
[WARNING] Unable to locate CUTLASS. Install nvidia-cutlass, clone CUTLASS next to DeepSpeed, or set CUTLASS_PATH to the CUTLASS checkout.
evoformer_attn ......... [NO] ....... [NO]
fp_quantizer ........... [NO] ....... [OKAY]
fused_lamb ............. [NO] ....... [OKAY]
fused_lion ............. [NO] ....... [OKAY]
[WARNING] gds requires the dev libaio .so object and headers but these were not found.
[WARNING] gds: please install the libaio-dev package with apt
[WARNING] If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found.
gds .................... [NO] ....... [NO]
transformer_inference .. [NO] ....... [OKAY]
inference_core_ops ..... [NO] ....... [OKAY]
cutlass_ops ............ [NO] ....... [OKAY]
quantizer .............. [NO] ....... [OKAY]
ragged_device_ops ...... [NO] ....... [OKAY]
ragged_ops ............. [NO] ....... [OKAY]
random_ltd ............. [NO] ....... [OKAY]
spatial_inference ...... [NO] ....... [OKAY]
transformer ............ [NO] ....... [OKAY]
stochastic_transformer . [NO] ....... [OKAY]
utils .................. [NO] ....... [OKAY]
--------------------------------------------------
DeepSpeed general environment info:
torch install path ............... ['~/discovery/outreach_2026-10-14/train/venv/lib/python3.12/site-packages/torch']
torch version .................... 2.11.0+cu128
deepspeed install path ........... ['~/discovery/outreach_2026-10-14/train/venv/lib/python3.12/site-packages/deepspeed']
deepspeed info ................... 0.19.7, unknown, unknown
torch cuda version ............... 12.8
torch hip version ................ None
nvcc version ..................... 12.8
deepspeed wheel compiled w. ...... torch 2.11, cuda 12.8
shared memory (/dev/shm) size .... 15.34 GB
System info
- OS: Ubuntu 24.04.5
- GPU: 1x NVIDIA GeForce RTX 5080 Laptop GPU (sm_120), driver 595.99.02
- Python 3.12.3, PyTorch 2.11.0+cu128, CUDA toolkit 12.8 (nvcc 12.8.93)
Docker context None (bare metal).
Additional context Related: #8742 (same launchers, empty batch).
Suggested fix. Add the missing threadsPerGroup == 32 branch to the sub-block dispatch in all four launchers (launch_fused_ln, launch_fused_residual_ln, launch_fused_residual_ln_store_pre_ln_res, launch_rms_norm). For example, in launch_fused_ln:
} else if (threadsPerGroup == 16) {
LAUNCH_FUSED_LN(1, 16, maxThreads);
+ } else if (threadsPerGroup == 32) {
+ LAUNCH_FUSED_LN(1, 32, maxThreads);
}
reduce::partitioned_block<..., 32> already handles 32 threads. Tested only on the GPU above, with the op JIT-built from patched source:
- the reproducer gives ratio ≤ 0.90 at H = 96 and 128; H = 64 and 256 unchanged;
tests/unit/ops/transformer/inference/test_layer_norm.pyandtest_rms_norm.pypass before and after (424 and 140 tests), but they only use 384+ channels;- with 64, 96 and 128 channels added to those tests, 24 fp32 cases at 96/128 fail without the change and all pass with it.
Happy to open a PR with the fix and the extra test sizes.