mstar.model.components.attention#
Multi-head attention with GQA, optional QK-norm, and pluggable RoPE.
Constructed with separate q_proj / k_proj / v_proj Linears
matching the HF checkpoint layout. After loading, call
consolidate_qkv_weight() to fuse them into a single
qkv_proj_weight buffer (one fused GEMM instead of three) and null
out the originals; the forward branches on whether consolidation has
happened.
- Variations supported:
GQA via
num_kv_heads<num_heads.Optional bias on qkv / o projections.
Optional per-head QK-norm (RMSNorm applied to q / k after projection, before RoPE) — used by qwen3.
Different
input_hidden_sizefrom the model’s nominalhidden_size(used by pi05’s action expert, which shares K/V dims with PaliGemma but has its own width).Llama-style RoPE scaling parameters (
rope_scale,low_freq_factor, etc.) for the cache-handle path.
For non-standard RoPE schemes (e.g. qwen3’s 3D MRoPE), subclass and
override _apply_rope rather than going through the position resource.
Classes
|
|
|
Multi-head cross-attention over an engine-managed encoder-context KV. |
- class mstar.model.components.attention.Attention(*, hidden_size, num_heads, num_kv_heads, head_dim, qkv_bias=False, o_bias=False, qk_norm=False, rms_norm_eps=1e-6, rope_theta=10_000.0, rope_scale=1.0, rope_low_freq_factor=1.0, rope_high_freq_factor=1.0, rope_old_context_len=8192, input_hidden_size=None, attn_key='attn', kv_key='kv', pos_key='rope')[source]#
Bases:
Module- Parameters:
hidden_size (int)
num_heads (int)
num_kv_heads (int)
head_dim (int)
qkv_bias (bool)
o_bias (bool)
qk_norm (bool)
rms_norm_eps (float)
rope_theta (float)
rope_scale (float)
rope_low_freq_factor (float)
rope_high_freq_factor (float)
rope_old_context_len (int)
input_hidden_size (int | None)
attn_key (str)
kv_key (str)
pos_key (str | None)
- bind_resources(resources)[source]#
Resolve the resources this layer calls. See
NodeSubmodule.bind_node_resources..get: a layer may be bound on a node that owns only some of them.- Parameters:
resources (dict)
- Return type:
None
- class mstar.model.components.attention.CrossAttention(*, hidden_size, num_heads, head_dim, q_bias=True, k_bias=False, v_bias=True, o_bias=True, source='default', cross_key=None, context_kv_key=None)[source]#
Bases:
ModuleMulti-head cross-attention over an engine-managed encoder-context KV.
For encoder-decoder models (Whisper, etc.): the decoder attends to a fixed encoder context whose K/V are computed once at prefill and written into a KV resource of their own, under the
context_labelthe model’sCrossAttentionConfignames (see issue #160). Q is projected per step; K/V projections are exposed viacompute_kvso the submodule can write them into that cache at encode time.Q/K/V/O are separate
nn.Linearmatching the HF layout. Subclasses override projection details (bias flags, acompute_kvthat reshapes for a model-specific pool layout) as needed; the default matches Whisper (q/v/o biased, k unbiased).TODO(#160): the projections are plain
nn.Linear— this module is not yet TP/SP-compatible (no column/row-parallel splits over heads). A tensor-parallel cross-attention variant is needed to serve the decoder under TP alongside the self-attention path.- Parameters:
- bind_resources(resources)[source]#
Resolve this source’s cross-attention resource and the cache holding its context. See
NodeSubmodule.bind_node_resources.- Parameters:
resources (dict)
- Return type:
None
- compute_kv(encoder_states)[source]#
Project the encoder context to K/V for the cross-attention pool.
(enc_len, hidden) -> (k, v), each(enc_len, num_heads, head_dim). Override to reshape for a model-specific pool layout.