Skip to content

vllm.model_executor.layers.fusion.mm_input_norm

Fused Normalisation on the Device.

Equivalent to::

output = (input * rescale_factor - image_mean) / image_std

This is implemented as a single per-channel affine transform::

output = input * weight[c] + bias[c]

where::

weight = rescale_factor / image_std
bias = -image_mean / image_std

Classes:

  • FusedMMInputNorm –

    Module that applies rescaling and normalisation to input images.

  • IdentityInputNorm –

    Stand-in used when the processor requires no rescale/normalise.

  • NormParams –

    Resolved per-channel affine parameters (flags already folded in).

Functions:

FusedMMInputNorm

Bases: CustomOp

Module that applies rescaling and normalisation to input images. Equivalent to: output = (input * rescale_factor - mean) / std

Dtype semantics:

  • Input dtype — the dtype of the pixel_values argument to forward_*. It is uint8 when mm_device_do_normalize is enabled (raw bytes travel to the device unprocessed) and equals visual_dtype otherwise.
  • Output dtype — the visual_dtype argument of forward_*. The computation itself is always fp32, independent of the output dtype (e.g. compute fp32, emit bf16).

Input Layout semantics:

  • layout="pcl" (default) — pixel_values is (patches, C*L), output has the same shape.
  • layout="bchw" — pixel_values is (B, C, H, W), output has the same shape.

Platform dispatch:

  • forward_native — pure PyTorch eager path, used as the semantic reference and default fallback on all platforms.
  • forward_cuda — Triton kernel path for CUDA devices.
  • forward_xpu — custom XPU kernel path.
  • forward_oot — out-of-tree platform override entry point; falls back to forward_native unless a plugin overrides it.

Methods:

  • forward_cuda –

    Triton kernel path for CUDA devices.

  • forward_native –

    Pure PyTorch eager implementation.

  • forward_oot –

    Out-of-tree platform override entrypoint.

  • forward_xpu –

    XPU fused custom kernel path for uint8 and Triton kernel path

_unpack(pixel_values)

Normalise either layout to (P, C, L) plus the output shape.

Returns a (x3, out_shape) pair where x3 is a (P, C, L) view of the input and out_shape is the shape the final output must have. The reshape below is a pure view as long as the caller's tensor is contiguous; the platform forward_* methods call .contiguous() inside fused_mm_input_norm_triton when needed.

forward_cuda(pixel_values, visual_dtype)

Triton kernel path for CUDA devices.

forward_native(pixel_values, visual_dtype)

Pure PyTorch eager implementation.

This is the semantic reference implementation and the fallback used on any platform without a specialised kernel.

forward_oot(pixel_values, visual_dtype)

Out-of-tree platform override entrypoint.

forward_xpu(pixel_values, visual_dtype)

XPU fused custom kernel path for uint8 and Triton kernel path for others.

IdentityInputNorm

Bases: Module

Stand-in used when the processor requires no rescale/normalise.

Not a no-op: with mm_device_do_normalize enabled, raw uint8 pixels travel to the device unprocessed and must be cast to visual_dtype here.

NormParams

Bases: NamedTuple

Resolved per-channel affine parameters (flags already folded in).

Attributes:

  • is_identity (bool) –

    Whether weight = rescale/std and bias = -mean/std are

is_identity property

Whether weight = rescale/std and bias = -mean/std are numerically the identity transform (mirrors torch.allclose's default fp32 tolerances).

_load_norm_params(model_config)

Resolve the per-channel affine parameters (image_mean, image_std, rescale_factor) from the processor config.

_validate_layout(layout)

Runtime validation for layouts supplied via config/plumbing.

A Literal is only checked statically; validating here preserves the fail-fast behaviour of the previous InputLayout(layout) call so that typos do not silently fall through to the PCL branch.

build_mm_input_norm(model_config, layout='pcl')

Build the input normalisation module for a model.

Returns an IdentityInputNorm when device-side normalisation is disabled or the processor's rescale/normalise is numerically the identity transform; otherwise a FusedMMInputNorm built from the processor's parameters.

fused_mm_input_norm_triton(inputs, outputs, weight, bias)

Fused per-channel affine transform for normalisation.

Parameters:

  • inputs

    (Tensor) –

    Input tensor, shape (P, C, L). A contiguous copy is materialized internally if needed.

  • outputs

    (Tensor) –

    Output tensor, contiguous and shaped exactly (P, C, L).

  • weight

    (Tensor) –

    Per-channel scale, shape (C,).

  • bias

    (Tensor) –

    Per-channel shift, shape (C,).

Returns:

  • Tensor –

    outputs, for chaining.