Construct a BitLinear from an existing nn.Linear, copying its weight.
(cls, linear: nn.Linear)
| 132 | # ------------------------------------------------------------------ |
| 133 | @classmethod |
| 134 | def from_linear(cls, linear: nn.Linear) -> "BitLinear": |
| 135 | """Construct a BitLinear from an existing nn.Linear, copying its weight.""" |
| 136 | bl = cls( |
| 137 | in_features=linear.in_features, |
| 138 | out_features=linear.out_features, |
| 139 | bias=linear.bias is not None, |
| 140 | dtype=linear.weight.dtype, |
| 141 | ) |
| 142 | with torch.no_grad(): |
| 143 | bl.weight.copy_(linear.weight) |
| 144 | if linear.bias is not None and bl.bias is not None: |
| 145 | bl.bias.copy_(linear.bias) |
| 146 | return bl |
| 147 | |
| 148 | |
| 149 | # ────────────────────────────────────────────────────────────────────────────── |
no outgoing calls
no test coverage detected