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