Class GatedAttention
java.lang.Object
smile.llm.model.qwen.GatedAttention
- All Implemented Interfaces:
Attention
Qwen3.5 gated full attention (GQA + partial RoPE + sigmoid output gate).
The query projection emits twice the head channels; the second half is a
per-token sigmoid gate applied to the attention output before o_proj.
Under tensor parallelism, Q/K/V projections are column-sharded by heads and
o_proj is row-parallel (all-reduce after the projection).
-
Constructor Summary
ConstructorsConstructorDescriptionGatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, int kvLayerId) Constructor.GatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, int kvLayerId, TensorParallelGroup tpGroup, int tpRank) Tensor-parallel constructor.GatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, KvCacheLayout layout) Test helper that allocates a private KV pool. -
Method Summary
Modifier and TypeMethodDescriptionstatic GatedAttentionforShard(int dim, int headDim, int rotaryDim, double normEps, int kvLayerId, TensorShardSpec shard, TensorParallelGroup tpGroup) Builds attention from a shard spec (local head counts).Decode with per-row cache write positions (seqLen == 1), uniform prefill/verify when every row shares the samepositions[0], or a genuinely ragged multi-token verify window (concurrent requests at different absolute positions,seqLen > 1) viaforwardVerifyRagged(Tensor, int[], Tensor, Tensor).Forward pass through the attention module.Forward with HuggingFace-style partial RoPE cos/sin tables.module()Returns pytorch module.voidreplaceProjections(LinearOp q, LinearOp k, LinearOp v, LinearOp o) Replaces Q/K/V/O with quantized ops (already sharded/packed).
-
Constructor Details
-
GatedAttention
public GatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, int kvLayerId) Constructor. Install aKvCachePoolviasetCachePool(KvCachePool)before the firstforward(Tensor, int, Tensor, Tensor).- Parameters:
dim- hidden size.numHeads- query head count (local under TP).numKvHeads- key/value head count (local under TP).headDim- per-head dimension.rotaryDim- partial RoPE dimension.normEps- RMSNorm epsilon.kvLayerId- layer index inside the KV pool.
-
GatedAttention
public GatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, int kvLayerId, TensorParallelGroup tpGroup, int tpRank) Tensor-parallel constructor.- Parameters:
dim- hidden size.numHeads- query head count (local under TP).numKvHeads- key/value head count (local under TP).headDim- per-head dimension.rotaryDim- partial RoPE dimension.normEps- RMSNorm epsilon.kvLayerId- layer index inside the KV pool.tpGroup- tensor-parallel group, ornullfor single-device.tpRank- this rank's TP index.
-
GatedAttention
public GatedAttention(int dim, int numHeads, int numKvHeads, int headDim, int rotaryDim, double normEps, KvCacheLayout layout) Test helper that allocates a private KV pool.- Parameters:
dim- hidden size.numHeads- query head count.numKvHeads- key/value head count.headDim- per-head dimension.rotaryDim- partial RoPE dimension.normEps- RMSNorm epsilon.layout- KV cache layout used to size the private pool.
-
-
Method Details
-
replaceProjections
-
forShard
public static GatedAttention forShard(int dim, int headDim, int rotaryDim, double normEps, int kvLayerId, TensorShardSpec shard, TensorParallelGroup tpGroup) Builds attention from a shard spec (local head counts).- Parameters:
dim- hidden size.headDim- per-head dimension.rotaryDim- partial RoPE dimension.normEps- RMSNorm epsilon.kvLayerId- layer index inside the KV pool.shard- local head / rank shard description.tpGroup- tensor-parallel group.- Returns:
- gated attention for the shard.
-
module
Description copied from interface:AttentionReturns pytorch module. -
forward
Description copied from interface:AttentionForward pass through the attention module. -
forward
Forward with HuggingFace-style partial RoPE cos/sin tables.- Parameters:
x- hidden states[B, S, D].startPos- KV cache write position.cos- cosines for this window[S, rotaryDim].sin- sines for this window[S, rotaryDim].mask- causal attention mask, ornullwhenS == 1.- Returns:
- attention output
[B, S, D].
-
forward
Decode with per-row cache write positions (seqLen == 1), uniform prefill/verify when every row shares the samepositions[0], or a genuinely ragged multi-token verify window (concurrent requests at different absolute positions,seqLen > 1) viaforwardVerifyRagged(Tensor, int[], Tensor, Tensor).- Parameters:
x- hidden states[B, S, D].positions- absolute write position per batch row.cos- cosines[S, R]or per-row[B, S, R].sin- sines with the same layout ascos.mask- causal mask; ignored by the ragged verify path (that kernel's causal masking is unconditional), used otherwise, ornullfor decode.- Returns:
- attention output.
-