Fix FLUX2 Klein load-time VRAM spikes on low-memory GPUs. (#726)
Keep the transformer and Qwen text encoder off CUDA during initial load/quantization in low-VRAM mode so model startup avoids full-model OOM before offloading and quantization can take effect. Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Jaret Burkett <jaretburkett@gmail.com>
This commit is contained in:
@@ -44,15 +44,14 @@ class Flux2KleinModel(Flux2Model):
|
|||||||
self.flux2_klein_te_path,
|
self.flux2_klein_te_path,
|
||||||
torch_dtype=dtype,
|
torch_dtype=dtype,
|
||||||
)
|
)
|
||||||
text_encoder.to(self.device_torch, dtype=dtype)
|
|
||||||
|
|
||||||
flush()
|
|
||||||
|
|
||||||
if self.model_config.quantize_te:
|
if self.model_config.quantize_te:
|
||||||
self.print_and_status_update("Quantizing Qwen3")
|
self.print_and_status_update("Quantizing Qwen3")
|
||||||
quantize(text_encoder, weights=get_qtype(self.model_config.qtype))
|
quantize(text_encoder, weights=get_qtype(self.model_config.qtype_te))
|
||||||
freeze(text_encoder)
|
freeze(text_encoder)
|
||||||
flush()
|
flush()
|
||||||
|
elif not self.model_config.low_vram:
|
||||||
|
text_encoder.to(self.device_torch, dtype=dtype)
|
||||||
|
flush()
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self.model_config.layer_offloading
|
self.model_config.layer_offloading
|
||||||
|
|||||||
@@ -155,11 +155,11 @@ class Flux2Model(BaseModel):
|
|||||||
|
|
||||||
transformer.load_state_dict(transformer_state_dict, assign=True)
|
transformer.load_state_dict(transformer_state_dict, assign=True)
|
||||||
|
|
||||||
transformer.to(self.quantize_device, dtype=dtype)
|
|
||||||
|
|
||||||
if self.model_config.quantize:
|
if self.model_config.quantize:
|
||||||
# patch the state dict method
|
# patch the state dict method
|
||||||
patch_dequantization_on_save(transformer)
|
patch_dequantization_on_save(transformer)
|
||||||
|
# Avoid full-model peak VRAM allocation before quantization.
|
||||||
|
self.print_and_status_update("Keeping transformer on CPU for quantization")
|
||||||
self.print_and_status_update("Quantizing Transformer")
|
self.print_and_status_update("Quantizing Transformer")
|
||||||
quantize_model(self, transformer)
|
quantize_model(self, transformer)
|
||||||
flush()
|
flush()
|
||||||
@@ -234,10 +234,16 @@ class Flux2Model(BaseModel):
|
|||||||
|
|
||||||
flush()
|
flush()
|
||||||
# just to make sure everything is on the right device and dtype
|
# just to make sure everything is on the right device and dtype
|
||||||
text_encoder[0].to(self.device_torch)
|
if self.model_config.low_vram:
|
||||||
|
text_encoder[0].to("cpu")
|
||||||
|
else:
|
||||||
|
text_encoder[0].to(self.device_torch)
|
||||||
text_encoder[0].requires_grad_(False)
|
text_encoder[0].requires_grad_(False)
|
||||||
text_encoder[0].eval()
|
text_encoder[0].eval()
|
||||||
pipe.transformer = pipe.transformer.to(self.device_torch)
|
if self.model_config.low_vram:
|
||||||
|
pipe.transformer = pipe.transformer.to("cpu")
|
||||||
|
else:
|
||||||
|
pipe.transformer = pipe.transformer.to(self.device_torch)
|
||||||
flush()
|
flush()
|
||||||
|
|
||||||
# save it to the model class
|
# save it to the model class
|
||||||
|
|||||||
Reference in New Issue
Block a user