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

Class LinearModuleForZeroStage3

deepspeed/runtime/zero/linear.py:162–226  ·  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

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