Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions modules/modelSaver/chroma/ChromaModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -29,18 +28,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

text_encoder = save_pipeline.text_encoder
if text_encoder is not None:
Expand Down
12 changes: 1 addition & 11 deletions modules/modelSaver/ernie/ErnieModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand All @@ -25,16 +24,7 @@ def __save_diffusers(
):
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
14 changes: 1 addition & 13 deletions modules/modelSaver/flux/FluxModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -29,18 +28,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer_2 = pipeline.tokenizer_2
tokenizer_2.__deepcopy__ = lambda memo: tokenizer_2

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer_2, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype, tokenizer_attrs=("tokenizer_2",))

text_encoder_2 = save_pipeline.text_encoder_2
if text_encoder_2 is not None:
Expand Down
14 changes: 1 addition & 13 deletions modules/modelSaver/flux2/Flux2ModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -27,18 +26,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None: #TODO necessary?
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
17 changes: 1 addition & 16 deletions modules/modelSaver/hidream/HiDreamModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -26,21 +25,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline(use_original_modules=True)
pipeline.to("cpu")
if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer_3 = pipeline.tokenizer_3
tokenizer_3.__deepcopy__ = lambda memo: tokenizer_3
tokenizer_4 = pipeline.tokenizer_4
tokenizer_4.__deepcopy__ = lambda memo: tokenizer_4

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer_3, '__deepcopy__')
delattr(tokenizer_4, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype, tokenizer_attrs=("tokenizer_3", "tokenizer_4"))

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
14 changes: 1 addition & 13 deletions modules/modelSaver/hunyuanVideo/HunyuanVideoModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -29,18 +28,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline(use_original_modules=True)
pipeline.to("cpu")
if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer_1 = pipeline.tokenizer
tokenizer_1.__deepcopy__ = lambda memo: tokenizer_1

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer_1, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

text_encoder_1 = save_pipeline.text_encoder
if text_encoder_1 is not None:
Expand Down
23 changes: 23 additions & 0 deletions modules/modelSaver/mixin/DtypeModelSaverMixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ def _convert_state_dict_to_contiguous(
else:
state_dict[key] = value.contiguous()

def _copy_pipeline_to_dtype(
self,
pipeline,
dtype: torch.dtype | None,
tokenizer_attrs: tuple[str, ...] = ("tokenizer",),
):
if dtype is None:
return pipeline

# replace the tokenizers' __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizers try to reload from the file system otherwise
tokenizers = [getattr(pipeline, attr) for attr in tokenizer_attrs]
for tokenizer in tokenizers:
tokenizer.__deepcopy__ = lambda memo, tokenizer=tokenizer: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

for tokenizer in tokenizers:
delattr(tokenizer, '__deepcopy__')

return save_pipeline

def __calculate_safetensors_hash(
self,
state_dict: dict[str, Tensor] | None = None,
Expand Down
14 changes: 1 addition & 13 deletions modules/modelSaver/pixartAlpha/PixArtAlphaModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -28,18 +27,7 @@ def __save_diffusers(
pipeline = model.create_pipeline()
pipeline.to("cpu")

if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
15 changes: 1 addition & 14 deletions modules/modelSaver/qwen/QwenModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -26,19 +25,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
#TODO is this code necessary for all models? in that case, share code
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
14 changes: 1 addition & 13 deletions modules/modelSaver/sana/SanaModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand All @@ -25,18 +24,7 @@ def __save_diffusers(
pipeline = model.create_pipeline()
pipeline.to("cpu")

if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -29,18 +28,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer_3 = pipeline.tokenizer_3
tokenizer_3.__deepcopy__ = lambda memo: tokenizer_3

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer_3, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype, tokenizer_attrs=("tokenizer_3",))

text_encoder_3 = save_pipeline.text_encoder_3
if text_encoder_3 is not None:
Expand Down
15 changes: 1 addition & 14 deletions modules/modelSaver/zImage/ZImageModelSaver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import os.path
from pathlib import Path

Expand Down Expand Up @@ -26,19 +25,7 @@ def __save_diffusers(
# Copy the model to cpu by first moving the original model to cpu. This preserves some VRAM.
pipeline = model.create_pipeline()
pipeline.to("cpu")
if dtype is not None:
#TODO is this code necessary for all models? in that case, share code
# replace the tokenizers __deepcopy__ before calling deepcopy, to prevent a copy being made.
# the tokenizer tries to reload from the file system otherwise
tokenizer = pipeline.tokenizer
tokenizer.__deepcopy__ = lambda memo: tokenizer

save_pipeline = copy.deepcopy(pipeline)
save_pipeline.to(device="cpu", dtype=dtype, silence_dtype_warnings=True)

delattr(tokenizer, '__deepcopy__')
else:
save_pipeline = pipeline
save_pipeline = self._copy_pipeline_to_dtype(pipeline, dtype)

os.makedirs(Path(destination).absolute(), exist_ok=True)
save_pipeline.save_pretrained(destination)
Expand Down