mstar.model.components.distributed#
Tensor-parallel building blocks.
Parallel linears (ColumnParallelLinear, RowParallelLinear,
MergedColumnParallelLinear, QKVParallelLinear), vocab-parallel
embedding (VocabParallelEmbedding), and the composed parallel
Attention / GatedMLP 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.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.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.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 multi-head attention. |
|
Vocabulary-parallel embedding for TP'd token LM stacks. |
|
TP-aware SwiGLU MLPs (parallel counterparts of |
|
Ulysses sequence parallelism — model-agnostic primitives. |