"""`lcm` and `res_multistep` for MiniMax-H3's `MiniMaxH3Scheduler.step()` slot — same pattern as `_euler_ancestral_step` / `_er_sde_step` in `pk_workflow.py`: one model evaluation per step, the sampler's maths done on the scheduler's own `sigmas`, installed per request by `use_schedule` and removed again on exit. Both are ports of ComfyUI's `comfy/k_diffusion/sampling.py` for its `CONST` (rectified-flow) model sampling: `lcm` — `sample_lcm`. Each step jumps all the way to the model's denoised estimate, then re-noises it to the NEXT sigma with fresh noise: `x_next = sigma_next * noise + (1 - sigma_next) * denoised` (`CONST.noise_scaling`). The last step (`sigma_next == 0`) returns `denoised`. 10Eros v1.0's card recommends LCM/simple at 8-12 steps (best turbo audio). `res_multistep` — `res_multistep(..., eta=0, cfg_pp=False)`, i.e. `sample_res_multistep`: the second-order multistep exponential integrator of arXiv:2308.02157 (RES). Deterministic. The first step, and the final step to sigma 0, are plain Euler; every step in between combines this step's and the previous step's denoised estimate. History is carried on the scheduler instance and reset each request by `use_schedule`. Both read `denoised` the same way the existing ports do: `denoised = sample + (1 - timestep) * model_output`. Staged Denoising: `lcm` carries no state besides its noise stream (seeded per stage by the caller). `res_multistep` carries the previous denoised estimate, which is NOT handed from one stage to the next, so the first step of every stage after the first is plain Euler — the same restart `_er_sde_step` has at a stage boundary, and a small deviation from one unstaged run. The coefficient maths is in plain Python floats (float64): `phi2` subtracts nearly equal numbers at the small step sizes a shifted grid has near sigma 1, which is where float32 loses digits. """ from __future__ import annotations import math def res_multistep_coefficients(sigma: float, sigma_next: float, sigma_prev: float | None, have_history: bool): """Mixing weights for one `res_multistep` step. Returns `("euler", None)` or `("multistep", (a, c_cur, c_old))`, with x_next = a * x + c_cur * denoised + c_old * old_denoised. ComfyUI's `eta=0` case: `sigma_down == sigma_next`, so the previous step's `sigma_down` is this step's `sigma`. """ if sigma_next == 0.0 or not have_history or sigma_prev is None: return "euler", None t = -math.log(sigma) t_next = -math.log(sigma_next) t_prev = -math.log(sigma_prev) t_old = t # previous step's sigma_down == sigma at eta = 0 h = t_next - t c2 = (t_prev - t_old) / h phi1 = math.expm1(-h) / (-h) phi2 = (phi1 - 1.0) / (-h) b1 = phi1 - phi2 / c2 b2 = phi2 / c2 return "multistep", (math.exp(-h), h * b1, h * b2) def _begin(scheduler, timestep, sample, model_output): """Shared preamble of the single-evaluation ports: step index, float32 `x` and `denoised`, float32 sigmas.""" import torch if scheduler._step_index is None: scheduler._step_index = ( scheduler.index_for_timestep(timestep) if scheduler._begin_index is None else scheduler._begin_index ) i = scheduler._step_index if 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_output compute_dtype = torch.float32 if sample.dtype in (torch.float16, torch.bfloat16) else sample.dtype return i, sample.to(dtype=compute_dtype), denoised.to(dtype=compute_dtype), compute_dtype def lcm_step(scheduler, generator, model_output, timestep, sample, s_noise: float = 1.0): import torch i, x, denoised, compute_dtype = _begin(scheduler, timestep, sample, model_output) sigma_next = scheduler.sigmas[i + 1].to(device=sample.device, dtype=compute_dtype) if sigma_next == 0: prev_sample = denoised else: noise = torch.randn(x.shape, dtype=x.dtype, device="cpu", generator=generator).to(x.device) prev_sample = sigma_next * (noise * s_noise) + (1 - sigma_next) * denoised scheduler._step_index += 1 return prev_sample.to(dtype=sample.dtype) def res_multistep_step(scheduler, model_output, timestep, sample): i, x, denoised, _ = _begin(scheduler, timestep, sample, model_output) sigma = float(scheduler.sigmas[i]) sigma_next = float(scheduler.sigmas[i + 1]) sigma_prev = float(scheduler.sigmas[i - 1]) if i > 0 else None old_denoised = getattr(scheduler, "_res_old_denoised", None) if i > 0 else None kind, coefficients = res_multistep_coefficients(sigma, sigma_next, sigma_prev, old_denoised is not None) if kind == "euler": # `d = (x - denoised) / sigma`, `x + d * (sigma_next - sigma)`; at `sigma_next == 0` this is exactly `denoised`. prev_sample = x + ((x - denoised) / sigma) * (sigma_next - sigma) else: a, c_cur, c_old = coefficients prev_sample = a * x + c_cur * denoised + c_old * old_denoised.to(device=x.device, dtype=x.dtype) scheduler._res_old_denoised = denoised scheduler._step_index += 1 return prev_sample.to(dtype=sample.dtype)