mstar.model.components.distributed#
Tensor-parallel building blocks.
Parallel linears (ColumnParallelLinear, RowParallelLinear,
MergedColumnParallelLinear, QKVParallelLinear), vocab-parallel
embedding (VocabParallelEmbedding), and the composed parallel
Attention / GatedMLP / dense MLP blocks. Each parallel parameter carries a
weight_loader attribute used by the model-level weight loader to
slice checkpoint tensors per-rank on load.
For vocab parallelism, pair VocabParallelEmbedding with
ColumnParallelLinear(gather_output=True) on the LM head: the
embedding all-reduces shard contributions before the first transformer
layer, and the LM head all-gathers logits along the vocab dim before
returning, so the sampler stays vocab-oblivious.
- class mstar.model.components.distributed.ColumnParallelLinear(comm_group, input_size, output_size, bias=False, gather_output=False, skip_bias_add=False, dtype=None)[source]#
Bases:
ModuleLinear layer with column parallelism.
The linear layer is defined as Y = XA + b. A is parallelized along its second dimension as A = [A_1, …, A_p].
- Parameters:
- forward(input_)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.distributed.KVColumnParallelLinear(comm_group, input_size, head_size, total_num_kv_heads, bias=False, dtype=None)[source]#
Bases:
ColumnParallelLinearOne K or V projection, sharded by head, replicated when heads run out.
For models whose checkpoint keeps
k_proj/v_projapart rather than fused into a QKV block.QKVParallelLinearalready handles this case, but only for the fused layout.The replication is not an optimization — it is what keeps the model and the KV cache agreeing.
KVConfig.shardgives every rank one whole head oncetp_size >= total_num_kv_heads, so a plain column shard, which would hand each rank a fraction of a head, would silently disagree with the cache it writes into.- Parameters:
- class mstar.model.components.distributed.MergedColumnParallelLinear(comm_group, input_size, output_sizes, bias=True, gather_output=False, skip_bias_add=False, dtype=None)[source]#
Bases:
ColumnParallelLinearPacked linear layers with column parallelism.
Similar to ColumnParallelLinear, but the weight matrix is concatenated along the output dimension. When the weight matrix is loaded, the different partitions are sharded separately.
- Parameters:
- class mstar.model.components.distributed.ParallelAttention(*, comm_group=None, 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:
comm_group (CommGroup | None)
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)
- class mstar.model.components.distributed.ParallelCrossAttention(*, comm_group=None, hidden_size, num_heads, num_kv_heads=None, 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:
ModuleCross-attention split across tensor-parallel ranks.
Each rank handles a subset of attention heads, then the output projection combines their contributions with an all-reduce.
- 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_kv_heads, head_dim). Override to reshape for a model-specific pool layout.
- class mstar.model.components.distributed.ParallelGatedDeltaNet(*, hidden_size, num_k_heads, num_v_heads, head_k_dim, head_v_dim, conv_kernel_size, layout=GDNProjLayout.SPLIT, proj_bias=False, conv_bias=False, rms_norm_eps=1e-6, linear_attn_key='linear_attn', state_key='gdn_state', comm_group=None)[source]#
Bases:
GatedDeltaNetGatedDeltaNetover one rank’s heads.Takes the checkpoint’s (total) head counts and divides them here.
- class mstar.model.components.distributed.ParallelGatedMLP(hidden_size, intermediate_size, comm_group=None, activation='silu', bias=False)[source]#
Bases:
ModuleSwiGLU-style gated MLP partitioned across TP ranks.
- Parameters:
comm_group (CommGroup | None) – TP comm group for this MLP’s parallel linears.
hidden_size (int) – model hidden dim (full, not per-partition).
intermediate_size (int) – SwiGLU intermediate dim (full).
activation (str | Callable) – HF activation name (
silu,gelu,gelu_tanh).bias (bool) – whether the linears have a bias term.
- forward(x)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.distributed.ParallelGatedMLPUnfused(hidden_size, intermediate_size, comm_group=None, activation='silu', bias=False)[source]#
Bases:
ModuleSwiGLU MLP with separate (unfused) gate/up column shards.
Same math and sharding as
ParallelGatedMLP, withgate_projandup_projkept as separateColumnParallelLinearprojections so the parameter names match a checkpoint’sgate_proj.weight/up_proj.weightone-to-one (plain name-matched loading, no stacked-parameter loader rules). A trivial comm group (world size 1) makes the projections plain linears.- Parameters:
- forward(x)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.distributed.ParallelMLP(hidden_size, intermediate_size, comm_group=None, activation='relu2', bias=False)[source]#
Bases:
ModuleDense two-projection MLP
down_proj(act(up_proj(x)))across TP ranks.The parallel counterpart of
mstar.model.components.MLP, named after the dense-LLM checkpoint convention (up_proj/down_proj) so a Nemotron-style FFN loads by name.activationaccepts the HF names_resolve_activationknows (relu2is the Nemotron squared ReLU) or a callable. A trivial comm group (world size 1) makes both projections plain linears.- Parameters:
- forward(x)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.distributed.QKVParallelLinear(comm_group, hidden_size, head_size, total_num_heads, total_num_kv_heads=None, bias=True, skip_bias_add=False, dtype=None, v_head_size=None)[source]#
Bases:
ColumnParallelLinearLinear layers for the attention’s QKV transformation.
Linear layers for the linear transformation of the query, key, and value vectors in the attention layer. The weight matrix is concatenated along the output dimension. The layer is parallelized along the head dimension. When the number of key/value heads is smaller than the number of query heads (e.g., multi-query/grouped-query attention), the key/value head may be replicated while the query heads are partitioned.
- Parameters:
- weight_loader(param, loaded_weight, loaded_shard_id=None)[source]#
Copy this rank’s q / k / v slice into
paramat the right offset within the merged qkv parameter.loaded_shard_idmust be one of"q","k", or"v". For GQA wheretp_size > total_num_kv_headsthe K / V heads are replicated across ranks (each rank in the same KV-replica group loads the same KV head).
- class mstar.model.components.distributed.RowParallelLinear(comm_group, input_size, output_size, bias=True, input_is_parallel=True, skip_bias_add=False, dtype=None, reduce_results=True)[source]#
Bases:
ModuleLinear layer with row parallelism.
The linear layer is defined as Y = XA + b. A is parallelized along its first dimension and X along its second dimension as:
A_1 |. |- A = | . | X = [X_1, …, X_p]
- . |A_p | - -
- Parameters:
- forward(input_)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.distributed.VocabParallelEmbedding(num_embeddings, embedding_dim, comm_group=None, padding_idx=None, dtype=None)[source]#
Bases:
ModuleRow-parallel token embedding.
Each rank holds rows
[tp_rank * V/tp : (tp_rank + 1) * V/tp]of the full[V, H]embedding matrix. The forward zeroes contributions for tokens outside this rank’s slice andall_reducesums shards into the replicated full embedding.- Parameters:
num_embeddings (int)
embedding_dim (int)
comm_group (CommGroup | None)
padding_idx (int | None)
dtype (torch.dtype | None)
- forward(input_)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Modules
TP-aware self- and cross-attention. |
|
Vocabulary-parallel embedding for TP'd token LM stacks. |
|
The gated delta-net mixer, sharded across tensor-parallel ranks. |
|
TP-aware SwiGLU MLPs (parallel counterparts of |
|
Ulysses sequence parallelism — model-agnostic primitives. |