Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
508c472
speed up test_vae_slicing in animatediff
sayakpaul Mar 26, 2024
d45699e
speed up test_karras_schedulers_shape for attend and excite.
sayakpaul Mar 26, 2024
43f5496
style.
sayakpaul Mar 26, 2024
75d5fff
Merge branch 'main' into speed-up-fastpipelines
sayakpaul Mar 27, 2024
ec4240b
get the static slices out.
sayakpaul Mar 27, 2024
70c459f
specify torch print options.
sayakpaul Mar 27, 2024
a6781d7
modify
sayakpaul Mar 27, 2024
eee7563
test run with controlnet
sayakpaul Mar 27, 2024
031e374
specify kwarg
sayakpaul Mar 27, 2024
0f71843
fix: things
sayakpaul Mar 27, 2024
62f0669
not None
sayakpaul Mar 27, 2024
aedccad
flatten
sayakpaul Mar 27, 2024
50de2cb
controlnet img2img
sayakpaul Mar 27, 2024
da64f0c
complete controlet sd
sayakpaul Mar 27, 2024
f8f79e3
finish more
sayakpaul Mar 27, 2024
77f93cd
finish more
sayakpaul Mar 27, 2024
ae43285
finish more
sayakpaul Mar 27, 2024
7d583e5
finish more
sayakpaul Mar 27, 2024
b5e1da0
finish the final batch
sayakpaul Mar 27, 2024
9fa2ffd
add cpu check for expected_pipe_slice.
sayakpaul Mar 27, 2024
b7ec1c2
finish the rest
sayakpaul Mar 27, 2024
8de2bdd
remove print
sayakpaul Mar 27, 2024
90736b0
style
sayakpaul Mar 27, 2024
9752100
fix ssd1b controlnet test
sayakpaul Mar 27, 2024
7260a1a
checking ssd1b
sayakpaul Mar 27, 2024
2cdc18e
disable the test.
sayakpaul Mar 27, 2024
f562e75
make the test_ip_adapter_single controlnet test more robust
sayakpaul Mar 27, 2024
0b0c290
fix: simple inpaint
sayakpaul Mar 27, 2024
fe4c043
multi
sayakpaul Mar 27, 2024
29a0b14
disable panorama
sayakpaul Mar 27, 2024
5b44d4e
enable again
sayakpaul Mar 27, 2024
a70526d
panorama is shaky so leave it for now
sayakpaul Mar 27, 2024
833b759
remove print
sayakpaul Mar 27, 2024
279c5b0
Merge branch 'main' into speed-up-fastpipelines
sayakpaul Mar 28, 2024
1b833ea
Merge branch 'main' into speed-up-fastpipelines
sayakpaul Mar 29, 2024
c0a6a11
raise tolerance.
sayakpaul Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/diffusers/utils/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,21 @@ def numpy_cosine_similarity_distance(a, b):
return distance


def print_tensor_test(tensor, filename="test_corrections.txt", expected_tensor_name="expected_slice"):
def print_tensor_test(
tensor,
limit_to_slices=None,
max_torch_print=None,
filename="test_corrections.txt",
expected_tensor_name="expected_slice",
):
if max_torch_print:
torch.set_printoptions(threshold=10_000)

test_name = os.environ.get("PYTEST_CURRENT_TEST")
if not torch.is_tensor(tensor):
tensor = torch.from_numpy(tensor)
if limit_to_slices:
tensor = tensor[0, -3:, -3:, -1]
Comment on lines +115 to +122
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convenience options. They are not harmful.


tensor_str = str(tensor.detach().cpu().flatten().to(torch.float32)).replace("\n", "")
# format is usually:
Expand All @@ -117,7 +128,7 @@ def print_tensor_test(tensor, filename="test_corrections.txt", expected_tensor_n
test_file, test_class, test_fn = test_name.split("::")
test_fn = test_fn.split()[0]
with open(filename, "a") as f:
print(";".join([test_file, test_class, test_fn, output_str]), file=f)
print("::".join([test_file, test_class, test_fn, output_str]), file=f)


def get_tests_dir(append_path=None):
Expand Down
39 changes: 39 additions & 0 deletions tests/pipelines/animatediff/test_animatediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ def test_motion_unet_loading(self):
def test_attention_slicing_forward_pass(self):
pass

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
Comment on lines +134 to +136
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We make use of torch_device which can change based on the environment. The slices below were obtained on Intel CPUs which is our default mode and used on the PR tests too.

For accelerators, the tests will run much faster anyway (even on MPS). So, I think this is the way to go here. But I welcome any other ideas too.

expected_pipe_slice = np.array(
[
0.5541,
0.5802,
0.5074,
0.4583,
0.4729,
0.5374,
0.4051,
0.4495,
0.4480,
0.5292,
0.6322,
0.6265,
0.5455,
0.4771,
0.5795,
0.5845,
0.4172,
0.6066,
0.6535,
0.4113,
0.6833,
0.5736,
0.3589,
0.5730,
0.4205,
0.3786,
0.5323,
]
)
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_inference_batch_single_identical(
self,
batch_size=2,
Expand Down Expand Up @@ -299,6 +335,9 @@ def test_xformers_attention_forwardGenerator_pass(self):
max_diff = np.abs(to_np(output_with_offload) - to_np(output_without_offload)).max()
self.assertLess(max_diff, 1e-4, "XFormers attention should not affect the inference results")

def test_vae_slicing(self):
return super().test_vae_slicing(image_count=2)


@slow
@require_torch_gpu
Expand Down
28 changes: 28 additions & 0 deletions tests/pipelines/animatediff/test_animatediff_video2video.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ def test_motion_unet_loading(self):
def test_attention_slicing_forward_pass(self):
pass

def test_ip_adapter_single(self):
expected_pipe_slice = None

if torch_device == "cpu":
expected_pipe_slice = np.array(
[
0.4947,
0.4780,
0.4340,
0.4666,
0.4028,
0.4645,
0.4915,
0.4101,
0.4308,
0.4581,
0.3582,
0.4953,
0.4466,
0.5348,
0.5863,
0.5299,
0.5213,
0.5017,
]
)
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_inference_batch_single_identical(
self,
batch_size=2,
Expand Down
18 changes: 18 additions & 0 deletions tests/pipelines/controlnet/test_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ def get_dummy_inputs(self, device, seed=0):
def test_attention_slicing_forward_pass(self):
return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3)

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.5234, 0.3333, 0.1745, 0.7605, 0.6224, 0.4637, 0.6989, 0.7526, 0.4665])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

@unittest.skipIf(
torch_device != "cuda" or not is_xformers_available(),
reason="XFormers attention is only available with CUDA and `xformers` installed",
Expand Down Expand Up @@ -455,6 +461,12 @@ def test_xformers_attention_forwardGenerator_pass(self):
def test_inference_batch_single_identical(self):
self._test_inference_batch_single_identical(expected_max_diff=2e-3)

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.2422, 0.3425, 0.4048, 0.5351, 0.3503, 0.2419, 0.4645, 0.4570, 0.3804])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_save_pretrained_raise_not_implemented_exception(self):
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)
Expand Down Expand Up @@ -668,6 +680,12 @@ def test_xformers_attention_forwardGenerator_pass(self):
def test_inference_batch_single_identical(self):
self._test_inference_batch_single_identical(expected_max_diff=2e-3)

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.5264, 0.3203, 0.1602, 0.8235, 0.6332, 0.4593, 0.7226, 0.7777, 0.4780])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_save_pretrained_raise_not_implemented_exception(self):
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)
Expand Down
12 changes: 12 additions & 0 deletions tests/pipelines/controlnet/test_controlnet_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ def get_dummy_inputs(self, device, seed=0):
def test_attention_slicing_forward_pass(self):
return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3)

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.7096, 0.5149, 0.3571, 0.5897, 0.4715, 0.4052, 0.6098, 0.6886, 0.4213])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

@unittest.skipIf(
torch_device != "cuda" or not is_xformers_available(),
reason="XFormers attention is only available with CUDA and `xformers` installed",
Expand Down Expand Up @@ -366,6 +372,12 @@ def test_xformers_attention_forwardGenerator_pass(self):
def test_inference_batch_single_identical(self):
self._test_inference_batch_single_identical(expected_max_diff=2e-3)

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.5293, 0.7339, 0.6642, 0.3950, 0.5212, 0.5175, 0.7002, 0.5907, 0.5182])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_save_pretrained_raise_not_implemented_exception(self):
components = self.get_dummy_components()
pipe = self.pipeline_class(**components)
Expand Down
15 changes: 15 additions & 0 deletions tests/pipelines/controlnet/test_controlnet_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ def get_dummy_inputs(self, device, seed=0):
def test_attention_slicing_forward_pass(self):
return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3)

def test_ip_adapter_single(self, from_ssd1b=False, expected_pipe_slice=None):
if not from_ssd1b:
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array(
[0.7331, 0.5907, 0.5667, 0.6029, 0.5679, 0.5968, 0.4033, 0.4761, 0.5090]
)
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we pass device = "cpu" to test_ip_adapter_single when using slice? would it help precision?
I saw a test failing right now ......

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #7477 (comment).

expected_slice is always None when the torch_device is not CPU.


@unittest.skipIf(
torch_device != "cuda" or not is_xformers_available(),
reason="XFormers attention is only available with CUDA and `xformers` installed",
Expand Down Expand Up @@ -1042,6 +1051,12 @@ def test_controlnet_sdxl_guess(self):
# make sure that it's equal
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-4

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.6832, 0.5703, 0.5460, 0.6300, 0.5856, 0.6034, 0.4494, 0.4613, 0.5036])
return super().test_ip_adapter_single(from_ssd1b=True, expected_pipe_slice=expected_pipe_slice)

def test_controlnet_sdxl_lcm(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator

Expand Down
6 changes: 6 additions & 0 deletions tests/pipelines/controlnet/test_controlnet_sdxl_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ def get_dummy_inputs(self, device, seed=0):

return inputs

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.6265, 0.5441, 0.5384, 0.5446, 0.5810, 0.5908, 0.5414, 0.5428, 0.5353])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_stable_diffusion_xl_controlnet_img2img(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def get_dummy_inputs(self, device, seed=0):
}
return inputs

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.1403, 0.5072, 0.5316, 0.1202, 0.3865, 0.4211, 0.5363, 0.3557, 0.3645])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_lcm_onestep(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def get_dummy_inputs(self, device, seed=0):
}
return inputs

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.4003, 0.3718, 0.2863, 0.5500, 0.5587, 0.3772, 0.4617, 0.4961, 0.4417])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_lcm_onestep(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator

Expand Down
37 changes: 37 additions & 0 deletions tests/pipelines/pia/test_pia.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,43 @@ def test_motion_unet_loading(self):

assert isinstance(pipe.unet, UNetMotionModel)

def test_ip_adapter_single(self):
expected_pipe_slice = None

if torch_device == "cpu":
expected_pipe_slice = np.array(
[
0.5609,
0.5756,
0.4830,
0.4420,
0.4547,
0.5129,
0.3779,
0.4042,
0.3772,
0.4450,
0.5710,
0.5536,
0.4835,
0.4308,
0.5578,
0.5578,
0.4395,
0.5440,
0.6051,
0.4651,
0.6258,
0.5662,
0.3988,
0.5108,
0.4153,
0.3993,
0.4803,
]
)
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

@unittest.skip("Attention slicing is not enabled in this pipeline")
def test_attention_slicing_forward_pass(self):
pass
Expand Down
6 changes: 6 additions & 0 deletions tests/pipelines/stable_diffusion/test_stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ def test_stable_diffusion_prompt_embeds_with_plain_negative_prompt_list(self):

assert np.abs(image_slice_1.flatten() - image_slice_2.flatten()).max() < 1e-4

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.3203, 0.4555, 0.4711, 0.3505, 0.3973, 0.4650, 0.5137, 0.3392, 0.4045])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_stable_diffusion_ddim_factor_8(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ def test_stable_diffusion_img2img_negative_prompt(self):

assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.4932, 0.5092, 0.5135, 0.5517, 0.5626, 0.6621, 0.6490, 0.5021, 0.5441])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_stable_diffusion_img2img_multiple_init_images(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
Expand Down
15 changes: 15 additions & 0 deletions tests/pipelines/stable_diffusion/test_stable_diffusion_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ def callback_on_step_end(pipe, i, t, callback_kwargs):
# they should be the same
assert torch.allclose(intermediate_latent, output_interrupted, atol=1e-4)

def test_ip_adapter_single(self, from_simple=False, expected_pipe_slice=None):
if not from_simple:
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array(
[0.4390, 0.5452, 0.3772, 0.5448, 0.6031, 0.4480, 0.5194, 0.4687, 0.4640]
)
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)


class StableDiffusionSimpleInpaintPipelineFastTests(StableDiffusionInpaintPipelineFastTests):
pipeline_class = StableDiffusionInpaintPipeline
Expand Down Expand Up @@ -475,6 +484,12 @@ def get_dummy_inputs_2images(self, device, seed=0, img_res=64):
}
return inputs

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.6345, 0.5395, 0.5611, 0.5403, 0.5830, 0.5855, 0.5193, 0.5443, 0.5211])
return super().test_ip_adapter_single(from_simple=True, expected_pipe_slice=expected_pipe_slice)

def test_stable_diffusion_inpaint(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def test_save_load_local(self):
def test_save_load_optional_components(self):
super().test_save_load_optional_components(expected_max_difference=4e-4)

def test_karras_schedulers_shape(self):
super().test_karras_schedulers_shape(num_inference_steps_for_strength_for_iterations=3)


@require_torch_gpu
@nightly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ def test_stable_diffusion_xl_negative_prompt_embeds(self):
# make sure that it's equal
assert np.abs(image_slice_1.flatten() - image_slice_2.flatten()).max() < 1e-4

def test_ip_adapter_single(self):
expected_pipe_slice = None
if torch_device == "cpu":
expected_pipe_slice = np.array([0.5552, 0.5569, 0.4725, 0.4348, 0.4994, 0.4632, 0.5142, 0.5012, 0.4700])
return super().test_ip_adapter_single(expected_pipe_slice=expected_pipe_slice)

def test_attention_slicing_forward_pass(self):
super().test_attention_slicing_forward_pass(expected_max_diff=3e-3)

Expand Down
Loading