Skip to content

Conversation

virginiafdez
Copy link
Contributor

Fixes #8448

Description

Classifier-free guidance scale can be used in the sampling methods of diffusion models to strengthen the conditioning. It's been used in one of the generative MONAI tutorials, but the actual Inferers (DiffusionInferer, and ControlNetDiffusionInferer) do not support this feature. This means that, whenever users want to use CFG, they have to either copy the Inferer object or write their own sampling method.
This PR incorporates classifier-free guidance into the inferer objects by modifying their sampling method and adding an argument cfg to the sampling method to control this.
This should not change the default behaviour (cfg=None), although some rewriting has been necessary.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes (to test_controlnet_inferers.py, test_diffusion_inferer.py and test_latent_diffusion_inferer.py).
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

Virginia Fernandez and others added 12 commits May 9, 2025 15:10
Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
….ac.uk>\nI, Virginia Fernandez <virginia.fernandez@kcl.ac.uk>, hereby add my Signed-off-by to this commit: 79f357a\nSigned-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>

Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
….ac.uk>

I, Virginia Fernandez <virginia.fernandez@kcl.ac.uk>, hereby add my Signed-off-by to this commit: 79f357a

Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
Signed-off-by: Virginia Fernandez <virginia.fernandez@kcl.ac.uk>
@virginiafdez virginiafdez marked this pull request as ready for review May 27, 2025 13:46
@ericspod ericspod requested a review from KumoLiu May 27, 2025 13:52
Copy link
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@KumoLiu
Copy link
Contributor

KumoLiu commented May 27, 2025

/build

@ericspod ericspod enabled auto-merge (squash) May 27, 2025 15:32
@ericspod ericspod merged commit 2d41655 into Project-MONAI:dev May 27, 2025
25 checks passed
@virginiafdez virginiafdez deleted the classifier-free-guidance branch September 8, 2025 10:39
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.

Classifier-free guidance added to the Inferers

3 participants