Spaces:
Sleeping
Sleeping
Update pk_workflow.py
Browse files174 unmodified lines1751761771781791806 unmodified lines18718818919019119219319419519619717 unmodified lines215216217218219220221222223224225174 unmodified lines"normal": normal_sigmas,}class use_schedule:"""Set each scheduler's shift for one request, and — for anything but `native` — force its sigma grid onto6 unmodified linesare shared, request-spanning objects that must not carry one request's shift into the next."""def __init__(self, pipe, steps: int, schedule_name: str, video_shift: float, audio_shift: float, threshold_noise: float = 0.025):self.pipe = pipeself.attr_names = ["scheduler", "audio_scheduler"]self.shifts = [float(video_shift), float(audio_shift)]self.schedule_name = schedule_nameself.steps = int(steps)self.threshold_noise = float(threshold_noise)self._originals: dict = {}17 unmodified linesreturn _f(_s, None, device, _grid)scheduler.set_timesteps = forcedreturn selfdef __exit__(self, *_):for attr_name, original in self._originals.items():getattr(self.pipe, attr_name).__dict__.pop("set_timesteps", None)setattr(self.pipe, attr_name, original)return False174 unmodified lines1751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202216 unmodified lines22822923023123223323423523623723823924017 unmodified lines258259260261262263264265266267268269270271272273274275276277278279280281282174 unmodified lines"normal": normal_sigmas,}def _euler_ancestral_step(scheduler, generator, model_output, timestep, sample, eta: float = 1.0, s_noise: float = 1.0):"""Ports k-diffusion's `sample_euler_ancestral_RF` — the flow-matching branch `sample_euler_ancestral`dispatches to for `CONST`-style model sampling, which is what MiniMax-H3's `[0, 1]` sigma space is — onto one`MiniMaxH3Scheduler.step()` call. Single model evaluation, same shape as `step()` itself, with freshancestral noise injected each step instead of a plain Euler blend. Mirrors `step()`'s own care aroundrecomputing `sigma_from_timestep` from `timestep` rather than reading `self.sigmas` at the current index, forthe same numerical-consistency reason documented there."""if scheduler._step_index is None:scheduler._step_index = scheduler.index_for_timestep(timestep) if scheduler._begin_index is None else scheduler._begin_indexif not isinstance(timestep, torch.Tensor):timestep = torch.tensor(timestep, dtype=sample.dtype)sigma_from_timestep = 1 - timestep.to(device=sample.device, dtype=sample.dtype)while sigma_from_timestep.ndim < sample.ndim:sigma_from_timestep = sigma_from_timestep.unsqueeze(-1)denoised = sample + sigma_from_timestep * model_outputcompute_dtype = torch.float32 if sample.dtype in (torch.float16, torch.bfloat16) else sample.dtypesigma = scheduler.sigmas[scheduler._step_index].to(device=sample.device, dtype=compute_dtype)sigma_next = scheduler.sigmas[scheduler._step_index + 1].to(device=sample.device, dtype=compute_dtype)x = sample.to(dtype=compute_dtype)denoised = denoised.to(dtype=compute_dtype)if sigma_next == 0:prev_sample = denoisedelse:downstep_ratio = 1 + (sigma_next / sigma - 1) * etasigma_down = sigma_next * downstep_ratioalpha_next = 1 - sigma_nextalpha_down = 1 - sigma_downrenoise_coeff = (sigma_next**2 - sigma_down**2 * alpha_next**2 / alpha_down**2).clamp_min(0).sqrt()ratio = sigma_down / sigmaprev_sample = ratio * x + (1 - ratio) * denoisedif eta > 0:noise = torch.randn(x.shape, dtype=x.dtype, device=x.device, generator=generator)prev_sample = (alpha_next / alpha_down) * prev_sample + noise * s_noise * renoise_coeffprev_sample = prev_sample.to(dtype=sample.dtype)scheduler._step_index += 1return prev_sampleclass use_schedule:"""Set each scheduler's shift for one request, and — for anything but `native` — force its sigma grid onto6 unmodified linesare shared, request-spanning objects that must not carry one request's shift into the next."""def __init__(self, pipe, steps: int, schedule_name: str, video_shift: float, audio_shift: float, sampler_name: str = "euler", seed: int = 0, threshold_noise: float = 0.025):self.pipe = pipeself.attr_names = ["scheduler", "audio_scheduler"]self.shifts = [float(video_shift), float(audio_shift)]self.schedule_name = schedule_nameself.sampler_name = sampler_nameself.seed = int(seed)self.steps = int(steps)self.threshold_noise = float(threshold_noise)self._originals: dict = {}17 unmodified linesreturn _f(_s, None, device, _grid)scheduler.set_timesteps = forcedif self.sampler_name == "euler_ancestral":# Separate `torch.Generator` per scheduler (offset seeds) so video and audio ancestral noise don't# correlate — each generator advances across every step call to *that* scheduler over the request.for offset, attr_name in enumerate(self.attr_names):scheduler = getattr(self.pipe, attr_name)generator = torch.Generator(device="cpu").manual_seed(self.seed + offset)def stepped(model_output, timestep, sample, return_dict=True, _s=scheduler, _g=generator, **_kwargs):return (_euler_ancestral_step(_s, _g, model_output, timestep, sample),)scheduler.step = steppedreturn selfdef __exit__(self, *_):for attr_name, original in self._originals.items():current = getattr(self.pipe, attr_name)current.__dict__.pop("set_timesteps", None)current.__dict__.pop("step", None)setattr(self.pipe, attr_name, original)return False
pk_workflow.py+59-2