MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / RowParallelLinear

Class RowParallelLinear

SwissArmyTransformer/sat/mpu/layers.py:359–481  ·  view source on GitHub ↗

Linear 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] | .

Source from the content-addressed store, hash-verified

357
358
359class RowParallelLinear(torch.nn.Module):
360 """Linear layer with row parallelism.
361
362 The linear layer is defined as Y = XA + b. A is parallelized along
363 its first dimension and X along its second dimension as:
364 - -
365 | A_1 |
366 | . |
367 A = | . | X = [X_1, ..., X_p]
368 | . |
369 | A_p |
370 - -
371 Arguments:
372 input_size: first dimension of matrix A.
373 output_size: second dimension of matrix A.
374 bias: If true, add bias. Note that bias is not parallelized.
375 input_is_parallel: If true, we assume that the input is already
376 split across the GPUs and we do not split
377 again.
378 init_method: method to initialize weights. Note that bias is always set
379 to zero.
380 stride: For the strided linear layers.
381 keep_master_weight_for_test: This was added for testing and should be
382 set to False. It returns the master weights
383 used for initialization.
384 """
385 def __init__(self, input_size, output_size, bias=True,
386 input_is_parallel=False,
387 init_method=unscaled_init_method(0.02), stride=1,
388 keep_master_weight_for_test=False, params_dtype=torch.float, module=None, name=None, skip_init=False, device=torch.device('cpu'), final_bias=True):
389 super(RowParallelLinear, self).__init__()
390
391 # Keep input parameters
392 self.input_size = input_size
393 self.output_size = output_size
394 self.input_is_parallel = input_is_parallel
395 # Divide the weight matrix along the last dimension.
396 world_size = get_model_parallel_world_size()
397 self.input_size_per_partition = divide(input_size, world_size)
398 self.final_bias = final_bias
399
400 # Parameters.
401 # Note: torch.nn.functional.linear performs XA^T + b and as a result
402 # we allocate the transpose.
403 self.weight = Parameter(torch.empty(self.output_size,
404 self.input_size_per_partition, dtype=params_dtype, device=device))
405 self.weight.model_parallel = True
406 if bias:
407 self.bias = Parameter(torch.empty(self.output_size, dtype=params_dtype, device=device))
408 # Always initialize bias to zero.
409 with torch.no_grad():
410 self.bias.zero_()
411 else:
412 self.register_parameter('bias', None)
413
414 # Initialize weight.
415 if not skip_init:
416 self.master_weight = _initialize_affine_weight(

Callers 4

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected