返回 JoyAI-Echo
noisers.py
根目录 / ltx-core / src / ltx_core / components / noisers.py
1 from dataclasses import replace
2 from typing import Protocol
3
4 import torch
5
6 from ltx_core.types import LatentState
7
8
9 class Noiser(Protocol):
10 """Protocol for adding noise to a latent state during diffusion."""
11
12 def __call__(self, latent_state: LatentState, noise_scale: float) -> LatentState: ...
13
14
15 class GaussianNoiser(Noiser):
16 """Adds Gaussian noise to a latent state, scaled by the denoise mask."""
17
18 def __init__(self, generator: torch.Generator):
19 super().__init__()
20
21 self.generator = generator
22
23 def __call__(self, latent_state: LatentState, noise_scale: float = 1.0) -> LatentState:
24 noise = torch.randn(
25 *latent_state.latent.shape,
26 device=latent_state.latent.device,
27 dtype=latent_state.latent.dtype,
28 generator=self.generator,
29 )
30 scaled_mask = latent_state.denoise_mask * noise_scale
31 latent = noise * scaled_mask + latent_state.latent * (1 - scaled_mask)
32 return replace(
33 latent_state,
34 latent=latent.to(latent_state.latent.dtype),
35 )
36
36 lines PYTHON