Official repository for LTX-Video
Python diffusion-modelsditimage-to-videoimage-to-video-generationtext-to-videotext-to-video-generation
Scheduler: Add LinearQuadratic sampler option
Yoav HaCohen9cdfcd1ee21a
diff --git a/inference.py b/inference.pyindex cac8110..dd34e08 100644--- a/inference.py+++ b/inference.py@@ -272,6 +272,15 @@ def main():help="Offloading unnecessary computations to CPU.",)+ # Add to the argument parser section+ parser.add_argument(+ "--sampler",+ type=str,+ choices=["uniform", "linear-quadratic"],+ default=None,+ help="Sampler to use for noise scheduling. Can be either 'uniform' or 'linear-quadratic'. If not specified, uses the sampler from the checkpoint.",+ )+logger = logging.get_logger(__name__)args = parser.parse_args()@@ -327,7 +336,16 @@ def main():ckpt_path = Path(args.ckpt_path)vae = CausalVideoAutoencoder.from_pretrained(ckpt_path)transformer = Transformer3DModel.from_pretrained(ckpt_path)- scheduler = RectifiedFlowScheduler.from_pretrained(ckpt_path)++ # Use constructor if sampler is specified, otherwise use from_pretrained+ if args.sampler:+ scheduler = RectifiedFlowScheduler(+ sampler=(+ "Uniform" if args.sampler.lower() == "uniform" else "LinearQuadratic"+ )+ )+ else:+ scheduler = RectifiedFlowScheduler.from_pretrained(ckpt_path)text_encoder = T5EncoderModel.from_pretrained("PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="text_encoder"diff --git a/ltx_video/schedulers/rf.py b/ltx_video/schedulers/rf.pyindex 892266d..c27d065 100644--- a/ltx_video/schedulers/rf.py+++ b/ltx_video/schedulers/rf.py@@ -22,6 +22,28 @@ from ltx_video.utils.diffusers_config_mapping import ()+def linear_quadratic_schedule(num_steps, threshold_noise=0.025, linear_steps=None):+ if linear_steps is None:+ linear_steps = num_steps // 2+ linear_sigma_schedule = [+ i * threshold_noise / linear_steps for i in range(linear_steps)+ ]+ threshold_noise_step_diff = linear_steps - threshold_noise * num_steps+ quadratic_steps = num_steps - linear_steps+ quadratic_coef = threshold_noise_step_diff / (linear_steps * quadratic_steps**2)+ linear_coef = threshold_noise / linear_steps - 2 * threshold_noise_step_diff / (+ quadratic_steps**2+ )+ const = quadratic_coef * (linear_steps**2)+ quadratic_sigma_schedule = [+ quadratic_coef * (i**2) + linear_coef * i + const+ for i in range(linear_steps, num_steps)+ ]+ sigma_schedule = linear_sigma_schedule + quadratic_sigma_schedule + [1.0]+ sigma_schedule = [1.0 - x for x in sigma_schedule]+ return torch.tensor(sigma_schedule[:-1])++def simple_diffusion_resolution_dependent_timestep_shift(samples: Tensor,timesteps: Tensor,@@ -157,19 +179,25 @@ class RectifiedFlowScheduler(SchedulerMixin, ConfigMixin, TimestepShifter):shifting: Optional[str] = None,base_resolution: int = 32**2,target_shift_terminal: Optional[float] = None,+ sampler: Optional[str] = "Uniform",):super().__init__()self.init_noise_sigma = 1.0self.num_inference_steps = None- self.timesteps = self.sigmas = torch.linspace(- 1, 1 / num_train_timesteps, num_train_timesteps- )- self.delta_timesteps = self.timesteps - torch.cat(- [self.timesteps[1:], torch.zeros_like(self.timesteps[-1:])]- )+ self.sampler = samplerself.shifting = shiftingself.base_resolution = base_resolutionself.target_shift_terminal = target_shift_terminal+ self.timesteps = self.sigmas = self.get_initial_timesteps(num_train_timesteps)+ self.delta_timesteps = self.timesteps - torch.cat(+ [self.timesteps[1:], torch.zeros_like(self.timesteps[-1:])]+ )++ def get_initial_timesteps(self, num_timesteps: int) -> Tensor:+ if self.sampler == "Uniform":+ return torch.linspace(1, 1 / num_timesteps, num_timesteps)+ elif self.sampler == "LinearQuadratic":+ return linear_quadratic_schedule(num_timesteps)def shift_timesteps(self, samples: Tensor, timesteps: Tensor) -> Tensor:if self.shifting == "SD3":@@ -197,10 +225,8 @@ class RectifiedFlowScheduler(SchedulerMixin, ConfigMixin, TimestepShifter):device (`Union[str, torch.device]`, *optional*): The device to which the timesteps tensor will be moved."""num_inference_steps = min(self.config.num_train_timesteps, num_inference_steps)- timesteps = torch.linspace(1, 1 / num_inference_steps, num_inference_steps).to(- device- )- self.timesteps = self.shift_timesteps(samples, timesteps)+ self.timesteps = self.get_initial_timesteps(num_inference_steps).to(device)+ self.timesteps = self.shift_timesteps(samples, self.timesteps)self.delta_timesteps = self.timesteps - torch.cat([self.timesteps[1:], torch.zeros_like(self.timesteps[-1:])])