Expand description
Tensor-parallel weight sharding helpers.
Megatron-style TP slices each weight tensor along one of two axes:
-
Column-parallel (Q/K/V proj, gate_proj, up_proj, lm_head): weight shape
[out, in]becomes[out / tp, in]. Rankrkeeps rows[r * out / tp, (r + 1) * out / tp). This is a single contiguous slice in row-major layout — onecopy_d2d. -
Row-parallel (O proj, down_proj): weight
[out, in]becomes[out, in / tp]. Rankrkeeps cols[r * in / tp, (r + 1) * in / tp). Per-row strided copy because the surviving slice is non-contiguous in row-major layout.
1D per-output vectors (q_norm_full, k_norm_full, gate_proj bias, etc.) shard with the same axis as their associated GEMM’s column-parallel output.
All shard helpers operate on BF16 weights before NVFP4 quantization; sharding the packed FP4 storage + FP8 scales is mechanical but adds two more axes to bookkeep, and pre-quant slicing keeps the existing quantize path untouched.
Structs§
- TpAttention
Dims - Pre-TP-shard attention dimensions reconstructed from
config. - TpGdn
Dims - Pre-TP-shard GDN (linear-attention / SSM) dimensions reconstructed from
config. - TpMoe
Dims - Pre-TP-shard dimensions for MoE expert projections. Unlike attention,
main.rsdoes NOT dividemoe_intermediate_sizebytp_size, sofull_inter == config.moe_intermediate_size. Local size is computed here for downstream callers.
Enums§
- TpShard
Kind - TP shard kind for a 2D BF16 weight
[out_dim, in_dim].
Functions§
- load_
qk_ norms_ tp - Q/K-norm 1D shard pair. The closure receives
(name, full_dim)and returns the loader’s sharded norm — typically aDenseWeight.q_normis sharded againstfull_q_n;k_normagainstfull_kv_n. Returns(q_norm, k_norm). - load_
qkvo_ tp - Sequence the four Q/K/V/O loads via a loader-supplied closure. The
closure receives
(name, full_out, full_in, kind)and returns the loader’s representation of that projection (BF16 dense, NVFP4 quantized, FP8 block-scaled — varies by format). - shard_
dense_ 1d_ bf16 - Shard a 1D BF16 vector
[dim](e.g. q_norm_full, gate_proj bias) on dim 0. Used for per-output vectors that pair with column-parallel GEMMs. - shard_
dense_ bf16 - Shard a BF16 dense weight
[out_dim, in_dim]according tokind. - shard_
dense_ weight - Convenience wrapper: shard a
DenseWeightBF16 tensor. The source weight is freed by the caller — this fn allocates a new device buffer. - shard_
fp8_ block_ scaled - Shard an FP8 block-scaled weight.
weightis[N, K]FP8 bytes;row_scaleis[N/block_size, K/block_size]FP32 (widened at load). Both slice on the same axis at block granularity. - shard_
gdn_ ba_ rows - Shard the BA gate BF16 weight
[2*full_nv, h]to[2*local_nv, h]. - shard_
gdn_ conv_ rows - Shard the depthwise
conv1dBF16 weight[full_conv_dim, d_conv]to[local_conv_dim, d_conv]. Channels ARE the QKV channels (one filter per channel), so this uses the SAME[Q|K|V]segment pattern as the QKV in-projection — the conv is NOT replicated across ranks. - shard_
gdn_ out_ proj_ row_ parallel - Shard the
out_projBF16 weight[h, full_value_dim]row-parallel on its input dim (value_dim). Rankrkeeps columns[r*local_value_dim, (r+1)*local_value_dim)of every output row; the partial products are summed with an all-reduce after the GEMM (mirrors attentiono_proj). Returns(ptr, h, local_value_dim). - shard_
gdn_ qkv_ rows - Shard the
[Q|K|V](in_proj_qkv) BF16 weight[full_conv_dim, h]to the local rank’s[local_conv_dim, h], slicing Q, K and V independently by the local head range. Returns(ptr, local_rows, h). - shard_
gdn_ qkvz_ rows - Shard the concatenated
[Q|K|V|Z](in_proj_qkvz) BF16 weight[full_qkvz_out, h]to the local rank’s[local_qkvz_out, h], slicing all four segments independently. Returns(ptr, local_rows, h). - shard_
gdn_ value_ vector - Shard a per-value-head 1D vector on the value-head axis. Handles BF16
(
norm,[full_nv*vd]→[local_nv*vd]withelem_bytes = 2,unit = vd) and FP32 (a_log/dt_bias,[full_nv]→[local_nv]withelem_bytes = 4,unit = 1).unitis the number of elements per value head. Returns(ptr, local_len_elems). - shard_
quantized_ nvfp4 - Shard an NVFP4-quantized weight. The packed weight is
[N, K/2]u8; the per-group scale is[N, K/group_size]u8 (FP8);weight_scale_2is a per-tensor f32 (replicated across all ranks).