Skip to content

Commit 2b04ec2

Browse files
sayakpaulDN6
andauthored
[Tests] Speed up fast pipelines part II (#7521)
* start printing the tensors. * print full throttle * set static slices for 7 tests. * remove printing. * flatten * disable test for controlnet * what happens when things are seeded properly? * set the right value * style./ * make pia test fail to check things * print. * fix pia. * checking for animatediff. * fix: animatediff. * video synthesis * final piece. * style. * print guess. * fix: assertion for control guess. --------- Co-authored-by: Dhruv Nair <[email protected]>
1 parent 000fa82 commit 2b04ec2

File tree

10 files changed

+76
-11
lines changed

10 files changed

+76
-11
lines changed

tests/pipelines/animatediff/test_animatediff.py

+6
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ def test_ip_adapter_single(self):
172172
)
173173
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)
174174

175+
def test_dict_tuple_outputs_equivalent(self):
176+
expected_slice = None
177+
if torch_device == "cpu":
178+
expected_slice = np.array([0.4051, 0.4495, 0.4480, 0.5845, 0.4172, 0.6066, 0.4205, 0.3786, 0.5323])
179+
return super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
180+
175181
def test_inference_batch_single_identical(
176182
self,
177183
batch_size=2,

tests/pipelines/controlnet/test_controlnet_blip_diffusion.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
PNDMScheduler,
2929
UNet2DConditionModel,
3030
)
31-
from diffusers.utils.testing_utils import enable_full_determinism
31+
from diffusers.utils.testing_utils import enable_full_determinism, torch_device
3232
from src.diffusers.pipelines.blip_diffusion.blip_image_processing import BlipImageProcessor
3333
from src.diffusers.pipelines.blip_diffusion.modeling_blip2 import Blip2QFormerModel
3434
from src.diffusers.pipelines.blip_diffusion.modeling_ctx_clip import ContextCLIPTextModel
@@ -196,6 +196,12 @@ def get_dummy_inputs(self, device, seed=0):
196196
}
197197
return inputs
198198

199+
def test_dict_tuple_outputs_equivalent(self):
200+
expected_slice = None
201+
if torch_device == "cpu":
202+
expected_slice = np.array([0.4803, 0.3865, 0.1422, 0.6119, 0.2283, 0.6365, 0.5453, 0.5205, 0.3581])
203+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
204+
199205
def test_blipdiffusion_controlnet(self):
200206
device = "cpu"
201207
components = self.get_dummy_components()

tests/pipelines/controlnet/test_controlnet_inpaint_sdxl.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ControlNetPipelineSDXLFastTests(
5757
image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS
5858

5959
def get_dummy_components(self):
60+
torch.manual_seed(0)
6061
unet = UNet2DConditionModel(
6162
block_out_channels=(32, 64),
6263
layers_per_block=2,
@@ -74,6 +75,7 @@ def get_dummy_components(self):
7475
projection_class_embeddings_input_dim=80, # 6 * 8 + 32
7576
cross_attention_dim=64,
7677
)
78+
torch.manual_seed(0)
7779
controlnet = ControlNetModel(
7880
block_out_channels=(32, 64),
7981
layers_per_block=2,
@@ -123,6 +125,7 @@ def get_dummy_components(self):
123125
text_encoder = CLIPTextModel(text_encoder_config)
124126
tokenizer = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
125127

128+
torch.manual_seed(0)
126129
text_encoder_2 = CLIPTextModelWithProjection(text_encoder_config)
127130
tokenizer_2 = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
128131

@@ -182,6 +185,12 @@ def get_dummy_inputs(self, device, seed=0, img_res=64):
182185
def test_attention_slicing_forward_pass(self):
183186
return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3)
184187

188+
def test_dict_tuple_outputs_equivalent(self):
189+
expected_slice = None
190+
if torch_device == "cpu":
191+
expected_slice = np.array([0.5490, 0.5053, 0.4676, 0.5816, 0.5364, 0.4830, 0.5937, 0.5719, 0.4318])
192+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
193+
185194
@unittest.skipIf(
186195
torch_device != "cuda" or not is_xformers_available(),
187196
reason="XFormers attention is only available with CUDA and `xformers` installed",
@@ -289,9 +298,7 @@ def test_controlnet_sdxl_guess(self):
289298

290299
output = sd_pipe(**inputs)
291300
image_slice = output.images[0, -3:, -3:, -1]
292-
expected_slice = np.array(
293-
[0.5381963, 0.4836803, 0.45821992, 0.5577731, 0.51210403, 0.4794795, 0.59282357, 0.5647199, 0.43100584]
294-
)
301+
expected_slice = np.array([0.549, 0.5053, 0.4676, 0.5816, 0.5364, 0.483, 0.5937, 0.5719, 0.4318])
295302

296303
# make sure that it's equal
297304
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-4

tests/pipelines/kandinsky3/test_kandinsky3_img2img.py

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
load_image,
3737
require_torch_gpu,
3838
slow,
39+
torch_device,
3940
)
4041

4142
from ..pipeline_params import (
@@ -152,6 +153,12 @@ def get_dummy_inputs(self, device, seed=0):
152153
}
153154
return inputs
154155

156+
def test_dict_tuple_outputs_equivalent(self):
157+
expected_slice = None
158+
if torch_device == "cpu":
159+
expected_slice = np.array([0.5762, 0.6112, 0.4150, 0.6018, 0.6167, 0.4626, 0.5426, 0.5641, 0.6536])
160+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
161+
155162
def test_kandinsky3_img2img(self):
156163
device = "cpu"
157164

tests/pipelines/pia/test_pia.py

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ def test_ip_adapter_single(self):
175175
)
176176
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)
177177

178+
def test_dict_tuple_outputs_equivalent(self):
179+
expected_slice = None
180+
if torch_device == "cpu":
181+
expected_slice = np.array([0.3740, 0.4284, 0.4038, 0.5417, 0.4405, 0.5521, 0.4273, 0.4124, 0.4997])
182+
return super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
183+
178184
@unittest.skip("Attention slicing is not enabled in this pipeline")
179185
def test_attention_slicing_forward_pass(self):
180186
pass

tests/pipelines/stable_diffusion_2/test_stable_diffusion_attend_and_excite.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
numpy_cosine_similarity_distance,
3333
require_torch_gpu,
3434
skip_mps,
35+
torch_device,
3536
)
3637

3738
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
@@ -153,6 +154,12 @@ def get_dummy_inputs(self, device, seed=0):
153154
}
154155
return inputs
155156

157+
def test_dict_tuple_outputs_equivalent(self):
158+
expected_slice = None
159+
if torch_device == "cpu":
160+
expected_slice = np.array([0.6391, 0.6290, 0.4860, 0.5134, 0.5550, 0.4577, 0.5033, 0.5023, 0.4538])
161+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice, expected_max_difference=3e-3)
162+
156163
def test_inference(self):
157164
device = "cpu"
158165

@@ -182,9 +189,6 @@ def test_inference_batch_consistent(self):
182189
def test_inference_batch_single_identical(self):
183190
self._test_inference_batch_single_identical(batch_size=2, expected_max_diff=7e-4)
184191

185-
def test_dict_tuple_outputs_equivalent(self):
186-
super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3)
187-
188192
def test_pt_np_pil_outputs_equivalent(self):
189193
super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4)
190194

tests/pipelines/stable_diffusion_gligen_text_image/test_stable_diffusion_gligen_text_image.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636
from diffusers.pipelines.stable_diffusion import CLIPImageProjection
3737
from diffusers.utils import load_image
38-
from diffusers.utils.testing_utils import enable_full_determinism
38+
from diffusers.utils.testing_utils import enable_full_determinism, torch_device
3939

4040
from ..pipeline_params import (
4141
TEXT_TO_IMAGE_BATCH_PARAMS,
@@ -160,6 +160,12 @@ def get_dummy_inputs(self, device, seed=0):
160160
}
161161
return inputs
162162

163+
def test_dict_tuple_outputs_equivalent(self):
164+
expected_slice = None
165+
if torch_device == "cpu":
166+
expected_slice = np.array([0.5052, 0.5546, 0.4567, 0.4770, 0.5195, 0.4085, 0.5026, 0.4909, 0.4495])
167+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
168+
163169
def test_stable_diffusion_gligen_text_image_default_case(self):
164170
device = "cpu" # ensure determinism for the device-dependent torch.Generator
165171
components = self.get_dummy_components()

tests/pipelines/test_pipelines_common.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def _test_inference_batch_single_identical(
10371037
max_diff = np.abs(to_np(output_batch[0][0]) - to_np(output[0][0])).max()
10381038
assert max_diff < expected_max_diff
10391039

1040-
def test_dict_tuple_outputs_equivalent(self, expected_max_difference=1e-4):
1040+
def test_dict_tuple_outputs_equivalent(self, expected_slice=None, expected_max_difference=1e-4):
10411041
components = self.get_dummy_components()
10421042
pipe = self.pipeline_class(**components)
10431043
for component in pipe.components.values():
@@ -1048,10 +1048,21 @@ def test_dict_tuple_outputs_equivalent(self, expected_max_difference=1e-4):
10481048
pipe.set_progress_bar_config(disable=None)
10491049

10501050
generator_device = "cpu"
1051-
output = pipe(**self.get_dummy_inputs(generator_device))[0]
1051+
if expected_slice is None:
1052+
output = pipe(**self.get_dummy_inputs(generator_device))[0]
1053+
else:
1054+
output = expected_slice
1055+
10521056
output_tuple = pipe(**self.get_dummy_inputs(generator_device), return_dict=False)[0]
10531057

1054-
max_diff = np.abs(to_np(output) - to_np(output_tuple)).max()
1058+
if expected_slice is None:
1059+
max_diff = np.abs(to_np(output) - to_np(output_tuple)).max()
1060+
else:
1061+
if output_tuple.ndim != 5:
1062+
max_diff = np.abs(to_np(output) - to_np(output_tuple)[0, -3:, -3:, -1].flatten()).max()
1063+
else:
1064+
max_diff = np.abs(to_np(output) - to_np(output_tuple)[0, -3:, -3:, -1, -1].flatten()).max()
1065+
10551066
self.assertLess(max_diff, expected_max_difference)
10561067

10571068
def test_components_function(self):

tests/pipelines/text_to_video_synthesis/test_text_to_video.py

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def get_dummy_inputs(self, device, seed=0):
133133
}
134134
return inputs
135135

136+
def test_dict_tuple_outputs_equivalent(self):
137+
expected_slice = None
138+
if torch_device == "cpu":
139+
expected_slice = np.array([0.4903, 0.5649, 0.5504, 0.5179, 0.4821, 0.5466, 0.4131, 0.5052, 0.5077])
140+
return super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
141+
136142
def test_text_to_video_default_case(self):
137143
device = "cpu" # ensure determinism for the device-dependent torch.Generator
138144
components = self.get_dummy_components()

tests/pipelines/unidiffuser/test_unidiffuser.py

+6
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ def get_dummy_inputs_with_latents(self, device, seed=0):
206206
}
207207
return inputs
208208

209+
def test_dict_tuple_outputs_equivalent(self):
210+
expected_slice = None
211+
if torch_device == "cpu":
212+
expected_slice = np.array([0.7489, 0.3722, 0.4475, 0.5630, 0.5923, 0.4992, 0.3936, 0.5844, 0.4975])
213+
super().test_dict_tuple_outputs_equivalent(expected_slice=expected_slice)
214+
209215
def test_unidiffuser_default_joint_v0(self):
210216
device = "cpu" # ensure determinism for the device-dependent torch.Generator
211217
components = self.get_dummy_components()

0 commit comments

Comments
 (0)