19
19
import numpy as np
20
20
import torch
21
21
22
- from diffusers import OnnxStableDiffusionUpscalePipeline
22
+ from diffusers import (
23
+ DPMSolverMultistepScheduler ,
24
+ EulerAncestralDiscreteScheduler ,
25
+ EulerDiscreteScheduler ,
26
+ LMSDiscreteScheduler ,
27
+ OnnxStableDiffusionUpscalePipeline ,
28
+ PNDMScheduler ,
29
+ )
23
30
from diffusers .utils import floats_tensor
24
31
from diffusers .utils .testing_utils import (
25
32
is_onnx_available ,
@@ -68,6 +75,86 @@ def test_pipeline_default_ddpm(self):
68
75
)
69
76
assert np .abs (image_slice - expected_slice ).max () < 1e-1
70
77
78
+ def test_pipeline_pndm (self ):
79
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
80
+ pipe .scheduler = PNDMScheduler .from_config (pipe .scheduler .config , skip_prk_steps = True )
81
+ pipe .set_progress_bar_config (disable = None )
82
+
83
+ inputs = self .get_dummy_inputs ()
84
+ image = pipe (** inputs ).images
85
+ image_slice = image [0 , - 3 :, - 3 :, - 1 ]
86
+
87
+ assert image .shape == (1 , 512 , 512 , 3 )
88
+ expected_slice = np .array (
89
+ [0.6898892 , 0.59240556 , 0.52499527 , 0.58866215 , 0.52258235 , 0.52572715 , 0.62414473 , 0.6174387 , 0.6214964 ]
90
+ )
91
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-1
92
+
93
+ def test_pipeline_dpm_multistep (self ):
94
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
95
+ pipe .scheduler = DPMSolverMultistepScheduler .from_config (pipe .scheduler .config )
96
+ pipe .set_progress_bar_config (disable = None )
97
+
98
+ inputs = self .get_dummy_inputs ()
99
+ image = pipe (** inputs ).images
100
+ image_slice = image [0 , - 3 :, - 3 :, - 1 ]
101
+
102
+ assert image .shape == (1 , 512 , 512 , 3 )
103
+ expected_slice = np .array (
104
+ [0.7659278 , 0.76437664 , 0.75579107 , 0.7691116 , 0.77666986 , 0.7727672 , 0.7758664 , 0.7812226 , 0.76942515 ]
105
+ )
106
+
107
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-1
108
+
109
+ def test_pipeline_lms (self ):
110
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
111
+ pipe .scheduler = LMSDiscreteScheduler .from_config (pipe .scheduler .config )
112
+ pipe .set_progress_bar_config (disable = None )
113
+
114
+ # warmup pass to apply optimizations
115
+ _ = pipe (** self .get_dummy_inputs ())
116
+
117
+ inputs = self .get_dummy_inputs ()
118
+ image = pipe (** inputs ).images
119
+ image_slice = image [0 , - 3 :, - 3 :, - 1 ]
120
+
121
+ assert image .shape == (1 , 512 , 512 , 3 )
122
+ expected_slice = np .array (
123
+ [0.6974782 , 0.68902093 , 0.70135885 , 0.7583618 , 0.7804545 , 0.7854912 , 0.78667426 , 0.78743863 , 0.78070223 ]
124
+ )
125
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-1
126
+
127
+ def test_pipeline_euler (self ):
128
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
129
+ pipe .scheduler = EulerDiscreteScheduler .from_config (pipe .scheduler .config )
130
+ pipe .set_progress_bar_config (disable = None )
131
+
132
+ inputs = self .get_dummy_inputs ()
133
+ image = pipe (** inputs ).images
134
+ image_slice = image [0 , - 3 :, - 3 :, - 1 ]
135
+
136
+ assert image .shape == (1 , 512 , 512 , 3 )
137
+ expected_slice = np .array (
138
+ [0.6974782 , 0.68902093 , 0.70135885 , 0.7583618 , 0.7804545 , 0.7854912 , 0.78667426 , 0.78743863 , 0.78070223 ]
139
+ )
140
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-1
141
+
142
+ def test_pipeline_euler_ancestral (self ):
143
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (self .hub_checkpoint , provider = "CPUExecutionProvider" )
144
+ pipe .scheduler = EulerAncestralDiscreteScheduler .from_config (pipe .scheduler .config )
145
+ pipe .set_progress_bar_config (disable = None )
146
+
147
+ inputs = self .get_dummy_inputs ()
148
+ image = pipe (** inputs ).images
149
+ image_slice = image [0 , - 3 :, - 3 :, - 1 ]
150
+
151
+ assert image .shape == (1 , 512 , 512 , 3 )
152
+ expected_slice = np .array (
153
+ [0.77424496 , 0.773601 , 0.7645288 , 0.7769598 , 0.7772739 , 0.7738688 , 0.78187233 , 0.77879584 , 0.767043 ]
154
+ )
155
+
156
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 1e-1
157
+
71
158
72
159
@nightly
73
160
@require_onnxruntime
@@ -98,8 +185,6 @@ def test_inference_default_ddpm(self):
98
185
# using the PNDM scheduler by default
99
186
pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (
100
187
"ssube/stable-diffusion-x4-upscaler-onnx" ,
101
- safety_checker = None ,
102
- feature_extractor = None ,
103
188
provider = self .gpu_provider ,
104
189
sess_options = self .gpu_options ,
105
190
)
@@ -124,3 +209,42 @@ def test_inference_default_ddpm(self):
124
209
# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues
125
210
126
211
assert np .abs (image_slice .flatten () - expected_slice ).max () < 2e-2
212
+
213
+ def test_inference_k_lms (self ):
214
+ init_image = load_image (
215
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
216
+ "/img2img/sketch-mountains-input.jpg"
217
+ )
218
+ init_image = init_image .resize ((128 , 128 ))
219
+ lms_scheduler = LMSDiscreteScheduler .from_pretrained (
220
+ "ssube/stable-diffusion-x4-upscaler-onnx" , subfolder = "scheduler"
221
+ )
222
+ pipe = OnnxStableDiffusionUpscalePipeline .from_pretrained (
223
+ "ssube/stable-diffusion-x4-upscaler-onnx" ,
224
+ scheduler = lms_scheduler ,
225
+ provider = self .gpu_provider ,
226
+ sess_options = self .gpu_options ,
227
+ )
228
+ pipe .set_progress_bar_config (disable = None )
229
+
230
+ prompt = "A fantasy landscape, trending on artstation"
231
+
232
+ generator = torch .manual_seed (0 )
233
+ output = pipe (
234
+ prompt = prompt ,
235
+ image = init_image ,
236
+ guidance_scale = 7.5 ,
237
+ num_inference_steps = 20 ,
238
+ generator = generator ,
239
+ output_type = "np" ,
240
+ )
241
+ images = output .images
242
+ image_slice = images [0 , 255 :258 , 383 :386 , - 1 ]
243
+
244
+ assert images .shape == (1 , 512 , 512 , 3 )
245
+ expected_slice = np .array (
246
+ [0.50173753 , 0.50223356 , 0.502039 , 0.50233036 , 0.5023725 , 0.5022601 , 0.5018758 , 0.50234085 , 0.50241566 ]
247
+ )
248
+ # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues
249
+
250
+ assert np .abs (image_slice .flatten () - expected_slice ).max () < 2e-2
0 commit comments