Skip to content

Commit f5dfe2a

Browse files
DN6sayakpaul
andauthored
LoRA test fixes (#6163)
* update * update * update * update --------- Co-authored-by: Sayak Paul <[email protected]>
1 parent 4836cfa commit f5dfe2a

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

tests/lora/test_lora_layers_old_backend.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,21 @@ def test_stable_diffusion_attn_processors(self):
343343
image = sd_pipe(**inputs).images
344344
assert image.shape == (1, 64, 64, 3)
345345

346+
@unittest.skipIf(not torch.cuda.is_available() or not is_xformers_available(), reason="xformers requires cuda")
347+
def test_stable_diffusion_set_xformers_attn_processors(self):
348+
# disable_full_determinism()
349+
device = "cuda" # ensure determinism for the device-dependent torch.Generator
350+
components, _ = self.get_dummy_components()
351+
sd_pipe = StableDiffusionPipeline(**components)
352+
sd_pipe = sd_pipe.to(device)
353+
sd_pipe.set_progress_bar_config(disable=None)
354+
355+
_, _, inputs = self.get_dummy_inputs()
356+
357+
# run normal sd pipe
358+
image = sd_pipe(**inputs).images
359+
assert image.shape == (1, 64, 64, 3)
360+
346361
# run lora xformers attention
347362
attn_processors, _ = create_unet_lora_layers(sd_pipe.unet)
348363
attn_processors = {
@@ -607,7 +622,7 @@ def test_unload_lora_sd(self):
607622
orig_image_slice, orig_image_slice_two, atol=1e-3
608623
), "Unloading LoRA parameters should lead to results similar to what was obtained with the pipeline without any LoRA parameters."
609624

610-
@unittest.skipIf(torch_device != "cuda", "This test is supposed to run on GPU")
625+
@unittest.skipIf(torch_device != "cuda" or not is_xformers_available(), "This test is supposed to run on GPU")
611626
def test_lora_unet_attn_processors_with_xformers(self):
612627
with tempfile.TemporaryDirectory() as tmpdirname:
613628
self.create_lora_weight_file(tmpdirname)
@@ -644,7 +659,7 @@ def test_lora_unet_attn_processors_with_xformers(self):
644659
if isinstance(module, Attention):
645660
self.assertIsInstance(module.processor, XFormersAttnProcessor)
646661

647-
@unittest.skipIf(torch_device != "cuda", "This test is supposed to run on GPU")
662+
@unittest.skipIf(torch_device != "cuda" or not is_xformers_available(), "This test is supposed to run on GPU")
648663
def test_lora_save_load_with_xformers(self):
649664
pipeline_components, lora_components = self.get_dummy_components()
650665
sd_pipe = StableDiffusionPipeline(**pipeline_components)
@@ -2270,8 +2285,8 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
22702285
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
22712286
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
22722287

2273-
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
2274-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
2288+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
2289+
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
22752290
pipe.enable_model_cpu_offload()
22762291

22772292
start_time = time.time()
@@ -2284,13 +2299,13 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
22842299

22852300
del pipe
22862301

2287-
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
2288-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
2302+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
2303+
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
22892304
pipe.fuse_lora()
22902305
pipe.enable_model_cpu_offload()
22912306

2292-
start_time = time.time()
22932307
generator = torch.Generator().manual_seed(0)
2308+
start_time = time.time()
22942309
for _ in range(3):
22952310
pipe(
22962311
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2

tests/lora/test_lora_layers_peft.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
floats_tensor,
4747
load_image,
4848
nightly,
49+
numpy_cosine_similarity_distance,
4950
require_peft_backend,
5051
require_torch_gpu,
5152
slow,
@@ -1713,7 +1714,7 @@ def test_sdxl_0_9_lora_three(self):
17131714
release_memory(pipe)
17141715

17151716
def test_sdxl_1_0_lora(self):
1716-
generator = torch.Generator().manual_seed(0)
1717+
generator = torch.Generator("cpu").manual_seed(0)
17171718

17181719
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
17191720
pipe.enable_model_cpu_offload()
@@ -1736,7 +1737,7 @@ def test_sdxl_lcm_lora(self):
17361737
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
17371738
pipe.enable_model_cpu_offload()
17381739

1739-
generator = torch.Generator().manual_seed(0)
1740+
generator = torch.Generator("cpu").manual_seed(0)
17401741

17411742
lora_model_id = "latent-consistency/lcm-lora-sdxl"
17421743

@@ -1753,7 +1754,8 @@ def test_sdxl_lcm_lora(self):
17531754
image_np = pipe.image_processor.pil_to_numpy(image)
17541755
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
17551756

1756-
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2))
1757+
max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
1758+
assert max_diff < 1e-4
17571759

17581760
pipe.unload_lora_weights()
17591761

@@ -1764,7 +1766,7 @@ def test_sdv1_5_lcm_lora(self):
17641766
pipe.to("cuda")
17651767
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
17661768

1767-
generator = torch.Generator().manual_seed(0)
1769+
generator = torch.Generator("cpu").manual_seed(0)
17681770

17691771
lora_model_id = "latent-consistency/lcm-lora-sdv1-5"
17701772
pipe.load_lora_weights(lora_model_id)
@@ -1780,7 +1782,8 @@ def test_sdv1_5_lcm_lora(self):
17801782
image_np = pipe.image_processor.pil_to_numpy(image)
17811783
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
17821784

1783-
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2))
1785+
max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
1786+
assert max_diff < 1e-4
17841787

17851788
pipe.unload_lora_weights()
17861789

@@ -1795,7 +1798,7 @@ def test_sdv1_5_lcm_lora_img2img(self):
17951798
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape.png"
17961799
)
17971800

1798-
generator = torch.Generator().manual_seed(0)
1801+
generator = torch.Generator("cpu").manual_seed(0)
17991802

18001803
lora_model_id = "latent-consistency/lcm-lora-sdv1-5"
18011804
pipe.load_lora_weights(lora_model_id)
@@ -1816,7 +1819,8 @@ def test_sdv1_5_lcm_lora_img2img(self):
18161819
image_np = pipe.image_processor.pil_to_numpy(image)
18171820
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
18181821

1819-
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2))
1822+
max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
1823+
assert max_diff < 1e-4
18201824

18211825
pipe.unload_lora_weights()
18221826

@@ -1849,7 +1853,7 @@ def test_sdxl_1_0_lora_fusion(self):
18491853
release_memory(pipe)
18501854

18511855
def test_sdxl_1_0_lora_unfusion(self):
1852-
generator = torch.Generator().manual_seed(0)
1856+
generator = torch.Generator("cpu").manual_seed(0)
18531857

18541858
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
18551859
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
@@ -1860,16 +1864,16 @@ def test_sdxl_1_0_lora_unfusion(self):
18601864
pipe.enable_model_cpu_offload()
18611865

18621866
images = pipe(
1863-
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
1867+
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=3
18641868
).images
1865-
images_with_fusion = images[0, -3:, -3:, -1].flatten()
1869+
images_with_fusion = images.flatten()
18661870

18671871
pipe.unfuse_lora()
1868-
generator = torch.Generator().manual_seed(0)
1872+
generator = torch.Generator("cpu").manual_seed(0)
18691873
images = pipe(
1870-
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
1874+
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=3
18711875
).images
1872-
images_without_fusion = images[0, -3:, -3:, -1].flatten()
1876+
images_without_fusion = images.flatten()
18731877

18741878
self.assertTrue(np.allclose(images_with_fusion, images_without_fusion, atol=1e-3))
18751879
release_memory(pipe)
@@ -1913,10 +1917,8 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
19131917
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
19141918
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
19151919

1916-
pipe = DiffusionPipeline.from_pretrained(
1917-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16
1918-
)
1919-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.bfloat16)
1920+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
1921+
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
19201922
pipe.enable_model_cpu_offload()
19211923

19221924
start_time = time.time()
@@ -1929,19 +1931,17 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
19291931

19301932
del pipe
19311933

1932-
pipe = DiffusionPipeline.from_pretrained(
1933-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16
1934-
)
1935-
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.bfloat16)
1934+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
1935+
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
19361936
pipe.fuse_lora()
1937+
19371938
# We need to unload the lora weights since in the previous API `fuse_lora` led to lora weights being
19381939
# silently deleted - otherwise this will CPU OOM
19391940
pipe.unload_lora_weights()
1940-
19411941
pipe.enable_model_cpu_offload()
19421942

1943-
start_time = time.time()
19441943
generator = torch.Generator().manual_seed(0)
1944+
start_time = time.time()
19451945
for _ in range(3):
19461946
pipe(
19471947
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2

0 commit comments

Comments
 (0)