2424
2525from diffusers import AutoencoderKL , DDIMScheduler , LMSDiscreteScheduler , PNDMScheduler , UNet2DConditionModel
2626from diffusers .pipelines .stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline
27- from diffusers .utils .testing_utils import floats_tensor , nightly , require_accelerator , require_torch_gpu , torch_device
27+ from diffusers .utils .testing_utils import (
28+ Expectations ,
29+ backend_empty_cache ,
30+ floats_tensor ,
31+ nightly ,
32+ require_accelerator ,
33+ require_torch_accelerator ,
34+ torch_device ,
35+ )
2836
2937
3038class SafeDiffusionPipelineFastTests (unittest .TestCase ):
3139 def setUp (self ):
3240 # clean up the VRAM before each test
3341 super ().setUp ()
3442 gc .collect ()
35- torch . cuda . empty_cache ( )
43+ backend_empty_cache ( torch_device )
3644
3745 def tearDown (self ):
3846 # clean up the VRAM after each test
3947 super ().tearDown ()
4048 gc .collect ()
41- torch . cuda . empty_cache ( )
49+ backend_empty_cache ( torch_device )
4250
4351 @property
4452 def dummy_image (self ):
@@ -262,19 +270,19 @@ def test_stable_diffusion_fp16(self):
262270
263271
264272@nightly
265- @require_torch_gpu
273+ @require_torch_accelerator
266274class SafeDiffusionPipelineIntegrationTests (unittest .TestCase ):
267275 def setUp (self ):
268276 # clean up the VRAM before each test
269277 super ().setUp ()
270278 gc .collect ()
271- torch . cuda . empty_cache ( )
279+ backend_empty_cache ( torch_device )
272280
273281 def tearDown (self ):
274282 # clean up the VRAM after each test
275283 super ().tearDown ()
276284 gc .collect ()
277- torch . cuda . empty_cache ( )
285+ backend_empty_cache ( torch_device )
278286
279287 def test_harm_safe_stable_diffusion (self ):
280288 sd_pipe = StableDiffusionPipeline .from_pretrained (
@@ -308,7 +316,14 @@ def test_harm_safe_stable_diffusion(self):
308316
309317 image = output .images
310318 image_slice = image [0 , - 3 :, - 3 :, - 1 ]
311- expected_slice = [0.2278 , 0.2231 , 0.2249 , 0.2333 , 0.2303 , 0.1885 , 0.2273 , 0.2144 , 0.2176 ]
319+ expected_slices = Expectations (
320+ {
321+ ("xpu" , 3 ): [0.0076 , 0.0058 , 0.0012 , 0 , 0.0047 , 0.0046 , 0 , 0 , 0 ],
322+ ("cuda" , 7 ): [0.2278 , 0.2231 , 0.2249 , 0.2333 , 0.2303 , 0.1885 , 0.2273 , 0.2144 , 0.2176 ],
323+ ("cuda" , 8 ): [0.0076 , 0.0058 , 0.0012 , 0 , 0.0047 , 0.0046 , 0 , 0 , 0 ],
324+ }
325+ )
326+ expected_slice = expected_slices .get_expectation ()
312327
313328 assert image .shape == (1 , 512 , 512 , 3 )
314329
@@ -335,6 +350,15 @@ def test_harm_safe_stable_diffusion(self):
335350 image_slice = image [0 , - 3 :, - 3 :, - 1 ]
336351 expected_slice = [0.2383 , 0.2276 , 0.236 , 0.2192 , 0.2186 , 0.2053 , 0.1971 , 0.1901 , 0.1719 ]
337352
353+ expected_slices = Expectations (
354+ {
355+ ("xpu" , 3 ): [0.0443 , 0.0439 , 0.0381 , 0.0336 , 0.0408 , 0.0345 , 0.0405 , 0.0338 , 0.0293 ],
356+ ("cuda" , 7 ): [0.2383 , 0.2276 , 0.236 , 0.2192 , 0.2186 , 0.2053 , 0.1971 , 0.1901 , 0.1719 ],
357+ ("cuda" , 8 ): [0.0443 , 0.0439 , 0.0381 , 0.0336 , 0.0408 , 0.0345 , 0.0405 , 0.0338 , 0.0293 ],
358+ }
359+ )
360+ expected_slice = expected_slices .get_expectation ()
361+
338362 assert image .shape == (1 , 512 , 512 , 3 )
339363
340364 assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-2
@@ -365,8 +389,14 @@ def test_nudity_safe_stable_diffusion(self):
365389
366390 image = output .images
367391 image_slice = image [0 , - 3 :, - 3 :, - 1 ]
368- expected_slice = [0.3502 , 0.3622 , 0.3396 , 0.3642 , 0.3478 , 0.3318 , 0.35 , 0.3348 , 0.3297 ]
369-
392+ expected_slices = Expectations (
393+ {
394+ ("xpu" , 3 ): [0.3244 , 0.3355 , 0.3260 , 0.3123 , 0.3246 , 0.3426 , 0.3109 , 0.3471 , 0.4001 ],
395+ ("cuda" , 7 ): [0.3502 , 0.3622 , 0.3396 , 0.3642 , 0.3478 , 0.3318 , 0.35 , 0.3348 , 0.3297 ],
396+ ("cuda" , 8 ): [0.3605 , 0.3684 , 0.3712 , 0.3624 , 0.3675 , 0.3726 , 0.3494 , 0.3748 , 0.4044 ],
397+ }
398+ )
399+ expected_slice = expected_slices .get_expectation ()
370400 assert image .shape == (1 , 512 , 512 , 3 )
371401
372402 assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-2
@@ -389,7 +419,16 @@ def test_nudity_safe_stable_diffusion(self):
389419
390420 image = output .images
391421 image_slice = image [0 , - 3 :, - 3 :, - 1 ]
392- expected_slice = [0.5531 , 0.5206 , 0.4895 , 0.5156 , 0.5182 , 0.4751 , 0.4802 , 0.4803 , 0.4443 ]
422+ expected_slices = Expectations (
423+ {
424+ ("xpu" , 3 ): [0.6178 , 0.6260 , 0.6194 , 0.6435 , 0.6265 , 0.6461 , 0.6567 , 0.6576 , 0.6444 ],
425+ ("cuda" , 7 ): [0.5531 , 0.5206 , 0.4895 , 0.5156 , 0.5182 , 0.4751 , 0.4802 , 0.4803 , 0.4443 ],
426+ ("cuda" , 8 ): [0.5892 , 0.5959 , 0.5914 , 0.6123 , 0.5982 , 0.6141 , 0.6180 , 0.6262 , 0.6171 ],
427+ }
428+ )
429+
430+ print (f"image_slice: { image_slice .flatten ()} " )
431+ expected_slice = expected_slices .get_expectation ()
393432
394433 assert image .shape == (1 , 512 , 512 , 3 )
395434
@@ -445,7 +484,14 @@ def test_nudity_safetychecker_safe_stable_diffusion(self):
445484
446485 image = output .images
447486 image_slice = image [0 , - 3 :, - 3 :, - 1 ]
448- expected_slice = np .array ([0.5818 , 0.6285 , 0.6835 , 0.6019 , 0.625 , 0.6754 , 0.6096 , 0.6334 , 0.6561 ])
449- assert image .shape == (1 , 512 , 512 , 3 )
487+ expected_slices = Expectations (
488+ {
489+ ("xpu" , 3 ): np .array ([0.0695 , 0.1244 , 0.1831 , 0.0527 , 0.0444 , 0.1660 , 0.0572 , 0.0677 , 0.1551 ]),
490+ ("cuda" , 7 ): np .array ([0.5818 , 0.6285 , 0.6835 , 0.6019 , 0.625 , 0.6754 , 0.6096 , 0.6334 , 0.6561 ]),
491+ ("cuda" , 8 ): np .array ([0.0695 , 0.1244 , 0.1831 , 0.0527 , 0.0444 , 0.1660 , 0.0572 , 0.0677 , 0.1551 ]),
492+ }
493+ )
494+ expected_slice = expected_slices .get_expectation ()
450495
496+ assert image .shape == (1 , 512 , 512 , 3 )
451497 assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-2
0 commit comments