MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / ddim_bit_scheduler_step

Function ddim_bit_scheduler_step

examples/community/bit_diffusion.py:45–132  ·  view source on GitHub ↗

Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion process from the learned model outputs (most often the predicted noise). Args: model_output (`torch.FloatTensor`): direct output from learned diffusion model. times

(
    self,
    model_output: torch.FloatTensor,
    timestep: int,
    sample: torch.FloatTensor,
    eta: float = 0.0,
    use_clipped_model_output: bool = True,
    generator=None,
    return_dict: bool = True,
)

Source from the content-addressed store, hash-verified

43
44# modified scheduler step functions for clamping the predicted x_0 between -bit_scale and +bit_scale
45def ddim_bit_scheduler_step(
46 self,
47 model_output: torch.FloatTensor,
48 timestep: int,
49 sample: torch.FloatTensor,
50 eta: float = 0.0,
51 use_clipped_model_output: bool = True,
52 generator=None,
53 return_dict: bool = True,
54) -> Union[DDIMSchedulerOutput, Tuple]:
55 """
56 Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
57 process from the learned model outputs (most often the predicted noise).
58 Args:
59 model_output (`torch.FloatTensor`): direct output from learned diffusion model.
60 timestep (`int`): current discrete timestep in the diffusion chain.
61 sample (`torch.FloatTensor`):
62 current instance of sample being created by diffusion process.
63 eta (`float`): weight of noise for added noise in diffusion step.
64 use_clipped_model_output (`bool`): TODO
65 generator: random number generator.
66 return_dict (`bool`): option for returning tuple rather than DDIMSchedulerOutput class
67 Returns:
68 [`~schedulers.scheduling_utils.DDIMSchedulerOutput`] or `tuple`:
69 [`~schedulers.scheduling_utils.DDIMSchedulerOutput`] if `return_dict` is True, otherwise a `tuple`. When
70 returning a tuple, the first element is the sample tensor.
71 """
72 if self.num_inference_steps is None:
73 raise ValueError(
74 "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"
75 )
76
77 # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf
78 # Ideally, read DDIM paper in-detail understanding
79
80 # Notation (<variable name> -> <name in paper>
81 # - pred_noise_t -> e_theta(x_t, t)
82 # - pred_original_sample -> f_theta(x_t, t) or x_0
83 # - std_dev_t -> sigma_t
84 # - eta -> η
85 # - pred_sample_direction -> "direction pointing to x_t"
86 # - pred_prev_sample -> "x_t-1"
87
88 # 1. get previous step value (=t-1)
89 prev_timestep = timestep - self.config.num_train_timesteps // self.num_inference_steps
90
91 # 2. compute alphas, betas
92 alpha_prod_t = self.alphas_cumprod[timestep]
93 alpha_prod_t_prev = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod
94
95 beta_prod_t = 1 - alpha_prod_t
96
97 # 3. compute predicted original sample from predicted noise also called
98 # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
99 pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
100
101 # 4. Clip "predicted x_0"
102 scale = self.bit_scale

Callers

nothing calls this directly

Calls 3

DDIMSchedulerOutputClass · 0.90
_get_varianceMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected