MCPcopy Create free account
hub / github.com/THUDM/GLM / RowParallelLinear

Class RowParallelLinear

mpu/layers.py:246–320  ·  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

244
245
246class RowParallelLinear(torch.nn.Module):
247 """Linear layer with row parallelism.
248
249 The linear layer is defined as Y = XA + b. A is parallelized along
250 its first dimension and X along its second dimension as:
251 - -
252 | A_1 |
253 | . |
254 A = | . | X = [X_1, ..., X_p]
255 | . |
256 | A_p |
257 - -
258 Arguments:
259 input_size: first dimension of matrix A.
260 output_size: second dimension of matrix A.
261 bias: If true, add bias. Note that bias is not parallelized.
262 input_is_parallel: If true, we assume that the input is already
263 split across the GPUs and we do not split
264 again.
265 init_method: method to initialize weights. Note that bias is always set
266 to zero.
267 stride: For the strided linear layers.
268 keep_master_weight_for_test: This was added for testing and should be
269 set to False. It returns the master weights
270 used for initialization.
271 """
272 def __init__(self, input_size, output_size, bias=True,
273 input_is_parallel=False,
274 init_method=init.xavier_normal_, stride=1,
275 keep_master_weight_for_test=False):
276 super(RowParallelLinear, self).__init__()
277
278 # Keep input parameters
279 self.input_size = input_size
280 self.output_size = output_size
281 self.input_is_parallel = input_is_parallel
282 # Divide the weight matrix along the last dimension.
283 world_size = get_model_parallel_world_size()
284 self.input_size_per_partition = divide(input_size, world_size)
285
286 # Parameters.
287 # Note: torch.nn.functional.linear performs XA^T + b and as a result
288 # we allocate the transpose.
289 self.weight = Parameter(torch.Tensor(self.output_size,
290 self.input_size_per_partition))
291 self.weight.model_parallel = True
292 if bias:
293 self.bias = Parameter(torch.Tensor(self.output_size))
294 # Always initialize bias to zero.
295 with torch.no_grad():
296 self.bias.zero_()
297 else:
298 self.register_parameter('bias', None)
299
300 # Initialize weight.
301 self.master_weight = _initialize_affine_weight(
302 self.weight, self.output_size, self.input_size,
303 self.input_size_per_partition, 1, init_method,

Callers 3

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected