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: compile Anima's transformer blocks (#16873)

oobabooga9fdbddcff6e5
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index ad624f9b97a..b0949c62659 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -328,6 +328,10 @@ _LUMINA2_BF16_WHEN_RESIDENT = (
_HIDREAM_BF16_WHEN_RESIDENT = (
"INT8 is barely faster than compiled bf16 on a card that holds bf16 and changes image detail"
)
+# Not SMALL_DIT_BF16_WHEN_RESIDENT: that reason also compiles from render 1; Anima's compile stays deferred.
+ANIMA_BF16_WHEN_RESIDENT = (
+ "INT8 is unmeasured on Anima; it stays bf16 while it fits, as it did before its blocks compiled"
+)
# Small DiTs (<= ~6B) stay bf16 while it fits: quantising moves detail (L4 vs bf16 LPIPS: FP8 FLUX.2-klein-4B 0.065,
# Z-Image-Turbo 0.103; FLUX.3-klein INT8 0.027). INT8 still when bf16 would offload; such loads compile from render 1.
SMALL_DIT_BF16_WHEN_RESIDENT = (
@@ -1225,6 +1229,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (
transformer_class = "CosmosTransformer3DModel",
base_repo = "circlestone-labs/Anima-Base-v1.0-Diffusers",
aliases = ("anima-base", "anima-2.9b"),
+ profile = FamilyProfile(auto_bf16_when_resident = ANIMA_BF16_WHEN_RESIDENT),
gguf_loadable = False,
fp16_incompatible = True,
),
diff --git a/studio/backend/core/inference/diffusion_regional_compile.py b/studio/backend/core/inference/diffusion_regional_compile.py
index ce4bd903428..23a1f8baf21 100644
--- a/studio/backend/core/inference/diffusion_regional_compile.py
+++ b/studio/backend/core/inference/diffusion_regional_compile.py
@@ -9,6 +9,7 @@ from typing import Any, Optional
_VERIFIED_BLOCKS: dict[str, tuple[str, ...]] = {
"Lumina2Transformer2DModel": ("Lumina2TransformerBlock",),
+ "CosmosTransformer3DModel": ("CosmosTransformerBlock",),
"HiDreamImageTransformer2DModel": (
"HiDreamImageTransformerBlock",
"HiDreamImageSingleTransformerBlock",
diff --git a/studio/backend/tests/fixtures/diffusion_family_data_freeze.json b/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
index a15fa5fb19c..a27e7dea59c 100644
--- a/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
+++ b/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
@@ -1017,7 +1017,7 @@
"streamed_weight_only": [],
"zero_row": []
},
- "auto_bf16_when_resident": null,
+ "auto_bf16_when_resident": "INT8 is unmeasured on Anima; it stays bf16 while it fits, as it did before its blocks compiled",
"bf16": {
"convrot_filename": null,
"convrot_repo": null,
@@ -4443,6 +4443,7 @@
"wan2.2-ti2v-5b"
],
"family_auto_bf16_when_resident": {
+ "anima": "INT8 is unmeasured on Anima; it stays bf16 while it fits, as it did before its blocks compiled",
"flux.2-klein": "a small denoiser stays bf16 while it fits (INT8 / FP8 change image detail: FP8 LPIPS 0.065 on FLUX.2-klein-4B, 0.103 on Z-Image-Turbo)",
"flux.3-klein": "a small denoiser stays bf16 while it fits (INT8 / FP8 change image detail: FP8 LPIPS 0.065 on FLUX.2-klein-4B, 0.103 on Z-Image-Turbo)",
"flux.3-klein-base": "a small denoiser stays bf16 while it fits (INT8 / FP8 change image detail: FP8 LPIPS 0.065 on FLUX.2-klein-4B, 0.103 on Z-Image-Turbo)",
diff --git a/studio/backend/tests/test_diffusion_pipeline_prequant.py b/studio/backend/tests/test_diffusion_pipeline_prequant.py
index bce11404af5..4dd40fdac2f 100644
--- a/studio/backend/tests/test_diffusion_pipeline_prequant.py
+++ b/studio/backend/tests/test_diffusion_pipeline_prequant.py
@@ -20,7 +20,7 @@ from core.inference.diffusion import DiffusionBackend
from core.inference.diffusion_auto_policy import DenseQuantEstimate
from core.inference.diffusion_denoiser_prequant import PIPELINE_SEED_DECLINED
from core.inference.diffusion_device import DiffusionDeviceTarget
-from core.inference.diffusion_families import detect_family_for_pick
+from core.inference.diffusion_families import ANIMA_BF16_WHEN_RESIDENT, detect_family_for_pick
from core.inference.diffusion_memory import DeviceMemory
from models.inference import DiffusionDownloadPlanResponse
@@ -1906,6 +1906,8 @@ def _small_dit_kept_bf16(monkeypatch, reason):
(dmod.SMALL_DIT_BF16_WHEN_RESIDENT, None, "default", False),
(dmod.SMALL_DIT_BF16_WHEN_RESIDENT, "off", "off", False),
("measured: no faster", None, "off", True),
+ # Anima's reason keeps the deferred compile (background, from render 3).
+ (ANIMA_BF16_WHEN_RESIDENT, None, "off", True),
],
)
def test_a_small_dit_kept_bf16_compiles_from_render_1(
diff --git a/studio/backend/tests/test_diffusion_regional_compile.py b/studio/backend/tests/test_diffusion_regional_compile.py
index 8376f9e08af..96f4c1696f6 100644
--- a/studio/backend/tests/test_diffusion_regional_compile.py
+++ b/studio/backend/tests/test_diffusion_regional_compile.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Lumina-2 / HiDream-I1 declare no ``_repeated_blocks``; Studio supplies them so the denoiser compiles regionally."""
+"""Lumina-2 / HiDream-I1 / Cosmos (Anima) declare no ``_repeated_blocks``; Studio supplies them so the denoiser compiles regionally."""
import sys
import types
@@ -53,6 +53,35 @@ def _tiny_hidream():
).eval()
+def _tiny_cosmos():
+ """Anima's transformer config (concat padding mask, no extra positional embedding), shrunk."""
+ return diffusers.CosmosTransformer3DModel(
+ in_channels = 4,
+ out_channels = 4,
+ num_attention_heads = 2,
+ attention_head_dim = 16,
+ num_layers = 2,
+ text_embed_dim = 8,
+ adaln_lora_dim = 8,
+ max_size = (4, 16, 16),
+ patch_size = (1, 2, 2),
+ rope_scale = (1.0, 4.0, 4.0),
+ concat_padding_mask = True,
+ extra_pos_embed_type = None,
+ ).eval()
+
+
+def _cosmos_inputs():
+ g = torch.Generator().manual_seed(0)
+ return dict(
+ hidden_states = torch.randn(1, 4, 1, 8, 8, generator = g),
+ timestep = torch.tensor([0.5]),
+ encoder_hidden_states = torch.randn(1, 6, 8, generator = g),
+ padding_mask = torch.zeros(1, 1, 8, 8),
+ return_dict = False,
+ )
+
+
def _hidream_inputs(batch = 2):
g = torch.Generator().manual_seed(0)
return dict(
@@ -65,7 +94,7 @@ def _hidream_inputs(batch = 2):
)
[email protected]("build", [_tiny_lumina, _tiny_hidream])
[email protected]("build", [_tiny_lumina, _tiny_hidream, _tiny_cosmos])
def test_released_class_still_declares_no_repeated_blocks(build):
model = build()
declared = list(type(model)._repeated_blocks or [])
@@ -81,6 +110,7 @@ def test_released_class_still_declares_no_repeated_blocks(build):
[
(_tiny_lumina, ["Lumina2TransformerBlock"]),
(_tiny_hidream, ["HiDreamImageTransformerBlock", "HiDreamImageSingleTransformerBlock"]),
+ (_tiny_cosmos, ["CosmosTransformerBlock"]),
],
)
def test_supplied_blocks_make_compile_repeated_blocks_run(build, blocks):
@@ -120,6 +150,20 @@ def test_discovery_matches_the_verified_names():
assert rc.discover_repeated_blocks(_tiny_hidream()) == rc.verified_repeated_blocks(
"HiDreamImageTransformer2DModel"
)
+ assert rc.discover_repeated_blocks(_tiny_cosmos()) == rc.verified_repeated_blocks(
+ "CosmosTransformer3DModel"
+ )
+
+
+def test_the_compiled_cosmos_blocks_match_eager():
+ model = _tiny_cosmos()
+ with torch.no_grad():
+ eager = model(**_cosmos_inputs())[0]
+ rc.ensure_repeated_blocks(model)
+ model.compile_repeated_blocks(fullgraph = True, dynamic = True)
+ with torch.no_grad():
+ compiled = model(**_cosmos_inputs())[0]
+ assert torch.allclose(compiled, eager, atol = 1e-5, rtol = 1e-4)
@pytest.mark.parametrize("dtype, tol", [(torch.float32, 1e-6), (torch.bfloat16, 1e-2)])
@@ -229,3 +273,18 @@ def test_studio_compile_path_engages_on_lumina(monkeypatch):
m for m in pipe.transformer.modules() if type(m).__name__ == "Lumina2TransformerBlock"
]
assert blocks and all(getattr(m, "_compiled_call_impl", None) is not None for m in blocks)
+
+
+def test_anima_keeps_its_bf16_auto_default_once_it_compiles():
+ """Anima's own reason replaces the lifted "cannot be regionally compiled" one, not the small-DiT one."""
+ from core.inference.diffusion import _auto_keeps_bf16_reason
+ from core.inference.diffusion_families import (
+ ANIMA_BF16_WHEN_RESIDENT,
+ SMALL_DIT_BF16_WHEN_RESIDENT,
+ detect_family,
+ )
+
+ anima = detect_family("", override = "anima")
+ reason = _auto_keeps_bf16_reason(anima, "pipeline", anima.base_repo)
+ assert reason and ANIMA_BF16_WHEN_RESIDENT in reason
+ assert SMALL_DIT_BF16_WHEN_RESIDENT not in reason