Skip to content

Commit 2ab1704

Browse files
Temp Revert "[Core] better support offloading when side loading is enabled… (#4927)
Revert "[Core] better support offloading when side loading is enabled. (#4855)" This reverts commit e4b8e79.
1 parent 914c513 commit 2ab1704

File tree

8 files changed

+1
-278
lines changed

8 files changed

+1
-278
lines changed

src/diffusers/loaders.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
if is_accelerate_available():
4747
from accelerate import init_empty_weights
48-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
4948
from accelerate.utils import set_module_tensor_to_device
5049

5150
logger = logging.get_logger(__name__)
@@ -779,21 +778,6 @@ def load_textual_inversion(
779778
f" `{self.load_textual_inversion.__name__}`"
780779
)
781780

782-
# Remove any existing hooks.
783-
is_model_cpu_offload = False
784-
is_sequential_cpu_offload = False
785-
recursive = False
786-
for _, component in self.components.items():
787-
if isinstance(component, nn.Module):
788-
if hasattr(component, "_hf_hook"):
789-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
790-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
791-
logger.info(
792-
"Accelerate hooks detected. Since you have called `load_textual_inversion()`, the previous hooks will be first removed. Then the textual inversion parameters will be loaded and the hooks will be applied again."
793-
)
794-
recursive = is_sequential_cpu_offload
795-
remove_hook_from_module(component, recurse=recursive)
796-
797781
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
798782
force_download = kwargs.pop("force_download", False)
799783
resume_download = kwargs.pop("resume_download", False)
@@ -947,12 +931,6 @@ def load_textual_inversion(
947931
for token_id, embedding in token_ids_and_embeddings:
948932
text_encoder.get_input_embeddings().weight.data[token_id] = embedding
949933

950-
# offload back
951-
if is_model_cpu_offload:
952-
self.enable_model_cpu_offload()
953-
elif is_sequential_cpu_offload:
954-
self.enable_sequential_cpu_offload()
955-
956934

957935
class LoraLoaderMixin:
958936
r"""
@@ -984,21 +962,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
984962
kwargs (`dict`, *optional*):
985963
See [`~loaders.LoraLoaderMixin.lora_state_dict`].
986964
"""
987-
# Remove any existing hooks.
988-
is_model_cpu_offload = False
989-
is_sequential_cpu_offload = False
990-
recurive = False
991-
for _, component in self.components.items():
992-
if isinstance(component, nn.Module):
993-
if hasattr(component, "_hf_hook"):
994-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
995-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
996-
logger.info(
997-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
998-
)
999-
recurive = is_sequential_cpu_offload
1000-
remove_hook_from_module(component, recurse=recurive)
1001-
1002965
state_dict, network_alphas = self.lora_state_dict(pretrained_model_name_or_path_or_dict, **kwargs)
1003966
self.load_lora_into_unet(state_dict, network_alphas=network_alphas, unet=self.unet)
1004967
self.load_lora_into_text_encoder(
@@ -1008,12 +971,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
1008971
lora_scale=self.lora_scale,
1009972
)
1010973

1011-
# Offload back.
1012-
if is_model_cpu_offload:
1013-
self.enable_model_cpu_offload()
1014-
elif is_sequential_cpu_offload:
1015-
self.enable_sequential_cpu_offload()
1016-
1017974
@classmethod
1018975
def lora_state_dict(
1019976
cls,

src/diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,26 +1549,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
15491549
# We could have accessed the unet config from `lora_state_dict()` too. We pass
15501550
# it here explicitly to be able to tell that it's coming from an SDXL
15511551
# pipeline.
1552-
1553-
# Remove any existing hooks.
1554-
if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0"):
1555-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
1556-
else:
1557-
raise ImportError("Offloading requires `accelerate v0.17.0` or higher.")
1558-
1559-
is_model_cpu_offload = False
1560-
is_sequential_cpu_offload = False
1561-
recursive = False
1562-
for _, component in self.components.items():
1563-
if isinstance(component, torch.nn.Module):
1564-
if hasattr(component, "_hf_hook"):
1565-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
1566-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
1567-
logger.info(
1568-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
1569-
)
1570-
recursive = is_sequential_cpu_offload
1571-
remove_hook_from_module(component, recurse=recursive)
15721552
state_dict, network_alphas = self.lora_state_dict(
15731553
pretrained_model_name_or_path_or_dict,
15741554
unet_config=self.unet.config,
@@ -1596,12 +1576,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
15961576
lora_scale=self.lora_scale,
15971577
)
15981578

1599-
# Offload back.
1600-
if is_model_cpu_offload:
1601-
self.enable_model_cpu_offload()
1602-
elif is_sequential_cpu_offload:
1603-
self.enable_sequential_cpu_offload()
1604-
16051579
@classmethod
16061580
# Copied from diffusers.pipelines.stable_diffusion_xl.pipeline_stable_diffusion_xl.StableDiffusionXLPipeline.save_lora_weights
16071581
def save_lora_weights(

src/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,26 +1216,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
12161216
# We could have accessed the unet config from `lora_state_dict()` too. We pass
12171217
# it here explicitly to be able to tell that it's coming from an SDXL
12181218
# pipeline.
1219-
1220-
# Remove any existing hooks.
1221-
if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0"):
1222-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
1223-
else:
1224-
raise ImportError("Offloading requires `accelerate v0.17.0` or higher.")
1225-
1226-
is_model_cpu_offload = False
1227-
is_sequential_cpu_offload = False
1228-
recursive = False
1229-
for _, component in self.components.items():
1230-
if isinstance(component, torch.nn.Module):
1231-
if hasattr(component, "_hf_hook"):
1232-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
1233-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
1234-
logger.info(
1235-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
1236-
)
1237-
recursive = is_sequential_cpu_offload
1238-
remove_hook_from_module(component, recurse=recursive)
12391219
state_dict, network_alphas = self.lora_state_dict(
12401220
pretrained_model_name_or_path_or_dict,
12411221
unet_config=self.unet.config,
@@ -1263,12 +1243,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
12631243
lora_scale=self.lora_scale,
12641244
)
12651245

1266-
# Offload back.
1267-
if is_model_cpu_offload:
1268-
self.enable_model_cpu_offload()
1269-
elif is_sequential_cpu_offload:
1270-
self.enable_sequential_cpu_offload()
1271-
12721246
@classmethod
12731247
# Copied from diffusers.pipelines.stable_diffusion_xl.pipeline_stable_diffusion_xl.StableDiffusionXLPipeline.save_lora_weights
12741248
def save_lora_weights(

src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -922,26 +922,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
922922
# We could have accessed the unet config from `lora_state_dict()` too. We pass
923923
# it here explicitly to be able to tell that it's coming from an SDXL
924924
# pipeline.
925-
926-
# Remove any existing hooks.
927-
if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0"):
928-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
929-
else:
930-
raise ImportError("Offloading requires `accelerate v0.17.0` or higher.")
931-
932-
is_model_cpu_offload = False
933-
is_sequential_cpu_offload = False
934-
recursive = False
935-
for _, component in self.components.items():
936-
if isinstance(component, torch.nn.Module):
937-
if hasattr(component, "_hf_hook"):
938-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
939-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
940-
logger.info(
941-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
942-
)
943-
recursive = is_sequential_cpu_offload
944-
remove_hook_from_module(component, recurse=recursive)
945925
state_dict, network_alphas = self.lora_state_dict(
946926
pretrained_model_name_or_path_or_dict,
947927
unet_config=self.unet.config,
@@ -969,12 +949,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
969949
lora_scale=self.lora_scale,
970950
)
971951

972-
# Offload back.
973-
if is_model_cpu_offload:
974-
self.enable_model_cpu_offload()
975-
elif is_sequential_cpu_offload:
976-
self.enable_sequential_cpu_offload()
977-
978952
@classmethod
979953
def save_lora_weights(
980954
self,

src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,26 +1072,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
10721072
# We could have accessed the unet config from `lora_state_dict()` too. We pass
10731073
# it here explicitly to be able to tell that it's coming from an SDXL
10741074
# pipeline.
1075-
1076-
# Remove any existing hooks.
1077-
if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0"):
1078-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
1079-
else:
1080-
raise ImportError("Offloading requires `accelerate v0.17.0` or higher.")
1081-
1082-
is_model_cpu_offload = False
1083-
is_sequential_cpu_offload = False
1084-
recursive = False
1085-
for _, component in self.components.items():
1086-
if isinstance(component, torch.nn.Module):
1087-
if hasattr(component, "_hf_hook"):
1088-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
1089-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
1090-
logger.info(
1091-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
1092-
)
1093-
recursive = is_sequential_cpu_offload
1094-
remove_hook_from_module(component, recurse=recursive)
10951075
state_dict, network_alphas = self.lora_state_dict(
10961076
pretrained_model_name_or_path_or_dict,
10971077
unet_config=self.unet.config,
@@ -1119,12 +1099,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
11191099
lora_scale=self.lora_scale,
11201100
)
11211101

1122-
# Offload back.
1123-
if is_model_cpu_offload:
1124-
self.enable_model_cpu_offload()
1125-
elif is_sequential_cpu_offload:
1126-
self.enable_sequential_cpu_offload()
1127-
11281102
@classmethod
11291103
# Copied from diffusers.pipelines.stable_diffusion_xl.pipeline_stable_diffusion_xl.StableDiffusionXLPipeline.save_lora_weights
11301104
def save_lora_weights(

src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,26 +1392,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
13921392
# We could have accessed the unet config from `lora_state_dict()` too. We pass
13931393
# it here explicitly to be able to tell that it's coming from an SDXL
13941394
# pipeline.
1395-
1396-
# Remove any existing hooks.
1397-
if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0"):
1398-
from accelerate.hooks import AlignDevicesHook, CpuOffload, remove_hook_from_module
1399-
else:
1400-
raise ImportError("Offloading requires `accelerate v0.17.0` or higher.")
1401-
1402-
is_model_cpu_offload = False
1403-
is_sequential_cpu_offload = False
1404-
recursive = False
1405-
for _, component in self.components.items():
1406-
if isinstance(component, torch.nn.Module):
1407-
if hasattr(component, "_hf_hook"):
1408-
is_model_cpu_offload = isinstance(getattr(component, "_hf_hook"), CpuOffload)
1409-
is_sequential_cpu_offload = isinstance(getattr(component, "_hf_hook"), AlignDevicesHook)
1410-
logger.info(
1411-
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
1412-
)
1413-
recursive = is_sequential_cpu_offload
1414-
remove_hook_from_module(component, recurse=recursive)
14151395
state_dict, network_alphas = self.lora_state_dict(
14161396
pretrained_model_name_or_path_or_dict,
14171397
unet_config=self.unet.config,
@@ -1439,12 +1419,6 @@ def load_lora_weights(self, pretrained_model_name_or_path_or_dict: Union[str, Di
14391419
lora_scale=self.lora_scale,
14401420
)
14411421

1442-
# Offload back.
1443-
if is_model_cpu_offload:
1444-
self.enable_model_cpu_offload()
1445-
elif is_sequential_cpu_offload:
1446-
self.enable_sequential_cpu_offload()
1447-
14481422
@classmethod
14491423
# Copied from diffusers.pipelines.stable_diffusion_xl.pipeline_stable_diffusion_xl.StableDiffusionXLPipeline.save_lora_weights
14501424
def save_lora_weights(

tests/models/test_lora_layers.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,42 +1081,6 @@ def test_a1111(self):
10811081

10821082
self.assertTrue(np.allclose(images, expected, atol=1e-3))
10831083

1084-
def test_a1111_with_model_cpu_offload(self):
1085-
generator = torch.Generator().manual_seed(0)
1086-
1087-
pipe = StableDiffusionPipeline.from_pretrained("hf-internal-testing/Counterfeit-V2.5", safety_checker=None)
1088-
pipe.enable_model_cpu_offload()
1089-
lora_model_id = "hf-internal-testing/civitai-light-shadow-lora"
1090-
lora_filename = "light_and_shadow.safetensors"
1091-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
1092-
1093-
images = pipe(
1094-
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
1095-
).images
1096-
1097-
images = images[0, -3:, -3:, -1].flatten()
1098-
expected = np.array([0.3636, 0.3708, 0.3694, 0.3679, 0.3829, 0.3677, 0.3692, 0.3688, 0.3292])
1099-
1100-
self.assertTrue(np.allclose(images, expected, atol=1e-3))
1101-
1102-
def test_a1111_with_sequential_cpu_offload(self):
1103-
generator = torch.Generator().manual_seed(0)
1104-
1105-
pipe = StableDiffusionPipeline.from_pretrained("hf-internal-testing/Counterfeit-V2.5", safety_checker=None)
1106-
pipe.enable_sequential_cpu_offload()
1107-
lora_model_id = "hf-internal-testing/civitai-light-shadow-lora"
1108-
lora_filename = "light_and_shadow.safetensors"
1109-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
1110-
1111-
images = pipe(
1112-
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
1113-
).images
1114-
1115-
images = images[0, -3:, -3:, -1].flatten()
1116-
expected = np.array([0.3636, 0.3708, 0.3694, 0.3679, 0.3829, 0.3677, 0.3692, 0.3688, 0.3292])
1117-
1118-
self.assertTrue(np.allclose(images, expected, atol=1e-3))
1119-
11201084
def test_kohya_sd_v15_with_higher_dimensions(self):
11211085
generator = torch.Generator().manual_seed(0)
11221086

@@ -1293,10 +1257,10 @@ def test_sdxl_1_0_lora(self):
12931257
generator = torch.Generator().manual_seed(0)
12941258

12951259
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
1296-
pipe.enable_model_cpu_offload()
12971260
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
12981261
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
12991262
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
1263+
pipe.enable_model_cpu_offload()
13001264

13011265
images = pipe(
13021266
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
@@ -1447,21 +1411,3 @@ def test_sdxl_1_0_fuse_unfuse_all(self):
14471411
assert state_dicts_almost_equal(text_encoder_1_sd, pipe.text_encoder.state_dict())
14481412
assert state_dicts_almost_equal(text_encoder_2_sd, pipe.text_encoder_2.state_dict())
14491413
assert state_dicts_almost_equal(unet_sd, pipe.unet.state_dict())
1450-
1451-
def test_sdxl_1_0_lora_with_sequential_cpu_offloading(self):
1452-
generator = torch.Generator().manual_seed(0)
1453-
1454-
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
1455-
pipe.enable_sequential_cpu_offload()
1456-
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
1457-
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
1458-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
1459-
1460-
images = pipe(
1461-
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
1462-
).images
1463-
1464-
images = images[0, -3:, -3:, -1].flatten()
1465-
expected = np.array([0.4468, 0.4087, 0.4134, 0.366, 0.3202, 0.3505, 0.3786, 0.387, 0.3535])
1466-
1467-
self.assertTrue(np.allclose(images, expected, atol=1e-3))

0 commit comments

Comments
 (0)