Class MultiHeadLatentAttention
java.lang.Object
io.github.kirstenali.deepj.layers.transformer.attention.MultiHeadLatentAttention
Compact Multi-Head Latent Attention (MLA) inspired by DeepSeek-V2/V3.
Unlike standard MHA which projects Q, K, V directly from x, MLA first compresses
through a low-rank bottleneck then expands:
cQ = x · Wdq [seqLen × qRank] — Q compression Q = cQ · Wuq [seqLen × dModel] — Q expansion cKV = x · Wdkv [seqLen × kvRank] — shared KV compression K = cKV · Wuk [seqLen × dModel] — K expansion V = cKV · Wuv [seqLen × dModel] — V expansion
RoPE is applied to Q and K after expansion. Scaled dot-product attention and the
output projection Wo are then identical to standard MHA.
The factorisation can support caching only cKV in an incremental decoder.
This layer currently computes a complete sequence and does not own an inference cache.
Parameters: Wdq, Wuq, Wdkv, Wuk, Wuv, Wo (6 total, vs 4 in standard MHA).
-
Constructor Summary
ConstructorsConstructorDescriptionMultiHeadLatentAttention(int dModel, int nHeads, int qRank, int kvRank, RotaryEmbedding rope, Random rnd) -
Method Summary
-
Constructor Details
-
MultiHeadLatentAttention
public MultiHeadLatentAttention(int dModel, int nHeads, int qRank, int kvRank, RotaryEmbedding rope, Random rnd) - Parameters:
dModel- model dimensionnHeads- number of attention heads; must dividedModelqRank- Q latent dimension (e.g. 1536 in DeepSeek-V2; use dModel/2 for small models)kvRank- KV latent dimension (e.g. 512 in DeepSeek-V2; use dModel/4 for small models)rope- pre-built rotary embedding sized fordModel / nHeadsrnd- random source for weight initialisation
-
-
Method Details