-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Improve post-processing performance #10159
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
Closed
soof-golan
wants to merge
1
commit into
huggingface:main
from
soof-golan:perf-denormlization-vectorization
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,7 +236,7 @@ def denormalize(images: Union[np.ndarray, torch.Tensor]) -> Union[np.ndarray, to | |
| `np.ndarray` or `torch.Tensor`: | ||
| The denormalized image array. | ||
| """ | ||
| return (images / 2 + 0.5).clamp(0, 1) | ||
| return (images * 0.5 + 0.5).clamp(0, 1) | ||
|
|
||
| @staticmethod | ||
| def convert_to_rgb(image: PIL.Image.Image) -> PIL.Image.Image: | ||
|
|
@@ -537,6 +537,27 @@ def binarize(self, image: PIL.Image.Image) -> PIL.Image.Image: | |
|
|
||
| return image | ||
|
|
||
| def _denormalize_conditionally( | ||
| self, images: torch.Tensor, do_denormalize: Optional[List[bool]] = None | ||
| ) -> torch.Tensor: | ||
| r""" | ||
| Denormalize a batch of images based on a condition list. | ||
|
|
||
| Args: | ||
| images (`torch.Tensor`): | ||
| The input image tensor. | ||
| do_denormalize (`Optional[List[bool]`, *optional*, defaults to `None`): | ||
| A list of booleans indicating whether to denormalize each image in the batch. If `None`, will use the | ||
| value of `do_normalize` in the `VaeImageProcessor` config. | ||
| """ | ||
| if do_denormalize is None: | ||
| return self.denormalize(images) if self.config.do_normalize else images | ||
|
|
||
| # De-normalizing a batch and selectively torch.stack'ing the results turns out to be | ||
| # significantly faster than performing a lot of smaller denormalizations | ||
| denormalized = self.denormalize(images) | ||
| return torch.stack([denormalized[i] if do_denormalize[i] else images[i] for i in range(images.shape[0])]) | ||
|
|
||
| def get_default_height_width( | ||
| self, | ||
| image: Union[PIL.Image.Image, np.ndarray, torch.Tensor], | ||
|
|
@@ -752,12 +773,7 @@ def postprocess( | |
| if output_type == "latent": | ||
| return image | ||
|
|
||
| if do_denormalize is None: | ||
| do_denormalize = [self.config.do_normalize] * image.shape[0] | ||
|
|
||
| image = torch.stack( | ||
| [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])] | ||
| ) | ||
| image = self._denormalize_conditionally(image, do_denormalize) | ||
|
Collaborator
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. if do_denormalize is None:
image = self.denormalize(images) if self.config.do_normalize
else:
image = torch.stack(
[self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])]
) |
||
|
|
||
| if output_type == "pt": | ||
| return image | ||
|
|
@@ -966,12 +982,7 @@ def postprocess( | |
| deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False) | ||
| output_type = "np" | ||
|
|
||
| if do_denormalize is None: | ||
| do_denormalize = [self.config.do_normalize] * image.shape[0] | ||
|
|
||
| image = torch.stack( | ||
| [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])] | ||
| ) | ||
| image = self._denormalize_conditionally(image, do_denormalize) | ||
|
|
||
| image = self.pt_to_numpy(image) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is some context to that, each image in the batch may have a different value for
do_normalizefor sd1.5, see the code herediffusers/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py
Line 1075 in 6131a93
Since most of the new pipelines (sdxl, sd3, flux), we do not pass the
do_normalizefrom the pipeline, i.e.do_normalize is Nonehere , see SDXL https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py#L1304 ,and we already did this so it will be batch processed for all the new pipelines already, I think this line is sufficient here