Repository navigation
AutoEP: fused Triton kernel for the clamped expert activations #8652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,3 +250,235 @@ def swiglu_packed(gate_up: torch.Tensor) -> torch.Tensor: | |
| return _swiglu_eager(gate, up) | ||
|
|
||
| return _SwiGLUPackedFn.apply(gate_up) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Clamped forms: silu(clamp(gate)) * clamp(up) ("swiglu_clamped", DeepSeek-V4) | ||
| # (clamp(up) + 1) * g * sigmoid(alpha * g) ("swiglu_oai", GPT-OSS / MiniMax-M3), g = clamp(gate) | ||
| # gate is clamped from above only, up on both sides. Same fusion as above: one kernel each way, float32 | ||
| # math, only gate and up saved for backward. The eager expression saves about five [rows, ffn] temporaries | ||
| # (the two clamps, the sigmoid, the products); on an expert-parallel rank that receives several times the | ||
| # average rows this is where the memory goes (MiniMax-M3, 65,536 tokens: an 11.96 GiB temporary here). | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| if _TRITON_AVAILABLE: | ||
|
|
||
| @triton.jit | ||
| def _swiglu_clamped_fwd_kernel(gate_ptr, up_ptr, out_ptr, n_elements, alpha, limit, OAI: tl.constexpr, | ||
| PACKED: tl.constexpr, INTER: tl.constexpr, BLOCK_SIZE: tl.constexpr): | ||
| pid = tl.program_id(axis=0).to(tl.int64) | ||
| offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
| mask = offsets < n_elements | ||
| if PACKED: | ||
| # gate_ptr and up_ptr are the same [rows, 2 * INTER] tensor, as in _swiglu_packed_fwd_kernel. | ||
| gate_offsets = (offsets // INTER) * (2 * INTER) + offsets % INTER | ||
| up_offsets = gate_offsets + INTER | ||
| else: | ||
| gate_offsets = offsets | ||
| up_offsets = offsets | ||
|
|
||
| gate = tl.load(gate_ptr + gate_offsets, mask=mask).to(tl.float32) | ||
| up = tl.load(up_ptr + up_offsets, mask=mask).to(tl.float32) | ||
| g = tl.minimum(gate, limit) | ||
| u = tl.minimum(tl.maximum(up, -limit), limit) | ||
|
|
||
| if OAI: | ||
| out = (u + 1.0) * (g * tl.sigmoid(g * alpha)) | ||
| else: | ||
| out = (g * tl.sigmoid(g)) * u | ||
|
|
||
| tl.store(out_ptr + offsets, out.to(out_ptr.dtype.element_ty), mask=mask) | ||
|
|
||
| @triton.jit | ||
| def _swiglu_clamped_bwd_kernel(grad_out_ptr, gate_ptr, up_ptr, grad_gate_ptr, grad_up_ptr, n_elements, alpha, | ||
| limit, OAI: tl.constexpr, PACKED: tl.constexpr, INTER: tl.constexpr, | ||
| BLOCK_SIZE: tl.constexpr): | ||
| pid = tl.program_id(axis=0).to(tl.int64) | ||
| offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
| mask = offsets < n_elements | ||
| if PACKED: | ||
| gate_offsets = (offsets // INTER) * (2 * INTER) + offsets % INTER | ||
| up_offsets = gate_offsets + INTER | ||
| else: | ||
| gate_offsets = offsets | ||
| up_offsets = offsets | ||
|
|
||
| grad_out = tl.load(grad_out_ptr + offsets, mask=mask).to(tl.float32) | ||
| gate = tl.load(gate_ptr + gate_offsets, mask=mask).to(tl.float32) | ||
| up = tl.load(up_ptr + up_offsets, mask=mask).to(tl.float32) | ||
| g = tl.minimum(gate, limit) | ||
| u = tl.minimum(tl.maximum(up, -limit), limit) | ||
| # torch.clamp passes the gradient through where the input is inside the limits, inclusive. | ||
| pass_gate = gate <= limit | ||
| pass_up = (up >= -limit) & (up <= limit) | ||
|
|
||
| if OAI: | ||
| sig = tl.sigmoid(g * alpha) | ||
| f = g * sig | ||
| df = sig * (1.0 + alpha * g * (1.0 - sig)) | ||
| grad_gate = grad_out * (u + 1.0) * df | ||
| grad_up = grad_out * f | ||
| else: | ||
| sig = tl.sigmoid(g) | ||
| f = g * sig | ||
| df = sig * (1.0 + g * (1.0 - sig)) | ||
| grad_gate = grad_out * u * df | ||
| grad_up = grad_out * f | ||
| grad_gate = tl.where(pass_gate, grad_gate, 0.0) | ||
| grad_up = tl.where(pass_up, grad_up, 0.0) | ||
|
|
||
| tl.store(grad_gate_ptr + gate_offsets, grad_gate.to(grad_gate_ptr.dtype.element_ty), mask=mask) | ||
| tl.store(grad_up_ptr + up_offsets, grad_up.to(grad_up_ptr.dtype.element_ty), mask=mask) | ||
|
|
||
| class _SwiGLUClampedFn(torch.autograd.Function): | ||
|
|
||
| @staticmethod | ||
| def forward(ctx, gate: torch.Tensor, up: torch.Tensor, alpha: float, limit: float, oai: bool) -> torch.Tensor: | ||
| gate = gate.contiguous() | ||
| up = up.contiguous() | ||
| out = torch.empty_like(gate) | ||
| n_elements = gate.numel() | ||
| if n_elements > 0: | ||
| grid = (triton.cdiv(n_elements, _BLOCK_SIZE), ) | ||
| _swiglu_clamped_fwd_kernel[grid](gate, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AGENTS.md reference: AGENTS.md:L40-L40 Useful? React with 👍 / 👎. |
||
| up, | ||
| out, | ||
| n_elements, | ||
| float(alpha), | ||
| float(limit), | ||
| OAI=oai, | ||
| PACKED=False, | ||
| INTER=1, | ||
| BLOCK_SIZE=_BLOCK_SIZE, | ||
| num_warps=_NUM_WARPS) | ||
| ctx.save_for_backward(gate, up) | ||
| ctx.alpha, ctx.limit, ctx.oai = float(alpha), float(limit), bool(oai) | ||
| return out | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_out: torch.Tensor): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller requests Useful? React with 👍 / 👎. |
||
| gate, up = ctx.saved_tensors | ||
| grad_out = grad_out.contiguous() | ||
| grad_gate = torch.empty_like(gate) | ||
| grad_up = torch.empty_like(up) | ||
| n_elements = gate.numel() | ||
| if n_elements > 0: | ||
| grid = (triton.cdiv(n_elements, _BLOCK_SIZE), ) | ||
| _swiglu_clamped_bwd_kernel[grid](grad_out, | ||
| gate, | ||
| up, | ||
| grad_gate, | ||
| grad_up, | ||
| n_elements, | ||
| ctx.alpha, | ||
| ctx.limit, | ||
| OAI=ctx.oai, | ||
| PACKED=False, | ||
| INTER=1, | ||
| BLOCK_SIZE=_BLOCK_SIZE, | ||
| num_warps=_NUM_WARPS) | ||
| return grad_gate, grad_up, None, None, None | ||
|
|
||
| class _SwiGLUClampedPackedFn(torch.autograd.Function): | ||
|
|
||
| @staticmethod | ||
| def forward(ctx, gate_up: torch.Tensor, alpha: float, limit: float, oai: bool) -> torch.Tensor: | ||
| gate_up = gate_up.contiguous() | ||
| inter = gate_up.shape[-1] // 2 | ||
| out = gate_up.new_empty(gate_up.shape[:-1] + (inter, )) | ||
| n_elements = out.numel() | ||
| if n_elements > 0: | ||
| grid = (triton.cdiv(n_elements, _BLOCK_SIZE), ) | ||
| # Triton uses the current device and stream, which may differ from the input tensor's device. | ||
| with torch.cuda.device(gate_up.device): #ignore-cuda | ||
| _swiglu_clamped_fwd_kernel[grid](gate_up, | ||
| gate_up, | ||
| out, | ||
| n_elements, | ||
| float(alpha), | ||
| float(limit), | ||
| OAI=oai, | ||
| PACKED=True, | ||
| INTER=inter, | ||
| BLOCK_SIZE=_BLOCK_SIZE, | ||
| num_warps=_NUM_WARPS) | ||
| ctx.save_for_backward(gate_up) | ||
| ctx.alpha, ctx.limit, ctx.oai = float(alpha), float(limit), bool(oai) | ||
| return out | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_out: torch.Tensor): | ||
| # The Triton backward has no autograd graph, so create_graph would silently lose the second derivative. | ||
| if torch.is_grad_enabled(): | ||
| raise RuntimeError("The packed clamped SwiGLU kernel has no second derivative; " | ||
| "backward with create_graph=True is not supported") | ||
| gate_up, = ctx.saved_tensors | ||
| grad_out = grad_out.contiguous() | ||
| grad_gate_up = torch.empty_like(gate_up) | ||
| n_elements = grad_out.numel() | ||
| if n_elements > 0: | ||
| grid = (triton.cdiv(n_elements, _BLOCK_SIZE), ) | ||
| with torch.cuda.device(gate_up.device): #ignore-cuda | ||
| _swiglu_clamped_bwd_kernel[grid](grad_out, | ||
| gate_up, | ||
| gate_up, | ||
| grad_gate_up, | ||
| grad_gate_up, | ||
| n_elements, | ||
| ctx.alpha, | ||
| ctx.limit, | ||
| OAI=ctx.oai, | ||
| PACKED=True, | ||
| INTER=gate_up.shape[-1] // 2, | ||
| BLOCK_SIZE=_BLOCK_SIZE, | ||
| num_warps=_NUM_WARPS) | ||
| return grad_gate_up, None, None, None | ||
|
|
||
|
|
||
| def _swiglu_clamped_eager(gate: torch.Tensor, up: torch.Tensor, alpha: float, limit: float, oai: bool) -> torch.Tensor: | ||
| """Pure-PyTorch reference used as the non-Triton fallback.""" | ||
| gate = gate.clamp(max=limit) | ||
| up = up.clamp(min=-limit, max=limit) | ||
| if oai: | ||
| return (up + 1.0) * (gate * torch.sigmoid(gate * alpha)) | ||
| return F.silu(gate) * up | ||
|
|
||
|
|
||
| def swiglu_clamped(gate: torch.Tensor, | ||
| up: torch.Tensor, | ||
| alpha: float = 1.702, | ||
| limit: float = 7.0, | ||
| oai: bool = True) -> torch.Tensor: | ||
| """Fused clamped SwiGLU. | ||
|
|
||
| ``oai=True``: ``(clamp(up) + 1) * g * sigmoid(alpha * g)`` with ``g = clamp(gate, max=limit)`` and | ||
| ``up`` clamped to ``[-limit, limit]`` (GPT-OSS, MiniMax-M3). ``oai=False``: ``silu(g) * clamp(up)`` | ||
| (DeepSeek-V4). Falls back to the eager expression when Triton is unavailable or the tensors are on | ||
| the CPU. | ||
| """ | ||
| if gate.shape != up.shape: | ||
| raise ValueError(f"swiglu_clamped expects gate and up to have the same shape, got {tuple(gate.shape)} " | ||
| f"and {tuple(up.shape)}") | ||
| if gate.dtype != up.dtype: | ||
| raise ValueError(f"swiglu_clamped expects gate and up to have the same dtype, got {gate.dtype} and {up.dtype}") | ||
| if not _TRITON_AVAILABLE or gate.device.type == "cpu": | ||
| return _swiglu_clamped_eager(gate, up, alpha, limit, oai) | ||
| return _SwiGLUClampedFn.apply(gate, up, alpha, limit, oai) | ||
|
|
||
|
|
||
| def swiglu_clamped_packed(gate_up: torch.Tensor, | ||
| alpha: float = 1.702, | ||
| limit: float = 7.0, | ||
| oai: bool = True) -> torch.Tensor: | ||
| """:func:`swiglu_clamped` of ``gate`` and ``up`` packed side by side in one ``[..., 2 * I]`` tensor, the output | ||
| of one gate+up GEMM, read in place as :func:`swiglu_packed` does. | ||
|
|
||
| The Triton path supports first-order gradients only and raises for ``create_graph=True``. Falls back to the | ||
| eager expression on the two halves when Triton is unavailable or the tensor is on the CPU. | ||
| """ | ||
| if gate_up.dim() == 0 or gate_up.shape[-1] % 2: | ||
| raise ValueError(f"swiglu_clamped_packed expects an even last dimension, got shape {tuple(gate_up.shape)}") | ||
| if not _TRITON_AVAILABLE or gate_up.device.type == "cpu": | ||
| gate, up = gate_up.chunk(2, dim=-1) | ||
| return _swiglu_clamped_eager(gate, up, alpha, limit, oai) | ||
| return _SwiGLUClampedPackedFn.apply(gate_up, alpha, limit, oai) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a preceding grouped GEMM yields a NaN in
gateorup, Triton's elementwiseminimum/maximumuse their default non-NaN-propagating mode, unlike the eagertorch.clampreference, so these lines replace the NaN with a finite clamp bound. The backward kernel repeats that clamp and its comparisons then zero the corresponding gradient, allowing a diverged AutoEP step to produce finite outputs and gradients; request NaN propagation (or use comparisons that preserve NaNs) in both kernels.Useful? React with 👍 / 👎.