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 video: reuse bounded conditioning image decodes (#16403)

Daniel Hanb58ea2335f49
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 45ee9e6d04a..617dd5ea2f0 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -1018,6 +1018,23 @@ def decode_b64_image(
max_pixels: Optional[int] = None,
) -> Any:
"""Decode a base64 or data: URL image; masks decode as single-channel L, source images as RGB."""
+ return decode_b64_image_sized(data, mode = mode, max_side = max_side, max_pixels = max_pixels)[0]
+
+
+def refuse_oversized_image(w: int, h: int, max_side: int, max_pixels: Optional[int]) -> None:
+ if w > max_side or h > max_side:
+ raise ValueError(f"Image is too large ({w}x{h}); maximum is {max_side}px per side.")
+ if max_pixels is not None and w * h > max_pixels:
+ raise ValueError(f"Image is too large ({w}x{h}); maximum is {max_pixels:,} source pixels.")
+
+
+def decode_b64_image_sized(
+ data: str,
+ *,
+ mode: str = "RGB",
+ max_side: int = 4096,
+ max_pixels: Optional[int] = None,
+) -> tuple[Any, tuple[int, int]]:
import base64
import binascii
import io
@@ -1035,12 +1052,7 @@ def decode_b64_image(
img = Image.open(io.BytesIO(blob))
# Reject from the header before img.load() so a huge-dimension file cannot spike memory
w, h = img.size
- if w > max_side or h > max_side:
- raise ValueError(f"Image is too large ({w}x{h}); maximum is {max_side}px per side.")
- if max_pixels is not None and w * h > max_pixels:
- raise ValueError(
- f"Image is too large ({w}x{h}); maximum is {max_pixels:,} source pixels."
- )
+ refuse_oversized_image(w, h, max_side, max_pixels)
img.load()
# Below the size guard because the transpose needs the pixels the guard exists to not read.
img = exif_upright(img)
@@ -1048,7 +1060,7 @@ def decode_b64_image(
raise # the size guard's own message; don't wrap it as a decode error
except Exception as exc: # noqa: BLE001 - surfaced as a 400 to the client
raise ValueError(f"Could not decode image: {exc}") from exc
- return flattened_rgb(img) if mode == "RGB" else img.convert(mode)
+ return (flattened_rgb(img) if mode == "RGB" else img.convert(mode)), (w, h)
def _snap_to_multiple(img: Any, multiple: int = 16) -> Any:
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index 61fbaf623e0..ae8893938c5 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -11800,6 +11800,7 @@ class VideoBackend:
audio_flow_shift: Optional[float] = None,
video_id: Optional[str] = None,
decoded_reference_audios: Optional[Any] = None,
+ decoded_reference_images: Optional[Any] = None,
expected_state: Optional[object] = None,
live_preview: Optional[bool] = None,
before_commit: Optional[Callable[[], None]] = None,
@@ -11828,8 +11829,19 @@ class VideoBackend:
resolved_reference_images = [input_reference]
else:
resolved_first_frame = input_reference
+ image_kwargs = (
+ {"decoded_images": decoded_reference_images.pool()}
+ if decoded_reference_images is not None
+ else {}
+ )
first_pil, last_pil, canvas_w, canvas_h, conditioning = self._resolve_keyframes(
- state.family, state.h3_task, resolved_first_frame, last_frame, width, height
+ state.family,
+ state.h3_task,
+ resolved_first_frame,
+ last_frame,
+ width,
+ height,
+ **image_kwargs,
)
references = self._resolve_references(
state.family,
@@ -11842,6 +11854,7 @@ class VideoBackend:
canvas_w,
canvas_h,
decoded_audios = decoded_reference_audios,
+ **image_kwargs,
)
shift, audio_shift = self._resolve_flow_shifts(
state.family, state.engine, flow_shift, audio_flow_shift
@@ -13064,6 +13077,8 @@ class VideoBackend:
last_frame: Optional[str],
width: Optional[int],
height: Optional[int],
+ *,
+ decoded_images: Optional[list] = None,
) -> tuple[Any, Any, int, int, str]:
"""Decode keyframes onto one shared canvas; omitting both dimensions matches the source aspect."""
validate_video_keyframe_conditioning(
@@ -13074,13 +13089,17 @@ class VideoBackend:
)
if fam.name == "ltx-2.5":
return VideoBackend._resolve_ltx25_first_frame(
- fam, first_frame, last_frame, width, height
+ fam, first_frame, last_frame, width, height, decoded_images = decoded_images
)
first = last = None
if first_frame or last_frame:
- from .diffusion import decode_b64_image
- first = decode_b64_image(first_frame, mode = "RGB") if first_frame else None
- last = decode_b64_image(last_frame, mode = "RGB") if last_frame else None
+ from .video_minimax_h3 import take_decoded_image
+
+ limits = dict(max_side = 4096, max_pixels = None)
+ first = (
+ take_decoded_image(decoded_images, first_frame, **limits) if first_frame else None
+ )
+ last = take_decoded_image(decoded_images, last_frame, **limits) if last_frame else None
# i2v pipelines centre-crop onto the nearest-aspect preset, as the reference workflow does.
i2v_pipeline = video_family_uses_i2v_pipeline(fam)
if first is not None:
@@ -13116,6 +13135,8 @@ class VideoBackend:
last_frame: Optional[str],
width: Optional[int],
height: Optional[int],
+ *,
+ decoded_images: Optional[list] = None,
) -> tuple[Any, Any, int, int, str]:
"""LTX-2.5 image-to-video: a first frame only, on a "match source" canvas when no size is given."""
from .video_ltx25 import ltx25_canvas_for_aspect, prepare_ltx25_first_frame
@@ -13126,8 +13147,8 @@ class VideoBackend:
)
first = None
if first_frame:
- from .diffusion import decode_b64_image
- first = decode_b64_image(first_frame, mode = "RGB")
+ from .video_minimax_h3 import take_decoded_image
+ first = take_decoded_image(decoded_images, first_frame, max_side = 4096, max_pixels = None)
if (width is None or height is None) and first is not None:
width, height = ltx25_canvas_for_aspect(*first.size)
else:
@@ -13167,6 +13188,7 @@ class VideoBackend:
height: int,
*,
decoded_audios: Optional[Any] = None,
+ decoded_images: Optional[list] = None,
) -> Any:
"""Sizing is settled here so both engines' min(1, ...) scales are no-ops; native refuses max."""
from .video_minimax_h3 import (
@@ -13190,13 +13212,13 @@ class VideoBackend:
return MiniMaxH3References()
policy = (reference_image_size or H3_REF_SIZE_MATCH).strip().lower()
- from .diffusion import decode_b64_image
+ from .video_minimax_h3 import take_decoded_image
fitted_images = tuple(
fit_h3_reference_image(
- decode_b64_image(
+ take_decoded_image(
+ decoded_images,
item,
- mode = "RGB",
max_side = H3_REF_IMAGE_SOURCE_MAX_SIDE,
max_pixels = H3_REF_IMAGE_SOURCE_MAX_PIXELS,
),
diff --git a/studio/backend/core/inference/video_minimax_h3.py b/studio/backend/core/inference/video_minimax_h3.py
index 87a81bf94c4..1872078fe2d 100644
--- a/studio/backend/core/inference/video_minimax_h3.py
+++ b/studio/backend/core/inference/video_minimax_h3.py
@@ -987,6 +987,29 @@ class DecodedReferenceAudios:
return self.decoded if tuple(audios) == self.sources else None
+@dataclass(frozen = True)
+class DecodedReferenceImages:
+ """One RGB image and its pre-EXIF dimensions per input slot."""
+
+ entries: tuple = ()
+
+ def pool(self) -> list:
+ return list(self.entries)
+
+
+def take_decoded_image(
+ pool: Optional[list], source: str, *, max_side: int, max_pixels: Optional[int]
+) -> Any:
+ from .diffusion import decode_b64_image, refuse_oversized_image
+ if pool:
+ for index, (known, size, image) in enumerate(pool):
+ if known is source or known == source:
+ del pool[index]
+ refuse_oversized_image(*size, max_side, max_pixels)
+ return image
+ return decode_b64_image(source, mode = "RGB", max_side = max_side, max_pixels = max_pixels)
+
+
@dataclass(frozen = True)
class MiniMaxH3StagedReferences:
"""``MiniMaxH3References`` written to disk the way sd-cli reads them back."""
diff --git a/studio/backend/routes/video.py b/studio/backend/routes/video.py
index a9ba657e2ed..d604d69681d 100644
--- a/studio/backend/routes/video.py
+++ b/studio/backend/routes/video.py
@@ -27,6 +27,7 @@ import time as _time
import uuid
import weakref
from dataclasses import asdict, dataclass
+from dataclasses import field as _dc_field
from datetime import datetime, timezone
from typing import Annotated, Any, Literal, Optional
@@ -1412,6 +1413,7 @@ _VIDEO_CREATE_FIELDS = (
_VIDEO_CREATE_LISTS = ("reference_images", "reference_audios")
# VideoJobCreateRequest's list limits, checked before multipart files are encoded.
_VIDEO_CREATE_LIST_MAX = {"reference_images": 9, "reference_audios": 3}
+_VIDEO_IMAGE_DECODE_CACHE_MAX_BYTES = 64 * 1024 * 1024
# The OpenAI SDK flattens a list of objects to key[][field]; each such part is one item.
_OBJECT_PART = {
"reference_images": _re.compile(r"reference_images(?:\[\d*\])?\[(image_url|file_id)\]"),
@@ -2084,6 +2086,7 @@ class _VideoCreateInputs:
size: Optional[tuple[int, int]]
seconds: Optional[float]
reference_audios: Optional[list[str]]
+ decoded_images: Optional[Any] = _dc_field(default = None, compare = False, repr = False)
async def _validated_video_inputs(
@@ -2125,20 +2128,21 @@ async def _validated_video_inputs(
raise _openai_video_error(exc.status, exc.detail, param = "reference_audios")
images = [("input_reference", reference), ("last_frame", body.last_frame)]
images += [("reference_images", item) for item in body.reference_images or []]
+ decoded_images = []
+ decoded_bytes = 0
for param, image in images:
if image is None:
continue
- # Check readability before a model switch. The conditioning path applies its own
- # keyframe or Ref2VA limit later, so this uses the broadest bounded source policy.
- from core.inference.diffusion import decode_b64_image
+ # Validate before switching; task-specific limits apply during resolution.
+ from core.inference.diffusion import decode_b64_image_sized
from core.inference.video_minimax_h3 import (
H3_REF_IMAGE_SOURCE_MAX_PIXELS,
H3_REF_IMAGE_SOURCE_MAX_SIDE,
)
try:
- await asyncio.to_thread(
- decode_b64_image,
+ decoded, stored_size = await asyncio.to_thread(
+ decode_b64_image_sized,
image,
mode = "RGB",
max_side = H3_REF_IMAGE_SOURCE_MAX_SIDE,
@@ -2146,7 +2150,21 @@ async def _validated_video_inputs(
)
except Exception: # noqa: BLE001 -- any decode failure is client input feedback
raise _openai_video_error(400, f"{param} is not a readable image.", param = param)
- return _VideoCreateInputs(size = size, seconds = seconds, reference_audios = reference_audios)
+ if decoded_images is not None:
+ decoded_bytes += 4 * decoded.width * decoded.height
+ if decoded_bytes <= _VIDEO_IMAGE_DECODE_CACHE_MAX_BYTES:
+ decoded_images.append((image, stored_size, decoded))
+ else:
+ decoded_images = None
+ del decoded
+ from core.inference.video_minimax_h3 import DecodedReferenceImages
+
+ return _VideoCreateInputs(
+ size = size,
+ seconds = seconds,
+ reference_audios = reference_audios,
+ decoded_images = DecodedReferenceImages(tuple(decoded_images)) if decoded_images else None,
+ )
def _new_video_id() -> str:
@@ -2277,6 +2295,8 @@ async def _start_openai_video(
from core.inference.media_model_index import expected_partition
from core.inference.video import _detect_load_family
+ # Release pixels before loading another model.
+ inputs.decoded_images = None
fam = _detect_load_family(pick.model_path, pick.gguf_filename, None)
if fam is None:
return
@@ -2363,6 +2383,8 @@ async def _start_openai_video(
generate_kwargs.update({k: v for k, v in controls.items() if v is not None})
if decoded_audios[0] is not None:
generate_kwargs["decoded_reference_audios"] = decoded_audios[0]
+ if inputs.decoded_images is not None:
+ generate_kwargs["decoded_reference_images"] = inputs.decoded_images
if expected_state is not None:
generate_kwargs["expected_state"] = expected_state
resolved = await asyncio.to_thread(
@@ -2653,6 +2675,7 @@ async def _create_or_queue_openai_video(
except Exception as exc:
if _video_wait(exc) != "busy":
raise
+ inputs.decoded_images = None
await asyncio.to_thread(_refuse_unreadable_audio, inputs.reference_audios)
model, size, seconds = await asyncio.to_thread(_queued_job_display, body, reference, inputs)
account = current_account()
diff --git a/studio/backend/tests/test_openai_videos_controls.py b/studio/backend/tests/test_openai_videos_controls.py
index 6c76bfd5623..7098f89d74e 100644
--- a/studio/backend/tests/test_openai_videos_controls.py
+++ b/studio/backend/tests/test_openai_videos_controls.py
@@ -129,7 +129,10 @@ def test_a_plain_openai_request_sends_the_old_kwargs_plus_a_seed(client, backend
)
for resp, kwargs in zip((json_resp, form_resp), calls, strict = True):
assert resp.status_code == 200, resp.json()
- assert set(kwargs) - {"seed"} == _PLAIN_KWARGS
+ # The readability check's decode rides along, so the render does not decode it again.
+ assert set(kwargs) - {"seed", "decoded_reference_images"} == _PLAIN_KWARGS
+ ((source, _, _),) = kwargs["decoded_reference_images"].entries
+ assert source is kwargs["input_reference"]
assert isinstance(kwargs["seed"], int) and 0 <= kwargs["seed"] < 2**53
job = resp.json()
assert set(job) - {"seed"} == _PLAIN_RESPONSE
diff --git a/studio/backend/tests/test_openai_videos_decode_once.py b/studio/backend/tests/test_openai_videos_decode_once.py
new file mode 100644
index 00000000000..f1505c7ef2f
--- /dev/null
+++ b/studio/backend/tests/test_openai_videos_decode_once.py
@@ -0,0 +1,318 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import asyncio
+import base64
+import gc
+import io
+import sys
+import weakref
+from pathlib import Path
+
+import pytest
+from fastapi import HTTPException
+from PIL import Image
+
+import routes.video as video_routes
+from core.inference import diffusion
+from core.inference.video import VideoBackend
+from core.inference.video_families import detect_video_family
+from core.inference.video_minimax_h3 import (
+ H3_REF_IMAGE_SOURCE_MAX_PIXELS,
+ H3_REF_IMAGE_SOURCE_MAX_SIDE,
+ DecodedReferenceImages,
+)
+
+_TESTS_DIR = Path(__file__).resolve().parent
+if str(_TESTS_DIR) not in sys.path:
+ sys.path.insert(0, str(_TESTS_DIR))
+
+from test_openai_videos_route import ( # noqa: E402,F401 -- fixtures are found by name
+ _create,
+ _healthy_diffusers,
+ _reference_image_bytes,
+ _wait_terminal,
+ backend,
+ client,
+)
+
+
+def _data_url(
+ size,
+ *,
+ fmt = "PNG",
+ mode = "RGB",
+ orientation = None,
+ color = (40, 90, 160),
+) -> str:
+ image = Image.new(mode, size, color if mode == "RGB" else color + (128,))
+ image.putpixel((0, 0), (255, 0, 0) if mode == "RGB" else (255, 0, 0, 255))
+ kwargs = {}
+ if orientation is not None:
+ exif = Image.Exif()
+ exif[0x0112] = orientation
+ kwargs["exif"] = exif.tobytes()
+ buf = io.BytesIO()
+ image.save(buf, format = fmt, **kwargs)
+ return f"data:image/{fmt.lower()};base64," + base64.b64encode(buf.getvalue()).decode()
+
+
+def _route_decodes(*sources) -> DecodedReferenceImages:
+ entries = []
+ for source in sources:
+ image, size = diffusion.decode_b64_image_sized(
+ source,
+ mode = "RGB",
+ max_side = H3_REF_IMAGE_SOURCE_MAX_SIDE,
+ max_pixels = H3_REF_IMAGE_SOURCE_MAX_PIXELS,
+ )
+ entries.append((source, size, image))
+ return DecodedReferenceImages(tuple(entries))
+
+
+def _count_opens(monkeypatch) -> list:
+ opened = []
+ real_open = Image.open
+
+ def _open(*args, **kwargs):
+ opened.append(1)
+ return real_open(*args, **kwargs)
+
+ monkeypatch.setattr(Image, "open", _open)
+ return opened
+
+
+def _pixels(image):
+ return None if image is None else (image.mode, image.size, image.tobytes())
+
+
+def test_cache_overflow_releases_every_image_and_stays_disabled(monkeypatch):
+ monkeypatch.setattr(video_routes, "_VIDEO_IMAGE_DECODE_CACHE_MAX_BYTES", 1025)
+ images = []
+ original = diffusion.decode_b64_image_sized
+
+ def tracked(*args, **kwargs):
+ image, size = original(*args, **kwargs)
+ images.append(weakref.ref(image))
+ return image, size
+
+ monkeypatch.setattr(diffusion, "decode_b64_image_sized", tracked)
+ body = video_routes.VideoJobCreateRequest(
+ prompt = "x",
+ reference_images = [_data_url((16, 16)), _data_url((16, 16)), _data_url((1, 1))],
+ )
+ inputs = asyncio.run(video_routes._validated_video_inputs(body, None))
+ assert inputs.decoded_images is None
+ assert len(images) == 3
+ gc.collect()
+ assert all(image() is None for image in images)
+
+
[email protected]("budget, retained", [(2048, 2), (2047, 0)])
+def test_cache_budget_counts_pillow_rgb_storage(monkeypatch, budget, retained):
+ monkeypatch.setattr(video_routes, "_VIDEO_IMAGE_DECODE_CACHE_MAX_BYTES", budget)
+ body = video_routes.VideoJobCreateRequest(
+ prompt = "x", reference_images = [_data_url((16, 16)), _data_url((16, 16))]
+ )
+ inputs = asyncio.run(video_routes._validated_video_inputs(body, None))
+ assert (len(inputs.decoded_images.entries) if inputs.decoded_images else 0) == retained
+
+
+def test_cache_overflow_still_validates_later_images(monkeypatch):
+ monkeypatch.setattr(video_routes, "_VIDEO_IMAGE_DECODE_CACHE_MAX_BYTES", 1)
+ body = video_routes.VideoJobCreateRequest(
+ prompt = "x", reference_images = [_data_url((16, 16)), "data:image/png;base64,aGVsbG8="]
+ )
+ with pytest.raises(HTTPException) as error:
+ asyncio.run(video_routes._validated_video_inputs(body, None))
+ assert error.value.status_code == 400
+ assert "reference_images is not a readable image." in str(error.value.detail)
+
+
[email protected]("orientation", [None, 3, 6, 8])
[email protected]("mode", ["RGB", "RGBA"])
+def test_reused_keyframes_match_a_fresh_decode_without_decoding(monkeypatch, orientation, mode):
+ h3 = detect_video_family("", override = "minimax-h3")
+ first = _data_url((320, 200), mode = mode, orientation = orientation)
+ last = _data_url((200, 320), mode = mode, orientation = orientation, color = (5, 6, 7))
+ fresh = VideoBackend._resolve_keyframes(h3, None, first, last, None, None)
+ decoded = _route_decodes(first, last)
+ opened = _count_opens(monkeypatch)
+ reused = VideoBackend._resolve_keyframes(
+ h3, None, first, last, None, None, decoded_images = decoded.pool()
+ )
+ assert opened == []
+ assert [_pixels(x) for x in reused[:2]] == [_pixels(x) for x in fresh[:2]]
+ assert reused[2:] == fresh[2:]
+
+
+def test_reused_references_match_a_fresh_decode_and_a_repeat_gets_its_own_image(monkeypatch):
+ h3 = detect_video_family("", override = "minimax-h3")
+ a = _data_url((640, 360), fmt = "JPEG")
+ b = _data_url((300, 500), mode = "RGBA", orientation = 6)
+ images = [a, b, a]
+ fresh = VideoBackend._resolve_references(
+ h3, "ref2va", "diffusers", images, None, None, "match", 960, 544
+ )
+ decoded = _route_decodes(*images)
+ opened = _count_opens(monkeypatch)
+ reused = VideoBackend._resolve_references(
+ h3,
+ "ref2va",
+ "diffusers",
+ images,
+ None,
+ None,
+ "match",
+ 960,
+ 544,
+ decoded_images = decoded.pool(),
+ )
+ assert opened == []
+ assert [_pixels(x) for x in reused.images] == [_pixels(x) for x in fresh.images]
+ assert decoded.entries[0][2] is not decoded.entries[2][2]
+
+
+def test_the_same_image_as_first_and_last_frame_stays_two_images():
+ h3 = detect_video_family("", override = "minimax-h3")
+ frame = _data_url((512, 512))
+ decoded = _route_decodes(frame, frame)
+ pool = decoded.pool()
+ VideoBackend._resolve_keyframes(h3, None, frame, frame, 512, 512, decoded_images = pool)
+ assert pool == []
+ assert decoded.entries[0][2] is not decoded.entries[1][2]
+
+
[email protected]("orientation", [None, 6])
+def test_a_reused_keyframe_over_the_keyframe_limit_is_refused_with_the_same_message(
+ monkeypatch, orientation
+):
+ h3 = detect_video_family("", override = "minimax-h3")
+ wide = _data_url((4100, 64), orientation = orientation)
+ with pytest.raises(ValueError) as fresh:
+ VideoBackend._resolve_keyframes(h3, None, wide, None, None, None)
+ decoded = _route_decodes(wide)
+ opened = _count_opens(monkeypatch)
+ with pytest.raises(ValueError) as reused:
+ VideoBackend._resolve_keyframes(
+ h3, None, wide, None, None, None, decoded_images = decoded.pool()
+ )
+ assert opened == []
+ assert (
+ str(reused.value)
+ == str(fresh.value)
+ == ("Image is too large (4100x64); maximum is 4096px per side.")
+ )
+
+
+def test_an_input_the_route_did_not_decode_is_decoded_as_before(monkeypatch):
+ h3 = detect_video_family("", override = "minimax-h3")
+ known = _data_url((256, 256))
+ other = _data_url((256, 256), color = (1, 2, 3))
+ opened = _count_opens(monkeypatch)
+ first, *_ = VideoBackend._resolve_keyframes(
+ h3, None, other, None, 256, 256, decoded_images = _route_decodes(known).pool()
+ )
+ assert len(opened) == 2
+ assert first.getpixel((10, 10)) == (1, 2, 3)
+
+
+def test_a_bad_image_after_a_good_one_keeps_the_route_error():
+ with pytest.raises(ValueError, match = "Could not decode image"):
+ diffusion.decode_b64_image_sized("aGVsbG8=")
+ with pytest.raises(ValueError, match = "maximum is 16px per side"):
+ diffusion.decode_b64_image_sized(_data_url((32, 8)), max_side = 16)
+ with pytest.raises(ValueError, match = "maximum is 100 source pixels"):
+ diffusion.decode_b64_image_sized(_data_url((20, 8)), max_pixels = 100)
+
+
+def test_the_route_hands_its_decodes_to_the_render(client, backend, monkeypatch):
+ calls = []
+ real = backend.begin_generate
+
+ def _capture(**kwargs):
+ calls.append(kwargs)
+ return real(**kwargs)
+
+ monkeypatch.setattr(backend, "begin_generate", _capture)
+ png = _reference_image_bytes("PNG")
+ resp = _create(client, {"prompt": "a fox"}, {"input_reference": ("f.png", png, "image/png")})
+ assert resp.status_code == 200, resp.json()
+ _wait_terminal(client, resp.json()["id"])
+ (kwargs,) = calls
+ ((source, size, image),) = kwargs["decoded_reference_images"].entries
+ assert source is kwargs["input_reference"]
+ assert _pixels(image) == _pixels(diffusion.decode_b64_image(source, mode = "RGB"))
+ assert size == Image.open(io.BytesIO(png)).size
+
+
+def test_a_queued_job_keeps_no_decoded_pixels(client, backend, monkeypatch):
+ from test_openai_videos_queue import _render_one
+
+ images = []
+ original_decode = diffusion.decode_b64_image_sized
+ original_display = video_routes._queued_job_display
+
+ def tracked(*args, **kwargs):
+ image, size = original_decode(*args, **kwargs)
+ images.append(weakref.ref(image))
+ return image, size
+
+ def display(*args, **kwargs):
+ gc.collect()
+ assert len(images) == 1
+ assert images[0]() is None
+ return original_display(*args, **kwargs)
+
+ monkeypatch.setattr(diffusion, "decode_b64_image_sized", tracked)
+ monkeypatch.setattr(video_routes, "_queued_job_display", display)
+ png = _reference_image_bytes("PNG")
+ _render_one(client, backend)
+ try:
+ queued = _create(
+ client, {"prompt": "next"}, {"input_reference": ("f.png", png, "image/png")}
+ )
+ assert queued.json()["status"] == "queued"
+ (entry,) = [e for e in video_routes._VIDEO_QUEUE if e.id == queued.json()["id"]]
+ assert entry.inputs.decoded_images is None
+ assert entry.reference is not None
+ finally:
+ backend.gate.set()
+
+
+def test_a_model_switch_drops_the_decodes_and_the_render_decodes_again(
+ client, backend, monkeypatch
+):
+ import types
+
+ from core.inference import media_auto_switch
+
+ async def _fake_switch(
+ model,
+ *,
+ before_switch = None,
+ **_kwargs,
+ ):
+ if before_switch is not None:
+ before_switch(types.SimpleNamespace(model_path = "unknown/model", gguf_filename = None))
+
+ monkeypatch.setattr(media_auto_switch, "maybe_auto_switch_media_model", _fake_switch)
+ calls = []
+ real = backend.begin_generate
+
+ def _capture(**kwargs):
+ calls.append(kwargs)
+ return real(**kwargs)
+
+ monkeypatch.setattr(backend, "begin_generate", _capture)
+ png = _reference_image_bytes("PNG")
+ resp = _create(
+ client, {"prompt": "a fox", "model": "x"}, {"input_reference": ("f.png", png, "image/png")}
+ )
+ assert resp.status_code == 200, resp.json()
+ _wait_terminal(client, resp.json()["id"])
+ (kwargs,) = calls
+ assert "decoded_reference_images" not in kwargs
+ assert kwargs["input_reference"] is not None