Skip to content

Commit a176cfd

Browse files
committed
update
1 parent 3aabef5 commit a176cfd

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

tests/modular_pipelines/stable_diffusion_xl/test_modular_pipeline_stable_diffusion_xl.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def _test_stable_diffusion_xl_euler(self, expected_image_shape, expected_slice,
9999

100100
assert image.shape == expected_image_shape
101101

102-
assert np.abs(image_slice.flatten() - expected_slice).max() < expected_max_diff, (
103-
"Image Slice does not match expected slice"
104-
)
102+
assert (
103+
np.abs(image_slice.flatten() - expected_slice).max() < expected_max_diff
104+
), "Image Slice does not match expected slice"
105105

106106

107107
class SDXLModularIPAdapterTests:
@@ -114,20 +114,20 @@ def test_pipeline_inputs_and_blocks(self):
114114
parameters = blocks.input_names
115115

116116
assert issubclass(self.pipeline_class, ModularIPAdapterMixin)
117-
assert "ip_adapter_image" in parameters, (
118-
"`ip_adapter_image` argument must be supported by the `__call__` method"
119-
)
117+
assert (
118+
"ip_adapter_image" in parameters
119+
), "`ip_adapter_image` argument must be supported by the `__call__` method"
120120
assert "ip_adapter" in blocks.sub_blocks, "pipeline must contain an IPAdapter block"
121121

122122
_ = blocks.sub_blocks.pop("ip_adapter")
123123
parameters = blocks.input_names
124124
intermediate_parameters = blocks.intermediate_input_names
125-
assert "ip_adapter_image" not in parameters, (
126-
"`ip_adapter_image` argument must be removed from the `__call__` method"
127-
)
128-
assert "ip_adapter_image_embeds" not in intermediate_parameters, (
129-
"`ip_adapter_image_embeds` argument must be supported by the `__call__` method"
130-
)
125+
assert (
126+
"ip_adapter_image" not in parameters
127+
), "`ip_adapter_image` argument must be removed from the `__call__` method"
128+
assert (
129+
"ip_adapter_image_embeds" not in intermediate_parameters
130+
), "`ip_adapter_image_embeds` argument must be supported by the `__call__` method"
131131

132132
def _get_dummy_image_embeds(self, cross_attention_dim: int = 32):
133133
return torch.randn((1, 1, cross_attention_dim), device=torch_device)
@@ -203,9 +203,9 @@ def test_ip_adapter(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
203203
max_diff_without_adapter_scale = np.abs(output_without_adapter_scale - output_without_adapter).max()
204204
max_diff_with_adapter_scale = np.abs(output_with_adapter_scale - output_without_adapter).max()
205205

206-
assert max_diff_without_adapter_scale < expected_max_diff, (
207-
"Output without ip-adapter must be same as normal inference"
208-
)
206+
assert (
207+
max_diff_without_adapter_scale < expected_max_diff
208+
), "Output without ip-adapter must be same as normal inference"
209209
assert max_diff_with_adapter_scale > 1e-2, "Output with ip-adapter must be different from normal inference"
210210

211211
# 2. Multi IP-Adapter test cases
@@ -235,12 +235,12 @@ def test_ip_adapter(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
235235
output_without_multi_adapter_scale - output_without_adapter
236236
).max()
237237
max_diff_with_multi_adapter_scale = np.abs(output_with_multi_adapter_scale - output_without_adapter).max()
238-
assert max_diff_without_multi_adapter_scale < expected_max_diff, (
239-
"Output without multi-ip-adapter must be same as normal inference"
240-
)
241-
assert max_diff_with_multi_adapter_scale > 1e-2, (
242-
"Output with multi-ip-adapter scale must be different from normal inference"
243-
)
238+
assert (
239+
max_diff_without_multi_adapter_scale < expected_max_diff
240+
), "Output without multi-ip-adapter must be same as normal inference"
241+
assert (
242+
max_diff_with_multi_adapter_scale > 1e-2
243+
), "Output with multi-ip-adapter scale must be different from normal inference"
244244

245245

246246
class SDXLModularControlNetTests:
@@ -253,9 +253,9 @@ def test_pipeline_inputs(self):
253253
parameters = blocks.input_names
254254

255255
assert "control_image" in parameters, "`control_image` argument must be supported by the `__call__` method"
256-
assert "controlnet_conditioning_scale" in parameters, (
257-
"`controlnet_conditioning_scale` argument must be supported by the `__call__` method"
258-
)
256+
assert (
257+
"controlnet_conditioning_scale" in parameters
258+
), "`controlnet_conditioning_scale` argument must be supported by the `__call__` method"
259259

260260
def _modify_inputs_for_controlnet_test(self, inputs: Dict[str, Any]):
261261
controlnet_embedder_scale_factor = 2
@@ -301,9 +301,9 @@ def test_controlnet(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
301301
max_diff_without_controlnet_scale = np.abs(output_without_controlnet_scale - output_without_controlnet).max()
302302
max_diff_with_controlnet_scale = np.abs(output_with_controlnet_scale - output_without_controlnet).max()
303303

304-
assert max_diff_without_controlnet_scale < expected_max_diff, (
305-
"Output without controlnet must be same as normal inference"
306-
)
304+
assert (
305+
max_diff_without_controlnet_scale < expected_max_diff
306+
), "Output without controlnet must be same as normal inference"
307307
assert max_diff_with_controlnet_scale > 1e-2, "Output with controlnet must be different from normal inference"
308308

309309
def test_controlnet_cfg(self):
@@ -383,26 +383,6 @@ def test_stable_diffusion_xl_euler(self):
383383
def test_inference_batch_single_identical(self):
384384
super().test_inference_batch_single_identical(expected_max_diff=3e-3)
385385

386-
@require_torch_accelerator
387-
def test_stable_diffusion_xl_offloads(self):
388-
pipes = []
389-
sd_pipe = self.get_pipeline().to(torch_device)
390-
pipes.append(sd_pipe)
391-
392-
cm = ComponentsManager()
393-
cm.enable_auto_cpu_offload(device=torch_device)
394-
sd_pipe = self.get_pipeline(components_manager=cm)
395-
pipes.append(sd_pipe)
396-
397-
image_slices = []
398-
for pipe in pipes:
399-
inputs = self.get_dummy_inputs(torch_device)
400-
image = pipe(**inputs, output="images")
401-
402-
image_slices.append(image[0, -3:, -3:, -1].flatten())
403-
404-
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3
405-
406386
def test_stable_diffusion_xl_save_from_pretrained(self):
407387
pipes = []
408388
sd_pipe = self.get_pipeline().to(torch_device)

tests/modular_pipelines/test_modular_pipelines_common.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,40 @@ def test_num_images_per_prompt(self):
320320
assert images.shape[0] == batch_size * num_images_per_prompt
321321

322322
@require_accelerator
323-
def test_components_auto_cpu_offload(self):
323+
def test_components_auto_cpu_offload_inference_consistent(self):
324324
base_pipe = self.get_pipeline().to(torch_device)
325-
for component in base_pipe.components:
326-
assert component.device == torch_device
327325

328326
cm = ComponentsManager()
329327
cm.enable_auto_cpu_offload(device=torch_device)
330328
offload_pipe = self.get_pipeline(components_manager=cm)
329+
330+
image_slices = []
331+
for pipe in [base_pipe, offload_pipe]:
332+
inputs = self.get_dummy_inputs(torch_device)
333+
image = pipe(**inputs, output="images")
334+
335+
image_slices.append(image[0, -3:, -3:, -1].flatten())
336+
337+
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3
338+
339+
def test_save_from_pretrained(self):
340+
pipes = []
341+
base_pipe = self.get_pipeline().to(torch_device)
342+
pipes.append(base_pipe)
343+
344+
with tempfile.TemporaryDirectory() as tmpdirname:
345+
base_pipe.save_pretrained(tmpdirname)
346+
pipe = ModularPipeline.from_pretrained(tmpdirname).to(torch_device)
347+
pipe.load_default_components(torch_dtype=torch.float32)
348+
pipe.to(torch_device)
349+
350+
pipes.append(pipe)
351+
352+
image_slices = []
353+
for pipe in pipes:
354+
inputs = self.get_dummy_inputs(torch_device)
355+
image = pipe(**inputs, output="images")
356+
357+
image_slices.append(image[0, -3:, -3:, -1].flatten())
358+
359+
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3

0 commit comments

Comments
 (0)