MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / ddpm_bit_scheduler_step

Function ddpm_bit_scheduler_step

diffusers/examples/community/bit_diffusion.py:135–210  ·  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.Tensor`): direct output from learned diffusion model. timestep (

(
    self,
    model_output: torch.Tensor,
    timestep: int,
    sample: torch.Tensor,
    prediction_type="epsilon",
    generator=None,
    return_dict: bool = True,
)

Source from the content-addressed store, hash-verified

133
134
135def ddpm_bit_scheduler_step(
136 self,
137 model_output: torch.Tensor,
138 timestep: int,
139 sample: torch.Tensor,
140 prediction_type="epsilon",
141 generator=None,
142 return_dict: bool = True,
143) -> Union[DDPMSchedulerOutput, Tuple]:
144 """
145 Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
146 process from the learned model outputs (most often the predicted noise).
147 Args:
148 model_output (`torch.Tensor`): direct output from learned diffusion model.
149 timestep (`int`): current discrete timestep in the diffusion chain.
150 sample (`torch.Tensor`):
151 current instance of sample being created by diffusion process.
152 prediction_type (`str`, default `epsilon`):
153 indicates whether the model predicts the noise (epsilon), or the samples (`sample`).
154 generator: random number generator.
155 return_dict (`bool`): option for returning tuple rather than DDPMSchedulerOutput class
156 Returns:
157 [`~schedulers.scheduling_utils.DDPMSchedulerOutput`] or `tuple`:
158 [`~schedulers.scheduling_utils.DDPMSchedulerOutput`] if `return_dict` is True, otherwise a `tuple`. When
159 returning a tuple, the first element is the sample tensor.
160 """
161 t = timestep
162
163 if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]:
164 model_output, predicted_variance = torch.split(model_output, sample.shape[1], dim=1)
165 else:
166 predicted_variance = None
167
168 # 1. compute alphas, betas
169 alpha_prod_t = self.alphas_cumprod[t]
170 alpha_prod_t_prev = self.alphas_cumprod[t - 1] if t > 0 else self.one
171 beta_prod_t = 1 - alpha_prod_t
172 beta_prod_t_prev = 1 - alpha_prod_t_prev
173
174 # 2. compute predicted original sample from predicted noise also called
175 # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf
176 if prediction_type == "epsilon":
177 pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
178 elif prediction_type == "sample":
179 pred_original_sample = model_output
180 else:
181 raise ValueError(f"Unsupported prediction_type {prediction_type}.")
182
183 # 3. Clip "predicted x_0"
184 scale = self.bit_scale
185 if self.config.clip_sample:
186 pred_original_sample = torch.clamp(pred_original_sample, -scale, scale)
187
188 # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t
189 # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
190 pred_original_sample_coeff = (alpha_prod_t_prev ** (0.5) * self.betas[t]) / beta_prod_t
191 current_sample_coeff = self.alphas[t] ** (0.5) * beta_prod_t_prev / beta_prod_t
192

Callers

nothing calls this directly

Calls 3

DDPMSchedulerOutputClass · 0.90
toMethod · 0.45
_get_varianceMethod · 0.45

Tested by

no test coverage detected