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 Images: run ESRGAN upscalers through the spandrel descriptor (#14876)

Daniel Han0d28035e2d52
diff --git a/studio/backend/core/inference/diffusion_upscalers.py b/studio/backend/core/inference/diffusion_upscalers.py
index 59ceba6fd68..02de135cf09 100644
--- a/studio/backend/core/inference/diffusion_upscalers.py
+++ b/studio/backend/core/inference/diffusion_upscalers.py
@@ -462,10 +462,10 @@ def upscale_with_model(
arr = torch.from_numpy(np.asarray(rgb, dtype = np.uint8).copy())
x = arr.permute(2, 0, 1).unsqueeze(0).to(device = dev, dtype = torch.float32) / 255.0
scale = int(descriptor.scale)
- model = descriptor.model
def _run(piece: Any) -> Any:
- return model(piece.to(dtype)).float()
+ # Not descriptor.model: the descriptor pads, unwraps tuple outputs (MMRealSR) and clamps.
+ return descriptor(piece.to(dtype)).float()
boxes = tile_boxes(x.shape[3], x.shape[2], tile, overlap)
try:
@@ -498,7 +498,7 @@ def upscale_with_model(
boxes = tile_boxes(x.shape[3], x.shape[2], tile, overlap)
finally:
if not keep_resident:
- del model, descriptor, cached
+ del descriptor, cached
_resident.pop(key, None)
tw, th = int(target[0]), int(target[1])
if (y.shape[3], y.shape[2]) != (tw, th):
diff --git a/studio/backend/tests/test_diffusion_hires.py b/studio/backend/tests/test_diffusion_hires.py
index 990ea96740d..56cc0dc9625 100644
--- a/studio/backend/tests/test_diffusion_hires.py
+++ b/studio/backend/tests/test_diffusion_hires.py
@@ -1143,6 +1143,43 @@ def test_stop_during_an_upscale_only_resize_returns_no_image(monkeypatch):
assert str(exc.value) == DIFFUSION_CANCELLED_MSG
+def test_a_drop_in_runs_through_the_spandrel_descriptor(tmp_path, monkeypatch):
+ torch = pytest.importorskip("torch")
+ pytest.importorskip("spandrel")
+ from PIL import Image
+ from spandrel import ModelLoader
+ from spandrel.architectures.MMRealSR import MMRealSR
+
+ torch.manual_seed(0)
+ net = MMRealSR(
+ num_in_ch = 3,
+ num_out_ch = 3,
+ scale = 4,
+ num_feat = 8,
+ num_block = 1,
+ num_grow_ch = 4,
+ num_feats = [8, 8, 8, 8],
+ num_blocks = [1, 1, 1, 1],
+ )
+ path = tmp_path / "tiny_mmrealsr.pth"
+ torch.save(net.state_dict(), path)
+ descriptor = ModelLoader(device = "cpu").load_from_file(str(path))
+ assert type(descriptor.architecture).__name__ == "MMRealSRArch"
+ probe = torch.rand(1, 3, 32, 32)
+ with torch.inference_mode():
+ assert isinstance(descriptor.model(probe), tuple)
+ want = descriptor(probe.clone())
+ monkeypatch.setattr(ups, "models_dir", lambda: tmp_path)
+ img = Image.fromarray((torch.rand(48, 40, 3) * 255).to(torch.uint8).numpy())
+ untiled = ups.upscale_with_model(img, "local:tiny_mmrealsr", (160, 192), device = "cpu", tile = 512)
+ tiled = ups.upscale_with_model(
+ img, "local:tiny_mmrealsr", (160, 192), device = "cpu", tile = 32, overlap = 8
+ )
+ assert untiled.model_scale == 4 and untiled.tiles == 1 and tiled.tiles > 1
+ assert untiled.image.size == tiled.image.size == (160, 192)
+ assert want.shape == (1, 3, 128, 128)
+
+
def test_a_resident_drop_in_reloads_when_its_file_is_replaced(tmp_path, monkeypatch):
import os
@@ -1190,9 +1227,14 @@ def _fake_model_upscaler(tmp_path, monkeypatch, fn):
monkeypatch.setattr(ups, "spandrel_available", lambda: True)
monkeypatch.setattr(ups, "resolve_model_path", lambda upscaler_id, cancel = None: path)
- monkeypatch.setattr(
- ups, "_load", lambda p, dev: (types.SimpleNamespace(scale = 2, model = fn), torch.float32)
- )
+
+ class _Descriptor:
+ scale = 2
+
+ def __call__(self, image):
+ return fn(image)
+
+ monkeypatch.setattr(ups, "_load", lambda p, dev: (_Descriptor(), torch.float32))
def test_model_upscaler_halves_the_tile_on_oom_like_comfyui(tmp_path, monkeypatch):