21
21
22
22
from diffusers .pipelines .stable_diffusion import OnnxStableDiffusionUpscalePipeline
23
23
from diffusers .utils import floats_tensor
24
+ from diffusers .utils .testing_utils import (
25
+ is_onnx_available ,
26
+ load_image ,
27
+ nightly ,
28
+ require_onnxruntime ,
29
+ require_torch_gpu ,
30
+ )
31
+
24
32
25
33
from ...test_pipelines_onnx_common import OnnxPipelineTesterMixin
26
34
27
35
36
+ if is_onnx_available ():
37
+ import onnxruntime as ort
38
+
39
+
28
40
class OnnxStableDiffusionUpscalePipelineFastTests (OnnxPipelineTesterMixin , unittest .TestCase ):
29
41
# TODO: is there an appropriate internal test set?
30
42
hub_checkpoint = "ssube/stable-diffusion-x4-upscaler-onnx"
@@ -42,7 +54,7 @@ def get_dummy_inputs(self, seed=0):
42
54
}
43
55
return inputs
44
56
45
- def test_pipeline_default_ddim (self ):
57
+ def test_pipeline_default_ddpm (self ):
46
58
pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
47
59
pipe .set_progress_bar_config (disable = None )
48
60
@@ -56,3 +68,60 @@ def test_pipeline_default_ddim(self):
56
68
[0.6974782 , 0.68902093 , 0.70135885 , 0.7583618 , 0.7804545 , 0.7854912 , 0.78667426 , 0.78743863 , 0.78070223 ]
57
69
)
58
70
assert np .abs (image_slice - expected_slice ).max () < 1e-1
71
+
72
+
73
+ @nightly
74
+ @require_onnxruntime
75
+ @require_torch_gpu
76
+ class OnnxStableDiffusionUpscalePipelineIntegrationTests (unittest .TestCase ):
77
+ @property
78
+ def gpu_provider (self ):
79
+ return (
80
+ "CUDAExecutionProvider" ,
81
+ {
82
+ "gpu_mem_limit" : "15000000000" , # 15GB
83
+ "arena_extend_strategy" : "kSameAsRequested" ,
84
+ },
85
+ )
86
+
87
+ @property
88
+ def gpu_options (self ):
89
+ options = ort .SessionOptions ()
90
+ options .enable_mem_pattern = False
91
+ return options
92
+
93
+ def test_inference_default_ddpm (self ):
94
+ init_image = load_image (
95
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
96
+ "/img2img/sketch-mountains-input.jpg"
97
+ )
98
+ init_image = init_image .resize ((128 , 128 ))
99
+ # using the PNDM scheduler by default
100
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (
101
+ "ssube/stable-diffusion-x4-upscaler-onnx" ,
102
+ safety_checker = None ,
103
+ feature_extractor = None ,
104
+ provider = self .gpu_provider ,
105
+ sess_options = self .gpu_options ,
106
+ )
107
+ pipe .set_progress_bar_config (disable = None )
108
+
109
+ prompt = "A fantasy landscape, trending on artstation"
110
+
111
+ generator = torch .manual_seed (0 )
112
+ output = pipe (
113
+ prompt = prompt ,
114
+ image = init_image ,
115
+ guidance_scale = 7.5 ,
116
+ num_inference_steps = 10 ,
117
+ generator = generator ,
118
+ output_type = "np" ,
119
+ )
120
+ images = output .images
121
+ image_slice = images [0 , 255 :258 , 383 :386 , - 1 ]
122
+
123
+ assert images .shape == (1 , 512 , 512 , 3 )
124
+ expected_slice = np .array ([0.4883 , 0.4947 , 0.4980 , 0.4975 , 0.4982 , 0.4980 , 0.5000 , 0.5006 , 0.4972 ])
125
+ # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues
126
+
127
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 2e-2
0 commit comments