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

Class LinearModuleForZeroStage3

deepspeed/runtime/zero/linear.py:168–232  ·  view source on GitHub ↗

r"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`. The weights are pre-transposed and stored as A^T instead of transposing during each forward. Memory savings proportional to the parameter size. Args: in_features: size of each input sample ou

Source from the content-addressed store, hash-verified

166
167
168class LinearModuleForZeroStage3(Module):
169 r"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`.
170 The weights are pre-transposed and stored as A^T instead of transposing during each
171 forward. Memory savings proportional to the parameter size.
172
173 Args:
174 in_features: size of each input sample
175 out_features: size of each output sample
176 bias: If set to ``False``, the layer will not learn an additive bias.
177 Default: ``True``
178
179 Shape:
180 - Input: :math:`(N, *, H_{in})` where :math:`*` means any number of
181 additional dimensions and :math:`H_{in} = \text{in\_features}`
182 - Output: :math:`(N, *, H_{out})` where all but the last dimension
183 are the same shape as the input and :math:`H_{out} = \text{out\_features}`.
184
185 Attributes:
186 weight: the learnable weights of the module of shape
187 :math:`(\text{out\_features}, \text{in\_features})`. The values are
188 initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
189 :math:`k = \frac{1}{\text{in\_features}}`
190 bias: the learnable bias of the module of shape :math:`(\text{out\_features})`.
191 If :attr:`bias` is ``True``, the values are initialized from
192 :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
193 :math:`k = \frac{1}{\text{in\_features}}`
194
195 Examples::
196
197 >>> m = nn.Linear(20, 30)
198 >>> input = torch.randn(128, 20)
199 >>> output = m(input)
200 >>> print(output.size())
201 torch.Size([128, 30])
202 """
203 __constants__ = ['in_features', 'out_features']
204 in_features: int
205 out_features: int
206 weight: Tensor
207
208 def __init__(self, in_features: int, out_features: int, bias: bool = True) -> None:
209 super(LinearModuleForZeroStage3, self).__init__()
210 print("Building ZeRO module")
211 self.in_features = in_features
212 self.out_features = out_features
213 self.weight = Parameter(torch.Tensor(out_features, in_features))
214 if bias:
215 self.bias = Parameter(torch.Tensor(out_features))
216 else:
217 self.register_parameter('bias', None)
218 self.reset_parameters()
219
220 def reset_parameters(self) -> None:
221 init.kaiming_uniform_(self.weight, a=math.sqrt(5))
222 if self.bias is not None:
223 fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
224 bound = 1 / math.sqrt(fan_in)
225 init.uniform_(self.bias, -bound, bound)

Callers 4

test_autocast_linearMethod · 0.90
test.pyFile · 0.85

Calls

no outgoing calls

Tested by 3

test_autocast_linearMethod · 0.72