"""TP-aware linear layers (L2 operators). TODO: Consider demoting to L1 -- these are TP-aware wrappers around L1 primitives (Linear, Fp8Linear, AllReduce) and are used by other L2 ops, creating same-level dependencies. Moving them to L1 would make each L2 operator independently benchmarkable. """ from __future__ import annotations import math import torch import torch.nn as nn import torch.nn.functional as F from ....infra.tp import _tp_size, _tp_rank from ..L1.allreduce import AllReduce def _get_fp8_linear_cls(): from ..L1.fp8_linear import Fp8Linear return Fp8Linear _FP8_BLOCK = 128 def _scale_shape(out_dim: int, in_dim: int) -> tuple[int, int]: return (math.ceil(out_dim / _FP8_BLOCK), math.ceil(in_dim / _FP8_BLOCK)) class ColumnParallelLinear(nn.Module): """Splits output dim across TP ranks.""" def __init__(self, input_size: int, output_size: int, bias: bool = False, quant_config: dict | None = None): super().__init__() tp = _tp_size() assert output_size % tp == 0 self.output_size_per_partition = output_size // tp self.use_fp8 = quant_config is not None if self.use_fp8: self.weight = nn.Parameter( torch.empty(self.output_size_per_partition, input_size, dtype=torch.float8_e4m3fn), requires_grad=False, ) self.weight_scale_inv = nn.Parameter( torch.empty(*_scale_shape(self.output_size_per_partition, input_size), dtype=torch.float32), requires_grad=False, ) self.weight.weight_loader = self._weight_loader self.weight_scale_inv.weight_loader = self._scale_loader self.linear_op = _get_fp8_linear_cls()() else: self.weight = nn.Parameter(torch.empty(self.output_size_per_partition, input_size)) self.weight.weight_loader = self._weight_loader self.bias = nn.Parameter(torch.empty(self.output_size_per_partition)) if bias else None if self.bias is not None: self.bias.weight_loader = self._weight_loader def _weight_loader(self, param, loaded_weight): tp, rank = _tp_size(), _tp_rank() shard = param.data.size(0) loaded_weight = loaded_weight.narrow(0, rank * shard, shard) param.data.copy_(loaded_weight) def _scale_loader(self, param, loaded_weight): tp, rank = _tp_size(), _tp_rank() rows_per_shard = param.data.size(0) loaded_weight = loaded_weight.narrow(0, rank * rows_per_shard, rows_per_shard) param.data.copy_(loaded_weight) def forward(self, x): if self.use_fp8: return self.linear_op(x, self.weight, self.weight_scale_inv, self.bias) return F.linear(x, self.weight, self.bias) class MergedColumnParallelLinear(nn.Module): """gate_proj + up_proj merged into one linear, sharded across TP.""" def __init__(self, input_size: int, output_sizes: list[int], bias: bool = False, quant_config: dict | None = None, disable_tp: bool = False): super().__init__() tp = _tp_size() self.disable_tp = disable_tp self.output_sizes = output_sizes total = sum(output_sizes) if not disable_tp: assert all(s % tp == 0 for s in output_sizes) self.use_fp8 = quant_config is not None effective_tp = 1 if disable_tp else tp if self.use_fp8: self.weight = nn.Parameter( torch.empty(total // effective_tp, input_size, dtype=torch.float8_e4m3fn), requires_grad=False, ) self.weight_scale_inv = nn.Parameter( torch.empty(*_scale_shape(total // effective_tp, input_size), dtype=torch.float32), requires_grad=False, ) self.weight.weight_loader = self._weight_loader self.weight_scale_inv.weight_loader = self._scale_loader self.linear_op = _get_fp8_linear_cls()() else: self.weight = nn.Parameter(torch.empty(total // effective_tp, input_size)) self.weight.weight_loader = self._weight_loader self.bias = None if bias: self.bias = nn.Parameter(torch.empty(total // tp)) self.bias.weight_loader = self._weight_loader def _weight_loader(self, param, loaded_weight, shard_id: int | None = None): tp, rank = _tp_size(), _tp_rank() if shard_id is None: # Fused weight: ``loaded_weight`` is the full ``[sum(output_sizes), in]`` # tensor. Recurse per-shard so each output block is sharded across # TP ranks independently (mirrors vLLM's ``MergedColumnParallelLinear`` # weight loader when called without an explicit shard id). offset = 0 for sid, sz in enumerate(self.output_sizes): self._weight_loader( param, loaded_weight.narrow(0, offset, sz), sid, ) offset += sz return effective_tp = 1 if self.disable_tp else tp shard_offset = sum(self.output_sizes[:shard_id]) // effective_tp shard_size = self.output_sizes[shard_id] // effective_tp dst = param.data.narrow(0, shard_offset, shard_size) if self.disable_tp: dst.copy_(loaded_weight) else: src = loaded_weight.chunk(tp, 0)[rank] dst.copy_(src) def _scale_loader(self, param, loaded_weight, shard_id: int): tp, rank = _tp_size(), _tp_rank() effective_tp = 1 if self.disable_tp else tp shard_size_out = self.output_sizes[shard_id] // effective_tp scale_rows = math.ceil(shard_size_out / _FP8_BLOCK) shard_offset_out = sum(self.output_sizes[:shard_id]) // effective_tp scale_offset = math.ceil(shard_offset_out / _FP8_BLOCK) if self.disable_tp: param.data.narrow(0, scale_offset, scale_rows).copy_(loaded_weight) else: src = loaded_weight.chunk(tp, 0)[rank] param.data.narrow(0, scale_offset, scale_rows).copy_(src) def forward(self, x): if self.use_fp8: return self.linear_op(x, self.weight, self.weight_scale_inv, self.bias) return F.linear(x, self.weight, self.bias) class QKVParallelLinear(nn.Module): """Q, K, V projections merged and sharded across TP.""" def __init__(self, hidden_size: int, head_size: int, total_num_heads: int, total_num_kv_heads: int, bias: bool = False, quant_config: dict | None = None): super().__init__() tp = _tp_size() self.head_size = head_size self.num_heads = total_num_heads // tp # Replicate KV heads when not evenly divisible by TP if total_num_kv_heads % tp == 0: self.num_kv_heads = total_num_kv_heads // tp self._replicate_kv = False else: self.num_kv_heads = total_num_kv_heads self._replicate_kv = True output_size = (self.num_heads + 2 * self.num_kv_heads) * head_size self.use_fp8 = quant_config is not None if self.use_fp8: self.weight = nn.Parameter( torch.empty(output_size, hidden_size, dtype=torch.float8_e4m3fn), requires_grad=False, ) self.weight_scale_inv = nn.Parameter( torch.empty(*_scale_shape(output_size, hidden_size), dtype=torch.float32), requires_grad=False, ) self.weight.weight_loader = self._weight_loader self.weight_scale_inv.weight_loader = self._scale_loader self.linear_op = _get_fp8_linear_cls()() else: self.weight = nn.Parameter(torch.empty(output_size, hidden_size)) self.weight.weight_loader = self._weight_loader self.bias = None if bias: self.bias = nn.Parameter(torch.empty(output_size)) self.bias.weight_loader = self._weight_loader def _weight_loader(self, param, loaded_weight, shard_id: str): tp, rank = _tp_size(), _tp_rank() if shard_id == "q": shard_size = self.num_heads * self.head_size shard_offset = 0 src = loaded_weight.chunk(tp, 0)[rank] elif shard_id == "k": shard_size = self.num_kv_heads * self.head_size shard_offset = self.num_heads * self.head_size src = loaded_weight if self._replicate_kv else loaded_weight.chunk(tp, 0)[rank] else: shard_size = self.num_kv_heads * self.head_size shard_offset = self.num_heads * self.head_size + self.num_kv_heads * self.head_size src = loaded_weight if self._replicate_kv else loaded_weight.chunk(tp, 0)[rank] dst = param.data.narrow(0, shard_offset, shard_size) dst.copy_(src) def _scale_loader(self, param, loaded_weight, shard_id: str): tp, rank = _tp_size(), _tp_rank() if shard_id == "q": shard_size = self.num_heads * self.head_size shard_offset = 0 elif shard_id == "k": shard_size = self.num_kv_heads * self.head_size shard_offset = self.num_heads * self.head_size else: shard_size = self.num_kv_heads * self.head_size shard_offset = self.num_heads * self.head_size + self.num_kv_heads * self.head_size scale_rows = math.ceil(shard_size / _FP8_BLOCK) scale_offset = math.ceil(shard_offset / _FP8_BLOCK) src = loaded_weight.chunk(tp, 0)[rank] param.data.narrow(0, scale_offset, scale_rows).copy_(src) def forward(self, x): if self.use_fp8: return self.linear_op(x, self.weight, self.weight_scale_inv, self.bias) return F.linear(x, self.weight, self.bias) class ReplicatedLinear(nn.Module): """Full weight replicated on every TP rank (no sharding, no all-reduce).""" def __init__(self, input_size: int, output_size: int, bias: bool = True, quant_config: dict | None = None): super().__init__() self.use_fp8 = quant_config is not None if self.use_fp8: self.weight = nn.Parameter( torch.empty(output_size, input_size, dtype=torch.float8_e4m3fn), requires_grad=False, ) self.weight_scale_inv = nn.Parameter( torch.empty(*_scale_shape(output_size, input_size), dtype=torch.float32), requires_grad=False, ) self.weight.weight_loader = lambda p, w: p.data.copy_(w) self.weight_scale_inv.weight_loader = lambda p, w: p.data.copy_(w) self.linear_op = _get_fp8_linear_cls()() else: self.weight = nn.Parameter(torch.empty(output_size, input_size)) self.weight.weight_loader = lambda p, w: p.data.copy_(w) self.bias = nn.Parameter(torch.empty(output_size)) if bias else None if self.bias is not None: self.bias.weight_loader = lambda p, w: p.data.copy_(w) def forward(self, x): if self.use_fp8: return self.linear_op(x, self.weight, self.weight_scale_inv, self.bias) return F.linear(x, self.weight, self.bias) class RowParallelLinear(nn.Module): """Splits input dim across TP ranks, all-reduces output.""" def __init__(self, input_size: int, output_size: int, bias: bool = False, quant_config: dict | None = None, reduce_results: bool = True): super().__init__() tp = _tp_size() assert input_size % tp == 0 self.input_size_per_partition = input_size // tp self.tp_size = tp self.tp_rank = _tp_rank() self.reduce_results = reduce_results self.use_fp8 = quant_config is not None if self.use_fp8: self.weight = nn.Parameter( torch.empty(output_size, self.input_size_per_partition, dtype=torch.float8_e4m3fn), requires_grad=False, ) self.weight_scale_inv = nn.Parameter( torch.empty(*_scale_shape(output_size, self.input_size_per_partition), dtype=torch.float32), requires_grad=False, ) self.weight.weight_loader = self._weight_loader self.weight_scale_inv.weight_loader = self._scale_loader self.linear_op = _get_fp8_linear_cls()() else: self.weight = nn.Parameter(torch.empty(output_size, self.input_size_per_partition)) self.weight.weight_loader = self._weight_loader self.bias = nn.Parameter(torch.empty(output_size)) if bias else None if self.bias is not None: self.bias.weight_loader = lambda p, w: p.data.copy_(w) self.allreduce = AllReduce() def _weight_loader(self, param, loaded_weight): tp, rank = _tp_size(), _tp_rank() shard = param.data.size(1) loaded_weight = loaded_weight.narrow(1, rank * shard, shard) param.data.copy_(loaded_weight) def _scale_loader(self, param, loaded_weight): tp, rank = _tp_size(), _tp_rank() cols_per_shard = param.data.size(1) loaded_weight = loaded_weight.narrow(1, rank * cols_per_shard, cols_per_shard) param.data.copy_(loaded_weight) def forward(self, x): if self.use_fp8: y = self.linear_op(x, self.weight, self.weight_scale_inv, self.bias if self.tp_rank == 0 else None) else: y = F.linear(x, self.weight, self.bias if self.tp_rank == 0 else None) if self.reduce_results and self.tp_size > 1: y = self.allreduce(y) return y