Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,30 @@ def check_inputs(
f" {negative_prompt_embeds.shape}."
)

if (indices is None) or (indices is not None and not isinstance(indices, List)):
raise ValueError(f"`indices` has to be a list but is {type(indices)}")
indices_is_list_ints = isinstance(indices, list) and isinstance(indices[0], int)
indices_is_list_list_ints = (
isinstance(indices, list) and isinstance(indices[0], list) and isinstance(indices[0][0], int)
)

if not indices_is_list_ints and not indices_is_list_list_ints:
raise TypeError("`indices` must be a list of ints or a list of a list of ints")

if indices_is_list_ints:
indices_batch_size = 1
elif indices_is_list_list_ints:
indices_batch_size = len(indices)

if prompt is not None and isinstance(prompt, str):
prompt_batch_size = 1
elif prompt is not None and isinstance(prompt, list):
prompt_batch_size = len(prompt)
elif prompt_embeds is not None:
prompt_batch_size = prompt_embeds.shape[0]

if indices_batch_size != prompt_batch_size:
raise ValueError(
f"indices batch size must be same as prompt batch size. indices batch size: {indices_batch_size}, prompt batch size: {prompt_batch_size}"
)

# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latents
def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
Expand Down Expand Up @@ -675,7 +697,7 @@ def get_indices(self, prompt: str) -> Dict[str, int]:
def __call__(
self,
prompt: Union[str, List[str]],
token_indices: List[int],
token_indices: Union[List[int], List[List[int]]],
height: Optional[int] = None,
width: Optional[int] = None,
num_inference_steps: int = 50,
Expand Down Expand Up @@ -851,7 +873,9 @@ def __call__(

if isinstance(token_indices[0], int):
token_indices = [token_indices]

indices = []

for ind in token_indices:
indices = indices + [ind] * num_images_per_prompt

Expand Down
13 changes: 0 additions & 13 deletions tests/pipelines/paint_by_example/test_paint_by_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,6 @@ def test_paint_by_example_image_tensor(self):
assert out_1.shape == (1, 64, 64, 3)
assert np.abs(out_1.flatten() - out_2.flatten()).max() < 5e-2

def test_paint_by_example_inpaint_with_num_images_per_prompt(self):
device = "cpu"
pipe = PaintByExamplePipeline(**self.get_dummy_components())
pipe = pipe.to(device)
pipe.set_progress_bar_config(disable=None)

inputs = self.get_dummy_inputs()

images = pipe(**inputs, num_images_per_prompt=2).images

# check if the output is a list of 2 images
assert len(images) == 2


@slow
@require_torch_gpu
Expand Down
2 changes: 1 addition & 1 deletion tests/pipelines/stable_diffusion/test_cycle_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CycleDiffusionPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
"negative_prompt_embeds",
}
required_optional_params = PipelineTesterMixin.required_optional_params - {"latents"}
batch_params = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
batch_params = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"source_prompt"})

def get_dummy_components(self):
torch.manual_seed(0)
Expand Down
37 changes: 0 additions & 37 deletions tests/pipelines/stable_diffusion/test_stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,43 +477,6 @@ def test_stable_diffusion_negative_prompt(self):

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

def test_stable_diffusion_num_images_per_prompt(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
components["scheduler"] = PNDMScheduler(skip_prk_steps=True)
sd_pipe = StableDiffusionPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

prompt = "A painting of a squirrel eating a burger"

# test num_images_per_prompt=1 (default)
images = sd_pipe(prompt, num_inference_steps=2, output_type="np").images

assert images.shape == (1, 64, 64, 3)

# test num_images_per_prompt=1 (default) for batch of prompts
batch_size = 2
images = sd_pipe([prompt] * batch_size, num_inference_steps=2, output_type="np").images

assert images.shape == (batch_size, 64, 64, 3)

# test num_images_per_prompt for single prompt
num_images_per_prompt = 2
images = sd_pipe(
prompt, num_inference_steps=2, output_type="np", num_images_per_prompt=num_images_per_prompt
).images

assert images.shape == (num_images_per_prompt, 64, 64, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
images = sd_pipe(
[prompt] * batch_size, num_inference_steps=2, output_type="np", num_images_per_prompt=num_images_per_prompt
).images

assert images.shape == (batch_size * num_images_per_prompt, 64, 64, 3)

def test_stable_diffusion_long_prompt(self):
components = self.get_dummy_components()
components["scheduler"] = LMSDiscreteScheduler.from_config(components["scheduler"].config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,6 @@ def test_stable_diffusion_img_variation_multiple_images(self):

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

def test_stable_diffusion_img_variation_num_images_per_prompt(self):
device = "cpu"
components = self.get_dummy_components()
sd_pipe = StableDiffusionImageVariationPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

# test num_images_per_prompt=1 (default)
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs).images

assert images.shape == (1, 64, 64, 3)

# test num_images_per_prompt=1 (default) for batch of images
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["image"] = batch_size * [inputs["image"]]
images = sd_pipe(**inputs).images

assert images.shape == (batch_size, 64, 64, 3)

# test num_images_per_prompt for single prompt
num_images_per_prompt = 2
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (num_images_per_prompt, 64, 64, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["image"] = batch_size * [inputs["image"]]
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (batch_size * num_images_per_prompt, 64, 64, 3)


@slow
@require_torch_gpu
Expand Down
36 changes: 0 additions & 36 deletions tests/pipelines/stable_diffusion/test_stable_diffusion_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,42 +181,6 @@ def test_stable_diffusion_img2img_k_lms(self):

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

def test_stable_diffusion_img2img_num_images_per_prompt(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
sd_pipe = StableDiffusionImg2ImgPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

# test num_images_per_prompt=1 (default)
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs).images

assert images.shape == (1, 32, 32, 3)

# test num_images_per_prompt=1 (default) for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs).images

assert images.shape == (batch_size, 32, 32, 3)

# test num_images_per_prompt for single prompt
num_images_per_prompt = 2
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (num_images_per_prompt, 32, 32, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (batch_size * num_images_per_prompt, 32, 32, 3)

@skip_mps
def test_save_load_local(self):
return super().test_save_load_local()
Expand Down
13 changes: 0 additions & 13 deletions tests/pipelines/stable_diffusion/test_stable_diffusion_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,6 @@ def test_stable_diffusion_inpaint_image_tensor(self):
assert out_pil.shape == (1, 64, 64, 3)
assert np.abs(out_pil.flatten() - out_tensor.flatten()).max() < 5e-2

def test_stable_diffusion_inpaint_with_num_images_per_prompt(self):
device = "cpu"
components = self.get_dummy_components()
sd_pipe = StableDiffusionInpaintPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=2).images

# check if the output is a list of 2 images
assert len(images) == 2


@slow
@require_torch_gpu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,42 +191,6 @@ def test_stable_diffusion_pix2pix_euler(self):

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

def test_stable_diffusion_pix2pix_num_images_per_prompt(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
sd_pipe = StableDiffusionInstructPix2PixPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

# test num_images_per_prompt=1 (default)
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs).images

assert images.shape == (1, 32, 32, 3)

# test num_images_per_prompt=1 (default) for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs).images

assert images.shape == (batch_size, 32, 32, 3)

# test num_images_per_prompt for single prompt
num_images_per_prompt = 2
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (num_images_per_prompt, 32, 32, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (batch_size * num_images_per_prompt, 32, 32, 3)


@slow
@require_torch_gpu
Expand Down
36 changes: 0 additions & 36 deletions tests/pipelines/stable_diffusion/test_stable_diffusion_panorama.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,42 +177,6 @@ def test_stable_diffusion_panorama_pndm(self):
with self.assertRaises(ValueError):
_ = sd_pipe(**inputs).images

def test_stable_diffusion_panorama_num_images_per_prompt(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
sd_pipe = StableDiffusionPanoramaPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

# test num_images_per_prompt=1 (default)
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs).images

assert images.shape == (1, 64, 64, 3)

# test num_images_per_prompt=1 (default) for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs).images

assert images.shape == (batch_size, 64, 64, 3)

# test num_images_per_prompt for single prompt
num_images_per_prompt = 2
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (num_images_per_prompt, 64, 64, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (batch_size * num_images_per_prompt, 64, 64, 3)


@slow
@require_torch_gpu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,34 +191,6 @@ def test_stable_diffusion_pix2pix_zero_ddpm(self):

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

def test_stable_diffusion_pix2pix_zero_num_images_per_prompt(self):
device = "cpu" # ensure determinism for the device-dependent torch.Generator
components = self.get_dummy_components()
sd_pipe = StableDiffusionPix2PixZeroPipeline(**components)
sd_pipe = sd_pipe.to(device)
sd_pipe.set_progress_bar_config(disable=None)

# test num_images_per_prompt=1 (default)
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs).images

assert images.shape == (1, 64, 64, 3)

# test num_images_per_prompt=2 for a single prompt
num_images_per_prompt = 2
inputs = self.get_dummy_inputs(device)
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (num_images_per_prompt, 64, 64, 3)

# test num_images_per_prompt for batch of prompts
batch_size = 2
inputs = self.get_dummy_inputs(device)
inputs["prompt"] = [inputs["prompt"]] * batch_size
images = sd_pipe(**inputs, num_images_per_prompt=num_images_per_prompt).images

assert images.shape == (batch_size * num_images_per_prompt, 64, 64, 3)

# Non-determinism caused by the scheduler optimizing the latent inputs during inference
@unittest.skip("non-deterministic pipeline")
def test_inference_batch_single_identical(self):
Expand Down
Loading