Move the offload and quantization to the base model
This commit is contained in:
@@ -1628,37 +1628,41 @@ class BaseModel:
|
||||
else:
|
||||
self.text_encoder.to(*args, **kwargs)
|
||||
|
||||
def prepare_text_encoder(self, text_encoder, dtype=None):
|
||||
"""Standard post-load text-encoder policy: layer offloading, device
|
||||
placement, then quantize_te. Skips quantization when the checkpoint
|
||||
loaded pre-quantized."""
|
||||
from optimum.quanto import freeze
|
||||
from toolkit.memory_management import MemoryManager
|
||||
from toolkit.util.quantize import get_qtype, quantize
|
||||
|
||||
dtype = dtype if dtype is not None else self.torch_dtype
|
||||
if (
|
||||
self.model_config.layer_offloading
|
||||
and self.model_config.layer_offloading_text_encoder_percent > 0
|
||||
):
|
||||
MemoryManager.attach(
|
||||
text_encoder,
|
||||
self.device_torch,
|
||||
offload_percent=self.model_config.layer_offloading_text_encoder_percent,
|
||||
)
|
||||
|
||||
text_encoder.to(self.device_torch, dtype=dtype)
|
||||
flush()
|
||||
|
||||
if self.model_config.quantize_te and not getattr(
|
||||
text_encoder, "aitk_is_quantized", False
|
||||
):
|
||||
self.print_and_status_update("Quantizing Text Encoder")
|
||||
quantize(text_encoder, weights=get_qtype(self.model_config.qtype_te))
|
||||
freeze(text_encoder)
|
||||
flush()
|
||||
return text_encoder
|
||||
|
||||
def component_load_kwargs(self, role: str = "transformer", dtype=None):
|
||||
"""kwargs for a v2 module's .load()/.aitk_post_load(), derived from
|
||||
model_config: qtype (with the accuracy recovery adapter recombined),
|
||||
offload fraction, devices, low_vram placement. Roles: "transformer",
|
||||
"te", "vae"."""
|
||||
mc = self.model_config
|
||||
qtype, offload = None, 0.0
|
||||
if role == "transformer":
|
||||
if mc.quantize:
|
||||
qtype = mc.qtype
|
||||
if mc.accuracy_recovery_adapter and "|" not in (qtype or ""):
|
||||
qtype = f"{qtype}|{mc.accuracy_recovery_adapter}"
|
||||
if mc.layer_offloading:
|
||||
offload = mc.layer_offloading_transformer_percent
|
||||
elif role == "te":
|
||||
if mc.quantize_te:
|
||||
qtype = mc.qtype_te
|
||||
if mc.layer_offloading:
|
||||
offload = mc.layer_offloading_text_encoder_percent
|
||||
if dtype is None:
|
||||
dtype = self.vae_torch_dtype if role == "vae" else self.torch_dtype
|
||||
device = self.te_device_torch if role == "te" else self.device_torch
|
||||
if mc.low_vram and role in ("transformer", "te"):
|
||||
device = "cpu"
|
||||
elif role == "vae":
|
||||
device = self.vae_device_torch
|
||||
return dict(
|
||||
qtype=qtype,
|
||||
offload=offload,
|
||||
dtype=dtype,
|
||||
device=device,
|
||||
quantize_device=self.device_torch,
|
||||
base_model=self,
|
||||
use_comfy_weights=mc.model_kwargs.get("use_comfy_weights", True),
|
||||
)
|
||||
|
||||
def convert_lora_weights_before_save(self, state_dict):
|
||||
# can be overridden in child classes to convert weights before saving
|
||||
|
||||
Reference in New Issue
Block a user