Dequantize weights on the fly before doing the compute
| 225 | raise NotImplementedError |
| 226 | |
| 227 | class GGMLOps(comfy.ops.manual_cast): |
| 228 | """ |
| 229 | Dequantize weights on the fly before doing the compute |
| 230 | """ |
| 231 | class Linear(GGMLLayer, comfy.ops.manual_cast.Linear): |
| 232 | def __init__(self, in_features, out_features, bias=True, device=None, dtype=None): |
| 233 | torch.nn.Module.__init__(self) |
| 234 | # TODO: better workaround for reserved memory spike on windows |
| 235 | # Issue is with `torch.empty` still reserving the full memory for the layer |
| 236 | # Windows doesn't over-commit memory so without this 24GB+ of pagefile is used |
| 237 | self.in_features = in_features |
| 238 | self.out_features = out_features |
| 239 | self.weight = None |
| 240 | self.bias = None |
| 241 | |
| 242 | def forward_ggml_cast_weights(self, input): |
| 243 | weight, bias = self.cast_bias_weight(input) |
| 244 | return torch.nn.functional.linear(input, weight, bias) |
| 245 | |
| 246 | class Conv2d(GGMLLayer, comfy.ops.manual_cast.Conv2d): |
| 247 | def forward_ggml_cast_weights(self, input): |
| 248 | weight, bias = self.cast_bias_weight(input) |
| 249 | return self._conv_forward(input, weight, bias) |
| 250 | |
| 251 | class Embedding(GGMLLayer, comfy.ops.manual_cast.Embedding): |
| 252 | def forward_ggml_cast_weights(self, input, out_dtype=None): |
| 253 | output_dtype = out_dtype |
| 254 | if self.weight.dtype == torch.float16 or self.weight.dtype == torch.bfloat16: |
| 255 | out_dtype = None |
| 256 | weight, _bias = self.cast_bias_weight(self, device=input.device, dtype=out_dtype) |
| 257 | return torch.nn.functional.embedding( |
| 258 | input, weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse |
| 259 | ).to(dtype=output_dtype) |
| 260 | |
| 261 | class LayerNorm(GGMLLayer, comfy.ops.manual_cast.LayerNorm): |
| 262 | def forward_ggml_cast_weights(self, input): |
| 263 | if self.weight is None: |
| 264 | return super().forward_comfy_cast_weights(input) |
| 265 | weight, bias = self.cast_bias_weight(input) |
| 266 | return torch.nn.functional.layer_norm(input, self.normalized_shape, weight, bias, self.eps) |
| 267 | |
| 268 | class GroupNorm(GGMLLayer, comfy.ops.manual_cast.GroupNorm): |
| 269 | def forward_ggml_cast_weights(self, input): |
| 270 | weight, bias = self.cast_bias_weight(input) |
| 271 | return torch.nn.functional.group_norm(input, self.num_groups, weight, bias, self.eps) |
| 272 | |
| 273 | def move_patch_to_device(item, device): |
| 274 | if isinstance(item, torch.Tensor): |