Skip to content

Conversation

@patrickvonplaten
Copy link
Contributor

@patrickvonplaten patrickvonplaten commented Sep 2, 2022

Model Outputs

This PR allows the user to switch between tuple and Dict/Dataclass outputs for schedulers, models and pipelines.
Tuples are now returned whenever the return_dict flag is set to False of the respective step, forward and __call__ functions.

New recommended API:

Having merged this PR, users are recommended to use schedulers as following:

sample = scheduler.step(...).prev_sample

instead of

sample = scheduler.step(...)["prev_sample"]  # which is still possible though!

,models as following:

sample = unet(...).sample

instead of

sample = unet(...)["sample"]  # which is still possible though!

, and pipelines:

image = pipe(...).images[0]

instead of

image = pipe(...)["sample"][0]  # which is still possible, **but now has a deprecating warning**

Tests

Extensive comparison tests are added for schedulers, models, and fast pipeline tests

🚨 Deprecation Warnings 🚨

In this PR we're deprecating the "sample" output keyword for pipelines. Pipelines are modality specific and therefore it is more intuitive to call pipe(...).images[0]

🚨🚨🚨 Breaking changes 🚨🚨🚨

This PR introduces breaking changes for the following public-facing methods:

  • VQModel.encode -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change latents = model.encode(...) to latents = model.encode(...)[0] or latents = model.encode(...).latens
  • VQModel.decode -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change sample = model.decode(...) to sample = model.decode(...)[0] or sample = model.decode(...).sample
  • VQModel.forward -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change sample = model(...) to sample = model(...)[0] or sample = model(...).sample
  • AutoencoderKL.encode -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change latent_dist = model.encode(...) to latent_dist = model.encode(...)[0] or latent_dist = model.encode(...).latent_dist
  • AutoencoderKL.decode -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change sample = model.decode(...) to sample = model.decode(...)[0] or sample = model.decode(...).sample
  • AutoencoderKL.forward -> we return a dict/dataclass instead of a single tensor. In the future it's very likely required to return more than just one tensor. Please make sure to change sample = model(...) to sample = model(...)[0] or sample = model(...).sample

TODOS once PR is merged

@patrickvonplaten patrickvonplaten linked an issue Sep 2, 2022 that may be closed by this pull request
@patrickvonplaten patrickvonplaten changed the title [ModelOutputs] Add outputs for models and allow tuple to be returned [WIP][ModelOutputs] Add outputs for models and allow tuple to be returned Sep 2, 2022
@patrickvonplaten patrickvonplaten changed the title [WIP][ModelOutputs] Add outputs for models and allow tuple to be returned [ModelOutputs] Add outputs for models and allow tuple to be returned Sep 3, 2022
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Sep 3, 2022

The documentation is not available anymore as the PR was closed or merged.

@patrickvonplaten
Copy link
Contributor Author

Checked that all slow tests run as expected ✔️ - some tests are failing but they are currently also failing on master

@patrickvonplaten patrickvonplaten changed the title [ModelOutputs] Add outputs for models and allow tuple to be returned [ModelOutputs] Replace dict outputs with Dict/Dataclass and allow to return tuples Sep 3, 2022
Copy link
Member

@pcuenca pcuenca left a comment

Choose a reason for hiding this comment

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

Really comprehensive work, very impressive! We should merge it soon and deal with the breaking changes as it permeates the whole codebase.

We also need to adapt the training examples when we merge.

`DiagonalGaussianDistribution` allows for sampling latents from the distribution.
"""

latent_dist: torch.FloatTensor = None
Copy link
Member

Choose a reason for hiding this comment

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

This should be of type DiagonalGaussianDistribution as the comment says.

The problem is that DiagonalGaussianDistribution is defined later in the file. Solutions:

  • Move this down. It would be out of place with respect to the other output dataclasses.
  • Declare class DiagonalGaussianDistribution: pass before, and comment it's a "forward" declaration.
  • Use object as the type, with a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the string version instead as suggested here: https://peps.python.org/pep-0484/#forward-references

Copy link
Member

Choose a reason for hiding this comment

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

Ohh, nice!

Copy link
Contributor

@patil-suraj patil-suraj left a comment

Choose a reason for hiding this comment

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

Thanks a lot for working on this big PR! In general everything looks good, just left some nit.

My only big comment, same as Pedro's is, should we rename ModelOutput to something else, as this is used for pipelines, and schedulers as well.

`DiagonalGaussianDistribution` allows for sampling latents from the distribution.
"""

latent_dist: torch.FloatTensor = None
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Co-authored-by: Suraj Patil <[email protected]>
Co-authored-by: Pedro Cuenca <[email protected]>
@patrickvonplaten patrickvonplaten merged commit cc59b05 into main Sep 5, 2022
@patrickvonplaten patrickvonplaten deleted the add_model_outputs branch September 5, 2022 12:49
natolambert pushed a commit that referenced this pull request Sep 7, 2022
…return tuples (#334)

* add outputs for models

* add for pipelines

* finish schedulers

* better naming

* adapt tests as well

* replace dict access with . access

* make schedulers works

* finish

* correct readme

* make  bcp compatible

* up

* small fix

* finish

* more fixes

* more fixes

* Apply suggestions from code review

Co-authored-by: Suraj Patil <[email protected]>
Co-authored-by: Pedro Cuenca <[email protected]>

* Update src/diffusers/models/vae.py

Co-authored-by: Pedro Cuenca <[email protected]>

* Adapt model outputs

* Apply more suggestions

* finish examples

* correct

Co-authored-by: Suraj Patil <[email protected]>
Co-authored-by: Pedro Cuenca <[email protected]>
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…return tuples (huggingface#334)

* add outputs for models

* add for pipelines

* finish schedulers

* better naming

* adapt tests as well

* replace dict access with . access

* make schedulers works

* finish

* correct readme

* make  bcp compatible

* up

* small fix

* finish

* more fixes

* more fixes

* Apply suggestions from code review

Co-authored-by: Suraj Patil <[email protected]>
Co-authored-by: Pedro Cuenca <[email protected]>

* Update src/diffusers/models/vae.py

Co-authored-by: Pedro Cuenca <[email protected]>

* Adapt model outputs

* Apply more suggestions

* finish examples

* correct

Co-authored-by: Suraj Patil <[email protected]>
Co-authored-by: Pedro Cuenca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ONNX compatibility issues

6 participants