Skip to content

Engram Operators

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

N-gram hash

tileops.sequence_modeling.NGramHashFwdOp

Hash n-grams and update their context in place.

Each head hashes an order from two through the multiplier count, with equal heads per order. Products and sums wrap in int64, and remainders are nonnegative. A previous end-of-sequence (EOS) token blocks older history; the current EOS participates normally. Context stores raw tokens, oldest first.

Distinct context_indices select pool rows. num_accepted_tokens selects the history window starting at accepted minus one, then preserves its last N-2 tokens and every input token for speculative rollback. Without it, only the last N-1 tokens are committed. Empty sequences leave their rows untouched.

__init__

__init__(
    eos_token_id,
    *,
    validate_inputs=False,
    target=None
)

Build the op.

Parameters:

  • eos_token_id (int) –

    Token used at missing history and EOS boundaries.

  • validate_inputs (bool, default: False ) –

    Check packed offsets and metadata bounds on the CPU.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input_ids,
    context,
    multipliers,
    primes,
    offsets,
    cu_seqlens=None,
    context_indices=None,
    num_accepted_tokens=None,
)

Return int64 hashed ids, updating the addressed context rows.

Parameters:

  • input_ids (Tensor) –

    Dense [B, L] or packed [T] int64 tokens.

  • context (Tensor) –

    Int64 context pool [S, W], written in place.

  • multipliers (Tensor) –

    Int64 [N] hash multipliers.

  • primes (Tensor) –

    Positive int64 [heads] divisors.

  • offsets (Tensor) –

    Int64 [heads] embedding offsets.

  • cu_seqlens (Tensor | None, default: None ) –

    Optional int32 [B+1] packed prefix offsets.

  • context_indices (Tensor | None, default: None ) –

    Optional distinct int32 [B] pool row indices.

  • num_accepted_tokens (Tensor | None, default: None ) –

    Optional int32 [B] accepted counts, at least one.

Returns:

  • Tensor –

    Ids shaped [B, L, heads] or [T, heads].

GateConv

tileops.sequence_modeling.EngramGateConvFwdOp

Engram GateConv forward operator (post-projection fusion).

Assumes k = E @ W_K and v = E @ W_V have been computed externally via standard GEMM. This op fuses the remaining memory-bound stages: RMSNorm gating -> causal DWConv1D -> SiLU + residual

Returns Y plus saved intermediates for backward

vhat, alpha, rrms_h, rrms_k, rrms_v

__init__

__init__(
    eps=1e-06,
    *,
    target=None
)

Build the op. The batch size, sequence length, hidden size and dtype come with each call.

Parameters:

  • eps (float, default: 1e-06 ) –

    RMSNorm epsilon (default 1e-6).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    H,
    k,
    v,
    rms_w_h,
    rms_w_v,
    conv_w,
)

Run the fused gate and convolution.

Parameters:

  • H (Tensor) –

    (M, seq_len, d) — hidden states from preceding layers.

  • k (Tensor) –

    (M, seq_len, d) — key projection output (E @ W_K).

  • v (Tensor) –

    (M, seq_len, d) — value projection output (E @ W_V).

  • rms_w_h (Tensor) –

    (d,) — RMSNorm weight for H and k.

  • rms_w_v (Tensor) –

    (d,) — RMSNorm weight for gated value (conv input).

  • conv_w (Tensor) –

    (4, d) — depthwise causal conv1d weights.

Returns:

  • tuple[Tensor, ...] –

    Tuple (Y, vhat, alpha, rrms_h, rrms_k, rrms_v): Y: (M, seq_len, d) — output to add as residual to H. vhat: (M, seq_len, d) — saved for backward. alpha: (M, seq_len) — scalar gate, saved for backward. rrms_h: (M, seq_len) — RMSNorm reciprocal rms of H. rrms_k: (M, seq_len) — RMSNorm reciprocal rms of k. rrms_v: (M, seq_len) — RMSNorm reciprocal rms of v_hat.

tileops.sequence_modeling.EngramGateConvBwdOp

Engram GateConv backward operator.

Given dY and saved intermediates from forward (vhat, alpha, rrms_*), computes gradients for H, k, v, rms_w_h, rms_w_v, conv_w.

The caller is responsible for the projection backward (GEMM): dE = dk @ W_K^T + dv @ W_V^T dW_K = E^T @ dk dW_V = E^T @ dv

__init__

__init__(
    eps=1e-06,
    *,
    target=None
)

Build the op. The batch size, sequence length, hidden size and dtype come with each call.

Parameters:

  • eps (float, default: 1e-06 ) –

    RMSNorm epsilon (default 1e-6).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    dY,
    H,
    k,
    v,
    rms_w_h,
    rms_w_v,
    conv_w,
    vhat,
    alpha,
    rrms_h,
    rrms_k,
    rrms_v,
)

Propagate dY back through the fused gate and convolution.

Parameters:

  • dY (Tensor) –

    (M, T, d) — gradient of output Y.

  • H (Tensor) –

    (M, T, d) — hidden states (forward input).

  • k (Tensor) –

    (M, T, d) — key projection (forward input, or recomputed).

  • v (Tensor) –

    (M, T, d) — value projection (forward input).

  • rms_w_h (Tensor) –

    (d,) — RMSNorm weight for H and k.

  • rms_w_v (Tensor) –

    (d,) — RMSNorm weight for v_hat.

  • conv_w (Tensor) –

    (4, d) — conv weights.

  • vhat (Tensor) –

    (M, T, d) — saved from forward.

  • alpha (Tensor) –

    (M, T) — saved from forward.

  • rrms_h (Tensor) –

    (M, T) — saved from forward.

  • rrms_k (Tensor) –

    (M, T) — saved from forward.

  • rrms_v (Tensor) –

    (M, T) — saved from forward.

Returns:

  • tuple[Tensor, ...] –

    Tuple (dH, dk, dv, drms_w_h, drms_w_v, dconv_w): dH: (M, T, d) dk: (M, T, d) dv: (M, T, d) drms_w_h: (d,) — fp32 drms_w_v: (d,) — fp32 dconv_w: (4, d) — fp32

Decode

tileops.sequence_modeling.EngramDecodeFwdOp

Engram fused decode operator — single-token inference.

Fuses GEMV projection + gating + dilated conv + SiLU. It compiles for max_conv_len and accepts any conv_state length up to it without recompiling.

__init__

__init__(
    max_conv_len,
    conv_kernel_size,
    dilation,
    eps=1e-06,
    *,
    target=None
)

Build the op. The batch size, memory and hidden sizes and dtype come with each call.

Parameters:

  • max_conv_len (int) –

    Max conv cache capacity (compile-time).

  • conv_kernel_size (int) –

    Number of conv taps w (model param, e.g. 4).

  • dilation (int) –

    Dilation factor δ (model param, e.g. max N-gram order).

  • eps (float, default: 1e-06 ) –

    RMSNorm epsilon (default 1e-6).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    e_t,
    h_t,
    conv_state,
    W_K,
    W_V,
    rms_w_h,
    rms_w_v,
    conv_w,
)

Run one decode token through the fused projection, gate and convolution.

Parameters:

  • e_t (Tensor) –

    (B, d_mem) — gathered N-gram embedding for current token.

  • h_t (Tensor) –

    (B, d) — hidden state for current token.

  • conv_state (Tensor) –

    (B, L, d) — conv history, L <= max_conv_len. Left-padded to max_conv_len internally if L < max_conv_len.

  • W_K (Tensor) –

    (d_mem, d) — key projection weight.

  • W_V (Tensor) –

    (d_mem, d) — value projection weight.

  • rms_w_h (Tensor) –

    (d,) — RMSNorm weight for h and k.

  • rms_w_v (Tensor) –

    (d,) — RMSNorm weight for v_hat.

  • conv_w (Tensor) –

    (w, d) — depthwise conv weights (w = conv_kernel_size).

Returns:

  • tuple[Tensor, Tensor] –

    Tuple (y_t, new_conv_state): y_t: (B, d) — output to add as residual. new_conv_state: (B, max_conv_len, d) — updated state for next step.