Skip to content

vllm.models.inkling2.nvidia.ops.attn_res

Block attention residual: softmax-weighted mix of residual stream blocks.

For each token, sources x_n (stored blocks, then the partial block plus an optional addend, all in fp32) are scored as (q . x_n) * rsqrt(mean(x_n^2) + eps) per pseudo-query q; the output is sum_n softmax_n * x_n, with x_n RMS-normalized when normalize_values is set. The fp32 mix then goes through the following sublayer's RMSNorm in the same kernel, or is returned as is (fp32) without a norm weight.

Functions:

  • attn_res –

    Mix [*stack, last (+ addend)] with each pseudo-query.

attn_res(stack, last, addend, queries, norm_weight, norm_eps, normalize_values, eps=1e-06)

Mix [*stack, last (+ addend)] with each pseudo-query.

queries are fp32 (Q, D), already scaled by D**-0.5; each fp32 mix is RMS-normalized with its row of norm_weight (Q, D). Returns (Q, T, D) in last.dtype, or the fp32 mixes without norm_weight.

Source code in vllm/models/inkling2/nvidia/ops/attn_res.py
def attn_res(
    stack: torch.Tensor,
    last: torch.Tensor,
    addend: torch.Tensor | None,
    queries: torch.Tensor,
    norm_weight: torch.Tensor | None,
    norm_eps: float,
    normalize_values: bool,
    eps: float = 1e-6,
) -> torch.Tensor:
    """Mix `[*stack, last (+ addend)]` with each pseudo-query.

    `queries` are fp32 `(Q, D)`, already scaled by `D**-0.5`; each fp32
    mix is RMS-normalized with its row of `norm_weight` `(Q, D)`. Returns
    `(Q, T, D)` in `last.dtype`, or the fp32 mixes without `norm_weight`.
    """
    num_tokens, d = last.shape
    num_stack = stack.shape[0]
    num_q = queries.shape[0]
    assert last.is_contiguous() and queries.is_contiguous()
    assert queries.dtype == torch.float32
    assert stack.stride(1) == d and stack.stride(2) == 1
    if addend is not None:
        assert addend.is_contiguous() and addend.shape == last.shape
    apply_norm = norm_weight is not None
    if norm_weight is None:
        norm_weight = queries  # unread
    assert norm_weight.shape == queries.shape and norm_weight.is_contiguous()
    out_dtype = last.dtype if apply_norm else torch.float32
    out = torch.empty((num_q, num_tokens, d), dtype=out_dtype, device=last.device)
    args = (
        stack,
        last,
        addend,
        queries,
        norm_weight,
        out,
        stack.stride(0),
        num_tokens,
        eps,
        norm_eps,
    )
    meta = dict(
        D=d,
        N_STACK=num_stack,
        HAS_ADDEND=addend is not None,
        NORMALIZE=normalize_values,
        APPLY_NORM=apply_norm,
        BLOCK_N=triton.next_power_of_2(num_stack + 1),
        enable_fp_fusion=False,
        launch_pdl=True,
    )
    # The queries of a token run back to back and share its sources in L2.
    grid = (num_q, num_tokens)
    if num_tokens * num_q <= 256:
        block_a = triton.next_power_of_2(d + 1) // 2
        block_b = max(triton.next_power_of_2(d - block_a), 16)
        _attn_res_row_kernel[grid](
            *args, **meta, BLOCK_A=block_a, BLOCK_B=block_b, num_warps=8
        )
    else:
        _attn_res_kernel[grid](*args, **meta, BLOCK_D=1024, num_warps=4)
    return out