Repository navigation
Expand file tree
/
Copy pathquant_ops.py
More file actions
184 lines (147 loc) · 5.74 KB
/
Copy pathquant_ops.py
File metadata and controls
184 lines (147 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# GGML QuantizedTensor support for ComfyUI DynamicVRAM loading.
from dataclasses import dataclass, replace
import gguf
import torch
from comfy_kitchen.tensor import (
BaseLayoutParams,
QuantizedLayout,
QuantizedTensor,
register_layout_class,
)
try:
from comfy_kitchen.tensor import register_layout_op
except ImportError:
def register_layout_op(*_args, **_kwargs):
return lambda function: function
from .dequant import TORCH_COMPATIBLE_QTYPES, dequantize_functions
from .kquant_backend import try_kquant_linear
@dataclass(frozen=True)
class GGMLLayoutParams(BaseLayoutParams):
tensor_type: int
transposed: bool = False
class GGMLLayout(QuantizedLayout):
Params = GGMLLayoutParams
@classmethod
def quantize(cls, tensor, **kwargs):
raise NotImplementedError("Quantization to GGML format is not supported")
@classmethod
def dequantize(cls, qdata, params):
qtype = gguf.GGMLQuantizationType(params.tensor_type)
orig_shape = (
tuple(reversed(params.orig_shape))
if params.transposed
else params.orig_shape
)
if qtype in TORCH_COMPATIBLE_QTYPES:
return qdata.reshape(orig_shape).to(params.orig_dtype)
if qtype not in dequantize_functions:
dequantized = gguf.quants.dequantize(qdata.cpu().numpy(), qtype)
return torch.from_numpy(dequantized).reshape(orig_shape).to(
device=qdata.device,
dtype=params.orig_dtype,
)
_, type_size = gguf.GGML_QUANT_SIZES[qtype]
raw = qdata.reshape(-1).view(torch.uint8)
blocks = raw.reshape((raw.numel() // type_size, type_size))
return dequantize_functions[qtype](blocks, *gguf.GGML_QUANT_SIZES[qtype], None).reshape(
orig_shape
).to(params.orig_dtype)
@classmethod
def get_plain_tensors(cls, qtensor):
return (qtensor._qdata,)
@classmethod
def state_dict_tensors(cls, qdata, params):
return {"weight": qdata}
register_layout_class("GGMLLayout", GGMLLayout)
def _is_ggml_qtensor(tensor):
return (
isinstance(tensor, QuantizedTensor)
and tensor._layout_cls == "GGMLLayout"
)
def _physical_weight_shape(weight):
shape = tuple(weight._params.orig_shape)
return tuple(reversed(shape)) if weight._params.transposed else shape
def _try_native_linear(input_tensor, weight, bias):
if (
not _is_ggml_qtensor(weight)
or weight._params.transposed
or weight._params.orig_dtype != input_tensor.dtype
):
return None
qtype = gguf.GGMLQuantizationType(weight._params.tensor_type)
return try_kquant_linear(
input_tensor,
weight._qdata,
qtype,
_physical_weight_shape(weight),
bias,
dequant_dtype=None,
)
@register_layout_op(torch.ops.aten.t.default, GGMLLayout)
def _handle_ggml_t(qtensor, args, kwargs):
input_tensor = args[0]
if not _is_ggml_qtensor(input_tensor) or len(input_tensor.shape) != 2:
return torch.ops.aten.t.default(input_tensor.dequantize())
params = replace(
input_tensor._params,
orig_shape=tuple(reversed(input_tensor._params.orig_shape)),
transposed=not input_tensor._params.transposed,
)
return QuantizedTensor(input_tensor._qdata, "GGMLLayout", params)
@register_layout_op(torch.ops.aten.linear.default, GGMLLayout)
def _handle_ggml_linear(qtensor, args, kwargs):
input_tensor, weight = args[0], args[1]
bias = args[2] if len(args) > 2 else kwargs.get("bias")
if isinstance(input_tensor, QuantizedTensor):
input_tensor = input_tensor.dequantize()
if not _is_ggml_qtensor(weight):
if isinstance(weight, QuantizedTensor):
weight = weight.dequantize()
return torch.nn.functional.linear(input_tensor, weight, bias)
output = _try_native_linear(input_tensor, weight, bias)
if output is not None:
return output
return torch.nn.functional.linear(input_tensor, weight.dequantize(), bias)
def _resolve_transposed_rhs(rhs):
if not _is_ggml_qtensor(rhs) or not rhs._params.transposed:
return None
params = replace(
rhs._params,
orig_shape=tuple(reversed(rhs._params.orig_shape)),
transposed=False,
)
return QuantizedTensor(rhs._qdata, "GGMLLayout", params)
@register_layout_op(torch.ops.aten.mm.default, GGMLLayout)
def _handle_ggml_mm(qtensor, args, kwargs):
left, right = args[0], args[1]
if isinstance(left, QuantizedTensor):
left = left.dequantize()
weight = _resolve_transposed_rhs(right)
if weight is not None:
output = _try_native_linear(left, weight, None)
if output is not None:
return output
if isinstance(right, QuantizedTensor):
right = right.dequantize()
return torch.mm(left, right)
@register_layout_op(torch.ops.aten.addmm.default, GGMLLayout)
def _handle_ggml_addmm(qtensor, args, kwargs):
bias, left, right = args[0], args[1], args[2]
if isinstance(left, QuantizedTensor):
left = left.dequantize()
weight = _resolve_transposed_rhs(right)
if not kwargs and weight is not None:
output = _try_native_linear(left, weight, bias)
if output is not None:
return output
if isinstance(right, QuantizedTensor):
right = right.dequantize()
return torch.addmm(bias, left, right, **kwargs)
def make_quantized(qdata, tensor_type, tensor_shape, orig_dtype=torch.float16):
params = GGMLLayoutParams(
scale=torch.ones((), dtype=torch.float32),
orig_dtype=orig_dtype,
orig_shape=tuple(tensor_shape),
tensor_type=tensor_type.value if not isinstance(tensor_type, int) else tensor_type,
)
return QuantizedTensor(qdata, "GGMLLayout", params)