Source: sources/0363/REG-b9c867cff57a/t2i_diffusers/call_method.py
SHA256: 7a090eb98d0ad35fb62ce4e87c3dfb8c5eab160210f1f48ca433be21d6e6ccdb
Read at: 2026-10-25T10:06:42.129420+00:00
308:                 # perform guidance
309:                 if kwargs['cfg_type'] == 'original':
310:                     noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
311:                     # print(f"peak memory = {torch.cuda.max_memory_allocated() / 1024 / 1024 / 1024} GB")
312:                 elif kwargs['cfg_type'] == 'linear':
313:                     k = (kwargs['cfg_param']['end_scale'] - self.guidance_scale) / (timesteps[-1] - timesteps[0])
314:                     coeff = k * (t - timesteps[0]) + self.guidance_scale
315:                     # print(f"at {t}, coeff= {coeff}, t[-1]={timesteps[-1]}, t[0]={timesteps[0]}")
316:                     noise_pred = noise_pred_uncond + coeff * (noise_pred_text - noise_pred_uncond)
317:                 elif kwargs['cfg_type'] == 'cosine':
318:                     progress = (t - timesteps[0]) / (timesteps[-1] - timesteps[0])
319:                     coeff = (1 - torch.cos(torch.pi * torch.pow(progress, kwargs['cfg_param']['s']))) * 0.5 * self.guidance_scale
320:                     # print(f"at {t}, progress={progress}, coeff={coeff}")
321:                     noise_pred = noise_pred_uncond + coeff * (noise_pred_text - noise_pred_uncond)
322:                 elif kwargs['cfg_type'] == 'reg_original':
323:                     noise_pred = noise_pred_text + (coeff1 - coeff2 * grad) * \
324:                                      self.guidance_scale * (noise_pred_text - noise_pred_uncond)
325:                     # print(f"peak memory = {torch.cuda.max_memory_allocated() / 1024 / 1024 / 1024} GB")
326:                 elif kwargs['cfg_type'] == 'reg_linear':
327:                     k = (kwargs['cfg_param']['end_scale'] - self.guidance_scale) / (timesteps[-1] - timesteps[0])
328:                     coeff = k * (t - timesteps[0]) + self.guidance_scale
329:                     # print(f"at {t}, coeff= {coeff}, t[-1]={timesteps[-1]}, t[0]={timesteps[0]}")
330:                     noise_pred = noise_pred_text + (coeff1 - coeff2 * grad) * coeff * (noise_pred_text - noise_pred_uncond)
331:                 elif kwargs['cfg_type'] == 'reg_cosine':
332:                     progress = (t - timesteps[0]) / (timesteps[-1] - timesteps[0])
333:                     coeff = (1 - torch.cos(torch.pi * torch.pow(progress, kwargs['cfg_param']['s']))) * 0.5 * self.guidance_scale
334:                     noise_pred = noise_pred_text + (coeff1 - coeff2 * grad) * coeff * (noise_pred_text - noise_pred_uncond)
335:                 else:
336:                     raise NotImplementedError
337: 
338:                 if self.do_classifier_free_guidance and self.guidance_rescale > 0.0:
339:                     # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
340:                     noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=self.guidance_rescale)
341: 
342:                 # compute the previous noisy sample x_t -> x_t-1
343:                 latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
344: 
345:                 if callback_on_step_end is not None:
346:                     callback_kwargs = {}
347:                     for k in callback_on_step_end_tensor_inputs:
348:                         callback_kwargs[k] = locals()[k]
349:                     callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
350: 
351:                     latents = callback_outputs.pop("latents", latents)
352:                     prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
353:                     negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
354: 
355:                 # call the callback, if provided
356:                 if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
357:                     progress_bar.update()
358:                     if callback is not None and i % callback_steps == 0:
359:                         step_idx = i // getattr(self.scheduler, "order", 1)
360:                         callback(step_idx, t, latents)
361: 
