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

Class ColumnParallelLinear

SwissArmyTransformer/sat/mpu/layers.py:170–356  ·  view source on GitHub ↗

Linear layer with column parallelism. The linear layer is defined as Y = XA + b. A is parallelized along its second dimension as A = [A_1, ..., A_p]. Arguments: input_size: first dimension of matrix A. output_size: second dimension of matrix A. bias: If true, ad

Source from the content-addressed store, hash-verified

168
169
170class ColumnParallelLinear(torch.nn.Module):
171 """Linear layer with column parallelism.
172
173 The linear layer is defined as Y = XA + b. A is parallelized along
174 its second dimension as A = [A_1, ..., A_p].
175
176 Arguments:
177 input_size: first dimension of matrix A.
178 output_size: second dimension of matrix A.
179 bias: If true, add bias
180 gather_output: If true, call all-gether on output and make Y avaiable
181 to all GPUs, otherwise, every GPU will have its output
182 which is Y_i = XA_i
183 init_method: method to initialize weights. Note that bias is always set
184 to zero.
185 stride: For the strided linear layers. only used in initialization.
186 keep_master_weight_for_test: This was added for testing and should be
187 set to False. It returns the master weights
188 used for initialization.
189 """
190 def __init__(self, input_size, output_size, bias=True, gather_output=True,
191 init_method=unscaled_init_method(0.02), stride=1,
192 keep_master_weight_for_test=False, params_dtype=torch.float, module=None, name=None, skip_init=False, device=torch.device('cpu')):
193 super(ColumnParallelLinear, self).__init__()
194
195 # Keep input parameters
196 self.stride = stride
197 self.input_size = input_size
198 self.output_size = output_size
199 self.gather_output = gather_output
200 # Divide the weight matrix along the last dimension.
201 world_size = get_model_parallel_world_size()
202 self.output_size_per_partition = divide(output_size, world_size)
203
204 # Parameters.
205 # Note: torch.nn.functional.linear performs XA^T + b and as a result
206 # we allocate the transpose.
207 self.weight = Parameter(torch.empty(self.output_size_per_partition,
208 self.input_size, dtype=params_dtype,
209 device=device))
210 self.weight.model_parallel = True
211 if bias:
212 self.bias = Parameter(torch.empty(self.output_size_per_partition,dtype=params_dtype, device=device))
213 self.bias.model_parallel = True
214 # Always initialize bias to zero.
215 with torch.no_grad():
216 self.bias.zero_()
217 else:
218 self.register_parameter('bias', None)
219
220 # Initialize weight.
221 if not skip_init:
222 self.master_weight = _initialize_affine_weight(
223 self.weight, self.output_size, self.input_size,
224 self.output_size_per_partition, 0, init_method,
225 stride=self.stride, return_master_weight=keep_master_weight_for_test, module=module, name=name, self=self)
226
227 def forward(self, input_):

Callers 13

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
reinitMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__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