Skip to content

vllm.models.inkling2.nvidia.ops.vision

Triton kernels for the Inkling2 vision encoder.

Rows are tokens of the packed images (a CLS row, then the native patches in row-major order). Row metadata is int32 [T, 4]: native (y, x) and the native grid (height, width), with y = x = -1 on CLS rows. Outputs are rounded to bf16 at the same points as the eager PyTorch ops they replace.

Functions:

  • apply_rotary_qk_ –

    In-place interleaved 2D RoPE on the q and k thirds of qkv [T, 3D].

  • embed_tokens –

    Returns (hidden, ln_1(hidden)) with hidden = ln_pre(embeddings).

  • gather_patches –

    Native patches of raw [N, side, side, 3] pixels as bf16 rows of

  • scaled_residual_norm_ –

    hidden += update * gamma in place; returns norm(hidden).

apply_rotary_qk_(qkv, meta, num_heads)

In-place interleaved 2D RoPE on the q and k thirds of qkv [T, 3D].

Source code in vllm/models/inkling2/nvidia/ops/vision.py
def apply_rotary_qk_(qkv: torch.Tensor, meta: torch.Tensor, num_heads: int) -> None:
    """In-place interleaved 2D RoPE on the q and k thirds of `qkv [T, 3D]`."""
    num_tokens, width = qkv.shape
    head_dim = width // (3 * num_heads)
    block_heads = min(4, triton.next_power_of_2(num_heads))
    grid = (num_tokens, triton.cdiv(num_heads, block_heads))
    _rotary_qk_kernel[grid](
        qkv,
        meta,
        _rope_inv_freq(head_dim, qkv.device),
        qkv.stride(0),
        NUM_HEADS=num_heads,
        HEAD_DIM=head_dim,
        BLOCK_HEADS=block_heads,
        BLOCK_PAIRS=triton.next_power_of_2(head_dim // 2),
        num_warps=4,
    )

embed_tokens(patch_embeds, patch_rows, position_grid, meta, class_embedding, cls_position_embedding, ln_pre, ln_1)

Returns (hidden, ln_1(hidden)) with hidden = ln_pre(embeddings).

Token row i takes patch embedding patch_embeds[patch_rows[i]]; CLS rows (patch_rows unused) take the class embedding.

Source code in vllm/models/inkling2/nvidia/ops/vision.py
def embed_tokens(
    patch_embeds: torch.Tensor,
    patch_rows: torch.Tensor,
    position_grid: torch.Tensor,
    meta: torch.Tensor,
    class_embedding: torch.Tensor,
    cls_position_embedding: torch.Tensor,
    ln_pre: torch.nn.LayerNorm,
    ln_1: torch.nn.LayerNorm,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Returns `(hidden, ln_1(hidden))` with `hidden = ln_pre(embeddings)`.

    Token row i takes patch embedding `patch_embeds[patch_rows[i]]`; CLS rows
    (`patch_rows` unused) take the class embedding.
    """
    num_rows, dim = meta.shape[0], patch_embeds.shape[1]
    assert ln_pre.eps == ln_1.eps
    hidden = patch_embeds.new_empty((num_rows, dim))
    normed = torch.empty_like(hidden)
    _embed_kernel[(num_rows,)](
        patch_embeds,
        patch_rows,
        position_grid,
        meta,
        class_embedding,
        cls_position_embedding,
        ln_pre.weight,
        ln_pre.bias,
        ln_1.weight,
        ln_1.bias,
        hidden,
        normed,
        ln_pre.eps,
        SOURCE_HEIGHT=position_grid.shape[0],
        SOURCE_WIDTH=position_grid.shape[1],
        DIM=dim,
        BLOCK_DIM=triton.next_power_of_2(dim),
        num_warps=4,
    )
    return hidden, normed

gather_patches(pixels, offsets, patch_size, mean, clip_std, std)

Native patches of raw [N, side, side, 3] pixels as bf16 rows of (py, px, c) values in [-1, 1].

Each value is 2 * (bf16((v / 255 - mean) / clip_std) * std + mean) - 1. offsets[i] is the element offset of row i's top-left pixel.

Source code in vllm/models/inkling2/nvidia/ops/vision.py
def gather_patches(
    pixels: torch.Tensor,
    offsets: torch.Tensor,
    patch_size: int,
    mean: tuple[float, float, float],
    clip_std: tuple[float, float, float],
    std: tuple[float, float, float],
) -> torch.Tensor:
    """Native patches of raw `[N, side, side, 3]` pixels as bf16 rows of
    `(py, px, c)` values in [-1, 1].

    Each value is `2 * (bf16((v / 255 - mean) / clip_std) * std + mean) - 1`.
    `offsets[i]` is the element offset of row i's top-left pixel.
    """
    _, side, _, channels = pixels.shape
    assert channels == 3 and pixels.is_contiguous()
    features = patch_size * patch_size * 3
    out = pixels.new_empty((offsets.numel(), features))
    _gather_patches_kernel[(offsets.numel(),)](
        pixels,
        offsets,
        out,
        *mean,
        *clip_std,
        *std,
        SIDE=side,
        PATCH=patch_size,
        BLOCK=triton.next_power_of_2(features),
        num_warps=4,
        enable_fp_fusion=False,
    )
    return out

scaled_residual_norm_(hidden, update, gamma, norm)

hidden += update * gamma in place; returns norm(hidden).

Source code in vllm/models/inkling2/nvidia/ops/vision.py
def scaled_residual_norm_(
    hidden: torch.Tensor,
    update: torch.Tensor,
    gamma: torch.Tensor,
    norm: torch.nn.LayerNorm | None,
) -> torch.Tensor | None:
    """`hidden += update * gamma` in place; returns `norm(hidden)`."""
    num_rows, dim = hidden.shape
    assert hidden.is_contiguous() and update.is_contiguous()
    normed = None if norm is None else torch.empty_like(hidden)
    _scaled_residual_norm_kernel[(num_rows,)](
        hidden,
        update,
        gamma,
        None if norm is None else norm.weight,
        None if norm is None else norm.bias,
        normed,
        0.0 if norm is None else norm.eps,
        DIM=dim,
        BLOCK_DIM=triton.next_power_of_2(dim),
        HAS_NORM=norm is not None,
        num_warps=4,
    )
    return normed