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: size FLUX.1 Kontext guard, preview and compile shape from its ~1 MP render (#15797)

Daniel Han7df6a08efd5c
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 5c738266870..1b8a9f9e87a 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -1129,9 +1129,21 @@ def _compile_shape_dims(
iw, ih = init_pil.size
if getattr(family, "layer_count", 0):
return _layered_canvas(family, (iw, ih))
+ if getattr(family, "pipeline_class", None) == "FluxKontextPipeline":
+ return _kontext_render_size((iw, ih))
return int(iw), int(ih)
+def _kontext_render_size(size: tuple[int, int]) -> tuple[int, int]:
+ """FluxKontextPipeline renders at max_area at the source's aspect whatever size it is given, as ComfyUI does."""
+ iw, ih = size
+ ratio = float(iw) / float(max(1, ih))
+ area = 1024 * 1024
+ width = round((area * ratio) ** 0.5) // 16 * 16
+ height = round((area / ratio) ** 0.5) // 16 * 16
+ return int(width), int(height)
+
+
def _layered_canvas(family: Any, size: tuple[int, int]) -> tuple[int, int]:
"""The (width, height) QwenImageLayeredPipeline decomposes at: the ``layer_resolution`` square's area at the input's
aspect ratio, each side rounded to 32 (``calculate_dimensions`` in the pipeline). The input image's own size only
diff --git a/studio/backend/tests/test_diffusion_kontext_size.py b/studio/backend/tests/test_diffusion_kontext_size.py
new file mode 100644
index 00000000000..b97f63acc9f
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_kontext_size.py
@@ -0,0 +1,76 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""FLUX.1 Kontext renders at ~1 MP whatever the source size; sizing must follow the render, not the upload."""
+
+import pytest
+from PIL import Image
+
+from core.inference.diffusion import _compile_shape_dims
+from core.inference.diffusion_families import detect_family
+
+
+class _Stop(Exception):
+ pass
+
+
+def _diffusers_kontext_size(width, height):
+ diffusers = pytest.importorskip("diffusers")
+ pipe_cls = getattr(diffusers, "FluxKontextPipeline", None)
+ if pipe_cls is None:
+ pytest.skip("diffusers has no FluxKontextPipeline")
+
+ class _Fake:
+ default_sample_size = 128
+ vae_scale_factor = 8
+
+ def check_inputs(self, prompt, prompt_2, height, width, *args, **kwargs):
+ raise _Stop(width, height)
+
+ with pytest.raises(_Stop) as stop:
+ pipe_cls.__call__(_Fake(), prompt = "p", height = height, width = width)
+ return stop.value.args
+
+
+_CASES = [
+ ((96, 64), (1248, 832)),
+ ((256, 256), (1024, 1024)),
+ ((1000, 500), (1440, 720)),
+ ((640, 1136), (768, 1360)),
+ ((2048, 1536), (1168, 880)),
+]
+
+
[email protected]("size,want", _CASES)
+def test_kontext_edit_dims_are_the_pipeline_render_size(size, want):
+ kontext = detect_family("black-forest-labs/FLUX.1-Kontext-dev")
+ assert _compile_shape_dims("edit", Image.new("RGB", size), 512, 512, kontext) == want
+
+
[email protected]("size,want", _CASES)
+def test_kontext_cases_match_installed_diffusers(size, want):
+ assert _diffusers_kontext_size(*size) == want
+
+
+def test_kontext_dims_unchanged_at_one_megapixel():
+ kontext = detect_family("black-forest-labs/FLUX.1-Kontext-dev")
+ src = Image.new("RGB", (1024, 1024))
+ assert _compile_shape_dims("edit", src, 512, 512, kontext) == (1024, 1024)
+
+
+def test_other_edit_families_keep_the_source_size():
+ qwen_edit = detect_family("Qwen/Qwen-Image-Edit")
+ src = Image.new("RGB", (96, 64))
+ assert _compile_shape_dims("edit", src, 1024, 1024, qwen_edit) == (96, 64)
+
+
+def test_kontext_live_preview_tiles_the_rendered_latents():
+ torch = pytest.importorskip("torch")
+ from core.inference.diffusion_preview import factors_for, latent_grid
+
+ spec = factors_for("flux.1-kontext")
+ assert spec is not None
+ kontext = detect_family("black-forest-labs/FLUX.1-Kontext-dev")
+ latents = torch.zeros(1, (832 // spec.down) * (1248 // spec.down), spec.channels)
+ w, h = _compile_shape_dims("edit", Image.new("RGB", (768, 512)), 512, 512, kontext)
+ assert latent_grid(latents, spec, h, w) is not None
diff --git a/studio/backend/tests/test_diffusion_qwen_layered.py b/studio/backend/tests/test_diffusion_qwen_layered.py
index 067759d018d..3d3b8a298de 100644
--- a/studio/backend/tests/test_diffusion_qwen_layered.py
+++ b/studio/backend/tests/test_diffusion_qwen_layered.py
@@ -15,6 +15,7 @@ from PIL import Image
from core.inference.diffusion import (
DiffusionBackend,
_compile_shape_dims,
+ _kontext_render_size,
_family_workflows,
_is_trusted_diffusion_repo,
)
@@ -235,7 +236,7 @@ def test_registered_shape_is_the_decomposition_canvas(size, expected):
# 640 x 640 area at the input aspect on the 32 px grid, as the pipeline sizes it
assert _compile_shape_dims("edit", src, 1024, 1024, fam) == expected
kontext = detect_family("black-forest-labs/FLUX.1-Kontext-dev")
- assert _compile_shape_dims("edit", src, 1024, 1024, kontext) == size
+ assert _compile_shape_dims("edit", src, 1024, 1024, kontext) == _kontext_render_size(size)
def _saved(root, class_name):
diff --git a/studio/backend/tests/test_diffusion_unified_edit.py b/studio/backend/tests/test_diffusion_unified_edit.py
index 9ad1875342d..6b4c11ffc0f 100644
--- a/studio/backend/tests/test_diffusion_unified_edit.py
+++ b/studio/backend/tests/test_diffusion_unified_edit.py
@@ -234,7 +234,7 @@ def test_unified_edit_registers_the_requested_shape_not_the_source(backend21):
src = Image.new("RGBA", (96, 64))
assert _compile_shape_dims("edit", src, 1024, 768, fam) == (1024, 768)
kontext = detect_family("black-forest-labs/FLUX.1-Kontext-dev")
- assert _compile_shape_dims("edit", src, 1024, 768, kontext) == (96, 64)
+ assert _compile_shape_dims("edit", src, 1024, 768, kontext) == (1248, 832)
@pytest.mark.parametrize("extras,ok", [(0, True), (3, True), (9, True), (10, False)])