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
Show file tree
Hide file tree
Changes from 10 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
32 changes: 24 additions & 8 deletions src/diffusers/configuration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def load_config(
_ = kwargs.pop("mirror", None)
subfolder = kwargs.pop("subfolder", None)
user_agent = kwargs.pop("user_agent", {})
dduf_reader = kwargs.pop("dduf_reader", None)
sayakpaul marked this conversation as resolved.
Show resolved Hide resolved

user_agent = {**user_agent, "file_type": "config"}
user_agent = http_user_agent(user_agent)
Expand All @@ -358,8 +359,22 @@ def load_config(
"`self.config_name` is not defined. Note that one should not load a config from "
"`ConfigMixin`. Please make sure to define `config_name` in a class inheriting from `ConfigMixin`"
)

if os.path.isfile(pretrained_model_name_or_path):
# Custom path for now
if dduf_reader:
if subfolder is not None:
if dduf_reader.has_file(os.path.join(pretrained_model_name_or_path, subfolder, cls.config_name)):
config_file = os.path.join(subfolder, cls.config_name)
else:
raise ValueError(
f"We did not manage to find the file {os.path.join(pretrained_model_name_or_path, subfolder, cls.config_name)} in the archive. We only have the following files {dduf_reader.files}"
)
elif dduf_reader.has_file(os.path.join(pretrained_model_name_or_path, cls.config_name)):
config_file = os.path.join(pretrained_model_name_or_path, cls.config_name)
else:
raise ValueError(
f"We did not manage to find the file {os.path.join(pretrained_model_name_or_path, cls.config_name)} in the archive. We only have the following files {dduf_reader.files}"
)
elif os.path.isfile(pretrained_model_name_or_path):
config_file = pretrained_model_name_or_path
elif os.path.isdir(pretrained_model_name_or_path):
if subfolder is not None and os.path.isfile(
Expand Down Expand Up @@ -426,10 +441,8 @@ def load_config(
f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory "
f"containing a {cls.config_name} file"
)

try:
# Load config dict
config_dict = cls._dict_from_json_file(config_file)
config_dict = cls._dict_from_json_file(config_file, dduf_reader=dduf_reader)

commit_hash = extract_commit_hash(config_file)
except (json.JSONDecodeError, UnicodeDecodeError):
Expand Down Expand Up @@ -552,9 +565,12 @@ def extract_init_dict(cls, config_dict, **kwargs):
return init_dict, unused_kwargs, hidden_config_dict

@classmethod
def _dict_from_json_file(cls, json_file: Union[str, os.PathLike]):
with open(json_file, "r", encoding="utf-8") as reader:
text = reader.read()
def _dict_from_json_file(cls, json_file: Union[str, os.PathLike], dduf_reader=None):
SunMarc marked this conversation as resolved.
Show resolved Hide resolved
if dduf_reader:
text = dduf_reader.read_file(json_file, encoding="utf-8")
else:
with open(json_file, "r", encoding="utf-8") as reader:
text = reader.read()
return json.loads(text)

def __repr__(self):
Expand Down
13 changes: 11 additions & 2 deletions src/diffusers/models/model_loading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _fetch_remapped_cls_from_config(config, old_class):
return old_class


def load_state_dict(checkpoint_file: Union[str, os.PathLike], variant: Optional[str] = None):
def load_state_dict(checkpoint_file: Union[str, os.PathLike], variant: Optional[str] = None, dduf_reader=None):
"""
Reads a checkpoint file, returning properly formatted errors if they arise.
"""
Expand All @@ -138,8 +138,15 @@ def load_state_dict(checkpoint_file: Union[str, os.PathLike], variant: Optional[
return checkpoint_file
try:
file_extension = os.path.basename(checkpoint_file).split(".")[-1]
if dduf_reader:
checkpoint_file = dduf_reader.read_file(checkpoint_file)
if file_extension == SAFETENSORS_FILE_EXTENSION:
return safetensors.torch.load_file(checkpoint_file, device="cpu")
if dduf_reader:
# tensors are loaded on cpu
return safetensors.torch.load(checkpoint_file)
else:
return safetensors.torch.load_file(checkpoint_file, device="cpu")

else:
weights_only_kwarg = {"weights_only": True} if is_torch_version(">=", "1.13") else {}
return torch.load(
Expand Down Expand Up @@ -272,6 +279,7 @@ def _fetch_index_file(
revision,
user_agent,
commit_hash,
dduf_reader=None,
):
if is_local:
index_file = Path(
Expand All @@ -297,6 +305,7 @@ def _fetch_index_file(
subfolder=None,
user_agent=user_agent,
commit_hash=commit_hash,
dduf_reader=dduf_reader,
)
index_file = Path(index_file)
except (EntryNotFoundError, EnvironmentError):
Expand Down
12 changes: 9 additions & 3 deletions src/diffusers/models/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
variant = kwargs.pop("variant", None)
use_safetensors = kwargs.pop("use_safetensors", None)
quantization_config = kwargs.pop("quantization_config", None)
dduf_reader = kwargs.pop("dduf_reader", None)

allow_pickle = False
if use_safetensors is None:
Expand Down Expand Up @@ -649,6 +650,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
revision=revision,
subfolder=subfolder,
user_agent=user_agent,
dduf_reader=dduf_reader,
**kwargs,
)
# no in-place modification of the original config.
Expand Down Expand Up @@ -724,6 +726,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
"revision": revision,
"user_agent": user_agent,
"commit_hash": commit_hash,
"dduf_reader": dduf_reader,
}
index_file = _fetch_index_file(**index_file_kwargs)
# In case the index file was not found we still have to consider the legacy format.
Expand Down Expand Up @@ -759,7 +762,8 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P

model = load_flax_checkpoint_in_pytorch_model(model, model_file)
else:
if is_sharded:
# in the case it is sharded, we have already the index
if is_sharded and not dduf_reader:
sharded_ckpt_cached_folder, sharded_metadata = _get_checkpoint_shard_files(
pretrained_model_name_or_path,
index_file,
Expand Down Expand Up @@ -790,6 +794,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
subfolder=subfolder,
user_agent=user_agent,
commit_hash=commit_hash,
dduf_reader=dduf_reader,
)

except IOError as e:
Expand All @@ -813,6 +818,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
subfolder=subfolder,
user_agent=user_agent,
commit_hash=commit_hash,
dduf_reader=dduf_reader,
)

if low_cpu_mem_usage:
Expand All @@ -837,7 +843,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
# TODO (sayakpaul, SunMarc): remove this after model loading refactor
elif is_quant_method_bnb:
param_device = torch.cuda.current_device()
state_dict = load_state_dict(model_file, variant=variant)
state_dict = load_state_dict(model_file, variant=variant, dduf_reader=dduf_reader)
model._convert_deprecated_attention_blocks(state_dict)

# move the params from meta device to cpu
Expand Down Expand Up @@ -937,7 +943,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
else:
model = cls.from_config(config, **unused_kwargs)

state_dict = load_state_dict(model_file, variant=variant)
state_dict = load_state_dict(model_file, variant=variant, dduf_reader=dduf_reader)
model._convert_deprecated_attention_blocks(state_dict)

model, missing_keys, unexpected_keys, mismatched_keys, error_msgs = cls._load_pretrained_model(
Expand Down
6 changes: 5 additions & 1 deletion src/diffusers/pipelines/pipeline_loading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ def load_sub_model(
low_cpu_mem_usage: bool,
cached_folder: Union[str, os.PathLike],
use_safetensors: bool,
dduf_reader,
):
"""Helper method to load the module `name` from `library_name` and `class_name`"""

Expand Down Expand Up @@ -721,7 +722,10 @@ def load_sub_model(
loading_kwargs["low_cpu_mem_usage"] = False

# check if the module is in a subdirectory
if os.path.isdir(os.path.join(cached_folder, name)):
if dduf_reader:
loading_kwargs["dduf_reader"] = dduf_reader
loaded_sub_model = load_method(name, **loading_kwargs)
elif os.path.isdir(os.path.join(cached_folder, name)):
loaded_sub_model = load_method(os.path.join(cached_folder, name), **loading_kwargs)
else:
# else load from the root directory
Expand Down
Loading
Loading