Skip to content

vllm.models.inkling2.nvidia.ops.dspark

Kernels of the Inkling2 DSpark draft, matching the reference implementation.

Functions:

  • add_softcap –

    bf16 softcap * tanh(bf16(a + b) / softcap) of two bf16 2D tensors.

  • markov_gate –

    The recurrent head's gating of the fp32 rnn_proj output raw

  • mix_rmsnorm –

    RMSNorm of the alpha-weighted sum of targets[:, i] (fp32 math).

  • qk_norm_rope_ –

    In-place per-head Q/K RMSNorm (rounded to bf16) + neox RoPE on the

  • rope_ –

    In-place neox RoPE on x of shape (T, H, D) (fp32 math).

  • silu_and_mul –

    silu(gate) * up in fp32 (precise exp/div), rounded once.

add_softcap(a, b, softcap)

bf16 softcap * tanh(bf16(a + b) / softcap) of two bf16 2D tensors.

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def add_softcap(a: torch.Tensor, b: torch.Tensor, softcap: float) -> torch.Tensor:
    """bf16 `softcap * tanh(bf16(a + b) / softcap)` of two bf16 2D tensors."""
    rows, n = a.shape
    assert a.stride(1) == 1 and b.stride(1) == 1
    out = a.new_empty((rows, n))
    if rows:
        _add_softcap_kernel[(triton.cdiv(n, 1024), rows)](
            a, b, out, a.stride(0), b.stride(0), softcap, n, 1024
        )
    return out

markov_gate(raw, state)

The recurrent head's gating of the fp32 rnn_proj output raw (rounded to bf16 first), as the reference's bf16 torch ops: returns (gate * state + (1 - gate) * tanh(cand), tanh(out)).

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def markov_gate(
    raw: torch.Tensor, state: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
    """The recurrent head's gating of the fp32 `rnn_proj` output `raw`
    (rounded to bf16 first), as the reference's bf16 torch ops: returns
    `(gate * state + (1 - gate) * tanh(cand), tanh(out))`."""
    rows, rank = state.shape
    assert raw.shape == (rows, 3 * rank) and raw.is_contiguous()
    assert state.is_contiguous() and rank & (rank - 1) == 0
    out, latent = torch.empty_like(state), torch.empty_like(state)
    if rows:
        _markov_gate_kernel[(rows,)](raw, state, out, latent, rank)
    return out, latent

mix_rmsnorm(targets, alpha, weight, eps)

RMSNorm of the alpha-weighted sum of targets[:, i] (fp32 math).

targets is (rows, taps, hidden); the output has targets.dtype.

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def mix_rmsnorm(
    targets: torch.Tensor, alpha: torch.Tensor, weight: torch.Tensor, eps: float
) -> torch.Tensor:
    """RMSNorm of the `alpha`-weighted sum of `targets[:, i]` (fp32 math).

    `targets` is `(rows, taps, hidden)`; the output has `targets.dtype`.
    """
    rows, taps, hidden = targets.shape
    assert alpha.shape == (taps,) and alpha.is_contiguous()
    assert weight.shape == (hidden,) and weight.is_contiguous()
    out = targets.new_empty((rows, hidden))
    if rows:
        _mix_rmsnorm_kernel[(rows,)](
            targets,
            alpha,
            weight,
            out,
            *targets.stride(),
            taps,
            hidden,
            eps,
            triton.next_power_of_2(hidden),
            num_warps=8,
        )
    return out

qk_norm_rope_(qkv, positions, q_weight, k_weight, cos_sin_cache, num_q_heads, num_k_heads, eps)

In-place per-head Q/K RMSNorm (rounded to bf16) + neox RoPE on the leading num_q_heads + num_k_heads heads of the fused qkv.

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def qk_norm_rope_(
    qkv: torch.Tensor,
    positions: torch.Tensor,
    q_weight: torch.Tensor,
    k_weight: torch.Tensor,
    cos_sin_cache: torch.Tensor,
    num_q_heads: int,
    num_k_heads: int,
    eps: float,
) -> None:
    """In-place per-head Q/K RMSNorm (rounded to bf16) + neox RoPE on the
    leading `num_q_heads + num_k_heads` heads of the fused `qkv`."""
    head_dim = cos_sin_cache.shape[-1]
    if qkv.shape[0]:
        _qk_norm_rope_kernel[(qkv.shape[0], num_q_heads + num_k_heads)](
            qkv,
            q_weight,
            k_weight,
            cos_sin_cache,
            positions,
            qkv.stride(0),
            num_q_heads * head_dim,
            NHQ=num_q_heads,
            D=head_dim,
            EPS=eps,
            NORM=True,
        )

rope_(x, positions, cos_sin_cache)

In-place neox RoPE on x of shape (T, H, D) (fp32 math).

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def rope_(x: torch.Tensor, positions: torch.Tensor, cos_sin_cache: torch.Tensor):
    """In-place neox RoPE on `x` of shape `(T, H, D)` (fp32 math)."""
    num_tokens, num_heads, head_dim = x.shape
    assert x.stride(1) == head_dim and x.stride(2) == 1
    if num_tokens:
        _qk_norm_rope_kernel[(num_tokens, num_heads)](
            x,
            x,
            x,
            cos_sin_cache,
            positions,
            x.stride(0),
            0,
            NHQ=0,
            D=head_dim,
            EPS=0.0,
            NORM=False,
        )

silu_and_mul(x)

silu(gate) * up in fp32 (precise exp/div), rounded once.

Source code in vllm/models/inkling2/nvidia/ops/dspark.py
def silu_and_mul(x: torch.Tensor) -> torch.Tensor:
    """`silu(gate) * up` in fp32 (precise exp/div), rounded once."""
    num_tokens, width = x.shape
    n = width // 2
    out = x.new_empty((num_tokens, n))
    if num_tokens:
        _silu_and_mul_kernel[(num_tokens, triton.cdiv(n, 1024))](
            x, out, x.stride(0), n, 1024, num_warps=4
        )
    return out