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: count the LTX-2.5 encoder worker context in every whole-module phase (#15151)

Daniel Hand4b0044b5279
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index f998777e351..8df6bc98286 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -3447,11 +3447,13 @@ def _video_prefer_whole_module(
text_encoder_mib: Optional[int],
vae_mib: Optional[int],
runtime_mib: int,
+ resident_mib: int = 0,
) -> Any:
"""Swap a streamed-DiT plan for whole-module offload when every component fits whole, one at a time.
Wan2.2-5B at 16 GiB: 0.14-0.28 s per forward whole-module vs 0.28-0.33 s int8 streamed, so streaming only wins
- very short calls. ``denoiser_mib`` is one expert of a dual-DiT family. An explicit memory mode is left as asked."""
+ very short calls. ``denoiser_mib`` is one expert of a dual-DiT family. ``resident_mib`` stays on the card in every
+ phase (LTX-2.5's encoder worker context), excluded from ``vae_mib``. An explicit memory mode is left as asked."""
if (
getattr(plan, "offload_policy", None) != OFFLOAD_GROUP
or not bool(getattr(plan, "stream_transformer", True))
@@ -3464,14 +3466,17 @@ def _video_prefer_whole_module(
budget = (getattr(plan, "estimates", None) or {}).get("safe_device_budget_mib")
if budget is None:
return plan
+ resident = max(0, int(resident_mib))
phases = (
- int(text_encoder_mib) + DEFAULT_BASE_OVERHEAD_MIB,
+ int(text_encoder_mib) + resident + DEFAULT_BASE_OVERHEAD_MIB,
int(denoiser_mib)
+ min(int(runtime_mib), _VIDEO_DENOISE_ACTIVATION_MIB)
+ + resident
+ DEFAULT_BASE_OVERHEAD_MIB,
# the decode holds only the decoded-clip share; the denoise base is already in the DiT phase
int(vae_mib)
+ max(0, int(runtime_mib) - _VIDEO_DENOISE_ACTIVATION_MIB)
+ + resident
+ DEFAULT_BASE_OVERHEAD_MIB,
)
if max(phases) > int(budget):
@@ -8532,16 +8537,19 @@ class VideoBackend:
# pins the VAE to fp32 either way, so scaling it counts those bytes twice (2.8 GB on Wan TI2V-5B) against a
# hard refusal.
vae_gb = components[2] if components is not None else 0.0
+ worker_gb = 0.0
if components is not None and fam.name == "ltx-2.5":
from .video_ltx25 import LTX25_WORKER_RESIDENT_GB
# The encoder worker is a second process on the card: its CUDA context and allocator slack stay
# resident on every tier, even when its weights are off the card. The latent upsampler a dev (single
# stage) load never attaches is already out of components[2] (video_components_gb(dev=...)).
- vae_gb += LTX25_WORKER_RESIDENT_GB
+ worker_gb = LTX25_WORKER_RESIDENT_GB
+ vae_gb += worker_gb
vae_scale = 1.0 if getattr(fam, "vae_force_fp32", False) else dtype_scale
scaled_te_gb = text_encoder_gb * dtype_scale
scaled_vae_gb = vae_gb * vae_scale
+ worker_mib = int(worker_gb * vae_scale * mib_per_gb)
scaled_companions_gb = scaled_te_gb + scaled_vae_gb if components is not None else 0.0
if log and scale != 1.0 and components is not None:
logger.info(
@@ -8591,8 +8599,9 @@ class VideoBackend:
),
denoiser_mib = None if dit_mib is None else dit_mib // experts,
text_encoder_mib = text_encoder_mib,
- vae_mib = vae_mib,
+ vae_mib = None if vae_mib is None else vae_mib - worker_mib,
runtime_mib = runtime_mib,
+ resident_mib = worker_mib,
)
# Re-plan with the scheme factor: the bf16-table plan can offload a DiT the quantised one would not.
dense_plan = planned
@@ -8654,8 +8663,9 @@ class VideoBackend:
),
denoiser_mib = int(components[0] * factor * mib_per_gb) // experts,
text_encoder_mib = text_encoder_mib,
- vae_mib = vae_mib,
+ vae_mib = None if vae_mib is None else vae_mib - worker_mib,
runtime_mib = runtime_mib,
+ resident_mib = worker_mib,
)
if _video_plan_rank(replanned) < _video_plan_rank(planned):
if log:
diff --git a/studio/backend/tests/test_video_ltx25.py b/studio/backend/tests/test_video_ltx25.py
index db8f4cc5403..9b8f4fed3d4 100644
--- a/studio/backend/tests/test_video_ltx25.py
+++ b/studio/backend/tests/test_video_ltx25.py
@@ -2189,7 +2189,9 @@ def _ltx25_pack_plan(monkeypatch, total_gib):
# Companions alone exceed the card: the DiT and the encoder both stream; the worker never holds Gemma 4.
(24, "group", True, True, False),
# The DiT streams, the companions (Gemma 4 included) stay: the worker keeps it on the card.
+ # Whole-module misses: worker context beside the whole DiT is 47152 of 46504 MiB.
(48, "group", False, True, True),
+ (50, "model", False, True, False),
# The DiT fits once the encoder is not resident: the worker onloads only for an encode, when there is room.
(80, "group", True, False, False),
(180, "none", False, True, True),
@@ -2223,6 +2225,42 @@ def test_plan_counts_the_encoder_worker_per_card(
else plan.estimates["group_floor_mib"]
)
assert floor <= budget and inputs["text_encoder_dense_mib"] <= floor
+ if policy == "model":
+ worker_mib = int(LTX25_WORKER_RESIDENT_GB * mib)
+ assert plan.estimates["whole_module_fits_mib"] >= int(42.0 * mib) + worker_mib
+ assert plan.estimates["whole_module_fits_mib"] <= plan.estimates["safe_device_budget_mib"]
+
+
+def test_whole_module_phases_count_the_always_resident_share():
+ import dataclasses
+
+ from core.inference import video as vid
+ from core.inference.diffusion_memory import (
+ DEFAULT_BASE_OVERHEAD_MIB,
+ MEMORY_MODE_AUTO,
+ OFFLOAD_GROUP,
+ OFFLOAD_MODEL,
+ )
+
+ @dataclasses.dataclass
+ class _Plan:
+ offload_policy: str = OFFLOAD_GROUP
+ stream_transformer: bool = True
+ stream_text_encoders: bool = False
+ requested_mode: str = MEMORY_MODE_AUTO
+ vae_tiling: bool = False
+ vae_slicing: bool = False
+ reasons: tuple = ()
+ estimates: dict = dataclasses.field(
+ default_factory = lambda: {"safe_device_budget_mib": 46504}
+ )
+
+ plan = _Plan()
+ kw = dict(denoiser_mib = 40054, text_encoder_mib = 22792, vae_mib = 8773, runtime_mib = 7839)
+ dit_phase = 40054 + 4096 + DEFAULT_BASE_OVERHEAD_MIB
+ assert dit_phase <= 46504 < dit_phase + 954
+ assert vid._video_prefer_whole_module(plan, **kw).offload_policy == OFFLOAD_MODEL
+ assert vid._video_prefer_whole_module(plan, **kw, resident_mib = 954) is plan
class _DeviceTensor: