46
46
floats_tensor ,
47
47
load_image ,
48
48
nightly ,
49
+ numpy_cosine_similarity_distance ,
49
50
require_peft_backend ,
50
51
require_torch_gpu ,
51
52
slow ,
@@ -1753,7 +1754,8 @@ def test_sdxl_lcm_lora(self):
1753
1754
image_np = pipe .image_processor .pil_to_numpy (image )
1754
1755
expected_image_np = pipe .image_processor .pil_to_numpy (expected_image )
1755
1756
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
1757
1759
1758
1760
pipe .unload_lora_weights ()
1759
1761
@@ -1780,7 +1782,8 @@ def test_sdv1_5_lcm_lora(self):
1780
1782
image_np = pipe .image_processor .pil_to_numpy (image )
1781
1783
expected_image_np = pipe .image_processor .pil_to_numpy (expected_image )
1782
1784
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
1784
1787
1785
1788
pipe .unload_lora_weights ()
1786
1789
@@ -1816,7 +1819,8 @@ def test_sdv1_5_lcm_lora_img2img(self):
1816
1819
image_np = pipe .image_processor .pil_to_numpy (image )
1817
1820
expected_image_np = pipe .image_processor .pil_to_numpy (expected_image )
1818
1821
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
1820
1824
1821
1825
pipe .unload_lora_weights ()
1822
1826
@@ -1849,7 +1853,7 @@ def test_sdxl_1_0_lora_fusion(self):
1849
1853
release_memory (pipe )
1850
1854
1851
1855
def test_sdxl_1_0_lora_unfusion (self ):
1852
- generator = torch .Generator ().manual_seed (0 )
1856
+ generator = torch .Generator ("cpu" ).manual_seed (0 )
1853
1857
1854
1858
pipe = DiffusionPipeline .from_pretrained ("stabilityai/stable-diffusion-xl-base-1.0" )
1855
1859
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
@@ -1860,16 +1864,18 @@ def test_sdxl_1_0_lora_unfusion(self):
1860
1864
pipe .enable_model_cpu_offload ()
1861
1865
1862
1866
images = pipe (
1863
- "masterpiece, best quality, mountain" , output_type = "np " , generator = generator , num_inference_steps = 2
1867
+ "masterpiece, best quality, mountain" , output_type = "pil " , generator = generator , num_inference_steps = 3
1864
1868
).images
1865
- images_with_fusion = images [0 , - 3 :, - 3 :, - 1 ].flatten ()
1869
+ images [0 ].save ("fused.png" )
1870
+ images_with_fusion = images .flatten ()
1866
1871
1867
1872
pipe .unfuse_lora ()
1868
- generator = torch .Generator ().manual_seed (0 )
1873
+ generator = torch .Generator ("cpu" ).manual_seed (0 )
1869
1874
images = pipe (
1870
- "masterpiece, best quality, mountain" , output_type = "np " , generator = generator , num_inference_steps = 2
1875
+ "masterpiece, best quality, mountain" , output_type = "pil " , generator = generator , num_inference_steps = 3
1871
1876
).images
1872
- images_without_fusion = images [0 , - 3 :, - 3 :, - 1 ].flatten ()
1877
+ images [0 ].save ("unfused.png" )
1878
+ images_without_fusion = images .flatten ()
1873
1879
1874
1880
self .assertTrue (np .allclose (images_with_fusion , images_without_fusion , atol = 1e-3 ))
1875
1881
release_memory (pipe )
@@ -1913,10 +1919,8 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
1913
1919
lora_model_id = "hf-internal-testing/sdxl-1.0-lora"
1914
1920
lora_filename = "sd_xl_offset_example-lora_1.0.safetensors"
1915
1921
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 )
1922
+ pipe = DiffusionPipeline .from_pretrained ("stabilityai/stable-diffusion-xl-base-1.0" , torch_dtype = torch .float16 )
1923
+ pipe .load_lora_weights (lora_model_id , weight_name = lora_filename , torch_dtype = torch .float16 )
1920
1924
pipe .enable_model_cpu_offload ()
1921
1925
1922
1926
start_time = time .time ()
@@ -1929,10 +1933,8 @@ def test_sdxl_1_0_lora_fusion_efficiency(self):
1929
1933
1930
1934
del pipe
1931
1935
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 )
1936
+ pipe = DiffusionPipeline .from_pretrained ("stabilityai/stable-diffusion-xl-base-1.0" , torch_dtype = torch .float16 )
1937
+ pipe .load_lora_weights (lora_model_id , weight_name = lora_filename , torch_dtype = torch .float16 )
1936
1938
pipe .fuse_lora ()
1937
1939
# We need to unload the lora weights since in the previous API `fuse_lora` led to lora weights being
1938
1940
# silently deleted - otherwise this will CPU OOM
0 commit comments