DeepSpeed icon indicating copy to clipboard operation
DeepSpeed copied to clipboard

[BUG]Triton SwiGLU and AutoEP fused restore kernels use int32 offsets: illegal memory access above 2^31 elements

Open pengdurice opened this issue 2 months ago • 0 comments

Triton SwiGLU and AutoEP fused restore kernels use int32 offsets: illegal memory access above 2^31 elements

Describe the bug

deepspeed/ops/triton_ops/swiglu_triton.py computes element offsets in int32. When the input has more than 2^31 elements, the offsets wrap to negative numbers. The mask offsets < n_elements does not reject a negative offset, so both kernels load and store memory before the start of the tensor. The result is CUDA error: an illegal memory access was encountered.

The same pattern is in deepspeed/ops/triton_ops/autoep_fused_token_ops.py, the opt-in combine_impl="fused_weighted_sum" path. There the overflowing product is tokens x hidden size.

Converting the program id to int64 fixes both. Measured with the fix: output and gradients are bit-identical below 2^31, and the kernel time does not change.

Where

Upstream master at 4e394422a (2026-09-18).

deepspeed/ops/triton_ops/swiglu_triton.py (added in #8244):

_BLOCK_SIZE = 2048                                                  # line 33

def _swiglu_fwd_kernel(gate_ptr, up_ptr, out_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    pid = tl.program_id(axis=0)                                     # line 38: int32
    offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)           # line 39: int32 product
    mask = offsets < n_elements                                     # line 40

_swiglu_bwd_kernel has the same three lines at 53-55. The wrapper launches triton.cdiv(n_elements, 2048) programs (lines 82 and 98) and does not check the size.

pid * 2048 passes 2^31 - 1 at pid = 1,048,576. The grid reaches that program as soon as n_elements > 2^31. The product wraps to a negative number, negative < n_elements is true, and the mask lets it through. For a bf16 input of 1.25 x 2^31 elements, the wrapped programs address memory from 4.0 GiB to 3.0 GiB before the start of the tensor.

deepspeed/ops/triton_ops/autoep_fused_token_ops.py (added in #8326): _weighted_restore_forward_kernel and _weighted_restore_backward_kernel set token = tl.program_id(0) (lines 56 and 102), then address the output and its gradient with token * out_stride and token * grad_out_stride. Both strides equal the hidden size, so the product overflows when tokens x hidden > 2^31. The expert-row addresses are already int64 (source_rows ... .to(tl.int64)), so only the token products are affected.

When it matters

SwiGLU (default path). GroupedExperts (deepspeed/moe/ep_experts.py) calls swiglu in both _run_experts_grouped_mm (line 128) and _run_experts_triton_grouped_mm (line 171). Its input is the gate projection, of shape [rows received by this rank, moe_intermediate_size]. With balanced routing each expert-parallel rank receives about tokens per rank x top_k rows, so the kernel faults when

tokens per rank x top_k x moe_intermediate_size > 2^31
model top_k moe_intermediate_size faults above, tokens per rank
Qwen3.5-397B-A17B 10 1024 209,715
DeepSeek-V3 8 2048 131,072

These rows are arithmetic from the formula, not runs. For example, one 262,144-token sequence per rank on Qwen3.5-397B-A17B gives 262,144 x 10 x 1024 = 2,684,354,560 elements (1.25 x 2^31).

Unbalanced routing reaches the limit sooner, because one rank can receive up to expert-parallel size x tokens per rank x top_k rows. That is how we found it. A separate transformers bug left every Qwen3.5 router weight at zero under ZeRO-3 initialization (link: the transformers issue), so every token was routed to experts 0-9, which are all on one rank. On 4 nodes x 8 H200 with expert-parallel size 32 and 8,192-token sequences, that rank received 32 x 8,192 x 10 = 2,621,440 rows, plus 128 rows of alignment padding. SwiGLU then received 2,621,568 x 1,024 = 2,684,485,632 elements (1.25 x 2^31), and training crashed with an illegal memory access. The same collapse at expert-parallel size 8 gives 655,872 rows (0.31 x 2^31) and did not crash. That is why the crash looked like it depended on the expert-parallel size.

Fused weighted restore (opt-in). Only with combine_impl="fused_weighted_sum", when tokens per rank x hidden > 2^31: above 524,288 tokens per rank at hidden size 4096, or above 299,593 at hidden size 7168 (arithmetic).

Measured

One H200, torch 2.8.0+cu126, Triton 3.4.0, CUDA_LAUNCH_BLOCKING=1, each size in a fresh process.

SwiGLU, bf16 inputs of shape [rows, 1024]. Error = largest absolute difference from a float32 F.silu(gate) * up and its gradients, on rows 0, middle and last:

elements x 2^31 upstream master with the fix
671,612,928 0.31 ok: forward 0.0144, backward 0.0076 / 0.0075 ok, bit-identical to upstream (sha256 of output, gate gradient and up gradient all match)
2,149,580,800 1.001 illegal memory access in the forward kernel ok: forward 0.0132, backward 0.0077 / 0.0077
2,684,485,632 1.25 illegal memory access in the forward kernel ok: forward 0.0147, backward 0.0078 / 0.0075

The errors with the fix are the same size as upstream's errors below 2^31. They come from rounding the float32 result to bf16 on store.

SwiGLU time, median of 50 calls at 671,612,928 elements:

kernel forward backward
upstream (int32 offsets) 0.962 ms 1.634 ms
with the fix (int64 offsets) 0.962 ms 1.634 ms

Both versions move about 4.2 TB/s forward and 4.1 TB/s backward, so memory traffic sets the time, and the int64 multiply does not change it.

Fused weighted restore, top_k 2, hidden 4096, bf16 rows. Error = largest absolute difference from a float32 reference on tokens 0, middle and last:

tokens tokens x hidden upstream master with the fix
524,000 0.9995 x 2^31 ok: forward 0.0078 ok, bit-identical to upstream (sha256 of output, row gradient and score gradient all match)
524,800 1.001 x 2^31 illegal memory access in the forward call ok: forward 0.0077, row gradient 0.0075, score gradient 7.6e-6

The upstream backward kernel was not run above the limit, because the forward call faults first. It has the same token * grad_out_stride product, and the fixed backward kernel is correct at 1.001 x 2^31.

Proposed fix

# deepspeed/ops/triton_ops/swiglu_triton.py, _swiglu_fwd_kernel and _swiglu_bwd_kernel
pid = tl.program_id(axis=0).to(tl.int64)

# deepspeed/ops/triton_ops/autoep_fused_token_ops.py, both _weighted_restore_*_kernel
token = tl.program_id(0).to(tl.int64)

A short comment next to each line would help, because a reviewer can easily miss the bug: the mask looks like it guards the access, but it does not reject a negative offset.

Why it is safe

  • Only address arithmetic changes. The values loaded, computed and stored are the same. Measured: bit-identical results below the limit (tables above).
  • No measurable time cost (table above).

To reproduce

One GPU with at least 20 GB free (the three tensors take 12.9 GB):

import sys
import torch
import torch.nn.functional as F
from deepspeed.ops.triton_ops.swiglu_triton import swiglu

rows = int(sys.argv[1]) if len(sys.argv) > 1 else 2_099_200  # x 1024 = 2,149,580,800 = 1.001 x 2**31
gate = torch.randn(rows, 1024, dtype=torch.bfloat16, device="cuda")
up = torch.randn(rows, 1024, dtype=torch.bfloat16, device="cuda")
out = swiglu(gate, up)
torch.cuda.synchronize()  # before the fix: "an illegal memory access was encountered"
reference = F.silu(gate[-1].float()) * up[-1].float()
print("last row, max abs error:", (out[-1].float() - reference).abs().max().item())

Upstream master: RuntimeError: Triton Error [CUDA]: an illegal memory access was encountered, both at the default size and with 2621568 (1.25 x 2^31). With the fix it prints a last-row error of about 0.01 (0.0078 and 0.0143 in one run, 0.0094 and 0.0073 in another; the inputs are not seeded).

Run it with CUDA_LAUNCH_BLOCKING=1. Without it, CUDA may report the fault at a later, unrelated call, such as the next torch.randn.

Notes

  • A regression test needs a tensor above 2^31 elements (about 13 GB for SwiGLU in bf16), so it probably needs a skip when the GPU has less free memory.
  • We did not audit the other Triton kernels in deepspeed/ops/triton_ops/ for this pattern.

System info

  • DeepSpeed upstream master 4e394422a (2026-09-18), and the same tree with the fix
  • torch 2.8.0+cu126, Triton 3.4.0, transformers 5.15.1
  • Measurements on 1 x NVIDIA H200. The training crash was on 4 nodes x 8 NVIDIA H200, EFA.

pengdurice avatar Sep 18 '26 18:09 pengdurice