MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / SubParamLinearLayer

Class SubParamLinearLayer

deepspeed/module_inject/layers.py:2169–2304  ·  view source on GitHub ↗

Column-parallel linear layer with sub-parameter support. Handles cases where weights contain multiple logical sub-parameters that need to be partitioned separately (e.g., fused QKV, chunked MLP, GQA). The `shape` parameter controls how the weight is viewed and partitioned: - (

Source from the content-addressed store, hash-verified

2167
2168
2169class SubParamLinearLayer(TensorParallel_Layer):
2170 """
2171 Column-parallel linear layer with sub-parameter support.
2172
2173 Handles cases where weights contain multiple logical sub-parameters
2174 that need to be partitioned separately (e.g., fused QKV, chunked MLP, GQA).
2175
2176 The `shape` parameter controls how the weight is viewed and partitioned:
2177 - (3, -1) with partition_dim=0: 3 equal sub-params, partition each at dim 0
2178 - ((q, k, v), -1) with partition_dim=0: 3 unequal sub-params (1-level nesting)
2179 """
2180
2181 def __init__(self, module, mp_group, shape, partition_dim=0, **kwargs):
2182 super(SubParamLinearLayer, self).__init__(mp_group, **kwargs)
2183 self.weight = module.weight
2184 self.bias = module.bias
2185 self.shape = shape
2186 self.partition_dim = partition_dim
2187
2188 self._orig_weight_shape = tuple(module.weight.shape)
2189 self._orig_bias_shape = tuple(module.bias.shape) if self.bias is not None else None
2190 (self._logical_shape, self._output_shape, self._subparam_sizes,
2191 self._bias_partition_dim) = _infer_subparam_logical_shapes(self._orig_weight_shape, self.shape,
2192 self.partition_dim, self.name)
2193 # Resolve the per-rank widths once, for the same reason _freeze_partition_sizes does:
2194 # the split depends on this model's tp_meta.
2195 self._subparam_shard_widths = _subparam_shard_widths(
2196 self._subparam_sizes or (self._logical_shape[self.partition_dim], ), self.tp_world_size, self.tp_meta,
2197 self.name)
2198 self._bias_shape_spec = _bias_subparam_shape_spec(self._output_shape, self._bias_partition_dim,
2199 self._subparam_sizes)
2200 if self.bias is not None and self.bias.numel() != _shape_prod(self._output_shape):
2201 raise ValueError(f"AutoTP layer '{self.name}' bias size {self.bias.numel()} does not match output shape "
2202 f"{self._output_shape}.")
2203
2204 if self._should_materialize_tp_partition():
2205 self._tp_partition([self.weight, self.bias])
2206 self.support_training = True
2207 self.config_tp_params(self.weight)
2208 if self.bias is not None:
2209 self.config_tp_params(self.bias)
2210 self._mark_uc_metadata()
2211
2212 def forward(self, input):
2213 self._assert_compiled_if_deferred()
2214 if getattr(self, 'mp_group', None) is not None and not self.defer_collectives_to_compiler:
2215 input = ColumnParallel.apply(self.mp_group, input)
2216 output = torch.matmul(input, self.weight.transpose(-1, -2))
2217 if self.bias is not None:
2218 output = add_bias(output, self.bias, self.is_training_mode())
2219 return output
2220
2221 @torch.no_grad()
2222 def gather_params(self, params_list):
2223 """Gather partitioned parameters back to full size."""
2224 for idx, param in enumerate(params_list):
2225 if param is None:
2226 continue

Calls

no outgoing calls