# (c) City96 || Apache-2.0 (apache.org/licenses/LICENSE-2.0) import gguf import torch import comfy.ops from .dequant import dequantize_tensor class GGMLTensor(torch.Tensor): """ Main tensor-like class for storing quantized weights """ def __init__(self, *args, tensor_type, tensor_shape, patches=[], **kwargs): super().__init__() self.tensor_type = tensor_type self.tensor_shape = tensor_shape self.patches = patches def __new__(cls, *args, tensor_type, tensor_shape, patches=[], **kwargs): return super().__new__(cls, *args, **kwargs) def to(self, *args, **kwargs): new = super().to(*args, **kwargs) new.tensor_type = getattr(self, "tensor_type", None) new.tensor_shape = getattr(self, "tensor_shape", new.data.shape) new.patches = getattr(self, "patches", []).copy() return new def clone(self, *args, **kwargs): return self def detach(self, *args, **kwargs): return self def copy_(self, *args, **kwargs): # fixes .weight.copy_ in comfy/clip_model/CLIPTextModel try: return super().copy_(*args, **kwargs) except Exception as e: print(f"ignoring 'copy_' on tensor: {e}") def __deepcopy__(self, *args, **kwargs): # Intel Arc fix, ref#50 new = super().__deepcopy__(*args, **kwargs) new.tensor_type = getattr(self, "tensor_type", None) new.tensor_shape = getattr(self, "tensor_shape", new.data.shape) new.patches = getattr(self, "patches", []).copy() return new @property def shape(self): if not hasattr(self, "tensor_shape"): self.tensor_shape = self.size() return self.tensor_shape class GGMLLayer(torch.nn.Module): """ This (should) be responsible for de-quantizing on the fly """ comfy_cast_weights = True dequant_dtype = None patch_dtype = None def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs): for k,v in state_dict.items(): if k[len(prefix):] == "weight": self.weight = torch.nn.Parameter(v, requires_grad=False) elif k[len(prefix):] == "bias" and v is not None: self.bias = torch.nn.Parameter(v, requires_grad=False) else: missing_keys.append(k) def _save_to_state_dict(self, destination, prefix, keep_vars): # This is a fake state dict for vram estimation if self.weight is not None: weight = torch.zeros_like(self.weight, device=torch.device("meta")) destination[f"{prefix}weight"] = weight if self.bias is not None: bias = torch.zeros_like(self.bias, device=torch.device("meta")) destination[f"{prefix}bias"] = bias return # This would return the actual state dict weight, bias = self.get_weights() if weight is not None: destination[f"{prefix}weight"] = weight if bias is not None: destination[f"{prefix}bias"] = weight def get_weight(self, tensor, dtype): if tensor is None: return # consolidate and load patches to GPU in async patch_list = [] device = tensor.device for function, patches, key in getattr(tensor, "patches", []): patch_list += move_patch_to_cuda(patches, device) # dequantize tensor while patches load weight = dequantize_tensor(tensor, dtype, self.dequant_dtype) # apply patches if patch_list: if self.patch_dtype is None: weight = function(patch_list, weight, key) else: # for testing, may degrade image quality patch_dtype = dtype if self.patch_dtype == "target" else self.patch_dtype weight = function(patch_list, weight, key, patch_dtype) return weight def cast_bias_weight(s, input=None, dtype=None, device=None, bias_dtype=None): if input is not None: if dtype is None: dtype = input.dtype if bias_dtype is None: bias_dtype = dtype if device is None: device = input.device bias = None non_blocking = comfy.model_management.device_supports_non_blocking(device) if s.bias is not None: bias = s.get_weight(s.bias.to(device), dtype) if hasattr(s, "get_weight") else s.bias bias = comfy.ops.cast_to(bias, bias_dtype, device, non_blocking=non_blocking, copy=False) weight = s.get_weight(s.weight.to(device), dtype) if hasattr(s, "get_weight") else s.weight weight = comfy.ops.cast_to(weight, dtype, device, non_blocking=non_blocking, copy=False) return weight, bias class GGMLOps(comfy.ops.manual_cast): """ Dequantize weights on the fly before doing the compute """ class Linear(GGMLLayer, comfy.ops.manual_cast.Linear): def forward_comfy_cast_weights(self, input): weight, bias = self.cast_bias_weight(input) return torch.nn.functional.linear(input, weight, bias) class Conv2d(GGMLLayer, comfy.ops.manual_cast.Conv2d): def forward_comfy_cast_weights(self, input): weight, bias = self.cast_bias_weight(input) return self._conv_forward(input, weight, bias) def move_patch_to_cuda(item, device): if isinstance(item, torch.Tensor): return item.to(device, non_blocking=True) elif isinstance(item, tuple): return tuple(move_patch_to_cuda(x, device) for x in item) elif isinstance(item, list): return [move_patch_to_cuda(x, device) for x in item] else: return item