Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] DDUF format #10037

Merged
merged 92 commits into from
Jan 14, 2025
Merged
Changes from 3 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
1fb86e3
load and save dduf archive
SunMarc Nov 27, 2024
0389333
style
SunMarc Nov 27, 2024
2eeda25
switch to zip uncompressed
SunMarc Nov 28, 2024
cbee7cb
Merge branch 'main' into dduf
sayakpaul Nov 30, 2024
d840867
updates
sayakpaul Nov 29, 2024
78135f1
Merge branch 'main' into dduf
sayakpaul Dec 4, 2024
7d2c7d5
Update src/diffusers/pipelines/pipeline_utils.py
SunMarc Dec 4, 2024
e66c4d0
Update src/diffusers/pipelines/pipeline_utils.py
SunMarc Dec 4, 2024
b14bffe
first draft
SunMarc Dec 4, 2024
1cd5155
remove print
SunMarc Dec 4, 2024
b8a43e7
switch to dduf_file for consistency
SunMarc Dec 5, 2024
cac988a
Merge remote-tracking branch 'origin/main' into dduf
SunMarc Dec 5, 2024
977baa3
switch to huggingface hub api
SunMarc Dec 5, 2024
81bd097
fix log
SunMarc Dec 6, 2024
c4df147
Merge branch 'main' into dduf
sayakpaul Dec 8, 2024
d0a861c
add a basic test
sayakpaul Dec 8, 2024
1ec988f
Update src/diffusers/configuration_utils.py
SunMarc Dec 9, 2024
5217712
Update src/diffusers/pipelines/pipeline_utils.py
SunMarc Dec 9, 2024
6922226
Update src/diffusers/pipelines/pipeline_utils.py
SunMarc Dec 9, 2024
3b0d84d
fix
SunMarc Dec 9, 2024
1bc953b
Merge remote-tracking branch 'origin/dduf' into dduf
SunMarc Dec 9, 2024
04ecf0e
fix variant
SunMarc Dec 9, 2024
9fff68a
change saving logic
SunMarc Dec 10, 2024
ed6c727
DDUF - Load transformers components manually (#10171)
Wauplin Dec 11, 2024
17d50d1
working version with transformers and tokenizer !
SunMarc Dec 11, 2024
59929a5
add generation_config case
SunMarc Dec 12, 2024
aa0d497
fix tests
sayakpaul Dec 12, 2024
a793066
Merge branch 'main' into dduf
sayakpaul Dec 12, 2024
7602952
remove saving for now
SunMarc Dec 12, 2024
660d7c8
typing
SunMarc Dec 12, 2024
8358ef6
need next version from transformers
SunMarc Dec 12, 2024
4e7d15a
Update src/diffusers/configuration_utils.py
SunMarc Dec 12, 2024
cc75db3
check path corectly
SunMarc Dec 12, 2024
63575af
Merge remote-tracking branch 'origin/dduf' into dduf
SunMarc Dec 12, 2024
1e5ebf5
Apply suggestions from code review
SunMarc Dec 12, 2024
53e100b
udapte
SunMarc Dec 12, 2024
54991ac
Merge remote-tracking branch 'origin/dduf' into dduf
SunMarc Dec 12, 2024
1eb25dc
typing
SunMarc Dec 12, 2024
0cb1b98
remove check for subfolder
SunMarc Dec 12, 2024
5ec3951
quality
SunMarc Dec 12, 2024
1785eaa
revert setup changes
SunMarc Dec 12, 2024
7486016
oups
SunMarc Dec 12, 2024
73e81a5
more readable condition
SunMarc Dec 12, 2024
ea0126d
add loading from the hub test
sayakpaul Dec 13, 2024
3ebdcff
add basic docs.
sayakpaul Dec 13, 2024
5943a60
Merge branch 'main' into dduf
sayakpaul Dec 13, 2024
021abf0
Apply suggestions from code review
SunMarc Dec 13, 2024
af2ca07
add example
SunMarc Dec 13, 2024
9d70b6c
Merge branch 'main' into dduf
sayakpaul Dec 13, 2024
47cb92c
Merge remote-tracking branch 'origin/dduf' into dduf
SunMarc Dec 13, 2024
c9734ab
add
SunMarc Dec 13, 2024
27ebf9e
make functions private
SunMarc Dec 13, 2024
d5dbb5c
Apply suggestions from code review
SunMarc Dec 13, 2024
627aec0
resolve conflicts.
sayakpaul Dec 18, 2024
f0e21a9
Merge branch 'main' into dduf
sayakpaul Dec 18, 2024
e9b7429
minor.
sayakpaul Dec 18, 2024
b8b699a
fixes
sayakpaul Dec 18, 2024
6003176
Merge branch 'main' into dduf
sayakpaul Dec 18, 2024
0e54b06
fix
sayakpaul Dec 18, 2024
a026055
change the precdence of parameterized.
sayakpaul Dec 18, 2024
03e30b4
Merge remote-tracking branch 'upstream/main' into dduf
SunMarc Dec 23, 2024
da48dcb
Merge branch 'main' into dduf
SunMarc Dec 23, 2024
0fbea9a
Merge branch 'main' into dduf
sayakpaul Dec 30, 2024
6a163c7
error out when custom pipeline is passed with dduf_file.
sayakpaul Dec 30, 2024
67b617e
updates
sayakpaul Dec 31, 2024
b40272e
fix
sayakpaul Dec 31, 2024
ce237f3
updates
sayakpaul Dec 31, 2024
454b9b9
fixes
sayakpaul Dec 31, 2024
366aa2f
updates
sayakpaul Dec 31, 2024
6648995
fix xfail condition.
sayakpaul Dec 31, 2024
21ae7ee
fix xfail
sayakpaul Dec 31, 2024
15d4569
fixes
sayakpaul Dec 31, 2024
f3a4ddc
sharded checkpoint compat
SunMarc Jan 3, 2025
a032025
Merge branch 'dduf' of github.com:huggingface/diffusers into dduf
SunMarc Jan 3, 2025
faa0cac
Merge branch 'main' into dduf
sayakpaul Jan 3, 2025
0205cc8
add test for sharded checkpoint
SunMarc Jan 3, 2025
9cda4c1
Merge branch 'dduf' of github.com:huggingface/diffusers into dduf
SunMarc Jan 3, 2025
cd0734e
Merge branch 'main' into dduf
sayakpaul Jan 4, 2025
5037d39
add suggestions
SunMarc Jan 6, 2025
c9e08da
Merge branch 'dduf' of github.com:huggingface/diffusers into dduf
SunMarc Jan 6, 2025
02a368b
Update src/diffusers/models/model_loading_utils.py
SunMarc Jan 7, 2025
7bc9347
from suggestions
SunMarc Jan 7, 2025
fff5954
add class attributes to flag dduf tests
SunMarc Jan 7, 2025
da402da
last one
SunMarc Jan 7, 2025
9ebbf84
Merge branch 'main' into dduf
sayakpaul Jan 8, 2025
aaaa947
Merge branch 'main' into dduf
sayakpaul Jan 8, 2025
290b88d
resolve conflicts.
sayakpaul Jan 12, 2025
ac420af
fix logic
SunMarc Jan 13, 2025
f62527f
remove comment
SunMarc Jan 13, 2025
c34ce42
Merge remote-tracking branch 'upstream/main' into dduf
SunMarc Jan 13, 2025
c899fd0
revert changes
SunMarc Jan 13, 2025
bb1cff8
Merge branch 'main' into dduf
sayakpaul Jan 14, 2025
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
78 changes: 78 additions & 0 deletions src/diffusers/pipelines/pipeline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def save_pretrained(
variant: Optional[str] = None,
max_shard_size: Optional[Union[int, str]] = None,
push_to_hub: bool = False,
dduf_filename: Optional[Union[str, os.PathLike]] = None,
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
):
"""
Expand Down Expand Up @@ -301,9 +302,54 @@ def is_saveable_module(name, value):

save_method(os.path.join(save_directory, pipeline_component_name), **save_kwargs)

if dduf_filename:
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
import shutil
import zipfile

def zipdir(dir_to_archive, zipf):
"zip a directory"
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
for root, dirs, files in os.walk(dir_to_archive):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.join(
os.path.basename(dir_to_archive), os.path.relpath(file_path, start=dir_to_archive)
)
zipf.write(file_path, arcname=arcname)

dduf_file_path = os.path.join(save_directory, dduf_filename)

if os.path.isdir(dduf_file_path):
logger.warning(
f"Removing the existing folder {dduf_file_path} so that we can save the DDUF archive."
)
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
shutil.rmtree(dduf_file_path)
if (
os.path.exists(dduf_file_path)
and os.path.isfile(dduf_file_path)
and zipfile.is_zipfile(dduf_file_path)
):
# Open in append mode if the file exists
mode = "a"
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
else:
# Open in write mode to create it if it doesn't exist
mode = "w"
with zipfile.ZipFile(dduf_file_path, mode=mode, compression=zipfile.ZIP_STORED) as zipf:
dir_to_archive = os.path.join(save_directory, pipeline_component_name)
if os.path.isdir(dir_to_archive):
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
zipdir(dir_to_archive, zipf)
shutil.rmtree(dir_to_archive)
julien-c marked this conversation as resolved.
Show resolved Hide resolved

# finally save the config
self.save_config(save_directory)

if dduf_filename:
import zipfile

with zipfile.ZipFile(dduf_file_path, mode="a", compression=zipfile.ZIP_STORED) as zipf:
config_path = os.path.join(save_directory, self.config_name)
zipf.write(config_path, arcname=os.path.basename(config_path))
os.remove(config_path)
SunMarc marked this conversation as resolved.
Show resolved Hide resolved

if push_to_hub:
# Create a new empty model card and eventually tag it
model_card = load_or_create_model_card(repo_id, token=token, is_pipeline=True)
Expand Down Expand Up @@ -523,6 +569,8 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
- A path to a *directory* (for example `./my_pipeline_directory/`) containing pipeline weights
saved using
[`~DiffusionPipeline.save_pretrained`].
- A path to a *directory* (for example `./my_pipeline_directory/`) containing a dduf archive or
folder
torch_dtype (`str` or `torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype. If "auto" is passed, the
dtype is automatically derived from the model's weights.
Expand Down Expand Up @@ -617,6 +665,8 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
variant (`str`, *optional*):
Load weights from a specified variant filename such as `"fp16"` or `"ema"`. This is ignored when
loading `from_flax`.
dduf (`str`, *optional*):
Load weights from the specified dduf archive or folder.
SunMarc marked this conversation as resolved.
Show resolved Hide resolved

<Tip>

Expand Down Expand Up @@ -666,6 +716,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
offload_state_dict = kwargs.pop("offload_state_dict", False)
low_cpu_mem_usage = kwargs.pop("low_cpu_mem_usage", _LOW_CPU_MEM_USAGE_DEFAULT)
variant = kwargs.pop("variant", None)
dduf = kwargs.pop("dduf", None)
use_safetensors = kwargs.pop("use_safetensors", None)
use_onnx = kwargs.pop("use_onnx", None)
load_connected_pipeline = kwargs.pop("load_connected_pipeline", False)
Expand Down Expand Up @@ -736,6 +787,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
custom_pipeline=custom_pipeline,
custom_revision=custom_revision,
variant=variant,
dduf=dduf,
load_connected_pipeline=load_connected_pipeline,
**kwargs,
)
Expand All @@ -757,6 +809,25 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
)
logger.warning(warn_msg)

if dduf:
import zipfile

zip_file_path = os.path.join(cached_folder, dduf)
extract_to = os.path.join(cached_folder, f"{dduf}_extracted")
# if zip file, we need to extract the zipfile and remove it
if os.path.isfile(zip_file_path):
if zipfile.is_zipfile(zip_file_path):
with zipfile.ZipFile(zip_file_path, "r") as zipf:
zipf.extractall(extract_to)
# remove zip archive to free memory
os.remove(zip_file_path)
# rename folder to match the name of the dduf archive
os.rename(extract_to, zip_file_path)
else:
raise RuntimeError("The dduf path passed is not a zip archive")
# udapte cached folder location as the dduf content is in a seperate folder
cached_folder = zip_file_path

config_dict = cls.load_config(cached_folder)

# pop out "_ignore_files" as it is only needed for download
Expand Down Expand Up @@ -1227,6 +1298,8 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
variant (`str`, *optional*):
Load weights from a specified variant filename such as `"fp16"` or `"ema"`. This is ignored when
loading `from_flax`.
dduf(`str`, *optional*):
Load weights from the specified DDUF archive or folder.
use_safetensors (`bool`, *optional*, defaults to `None`):
If set to `None`, the safetensors weights are downloaded if they're available **and** if the
safetensors library is installed. If set to `True`, the model is forcibly loaded from safetensors
Expand Down Expand Up @@ -1267,6 +1340,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
use_onnx = kwargs.pop("use_onnx", None)
load_connected_pipeline = kwargs.pop("load_connected_pipeline", False)
trust_remote_code = kwargs.pop("trust_remote_code", False)
dduf = kwargs.pop("dduf", False)

allow_pickle = False
if use_safetensors is None:
Expand Down Expand Up @@ -1346,6 +1420,10 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
allow_patterns += [f"{custom_pipeline}.py"] if f"{custom_pipeline}.py" in filenames else []
# also allow downloading config.json files with the model
allow_patterns += [os.path.join(k, "config.json") for k in model_folder_names]
# also allow downloading the dduf
# TODO: check that the file actually exist
if dduf is not None:
allow_patterns += [dduf]
SunMarc marked this conversation as resolved.
Show resolved Hide resolved

allow_patterns += [
SCHEDULER_CONFIG_NAME,
Expand Down
Loading