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
25 changes: 0 additions & 25 deletions src/diffusers/loaders/ip_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
_get_model_file,
_is_valid_type,
is_accelerate_available,
is_torch_version,
is_transformers_available,
is_transformers_version,
logging,
Expand Down Expand Up @@ -152,12 +151,6 @@ def load_ip_adapter(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)
Comment on lines -155 to -159

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!


user_agent = {"file_type": "attn_procs_weights", "framework": "pytorch"}
state_dicts = []
for pretrained_model_name_or_path_or_dict, weight_name, subfolder in zip(
Expand Down Expand Up @@ -451,12 +444,6 @@ def load_ip_adapter(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

user_agent = {
"file_type": "attn_procs_weights",
"framework": "pytorch",
Expand Down Expand Up @@ -711,12 +698,6 @@ def load_ip_adapter(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

user_agent = {"file_type": "attn_procs_weights", "framework": "pytorch"}
state_dicts = []
for pretrained_model_name_or_path_or_dict, weight_name, subfolder in zip(
Expand Down Expand Up @@ -996,12 +977,6 @@ def load_ip_adapter(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

user_agent = {"file_type": "attn_procs_weights", "framework": "pytorch"}

if not isinstance(pretrained_model_name_or_path_or_dict, dict):
Expand Down
36 changes: 4 additions & 32 deletions src/diffusers/loaders/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import torch
from huggingface_hub import snapshot_download
from huggingface_hub.utils import LocalEntryNotFoundError, validate_hf_hub_args
from packaging import version
from typing_extensions import Self

from ..utils import _maybe_remap_relocated_pipeline_class, deprecate, is_transformers_available, logging
Expand All @@ -45,7 +44,6 @@
SINGLE_FILE_OPTIONAL_COMPONENTS = ["safety_checker"]

if is_transformers_available():
import transformers
from transformers import PreTrainedModel, PreTrainedTokenizer


Expand Down Expand Up @@ -77,21 +75,8 @@ def load_single_file_sub_model(
library = importlib.import_module(library_name)
class_obj = getattr(library, class_name)

if is_transformers_available():
transformers_version = version.parse(version.parse(transformers.__version__).base_version)
else:
transformers_version = "N/A"

is_transformers_model = (
is_transformers_available()
and issubclass(class_obj, PreTrainedModel)
and transformers_version >= version.parse("4.20.0")
)
is_tokenizer = (
is_transformers_available()
and issubclass(class_obj, PreTrainedTokenizer)
and transformers_version >= version.parse("4.20.0")
)
is_transformers_model = is_transformers_available() and issubclass(class_obj, PreTrainedModel)
is_tokenizer = is_transformers_available() and issubclass(class_obj, PreTrainedTokenizer)

diffusers_module = importlib.import_module(__name__.split(".")[0])
is_diffusers_single_file_model = issubclass(class_obj, diffusers_module.FromOriginalModelMixin)
Expand Down Expand Up @@ -188,26 +173,13 @@ def _map_component_types_to_config_dict(component_types):
config_dict = {}
component_types.pop("self", None)

if is_transformers_available():
transformers_version = version.parse(version.parse(transformers.__version__).base_version)
else:
transformers_version = "N/A"

for component_name, component_value in component_types.items():
is_diffusers_model = issubclass(component_value[0], diffusers_module.ModelMixin)
is_scheduler_enum = component_value[0].__name__ == "KarrasDiffusionSchedulers"
is_scheduler = issubclass(component_value[0], diffusers_module.SchedulerMixin)

is_transformers_model = (
is_transformers_available()
and issubclass(component_value[0], PreTrainedModel)
and transformers_version >= version.parse("4.20.0")
)
is_transformers_tokenizer = (
is_transformers_available()
and issubclass(component_value[0], PreTrainedTokenizer)
and transformers_version >= version.parse("4.20.0")
)
is_transformers_model = is_transformers_available() and issubclass(component_value[0], PreTrainedModel)
is_transformers_tokenizer = is_transformers_available() and issubclass(component_value[0], PreTrainedTokenizer)

if is_diffusers_model and component_name not in SINGLE_FILE_OPTIONAL_COMPONENTS:
config_dict[component_name] = ["diffusers", component_value[0].__name__]
Expand Down
4 changes: 2 additions & 2 deletions src/diffusers/loaders/single_file_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_expand_device_map,
)
from ..quantizers import DiffusersAutoQuantizer
from ..utils import deprecate, is_accelerate_available, is_torch_version, logging
from ..utils import deprecate, is_accelerate_available, logging
from ..utils.torch_utils import empty_device_cache
from .single_file_utils import (
SingleFileComponentError,
Expand Down Expand Up @@ -80,7 +80,7 @@

from ..models.model_loading_utils import load_model_dict_into_meta

if is_torch_version(">=", "1.9.0") and is_accelerate_available():
if is_accelerate_available():
_LOW_CPU_MEM_USAGE_DEFAULT = True
else:
_LOW_CPU_MEM_USAGE_DEFAULT = False
Expand Down
14 changes: 1 addition & 13 deletions src/diffusers/loaders/transformer_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from ..models.model_loading_utils import load_model_dict_into_meta
from ..models.modeling_utils import _LOW_CPU_MEM_USAGE_DEFAULT
from ..utils import is_accelerate_available, is_torch_version, logging
from ..utils import is_accelerate_available, logging
from ..utils.torch_utils import empty_device_cache


Expand Down Expand Up @@ -48,12 +48,6 @@ def _convert_ip_adapter_image_proj_to_diffusers(self, state_dict, low_cpu_mem_us
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

updated_state_dict = {}
image_projection = None
init_context = init_empty_weights if low_cpu_mem_usage else nullcontext
Expand Down Expand Up @@ -102,12 +96,6 @@ def _convert_ip_adapter_attn_to_diffusers(self, state_dicts, low_cpu_mem_usage=_
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

# set ip-adapter cross-attention processors & load state_dict
attn_procs = {}
key_id = 0
Expand Down
14 changes: 1 addition & 13 deletions src/diffusers/loaders/transformer_sd3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..models.embeddings import IPAdapterTimeImageProjection
from ..models.model_loading_utils import load_model_dict_into_meta
from ..models.modeling_utils import _LOW_CPU_MEM_USAGE_DEFAULT
from ..utils import is_accelerate_available, is_torch_version, logging
from ..utils import is_accelerate_available, logging
from ..utils.torch_utils import empty_device_cache


Expand All @@ -43,12 +43,6 @@ def _convert_ip_adapter_attn_to_diffusers(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

# IP-Adapter cross attention parameters
hidden_size = self.config.attention_head_dim * self.config.num_attention_heads
ip_hidden_states_dim = self.config.attention_head_dim * self.config.num_attention_heads
Expand Down Expand Up @@ -101,12 +95,6 @@ def _convert_ip_adapter_image_proj_to_diffusers(
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

init_context = init_empty_weights if low_cpu_mem_usage else nullcontext

# Convert to diffusers
Expand Down
13 changes: 0 additions & 13 deletions src/diffusers/loaders/unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from ..utils import (
_get_model_file,
is_accelerate_available,
is_torch_version,
logging,
)
from ..utils.torch_utils import empty_device_cache
Expand Down Expand Up @@ -390,12 +389,6 @@ def _convert_ip_adapter_image_proj_to_diffusers(self, state_dict, low_cpu_mem_us
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

updated_state_dict = {}
image_projection = None
init_context = init_empty_weights if low_cpu_mem_usage else nullcontext
Expand Down Expand Up @@ -615,12 +608,6 @@ def _convert_ip_adapter_attn_to_diffusers(self, state_dicts, low_cpu_mem_usage=_
" install accelerate\n```\n."
)

if low_cpu_mem_usage is True and not is_torch_version(">=", "1.9.0"):
raise NotImplementedError(
"Low memory initialization requires torch >= 1.9.0. Please either update your PyTorch version or set"
" `low_cpu_mem_usage=False`."
)

# set ip-adapter cross-attention processors & load state_dict
attn_procs = {}
key_id = 1
Expand Down
8 changes: 1 addition & 7 deletions src/diffusers/models/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from torch import nn

from ..utils import deprecate
from ..utils.import_utils import is_torch_npu_available, is_torch_version
from ..utils.import_utils import is_torch_npu_available


if is_torch_npu_available():
Expand Down Expand Up @@ -79,9 +79,6 @@ def __init__(self, dim_in: int, dim_out: int, approximate: str = "none", bias: b
self.approximate = approximate

def gelu(self, gate: torch.Tensor) -> torch.Tensor:
if gate.device.type == "mps" and is_torch_version("<", "2.0.0"):
# fp16 gelu not supported on mps before torch 2.0
return F.gelu(gate.to(dtype=torch.float32), approximate=self.approximate).to(dtype=gate.dtype)
return F.gelu(gate, approximate=self.approximate)

def forward(self, hidden_states) -> torch.Tensor:
Expand All @@ -105,9 +102,6 @@ def __init__(self, dim_in: int, dim_out: int, bias: bool = True):
self.proj = nn.Linear(dim_in, dim_out * 2, bias=bias)

def gelu(self, gate: torch.Tensor) -> torch.Tensor:
if gate.device.type == "mps" and is_torch_version("<", "2.0.0"):
# fp16 gelu not supported on mps before torch 2.0
return F.gelu(gate.to(dtype=torch.float32)).to(dtype=gate.dtype)
return F.gelu(gate)

def forward(self, hidden_states, *args, **kwargs) -> torch.Tensor:
Expand Down
22 changes: 2 additions & 20 deletions src/diffusers/models/attention_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,8 @@
else:
xops = None

# Version guard for PyTorch compatibility - custom_op was added in PyTorch 2.4
if torch.__version__ >= "2.4.0":
_custom_op = torch.library.custom_op
_register_fake = torch.library.register_fake
else:

def custom_op_no_op(name, fn=None, /, *, mutates_args, device_types=None, schema=None):
def wrap(func):
return func

return wrap if fn is None else fn

def register_fake_no_op(op, fn=None, /, *, lib=None, _stacklevel=1):
def wrap(func):
return func

return wrap if fn is None else fn

_custom_op = custom_op_no_op
_register_fake = register_fake_no_op
_custom_op = torch.library.custom_op
_register_fake = torch.library.register_fake


# TODO(aryan): Add support for the following:
Expand Down
14 changes: 1 addition & 13 deletions src/diffusers/models/attention_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ..image_processor import IPAdapterMaskProcessor
from ..utils import deprecate, is_torch_xla_available, logging
from ..utils.import_utils import is_torch_npu_available, is_torch_xla_version, is_xformers_available
from ..utils.torch_utils import is_torch_version, maybe_allow_in_graph
from ..utils.torch_utils import maybe_allow_in_graph


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down Expand Up @@ -2087,12 +2087,6 @@ def __call__(
class AuraFlowAttnProcessor2_0:
"""Attention processor used typically in processing Aura Flow."""

def __init__(self):
if not hasattr(F, "scaled_dot_product_attention") and is_torch_version("<", "2.1"):
raise ImportError(
"AuraFlowAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to at least 2.1 or above as we use `scale` in `F.scaled_dot_product_attention()`. "
)

def __call__(
self,
attn: Attention,
Expand Down Expand Up @@ -2180,12 +2174,6 @@ def __call__(
class FusedAuraFlowAttnProcessor2_0:
"""Attention processor used typically in processing Aura Flow with fused projections."""

def __init__(self):
if not hasattr(F, "scaled_dot_product_attention") and is_torch_version("<", "2.1"):
raise ImportError(
"FusedAuraFlowAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to at least 2.1 or above as we use `scale` in `F.scaled_dot_product_attention()`. "
)

def __call__(
self,
attn: Attention,
Expand Down
Loading
Loading