Skip to content

vllm.models.hy_v4_flash.common.engram_ops

Fused Triton kernels for the HY V4 Flash Engram layer.

Functions:

  • gated_update –

    Apply the gated Engram update to the hyper-connection streams.

  • lookup_norm –

    Gather, dequantize and RMS-normalize Engram table rows per head.

  • ngram_hashes –

    Write the global Engram buckets of the n-grams ending at each token.

gated_update(hidden, proj, hashes, key_norm, query_norm, pre_gamma, hc_scales)

Apply the gated Engram update to the hyper-connection streams.

Parameters:

  • hidden

    (Tensor) –

    [T, hc, dim] streams.

  • proj

    (Tensor) –

    [T, (hc + 1) * dim] reduced key projections followed by the value projection.

  • hashes

    (Tensor) –

    [T, n_hash_cols]; tokens whose hashes are all negative pass through unchanged.

  • key_norm

    (Tensor) –

    [hc, dim] key RMSNorm weights.

  • query_norm

    (Tensor) –

    [hc, dim] query RMSNorm weights.

  • pre_gamma

    (Tensor) –

    [dim] value RMSNorm weight.

  • hc_scales

    (Tensor) –

    [hc, dim] per-stream update scales.

Returns:

  • Tensor –

    [T, hc, dim] updated streams.

Source code in vllm/models/hy_v4_flash/common/engram_ops.py
def gated_update(
    hidden: torch.Tensor,
    proj: torch.Tensor,
    hashes: torch.Tensor,
    key_norm: torch.Tensor,
    query_norm: torch.Tensor,
    pre_gamma: torch.Tensor,
    hc_scales: torch.Tensor,
) -> torch.Tensor:
    """Apply the gated Engram update to the hyper-connection streams.

    Args:
        hidden: `[T, hc, dim]` streams.
        proj: `[T, (hc + 1) * dim]` reduced key projections followed by the
            value projection.
        hashes: `[T, n_hash_cols]`; tokens whose hashes are all negative pass
            through unchanged.
        key_norm: `[hc, dim]` key RMSNorm weights.
        query_norm: `[hc, dim]` query RMSNorm weights.
        pre_gamma: `[dim]` value RMSNorm weight.
        hc_scales: `[hc, dim]` per-stream update scales.

    Returns:
        `[T, hc, dim]` updated streams.

    """
    num_tokens, hc_mult, dim = hidden.shape
    assert hidden.stride(2) == 1 and proj.stride(1) == 1 and hashes.stride(1) == 1
    assert proj.shape == (num_tokens, (hc_mult + 1) * dim)
    out = torch.empty_like(hidden)
    if num_tokens == 0:
        return out
    block = triton.next_power_of_2(dim)
    _gated_update_kernel[(num_tokens * hc_mult,)](
        hidden,
        proj,
        hashes,
        key_norm,
        query_norm,
        pre_gamma,
        hc_scales,
        out,
        hidden.stride(0),
        hidden.stride(1),
        proj.stride(0),
        hashes.stride(0),
        out.stride(0),
        out.stride(1),
        hashes.shape[1],
        torch.finfo(torch.float32).eps,
        DIM=dim,
        HC_MULT=hc_mult,
        BLOCK_D=block,
        BLOCK_C=triton.next_power_of_2(hashes.shape[1]),
        num_warps=8 if block >= 2048 else 4,
    )
    return out

lookup_norm(table, scales, hashes, vocab_start, vocab_end, gamma, dtype)

Gather, dequantize and RMS-normalize Engram table rows per head.

Parameters:

  • table

    (Tensor) –

    [vocab_end - vocab_start, dim] local table rows (FP8 or BF16), possibly a CUDA view of pinned host memory.

  • scales

    (Tensor | None) –

    [vocab_end - vocab_start] FP8 row scales, or None for BF16 tables.

  • hashes

    (Tensor) –

    [T, heads] global buckets; a bucket outside [vocab_start, vocab_end), including a negative one, zeroes its head.

  • vocab_start

    (int) –

    First global bucket of table.

  • vocab_end

    (int) –

    End of the global buckets of table.

  • gamma

    (Tensor) –

    [heads, dim] per-head norm weights.

  • dtype

    (dtype) –

    output dtype.

Returns:

  • Tensor –

    [T, heads * dim] normalized rows.

Source code in vllm/models/hy_v4_flash/common/engram_ops.py
def lookup_norm(
    table: torch.Tensor,
    scales: torch.Tensor | None,
    hashes: torch.Tensor,
    vocab_start: int,
    vocab_end: int,
    gamma: torch.Tensor,
    dtype: torch.dtype,
) -> torch.Tensor:
    """Gather, dequantize and RMS-normalize Engram table rows per head.

    Args:
        table: `[vocab_end - vocab_start, dim]` local table rows (FP8 or
            BF16), possibly a CUDA view of pinned host memory.
        scales: `[vocab_end - vocab_start]` FP8 row scales, or None for BF16
            tables.
        hashes: `[T, heads]` global buckets; a bucket outside
            `[vocab_start, vocab_end)`, including a negative one, zeroes its
            head.
        vocab_start: First global bucket of `table`.
        vocab_end: End of the global buckets of `table`.
        gamma: `[heads, dim]` per-head norm weights.
        dtype: output dtype.

    Returns:
        `[T, heads * dim]` normalized rows.

    """
    num_tokens, heads = hashes.shape
    dim = table.shape[1]
    assert table.is_contiguous() and gamma.shape == (heads, dim)
    assert gamma.is_contiguous() and hashes.stride(1) == 1
    out = torch.empty(num_tokens, heads * dim, dtype=dtype, device=hashes.device)
    if num_tokens == 0 or heads == 0:
        return out.zero_()
    _lookup_norm_kernel[(num_tokens,)](
        table,
        table if scales is None else scales,
        hashes,
        gamma,
        out,
        hashes.stride(0),
        vocab_start,
        vocab_end,
        torch.finfo(torch.float32).eps,
        HEADS=heads,
        DIM=dim,
        BLOCK_H=triton.next_power_of_2(heads),
        BLOCK_D=triton.next_power_of_2(dim),
        HAS_SCALES=scales is not None,
        num_warps=4,
    )
    return out

ngram_hashes(input_ids, lookback, starts, seq_lens, num_live, token_map, multipliers, primes, offsets, pad_id, skip_ids, out)

Write the global Engram buckets of the n-grams ending at each token.

Parameters:

  • input_ids

    (Tensor) –

    [T] token ids of the batch.

  • lookback

    (Tensor) –

    [requests, depth] ids preceding each request's chunk, nearest first; negative ids are unknown.

  • starts

    (Tensor) –

    [requests + 1] query start offsets.

  • seq_lens

    (Tensor) –

    [requests] sequence lengths including the chunk.

  • num_live

    (int) –

    Rows at or past this count are padding.

  • token_map

    (Tensor) –

    [vocab] compressed id of each token.

  • multipliers

    (Tensor) –

    [layers, max_ngram] per-layer hash multipliers.

  • primes

    (Tensor) –

    [layers, columns] bucket count of each hash column.

  • offsets

    (Tensor) –

    [layers, columns] first global bucket of each column.

  • pad_id

    (int) –

    Compressed id standing in for missing history.

  • skip_ids

    (tuple[int, ...]) –

    Up to three token ids (pad and visual tokens) whose rows get no buckets.

  • out

    (Tensor) –

    [layers, T, columns] INT32 buckets; skipped rows are -1.

Source code in vllm/models/hy_v4_flash/common/engram_ops.py
def ngram_hashes(
    input_ids: torch.Tensor,
    lookback: torch.Tensor,
    starts: torch.Tensor,
    seq_lens: torch.Tensor,
    num_live: int,
    token_map: torch.Tensor,
    multipliers: torch.Tensor,
    primes: torch.Tensor,
    offsets: torch.Tensor,
    pad_id: int,
    skip_ids: tuple[int, ...],
    out: torch.Tensor,
) -> None:
    """Write the global Engram buckets of the n-grams ending at each token.

    Args:
        input_ids: `[T]` token ids of the batch.
        lookback: `[requests, depth]` ids preceding each request's chunk,
            nearest first; negative ids are unknown.
        starts: `[requests + 1]` query start offsets.
        seq_lens: `[requests]` sequence lengths including the chunk.
        num_live: Rows at or past this count are padding.
        token_map: `[vocab]` compressed id of each token.
        multipliers: `[layers, max_ngram]` per-layer hash multipliers.
        primes: `[layers, columns]` bucket count of each hash column.
        offsets: `[layers, columns]` first global bucket of each column.
        pad_id: Compressed id standing in for missing history.
        skip_ids: Up to three token ids (pad and visual tokens) whose rows
            get no buckets.
        out: `[layers, T, columns]` INT32 buckets; skipped rows are -1.

    """
    num_tokens = input_ids.numel()
    layers, max_ngram = multipliers.shape
    heads = primes.shape[1] // (max_ngram - 1)
    assert out.shape == (layers, num_tokens, primes.shape[1]) and out.is_contiguous()
    assert input_ids.is_contiguous() and len(skip_ids) <= 3
    if num_tokens == 0:
        return
    skip0, skip1, skip2 = (*skip_ids, -1, -1, -1)[:3]
    block = 32
    _ngram_hash_kernel[(triton.cdiv(num_tokens, block), layers)](
        input_ids,
        token_map,
        lookback,
        starts,
        seq_lens,
        multipliers,
        primes,
        offsets,
        out,
        num_tokens,
        min(num_live, num_tokens),
        starts.numel() - 1,
        token_map.numel(),
        pad_id,
        skip0,
        skip1,
        skip2,
        lookback.shape[1],
        lookback.stride(0),
        lookback.stride(1),
        MAX_NGRAM=max_ngram,
        HEADS=heads,
        BLOCK_T=block,
        BLOCK_H=triton.next_power_of_2(heads),
        num_warps=4,
    )