Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep the Z-Image CFG split on when group offload leaves every group resident (#14955)

Daniel Han8785d7b3cf49
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index c1778abd7ea..56e194a5a4c 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -118,6 +118,7 @@ from .diffusion_device import (
from .diffusion_cfg_batch import cfg_batch_status, end_cfg_batch_render, install_cfg_batch
from .diffusion_zimage_cfg_split import (
install_zimage_cfg_split,
+ split_aware_request_extra,
uninstall_zimage_cfg_split,
zimage_cfg_split_status,
)
@@ -316,6 +317,7 @@ from .diffusion_cache import (
TC_STATIC,
apply_step_cache,
auto_static_skip_plan,
+ auto_step_cache_mode,
auto_step_cache_allowed,
skip_tier,
cache_breaks_graph,
@@ -13501,6 +13503,34 @@ class DiffusionBackend:
# cap; the OOM backoff below halves a failed chunk. Layered: one input per forward (the pipeline's
# per-prompt layer stride is only right for one).
chunks = _generation_chunks(state.family, jobs, batch_size, layer_count = layer_count)
+
+ # EFFECTIVE steps: img2img at strength < 1 denoises a fraction of `steps`.
+ strength_applied = effective_request_strength(
+ strength,
+ init_pil is not None,
+ "strength" in call_params,
+ call_params["strength"].default if "strength" in call_params else None,
+ )
+ denoise_steps = effective_denoise_steps(steps, strength_applied)
+ auto_step_cache_on = False
+ # (mode,) once the pre-guard disable ran, so the toggle below is not repeated
+ pre_guard_toggle = None
+ if state.cache_auto:
+ # Judged on the pipeline that RUNS: from_pipe workflows open no cache_context for FBCache.
+ from .diffusion_autoopt.runtime import auto_cache_steps
+
+ cache_steps, runner_reason = auto_cache_steps(state.pipe, pipe, denoise_steps)
+ auto_step_cache_on = auto_step_cache_mode(cache_steps) is not None
+ if not auto_step_cache_on:
+ pre_guard_toggle = (
+ maybe_toggle_step_cache(
+ state.pipe,
+ steps = cache_steps,
+ quant_active = state.cache_quant_active,
+ threshold = state.cache_threshold,
+ logger = logger,
+ ),
+ )
guard_width = guard_height = guard_target = None
guard_batch = 1
@@ -13553,15 +13583,21 @@ class DiffusionBackend:
and ref_resolution is None
and not is_flux3_family(fam)
)
- extra_mib = measured_request_extra_mib(
+ guard_cfg, extra_mib = split_aware_request_extra(
state.pipe,
- width = guard_width,
- height = guard_height,
- batch_size = guard_batch,
- condition_pixels = guard_condition_pixels,
- cfg = guard_cfg,
- unpriced_references = guard_unpriced_refs,
- workflow = _peak_workflow(fam, workflow),
+ guard_cfg,
+ workflow,
+ lambda cfg: measured_request_extra_mib(
+ state.pipe,
+ width = guard_width,
+ height = guard_height,
+ batch_size = guard_batch,
+ condition_pixels = guard_condition_pixels,
+ cfg = cfg,
+ unpriced_references = guard_unpriced_refs,
+ workflow = _peak_workflow(fam, workflow),
+ ),
+ step_cache = auto_step_cache_on,
)
if extra_mib > 0:
releasable_mib = resident_group_mib(state.pipe)
@@ -13766,29 +13802,18 @@ class DiffusionBackend:
):
return pipe(**chunk_kwargs).images
- # EFFECTIVE steps: img2img at strength < 1 denoises a fraction of `steps`.
- strength_applied = effective_request_strength(
- strength,
- init_pil is not None,
- "strength" in call_params,
- call_params["strength"].default if "strength" in call_params else None,
- )
- denoise_steps = effective_denoise_steps(steps, strength_applied)
-
# Re-check an AUTO cache decision against the ACTUAL step count; explicit choices never toggle.
if state.cache_auto:
- # Judged against the pipeline that RUNS this call: a from_pipe workflow (FLUX.1 img2img /
- # inpaint) shares the denoiser but opens no cache_context, which the FBCache hooks need.
- from .diffusion_autoopt.runtime import auto_cache_steps
-
- cache_steps, runner_reason = auto_cache_steps(state.pipe, pipe, denoise_steps)
- toggled = maybe_toggle_step_cache(
- state.pipe,
- steps = cache_steps,
- quant_active = state.cache_quant_active,
- threshold = state.cache_threshold,
- logger = logger,
- )
+ if pre_guard_toggle is not None:
+ toggled = pre_guard_toggle[0]
+ else:
+ toggled = maybe_toggle_step_cache(
+ state.pipe,
+ steps = cache_steps,
+ quant_active = state.cache_quant_active,
+ threshold = state.cache_threshold,
+ logger = logger,
+ )
if toggled != state.transformer_cache:
# _LoadState is frozen; the one deliberate in-place update, so status() reports the pipe-level
# toggle.
diff --git a/studio/backend/core/inference/diffusion_zimage_cfg_split.py b/studio/backend/core/inference/diffusion_zimage_cfg_split.py
index ae1b1cfa111..63008833fc9 100644
--- a/studio/backend/core/inference/diffusion_zimage_cfg_split.py
+++ b/studio/backend/core/inference/diffusion_zimage_cfg_split.py
@@ -18,7 +18,7 @@ bit-exact: a single sample with no mask can pick other attention / GEMM tiles th
Engaged per render (``render``: Studio knows the images per call, so ``2 * B`` samples is a guided call, ``B`` is not),
only on a resident denoiser: a block-streamed one would upload its weights twice per step, which costs more than the
-recompile. Declined per call under a step cache and a static step skip that cannot key a step's two calls apart.
+recompile. Group offload counts as resident (checked per call) while every offload group is pinned. Declined per call under a step cache and a static step skip that cannot key a step's two calls apart.
Under a CFG-parallel router the halves go to ``run_pair`` (identity pairing never matches fresh tensors). Kill switch: ``UNSLOTH_DIFFUSION_ZIMAGE_CFG_SPLIT=0``.
Studio arms it ``compiled_only``: a render splits only while the denoiser runs compiled code or replays CUDA graphs,
@@ -39,7 +39,8 @@ from typing import Any, Optional
ZIMAGE_CFG_SPLIT_ENV = "UNSLOTH_DIFFUSION_ZIMAGE_CFG_SPLIT"
_TRANSFORMER_CLASS = "ZImageTransformer2DModel"
-_STREAMED_POLICIES = ("group", "streaming", "sequential")
+_STREAMED_POLICIES = ("streaming", "sequential")
+_GROUP_POLICY = "group"
# Instance attributes: armed at load, images per guidance-off call during a render, per-load counters.
_ARMED_ATTR = "_unsloth_zimage_cfg_split"
_SAMPLES_ATTR = "_unsloth_zimage_cfg_samples"
@@ -48,6 +49,9 @@ _COMPILED_ONLY_ATTR = "_unsloth_zimage_cfg_split_compiled_only"
# During an eager compiled_only render: the guided call runs batched and splits only once it ran out of memory.
_EAGER_ATTR = "_unsloth_zimage_cfg_eager"
_OOM_ATTR = "_unsloth_zimage_cfg_oom"
+_POLICY_ATTR = "_unsloth_zimage_cfg_policy"
+# diffusion_memory._keep_groups_resident / release_resident_groups keep {"streamed": <groups not resident>} here.
+_STREAM_STATE_ATTR = "_unsloth_stream_state"
# Keyword arguments a split call may carry (anything else, e.g. siglip / controlnet / noise masks, runs batched).
_PLAIN_KWARGS = frozenset({"return_dict", "joint_attention_kwargs", "patch_size", "f_patch_size"})
@@ -77,8 +81,29 @@ def _skip_keys_calls_apart(skip: Any) -> bool:
return getattr(skip, "context", None) is None and bool(getattr(skip, "step_signal", False))
+def _has_offload_groups(module: Any) -> bool:
+ try:
+ from .diffusion_memory import _offload_groups
+ return bool(_offload_groups(module))
+ except Exception: # noqa: BLE001 - unknown: streaming
+ return True
+
+
+def _groups_stream(module: Any, slots: dict) -> bool:
+ """Whether any offload group streams now (unknown = streaming)."""
+ state = slots.get(_STREAM_STATE_ATTR)
+ if not isinstance(state, dict):
+ return _has_offload_groups(module)
+ try:
+ return int(state.get("streamed", 1)) > 0
+ except (TypeError, ValueError):
+ return True
+
+
def _declined_now(module: Any) -> Optional[str]:
slots = getattr(module, "__dict__", {})
+ if slots.get(_POLICY_ATTR) == _GROUP_POLICY and _groups_stream(module, slots):
+ return "denoiser blocks stream (group offload)"
# FBCache keeps one residual per cache_context; two calls in one context would compare cond against uncond.
if getattr(module, "_unsloth_step_cache", None):
return "step cache"
@@ -217,6 +242,25 @@ def _router_declines_now() -> bool:
return False
+def _routed_now(slots: dict) -> bool:
+ router = slots.get("_unsloth_cfg_router")
+ return (
+ router is not None
+ and slots.get("forward") is router
+ and getattr(router, "disabled_reason", None) is None
+ and not _router_declines_now()
+ )
+
+
+def _eager_now(pipe: Any, denoiser: Any, slots: dict) -> bool:
+ """compiled_only with nothing compiled or graphed and no active router: the render stays batched (split on OOM)."""
+ return (
+ bool(slots.get(_COMPILED_ONLY_ATTR))
+ and not _graphs_engaged(pipe, denoiser)
+ and not _routed_now(slots)
+ )
+
+
def _static_skip_state(slots: dict) -> Optional[tuple]:
skip = slots.get("_unsloth_static_skip")
if skip is None or not hasattr(skip, "ordinal"):
@@ -259,12 +303,7 @@ def _make_call(orig_call: Any) -> Any:
stats["batched"] += 1
return orig_call(self, *args, **kwargs)
router = slots.get("_unsloth_cfg_router")
- routed = (
- router is not None
- and slots.get("forward") is router
- and getattr(router, "disabled_reason", None) is None
- and not _router_declines_now()
- )
+ routed = _routed_now(slots)
# A CFG-parallel router needs the halves even eager: it cannot pair one batched call.
if slots.get(_EAGER_ATTR) and not slots.get(_OOM_ATTR) and not routed:
# Eager: one batched call is faster; two calls at half the activations only when it does not fit.
@@ -329,6 +368,7 @@ def uninstall_zimage_cfg_split(pipe: Any = None) -> None:
_COMPILED_ONLY_ATTR,
_EAGER_ATTR,
_OOM_ATTR,
+ _POLICY_ATTR,
):
slots.pop(name, None)
with _LOCK:
@@ -367,12 +407,21 @@ def install_zimage_cfg_split(
denoiser.__dict__[_ARMED_ATTR] = True
denoiser.__dict__[_STATS_ATTR] = {"split": 0, "batched": 0}
denoiser.__dict__[_COMPILED_ONLY_ATTR] = bool(compiled_only)
+ denoiser.__dict__[_POLICY_ATTR] = offload_policy
else:
denoiser.__dict__.pop(_ARMED_ATTR, None)
+ denoiser.__dict__.pop(_POLICY_ATTR, None)
if logger is not None:
logger.info(
"diffusion.zimage_cfg_split: %s",
- "armed (guided steps run as two calls at the guidance-off batch)"
+ (
+ "armed (guided steps run as two calls at the guidance-off batch"
+ + (
+ ", while every offload group is resident)"
+ if offload_policy == _GROUP_POLICY
+ else ")"
+ )
+ )
if reason is None
else f"not armed ({reason})",
)
@@ -383,6 +432,45 @@ def install_zimage_cfg_split(
return f"{type(exc).__name__}: {exc}"
+# Workflows whose Z-Image call is the plain one ``_halves`` splits (upscale / hires refine run the img2img pipeline).
+SPLIT_PRICED_WORKFLOWS = ("txt2img", "img2img", "inpaint", "upscale")
+
+
+def split_aware_request_extra(
+ pipe: Any,
+ cfg: Optional[bool],
+ workflow: str,
+ price: Any,
+ *,
+ step_cache: bool = False,
+) -> tuple:
+ """``(cfg priced, extra MiB)``: guidance-off price only when the split will engage (no AUTO step cache)."""
+ if (
+ cfg is not False
+ and workflow in SPLIT_PRICED_WORKFLOWS
+ and not step_cache
+ and guided_runs_at_guidance_off_batch(pipe)
+ ):
+ extra = int(price(False))
+ if extra <= 0:
+ return False, extra
+ return cfg, int(price(cfg))
+
+
+def guided_runs_at_guidance_off_batch(pipe: Any) -> bool:
+ """Whether a guided render runs at the guidance-off batch."""
+ denoiser = getattr(pipe, "transformer", None)
+ slots = getattr(denoiser, "__dict__", None)
+ if not isinstance(slots, dict) or not slots.get(_ARMED_ATTR) or zimage_cfg_split_disabled():
+ return False
+ try:
+ if _eager_now(pipe, denoiser, slots):
+ return False
+ return _declined_now(denoiser) is None
+ except Exception: # noqa: BLE001 - unknown: sized as the batched call
+ return False
+
+
def zimage_cfg_split_status(pipe: Any) -> Optional[dict]:
denoiser = getattr(pipe, "transformer", None)
if denoiser is None or not _is_zimage(denoiser):
@@ -400,8 +488,8 @@ def render(pipe: Any, samples: int):
if not armed:
yield
return
- # Eager (compiled_only, nothing compiled or graphed): batched, split only after the batched call ran out of memory.
- eager = bool(slots.get(_COMPILED_ONLY_ATTR)) and not _graphs_engaged(pipe, denoiser)
+ # Decided once per render, matching the guard's price: eager stays batched (split on OOM).
+ eager = _eager_now(pipe, denoiser, slots)
denoiser.__dict__[_SAMPLES_ATTR] = int(samples)
if eager:
denoiser.__dict__[_EAGER_ATTR] = True
diff --git a/studio/backend/tests/test_diffusion_dense_tier_activation.py b/studio/backend/tests/test_diffusion_dense_tier_activation.py
index 6893f44bc43..8e3b44a6b06 100644
--- a/studio/backend/tests/test_diffusion_dense_tier_activation.py
+++ b/studio/backend/tests/test_diffusion_dense_tier_activation.py
@@ -249,5 +249,6 @@ def test_generate_passes_unpriced_references_to_the_guard():
"not is_flux3_family(fam)",
):
assert part in window, part
- call = src.index("extra_mib = measured_request_extra_mib(", at)
- assert "unpriced_references = guard_unpriced_refs," in src[call : call + 500]
+ call = src.index("extra_mib = split_aware_request_extra(", at)
+ assert "measured_request_extra_mib(" in src[call : call + 500]
+ assert "unpriced_references = guard_unpriced_refs," in src[call : call + 800]
diff --git a/studio/backend/tests/test_diffusion_learned_peaks.py b/studio/backend/tests/test_diffusion_learned_peaks.py
index 215764e4387..592c28727b1 100644
--- a/studio/backend/tests/test_diffusion_learned_peaks.py
+++ b/studio/backend/tests/test_diffusion_learned_peaks.py
@@ -1834,7 +1834,9 @@ def test_cfg_off_keys_are_wired_into_the_load_and_guard():
src = "".join(src.split()) # whitespace-free: the formatter may wrap these lines
for needle in (
'pipe._unsloth_peak_expects_cfg = bool(getattr(fam, "uses_negative_prompt", False))',
- "cfg = guard_cfg,",
+ # the guard's CFG value reaches the pricer through the split-aware wrapper
+ "guard_cfg, extra_mib = split_aware_request_extra(state.pipe, guard_cfg, workflow, lambda cfg:",
+ "cfg = cfg, unpriced_references = guard_unpriced_refs,",
'plan.estimates.get("measured_learned_cfg_off", False),',
"state.pipe._unsloth_peak_cfg = _negative_prompt_engaged(state.family, guidance)",
):
diff --git a/studio/backend/tests/test_diffusion_zimage_cfg_split.py b/studio/backend/tests/test_diffusion_zimage_cfg_split.py
index 558e4bd4f3c..92143e134e5 100644
--- a/studio/backend/tests/test_diffusion_zimage_cfg_split.py
+++ b/studio/backend/tests/test_diffusion_zimage_cfg_split.py
@@ -152,13 +152,54 @@ def test_guidance_off_and_unarmed_calls_are_untouched():
assert zs.zimage_cfg_split_status(pipe)["split"] == 0
[email protected]("policy", ["group", "streaming", "sequential"])
[email protected]("policy", ["streaming", "sequential"])
def test_streamed_denoiser_is_not_armed(policy):
pipe = types.SimpleNamespace(transformer = _model())
assert "streamed" in zs.install_zimage_cfg_split(pipe, offload_policy = policy)
assert zs.zimage_cfg_split_status(pipe)["armed"] is False
+def test_group_offload_with_every_group_resident_splits():
+ """12 GiB Z-Image-Turbo int8 / fp8 = group offload, all 37 groups resident; declining it cost 3 recompiles."""
+ torch._dynamo.reset()
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ assert zs.install_zimage_cfg_split(pipe, offload_policy = "group") is None
+ assert zs.zimage_cfg_split_status(pipe)["armed"] is True
+ model._unsloth_stream_state = {
+ "streamed": 0
+ } # what _keep_groups_resident records when every group is pinned
+ _compile_blocks(model)
+ t = torch.tensor([0.4])
+ with zs.render(pipe, 1):
+ _run(model, [_latent(0)], t, [_caption(9, 0)])
+ _run(model, [_latent(1)], t, [_caption(45, 1)])
+ warm = _graphs()
+ _run(model, *_guided_args(_latent(2), t, 45, 9, 2))
+ _run(model, *_guided_args(_latent(3), t, 7, 9, 3))
+ assert _graphs() == warm
+ assert zs.zimage_cfg_split_status(pipe)["split"] == 2
+
+
[email protected]("state", [None, {"streamed": 3}, {"streamed": "?"}])
+def test_group_offload_while_groups_stream_keeps_the_batched_call(state, monkeypatch):
+ """Streaming groups (partial, released, or hooked with no record) keep the batched call."""
+ monkeypatch.setattr(zs, "_has_offload_groups", lambda module: True)
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ assert zs.install_zimage_cfg_split(pipe, offload_policy = "group") is None
+ if state is not None:
+ model._unsloth_stream_state = state
+ t = torch.tensor([0.4])
+ with zs.render(pipe, 1):
+ out = _run(model, *_guided_args(_latent(2), t, 45, 9, 2))
+ stats = zs.zimage_cfg_split_status(pipe)
+ assert stats["split"] == 0 and stats["batched"] == 1
+ zs.uninstall_zimage_cfg_split(pipe)
+ stock = _run(model, *_guided_args(_latent(2), t, 45, 9, 2)) # the unpatched, batched call
+ assert all(torch.equal(a, b) for a, b in zip(out, stock))
+
+
def test_step_cache_and_step_skip_keep_the_batched_call():
model = _model()
pipe = types.SimpleNamespace(transformer = model)
@@ -457,6 +498,108 @@ def test_compiled_only_eager_oom_retries_split_for_the_render(monkeypatch):
_run(model, *args)
+def test_group_offload_resident_transformer_tier_splits():
+ """stream_transformer=False: only the text encoders stream, the transformer has no groups and no residency record."""
+ torch._dynamo.reset()
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ assert zs.install_zimage_cfg_split(pipe, offload_policy = "group") is None
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is True
+ _compile_blocks(model)
+ t = torch.tensor([0.4])
+ with zs.render(pipe, 1):
+ _run(model, [_latent(0)], t, [_caption(9, 0)])
+ _run(model, [_latent(1)], t, [_caption(45, 1)])
+ warm = _graphs()
+ _run(model, *_guided_args(_latent(2), t, 45, 9, 2))
+ assert _graphs() == warm
+ assert zs.zimage_cfg_split_status(pipe)["split"] == 1
+
+
+def test_request_guard_reprices_doubled_when_the_split_would_be_declined():
+ """Releasing groups or an AUTO FBCache engaging after the guard declines the split: the doubled call is priced."""
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ zs.install_zimage_cfg_split(pipe, offload_policy = "none")
+
+ def price(extra_split, extra_cfg):
+ def _price(cfg):
+ return extra_split if cfg is False else extra_cfg
+
+ return _price
+
+ for workflow in ("txt2img", "img2img", "inpaint", "upscale"):
+ assert zs.split_aware_request_extra(pipe, True, workflow, price(0, 900)) == (False, 0)
+ assert zs.split_aware_request_extra(pipe, None, "upscale", price(-5, 900)) == (False, -5)
+ # Fits only after a release: the release can demote denoiser groups, so the batched call is priced.
+ assert zs.split_aware_request_extra(pipe, True, "txt2img", price(100, 900)) == (True, 900)
+ assert zs.split_aware_request_extra(pipe, None, "txt2img", price(100, 900)) == (None, 900)
+ assert zs.split_aware_request_extra(pipe, True, "txt2img", price(0, 900), step_cache = True) == (
+ True,
+ 900,
+ )
+ assert zs.split_aware_request_extra(pipe, True, "controlnet", price(0, 900)) == (True, 900)
+ assert zs.split_aware_request_extra(pipe, False, "txt2img", price(0, 900)) == (False, 0)
+ model._unsloth_step_cache = object()
+ assert zs.split_aware_request_extra(pipe, True, "txt2img", price(0, 900)) == (True, 900)
+
+
+def test_studio_guard_passes_the_auto_step_cache_prediction():
+ import ast
+ import pathlib
+
+ source = pathlib.Path(zs.__file__).with_name("diffusion.py").read_text()
+ tree = ast.parse(source)
+ calls = [
+ node
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Call)
+ and getattr(node.func, "id", None) == "split_aware_request_extra"
+ ]
+ assert len(calls) == 1
+ assert {k.arg for k in calls[0].keywords} == {"step_cache"}
+ assert "guided_runs_at_guidance_off_batch(" not in source
+ predict = source.index("auto_step_cache_on = auto_step_cache_mode(cache_steps)")
+ assert predict < source.index("guard_cfg, extra_mib = split_aware_request_extra(")
+ assert predict < source.index("toggled = maybe_toggle_step_cache(")
+
+
+def test_request_guard_sizes_a_split_guided_render_as_guidance_off(monkeypatch):
+ """Pricing a split render doubled released 5 GiB of resident groups, declining the split; it needs no extra memory."""
+ monkeypatch.setattr(zs, "_has_offload_groups", lambda module: True)
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False # not armed
+ zs.install_zimage_cfg_split(pipe, offload_policy = "group")
+ assert (
+ zs.guided_runs_at_guidance_off_batch(pipe) is False
+ ) # hooked, no residency record: groups stream
+ model._unsloth_stream_state = {"streamed": 0}
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is True
+ model._unsloth_stream_state = {"streamed": 2}
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+ model._unsloth_stream_state = {"streamed": 0}
+ model._unsloth_step_cache = object()
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False # FBCache keeps the batched call
+ del model._unsloth_step_cache
+ resident = types.SimpleNamespace(transformer = _model())
+ zs.install_zimage_cfg_split(resident, offload_policy = "none")
+ assert zs.guided_runs_at_guidance_off_batch(resident) is True
+
+
+def test_request_guard_prices_an_eager_compiled_only_load_as_doubled():
+ """compiled_only: eager keeps the doubled price, compiled gets the guidance-off one."""
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ assert zs.install_zimage_cfg_split(pipe, offload_policy = "group", compiled_only = True) is None
+ model._unsloth_stream_state = {"streamed": 0}
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+ model._unsloth_regional_compile_kwargs = {"dynamic": True}
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is True
+ model._unsloth_stream_state = {"streamed": 1}
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+
+
def test_eager_oom_split_keeps_static_skip_branch_keys(monkeypatch):
"""A batched eager call that runs out of memory consumed a static-skip ordinal; the two halves still key cond as
``#0`` and uncond as ``#1``, so a later skipped step reuses each branch's own prediction."""
@@ -560,3 +703,46 @@ def test_compiled_only_honours_the_background_compile_eager_gate():
with zs.render(pipe, 1):
_run(model, *args)
assert zs.zimage_cfg_split_status(pipe)["split"] == 1
+
+
+def test_eager_render_behind_an_active_router_is_priced_and_run_split():
+ """Eager compiled_only behind an active router: priced guidance-off and keeps splitting if the router gives up."""
+ from core.inference import diffusion_bg_compile as bg
+ from core.inference.diffusion_multigpu import CfgParallelRouter
+
+ model = _model()
+ pipe = types.SimpleNamespace(transformer = model)
+ zs.install_zimage_cfg_split(pipe, offload_policy = "none", compiled_only = True)
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+ router = CfgParallelRouter(model, primary = "cpu", secondary = "cpu")
+ model.__dict__["forward"] = router
+ model.__dict__["_unsloth_cfg_router"] = router
+ args = _guided_args(_latent(17), torch.tensor([0.4]), 9, 12, 17)
+ try:
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is True
+ with bg.force_eager():
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+ with zs.render(pipe, 1):
+ _run(model, *args)
+ router.disabled_reason = "replica failed"
+ _run(model, *args)
+ assert zs.zimage_cfg_split_status(pipe)["split"] == 2
+ assert zs.guided_runs_at_guidance_off_batch(pipe) is False
+ finally:
+ model.__dict__.pop("forward", None)
+ model.__dict__.pop("_unsloth_cfg_router", None)
+
+
+def test_studio_guard_disengages_a_pending_auto_cache_before_pricing():
+ """A cache the AUTO toggle will turn off is disengaged before the guard, so the split is not priced as declined."""
+ source = _diffusion_source()
+ predict = source.index("auto_step_cache_on = auto_step_cache_mode(cache_steps)")
+ guard = source.index("guard_cfg, extra_mib = split_aware_request_extra(")
+ early = source.index("maybe_toggle_step_cache(", predict)
+ assert predict < early < guard
+ assert "if not auto_step_cache_on:" in source[predict:early]
+
+
+def _diffusion_source():
+ import pathlib
+ return pathlib.Path(zs.__file__).with_name("diffusion.py").read_text()