LTX 0.9.7-distilled; documentation improvements (#11571) · sffej/huggingface-diffusers@05c8b42 · GitHub
Skip to content

Commit 05c8b42

Browse files
LTX 0.9.7-distilled; documentation improvements (huggingface#11571)
* add guidance rescale * update docs * support adaptive instance norm filter * fix custom timesteps support * add custom timestep example to docs * add a note about best generation settings being available only in the original repository * use original org hub ids instead of personal * make fix-copies --------- Co-authored-by: Linoy Tsaban <57615435+linoytsaban@users.noreply.github.com>
1 parent c8bb1ff commit 05c8b42

5 files changed

Lines changed: 280 additions & 6 deletions

File tree

‎docs/source/en/api/pipelines/ltx_video.md‎

Lines changed: 109 additions & 3 deletions

‎src/diffusers/pipelines/ltx/pipeline_ltx.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ def retrieve_timesteps(
140140
return timesteps, num_inference_steps
141141

142142

143+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.rescale_noise_cfg
144+
def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
145+
r"""
146+
Rescales `noise_cfg` tensor based on `guidance_rescale` to improve image quality and fix overexposure. Based on
147+
Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are
148+
Flawed](https://huggingface.co/papers/2305.08891).
149+
150+
Args:
151+
noise_cfg (`torch.Tensor`):
152+
The predicted noise tensor for the guided diffusion process.
153+
noise_pred_text (`torch.Tensor`):
154+
The predicted noise tensor for the text-guided diffusion process.
155+
guidance_rescale (`float`, *optional*, defaults to 0.0):
156+
A rescale factor applied to the noise predictions.
157+
158+
Returns:
159+
noise_cfg (`torch.Tensor`): The rescaled noise prediction tensor.
160+
"""
161+
std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
162+
std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)
163+
# rescale the results from guidance (fixes overexposure)
164+
noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
165+
# mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images
166+
noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg
167+
return noise_cfg
168+
169+
143170
class LTXPipeline(DiffusionPipeline, FromSingleFileMixin, LTXVideoLoraLoaderMixin):
144171
r"""
145172
Pipeline for text-to-video generation.
@@ -481,6 +508,10 @@ def prepare_latents(
481508
def guidance_scale(self):
482509
return self._guidance_scale
483510

511+
@property
512+
def guidance_rescale(self):
513+
return self._guidance_rescale
514+
484515
@property
485516
def do_classifier_free_guidance(self):
486517
return self._guidance_scale > 1.0
@@ -514,6 +545,7 @@ def __call__(
514545
num_inference_steps: int = 50,
515546
timesteps: List[int] = None,
516547
guidance_scale: float = 3,
548+
guidance_rescale: float = 0.0,
517549
num_videos_per_prompt: Optional[int] = 1,
518550
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
519551
latents: Optional[torch.Tensor] = None,
@@ -556,6 +588,11 @@ def __call__(
556588
of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
557589
`guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
558590
the text `prompt`, usually at the expense of lower image quality.
591+
guidance_rescale (`float`, *optional*, defaults to 0.0):
592+
Guidance rescale factor proposed by [Common Diffusion Noise Schedules and Sample Steps are
593+
Flawed](https://arxiv.org/pdf/2305.08891.pdf) `guidance_scale` is defined as `φ` in equation 16. of
594+
[Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf).
595+
Guidance rescale factor should fix overexposure when using zero terminal SNR.
559596
num_videos_per_prompt (`int`, *optional*, defaults to 1):
560597
The number of videos to generate per prompt.
561598
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
@@ -624,6 +661,7 @@ def __call__(
624661
)
625662

626663
self._guidance_scale = guidance_scale
664+
self._guidance_rescale = guidance_rescale
627665
self._attention_kwargs = attention_kwargs
628666
self._interrupt = False
629667
self._current_timestep = None
@@ -737,6 +775,12 @@ def __call__(
737775
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
738776
noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
739777

778+
if self.guidance_rescale > 0:
779+
# Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
780+
noise_pred = rescale_noise_cfg(
781+
noise_pred, noise_pred_text, guidance_rescale=self.guidance_rescale
782+
)
783+
740784
# compute the previous noisy sample x_t -> x_t-1
741785
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
742786

‎src/diffusers/pipelines/ltx/pipeline_ltx_condition.py‎

Lines changed: 48 additions & 2 deletions

0 commit comments

Comments
 (0)