Skip to content

vllm.models.inkling2.nvidia.ops.linear

bf16 linears: small batches on FlashInfer's CuTe DSL low-M kernels, and the reference linear with fp32 accumulation in ordered 1024-wide K groups.

Functions:

  • k_group_partials –

    fp32 partials [G, T, N] of x @ weight.T whose in-order sum over G is

  • linear –

    bf16 x @ weight.T with fp32 accumulation (into out if given,

  • linear_k_groups –

    x @ weight.T for bf16 x [T, K] and weight [N, K].

k_group_partials(x, weight, out_dtype=torch.float32, launch_dependents=False)

fp32 partials [G, T, N] of x @ weight.T whose in-order sum over G is the reference linear. G is 1 (already summed, in out_dtype) when there are enough rows to fill the GPU without splitting K. With launch_dependents, the next kernel may start (and must wait) while the TMA kernel runs.

Source code in vllm/models/inkling2/nvidia/ops/linear.py
def k_group_partials(
    x: torch.Tensor,
    weight: torch.Tensor,
    out_dtype: torch.dtype = torch.float32,
    launch_dependents: bool = False,
) -> torch.Tensor:
    """fp32 partials [G, T, N] of `x @ weight.T` whose in-order sum over G is
    the reference `linear`. G is 1 (already summed, in `out_dtype`) when
    there are enough rows to fill the GPU without splitting K. With
    `launch_dependents`, the next kernel may start (and must wait) while
    the TMA kernel runs."""
    assert x.is_contiguous() and weight.is_contiguous()
    tokens, hidden = x.shape
    n = weight.shape[0]
    sm120 = current_platform.is_device_capability_family(120)
    if tokens >= TMA_MIN_TOKENS:
        from triton.tools.tensor_descriptor import TensorDescriptor

        out = torch.empty((1, tokens, n), dtype=out_dtype, device=x.device)
        # SM12x allows ~99 KiB of shared memory per block: two 48 KiB stages fit.
        block_n = 64 if tokens < 4096 or sm120 else 128
        _k_group_tma_kernel[(triton.cdiv(n, block_n), triton.cdiv(tokens, 128))](
            TensorDescriptor.from_tensor(x, [128, 128]),
            TensorDescriptor.from_tensor(weight, [block_n, 128]),
            out,
            tokens,
            n,
            hidden,
            128,
            block_n,
            128,
            launch_dependents,
            num_warps=4 if block_n == 64 else 8,
            num_stages=2 if sm120 else 4 if block_n == 64 else 3,
            enable_fp_fusion=False,
            launch_pdl=True,
        )
        return out
    block_m = 16 if tokens <= 128 else 64
    block_n = 32 if tokens <= 16 else 64 if tokens <= 256 else 128
    groups = triton.cdiv(hidden, 1024)
    split = groups > 1 and tokens < 4096
    num_warps = 4 if block_n <= 64 else 8
    num_stages = 4 if not split else 3 if block_n <= 64 else 2
    if sm120:
        num_stages = min(num_stages, 2)
    out = torch.empty(
        (groups if split else 1, tokens, n),
        dtype=torch.float32 if split else out_dtype,
        device=x.device,
    )
    grid = (
        triton.cdiv(n, block_n),
        triton.cdiv(tokens, block_m),
        groups if split else 1,
    )
    _k_group_kernel[grid](
        x,
        weight,
        out,
        tokens,
        n,
        hidden,
        block_m,
        block_n,
        split,
        num_warps=num_warps,
        num_stages=num_stages,
        enable_fp_fusion=False,
        launch_pdl=True,
    )
    return out

linear(x, weight, out=None)

bf16 x @ weight.T with fp32 accumulation (into out if given, batches over LOW_M_MAX_TOKENS only).

Source code in vllm/models/inkling2/nvidia/ops/linear.py
def linear(
    x: torch.Tensor, weight: torch.Tensor, out: torch.Tensor | None = None
) -> torch.Tensor:
    """bf16 `x @ weight.T` with fp32 accumulation (into `out` if given,
    batches over LOW_M_MAX_TOKENS only)."""
    if out is not None:
        assert x.shape[0] > LOW_M_MAX_TOKENS
        return torch.mm(x, weight.t(), out=out)
    if use_sm12x_bf16_ll_gemm(x, weight):
        return sm12x_bf16_ll_gemm(x, weight)
    # Autotuning would profile the CuTe DSL kernels past their 32-row limit.
    if (
        x.shape[0] > LOW_M_MAX_TOKENS
        or is_flashinfer_autotuning()
        or not use_sm100_kernels()
    ):
        return torch.nn.functional.linear(x, weight)
    from flashinfer import mm_bf16

    return mm_bf16(x, weight.t(), pdl=True, backend="cute-dsl")

linear_k_groups(x, weight, out_dtype=torch.float32)

x @ weight.T for bf16 x [T, K] and weight [N, K].

Each 1024-wide K group is accumulated in fp32 and the partials are summed in order, as in the reference linear.

Source code in vllm/models/inkling2/nvidia/ops/linear.py
def linear_k_groups(
    x: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype = torch.float32
) -> torch.Tensor:
    """`x @ weight.T` for bf16 `x` [T, K] and `weight` [N, K].

    Each 1024-wide K group is accumulated in fp32 and the partials are summed in
    order, as in the reference `linear`.
    """
    partials = k_group_partials(x, weight, out_dtype)
    if partials.shape[0] == 1:
        return partials[0]
    out = torch.empty(partials.shape[1:], dtype=out_dtype, device=x.device)
    _k_group_sum_kernel[(triton.cdiv(out.numel(), 1024),)](
        partials, out, out.numel(), partials.shape[0], 1024, launch_pdl=True
    )
    return out