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 LTX-2.5: compact conditioning transport and stage-two connector reuse (#17232)

Daniel Han75d03078f013
diff --git a/studio/backend/core/inference/video_ltx25.py b/studio/backend/core/inference/video_ltx25.py
index 5dcae5237ea..2eedd924be2 100644
--- a/studio/backend/core/inference/video_ltx25.py
+++ b/studio/backend/core/inference/video_ltx25.py
@@ -6,6 +6,7 @@ encoder runs in a worker on the .venv_t5_510 transformers 5.10 sidecar"""
from __future__ import annotations
+import contextlib
import os
import queue as _queue
import threading
@@ -853,6 +854,44 @@ def ensure_text_encoder_sidecar() -> str:
return _VENV_T5_510_DIR
+# ``0``: the old path (full-width transport, connectors rerun in stage two).
+COMPACT_CONDITIONING_ENV = "UNSLOTH_LTX25_COMPACT_CONDITIONING"
+
+# Engagement counters, read by tests.
+CONDITIONING_STATS: dict[str, int] = {
+ "compact_encodes": 0,
+ "full_encodes": 0,
+ "host_bytes": 0,
+ "upload_bytes": 0,
+ "connector_runs": 0,
+ "connector_reuses": 0,
+}
+
+
+def compact_conditioning_enabled() -> bool:
+ return (os.environ.get(COMPACT_CONDITIONING_ENV) or "").strip() != "0"
+
+
+def _tensor_bytes(t: Any) -> int:
+ try:
+ return int(t.numel()) * int(t.element_size())
+ except Exception: # noqa: BLE001 -- not a tensor
+ return 0
+
+
+def expand_valid_rows(rows: Any, mask: Any, *, device: Any, dtype: Any) -> Any:
+ """Inverse of ``compact_valid_rows`` on ``device``, padded rows zero. The connectors zero padded rows before
+ mixing tokens, so their outputs equal the full-width ones."""
+ import torch
+
+ mask = mask.to(device = device)
+ batch, seq_len = mask.shape[0], mask.shape[-1]
+ rows = rows.to(device = device, dtype = dtype)
+ full = torch.zeros((batch, seq_len, rows.shape[-1]), device = rows.device, dtype = rows.dtype)
+ full.masked_scatter_(mask.view(batch, seq_len, 1).bool(), rows)
+ return full
+
+
def _release_cached_device_memory(device: Optional[str]) -> None:
kind = str(device or "").split(":", 1)[0]
if kind not in ("cuda", "xpu"):
@@ -1344,8 +1383,15 @@ class Ltx25TextEncoderClient:
self.close()
return True
- def cached(self, prompts: list[str], max_sequence_length: int) -> bool:
- key = (tuple(prompts), int(max_sequence_length))
+ def cached(
+ self,
+ prompts: list[str],
+ max_sequence_length: int,
+ *,
+ compact: Optional[bool] = None,
+ ) -> bool:
+ compact = compact_conditioning_enabled() if compact is None else bool(compact)
+ key = (tuple(prompts), int(max_sequence_length), compact)
with self._lock:
return any(cached_key == key for cached_key, _ in self._cache)
@@ -1356,10 +1402,12 @@ class Ltx25TextEncoderClient:
cancel: Optional[threading.Event] = None,
*,
copy: bool = True,
+ compact: Optional[bool] = None,
) -> tuple[Any, Any]:
"""``copy=False`` returns the cached host tensors themselves, for a caller that only reads them: a 385 MB host
- clone per call is ~40 ms on a B200 host."""
- key = (tuple(prompts), int(max_sequence_length))
+ clone per call is ~40 ms on a B200 host. Compact (the default) embeds are ``[valid rows, D]``."""
+ compact = compact_conditioning_enabled() if compact is None else bool(compact)
+ key = (tuple(prompts), int(max_sequence_length), compact)
with self._lock:
for i, (cached_key, value) in enumerate(self._cache):
if cached_key == key:
@@ -1386,6 +1434,7 @@ class Ltx25TextEncoderClient:
# The server pins its own host weights during the first render; the encoder's pin waits for that
# so it never takes the host RAM those pins need.
"pin_host": self.renders > 1,
+ "compact": compact,
}
)
self._inflight = seq
@@ -1403,6 +1452,14 @@ class Ltx25TextEncoderClient:
self.last_encode = {
k: v for k, v in msg.items() if k not in ("type", "id", "embeds", "mask")
}
+ # An older worker replies full width: ``_one`` reads the shape, not the request.
+ CONDITIONING_STATS[
+ "compact_encodes" if getattr(embeds, "ndim", 3) == 2 else "full_encodes"
+ ] += 1
+ CONDITIONING_STATS["host_bytes"] += _tensor_bytes(embeds) + _tensor_bytes(mask)
+ self.last_encode["host_mib"] = round(
+ (_tensor_bytes(embeds) + _tensor_bytes(mask)) / 2**20, 2
+ )
logger.info(
"video.ltx25_te: encoded on %s %s", self.last_encode.get("device"), self.last_encode
)
@@ -1434,10 +1491,14 @@ def install_remote_encode_prompt(pipe: Any, client: Ltx25TextEncoderClient) -> N
device = device or pipe._execution_device
dtype = dtype or getattr(getattr(pipe, "transformer", None), "dtype", None)
+ transport: dict[str, bool] = {}
+
def _lend(*texts: list[str]) -> Optional[Callable[..., None]]:
# Cache miss: lend resident DiT blocks to the worker for the encode.
is_cached = getattr(client, "cached", None)
- if not callable(is_cached) or all(is_cached(t, max_sequence_length) for t in texts):
+ if not callable(is_cached) or all(
+ is_cached(t, max_sequence_length, **transport) for t in texts
+ ):
return None
return lend_resident_dit_for_encode(pipe, client)
@@ -1445,7 +1506,9 @@ def install_remote_encode_prompt(pipe: Any, client: Ltx25TextEncoderClient) -> N
"""Cached (read-only) tensors per prompt list, all encoded under one lease."""
restore = _lend(*texts)
try:
- return [client.encode(t, max_sequence_length, copy = False) for t in texts]
+ return [
+ client.encode(t, max_sequence_length, copy = False, **transport) for t in texts
+ ]
except BaseException:
# A cancelled encode may still use the lent memory: stop it before pinning back.
stop = getattr(client, "stop_if_busy", None)
@@ -1458,14 +1521,19 @@ def install_remote_encode_prompt(pipe: Any, client: Ltx25TextEncoderClient) -> N
def _one(texts: list[str], host: Optional[tuple] = None) -> tuple[Any, Any]:
cached, mask = host if host is not None else _hosts(texts)[0]
- batch = cached.shape[0]
- embeds = cached.to(device = device, dtype = dtype)
+ CONDITIONING_STATS["upload_bytes"] += _tensor_bytes(cached) + _tensor_bytes(mask)
+ batch = mask.shape[0]
+ if cached.ndim == 2:
+ # A fresh tensor, never an alias of the cache.
+ embeds = expand_valid_rows(cached, mask, device = device, dtype = dtype)
+ else:
+ embeds = cached.to(device = device, dtype = dtype)
mask = mask.to(device = device)
seq_len = embeds.shape[1]
# A ``.to`` that moved or cast already copied, so one video skips ``repeat``; a no-op ``.to`` aliases the cache.
if (
num_videos_per_prompt != 1
- or embeds.data_ptr() == cached.data_ptr()
+ or (cached.ndim != 2 and embeds.data_ptr() == cached.data_ptr())
or not embeds.is_contiguous()
):
embeds = embeds.repeat(1, num_videos_per_prompt, 1).view(
@@ -1476,6 +1544,9 @@ def install_remote_encode_prompt(pipe: Any, client: Ltx25TextEncoderClient) -> N
prompt = [prompt] if isinstance(prompt, str) else prompt
batch_size = len(prompt) if prompt is not None else prompt_embeds.shape[0]
+ if num_videos_per_prompt != 1 and batch_size > 1 and compact_conditioning_enabled():
+ # diffusers repeats embeds per prompt but the mask per batch, so padded rows can count as valid there.
+ transport["compact"] = False
if (
prompt_embeds is None
and do_classifier_free_guidance
@@ -1804,6 +1875,87 @@ def render(
pass
+def reuse_connector_outputs(runner: Any):
+ """Inside ``encode_once``: connectors called on exactly the tensors an ``encode_prompt`` of this span returned
+ (identity, via weak references) reuse the first such call's outputs; any other input (CFG concatenation) runs."""
+ connectors = getattr(runner, "connectors", None)
+ encode = getattr(runner, "encode_prompt", None)
+ if (
+ not compact_conditioning_enabled()
+ or connectors is None
+ or not callable(encode)
+ or not callable(getattr(connectors, "forward", None))
+ ):
+ yield
+ return
+ import weakref
+
+ import torch
+
+ returned: list[tuple[str, Any, Any]] = []
+ results: dict[tuple[str, str], tuple] = {}
+ had_encode = "encode_prompt" in getattr(runner, "__dict__", {})
+ wrapped: dict[str, Any] = {}
+
+ def arm() -> None:
+ # Again before each call: an offload reset between the calls installs a new hooked ``forward``.
+ if connectors.__dict__.get("forward") is forward:
+ return
+ wrapped["own"] = "forward" in connectors.__dict__
+ wrapped["prior"] = connectors.__dict__.get("forward")
+ wrapped["inner"] = connectors.forward
+ connectors.forward = forward
+
+ def encode_prompt(*args: Any, **kwargs: Any) -> Any:
+ out = encode(*args, **kwargs)
+ if (
+ not args
+ and not any(isinstance(v, torch.Tensor) for v in kwargs.values())
+ and isinstance(out, tuple)
+ and len(out) >= 2
+ and isinstance(out[0], torch.Tensor)
+ and isinstance(out[1], torch.Tensor)
+ ):
+ key = repr(sorted((k, repr(v)) for k, v in kwargs.items()))
+ returned.append((key, weakref.ref(out[0]), weakref.ref(out[1])))
+ arm()
+ return out
+
+ def forward(*args: Any, **kwargs: Any) -> Any:
+ source = None
+ if len(args) >= 2:
+ source = next((k for k, e, m in returned if e() is args[0] and m() is args[1]), None)
+ key = (source, repr((args[2:], sorted(kwargs.items())))) if source is not None else None
+ if key is not None and key in results:
+ CONDITIONING_STATS["connector_reuses"] += 1
+ return results[key]
+ out = wrapped["inner"](*args, **kwargs)
+ CONDITIONING_STATS["connector_runs"] += 1
+ if key is not None and isinstance(out, tuple):
+ # A copy: stage one's denoise owns its tensors.
+ results[key] = tuple(t.clone() if isinstance(t, torch.Tensor) else t for t in out)
+ return out
+
+ runner.encode_prompt = encode_prompt
+ arm()
+ try:
+ yield
+ finally:
+ results.clear()
+ returned.clear()
+ if connectors.__dict__.get("forward") is forward:
+ if wrapped.get("own"):
+ connectors.forward = wrapped["prior"]
+ else:
+ del connectors.forward
+ if runner.__dict__.get("encode_prompt") is encode_prompt:
+ if had_encode:
+ runner.encode_prompt = encode
+ else:
+ del runner.encode_prompt
+
+
def _render(
pipe: Any,
kwargs: dict[str, Any],
@@ -1929,7 +2081,7 @@ def _render(
)
from .diffusion_multistage import encode_once, hold_denoiser_between_calls
- with encode_once(runner):
+ with encode_once(runner), reuse_connector_outputs(runner):
half_w, half_h = width // 2, height // 2
stage1 = dict(
base,
diff --git a/studio/backend/core/inference/video_ltx25_te_worker.py b/studio/backend/core/inference/video_ltx25_te_worker.py
index 978bf66cf14..552b7a37ed9 100644
--- a/studio/backend/core/inference/video_ltx25_te_worker.py
+++ b/studio/backend/core/inference/video_ltx25_te_worker.py
@@ -1087,6 +1087,11 @@ def _pack_hidden_states(model: Any, ids: Any, mask: Any) -> Any:
return torch.stack(out.hidden_states, dim = -1).flatten(2, 3).to(dtype = model.dtype)
+def compact_valid_rows(embeds: Any, mask: Any) -> Any:
+ """``[B, L, D]`` -> ``[valid rows, D]``, row-major over ``(B, L)`` as ``masked_scatter_`` refills it."""
+ return embeds[mask.to(device = embeds.device).bool()]
+
+
def _truncate_dead_tail(tokenizer: Any, model: Any) -> dict:
"""All hidden states are read, so only the unused 262k-vocab logits head goes."""
from core.inference.diffusion_te_truncate import truncate_text_encoders
@@ -1231,6 +1236,9 @@ def _run_encode(model: Any, config: dict, inputs: Any, device: str, stream: Any)
ids = inputs.input_ids.to(device)
mask = inputs.attention_mask.to(device)
embeds = _pack_hidden_states(model, ids, mask)
+ if config.get("_compact"):
+ # Valid rows only: the connectors zero padded rows before mixing tokens.
+ embeds = compact_valid_rows(embeds, mask)
embeds_host = embeds.to("cpu")
mask_host = mask.to("cpu")
del embeds
@@ -1295,6 +1303,7 @@ def run_text_encoder_worker(*, cmd_queue: Any, resp_queue: Any, config: dict) ->
continue
try:
config["_pin_host"] = bool(msg.get("pin_host"))
+ config["_compact"] = bool(msg.get("compact"))
embeds, mask, device = _encode_on(
tokenizer,
model,
@@ -1309,6 +1318,7 @@ def run_text_encoder_worker(*, cmd_queue: Any, resp_queue: Any, config: dict) ->
"id": msg.get("id"),
"embeds": embeds,
"mask": mask,
+ "compact": bool(config.get("_compact")),
"device": device,
"streamed": bool(config.get("_streamed")),
"prefetched_layers": int(
diff --git a/studio/backend/tests/test_video_ltx25_compact_conditioning.py b/studio/backend/tests/test_video_ltx25_compact_conditioning.py
new file mode 100644
index 00000000000..d0a9328ea53
--- /dev/null
+++ b/studio/backend/tests/test_video_ltx25_compact_conditioning.py
@@ -0,0 +1,300 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""LTX-2.5 compact conditioning: the worker sends only valid rows, the server rebuilds the connector inputs, and the
+two-stage render reuses the first call's connector outputs. Every arm is compared bit for bit with the full-width
+path on real (tiny, random) diffusers connectors."""
+
+from __future__ import annotations
+
+import queue
+from types import SimpleNamespace
+
+import pytest
+
+torch = pytest.importorskip("torch")
+connectors_mod = pytest.importorskip("diffusers.pipelines.ltx2.connectors")
+
+import inspect # noqa: E402
+
+if (
+ "per_modality_projections"
+ not in inspect.signature(connectors_mod.LTX2TextConnectors.__init__).parameters
+):
+ pytest.skip("diffusers predates the LTX-2.3 connectors", allow_module_level = True)
+
+from core.inference import diffusion_multistage as MS # noqa: E402
+from core.inference import video_ltx25 as v25 # noqa: E402
+from core.inference import video_ltx25_te_worker as worker # noqa: E402
+
+CAPTION, LAYERS = 8, 3
+
+
+def _connectors(per_modality: bool, registers: int = 16) -> object:
+ torch.manual_seed(0)
+ module = connectors_mod.LTX2TextConnectors(
+ caption_channels = CAPTION,
+ text_proj_in_factor = LAYERS,
+ video_connector_num_attention_heads = 2,
+ video_connector_attention_head_dim = 4,
+ video_connector_num_layers = 1,
+ video_connector_num_learnable_registers = registers,
+ audio_connector_num_attention_heads = 2,
+ audio_connector_attention_head_dim = 4,
+ audio_connector_num_layers = 1,
+ audio_connector_num_learnable_registers = registers,
+ per_modality_projections = per_modality,
+ # 2.3 / 2.5 project per modality to these widths; 2.0 projects to the caption width (also 2 x 4).
+ video_hidden_dim = 8,
+ audio_hidden_dim = 8,
+ )
+ return module.eval()
+
+
+def _packed(valid: list[int], seq_len: int, dtype) -> tuple:
+ """Left-padded packed hidden states whose padded rows are large garbage (what a real encoder leaves there)."""
+ g = torch.Generator().manual_seed(1)
+ embeds = torch.randn(len(valid), seq_len, CAPTION * LAYERS, generator = g) * 3
+ mask = torch.zeros(len(valid), seq_len, dtype = torch.long)
+ for b, n in enumerate(valid):
+ if n:
+ mask[b, seq_len - n :] = 1
+ embeds[b, : seq_len - n] = torch.randn(seq_len - n, CAPTION * LAYERS, generator = g) * 1e4
+ return embeds.to(dtype), mask
+
+
[email protected]("per_modality", [True, False])
[email protected]("dtype", [torch.float32, torch.bfloat16])
[email protected]("valid", [[2, 16], [1, 32], [32, 32], [7, 31]])
+def test_compact_rows_give_bit_identical_connector_outputs(per_modality, dtype, valid):
+ seq_len = 32
+ conn = _connectors(per_modality).to(dtype)
+ embeds, mask = _packed(valid, seq_len, dtype)
+ rows = worker.compact_valid_rows(embeds, mask)
+ assert rows.shape == (sum(valid), CAPTION * LAYERS)
+ rebuilt = v25.expand_valid_rows(rows, mask, device = "cpu", dtype = dtype)
+ assert rebuilt.shape == embeds.shape
+ assert torch.equal(rebuilt[mask.bool()], embeds[mask.bool()])
+ assert not rebuilt[~mask.bool()].any()
+ with torch.inference_mode():
+ old = conn(embeds, mask, padding_side = "left")
+ new = conn(rebuilt, mask, padding_side = "left")
+ for a, b in zip(old, new):
+ assert a.dtype == b.dtype and torch.equal(a, b)
+
+
+def test_shortening_the_sequence_would_not_be_equivalent():
+ """Why the rebuild keeps the full length: the registers fill the tail by absolute position."""
+ conn = _connectors(True)
+ embeds, mask = _packed([2], 32, torch.float32)
+ with torch.inference_mode():
+ full = conn(embeds, mask)[0]
+ short = conn(embeds[:, -16:], mask[:, -16:])[0]
+ assert not torch.equal(full[:, :16], short)
+
+
+class _Model:
+ """Stands in for the Gemma encoder: ``LAYERS`` hidden states of width ``CAPTION``."""
+
+ dtype = torch.float32
+
+ def __call__(self, input_ids, attention_mask, output_hidden_states, use_cache):
+ b, l = input_ids.shape
+ base = input_ids.float().unsqueeze(-1).expand(b, l, CAPTION)
+ return SimpleNamespace(hidden_states = tuple(base * (i + 1) for i in range(LAYERS)))
+
+
+def test_worker_encode_returns_only_valid_rows():
+ inputs = SimpleNamespace(
+ input_ids = torch.tensor([[0, 0, 5, 6], [1, 2, 3, 4]]),
+ attention_mask = torch.tensor([[0, 0, 1, 1], [1, 1, 1, 1]]),
+ )
+ full, mask_full = worker._run_encode(_Model(), {"resident": True}, inputs, "cpu", None)
+ rows, mask = worker._run_encode(
+ _Model(), {"resident": True, "_compact": True}, inputs, "cpu", None
+ )
+ assert full.shape == (2, 4, CAPTION * LAYERS) and rows.shape == (6, CAPTION * LAYERS)
+ assert torch.equal(mask, mask_full)
+ assert torch.equal(
+ v25.expand_valid_rows(rows, mask, device = "cpu", dtype = torch.float32)[mask.bool()],
+ full[mask.bool()],
+ )
+
+
+def _client(seq_len: int, valid) -> object:
+ client = v25.Ltx25TextEncoderClient(
+ repo = "r",
+ hf_token = None,
+ cache_dir = None,
+ local_files_only = True,
+ device = "cpu",
+ dtype = "bfloat16",
+ resident = False,
+ venv_dir = "/x",
+ )
+ client._cmd, client._resp = queue.Queue(), queue.Queue()
+ client._proc = SimpleNamespace(is_alive = lambda: True, exitcode = None)
+ client._ready = True
+ client.sent = []
+ embeds, mask = _packed(valid if isinstance(valid, list) else [valid], seq_len, torch.float32)
+
+ def fake_put(msg):
+ # The worker's reply, built with its own compaction helper.
+ client.sent.append(msg)
+ out = worker.compact_valid_rows(embeds, mask) if msg.get("compact") else embeds.clone()
+ client._resp.put({"type": "embeds", "id": msg["id"], "embeds": out, "mask": mask.clone()})
+
+ client._cmd.put = fake_put
+ return client
+
+
+def _stats() -> dict:
+ return dict(v25.CONDITIONING_STATS)
+
+
+def _delta(before: dict) -> dict:
+ return {k: v25.CONDITIONING_STATS[k] - before[k] for k in before}
+
+
+class _Pipe:
+ """The two calls diffusers' LTX-2 pipelines make per stage: ``encode_prompt`` then ``self.connectors``."""
+
+ def __init__(self, conn):
+ self._execution_device = torch.device("cpu")
+ self.transformer = SimpleNamespace(dtype = torch.float32)
+ self.connectors = conn
+
+ def __call__(
+ self,
+ guidance: bool = False,
+ prompt = "a fox",
+ videos: int = 1,
+ ):
+ pe, pm, ne, nm = self.encode_prompt(
+ prompt = prompt,
+ negative_prompt = "blurry" if isinstance(prompt, str) else ["blurry"] * len(prompt),
+ do_classifier_free_guidance = guidance,
+ num_videos_per_prompt = videos,
+ prompt_embeds = None,
+ negative_prompt_embeds = None,
+ prompt_attention_mask = None,
+ negative_prompt_attention_mask = None,
+ max_sequence_length = 32,
+ device = self._execution_device,
+ )
+ if guidance:
+ pe, pm = torch.cat([ne, pe]), torch.cat([nm, pm])
+ return self.connectors(pe, pm, padding_side = "left")
+
+
+def _two_stage(
+ monkeypatch,
+ compact: str,
+ guidance: bool = False,
+) -> tuple:
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, compact)
+ conn = _connectors(True)
+ pipe = _Pipe(conn)
+ client = _client(32, 5)
+ v25.install_remote_encode_prompt(pipe, client)
+ before = _stats()
+ with torch.inference_mode(), MS.encode_once(pipe), v25.reuse_connector_outputs(pipe):
+ first = pipe(guidance)
+ second = pipe(guidance)
+ # Everything it replaced is back.
+ assert "forward" not in conn.__dict__ and pipe.encode_prompt.__name__ == "encode_prompt"
+ return first, second, _delta(before), client
+
+
+def test_two_stage_reuses_connectors_and_matches_the_old_path(monkeypatch):
+ old_first, old_second, old_stats, old_client = _two_stage(monkeypatch, "0")
+ new_first, new_second, new_stats, new_client = _two_stage(monkeypatch, "1")
+ for ref, got in ((old_first, new_first), (old_second, new_second), (old_first, new_second)):
+ for a, b in zip(ref, got):
+ assert torch.equal(a, b)
+ # Kill switch: the old path exactly (full-width transport, a connector run per stage, no counters).
+ assert old_stats["full_encodes"] == 1 and old_stats["compact_encodes"] == 0
+ assert old_stats["connector_runs"] == 0 and old_stats["connector_reuses"] == 0
+ assert all(not m["compact"] for m in old_client.sent)
+ # New path engaged: one compact encode, one connector run, one reuse, ~valid/seq_len of the bytes.
+ assert new_stats["compact_encodes"] == 1 and new_stats["full_encodes"] == 0
+ assert new_stats["connector_runs"] == 1 and new_stats["connector_reuses"] == 1
+ assert new_stats["upload_bytes"] < old_stats["upload_bytes"] / 4
+ assert new_stats["host_bytes"] < old_stats["host_bytes"] / 4
+
+
+def test_cfg_concatenation_runs_the_connectors_each_time(monkeypatch):
+ old_first, _, _, _ = _two_stage(monkeypatch, "0", guidance = True)
+ first, second, stats, _ = _two_stage(monkeypatch, "1", guidance = True)
+ assert stats["connector_runs"] == 2 and stats["connector_reuses"] == 0
+ for a, b, c in zip(old_first, first, second):
+ assert torch.equal(a, b) and torch.equal(a, c)
+
+
+def test_reuse_restores_an_offload_hook_forward(monkeypatch):
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, "1")
+ conn = _connectors(True)
+ hooked = conn.forward
+ conn.forward = lambda *a, **k: hooked(*a, **k)
+ marker = conn.forward
+ pipe = _Pipe(conn)
+ pipe.encode_prompt = lambda **k: None
+ with v25.reuse_connector_outputs(pipe):
+ assert conn.forward is not marker
+ assert conn.forward is marker
+
+
+def test_kill_switch_makes_reuse_a_no_op(monkeypatch):
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, "0")
+ conn = _connectors(True)
+ pipe = _Pipe(conn)
+ encode = pipe.encode_prompt = lambda **k: None
+ with v25.reuse_connector_outputs(pipe):
+ assert pipe.encode_prompt is encode and "forward" not in conn.__dict__
+
+
+def test_cache_key_separates_the_transport_forms(monkeypatch):
+ client = _client(32, 5)
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, "1")
+ rows, _ = client.encode(["p"], 32, copy = False)
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, "0")
+ assert not client.cached(["p"], 32)
+ full, _ = client.encode(["p"], 32, copy = False)
+ assert rows.ndim == 2 and full.ndim == 3 and len(client.sent) == 2
+
+
+def test_reuse_survives_an_offload_reset_between_the_calls(monkeypatch):
+ """diffusers' end-of-call offload reset re-installs the hooks: a new ``forward`` on the module."""
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, "1")
+ conn = _connectors(True)
+ pipe = _Pipe(conn)
+ v25.install_remote_encode_prompt(pipe, _client(32, 5))
+ before = _stats()
+ with torch.inference_mode(), MS.encode_once(pipe), v25.reuse_connector_outputs(pipe):
+ first = pipe()
+ rearmed = conn.__dict__.pop("forward")
+ assert rearmed is not None
+ hook = lambda *a, **k: type(conn).forward(conn, *a, **k) # noqa: E731
+ conn.forward = hook
+ second = pipe()
+ delta = _delta(before)
+ assert delta["connector_runs"] == 1 and delta["connector_reuses"] == 1
+ assert all(torch.equal(a, b) for a, b in zip(first, second))
+ assert conn.forward is hook
+
+
[email protected]("guidance", [False, True])
+def test_batched_prompts_with_several_videos_keep_the_full_width(monkeypatch, guidance):
+ """diffusers repeats the embeds per prompt but the mask per batch there, so padded rows can reach the connectors:
+ the compact transport steps aside and the outputs stay those of the old path."""
+ outs, stats = [], []
+ for mode in ("0", "1"):
+ monkeypatch.setenv(v25.COMPACT_CONDITIONING_ENV, mode)
+ pipe = _Pipe(_connectors(True))
+ v25.install_remote_encode_prompt(pipe, _client(32, [3, 20]))
+ before = _stats()
+ with torch.inference_mode():
+ outs.append(pipe(guidance, prompt = ["a", "b"], videos = 2))
+ stats.append(_delta(before))
+ assert all(torch.equal(a, b) for a, b in zip(*outs))
+ assert stats[1]["full_encodes"] >= 1 and stats[1]["compact_encodes"] == 0