51
51
)
52
52
from optimum .habana .utils import set_seed
53
53
54
- from .clip_coco_utils import download_files
54
+ from .clip_coco_utils import calculate_clip_score , download_files
55
55
56
56
57
57
if os .environ .get ("GAUDI2_CI" , "0" ) == "1" :
@@ -608,6 +608,8 @@ def test_no_throughput_regression_autocast(self):
608
608
609
609
@slow
610
610
def test_no_generation_regression (self ):
611
+ seed = 27
612
+ set_seed (seed )
611
613
model_name = "CompVis/stable-diffusion-v1-4"
612
614
# fp32
613
615
scheduler = GaudiDDIMScheduler .from_pretrained (model_name , subfolder = "scheduler" )
@@ -619,39 +621,33 @@ def test_no_generation_regression(self):
619
621
use_hpu_graphs = True ,
620
622
gaudi_config = GaudiConfig (use_torch_autocast = False ),
621
623
)
622
- set_seed (27 )
624
+
625
+ prompt = "An image of a squirrel in Picasso style"
626
+ generator = torch .manual_seed (seed )
623
627
outputs = pipeline (
624
- prompt = "An image of a squirrel in Picasso style" ,
628
+ prompt = prompt ,
629
+ generator = generator ,
625
630
output_type = "np" ,
626
631
)
627
632
628
633
if os .environ .get ("GAUDI2_CI" , "0" ) == "1" :
629
- expected_slice = np .array (
630
- [
631
- 0.68306947 ,
632
- 0.6812112 ,
633
- 0.67309505 ,
634
- 0.70057267 ,
635
- 0.6582885 ,
636
- 0.6325019 ,
637
- 0.6708976 ,
638
- 0.6226433 ,
639
- 0.58038336 ,
640
- ]
641
- )
634
+ target_score = 29.8925
642
635
else :
643
- expected_slice = np .array (
644
- [0.70760196 , 0.7136303 , 0.7000798 , 0.714934 , 0.6776865 , 0.6800843 , 0.6923707 , 0.6653969 , 0.6408076 ]
645
- )
636
+ target_score = 36.774
637
+
646
638
image = outputs .images [0 ]
647
639
pil_image = numpy_to_pil (image )[0 ]
648
640
pil_image .save ("test_no_generation_regression_output.png" )
649
641
642
+ clip_score = calculate_clip_score (np .expand_dims (image , axis = 0 ), [prompt ])
643
+
650
644
self .assertEqual (image .shape , (512 , 512 , 3 ))
651
- self .assertLess ( np . abs ( expected_slice - image [ - 3 :, - 3 :, - 1 ]. flatten ()). max (), 5e-3 )
645
+ self .assertGreaterEqual ( clip_score , target_score )
652
646
653
647
@slow
654
648
def test_no_generation_regression_ldm3d (self ):
649
+ seed = 27
650
+ set_seed (seed )
655
651
model_name = "Intel/ldm3d-4c"
656
652
# fp32
657
653
scheduler = GaudiDDIMScheduler .from_pretrained (model_name , subfolder = "scheduler" )
@@ -663,61 +659,28 @@ def test_no_generation_regression_ldm3d(self):
663
659
use_hpu_graphs = True ,
664
660
gaudi_config = GaudiConfig (),
665
661
)
666
- set_seed (27 )
662
+
663
+ prompt = "An image of a squirrel in Picasso style"
664
+ generator = torch .manual_seed (seed )
667
665
outputs = pipeline (
668
- prompt = "An image of a squirrel in Picasso style" ,
666
+ prompt = prompt ,
667
+ generator = generator ,
669
668
output_type = "np" ,
670
669
)
671
670
672
671
if os .environ .get ("GAUDI2_CI" , "0" ) == "1" :
673
- expected_slice_rgb = np .array (
674
- [
675
- 0.2099357 ,
676
- 0.16664368 ,
677
- 0.08352646 ,
678
- 0.20643419 ,
679
- 0.16748399 ,
680
- 0.08781305 ,
681
- 0.21379063 ,
682
- 0.19943115 ,
683
- 0.04389626 ,
684
- ]
685
- )
686
- expected_slice_depth = np .array (
687
- [
688
- 0.68369114 ,
689
- 0.6827824 ,
690
- 0.6852779 ,
691
- 0.6836072 ,
692
- 0.6888298 ,
693
- 0.6895473 ,
694
- 0.6853674 ,
695
- 0.67561126 ,
696
- 0.660434 ,
697
- ]
698
- )
672
+ target_score = 28.0894
699
673
else :
700
- expected_slice_rgb = np .array ([0.7083766 , 1.0 , 1.0 , 0.70610344 , 0.9867363 , 1.0 , 0.7214538 , 1.0 , 1.0 ])
701
- expected_slice_depth = np .array (
702
- [
703
- 0.919621 ,
704
- 0.92072034 ,
705
- 0.9184986 ,
706
- 0.91994286 ,
707
- 0.9242079 ,
708
- 0.93387043 ,
709
- 0.92345214 ,
710
- 0.93558526 ,
711
- 0.9223714 ,
712
- ]
713
- )
674
+ target_score = 35.81
675
+
714
676
rgb = outputs .rgb [0 ]
715
677
depth = outputs .depth [0 ]
716
678
679
+ rgb_clip_score = calculate_clip_score (np .expand_dims (rgb , axis = 0 ), [prompt ])
680
+
717
681
self .assertEqual (rgb .shape , (512 , 512 , 3 ))
718
682
self .assertEqual (depth .shape , (512 , 512 , 1 ))
719
- self .assertLess (np .abs (expected_slice_rgb - rgb [- 3 :, - 3 :, - 1 ].flatten ()).max (), 5e-3 )
720
- self .assertLess (np .abs (expected_slice_depth - depth [- 3 :, - 3 :, - 1 ].flatten ()).max (), 5e-3 )
683
+ self .assertGreaterEqual (rgb_clip_score , target_score )
721
684
722
685
@slow
723
686
def test_no_generation_regression_upscale (self ):
0 commit comments