File size: 9,107 Bytes
c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 c4a514f c2ecfb5 |
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
from fnmatch import fnmatch
from typing import List, Optional, Union
import torch
from click import secho
from cublas_ops import CublasLinear
from quanto import (
QModuleMixin,
quantize_module,
QLinear,
QConv2d,
QLayerNorm,
)
from quanto.tensor import Optimizer, qtype, qfloat8, qint4, qint8
from torch import nn
class QuantizationDtype:
qfloat8 = "qfloat8"
qint2 = "qint2"
qint4 = "qint4"
qint8 = "qint8"
def into_qtype(qtype: QuantizationDtype) -> qtype:
if qtype == QuantizationDtype.qfloat8:
return qfloat8
elif qtype == QuantizationDtype.qint4:
return qint4
elif qtype == QuantizationDtype.qint8:
return qint8
else:
raise ValueError(f"Unknown qtype: {qtype}")
def _set_module_by_name(parent_module, name, child_module):
module_names = name.split(".")
if len(module_names) == 1:
setattr(parent_module, name, child_module)
else:
parent_module_name = name[: name.rindex(".")]
parent_module = parent_module.get_submodule(parent_module_name)
setattr(parent_module, module_names[-1], child_module)
def _quantize_submodule(
model: torch.nn.Module,
name: str,
module: torch.nn.Module,
weights: Optional[Union[str, qtype]] = None,
activations: Optional[Union[str, qtype]] = None,
optimizer: Optional[Optimizer] = None,
):
if isinstance(module, CublasLinear):
return 0
num_quant = 0
qmodule = quantize_module(
module, weights=weights, activations=activations, optimizer=optimizer
)
if qmodule is not None:
_set_module_by_name(model, name, qmodule)
# num_quant += 1
qmodule.name = name
for name, param in module.named_parameters():
# Save device memory by clearing parameters
setattr(module, name, None)
del param
num_quant += 1
return num_quant
def _quantize(
model: torch.nn.Module,
weights: Optional[Union[str, qtype]] = None,
activations: Optional[Union[str, qtype]] = None,
optimizer: Optional[Optimizer] = None,
include: Optional[Union[str, List[str]]] = None,
exclude: Optional[Union[str, List[str]]] = None,
):
"""Quantize the specified model submodules
Recursively quantize the submodules of the specified parent model.
Only modules that have quantized counterparts will be quantized.
If include patterns are specified, the submodule name must match one of them.
If exclude patterns are specified, the submodule must not match one of them.
Include or exclude patterns are Unix shell-style wildcards which are NOT regular expressions. See
https://docs.python.org/3/library/fnmatch.html for more details.
Note: quantization happens in-place and modifies the original model and its descendants.
Args:
model (`torch.nn.Module`): the model whose submodules will be quantized.
weights (`Optional[Union[str, qtype]]`): the qtype for weights quantization.
activations (`Optional[Union[str, qtype]]`): the qtype for activations quantization.
include (`Optional[Union[str, List[str]]]`):
Patterns constituting the allowlist. If provided, module names must match at
least one pattern from the allowlist.
exclude (`Optional[Union[str, List[str]]]`):
Patterns constituting the denylist. If provided, module names must not match
any patterns from the denylist.
"""
num_quant = 0
if include is not None:
include = [include] if isinstance(include, str) else exclude
if exclude is not None:
exclude = [exclude] if isinstance(exclude, str) else exclude
for name, m in model.named_modules():
if include is not None and not any(
fnmatch(name, pattern) for pattern in include
):
continue
if exclude is not None and any(fnmatch(name, pattern) for pattern in exclude):
continue
num_quant += _quantize_submodule(
model,
name,
m,
weights=weights,
activations=activations,
optimizer=optimizer,
)
return num_quant
def _freeze(model):
for name, m in model.named_modules():
if isinstance(m, QModuleMixin):
m.freeze()
def _is_block_compilable(module: nn.Module) -> bool:
for module in module.modules():
if _is_quantized(module):
return False
if _is_quantized(module):
return False
return True
def _simple_swap_linears(model: nn.Module, root_name: str = ""):
for name, module in model.named_children():
if (
_is_linear(module)
and hasattr(module, "weight")
and module.weight is not None
and module.weight.data is not None
):
weights = module.weight.data
bias = None
if module.bias is not None:
bias = module.bias.data
with torch.device(module.weight.device):
new_cublas = CublasLinear(
module.in_features,
module.out_features,
bias=bias is not None,
device=module.weight.device,
dtype=module.weight.dtype,
)
new_cublas.weight.data = weights
if bias is not None:
new_cublas.bias.data = bias
setattr(model, name, new_cublas)
if root_name == "":
secho(f"Replaced {name} with CublasLinear", fg="green")
else:
secho(f"Replaced {root_name}.{name} with CublasLinear", fg="green")
else:
if root_name == "":
_simple_swap_linears(module, str(name))
else:
_simple_swap_linears(module, str(root_name) + "." + str(name))
def _full_quant(
model, max_quants=24, current_quants=0, quantization_dtype: qtype = qfloat8
):
if current_quants < max_quants:
current_quants += _quantize(model, quantization_dtype)
_freeze(model)
print(f"Quantized {current_quants} modules with {quantization_dtype}")
return current_quants
def _is_linear(module: nn.Module) -> bool:
return not isinstance(
module, (QLinear, QConv2d, QLayerNorm, CublasLinear)
) and isinstance(module, nn.Linear)
def _is_quantized(module: nn.Module) -> bool:
return isinstance(module, (QLinear, QConv2d, QLayerNorm))
def quantize_and_dispatch_to_device(
flow_model: nn.Module,
flux_device: torch.device = torch.device("cuda"),
flux_dtype: torch.dtype = torch.float16,
num_layers_to_quantize: int = 20,
quantization_dtype: QuantizationDtype = QuantizationDtype.qfloat8,
compile_blocks: bool = True,
compile_extras: bool = True,
quantize_extras: bool = False,
replace_linears: bool = True,
):
quant_type = into_qtype(quantization_dtype)
num_quanted = 0
flow_model = flow_model.requires_grad_(False).eval().type(flux_dtype)
for block in flow_model.single_blocks:
block.cuda(flux_device)
if num_quanted < num_layers_to_quantize:
num_quanted = _full_quant(
block,
num_layers_to_quantize,
num_quanted,
quantization_dtype=quant_type,
)
for block in flow_model.double_blocks:
block.cuda(flux_device)
if num_quanted < num_layers_to_quantize:
num_quanted = _full_quant(
block,
num_layers_to_quantize,
num_quanted,
quantization_dtype=quant_type,
)
to_gpu_extras = [
"vector_in",
"img_in",
"txt_in",
"time_in",
"guidance_in",
"final_layer",
"pe_embedder",
]
if compile_blocks:
for i, block in enumerate(flow_model.single_blocks):
if _is_block_compilable(block):
block.compile()
secho(f"Compiled block {i}", fg="green")
for i, block in enumerate(flow_model.double_blocks):
if _is_block_compilable(block):
block.compile()
secho(f"Compiled block {i}", fg="green")
if replace_linears:
_simple_swap_linears(flow_model)
for extra in to_gpu_extras:
m_extra = getattr(flow_model, extra).cuda(flux_device).type(flux_dtype)
if compile_extras:
if extra in ["time_in", "vector_in", "guidance_in", "final_layer"]:
m_extra.compile()
secho(
f"Compiled extra {extra} -- {m_extra.__class__.__name__}",
fg="green",
)
elif quantize_extras:
if not isinstance(m_extra, nn.Linear):
_full_quant(
m_extra,
current_quants=num_quanted,
max_quants=num_layers_to_quantize,
quantization_dtype=quantization_dtype,
)
return flow_model
|