Skip to content

Commit

Permalink
6676 port diffusion schedulers (#7364)
Browse files Browse the repository at this point in the history
This is an update to PR #7332
- I addressed the comments but failed to push the changes before it was
merged! Changes are very minor.

### Description

A few sentences describing the changes proposed in this pull request.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] 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.

---------

Signed-off-by: Mark Graham <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
marksgraham and pre-commit-ci[bot] authored Jan 5, 2024
1 parent 3447b09 commit 3ab5c62
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions monai/networks/schedulers/ddim.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,10 @@
import numpy as np
import torch

from monai.utils import StrEnum

from .ddpm import DDPMPredictionType
from .scheduler import Scheduler


class DDIMPredictionType(StrEnum):
"""
Set of valid prediction type names for the DDIM scheduler's `prediction_type` argument.
epsilon: predicting the noise of the diffusion process
sample: directly predicting the noisy sample
v_prediction: velocity prediction, see section 2.4 https://imagen.research.google/video/paper.pdf
"""

EPSILON = "epsilon"
SAMPLE = "sample"
V_PREDICTION = "v_prediction"
DDIMPredictionType = DDPMPredictionType


class DDIMScheduler(Scheduler):
Expand Down Expand Up @@ -126,6 +113,13 @@ def set_timesteps(self, num_inference_steps: int, device: str | torch.device | N

self.num_inference_steps = num_inference_steps
step_ratio = self.num_train_timesteps // self.num_inference_steps
if self.steps_offset >= step_ratio:
raise ValueError(
f"`steps_offset`: {self.steps_offset} cannot be greater than or equal to "
f"`num_train_timesteps // num_inference_steps : {step_ratio}` as this will cause timesteps to exceed"
f" the max train timestep."
)

# creates integer timesteps by multiplying by ratio
# casting to int to avoid issues when num_inference_step is power of 3
timesteps = (np.arange(0, num_inference_steps) * step_ratio).round()[::-1].copy().astype(np.int64)
Expand Down Expand Up @@ -159,7 +153,6 @@ def step(
timestep: current discrete timestep in the diffusion chain.
sample: current instance of sample being created by diffusion process.
eta: weight of noise for added noise in diffusion step.
predict_epsilon: flag to use when model predicts the samples directly instead of the noise, epsilon.
generator: random number generator.
Returns:
Expand Down

0 comments on commit 3ab5c62

Please sign in to comment.