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
4 changes: 3 additions & 1 deletion src/diffusers/loaders/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs) -> Self:
- A link to the `.ckpt` file (for example
`"https://huggingface.co/<repo_id>/blob/main/<path_to_file>.ckpt"`) on the Hub.
- A path to a *file* containing all pipeline weights.
torch_dtype (`str` or `torch.dtype`, *optional*):
dtype (`str` or `torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -361,6 +361,8 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs) -> Self:
local_files_only = kwargs.pop("local_files_only", False)
revision = kwargs.pop("revision", None)
torch_dtype = kwargs.pop("torch_dtype", None)
dtype = kwargs.pop("dtype", None)
torch_dtype = dtype if dtype is not None else torch_dtype
disable_mmap = kwargs.pop("disable_mmap", False)

is_legacy_loading = False
Expand Down
4 changes: 3 additions & 1 deletion src/diffusers/loaders/single_file_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def from_single_file(cls, pretrained_model_link_or_path_or_dict: str | None = No
original_config (`str`, *optional*):
Dict or path to a yaml file containing the configuration for the model in its original format.
If a dict is provided, it will be used to initialize the model configuration.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -351,6 +351,8 @@ def from_single_file(cls, pretrained_model_link_or_path_or_dict: str | None = No
revision = kwargs.pop("revision", None)
config_revision = kwargs.pop("config_revision", None)
torch_dtype = kwargs.pop("torch_dtype", None)
dtype = kwargs.pop("dtype", None)
torch_dtype = dtype if dtype is not None else torch_dtype
quantization_config = kwargs.pop("quantization_config", None)
low_cpu_mem_usage = kwargs.pop("low_cpu_mem_usage", _LOW_CPU_MEM_USAGE_DEFAULT)
device = kwargs.pop("device", None)
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/models/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def from_pretrained(cls, pretrained_model_path: str | os.PathLike | None, **kwar
pretrained_model_path (`os.PathLike`):
A path to a *directory* containing model weights saved using
[`~diffusers.models.adapter.MultiAdapter.save_pretrained`], e.g., `./my_model_directory/adapter`.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model under this dtype.
output_loading_info(`bool`, *optional*, defaults to `False`):
Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/models/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def from_pretrained(cls, pretrained_model_or_path: str | os.PathLike | None = No
cache_dir (`str | os.PathLike`, *optional*):
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
is not used.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/models/controlnets/multicontrolnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def from_pretrained(cls, pretrained_model_path: str | os.PathLike | None, **kwar
A path to a *directory* containing model weights saved using
[`~models.controlnets.multicontrolnet.MultiControlNetModel.save_pretrained`], e.g.,
`./my_model_directory/controlnet`.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model under this dtype.
output_loading_info(`bool`, *optional*, defaults to `False`):
Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/models/controlnets/multicontrolnet_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def from_pretrained(cls, pretrained_model_path: str | os.PathLike | None, **kwar
A path to a *directory* containing model weights saved using
[`~models.controlnets.multicontrolnet.MultiControlNetUnionModel.save_pretrained`], e.g.,
`./my_model_directory/controlnet`.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model under this dtype.
output_loading_info(`bool`, *optional*, defaults to `False`):
Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
Expand Down
4 changes: 3 additions & 1 deletion src/diffusers/models/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike | None
cache_dir (`str | os.PathLike`, *optional*):
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
is not used.
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -1018,6 +1018,8 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike | None
token = kwargs.pop("token", None)
revision = kwargs.pop("revision", None)
torch_dtype = kwargs.pop("torch_dtype", None)
dtype = kwargs.pop("dtype", None)
torch_dtype = dtype if dtype is not None else torch_dtype
subfolder = kwargs.pop("subfolder", None)
device_map = kwargs.pop("device_map", None)
max_memory = kwargs.pop("max_memory", None)
Expand Down
3 changes: 2 additions & 1 deletion src/diffusers/modular_pipelines/modular_pipeline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,11 @@ def load(self, **kwargs) -> Any:

from diffusers import AutoModel

# `torch_dtype` is not an accepted parameter for tokenizers and processors.
# `dtype`/`torch_dtype` is not an accepted parameter for tokenizers and processors.
# As a result, it gets stored in `init_kwargs`, which are written to the config
# during save. This causes JSON serialization to fail when saving the component.
if self.type_hint is not None and not issubclass(self.type_hint, (torch.nn.Module, AutoModel)):
kwargs.pop("dtype", None)
kwargs.pop("torch_dtype", None)

if self.type_hint is None:
Expand Down
8 changes: 4 additions & 4 deletions src/diffusers/pipelines/auto_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def from_pretrained(cls, pretrained_model_or_path, **kwargs):
- A path to a *directory* (for example `./my_pipeline_directory/`) containing pipeline weights
saved using
[`~DiffusionPipeline.save_pretrained`].
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -717,7 +717,7 @@ def from_pretrained(cls, pretrained_model_or_path, **kwargs):
- A path to a *directory* (for example `./my_pipeline_directory/`) containing pipeline weights
saved using
[`~DiffusionPipeline.save_pretrained`].
torch_dtype (`str` or `torch.dtype`, *optional*):
dtype (`str` or `torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -1022,7 +1022,7 @@ def from_pretrained(cls, pretrained_model_or_path, **kwargs):
- A path to a *directory* (for example `./my_pipeline_directory/`) containing pipeline weights
saved using
[`~DiffusionPipeline.save_pretrained`].
torch_dtype (`str` or `torch.dtype`, *optional*):
dtype (`str` or `torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down Expand Up @@ -1312,7 +1312,7 @@ def from_pretrained(cls, pretrained_model_or_path, **kwargs):
- A path to a *directory* (for example `./my_pipeline_directory/`) containing pipeline weights
saved using
[`~DiffusionPipeline.save_pretrained`].
torch_dtype (`torch.dtype`, *optional*):
dtype (`torch.dtype`, *optional*):
Override the default `torch.dtype` and load the model with another dtype.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
Expand Down
8 changes: 6 additions & 2 deletions src/diffusers/pipelines/pipeline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike, **kwa
saved using
[`~DiffusionPipeline.save_pretrained`].
- A path to a *directory* (for example `./my_pipeline_directory/`) containing a dduf file
torch_dtype (`torch.dtype` or `dict[str, Union[str, torch.dtype]]`, *optional*):
dtype (`torch.dtype` or `dict[str, Union[str, torch.dtype]]`, *optional*):
Override the default `torch.dtype` and load the model with another dtype. To load submodels with
different dtype pass a `dict` (for example `{'transformer': torch.bfloat16, 'vae': torch.float16}`).
Set the default dtype for unspecified components with `default` (for example `{'transformer':
Expand Down Expand Up @@ -769,6 +769,8 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike, **kwa
revision = kwargs.pop("revision", None)
from_flax = kwargs.pop("from_flax", False)
torch_dtype = kwargs.pop("torch_dtype", None)
dtype = kwargs.pop("dtype", None)
torch_dtype = dtype if dtype is not None else torch_dtype
custom_pipeline = kwargs.pop("custom_pipeline", None)
custom_revision = kwargs.pop("custom_revision", None)
provider = kwargs.pop("provider", None)
Expand Down Expand Up @@ -2121,7 +2123,9 @@ def from_pipe(cls, pipeline, **kwargs):
"""

original_config = dict(pipeline.config)
torch_dtype = kwargs.pop("torch_dtype", torch.float32)
torch_dtype = kwargs.pop("torch_dtype", None)
dtype = kwargs.pop("dtype", None)
torch_dtype = dtype if dtype is not None else (torch_dtype if torch_dtype is not None else torch.float32)
trust_remote_code = kwargs.pop("trust_remote_code", False)

# derive the pipeline class to instantiate
Expand Down
15 changes: 15 additions & 0 deletions tests/models/testing_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ def test_from_save_pretrained_dtype(self, tmp_path, dtype):
new_model = self.model_class.from_pretrained(tmp_path, low_cpu_mem_usage=False, torch_dtype=dtype)
assert new_model.dtype == dtype

@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=["fp32", "fp16", "bf16"])
def test_from_pretrained_dtype_alias(self, tmp_path, dtype):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
if torch_device == "mps" and dtype == torch.bfloat16:
pytest.skip(reason=f"{dtype} is not supported on {torch_device}")

model = self.model_class(**self.get_init_dict())
model.to(torch_device)
model.eval()

model.to(dtype)
model.save_pretrained(tmp_path)
new_model = self.model_class.from_pretrained(tmp_path, low_cpu_mem_usage=True, dtype=dtype)
assert new_model.dtype == dtype

@torch.no_grad()
def test_determinism(self, atol=1e-5, rtol=0):
model = self.model_class(**self.get_init_dict())
Expand Down
21 changes: 21 additions & 0 deletions tests/pipelines/cosmos/test_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,27 @@ def test_torch_dtype_dict(self):
f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}",
)

def test_dtype_alias(self):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname:
pipe.save_pretrained(tmpdirname, safe_serialization=False)
loaded_pipe = self.pipeline_class.from_pretrained(
tmpdirname, safety_checker=DummyCosmosSafetyChecker(), dtype=torch.float16
)

for name, component in loaded_pipe.components.items():
if name == "safety_checker":
continue
if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"):
self.assertEqual(
component.dtype,
torch.float16,
f"Component '{name}' has dtype {component.dtype} but expected {torch.float16}",
)

@unittest.skip(
"The pipeline should not be runnable without a safety checker. The test creates a pipeline without passing in "
"a safety checker, which makes the pipeline default to the actual Cosmos Guardrail. The Cosmos Guardrail is "
Expand Down
21 changes: 21 additions & 0 deletions tests/pipelines/cosmos/test_cosmos2_5_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,27 @@ def test_torch_dtype_dict(self):
f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}",
)

def test_dtype_alias(self):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname:
pipe.save_pretrained(tmpdirname, safe_serialization=False)
loaded_pipe = self.pipeline_class.from_pretrained(
tmpdirname, safety_checker=DummyCosmosSafetyChecker(), dtype=torch.float16
)

for name, component in loaded_pipe.components.items():
if name == "safety_checker":
continue
if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"):
self.assertEqual(
component.dtype,
torch.float16,
f"Component '{name}' has dtype {component.dtype} but expected {torch.float16}",
)

@unittest.skip(
"The pipeline should not be runnable without a safety checker. The test creates a pipeline without passing in "
"a safety checker, which makes the pipeline default to the actual Cosmos Guardrail. The Cosmos Guardrail is "
Expand Down
22 changes: 22 additions & 0 deletions tests/pipelines/cosmos/test_cosmos2_5_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,28 @@ def test_torch_dtype_dict(self):
f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}",
)

def test_dtype_alias(self):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname:
pipe.save_pretrained(tmpdirname, safe_serialization=False)
loaded_pipe = self.pipeline_class.from_pretrained(
tmpdirname, safety_checker=DummyCosmosSafetyChecker(), dtype=torch.float16
)

for name, component in loaded_pipe.components.items():
# Skip components that are not loaded from disk or have special handling
if name == "safety_checker":
continue
if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"):
self.assertEqual(
component.dtype,
torch.float16,
f"Component '{name}' has dtype {component.dtype} but expected {torch.float16}",
)

def test_save_load_optional_components(self, expected_max_difference=1e-4):
self.pipeline_class._optional_components.remove("safety_checker")
super().test_save_load_optional_components(expected_max_difference=expected_max_difference)
Expand Down
21 changes: 21 additions & 0 deletions tests/pipelines/cosmos/test_cosmos2_text2image.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ def test_torch_dtype_dict(self):
f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}",
)

def test_dtype_alias(self):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname:
pipe.save_pretrained(tmpdirname, safe_serialization=False)
loaded_pipe = self.pipeline_class.from_pretrained(
tmpdirname, safety_checker=DummyCosmosSafetyChecker(), dtype=torch.float16
)

for name, component in loaded_pipe.components.items():
if name == "safety_checker":
continue
if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"):
self.assertEqual(
component.dtype,
torch.float16,
f"Component '{name}' has dtype {component.dtype} but expected {torch.float16}",
)

@unittest.skip(
"The pipeline should not be runnable without a safety checker. The test creates a pipeline without passing in "
"a safety checker, which makes the pipeline default to the actual Cosmos Guardrail. The Cosmos Guardrail is "
Expand Down
21 changes: 21 additions & 0 deletions tests/pipelines/cosmos/test_cosmos2_video2world.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,27 @@ def test_torch_dtype_dict(self):
f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}",
)

def test_dtype_alias(self):
# `dtype` is an alias for `torch_dtype` in `from_pretrained`.
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname:
pipe.save_pretrained(tmpdirname, safe_serialization=False)
loaded_pipe = self.pipeline_class.from_pretrained(
tmpdirname, safety_checker=DummyCosmosSafetyChecker(), dtype=torch.float16
)

for name, component in loaded_pipe.components.items():
if name == "safety_checker":
continue
if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"):
self.assertEqual(
component.dtype,
torch.float16,
f"Component '{name}' has dtype {component.dtype} but expected {torch.float16}",
)

@unittest.skip(
"The pipeline should not be runnable without a safety checker. The test creates a pipeline without passing in "
"a safety checker, which makes the pipeline default to the actual Cosmos Guardrail. The Cosmos Guardrail is "
Expand Down
Loading
Loading