Skip to content

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

Conversation

soof-golan
Copy link
Contributor

What does this PR do?

Fixes: #10158

  • Use multiplication instead of division in VaeImageProcessor.denormalize
  • Avoid splitting and re-stacking tensors to reduce memory bandwidth and CPU-GPU syncs

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@hlky
Copy link
Contributor

hlky commented Dec 9, 2024

Thanks for your contribution @soof-golan! Can you run make style?

* Use multiplication instead of division
* Avoid splitting and re-stacking tensors to reduce memory bandwidth and CPU-GPU syncs
@soof-golan soof-golan force-pushed the perf-denormlization-vectorization branch from f94fd19 to 61fc97b Compare December 9, 2024 16:25
@soof-golan
Copy link
Contributor Author

Thanks for your contribution @soof-golan! Can you run make style?

@hlky Done!


# 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)
Copy link
Collaborator

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_normalize for sd1.5, see the code here

image = self.image_processor.postprocess(image, output_type=output_type, do_denormalize=do_denormalize)

Since most of the new pipelines (sdxl, sd3, flux), we do not pass the do_normalize from the pipeline, i.e. do_normalize is None here , 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

        if do_denormalize is None:
            return self.denormalize(images) if self.config.do_normalize else images

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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])]
        )

@soof-golan
Copy link
Contributor Author

@yiyixuxu I've opened up #10170 with only the simpler optimization, leaving the homogeneous case untouched.

Choose whichever you see fit for the scope of the project :)

@hlky
Copy link
Contributor

hlky commented Dec 10, 2024

Closed by #10170

@hlky hlky closed this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Post processing performance can be improved
4 participants