Update pk_workflow.py
Browse files80 unmodified lines8182838485868788899091929394959697989910010110210310410510610710810911011111211311480 unmodified linesreturn to_shift * base / (1.0 + (to_shift - 1.0) * base)class use_linear_quadratic:"""Force MiniMax-H3's two schedulers onto the `linear_quadratic` grid for one pipeline call.A context manager rather than a pipeline-block subclass on purpose: `MiniMaxH3Scheduler.set_timesteps` alreadytakes a fully-formed `sigmas=` schedule as public API, so nothing here reaches into the modular blocks, and theoverride lives and dies inside one request."""def __init__(self, pipe, steps: int, threshold_noise: float = 0.025, enabled: bool = True):self.schedulers = [pipe.scheduler, pipe.audio_scheduler] if enabled else []self.steps = int(steps)self.threshold_noise = float(threshold_noise)def __enter__(self):video_sigmas = linear_quadratic_sigmas(self.steps, self.threshold_noise)for scheduler in self.schedulers:sigmas = time_shift_sigma(video_sigmas, VIDEO_SHIFT, float(scheduler.shift))unbound = type(scheduler).set_timestepsdef forced(num_inference_steps=None, device=None, sigmas=None, _s=scheduler, _grid=sigmas, _f=unbound):return _f(_s, None, device, _grid)scheduler.set_timesteps = forcedreturn selfdef __exit__(self, *_):for scheduler in self.schedulers:scheduler.__dict__.pop("set_timesteps", None)return False80 unmodified lines8182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022180 unmodified linesreturn to_shift * base / (1.0 + (to_shift - 1.0) * base)# ----------------------------------------------------------------------------------------------------------------# BasicScheduler(sgm_uniform / simple / beta / ddim_uniform / normal)# ----------------------------------------------------------------------------------------------------------------# Five more of ComfyUI's `BasicScheduler` names, ported from `comfy/samplers.py`. Each is computed at the# *reference* shift (1.0 — where `time_snr_shift` is the identity, so `sigma(t) == t`) and reprojected onto each# scheduler's real shift by `time_shift_sigma`, exactly like `linear_quadratic_sigmas` already is and for the same# reason: it keeps the video and audio streams pinned to the same underlying denoising progress at each step,# which computing each stream's schedule independently at its own shift would not.## `FLOW_TIMESTEPS` mirrors ComfyUI's `ModelSamplingDiscreteFlow`/`ModelSamplingAV` default of 1000 discrete steps# (`comfy/model_sampling.py`). Unverified specifically for MiniMax-H3's own `sampling_settings` — if a ported# schedule's shape looks visibly different from ComfyUI's own render at the same steps/seed, this is the first# thing to check.FLOW_TIMESTEPS = 1000def _reference_sigma(index_1based: int) -> float:"""`ModelSamplingAV.sigma(timestep)` at shift == 1.0: the shift formula is the identity, so this is just theplain fraction `index / FLOW_TIMESTEPS`. `index_1based` matches ComfyUI's 1-based table construction(`torch.arange(1, timesteps + 1) / timesteps`)."""return index_1based / FLOW_TIMESTEPSdef sgm_uniform_sigmas(steps: int) -> torch.Tensor:"""ComfyUI's `sgm_uniform`. Uniform in *timestep* space between the max and min sigma, dropping the pointthat would land exactly on the minimum, then appending an exact 0.0. `steps + 1` sigmas."""steps = int(steps)timesteps = torch.linspace(float(FLOW_TIMESTEPS), 1.0, steps + 1)[:-1]sigmas = (timesteps / FLOW_TIMESTEPS).tolist() + [0.0]return torch.tensor(sigmas, dtype=torch.float32)def normal_sigmas(steps: int) -> torch.Tensor:"""ComfyUI's `normal`. Same idea as `sgm_uniform` but the linspace includes both endpoints (the minimumsigma is reached exactly, not dropped), with 0.0 still appended."""steps = int(steps)timesteps = torch.linspace(float(FLOW_TIMESTEPS), 1.0, steps)sigmas = (timesteps / FLOW_TIMESTEPS).tolist() + [0.0]return torch.tensor(sigmas, dtype=torch.float32)def simple_sigmas(steps: int) -> torch.Tensor:"""ComfyUI's `simple`: evenly-spaced *indices* into the 1000-entry sigma table, walked from the high-noiseend, then 0.0 appended."""steps = int(steps)stride = FLOW_TIMESTEPS / stepssigmas = [_reference_sigma(FLOW_TIMESTEPS - int(x * stride)) for x in range(steps)]sigmas.append(0.0)return torch.tensor(sigmas, dtype=torch.float32)def ddim_uniform_sigmas(steps: int) -> torch.Tensor:"""ComfyUI's `ddim_uniform`: a fixed-stride walk through the sigma table starting one index in, reversed sothe highest sigma comes first, ending at 0.0."""steps = int(steps)stride = max(FLOW_TIMESTEPS // steps, 1)sigmas = [0.0]index = 1while index < FLOW_TIMESTEPS:sigmas.append(_reference_sigma(index))index += stridesigmas.reverse()return torch.tensor(sigmas, dtype=torch.float32)def beta_sigmas(steps: int, alpha: float = 0.6, beta: float = 0.6) -> torch.Tensor:"""ComfyUI's `beta` (arxiv.org/abs/2407.12173): table indices drawn from a Beta(alpha, beta) inverse CDFinstead of an even stride, biasing samples toward one end of the trajectory. Needs `scipy`."""import numpyimport scipy.statssteps = int(steps)total = FLOW_TIMESTEPS - 1positions = 1.0 - numpy.linspace(0.0, 1.0, steps, endpoint=False)indices = numpy.rint(scipy.stats.beta.ppf(positions, alpha, beta) * total)sigmas = []last = -1for value in indices:if value != last:sigmas.append(_reference_sigma(int(value) + 1))last = valuesigmas.append(0.0)return torch.tensor(sigmas, dtype=torch.float32)SCHEDULE_SIGMA_FUNCS = {"linear_quadratic": linear_quadratic_sigmas,"sgm_uniform": sgm_uniform_sigmas,"simple": simple_sigmas,"beta": beta_sigmas,"ddim_uniform": ddim_uniform_sigmas,"normal": normal_sigmas,}class use_schedule:"""Set each scheduler's shift for one request, and — for anything but `native` — force its sigma grid ontoone of `SCHEDULE_SIGMA_FUNCS`'s named schedules.Shift is applied unconditionally, including under `native`, so the shift sliders affect the pipeline's owndefault schedule too and not just the custom ones — and it is always restored on exit, since`pipe.scheduler`/`pipe.audio_scheduler` are shared, request-spanning objects that must not carry onerequest'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.schedulers = [pipe.scheduler, pipe.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 = []def __enter__(self):self._originals = [(scheduler, scheduler.shift) for scheduler in self.schedulers]for scheduler, shift in zip(self.schedulers, self.shifts):scheduler.shift = shiftif self.schedule_name != "native":sigma_func = SCHEDULE_SIGMA_FUNCS[self.schedule_name]base = sigma_func(self.steps, self.threshold_noise) if sigma_func is linear_quadratic_sigmas else sigma_func(self.steps)for scheduler in self.schedulers:sigmas = time_shift_sigma(base, 1.0, float(scheduler.shift))unbound = type(scheduler).set_timestepsdef forced(num_inference_steps=None, device=None, sigmas=None, _s=scheduler, _grid=sigmas, _f=unbound):return _f(_s, None, device, _grid)scheduler.set_timesteps = forcedreturn selfdef __exit__(self, *_):for scheduler in self.schedulers:scheduler.__dict__.pop("set_timesteps", None)for scheduler, original_shift in self._originals:scheduler.shift = original_shiftreturn False
pk_workflow.py+121-14