跳转至

Normalization Operators

本页暂无中文版。以下为英文原文。

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

LayerNorm

tileops.norm.LayerNormFwdOp

Layer Normalization operator.

Computes layer normalization over the trailing normalized_shape axes:

\[ y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} \cdot w + b \]

Follows torch.nn.functional.layer_norm: an absent weight scales by one and an absent bias shifts by zero.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

__init__

__init__(
    normalized_shape,
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • normalized_shape (Sequence[int]) –

    Trailing-axis shape over which the statistics run.

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    weight=None,
    bias=None,
)

Apply layer normalization over the trailing normalized_shape.

Parameters:

  • input (Tensor) –

    Input tensor whose trailing shape equals normalized_shape.

  • weight (Tensor | None, default: None ) –

    Affine scale of shape normalized_shape, or None.

  • bias (Tensor | None, default: None ) –

    Affine shift of shape normalized_shape, or None.

Returns:

  • Tensor –

    Normalized tensor of the same shape as input.

tileops.norm.FusedAddLayerNormFwdOp

Fused residual addition and Layer Normalization operator.

Computes the residual sum followed by layer normalization in a single fused kernel:

\[ \begin{aligned} r &= x + \mathrm{residual} \\ y &= \frac{r - \mathrm{E}[r]}{\sqrt{\mathrm{Var}[r] + \epsilon}} \cdot w + b \end{aligned} \]

Returns dual outputs (y, residual_out) so downstream residual connections can reuse the pre-norm sum without recomputation.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

Note

Supports arbitrary leading dimensions (3-D+) via flatten/unflatten. Handles non-contiguous inputs and any hidden size.

__init__

__init__(
    eps=1e-06,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • eps (float, default: 1e-06 ) –

    Epsilon for numerical stability. The default 1e-6 is that of flash_attn's layer_norm_fn.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    residual,
    weight,
    bias,
)

Apply fused residual addition and normalization.

Parameters:

  • x (Tensor) –

    Input tensor of shape (*leading, N).

  • residual (Tensor) –

    Residual tensor of the same shape as x.

  • weight (Tensor) –

    Affine scale of shape \([N]\).

  • bias (Tensor) –

    Affine shift of shape \([N]\).

Returns:

  • Tensor –

    (y, residual_out), where residual_out is x + residual, both of the

  • Tensor –

    same shape as x.

Raises:

  • ValueError –

    Dtypes or shapes disagree. Raised by the generated signature checks.

RMSNorm

tileops.norm.RMSNormFwdOp

Standalone Root Mean Square (RMS) Normalization operator.

Follows torch.nn.functional.rms_norm. Computes::

y = x * rsqrt(mean(x ** 2, trailing_axes) + eps) * weight

where the reduction runs over the trailing len(normalized_shape) axes. An absent weight scales by one; eps=None is torch.finfo(torch.float32).eps, the value torch.nn.functional.rms_norm uses for float16 and bfloat16 inputs: torch takes the machine epsilon of the float32 accumulation type, not of the input dtype.

group_size splits the flattened trailing axes into runs of that many elements, each divided by its own root mean square, the weight still spanning all of them. zero_centered scales by 1 + weight, as vLLM's GemmaRMSNorm does: the arithmetic is float32 and the result rounds once to the input dtype.

Example
1
2
3
4
op = RMSNormFwdOp(normalized_shape=(4096,), eps=1e-6)
x = torch.randn(1024, 4096, dtype=torch.float16, device="cuda")
w = torch.randn(4096, dtype=torch.float16, device="cuda")
y = op(x, w)  # shape: (1024, 4096)

__init__

__init__(
    normalized_shape,
    eps=None,
    *,
    group_size=None,
    zero_centered=False,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • normalized_shape (Sequence[int]) –

    Trailing-axis shape over which the reduction runs.

  • eps (float | None, default: None ) –

    Epsilon for numerical stability; None takes the machine epsilon of float32, the dtype the reduction accumulates in, as torch does.

  • group_size (int | None, default: None ) –

    Elements each root mean square covers; None for all of normalized_shape.

  • zero_centered (bool, default: False ) –

    Whether to scale by 1 + weight instead of weight.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    weight=None,
)

Apply RMS normalization over the trailing normalized_shape.

Parameters:

  • x (Tensor) –

    Input tensor whose trailing shape equals normalized_shape.

  • weight (Tensor | None, default: None ) –

    Affine scale of shape normalized_shape, or None.

Returns:

  • Tensor –

    Normalized tensor of the same shape as x.

tileops.norm.FusedAddRMSNormFwdOp

Fused residual addition and RMS Normalization operator.

Computes the residual sum followed by RMS normalization in a single fused kernel:

\[ \begin{aligned} r &= x + \mathrm{residual} \\ y &= \frac{r}{\sqrt{\mathrm{mean}(r^2) + \epsilon}} \cdot w \end{aligned} \]

Returns dual outputs (y, residual_out) so downstream residual connections can reuse the pre-norm sum without recomputation.

zero_centered scales by 1 + w, as flashinfer.norm.gemma_fused_add_rmsnorm does: the arithmetic is float32 and y rounds once to the input dtype.

Supported dtypes

torch.float16, torch.bfloat16.

Note

Supports arbitrary leading dimensions (3-D+) via flatten/unflatten. Handles non-contiguous inputs and non-power-of-two hidden dims by padding to 256-element alignment.

__init__

__init__(
    eps=1e-06,
    *,
    zero_centered=False,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • eps (float, default: 1e-06 ) –

    Epsilon for numerical stability. The default 1e-6 is that of flashinfer.norm.fused_add_rmsnorm.

  • zero_centered (bool, default: False ) –

    Whether to scale by 1 + weight instead of weight.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    residual,
    weight,
)

Apply fused residual addition and normalization.

Parameters:

  • input (Tensor) –

    Input tensor of shape (*leading, N).

  • residual (Tensor) –

    Residual tensor of the same shape as input.

  • weight (Tensor) –

    Affine scale of shape \([N]\).

Returns:

  • Tensor –

    (y, residual_out), where residual_out is input + residual, both of the

  • Tensor –

    same shape as input.

Raises:

  • ValueError –

    Dtypes or shapes disagree. Raised by the generated signature checks.

tileops.norm.FusedAddRMSNormPerTokenQuantFwdOp

Fused residual addition, RMS normalization and dynamic per-token quantization.

One kernel computes the residual sum, both row reductions (the sum of squares and the absolute maximum) and the codes:

\[ \begin{aligned} r &= x + \mathrm{residual} \\ y &= \frac{r}{\sqrt{\mathrm{mean}(r^2) + \epsilon}} \cdot w \\ s &= \max\left(\frac{\min(\max|y|, \mathrm{scale\_ub})}{q_{\max}}, s_{\min}\right) \\ q &= \mathrm{sat}(y \cdot s^{-1}) \end{aligned} \]

It follows vLLM's rms_norm_dynamic_per_token_quant: r enters the reduction in float32 unrounded, y is rounded to the input dtype before and after the weight multiply, \(q_{\max}\) is 448 for float8_e4m3fn and 127 for int8, \(s_{\min}\) is \(1 / (448 \cdot 512)\) and the float32 epsilon respectively, and int8 codes round half to even. scale_ub applies to float8_e4m3fn codes only, as in vLLM. A NaN is skipped by the absolute maximum, and a NaN code becomes +448 in float8_e4m3fn and 0 in int8.

The row reduction's order differs from vLLM's, so the float32 \(\mathrm{rrms}\) may differ by one ulp. Then the scale may differ by up to two ulps of the input dtype, an FP8 code by one representable step and an INT8 code by \(\lceil 4 \epsilon_T \cdot 127 \rceil\) (4 for bfloat16, 1 for float16); residual_out is exact.

Returns (q, scale, residual_out): the codes, the float32 per-token scale shaped (*B, 1) and input + residual in the input dtype.

Supported dtypes

torch.float16, torch.bfloat16 inputs; torch.float8_e4m3fn, torch.int8 codes.

__init__

__init__(
    quant_dtype=torch.float8_e4m3fn,
    eps=1e-06,
    scale_ub=None,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from each call.

Parameters:

  • quant_dtype (dtype, default: float8_e4m3fn ) –

    Dtype of the codes, torch.float8_e4m3fn or torch.int8.

  • eps (float, default: 1e-06 ) –

    Epsilon for numerical stability. vLLM's rms_norm_dynamic_per_token_quant takes it without a default; 1e-6 is the default of vLLM's RMSNorm layer.

  • scale_ub (float | None, default: None ) –

    Upper bound on a token's absolute maximum before it sets the scale, or None for no bound.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    residual,
    weight,
)

Add, normalize and quantize each token.

Parameters:

  • input (Tensor) –

    Input tensor of shape (*B, N).

  • residual (Tensor) –

    Residual tensor of the same shape as input.

  • weight (Tensor) –

    Affine scale of shape \([N]\).

Returns:

  • Tensor –

    (q, scale, residual_out): q shaped like input in quant_dtype,

  • Tensor –

    scale of shape (*B, 1) in float32 and residual_out, input + residual,

  • Tensor –

    shaped and typed like input.

Raises:

  • ValueError –

    Dtypes or shapes disagree. Raised by the generated signature checks.

tileops.norm.FusedQKNormRoPEFwdOp

RMSNorm of every q and k head of a packed projection, then RoPE, in place.

qkv holds num_heads q heads, then num_kv_heads k heads, then as many v heads, D values each. For token n and a q or k head with values x:

\[ y = \frac{x}{\sqrt{\mathrm{mean}(x^2) + \epsilon}} \cdot w \]

with w the head's q_weight or k_weight. The first R values of y then rotate by the angles of row positions[n] of cos_sin_cache, whose first R / 2 columns are cosines and last R / 2 sines. rope_layout="neox" rotates the pairs (y[i], y[i + R/2]), "interleaved" the pairs (y[2i], y[2i + 1]); values past R pass through normalized. The v heads are not touched.

The arithmetic is float32 and each value rounds once to the qkv dtype. The reciprocal square root is the hardware's approximate one, within two float32 units in the last place.

zero_centered scales by 1 + w instead of w, as vLLM's GemmaRMSNorm does. mrope_section gives each token three positions, temporal, height and width, in positions of shape (3, N), as vLLM's MRotaryEmbedding does: of the R / 2 rotary frequencies, the first mrope_section[0] take their angles from the temporal position's cache row, the next mrope_section[1] from the height's and the rest from the width's; with mrope_interleaved, frequency i takes the height's when i % 3 == 1 and i < 3 * mrope_section[1], the width's when i % 3 == 2 and i < 3 * mrope_section[2], and the temporal's otherwise. The in-tree kernel serves the interleaved assignment; consecutive sections raise OpNotAvailableError when a kernel is selected.

__init__

__init__(
    num_heads,
    num_kv_heads,
    eps=1e-06,
    rope_layout="neox",
    *,
    zero_centered=False,
    mrope_section=None,
    mrope_interleaved=False,
    target=None
)

Build the op. The head dim, rotary width, token count and dtypes come with each call.

Parameters:

  • num_heads (int) –

    q heads in qkv.

  • num_kv_heads (int) –

    k heads in qkv, and as many v heads after them.

  • eps (float, default: 1e-06 ) –

    Added to the mean square before the root. vLLM's fused_qk_norm_rope takes it without a default; 1e-6 is the default of vLLM's RMSNorm layer.

  • rope_layout (str, default: 'neox' ) –

    "neox" to rotate the halves of the rotary values against each other, "interleaved" to rotate adjacent pairs.

  • zero_centered (bool, default: False ) –

    Whether to scale by 1 + weight instead of weight.

  • mrope_section (Sequence[int] | None, default: None ) –

    Rotary frequencies of the temporal, height and width positions, summing to R / 2; None for one position a token.

  • mrope_interleaved (bool, default: False ) –

    Whether the three positions take frequencies in turn instead of in consecutive sections.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    qkv,
    q_weight,
    k_weight,
    cos_sin_cache,
    positions,
)

Normalize and rotate the q and k heads of qkv in place.

Parameters:

  • qkv (Tensor) –

    (N, (num_heads + 2 * num_kv_heads) * D), float16 or bfloat16, contiguous; its q and k heads are overwritten.

  • q_weight (Tensor) –

    (D,), the q heads' RMSNorm weight, in qkv's dtype.

  • k_weight (Tensor) –

    (D,), the k heads' RMSNorm weight, in qkv's dtype.

  • cos_sin_cache (Tensor) –

    (P, R), float16, bfloat16 or float32: per position, R / 2 cosines then R / 2 sines.

  • positions (Tensor) –

    int64 (N,), each token's row of cos_sin_cache; (3, N), its temporal, height and width rows, with mrope_section. Every position lies in [0, P); the result of one outside it is undefined.

Returns:

  • None –

    None; qkv is mutated and no other tensor is touched.

tileops.norm.RMSNormGatedFwdOp

RMS normalization over groups of the last axis, gated by act(z).

Follows mamba_ssm.ops.triton.layernorm_gated.rmsnorm_fn, with fla's sigmoid gate. Over each group of group_size columns of a row (the whole row when None):

\[ y = \frac{h}{\sqrt{\mathrm{mean}(h^2) + \epsilon}} \cdot w + b \]

With norm_before_gate, h = x and y is then multiplied by act(z); otherwise h = x \cdot act(z). act is silu or sigmoid, by activation. Without z there is no gate; bias None shifts by zero.

The arithmetic is float32 and y rounds once to the dtype of x, as vLLM's and fla's kernels round it. The reciprocal square root and the gate's reciprocal are the hardware's approximate ones, within two float32 units in the last place. The gate's exponential is 2^-64 exp2(64 - |z| log2 e), which keeps a gate down to the smallest float32 subnormal; the float32 rounding of that exponent makes its relative error larger than the reciprocal's.

__init__

__init__(
    eps=1e-06,
    group_size=None,
    norm_before_gate=True,
    activation="silu",
    *,
    target=None
)

Build the op. Shapes and dtypes are taken from each call.

Parameters:

  • eps (float, default: 1e-06 ) –

    Added to the mean square before the root.

  • group_size (int | None, default: None ) –

    Columns each norm runs over, dividing the row; None for the whole row.

  • norm_before_gate (bool, default: True ) –

    Gate the normalized row when true, the input when false.

  • activation (str, default: 'silu' ) –

    "silu" or "sigmoid", the gate's activation.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    weight,
    bias,
    z=None,
)

Normalize, scale, shift and gate each row of x.

Parameters:

  • x (Tensor) –

    (*B, N), float16, bfloat16 or float32. Read where it lies when each row is contiguous and the rows are evenly spaced, as the columns of a wider projection are; copied otherwise.

  • weight (Tensor) –

    (N,) scale, float16, bfloat16 or float32.

  • bias (Tensor | None) –

    (N,) shift, float16, bfloat16 or float32, or None.

  • z (Tensor | None, default: None ) –

    The gate, shaped like x, float16, bfloat16 or float32, or None; read as x is.

Returns:

  • Tensor –

    A new tensor shaped like x, in its dtype.

Adaptive LayerNorm

tileops.norm.AdaLayerNormFwdOp

Adaptive Layer Normalization (AdaLN) operator.

Applies layer normalization with per-token adaptive scale and shift:

\[ y = s \cdot \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} + d \]

where s (scale) and d (shift) are per-token tensors of shape (M, N), pre-computed by the caller from a conditioning signal. Linear projection from the conditioning input to scale/shift is the caller's responsibility.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

Note

Supports arbitrary leading dimensions (3-D+) via flatten/unflatten. Handles non-contiguous inputs and non-power-of-two hidden dims.

__init__

__init__(
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability (manifest params.eps).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    scale,
    shift,
)

Apply adaptive layer normalization.

Parameters:

  • x (Tensor) –

    Tensor of shape (*leading, N).

  • scale (Tensor) –

    Tensor of shape (*leading, N).

  • shift (Tensor) –

    Tensor of shape (*leading, N).

Returns:

  • Tensor –

    Tensor of the same shape as x.

Raises:

  • ValueError –

    Dtypes or shapes disagree. Raised by the generated signature checks.

tileops.norm.AdaLayerNormZeroFwdOp

Adaptive Layer Normalization-Zero (AdaLN-Zero) operator.

Applies layer normalization with per-token adaptive scale, shift, and gating:

\[ y = g \cdot \left( s \cdot \frac{x - \mathrm{E}[x]} {\sqrt{\mathrm{Var}[x] + \epsilon}} + d \right) \]

where s (scale), d (shift), and g (gate) are per-token tensors of shape \([M \times N]\), pre-computed by the caller from a conditioning signal. Linear projection from the conditioning input to scale/shift/gate is the caller's responsibility.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

Note

Supports arbitrary leading dimensions (3-D+) via flatten/unflatten. Handles non-contiguous inputs and non-power-of-two hidden dims.

__init__

__init__(
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability (manifest params.eps).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    scale,
    shift,
    gate,
)

Apply adaptive layer normalization with zero-init gating.

Parameters:

  • x (Tensor) –

    Tensor of shape (*leading, N).

  • scale (Tensor) –

    Tensor of shape (*leading, N).

  • shift (Tensor) –

    Tensor of shape (*leading, N).

  • gate (Tensor) –

    Tensor of shape (*leading, N).

Returns:

  • Tensor –

    Tensor of the same shape as x.

Raises:

  • ValueError –

    Dtypes or shapes disagree. Raised by the generated signature checks.

BatchNorm

tileops.norm.BatchNormFwdOp

Batch Normalization forward; in training it updates the running statistics in place.

Computes batch normalization over the channel dimension:

\[ y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} \cdot \gamma + \beta \]

where the mean and variance are computed per channel over (N, *spatial) elements in training, and read from the running statistics in inference.

Follows torch.nn.functional.batch_norm: forward takes (input, running_mean, running_var, weight, bias) in PyTorch's positional order. It returns (output, save_mean, save_invstd), the structure of torch.ops.aten.native_batch_norm: in training the last two are the per-channel batch mean and reciprocal standard deviation that BatchNormBwdOp takes; without training they are None. The running statistics are optional in training, where they are updated in place when passed, and required in inference. An absent weight scales by one and an absent bias shifts by zero.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

__init__

__init__(
    training=False,
    momentum=0.1,
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • training (bool, default: False ) –

    Whether the batch statistics come from this call's input, which is also what decides whether passed running statistics are written.

  • momentum (float, default: 0.1 ) –

    Running-stat update momentum (used in training mode).

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    running_mean,
    running_var,
    weight=None,
    bias=None,
)

Run batch normalization forward pass.

The training mode is bound at ctor time. Construct a separate op instance to switch between training and inference.

Parameters:

  • input (Tensor) –

    Input tensor of shape (N, C, *spatial).

  • running_mean (Tensor | None) –

    Running mean of shape \([C]\), torch.float32, or None; updated in place in training, not None in inference.

  • running_var (Tensor | None) –

    Running variance, the same; passed exactly when running_mean is.

  • weight (Tensor | None, default: None ) –

    Affine scale (gamma) of shape \([C]\), torch.float32, or None.

  • bias (Tensor | None, default: None ) –

    Affine shift (beta) of shape \([C]\), torch.float32, or None.

Returns:

  • Tensor –

    (output, save_mean, save_invstd). output has the shape of input. In

  • Tensor | None –

    training, save_mean and save_invstd are the batch mean and

  • Tensor | None –

    \(1 / \sqrt{\mathrm{Var}[x] + \epsilon}\) (biased variance) per channel, \([C]\),

  • tuple[Tensor, Tensor | None, Tensor | None] –

    torch.float32; without training both are None.

tileops.norm.BatchNormBwdOp

Batch Normalization backward operator.

Computes the gradients of BatchNormFwdOp in training with respect to its input, scale and shift, from the per-channel save_mean and save_invstd that forward returns. An inference call of BatchNormFwdOp returns no statistics and feeds no backward.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

__init__

__init__(
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    doutput,
    input,
    save_mean,
    save_invstd,
    *,
    weight=None
)

Run batch normalization backward pass.

Parameters:

  • doutput (Tensor) –

    Upstream gradient of shape (N, C, *spatial).

  • input (Tensor) –

    Original input tensor of the same shape.

  • save_mean (Tensor) –

    Per-channel batch mean from the forward pass, \([C]\), torch.float32.

  • save_invstd (Tensor) –

    Per-channel reciprocal std from the forward pass, \([C]\), torch.float32.

  • weight (Tensor | None, default: None ) –

    Affine scale (gamma) of shape \([C]\), torch.float32, or None for the unit scale of a forward called without one.

Returns:

  • Tensor –

    Tuple of (dinput, dweight, dbias) where dinput

  • Tensor –

    has the same shape as input, dweight has shape \([C]\),

  • Tensor –

    and dbias has shape \([C]\).

GroupNorm and InstanceNorm

tileops.norm.GroupNormFwdOp

Group Normalization forward operator.

Computes group normalization over (C/num_groups, *spatial) slices:

\[ y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} \cdot w + b \]

where the mean and variance are computed per group over (C/num_groups, *spatial) elements. weight and bias are independent, as in torch: an absent one scales by one or shifts by zero.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

__init__

__init__(
    num_groups,
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • num_groups (int) –

    Number of groups; it divides the channel count.

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    weight=None,
    bias=None,
)

Apply group normalization.

Parameters:

  • input (Tensor) –

    Input tensor of shape (N, C, *spatial).

  • weight (Tensor | None, default: None ) –

    Affine scale of shape \([C]\), or None.

  • bias (Tensor | None, default: None ) –

    Affine shift of shape \([C]\), or None.

Returns:

  • Tensor –

    Normalized tensor of the same shape as input.

tileops.norm.InstanceNormFwdOp

Instance Normalization forward; use_input_stats updates running statistics in place.

Computes instance normalization over spatial dimensions for each (batch, channel) independently:

\[ y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} \cdot w + b \]

where the mean and variance are computed over *spatial for each sample-channel pair, or read from the running statistics when use_input_stats=False. An absent weight scales by one and an absent bias shifts by zero. With use_input_stats=True passed running statistics take the batch mean of the instance means and unbiased variances with momentum. As in torch, the running statistics are read and updated in the input's dtype.

Supported dtypes

torch.float32, torch.float16, torch.bfloat16.

__init__

__init__(
    use_input_stats=True,
    momentum=0.1,
    eps=1e-05,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • use_input_stats (bool, default: True ) –

    Whether each instance is normalized by its own statistics (the default) or by the running statistics.

  • momentum (float, default: 0.1 ) –

    Weight of this call's statistics in the running-statistics update.

  • eps (float, default: 1e-05 ) –

    Epsilon for numerical stability.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
    running_mean=None,
    running_var=None,
    weight=None,
    bias=None,
)

Apply instance normalization.

Parameters:

  • input (Tensor) –

    Input tensor of shape (N, C, *spatial).

  • running_mean (Tensor | None, default: None ) –

    Per-channel running mean of shape \([C]\), torch.float32. Required when use_input_stats=False; updated in place otherwise.

  • running_var (Tensor | None, default: None ) –

    Per-channel running variance, passed exactly when running_mean is.

  • weight (Tensor | None, default: None ) –

    Affine scale of shape \([C]\) in input's dtype, or None.

  • bias (Tensor | None, default: None ) –

    Affine shift of shape \([C]\) in input's dtype, or None.

Returns:

  • Tensor –

    Normalized tensor of the same shape as input.