Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(ernie_image): honour a partial denoise window on the Heun scheduler (#10006)

Muneeb Anjum7026c459bb26
diff --git a/invokeai/backend/ernie_image/denoise.py b/invokeai/backend/ernie_image/denoise.py
index 2694c10131..52a7fae798 100644
--- a/invokeai/backend/ernie_image/denoise.py
+++ b/invokeai/backend/ernie_image/denoise.py
@@ -14,10 +14,16 @@ import torch
from tqdm import tqdm
from invokeai.backend.ernie_image.sampling_utils import unpatchify_latents
+from invokeai.backend.flux.schedulers import set_heun_timesteps_from_sigmas
from invokeai.backend.rectified_flow.rectified_flow_inpaint_extension import RectifiedFlowInpaintExtension
from invokeai.backend.stable_diffusion.diffusers_pipeline import PipelineIntermediateState
+def _shift_flow_sigmas(sigmas: list[float], shift: float) -> list[float]:
+ """Map raw flow sigmas through the scheduler's own shift, as `set_timesteps` does."""
+ return [shift * s / (1.0 + (shift - 1.0) * s) for s in sigmas]
+
+
def denoise(
model: torch.nn.Module,
img: torch.Tensor,
@@ -86,15 +92,16 @@ def denoise(
scheduler.sigmas = scheduler.sigmas[:-1]
scheduler.timesteps = scheduler.timesteps[:-1]
else:
- # FlowMatchHeunDiscreteScheduler.set_timesteps only takes a step count and derives its
- # own sigmas, so a partial-denoise range cannot be honored. Refuse instead of silently
- # running a full denoise with the wrong schedule.
- if not (math.isclose(timesteps[0], 1.0) and math.isclose(timesteps[-1], 0.0, abs_tol=1e-6)):
- raise ValueError(
- f"{type(scheduler).__name__} does not accept an explicit sigma schedule, so it cannot honor "
- "denoising_start/denoising_end. Use the euler or lcm scheduler for partial denoising."
- )
- scheduler.set_timesteps(num_inference_steps=len(timesteps) - 1, device=img.device)
+ # `FlowMatchHeunDiscreteScheduler.set_timesteps` only takes a step count: it derives
+ # its own schedule and applies `shift` to it, so it cannot be handed a sigma window
+ # directly. Rebuild its expanded two-evaluation layout from the requested window --
+ # shifted the way `set_timesteps` would shift it -- so a partial denoise starts at
+ # `denoising_start` and stops at `denoising_end` instead of being refused. This is
+ # the same layout the FLUX driver installs for Heun.
+ shift = float(getattr(scheduler.config, "shift", 1.0))
+ shifted = _shift_flow_sigmas(list(timesteps[:-1]), shift)
+ terminal_sigma = _shift_flow_sigmas([timesteps[-1]], shift)[0]
+ set_heun_timesteps_from_sigmas(scheduler, shifted, terminal_sigma, img.device)
if init_latents is not None:
# `scheduler.sigmas[0]` is the *shifted* first sigma; `timesteps[0]` is the raw one.
diff --git a/tests/backend/ernie_image/test_ernie_denoise.py b/tests/backend/ernie_image/test_ernie_denoise.py
index ac926d8cc4..e597c1d821 100644
--- a/tests/backend/ernie_image/test_ernie_denoise.py
+++ b/tests/backend/ernie_image/test_ernie_denoise.py
@@ -75,11 +75,46 @@ def test_heun_progress_does_not_overrun_total_steps() -> None:
assert states[-1].step == states[-1].total_steps == len(states)
-def test_heun_rejects_a_partial_denoise_range() -> None:
- # Heun's `set_timesteps` takes only a step count, so it cannot honor a custom sigma window.
- # Silently running a full denoise instead would be worse than refusing.
- with pytest.raises(ValueError, match="denoising_start/denoising_end"):
- _run(FlowMatchHeunDiscreteScheduler(), steps=8, denoising_start=0.5)
+def test_heun_honours_a_partial_denoise_range() -> None:
+ """A partial window must open at the requested sigma instead of being refused.
+
+ Heun's `set_timesteps` takes only a step count, so the driver rebuilds the schedule from the
+ window; refusing the request made every img2img, inpaint and Canvas pass with strength < 1 fail
+ on a scheduler ERNIE-Image offers. Running a full denoise instead would be worse than failing,
+ so pin the sigma the first model evaluation actually sees.
+ """
+ scheduler = FlowMatchHeunDiscreteScheduler()
+ states, sigmas, _ = _run(scheduler, steps=8, denoising_start=0.5)
+
+ scheduler_sigmas = scheduler.sigmas
+ assert scheduler_sigmas is not None
+ # Raw window start 0.5 with shift 1.0 -> 0.5. A full denoise would open at 1.0.
+ assert float(scheduler_sigmas[0]) == pytest.approx(0.5, abs=1e-6)
+ # `denoising_end` defaulted to 1.0, so the pass still ends on clean latents.
+ assert float(scheduler_sigmas[-1]) == pytest.approx(0.0, abs=1e-6)
+ # Heun evaluates the first sigma once and every later one twice, then keeps one terminal sigma
+ # for `step()` to read, so the rebuilt layout matches what `set_timesteps` would have built.
+ assert len(scheduler.timesteps) == 2 * (len(sigmas) - 1) - 1
+ assert len(scheduler_sigmas) == len(scheduler.timesteps) + 1
+ assert states[-1].step == states[-1].total_steps == len(states)
+
+
[email protected](("denoising_end", "expected_terminal"), [(1.0, 0.0), (0.75, 0.5)])
+def test_heun_keeps_the_schedulers_own_shift_on_a_partial_range(denoising_end: float, expected_terminal: float) -> None:
+ """The rebuilt window must carry the scheduler's `shift`, mapped the way `set_timesteps` maps it.
+
+ With `shift=3`, the flow shift `3 * s / (1 + 2 * s)` turns the raw start 0.5 into 0.75 and a raw
+ end of 0.25 into 0.5. Splicing raw window values into the schedule instead would start a
+ shifted scheduler's partial pass at the wrong noise level, so both ends have to be checked --
+ `denoising_end=1.0` alone cannot catch it, because the shift maps 0 to 0.
+ """
+ scheduler = FlowMatchHeunDiscreteScheduler(shift=3.0)
+ _run(scheduler, steps=8, denoising_start=0.5, denoising_end=denoising_end)
+
+ scheduler_sigmas = scheduler.sigmas
+ assert scheduler_sigmas is not None
+ assert float(scheduler_sigmas[0]) == pytest.approx(0.75, abs=1e-6)
+ assert float(scheduler_sigmas[-1]) == pytest.approx(expected_terminal, abs=1e-6)
@pytest.mark.parametrize("denoising_end", [1.0, 0.75, 0.5])