-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Make sure all pipelines can run with batched input #1669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8ddb1d7
e60c350
8d10829
6bd5cae
922f21d
92ff4b6
e8d1ed7
056d477
da1b37d
d6ef2a3
7070369
57bec6b
5ed089a
3141275
b450b8a
4316ccd
4a26e71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,6 @@ def __call__( | |
| use_clipped_model_output: Optional[bool] = None, | ||
| output_type: Optional[str] = "pil", | ||
| return_dict: bool = True, | ||
| **kwargs, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never used |
||
| ) -> Union[ImagePipelineOutput, Tuple]: | ||
| r""" | ||
| Args: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,14 +35,26 @@ | |
| logger = logging.get_logger(__name__) # pylint: disable=invalid-name | ||
|
|
||
|
|
||
| # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.preprocess | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use already existing pre-processing functions |
||
| def preprocess(image): | ||
| w, h = image.size | ||
| w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32 | ||
| image = image.resize((w, h), resample=PIL_INTERPOLATION["lanczos"]) | ||
| image = np.array(image).astype(np.float32) / 255.0 | ||
| image = image[None].transpose(0, 3, 1, 2) | ||
| image = torch.from_numpy(image) | ||
| return 2.0 * image - 1.0 | ||
| if isinstance(image, torch.Tensor): | ||
| return image | ||
| elif isinstance(image, PIL.Image.Image): | ||
| image = [image] | ||
|
|
||
| if isinstance(image[0], PIL.Image.Image): | ||
| w, h = image[0].size | ||
| w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32 | ||
|
|
||
| image = [np.array(i.resize((w, h), resample=PIL_INTERPOLATION["lanczos"]))[None, :] for i in image] | ||
| image = np.concatenate(image, axis=0) | ||
| image = np.array(image).astype(np.float32) / 255.0 | ||
| image = image.transpose(0, 3, 1, 2) | ||
| image = 2.0 * image - 1.0 | ||
| image = torch.from_numpy(image) | ||
| elif isinstance(image[0], torch.Tensor): | ||
| image = torch.cat(image, dim=0) | ||
| return image | ||
|
|
||
|
|
||
| def posterior_sample(scheduler, latents, timestep, clean_latents, generator, eta): | ||
|
|
@@ -279,9 +291,9 @@ def _encode_prompt(self, prompt, device, num_images_per_prompt, do_classifier_fr | |
| return_tensors="pt", | ||
| ) | ||
| text_input_ids = text_inputs.input_ids | ||
| untruncated_ids = self.tokenizer(prompt, padding="max_length", return_tensors="pt").input_ids | ||
| untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids | ||
|
|
||
| if not torch.equal(text_input_ids, untruncated_ids): | ||
| if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids): | ||
| removed_text = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]) | ||
| logger.warning( | ||
| "The following part of your input was truncated because CLIP can only handle sequences up to" | ||
|
|
@@ -551,8 +563,7 @@ def __call__( | |
| ) | ||
|
|
||
| # 4. Preprocess image | ||
| if isinstance(image, PIL.Image.Image): | ||
| image = preprocess(image) | ||
| image = preprocess(image) | ||
|
|
||
| # 5. Prepare timesteps | ||
| self.scheduler.set_timesteps(num_inference_steps, device=device) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,9 +248,9 @@ def _encode_prompt(self, prompt, device, num_images_per_prompt, do_classifier_fr | |
| return_tensors="pt", | ||
| ) | ||
| text_input_ids = text_inputs.input_ids | ||
| untruncated_ids = self.tokenizer(prompt, padding="max_length", return_tensors="pt").input_ids | ||
| untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes: #1546 |
||
|
|
||
| if not torch.equal(text_input_ids, untruncated_ids): | ||
| if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids): | ||
| removed_text = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]) | ||
| logger.warning( | ||
| "The following part of your input was truncated because CLIP can only handle sequences up to" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there were problems with
broadcastonmps, I'll verify if they have been resolved.