Unverified Commit f5dfe2a8 authored by Dhruv Nair's avatar Dhruv Nair Committed by GitHub
Browse files

LoRA test fixes (#6163)



* update

* update

* update

* update

---------
Co-authored-by: default avatarSayak Paul <spsayakpaul@gmail.com>
parent 4836cfad
...@@ -343,6 +343,21 @@ class LoraLoaderMixinTests(unittest.TestCase): ...@@ -343,6 +343,21 @@ class LoraLoaderMixinTests(unittest.TestCase):
image = sd_pipe(**inputs).images image = sd_pipe(**inputs).images
assert image.shape == (1, 64, 64, 3) assert image.shape == (1, 64, 64, 3)
@unittest.skipIf(not torch.cuda.is_available() or not is_xformers_available(), reason="xformers requires cuda")
def test_stable_diffusion_set_xformers_attn_processors(self):
# disable_full_determinism()
device = "cuda" # ensure determinism for the device-dependent torch.Generator
components, _ = self.get_dummy_components()
sd_pipe = StableDiffusionPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)
_, _, inputs = self.get_dummy_inputs()
# run normal sd pipe
image = sd_pipe(**inputs).images
assert image.shape == (1, 64, 64, 3)
# run lora xformers attention # run lora xformers attention
attn_processors, _ = create_unet_lora_layers(sd_pipe.unet) attn_processors, _ = create_unet_lora_layers(sd_pipe.unet)
attn_processors = { attn_processors = {
...@@ -607,7 +622,7 @@ class LoraLoaderMixinTests(unittest.TestCase): ...@@ -607,7 +622,7 @@ class LoraLoaderMixinTests(unittest.TestCase):
orig_image_slice, orig_image_slice_two, atol=1e-3 orig_image_slice, orig_image_slice_two, atol=1e-3
), "Unloading LoRA parameters should lead to results similar to what was obtained with the pipeline without any LoRA parameters." ), "Unloading LoRA parameters should lead to results similar to what was obtained with the pipeline without any LoRA parameters."
@unittest.skipIf(torch_device != "cuda", "This test is supposed to run on GPU") @unittest.skipIf(torch_device != "cuda" or not is_xformers_available(), "This test is supposed to run on GPU")
def test_lora_unet_attn_processors_with_xformers(self): def test_lora_unet_attn_processors_with_xformers(self):
with tempfile.TemporaryDirectory() as tmpdirname: with tempfile.TemporaryDirectory() as tmpdirname:
self.create_lora_weight_file(tmpdirname) self.create_lora_weight_file(tmpdirname)
...@@ -644,7 +659,7 @@ class LoraLoaderMixinTests(unittest.TestCase): ...@@ -644,7 +659,7 @@ class LoraLoaderMixinTests(unittest.TestCase):
if isinstance(module, Attention): if isinstance(module, Attention):
self.assertIsInstance(module.processor, XFormersAttnProcessor) self.assertIsInstance(module.processor, XFormersAttnProcessor)
@unittest.skipIf(torch_device != "cuda", "This test is supposed to run on GPU") @unittest.skipIf(torch_device != "cuda" or not is_xformers_available(), "This test is supposed to run on GPU")
def test_lora_save_load_with_xformers(self): def test_lora_save_load_with_xformers(self):
pipeline_components, lora_components = self.get_dummy_components() pipeline_components, lora_components = self.get_dummy_components()
sd_pipe = StableDiffusionPipeline(**pipeline_components) sd_pipe = StableDiffusionPipeline(**pipeline_components)
...@@ -2270,8 +2285,8 @@ class LoraIntegrationTests(unittest.TestCase): ...@@ -2270,8 +2285,8 @@ class LoraIntegrationTests(unittest.TestCase):
lora_model_id = "hf-internal-testing/sdxl-1.0-lora" lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors" lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0") pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename) pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
start_time = time.time() start_time = time.time()
...@@ -2284,13 +2299,13 @@ class LoraIntegrationTests(unittest.TestCase): ...@@ -2284,13 +2299,13 @@ class LoraIntegrationTests(unittest.TestCase):
del pipe del pipe
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0") pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename) pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
pipe.fuse_lora() pipe.fuse_lora()
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
start_time = time.time()
generator = torch.Generator().manual_seed(0) generator = torch.Generator().manual_seed(0)
start_time = time.time()
for _ in range(3): for _ in range(3):
pipe( pipe(
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2 "masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
......
...@@ -46,6 +46,7 @@ from diffusers.utils.testing_utils import ( ...@@ -46,6 +46,7 @@ from diffusers.utils.testing_utils import (
floats_tensor, floats_tensor,
load_image, load_image,
nightly, nightly,
numpy_cosine_similarity_distance,
require_peft_backend, require_peft_backend,
require_torch_gpu, require_torch_gpu,
slow, slow,
...@@ -1713,7 +1714,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1713,7 +1714,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
release_memory(pipe) release_memory(pipe)
def test_sdxl_1_0_lora(self): def test_sdxl_1_0_lora(self):
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0") pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
...@@ -1736,7 +1737,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1736,7 +1737,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
lora_model_id = "latent-consistency/lcm-lora-sdxl" lora_model_id = "latent-consistency/lcm-lora-sdxl"
...@@ -1753,7 +1754,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1753,7 +1754,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
image_np = pipe.image_processor.pil_to_numpy(image) image_np = pipe.image_processor.pil_to_numpy(image)
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image) expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2)) max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
assert max_diff < 1e-4
pipe.unload_lora_weights() pipe.unload_lora_weights()
...@@ -1764,7 +1766,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1764,7 +1766,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
pipe.to("cuda") pipe.to("cuda")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
lora_model_id = "latent-consistency/lcm-lora-sdv1-5" lora_model_id = "latent-consistency/lcm-lora-sdv1-5"
pipe.load_lora_weights(lora_model_id) pipe.load_lora_weights(lora_model_id)
...@@ -1780,7 +1782,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1780,7 +1782,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
image_np = pipe.image_processor.pil_to_numpy(image) image_np = pipe.image_processor.pil_to_numpy(image)
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image) expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2)) max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
assert max_diff < 1e-4
pipe.unload_lora_weights() pipe.unload_lora_weights()
...@@ -1795,7 +1798,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1795,7 +1798,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape.png" "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape.png"
) )
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
lora_model_id = "latent-consistency/lcm-lora-sdv1-5" lora_model_id = "latent-consistency/lcm-lora-sdv1-5"
pipe.load_lora_weights(lora_model_id) pipe.load_lora_weights(lora_model_id)
...@@ -1816,7 +1819,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1816,7 +1819,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
image_np = pipe.image_processor.pil_to_numpy(image) image_np = pipe.image_processor.pil_to_numpy(image)
expected_image_np = pipe.image_processor.pil_to_numpy(expected_image) expected_image_np = pipe.image_processor.pil_to_numpy(expected_image)
self.assertTrue(np.allclose(image_np, expected_image_np, atol=1e-2)) max_diff = numpy_cosine_similarity_distance(image_np.flatten(), expected_image_np.flatten())
assert max_diff < 1e-4
pipe.unload_lora_weights() pipe.unload_lora_weights()
...@@ -1849,7 +1853,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1849,7 +1853,7 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
release_memory(pipe) release_memory(pipe)
def test_sdxl_1_0_lora_unfusion(self): def test_sdxl_1_0_lora_unfusion(self):
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0") pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
lora_model_id = "hf-internal-testing/sdxl-1.0-lora" lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
...@@ -1860,16 +1864,16 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1860,16 +1864,16 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
images = pipe( images = pipe(
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2 "masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=3
).images ).images
images_with_fusion = images[0, -3:, -3:, -1].flatten() images_with_fusion = images.flatten()
pipe.unfuse_lora() pipe.unfuse_lora()
generator = torch.Generator().manual_seed(0) generator = torch.Generator("cpu").manual_seed(0)
images = pipe( images = pipe(
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2 "masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=3
).images ).images
images_without_fusion = images[0, -3:, -3:, -1].flatten() images_without_fusion = images.flatten()
self.assertTrue(np.allclose(images_with_fusion, images_without_fusion, atol=1e-3)) self.assertTrue(np.allclose(images_with_fusion, images_without_fusion, atol=1e-3))
release_memory(pipe) release_memory(pipe)
...@@ -1913,10 +1917,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1913,10 +1917,8 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
lora_model_id = "hf-internal-testing/sdxl-1.0-lora" lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors" lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
pipe = DiffusionPipeline.from_pretrained( pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16 pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
)
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.bfloat16)
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
start_time = time.time() start_time = time.time()
...@@ -1929,19 +1931,17 @@ class LoraSDXLIntegrationTests(unittest.TestCase): ...@@ -1929,19 +1931,17 @@ class LoraSDXLIntegrationTests(unittest.TestCase):
del pipe del pipe
pipe = DiffusionPipeline.from_pretrained( pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16 pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.float16)
)
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename, torch_dtype=torch.bfloat16)
pipe.fuse_lora() pipe.fuse_lora()
# We need to unload the lora weights since in the previous API `fuse_lora` led to lora weights being # We need to unload the lora weights since in the previous API `fuse_lora` led to lora weights being
# silently deleted - otherwise this will CPU OOM # silently deleted - otherwise this will CPU OOM
pipe.unload_lora_weights() pipe.unload_lora_weights()
pipe.enable_model_cpu_offload() pipe.enable_model_cpu_offload()
start_time = time.time()
generator = torch.Generator().manual_seed(0) generator = torch.Generator().manual_seed(0)
start_time = time.time()
for _ in range(3): for _ in range(3):
pipe( pipe(
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2 "masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment