Class GroupedQueryAttention
java.lang.Object
smile.llm.model.llama.GroupedQueryAttention
- All Implemented Interfaces:
Attention
Grouped Query Attention (GQA). GQA is a highly efficient transformer
attention mechanism that bridges the gap between traditional
Multi-Head Attention (MHA) and Multi-Query Attention (MQA).
By grouping sets of query heads to share a single Key and Value head,
GQA drastically reduces memory usage during inference while maintaining
model quality.
KV activations are stored in a shared KvCachePool managed by
the inference engine (e.g. smile-serve), enabling radix-tree prefix reuse
across requests instead of allocating a private
maxBatchSize × maxSeqLen buffer per layer.
-
Constructor Summary
ConstructorsConstructorDescriptionGroupedQueryAttention(int dim, int numHeads, int numKvHeads, int layerId) Constructor.GroupedQueryAttention(int dim, int numHeads, int numKvHeads, KvCacheLayout layout) Convenience constructor that allocates a small test pool sized fromlayout. -
Method Summary
Modifier and TypeMethodDescriptionDecode forward with per-row cache write positions (FlashInfer ragged path).Forward pass through the attention module.module()Returns pytorch module.intReturns the key/value head count.intReturns the query head count on this rank.voidreplaceProjections(LinearOp wq, LinearOp wk, LinearOp wv, LinearOp wo) Replaces projection ops with quantized backends (shard-then-pack already applied).
-
Constructor Details
-
GroupedQueryAttention
public GroupedQueryAttention(int dim, int numHeads, int numKvHeads, int layerId) Constructor. Install aKvCachePoolviasetCachePool(KvCachePool)before the firstforward(Tensor, int, Tensor, Tensor).- Parameters:
dim- token embedding dimension.numHeads- number of query heads.numKvHeads- number of key/value heads.layerId- zero-based layer index within the transformer.
-
GroupedQueryAttention
Convenience constructor that allocates a small test pool sized fromlayout. Prefer installing a shared pool in production.- Parameters:
dim- token embedding dimension.numHeads- number of query heads.numKvHeads- number of key/value heads.layout- cache layout used for the private test pool.
-
-
Method Details
-
replaceProjections
Replaces projection ops with quantized backends (shard-then-pack already applied). Unregisters and frees denseLinearLayershells so their GPU storage is released before KV pool sizing.- Parameters:
wq- query projection op.wk- key projection op.wv- value projection op.wo- output projection op.
-
numQueryHeads
public int numQueryHeads()Returns the query head count on this rank.- Returns:
- query head count (equals global heads when TP size is 1).
-
numKeyValueHeads
public int numKeyValueHeads()Returns the key/value head count.- Returns:
- key/value head count (global; local when TP is applied).
-
module
Description copied from interface:AttentionReturns pytorch module. -
forward
Description copied from interface:AttentionForward pass through the attention module. -
forward
Description copied from interface:AttentionDecode forward with per-row cache write positions (FlashInfer ragged path).Default implementation rejects mixed positions; backends that support ragged decode override this.
-