Source code for mstar.engine.resources.rms_norm.rms_norm

"""Backend-neutral RMSNorm custom operator.

The operator keeps fused accelerator implementations behind one stable
``mstar::rms_norm`` graph boundary. CUDA currently dispatches to FlashInfer,
XPU dispatches to vllm-xpu-kernels, and other devices use the portable
PyTorch implementation. ``mstar::fused_add_rms_norm`` does the same for the
in-place residual add + RMSNorm: FlashInfer's fused kernel on CUDA in half
precision, the unfused add and norm elsewhere.
"""

import torch

from mstar.engine.node_dtype import warn_implicit_downcast


def _prepare_inputs(
    x: torch.Tensor,
    weight: torch.Tensor,
    norm_dtype: torch.dtype | None,
) -> tuple[torch.Tensor, torch.Tensor, torch.dtype]:
    """Apply the dtype contract shared by every RMSNorm kernel."""
    orig_dtype = x.dtype
    device_type = x.device.type
    if norm_dtype is not None:
        x = x.to(norm_dtype)
    elif torch.is_autocast_enabled(device_type):
        x = x.to(torch.get_autocast_dtype(device_type))
    elif x.dtype == torch.float32 and device_type == "cuda":
        warn_implicit_downcast("rms_norm", x.dtype, torch.bfloat16)
        x = x.to(torch.bfloat16)
    if weight.dtype != x.dtype:
        weight = weight.to(x.dtype)
    return x, weight, orig_dtype


@torch.library.custom_op("mstar::rms_norm", mutates_args=())
def rms_norm(
    x: torch.Tensor,
    weight: torch.Tensor,
    eps: float,
    norm_dtype: torch.dtype | None = None,
) -> torch.Tensor:
    """Portable RMSNorm fallback, returned in ``x``'s original dtype."""
    x, weight, orig_dtype = _prepare_inputs(x, weight, norm_dtype)
    variance = x.float().pow(2).mean(dim=-1, keepdim=True)
    output = x * torch.rsqrt(variance + eps).to(x.dtype)
    return (output * weight).to(orig_dtype)


@torch.library.register_kernel(rms_norm, "cuda")
def _rms_norm_cuda(
    x: torch.Tensor,
    weight: torch.Tensor,
    eps: float,
    norm_dtype: torch.dtype | None = None,
) -> torch.Tensor:
    import flashinfer

    x, weight, orig_dtype = _prepare_inputs(x, weight, norm_dtype)
    return flashinfer.norm.rmsnorm(x, weight, eps=eps).to(orig_dtype)


@torch.library.register_kernel(rms_norm, "xpu")
def _rms_norm_xpu(
    x: torch.Tensor,
    weight: torch.Tensor,
    eps: float,
    norm_dtype: torch.dtype | None = None,
) -> torch.Tensor:
    import vllm_xpu_kernels._C  # noqa: F401

    x, weight, orig_dtype = _prepare_inputs(x, weight, norm_dtype)
    output = torch.empty_like(x)
    torch.ops._C.rms_norm(output, x, weight, eps)
    return output.to(orig_dtype)


@rms_norm.register_fake
def _rms_norm_fake(
    x: torch.Tensor,
    weight: torch.Tensor,
    eps: float,
    norm_dtype: torch.dtype | None = None,
) -> torch.Tensor:
    return torch.empty_like(x)


[docs] def run_rms_norm( input: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6, rms_norm_dtype: torch.dtype | None = None, ) -> torch.Tensor: """Apply the backend-neutral ``mstar::rms_norm`` custom operator.""" return torch.ops.mstar.rms_norm( input, weight, eps, rms_norm_dtype, )
[docs] def gemma_rms_norm(x: torch.Tensor, weight: torch.Tensor, eps: float) -> torch.Tensor: """Gemma's RMSNorm, ``normed * (1 + weight)``, computed in fp32 as Hugging Face Gemma computes it and returned in ``x``'s dtype.""" orig_dtype = x.dtype x = x.to(torch.float32) var = x.square().mean(dim=-1, keepdim=True) normed = x * torch.rsqrt(var + eps) normed = normed * (1.0 + weight.to(torch.float32)) return normed.to(orig_dtype)
def _unfused_add_rms_norm( x: torch.Tensor, residual: torch.Tensor, weight: torch.Tensor, eps: float, gemma_mode: bool, ) -> None: residual.add_(x) x.copy_(gemma_rms_norm(residual, weight, eps) if gemma_mode else rms_norm(residual, weight, eps)) @torch.library.custom_op("mstar::fused_add_rms_norm", mutates_args=("x", "residual")) def fused_add_rms_norm( x: torch.Tensor, residual: torch.Tensor, weight: torch.Tensor, eps: float, gemma_mode: bool, ) -> None: """In place: ``residual += x``, then ``x = rms_norm(residual)`` (Gemma's ``(1 + weight)`` when ``gemma_mode``). Portable fallback: the add, then ``mstar::rms_norm``.""" # The portable fallback (XPU, and CUDA in fp32). Inductor can't # fuse the add across the op boundary; if an XPU model adopts ``run_layers``, # register a fused XPU kernel for this op as CUDA does below. _unfused_add_rms_norm(x, residual, weight, eps, gemma_mode) @torch.library.register_kernel(fused_add_rms_norm, "cuda") def _fused_add_rms_norm_cuda( x: torch.Tensor, residual: torch.Tensor, weight: torch.Tensor, eps: float, gemma_mode: bool, ) -> None: """FlashInfer's fused kernel, which takes half precision only, and reads ``residual`` and ``weight`` in ``x``'s dtype: any other mix runs unfused. It normalizes the sum before rounding it, where the unfused pair rounds first. The op, rather than calling FlashInfer's wrapper directly, keeps the kernel behind a graph boundary: the wrapper is plain Python (its op registration is a no-op), so Dynamo traces into it and breaks the graph, and each call site then compiles as a frame of its own, once per norm module and shape. """ if x.dtype not in (torch.bfloat16, torch.float16) or not x.dtype == residual.dtype == weight.dtype: _unfused_add_rms_norm(x, residual, weight, eps, gemma_mode) return from flashinfer.norm import fused_add_rmsnorm, gemma_fused_add_rmsnorm (gemma_fused_add_rmsnorm if gemma_mode else fused_add_rmsnorm)(x, residual, weight, eps) @fused_add_rms_norm.register_fake def _fused_add_rms_norm_fake( x: torch.Tensor, residual: torch.Tensor, weight: torch.Tensor, eps: float, gemma_mode: bool, ) -> None: return None