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

Class SubParamLinearLayer

deepspeed/module_inject/layers.py:2251–2386  ·  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

2249
2250
2251class SubParamLinearLayer(TensorParallel_Layer):
2252 """
2253 Column-parallel linear layer with sub-parameter support.
2254
2255 Handles cases where weights contain multiple logical sub-parameters
2256 that need to be partitioned separately (e.g., fused QKV, chunked MLP, GQA).
2257
2258 The `shape` parameter controls how the weight is viewed and partitioned:
2259 - (3, -1) with partition_dim=0: 3 equal sub-params, partition each at dim 0
2260 - ((q, k, v), -1) with partition_dim=0: 3 unequal sub-params (1-level nesting)
2261 """
2262
2263 def __init__(self, module, mp_group, shape, partition_dim=0, **kwargs):
2264 super(SubParamLinearLayer, self).__init__(mp_group, **kwargs)
2265 self.weight = module.weight
2266 self.bias = module.bias
2267 self.shape = shape
2268 self.partition_dim = partition_dim
2269
2270 self._orig_weight_shape = tuple(module.weight.shape)
2271 self._orig_bias_shape = tuple(module.bias.shape) if self.bias is not None else None
2272 (self._logical_shape, self._output_shape, self._subparam_sizes,
2273 self._bias_partition_dim) = _infer_subparam_logical_shapes(self._orig_weight_shape, self.shape,
2274 self.partition_dim, self.name)
2275 # Resolve the per-rank widths once, for the same reason _freeze_partition_sizes does:
2276 # the split depends on this model's tp_meta.
2277 self._subparam_shard_widths = _subparam_shard_widths(
2278 self._subparam_sizes or (self._logical_shape[self.partition_dim], ), self.tp_world_size, self.tp_meta,
2279 self.name)
2280 self._bias_shape_spec = _bias_subparam_shape_spec(self._output_shape, self._bias_partition_dim,
2281 self._subparam_sizes)
2282 if self.bias is not None and self.bias.numel() != _shape_prod(self._output_shape):
2283 raise ValueError(f"AutoTP layer '{self.name}' bias size {self.bias.numel()} does not match output shape "
2284 f"{self._output_shape}.")
2285
2286 if self._should_materialize_tp_partition():
2287 self._tp_partition([self.weight, self.bias])
2288 self.support_training = True
2289 self.config_tp_params(self.weight)
2290 if self.bias is not None:
2291 self.config_tp_params(self.bias)
2292 self._mark_uc_metadata()
2293
2294 def forward(self, input):
2295 self._assert_compiled_if_deferred()
2296 if getattr(self, 'mp_group', None) is not None and not self.defer_collectives_to_compiler:
2297 input = ColumnParallel.apply(self.mp_group, input)
2298 output = torch.matmul(input, self.weight.transpose(-1, -2))
2299 if self.bias is not None:
2300 output = add_bias(output, self.bias, self.is_training_mode())
2301 return output
2302
2303 @torch.no_grad()
2304 def gather_params(self, params_list):
2305 """Gather partitioned parameters back to full size."""
2306 for idx, param in enumerate(params_list):
2307 if param is None:
2308 continue

Calls

no outgoing calls