Linear Attention 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(...).
DeltaNet¶
tileops.linear_attention.DeltaNetChunkFwdOp
¶
DeltaNet forward operator (ungated).
Pipeline: prepare_wy_repr(k, beta) -> (Aw, Au) -> deltanet_fwd(q, k, v, beta, Aw, Au) -> o.
Layout: BHSD (batch, head, seq_len, dim).
Layout convention difference with FLA
TileOPs uses BHSD layout: q/k [B, H, S, DK], v [B, H, S, DV],
beta [B, H, S].
FLA (fla.ops.delta_rule.chunk_delta_rule) uses BTHN
layout: q/k [B, T, H, K], v [B, T, H, V], beta [B, T, H].
__init__
¶
Build the op. Shapes and dtype are taken from each call.
Parameters:
-
chunk_size(int, default:64) –Chunk size for chunked linear attention.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run deltanet forward.
Parameters:
-
q(Tensor) –Query tensor [B, H, S, DK].
-
k(Tensor) –Key tensor [B, H, S, DK].
-
v(Tensor) –Value tensor [B, H, S, DV].
-
beta(Tensor) –Beta tensor [B, H, S].
Returns:
-
tuple[Tensor, ...]–Tuple of (o, h, Aw, Au, w, u).
tileops.linear_attention.DeltaNetChunkBwdOp
¶
DeltaNet backward operator (ungated).
Pipeline: prepare_wy_repr -> fwd (to get Aw, Au) -> bwd kernel -> (dq, dk, dv, dbeta).
__init__
¶
Build the op. Shapes and dtype are taken from each call.
Parameters:
-
chunk_size(int, default:64) –Chunk size for chunked linear attention.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run deltanet backward.
Parameters:
-
do(Tensor) –Gradient of output [B, H, S, DV].
-
q(Tensor) –Query tensor [B, H, S, DK].
-
k(Tensor) –Key tensor [B, H, S, DK].
-
v(Tensor) –Value tensor [B, H, S, DV].
-
beta(Tensor) –Beta tensor [B, H, S].
-
h(Tensor) –Per-chunk boundary states from forward [B, H, NC+1, DK, DV].
-
Aw(Tensor) –A_inv matrix from forward [B, H, S, BC].
-
Au(Tensor) –A_inv matrix from forward [B, H, S, BC].
-
w(Tensor) –WY w vectors from forward [B, H, S, DK].
-
u(Tensor) –WY u vectors from forward [B, H, S, DV].
Returns:
-
tuple[Tensor, Tensor, Tensor, Tensor]–Tuple of (dq, dk, dv, dbeta).
tileops.linear_attention.DeltaNetRecurrentFwdOp
¶
DeltaNet decode (single-step recurrence, ungated).
Computes one step of the delta rule (no gate): v_new = beta * (v - S @ k) o = S @ q + (q . k) * v_new S_new = S + outer(k, v_new)
Layout: BHD (batch, head, dim). Supports float32, float16, and bfloat16 with fp32 accumulation. The read-only tensor inputs may be non-contiguous.
For fp32 dtype, dispatches to a dedicated FP32 kernel that uses element-wise matvec instead of T.gemm to avoid TF32 mantissa truncation.
__init__
¶
Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.
It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.
Parameters:
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
Raises:
-
ValueError–A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.
-
TypeError–An implementation breaks its kernel interface's contract.
forward
¶
Run one decode step.
Parameters:
-
q(Tensor) –Query [B, H, DK].
-
k(Tensor) –Key [B, H, DK].
-
v(Tensor) –Value [B, H, DV].
-
beta(Tensor) –Delta-rule step size [B, H].
-
state(Tensor) –Recurrent state [B, H, DK, DV].
Returns:
-
tuple[Tensor, Tensor]–o[B, H, DV] andnew_state[B, H, DK, DV].
tileops.linear_attention.DeltaNetFwdOp
¶
Inference forward for the ungated delta rule.
The input layout is [B, T, H, D]. One call covers equal-length
prefill, packed-varlen prefill, and single-token decode. The recurrent
state is FP32 and belongs to the caller: initial_state is optional,
while (o, final_state) is always returned.
The in-tree implementations serve a 64- or 128-wide square state in
float16 or bfloat16, with an even or single head count, and with or
without use_qk_l2norm_in_kernel. Prefill runs on SM89 and SM90,
equal-length or packed, with a sequence that is not a whole number of
64-token chunks. On SM89, shared memory holds the offsets of at most 703
packed sequences over a 128-wide state. Decode runs one token per
sequence on SM80, SM89 and SM90, and does not take cu_seqlens. A key
width that is neither 64 nor 128, or one that differs from the value
width, has no in-tree kernel and needs an external target implementation.
beta contains the already-transformed update strength. This Op does
not apply a sigmoid or another beta transform.
__init__
¶
Configure the attention scale, Q/K normalization, and target.
Parameters:
-
scale(float | None, default:None) –Query scale, or
NoneforK**-0.5(K the key head dim). -
use_qk_l2norm_in_kernel(bool, default:False) –Normalize Q and K internally.
-
validate_inputs(bool, default:False) –Check on the CPU that
cu_seqlens(andcu_seqlens_cpu) start at 0, never decrease and end atS. It synchronizes the device; leave it off under CUDA Graph capture. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run prefill or decode and return (o, final_state).
Parameters:
-
q(Tensor) –Queries, \([B \times S \times H \times K]\),
float16orbfloat16. -
k(Tensor) –Keys, \([B \times S \times H \times K]\), same dtype as
q. -
v(Tensor) –Values, \([B \times S \times H \times V]\), same dtype as
q. -
beta(Tensor) –Update strength, already transformed, \([B \times S \times H]\), same dtype as
q. -
initial_state(Tensor | None, default:None) –float32starting state, \([N \times H \times K \times V]\), orNone. -
cu_seqlens(Tensor | None, default:None) –int64offsets of the packed sequences, \([N + 1]\), orNonefor equal-length input. Requires \(B = 1\). -
cu_seqlens_cpu(Tensor | None, default:None) –The same offsets on the CPU, or
None. Requirescu_seqlens.
Returns:
-
Tensor–(o, final_state).ois the output, \([B \times S \times H \times V]\), in -
Tensor–the dtype of
q.final_stateis thefloat32state after the last -
tuple[Tensor, Tensor]–token, \([N \times H \times K \times V]\). \(N = B\) unless
cu_seqlensis given.
Gated DeltaNet¶
tileops.linear_attention.GDNFwdOp
¶
Gated DeltaNet (GDN) inference forward; updates a state pool in place given slot indices.
q and k use [B, T, H, K]. v, g, beta and the
output use HV recurrent heads, where HV is a multiple of H.
The recurrent state is always FP32. It is key-major [N, HV, K, V] by
default and value-major [N, HV, V, K] when state_v_first=True.
For equal-length inputs, N == B. Passing cu_seqlens selects packed
varlen mode: the token tensors have B == 1 and N is the number of
packed sequences.
By default, g contains the precomputed log-space decay and beta
contains the post-sigmoid update strength. use_gate_in_kernel=True
instead treats g as the raw gate and requires A_log and
dt_bias; use_beta_sigmoid_in_kernel=True treats beta as raw
logits. With allow_neg_eigval=True, the beta transform is
2 * sigmoid(beta) rather than sigmoid(beta).
One interface covers prefill and decode, and always returns
(o, final_state). This intentionally pins FLA's
output_final_state=True because inference callers need the state to
continue recurrence. The target callable selects the execution path from
the current inputs; in particular, T == 1 is decode rather than a
separate public Op.
The in-tree kernels serve a 64- or 128-wide square state in either
layout, with HV a multiple of H, an even or single head count, and
any combination of the three input transforms. Prefill runs on SM89 and
SM90, equal-length or packed, with a sequence that is not a whole number
of 64-token chunks. On SM89, shared memory holds the offsets of at most
703 packed sequences over a 128-wide state. Decode runs on SM80, SM89 and
SM90: one token per sequence, which takes no cu_seqlens, or, with
num_accepted_tokens, each sequence's tokens, equal-length or packed,
one after another on the state it keeps in registers. A state pool
(state_indices) is served by decode and by SM90 prefill, one state a
sequence; with num_accepted_tokens, decode keeps one a token. A
packed sequence of more tokens than state_indices holds slots breaks
the call's contract, which no check enforces; the in-tree kernel may then
write states outside that sequence's slots and leaves its outputs
undefined. Other calls need an external target implementation.
__init__
¶
__init__(
scale=None,
use_qk_l2norm_in_kernel=False,
use_beta_sigmoid_in_kernel=False,
allow_neg_eigval=False,
state_v_first=False,
use_gate_in_kernel=False,
*,
validate_inputs=False,
target=None
)
Fix recurrence semantics; tensor metadata comes from each call.
Parameters:
-
scale(float | None, default:None) –Query scale, or
NoneforK**-0.5(K the key head dim). -
use_qk_l2norm_in_kernel(bool, default:False) –Normalize Q and K internally.
-
use_beta_sigmoid_in_kernel(bool, default:False) –Treat
betaas raw logits and apply sigmoid internally. -
allow_neg_eigval(bool, default:False) –Compute
2 * sigmoid(beta)instead ofsigmoid(beta). Valid only withuse_beta_sigmoid_in_kernel=True. -
state_v_first(bool, default:False) –Use value-major recurrent state layout
[N, HV, V, K]instead of key-major[N, HV, K, V]. -
use_gate_in_kernel(bool, default:False) –Treat
gas a raw gate and internally compute-exp(A_log) * softplus(g + dt_bias). Otherwise,gmust already contain the log-space decay. -
validate_inputs(bool, default:False) –Check on the CPU that
cu_seqlens(andcu_seqlens_cpu) start at 0, never decrease, end atSand, withnum_accepted_tokens, hold no sequence longer than astate_indicesrow, and thatstate_indicesandnum_accepted_tokenslie in their manifest ranges. It synchronizes the device; leave it off under CUDA Graph capture. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
forward(
q,
k,
v,
g,
beta,
initial_state=None,
cu_seqlens=None,
cu_seqlens_cpu=None,
A_log=None,
dt_bias=None,
*,
state_indices=None,
num_accepted_tokens=None
)
Run prefill or decode and return (o, final_state).
q, k, v, g and beta may be token-strided, as the columns of a fused
projection are. SM90 prefill reads them in place when each token's row is contiguous
and the tokens lie a whole number of 16-byte vectors apart, at least a row; every other
call and layout reads packed copies.
Parameters:
-
q(Tensor) –Queries, \([B \times S \times H \times K]\),
float16orbfloat16. -
k(Tensor) –Keys, \([B \times S \times H \times K]\), same dtype as
q. -
v(Tensor) –Values, \([B \times S \times \mathit{HV} \times V]\), same dtype as
q. -
g(Tensor) –Log-space decay, or the raw gate under
use_gate_in_kernel, \([B \times S \times \mathit{HV}]\), same dtype asq. -
beta(Tensor) –Post-sigmoid update strength, or raw logits under
use_beta_sigmoid_in_kernel, \([B \times S \times \mathit{HV}]\), same dtype asq. -
initial_state(Tensor | None, default:None) –float32starting state, orNone: \([N \times \mathit{HV} \times K \times V]\), or \([N \times \mathit{HV} \times V \times K]\) understate_v_first. -
cu_seqlens(Tensor | None, default:None) –int64offsets of the packed sequences, \([N + 1]\), orNonefor equal-length input. Requires \(B = 1\). -
cu_seqlens_cpu(Tensor | None, default:None) –The same offsets on the CPU, or
None. Requirescu_seqlens. -
A_log(Tensor | None, default:None) –float32decay parameter, \([\mathit{HV}]\). Required underuse_gate_in_kernel. -
dt_bias(Tensor | None, default:None) –float32gate bias, \([\mathit{HV}]\). Required underuse_gate_in_kernel. -
state_indices(Tensor | None, default:None) –int32slots of a state pool, orNone. With it,initial_stateis the pool, \([P \times \mathit{HV} \times \ldots]\), updated in place: \([N]\), the slot each sequence starts from and ends in; withnum_accepted_tokens, \([N \times L]\), slotttaking the state after tokent. A slot of -1 is never written; a sequence starting from it has unspecified outputs and leaves unspecified states in its other slots. -
num_accepted_tokens(Tensor | None, default:None) –int32\([N]\), orNone: sequencenstarts from its slotnum_accepted_tokens[n] - 1.
Returns:
-
Tensor–(o, final_state).ois the output, -
Tensor–\([B \times S \times \mathit{HV} \times V]\), in the dtype of
q. -
tuple[Tensor, Tensor]–final_stateis thefloat32state after the last token, in the layout -
tuple[Tensor, Tensor]–initial_statetakes, or the pool itself withstate_indices. -
tuple[Tensor, Tensor]–\(N = B\) unless
cu_seqlensis given.
Kimi Delta Attention¶
tileops.linear_attention.KDAFwdOp
¶
Kimi Delta Attention (KDA) forward; updates a state pool in place given slot indices.
q and k use [B, T, H, K]. v, beta and the output use
HV recurrent heads, where HV is a multiple of H; g carries
one log-space decay per key channel, [B, T, HV, K], which is what
separates this operator from the scalar per-head gate of
GDNFwdOp. The recurrent state is always FP32 and key-major
[N, HV, K, V]; state_v_first=True asks for the value-major layout.
For equal-length inputs N == B. Passing cu_seqlens selects packed
varlen mode: the token tensors have B == 1 and N is the number of
packed sequences.
By default g contains the precomputed log-space decay and beta the
post-sigmoid update strength. A precomputed g is a decay, g <= 0, as both
raw-gate forms below produce; a positive value grows the state instead and is
unsupported. use_gate_in_kernel=True instead treats
g as the raw gate and requires A_log;
use_beta_sigmoid_in_kernel=True treats beta as raw logits, and with
allow_neg_eigval=True the beta transform is 2 * sigmoid(beta).
One interface covers equal-length prefill, packed varlen and single-token
decode, and always returns (o, final_state): an inference caller needs
the state to continue the recurrence. The target callable selects the
execution path from the current inputs, so T == 1 is decode rather than
a separate public Op.
The in-tree implementations cover SM90 prefill and decode over a 64- or
128-wide square state with precomputed beta and either precomputed or raw gates.
Raw gates use -exp(A_log) * softplus(g + dt_bias) without a lower bound,
or lower_bound * sigmoid(exp(A_log) * (g + dt_bias)) with one; an omitted
dt_bias is zero. A value-major state and a state pool (state_indices)
are served by decode and prefill. Raw beta,
num_accepted_tokens, other state widths and the other calls still require
an external target implementation.
__init__
¶
__init__(
scale=None,
use_qk_l2norm_in_kernel=False,
use_beta_sigmoid_in_kernel=False,
allow_neg_eigval=False,
state_v_first=False,
use_gate_in_kernel=False,
lower_bound=None,
*,
validate_inputs=False,
target=None
)
Fix recurrence semantics; tensor metadata comes from each call.
Parameters:
-
scale(float | None, default:None) –Query scale, or
NoneforK**-0.5(K the key head dim). -
use_qk_l2norm_in_kernel(bool, default:False) –Normalize Q and K internally.
-
use_beta_sigmoid_in_kernel(bool, default:False) –Treat
betaas raw logits and apply sigmoid internally. -
allow_neg_eigval(bool, default:False) –Compute
2 * sigmoid(beta)instead ofsigmoid(beta). Valid only withuse_beta_sigmoid_in_kernel=True. -
state_v_first(bool, default:False) –Use value-major recurrent state layout
[N, HV, V, K]instead of key-major[N, HV, K, V]. -
use_gate_in_kernel(bool, default:False) –Treat
gas a raw gate and internally compute the log-space decay fromA_loganddt_bias. Otherwisegmust already contain it. -
lower_bound(float | None, default:None) –Lower bound of the forget gate in log space, or
None. Only used withuse_gate_in_kernel=True. -
validate_inputs(bool, default:False) –Check on the CPU that
cu_seqlens(andcu_seqlens_cpu) start at 0, never decrease, end atSand, withnum_accepted_tokens, hold no sequence longer than astate_indicesrow, and thatstate_indicesandnum_accepted_tokenslie in their manifest ranges. It synchronizes the device; leave it off under CUDA Graph capture. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
forward(
q,
k,
v,
g,
beta,
initial_state=None,
cu_seqlens=None,
cu_seqlens_cpu=None,
A_log=None,
dt_bias=None,
*,
state_indices=None,
num_accepted_tokens=None
)
Run prefill or decode and return (o, final_state).
Parameters:
-
q(Tensor) –Queries, \([B \times S \times H \times K]\),
float16orbfloat16. -
k(Tensor) –Keys, \([B \times S \times H \times K]\), same dtype as
q. -
v(Tensor) –Values, \([B \times S \times \mathit{HV} \times V]\), same dtype as
q. -
g(Tensor) –Per-key-channel log-space decay, or the raw gate under
use_gate_in_kernel, \([B \times S \times \mathit{HV} \times K]\), same dtype asq. -
beta(Tensor) –Post-sigmoid update strength, or raw logits under
use_beta_sigmoid_in_kernel, \([B \times S \times \mathit{HV}]\), same dtype asq. -
initial_state(Tensor | None, default:None) –float32starting state, orNone: \([N \times \mathit{HV} \times K \times V]\), or \([N \times \mathit{HV} \times V \times K]\) understate_v_first. -
cu_seqlens(Tensor | None, default:None) –int64offsets of the packed sequences, \([N + 1]\), orNonefor equal-length input. Requires \(B = 1\). -
cu_seqlens_cpu(Tensor | None, default:None) –The same offsets on the CPU, or
None. Requirescu_seqlens. -
A_log(Tensor | None, default:None) –float32decay parameter, \([\mathit{HV}]\). Required underuse_gate_in_kernel. -
dt_bias(Tensor | None, default:None) –float32gate bias, \([\mathit{HV} \cdot K]\), orNonefor zero. Only used underuse_gate_in_kernel. -
state_indices(Tensor | None, default:None) –int32slots of a state pool, orNone. With it,initial_stateis the pool, \([P \times \mathit{HV} \times \ldots]\), updated in place: \([N]\), the slot each sequence starts from and ends in; withnum_accepted_tokens, \([N \times L]\), slotttaking the state after tokent. A slot of -1 is never written; a sequence starting from it has unspecified outputs and leaves unspecified states in its other slots. -
num_accepted_tokens(Tensor | None, default:None) –int32\([N]\), orNone: sequencenstarts from its slotnum_accepted_tokens[n] - 1.
Returns:
-
Tensor–(o, final_state).ois the output, -
Tensor–\([B \times S \times \mathit{HV} \times V]\), in the dtype of
q. -
tuple[Tensor, Tensor]–final_stateis thefloat32state after the last token, in the layout -
tuple[Tensor, Tensor]–initial_statetakes, or the pool itself withstate_indices. -
tuple[Tensor, Tensor]–\(N = B\) unless
cu_seqlensis given.
Gated Linear Attention¶
tileops.linear_attention.GLAChunkFwdOp
¶
Gated Linear Attention (GLA) forward operator.
Chunked GLA forward: (q, k, v, g) -> (o, final_state, h). h holds the states
GLAChunkBwdOp reads, so a forward call's outputs feed the backward directly.
Layout: BTHD (batch, seq_len, heads, dim). The read-only tensor inputs may be non-contiguous.
__init__
¶
Build the op. Shapes and dtype are taken from each call.
Parameters:
-
chunk_size(int, default:64) –Chunk size for chunked linear attention.
-
scale(float | None, default:None) –Positive query scale, or
NoneforK ** -0.5withKthe key head dim. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run GLA forward.
Parameters:
-
q(Tensor) –Query tensor [B, T, H, K].
-
k(Tensor) –Key tensor [B, T, H, K].
-
v(Tensor) –Value tensor [B, T, H, V].
-
g(Tensor) –Log-space forget gates [B, T, H, K].
-
initial_state(Tensor | None, default:None) –Optional fp32 initial hidden state [B, H, K, V]; absent starts the recurrence from zeros.
Returns:
-
Tensor–(o, final_state, h):o[B, T, H, V] in the input dtype; the fp32 -
Tensor–final_state[B, H, K, V]; the fp32h[B, T // chunk_size + 1, H, K, V], -
Tensor–the state entering each chunk followed by the final state, which is what
-
tuple[Tensor, Tensor, Tensor]–GLAChunkBwdOp takes as
h.
tileops.linear_attention.GLAChunkBwdOp
¶
Gated Linear Attention (GLA) backward operator.
Computes gradients (dq, dk, dv, dg, dinitial_state) given output gradient do.
Uses the h GLAChunkFwdOp returns (no recomputation needed).
Layout: BTHD (batch, seq_len, heads, dim). The read-only tensor inputs may be non-contiguous.
__init__
¶
Build the op. Shapes and dtype are taken from each call.
Parameters:
-
chunk_size(int, default:64) –Chunk size for chunked linear attention.
-
scale(float | None, default:None) –Positive query scale, or
NoneforK ** -0.5withKthe key head dim. -
has_initial_state(bool, default:False) –Whether the forward this backward pairs with was given an initial state; then the call also returns its gradient.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run GLA backward.
Parameters:
-
do(Tensor) –Output gradient [B, T, H, V].
-
dfinal_state(Tensor) –Final-state gradient [B, H, K, V].
-
q(Tensor) –Queries [B, T, H, K].
-
k(Tensor) –Keys [B, T, H, K].
-
v(Tensor) –Values [B, T, H, V].
-
g(Tensor) –Log-space forget gates [B, T, H, K].
-
h(Tensor) –The
hGLAChunkFwdOp returns [B, T // chunk_size + 1, H, K, V] (fp32).
Returns:
-
Tensor–(dq, dk, dv, dg, dinitial_state), all fp32: the first four shaped like -
Tensor–q,k,vandg;dinitial_state[B, H, K, V] is the gradient -
Tensor–of the forward's
initial_statewithhas_initial_state, elseNone.
tileops.linear_attention.GLARecurrentFwdOp
¶
Gated Linear Attention (GLA) decode (single-step recurrence).
Computes one step of the gated linear attention recurrence
S_new = diag(exp(g)) @ S + outer(k, v) o = scale * q^T @ S_new
Layout: BHD (batch, head, dim). Supports float32, float16, and bfloat16 with fp32 accumulation. The read-only tensor inputs may be non-contiguous.
An fp32 call whose key dimension is a multiple of 16 runs the fp32 kernel, one block a
head that streams the state in key tiles; other calls run one block a
(batch, head, value slice). Neither uses tensor cores.
__init__
¶
Build the op. Shapes and dtype are taken from each call.
Parameters:
-
scale(float | None, default:None) –Positive query scale, or
NoneforDK ** -0.5(key head dim). -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run one decode step.
Parameters:
-
q(Tensor) –Query [B, H, DK].
-
k(Tensor) –Key [B, H, DK].
-
v(Tensor) –Value [B, H, DV].
-
g(Tensor) –Log-space key gate [B, H, DK].
-
state(Tensor) –Recurrent state [B, H, DK, DV].
Returns:
-
tuple[Tensor, Tensor]–o[B, H, DV] andnew_state[B, H, DK, DV].
tileops.linear_attention.GLAFwdOp
¶
Gated Linear Attention (GLA) for inference, with caller-owned FP32 state.
Q, K, V and the log-space, per-key gate G use FP16/BF16 BTHD layout. One call is
equal-length prefill, packed-varlen prefill, or single-token decode. An absent
initial_state starts from zero; every call returns (o, final_state).
__init__
¶
Fix the query scale and optional backend target for this instance.
Parameters:
-
scale(float | None, default:None) –Positive query scale, or
NoneforK**-0.5(K the key head dim). -
validate_inputs(bool, default:False) –Check on the CPU that
cu_seqlens(andcu_seqlens_cpu) start at 0, never decrease and end atS. It synchronizes the device; leave it off under CUDA Graph capture. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run GLA prefill or decode and return output plus final FP32 state.
Parameters:
-
q(Tensor) –Queries, \([B \times S \times H \times K]\),
float16orbfloat16. -
k(Tensor) –Keys, \([B \times S \times H \times K]\), same dtype as
q. -
v(Tensor) –Values, \([B \times S \times H \times V]\), same dtype as
q. -
g(Tensor) –Log-space per-key gate, \([B \times S \times H \times K]\), same dtype as
q. -
initial_state(Tensor | None, default:None) –float32starting state, \([N \times H \times K \times V]\), orNoneto start from zero. -
cu_seqlens(Tensor | None, default:None) –int64offsets of the packed sequences, \([N + 1]\), orNonefor equal-length input. Requires \(B = 1\). -
cu_seqlens_cpu(Tensor | None, default:None) –The same offsets on the CPU, or
None. Requirescu_seqlens.
Returns:
-
Tensor–(o, final_state).ois the output, \([B \times S \times H \times V]\), in -
Tensor–the dtype of
q.final_stateis thefloat32state after the last -
tuple[Tensor, Tensor]–token, \([N \times H \times K \times V]\). \(N = B\) unless
cu_seqlensis given.