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: MOSS-Image (in-tree port, quantised builds, GGUF through stable-diffusion.cpp) (#14552)

Daniel Han9778ef79f58c
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index b7c49e0188d..096e3374954 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -151,6 +151,7 @@ from .diffusion_hidream import (
hidream_te4_kwargs,
)
from .diffusion_krea2 import KREA2_FAMILY_NAME, load_krea2_pipeline
+from .diffusion_families import MOSS_IMAGE_FAMILY_NAME
from .diffusion_flux3 import (
FLUX3_COMPONENTS,
FLUX3_COMPONENT_FOLDERS,
@@ -1953,6 +1954,17 @@ def _generation_defaults_for(
return {"steps": int(steps), "guidance": float(guidance)}
+def _refuse_lora_without_loader(fam: Any, loras: Any) -> None:
+ """A family whose pipeline has no LoRA loader refuses an active selection before anything loads."""
+ from core.inference import diffusion_lora
+
+ name = getattr(fam, "name", None)
+ if _has_active_lora(loras) and not diffusion_lora.diffusers_lora_family_supported(name):
+ raise ValueError(
+ diffusion_lora.diffusers_lora_family_message(getattr(fam, "label", None) or name)
+ )
+
+
_WHOLE_PIPELINE_GGUF_LORA_MSG = (
"LoRA is not available on a whole-pipeline GGUF: its UNet runs as the stored GGUF weights, which cannot carry "
"adapters. Load the .safetensors checkpoint to use a LoRA, or clear the LoRA selection."
@@ -2040,6 +2052,14 @@ def _install_gguf_dim_restore(logger: Any) -> None:
logger.warning("diffusion.gguf: dim-restore shim not installed: %s", exc)
+def _family_model_class(diffusers: Any, name: str) -> Any:
+ """``getattr(diffusers, name)``, or Studio's in-tree class of that name (MOSS-Image), imported on first use."""
+ if name in ("MOSSImagePipeline", "MOSSImageTransformer", "MOSSImageScheduler"):
+ from .diffusion_moss_image import in_tree_class
+ return in_tree_class(name)
+ return getattr(diffusers, name)
+
+
@functools.lru_cache(maxsize = None)
def _no_recast_pipeline_class(pipe_cls: Any) -> Any:
"""``pipe_cls`` with the dtype half of ``.to()`` dropped; device moves still work. Pipelines
@@ -4257,7 +4277,12 @@ class DiffusionBackend:
"""Header-only, so a bad pick is a 400 before eviction. Single-file / GGUF denoisers only."""
from .diffusion_comfy_components import validate_component_specs
- if kind not in ("gguf", "single_file") or fam.single_file_is_pipeline or fam.pipeline_only:
+ if (
+ kind not in ("gguf", "single_file")
+ or fam.single_file_is_pipeline
+ or fam.pipeline_only
+ or getattr(fam, "native_transformer_files_only", False)
+ ):
raise ValueError(
"text_encoder_file / vae_file load beside a single-file or GGUF transformer; a full pipeline "
"(or a whole-pipeline single file) already carries its own text encoders and VAE."
@@ -4270,6 +4295,11 @@ class DiffusionBackend:
f"separate text encoder / VAE files are not supported for '{fam.name}' yet; omit "
"text_encoder_files and vae_file (the base repo's encoder and VAE are used)."
)
+ if fam.name == MOSS_IMAGE_FAMILY_NAME and (vae_file or text_encoder_files):
+ raise ValueError(
+ f"separate text encoder / VAE files are not supported for '{fam.name}' yet; omit "
+ "text_encoder_files and vae_file (the base repo's encoder and VAE are used)."
+ )
if vae_file and fam.name == KREA2_FAMILY_NAME:
raise ValueError(
f"a separate VAE file is not supported for '{fam.name}' yet; omit vae_file."
@@ -4365,6 +4395,7 @@ class DiffusionBackend:
or not _is_local_path(repo_id)
or getattr(fam, "single_file_is_pipeline", False)
or getattr(fam, "pipeline_only", False)
+ or getattr(fam, "native_transformer_files_only", False)
or os.environ.get("UNSLOTH_DIFFUSION_COMFY_TEXT_ENCODERS", "").strip().lower()
in ("0", "false", "off", "no")
):
@@ -4511,6 +4542,20 @@ class DiffusionBackend:
# same predicate. Imported here, not at module import, because the router imports this module's siblings.
from .diffusion_engine_router import family_buildable_here
+ # MOSS-Image GGUFs are sd.cpp only: refuse before eviction and before the diffusers class check.
+ if getattr(fam, "native_transformer_files_only", False) and (
+ kind == "single_file"
+ or (
+ kind == "gguf"
+ and not family_buildable_here(fam, model_kind = kind, gguf_filename = gguf_filename)
+ )
+ ):
+ raise ValueError(
+ f"'{fam.name}' runs a {'GGUF' if kind == 'gguf' else 'single-file'} transformer only on the native "
+ f"stable-diffusion.cpp engine, from a build that supports it (upstream master-975 or newer); the "
+ f"diffusers engine loads the full pipeline repo. Select the pipeline repo, or a GGUF with the native "
+ f"engine available."
+ )
if not family_buildable_here(fam, model_kind = kind, gguf_filename = gguf_filename):
assert_pipeline_class_available(fam.pipeline_class, fam.name)
# Whole-pipeline families take a GGUF only when it is whole too (stable-diffusion.cpp ``convert``).
@@ -4733,6 +4778,7 @@ class DiffusionBackend:
and resolve_model_kind(gguf_filename, model_kind) == "gguf"
):
raise ValueError(_WHOLE_PIPELINE_GGUF_LORA_MSG)
+ _refuse_lora_without_loader(fam, loras)
if auto_ordinal:
# Automatic, after validation: one GPU, Apple, CPU and ROCm skip every probe here.
gpu_ordinal = auto_cuda_ordinal_for_load()
@@ -7481,6 +7527,7 @@ class DiffusionBackend:
family_override = family_override,
model_kind = model_kind,
)
+ _refuse_lora_without_loader(fam, loras)
kind = resolve_model_kind(gguf_filename, model_kind)
# Validate every mode string that can raise BEFORE this load evicts the previous pipeline.
normalize_transformer_quant(transformer_quant)
@@ -7650,8 +7697,8 @@ class DiffusionBackend:
comfy_scan = refuse_comfy_quant(single_file_path) if kind == "single_file" else None
# torchao weights from a ComfyUI file: compile like Studio's own quantized transformer
comfy_compile = False
- transformer_cls = getattr(diffusers, fam.transformer_class)
- pipeline_cls = getattr(diffusers, fam.pipeline_class)
+ transformer_cls = _family_model_class(diffusers, fam.transformer_class)
+ pipeline_cls = _family_model_class(diffusers, fam.pipeline_class)
# Decide placement up front (weights still on CPU). Budgets the GGUF; dense is preflighted below.
plan = self._plan_memory(
@@ -8824,6 +8871,22 @@ class DiffusionBackend:
)
finally:
self._stop_load_prefetch()
+ elif fam.name == MOSS_IMAGE_FAMILY_NAME:
+ from .diffusion_moss_image import load_moss_image_pipeline
+ try:
+ pipe = load_moss_image_pipeline(
+ _base_local_dir or fetch_base,
+ dtype,
+ hf_token = hf_token,
+ check_cancelled = lambda: self._raise_if_load_cancelled(
+ _load_token
+ ),
+ local_files_only = local_files_only,
+ text_encoder = pipe_kwargs.get("text_encoder"),
+ transformer = pipe_kwargs.get("transformer"),
+ )
+ finally:
+ self._stop_load_prefetch()
elif fam.name == KREA2_FAMILY_NAME:
# krea's transformers-5.x configs break the 4.x line: assemble per component from
# pipe_kwargs (pre-cast TE, seeded denoiser or None = dense shards).
@@ -10756,6 +10819,36 @@ class DiffusionBackend:
check_cancelled()
pipe.to(device)
return pipe
+ if getattr(fam, "name", None) == MOSS_IMAGE_FAMILY_NAME:
+ from .diffusion_moss_image import load_moss_image_pipeline
+
+ moss_te = None
+ if target is not None:
+ moss_te = te_prequant_pipe_kwargs(
+ fam,
+ base,
+ te_quant_mode = te_quant_mode,
+ target = target,
+ dtype = dtype,
+ hf_token = hf_token,
+ logger = logger,
+ local_files_only = local_files_only,
+ dense_source = base_local_dir or (base if local_files_only else None),
+ config_source = base_local_dir,
+ ).get("text_encoder")
+ check_cancelled()
+ pipe = load_moss_image_pipeline(
+ base_local_dir or base,
+ dtype,
+ hf_token = hf_token,
+ transformer = transformer,
+ text_encoder = moss_te,
+ check_cancelled = check_cancelled,
+ local_files_only = local_files_only,
+ )
+ check_cancelled()
+ pipe.to(device)
+ return pipe
if getattr(fam, "name", None) == KREA2_FAMILY_NAME:
# krea ships transformers-5.x configs and no top-level tokenizer files, so assemble per-component.
krea_te = None
@@ -12036,6 +12129,8 @@ class DiffusionBackend:
current = getattr(pipe, "_unsloth_loras", ())
specs = [(i, w) for (i, w) in (loras or []) if w != 0]
+ if specs:
+ _refuse_lora_without_loader(getattr(state, "family", None), specs)
quant_baked = bool(getattr(pipe, "_unsloth_loras_baked", False))
quant = (state.transformer_quant or "").lower()
if quant in ("int8", "fp8", "nvfp4", "mxfp8"):
@@ -14247,7 +14342,7 @@ def _explicit_pipeline_components(fam: Any) -> Optional[tuple[str, ...]]:
name = getattr(fam, "name", None)
if is_flux3_family(fam):
return FLUX3_COMPONENTS
- if name == KREA2_FAMILY_NAME:
+ if name in (KREA2_FAMILY_NAME, MOSS_IMAGE_FAMILY_NAME):
return ("scheduler", "text_encoder", "tokenizer", "transformer", "vae")
if name == IDEOGRAM4_FAMILY_NAME:
return (
diff --git a/studio/backend/core/inference/diffusion_auto_policy.py b/studio/backend/core/inference/diffusion_auto_policy.py
index c0ac64219bd..078024c96d0 100644
--- a/studio/backend/core/inference/diffusion_auto_policy.py
+++ b/studio/backend/core/inference/diffusion_auto_policy.py
@@ -84,6 +84,8 @@ _FAMILY_BF16_GB: dict[str, tuple[float, float, float]] = {
"qwen-image-2.1": (14.2, 17.5, 1.4),
"z-image": (12.3, 8.0, 0.2),
"krea-2": (26.3, 8.9, 0.5),
+ # MOSS-Image: 5.08B DiT + headless Qwen3.5-9B + fp32 FLUX.2 VAE (MOSS-Image-SFT index metadata).
+ "moss-image": (10.2, 15.9, 0.3),
# Ships fp32 (10.4 + 10.5 + 0.3 GB of shards); bf16-resident is half.
"lumina-2": (5.2, 5.2, 0.2),
# 17B dual-stream DiT (32.5 GB bf16 on disk) + Qwen2.5-VL 15.5 GB + ByT5 0.8 GB.
diff --git a/studio/backend/core/inference/diffusion_autoopt/patches.py b/studio/backend/core/inference/diffusion_autoopt/patches.py
index 220ac13bf9e..6a26b2ecc27 100644
--- a/studio/backend/core/inference/diffusion_autoopt/patches.py
+++ b/studio/backend/core/inference/diffusion_autoopt/patches.py
@@ -461,6 +461,16 @@ def _builtin_specs() -> list[PatchSpec]:
from ..diffusion_qwenimage21_fused import uninstall
uninstall()
+ def moss_fused(ctx: PatchContext) -> Any:
+ from ..diffusion_moss_fused import install
+ return install(
+ ctx.transformer,
+ ctx.logger,
+ offload_active = ctx.offload_active
+ if ctx.denoiser_offloaded is None
+ else bool(ctx.denoiser_offloaded),
+ )
+
return [
# Shared compile-safe fusions (RMSNorm / AdaLN) and the per-arch addcmul fusions: speed tiers only.
PatchSpec(
@@ -559,4 +569,14 @@ def _builtin_specs() -> list[PatchSpec]:
warn = "qwen-image-2.1 convrot sharing",
env = "UNSLOTH_DIFFUSION_Q21_CONVROT_FUSED",
),
+ PatchSpec(
+ "moss_fused",
+ STAGE_PRE_COMPILE,
+ moss_fused,
+ None,
+ group = GROUP_FUSED_DIT,
+ classes = ("MOSSImageTransformer",),
+ warn = "moss-image fused projections",
+ env = "UNSLOTH_DIFFUSION_MOSS_FUSED",
+ ),
]
diff --git a/studio/backend/core/inference/diffusion_denoiser_prequant.py b/studio/backend/core/inference/diffusion_denoiser_prequant.py
index 4462e6f343b..880a1bd343c 100644
--- a/studio/backend/core/inference/diffusion_denoiser_prequant.py
+++ b/studio/backend/core/inference/diffusion_denoiser_prequant.py
@@ -136,7 +136,11 @@ def denoiser_prequant_pipe_kwargs(
from .diffusion_prequant import load_prequantized_transformer
from .diffusion_transformer_quant import DEFAULT_MIN_LINEAR_FEATURES
- transformer_cls = getattr(diffusers, str(getattr(fam, "transformer_class", "")), None)
+ class_name = str(getattr(fam, "transformer_class", ""))
+ transformer_cls = getattr(diffusers, class_name, None)
+ if transformer_cls is None:
+ from .diffusion_moss_image import in_tree_class
+ transformer_cls = in_tree_class(class_name)
if transformer_cls is None:
_warn(
logger,
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.py
index 0ff5c0c0e3e..c697cee6951 100644
--- a/studio/backend/core/inference/diffusion_dynamic_text.py
+++ b/studio/backend/core/inference/diffusion_dynamic_text.py
@@ -89,6 +89,12 @@ _ZIMAGE_SOURCES: tuple[str, ...] = (
r".*\['args'\]\[[0-2]\]$",
)
+# MOSS-Image: unpadded text (cond and uncond differ), so every prompt is a new length.
+_MOSS_IMAGE_SOURCES: tuple[str, ...] = (
+ "L['x']",
+ "L['rope'][0]",
+ "L['rope'][1]",
+)
# Prompt-length policy for every denoiser class Studio loads (the ONE list; test_diffusion_prompt_length_policy checks
# it covers diffusion_families and video_families). A tuple: the denoiser's text length follows the prompt, and these
# are the repeated block's dynamo sources that carry it. FIXED_LENGTH: the pipeline pads every prompt to one length
@@ -108,6 +114,7 @@ PROMPT_LENGTH_POLICY: dict[str, Any] = {
"Ideogram4Transformer2DModel": FIXED_LENGTH, # token ids left-padded into max_sequence_length 2048
"UNet2DConditionModel": FIXED_LENGTH, # CLIP padded to 77
"MiniMaxH3Transformer3DModel": _MINIMAX_H3_SOURCES,
+ "MOSSImageTransformer": _MOSS_IMAGE_SOURCES,
"LTX2VideoTransformer3DModel": FIXED_LENGTH, # Gemma left-padded to max_sequence_length 1024
"WanTransformer3DModel": FIXED_LENGTH, # UMT5 padded to max_sequence_length 512
"HunyuanVideo15Transformer3DModel": FIXED_LENGTH, # padded to the tokenizer max length
@@ -132,6 +139,7 @@ _FAMILY_SOURCES: dict[str, tuple[str, ...]] = {
"QwenImage21Transformer2DModel": _QWEN_IMAGE_21_SOURCES,
"QwenImageTransformer2DModel": _QWEN_IMAGE_SOURCES,
"MiniMaxH3Transformer3DModel": _MINIMAX_H3_SOURCES,
+ "MOSSImageTransformer": _MOSS_IMAGE_SOURCES,
}
_ARMED_WHEN_DYNAMIC: frozenset[str] = frozenset({"QwenImageTransformer2DModel"})
diff --git a/studio/backend/core/inference/diffusion_engine_router.py b/studio/backend/core/inference/diffusion_engine_router.py
index 6d47af8357b..77eae789ce7 100644
--- a/studio/backend/core/inference/diffusion_engine_router.py
+++ b/studio/backend/core/inference/diffusion_engine_router.py
@@ -750,6 +750,8 @@ def select_and_activate_engine(
prefer_native = forced == ENGINE_SD_CPP
if sd_cpp_pref in _DISABLE_TOKENS and not prefer_native:
return _activate(ENGINE_DIFFUSERS, "native engine disabled (UNSLOTH_DIFFUSION_SD_CPP=0)")
+ if native_gguf_only(fam, model_kind):
+ prefer_native = True
target = resolve_diffusion_device_target()
backend = target.backend
@@ -873,14 +875,19 @@ def select_and_activate_engine(
if off_torch is not None:
reason = f"{reason}; UNSLOTH_DIFFUSION_SD_CPP_DEVICE={off_torch.label} not honoured"
logger.warning("image load stays on torch's device: %s", reason)
- if requested == ENGINE_SD_CPP:
- # The load asked for native: say why it cannot run, rather than load it on diffusers behind its back.
+ if requested == ENGINE_SD_CPP or native_gguf_only(fam, model_kind):
+ # Refuse before anything unloads rather than silently fall back to diffusers.
raise ValueError(f"The native sd.cpp engine cannot load this model here: {reason}.")
if before_fallback is not None:
before_fallback()
return _activate(ENGINE_DIFFUSERS, reason)
+def native_gguf_only(fam: Optional[DiffusionFamily], model_kind: Optional[str]) -> bool:
+ """A GGUF of a family whose transformer-only files load natively only (``native_transformer_files_only``)."""
+ return model_kind == "gguf" and bool(getattr(fam, "native_transformer_files_only", False))
+
+
def refuse_engine_pick(
fam: Optional[DiffusionFamily], *, engine: Optional[str], model_kind: Optional[str]
) -> None:
@@ -901,6 +908,10 @@ def refuse_engine_pick(
f"The native sd.cpp engine has no support for the {fam.name} model family."
)
elif requested == ENGINE_DIFFUSERS:
+ if native_gguf_only(fam, model_kind):
+ raise ValueError(
+ f"Diffusers cannot load a {fam.name} GGUF; use the Auto or native engine, or the pipeline repo."
+ )
if fam is not None and not family_pipeline_available(fam):
raise ValueError(
f"Diffusers cannot build the {fam.name} model family with the installed version; "
@@ -1000,6 +1011,8 @@ def predict_engine(
prefer_native = forced == ENGINE_SD_CPP
if sd_cpp_pref in _DISABLE_TOKENS and not prefer_native:
return ENGINE_DIFFUSERS
+ if native_gguf_only(fam, model_kind):
+ prefer_native = True
backend = resolve_diffusion_device_target().backend
if off_torch_sd_cpp_device(backend) is not None:
@@ -1024,11 +1037,16 @@ def predict_engine(
choice = select_diffusion_engine(
backend, native_available = native_available, prefer_native = prefer_native
)
- if requested == ENGINE_SD_CPP and choice != ENGINE_SD_CPP:
+ if (
+ requested == ENGINE_SD_CPP or native_gguf_only(fam, model_kind)
+ ) and choice != ENGINE_SD_CPP:
# Selection raises the same refusal; planning diffusers here would stage GB for a load that cannot run.
- raise ValueError(
- "The native sd.cpp engine cannot load this model here: native sd.cpp binary unavailable."
+ why = (
+ "native sd.cpp binary unavailable"
+ if requested == ENGINE_SD_CPP
+ else f"no usable sd.cpp build here supports '{fam.name}'"
)
+ raise ValueError(f"The native sd.cpp engine cannot load this model here: {why}.")
return choice
@@ -1056,6 +1074,11 @@ def family_buildable_here(
``predict_engine`` activates nothing and installs nothing."""
if fam is None:
return False
+ if native_gguf_only(fam, model_kind):
+ try:
+ return predict_engine(fam, model_kind = "gguf") == ENGINE_SD_CPP
+ except Exception: # noqa: BLE001 -- refused or unprobeable: not buildable here
+ return False
if family_pipeline_available(fam):
return True
if model_kind == "gguf" and gguf_filename and family_mlx_supported(fam):
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index 04172a78399..e4d0ad5f106 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -101,6 +101,8 @@ class DiffusionFamily:
# True for families needing MULTIPLE denoisers no single file carries (Ideogram 4), so only a full pipeline load
# is valid.
pipeline_only: bool = False
+ # Transformer-only files (GGUF) load only via sd.cpp; diffusers has no single-file loader (MOSS-Image).
+ native_transformer_files_only: bool = False
# Optional diffusers pipeline classes for image-conditioned workflows, built via ``Pipeline.from_pipe`` (no
# reload). None = unsupported.
img2img_pipeline_class: Optional[str] = None
@@ -211,6 +213,8 @@ class DiffusionFamily:
# VAE latent-format override for sd-cli (--vae-format): "flux2" for FLUX.2, None otherwise
sd_cpp_vae_format: Optional[str] = None
sd_cpp_text_encoders: tuple[tuple[str, str, str], ...] = field(default_factory = tuple)
+ # (repo_id, filename) of a tokenizer.json for sd-cli --tokenizer (MOSS-Image requires it).
+ sd_cpp_tokenizer: Optional[tuple[str, str]] = None
# mflux ModelConfig name (None = no MLX); ``_by_base`` maps a lowercase base repo to another config.
mlx_model_config: Optional[str] = None
mlx_model_config_by_base: tuple[tuple[str, str], ...] = field(default_factory = tuple)
@@ -386,6 +390,18 @@ _KREA2_SD_CPP_ARCH_MARKER = "Krea2"
# Keyed by architecture, not per variant: the base repo is read from the HF base_model tag at load time, so one entry
# covers Turbo/full, schnell/dev.
+MOSS_IMAGE_FAMILY_NAME = "moss-image"
+# MOSS-Image keep-list: text_embedder, timestep MLP, AdaLN stay bf16 (LPIPS 0.058 vs 0.077 generic).
+_MOSS_IMAGE_QUANT_KEEP: tuple[str, ...] = ("text_embedder", "time_embedding", "modulation")
+_MOSS_IMAGE_CONVROT_SUFFIXES = (
+ "proj.q",
+ "proj.k",
+ "proj.v",
+ "proj.o",
+ "proj.ffn_up_gate",
+ "proj.ffn_down",
+)
+
_FAMILIES: tuple[DiffusionFamily, ...] = (
DiffusionFamily(
name = "flux.1",
@@ -1299,6 +1315,69 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (
# Two DiTs assembled per-component, so no transformer-only single-file / GGUF load.
pipeline_only = True,
),
+ # MOSS-Image: remote-code repos, so the pipeline is the in-tree port in diffusion_moss_image; encoder needs transformers >= 5.3.
+ DiffusionFamily(
+ name = MOSS_IMAGE_FAMILY_NAME,
+ label = "MOSS-Image",
+ # ComfyUI's MOSSImage is ModelType.FLOW (ModelSamplingDiscreteFlow, shift 5), the node ModelSamplingSD3 sets.
+ comfy_shift_node = "sd3",
+ profile = FamilyProfile(
+ quant_exclude_tokens = _MOSS_IMAGE_QUANT_KEEP,
+ # Required, not optional: plain int8 with the same keep-list scored 0.111 against ConvRot's 0.058 (SFT).
+ int8_convrot = ConvRotSpec(
+ group = 256,
+ suffixes = _MOSS_IMAGE_CONVROT_SUFFIXES,
+ filename = "MOSS-Image-SFT-INT8-ConvRot.safetensors",
+ repo = "unsloth/MOSS-Image-SFT-FP8",
+ variant_repos = ("unsloth/MOSS-Image-Base-FP8",),
+ default_on = True,
+ ),
+ quant_names = ("moss-image-base",),
+ # fp8 scored LPIPS 0.163 (int8 ConvRot 0.058): explicit picks only.
+ auto_scheme_skip = frozenset({"fp8", "mxfp8"}),
+ # NVFP4 scored LPIPS 0.383 vs bf16 (INT8-ConvRot 0.058); as FLUX.3, a passing gate record lifts it.
+ scheme_deny = frozenset({"nvfp4"}),
+ # Small-DiT rule: bf16 while it fits, INT8-ConvRot (LPIPS 0.058) when bf16 would offload.
+ auto_bf16_when_resident = SMALL_DIT_BF16_WHEN_RESIDENT,
+ preview_factors = "flux2",
+ ),
+ pipeline_class = "MOSSImagePipeline",
+ transformer_class = "MOSSImageTransformer",
+ base_repo = "OpenMOSS-Team/MOSS-Image-SFT",
+ named_variant_bases = ("OpenMOSS-Team/MOSS-Image-Base",),
+ prequant_repos = (
+ ("int8", "unsloth/MOSS-Image-SFT-FP8"),
+ ("fp8", "unsloth/MOSS-Image-SFT-FP8"),
+ ),
+ prequant_filenames = (
+ ("int8", "MOSS-Image-SFT-INT8.safetensors"),
+ ("fp8", "MOSS-Image-SFT-FP8.safetensors"),
+ ),
+ prequant_variant_repos = (
+ ("openmoss-team/moss-image-base", "int8", "unsloth/MOSS-Image-Base-FP8"),
+ ("openmoss-team/moss-image-base", "fp8", "unsloth/MOSS-Image-Base-FP8"),
+ ),
+ # Other schemes on Base quantise Base's own weights, never SFT's file.
+ prequant_excluded_bases = ("openmoss-team/moss-image-base",),
+ # Qwen3.5-9B int8 ConvRot: LPIPS 0.059 vs bf16; without the hosted file auto keeps bf16.
+ te_quant_auto = "int8",
+ aliases = ("moss-image-sft", "moss-image-base", "mossimage", "moss_image"),
+ prompt_hint = (
+ "MOSS-Image reads plain captions, tag lists or JSON layouts; raise CFG (3 to 6) for short prompts."
+ ),
+ dimension_multiple = 16,
+ # ComfyUI lists bf16 / fp32 only for MOSS-Image; the residual stream overflows float16.
+ fp16_incompatible = True,
+ # cudnn.benchmark on the fp32 VAE decode took 254 s / 73 GiB and picked a 4x slower plan: keep the heuristic.
+ cudnn_benchmark = False,
+ native_transformer_files_only = True,
+ # MOSS's own small-decoder FLUX.2 VAE, not FLUX.2-dev's.
+ sd_cpp_vae = ("unsloth/MOSS-Image-SFT-FP8", "vae/diffusion_pytorch_model.safetensors"),
+ sd_cpp_text_encoders = (("unsloth/Qwen3.5-9B-GGUF", "Qwen3.5-9B-Q8_0.gguf", "llm"),),
+ sd_cpp_tokenizer = ("unsloth/MOSS-Image-SFT-FP8", "tokenizer/tokenizer.json"),
+ # sd.cpp master-975 model_version_to_str name; its default flow shift is already 5.
+ sd_cpp_arch_marker = "MOSS-Image",
+ ),
# SDXL is the one U-Net family: the denoiser is ``pipe.unet`` and a single-file ``.safetensors`` is the WHOLE
# pipeline. img2img / inpaint / ControlNet are the standard SDXL pipelines. No GGUF path.
DiffusionFamily(
@@ -1643,6 +1722,9 @@ _UNGATED_MIRROR_PAIRS: tuple[tuple[str, str], ...] = (
("Qwen/Qwen-Image-Edit", "unsloth/Qwen-Image-Edit-GGUF"),
("Qwen/Qwen-Image-Edit-2509", "unsloth/Qwen-Image-Edit-2509-GGUF"),
("Qwen/Qwen-Image-Layered", "unsloth/Qwen-Image-Layered-GGUF"),
+ # MOSS-Image, as FLUX.3: upstream diffusers folders are mirrored byte-identical into each variant's -FP8 repo.
+ ("OpenMOSS-Team/MOSS-Image-SFT", "unsloth/MOSS-Image-SFT-FP8"),
+ ("OpenMOSS-Team/MOSS-Image-Base", "unsloth/MOSS-Image-Base-FP8"),
("black-forest-labs/FLUX.2-klein-4B", "unsloth/FLUX.2-klein-4B"),
# Lookup is by exact id, so every VARIANT a pick can resolve to needs its own row: the base-4B is what
# `unsloth/FLUX.2-klein-base-4B-GGUF` resolves to from its card tag, and Dev / Fast are offered directly as bf16
@@ -1702,6 +1784,8 @@ _ARTIFACT_MIRRORS: frozenset[str] = frozenset(
"unsloth/qwen-image-edit-gguf",
"unsloth/qwen-image-edit-2509-gguf",
"unsloth/qwen-image-layered-gguf",
+ "unsloth/moss-image-sft-fp8",
+ "unsloth/moss-image-base-fp8",
}
)
_ALL_MIRROR_PAIRS: tuple[tuple[str, str], ...] = _MIRROR_PAIRS + _VIDEO_MIRROR_PAIRS
@@ -2119,6 +2203,10 @@ _GENERATION_DEFAULTS: tuple[tuple[str, int, float], ...] = (
("hidream", 50, 5.0),
# Ideogram 4: an explicit 48 steps at 7 still runs the card's tapered schedule.
("ideogram", 20, 7.0),
+ # MOSS-Image: ComfyUI workflow, 50 steps at CFG 3.
+ ("moss-image", 50, 3.0),
+ ("moss_image", 50, 3.0),
+ ("mossimage", 50, 3.0),
# SDXL: Turbo distilled; base wants ~30 steps + CFG ~7. "sdxl-turbo" precedes "sdxl".
("sdxl-turbo", 3, 0.0),
("stable-diffusion-xl", 25, 7.0),
@@ -2429,6 +2517,12 @@ def _json_dict(path: Path, max_bytes: int = _MAX_PIPELINE_MANIFEST_BYTES) -> Opt
_CALLER_SUPPLIED_COMPONENTS = {"HiDreamImagePipeline": frozenset({"text_encoder_4", "tokenizer_4"})}
+# Remote-code components loaded via an in-tree port, judged by the layout that port reads (else a config-only
+# MOSS-Image transformer/ would pass).
+_IN_TREE_PORT_LIBRARIES = {
+ ("pipeline_moss_image", "MOSSImageTransformer"): "diffusers",
+ ("pipeline_moss_image", "MOSSImageScheduler"): "diffusers",
+}
# safetensors first, default variant only: the load uses variant=None, which cannot open *.fp16.safetensors.
_LOCAL_PIPELINE_WEIGHT_FORMATS = (
(("diffusion_pytorch_model", "model"), "safetensors"),
@@ -2497,6 +2591,7 @@ def _local_pipeline_component_is_complete(
) -> bool:
if not component.is_dir():
return False
+ library_name = _IN_TREE_PORT_LIBRARIES.get((library_name, class_name), library_name)
if library_name not in {"diffusers", "transformers"}:
return any(_nonempty_file(child) for child in component.iterdir())
identity = class_name.replace("_", "").lower()
@@ -2625,6 +2720,46 @@ _PIPELINE_PROBE_FILES: dict[str, str] = {
}
+# In-tree pipelines: (diffusers VAE module, transformers floor, family label).
+_IN_TREE_PIPELINES: dict[str, tuple[str, str, str]] = {
+ "MOSSImagePipeline": ("models/autoencoders/autoencoder_kl_flux2.py", "5.3.0", "MOSS-Image"),
+}
+
+
+def in_tree_pipeline(pipeline_class: Optional[str]) -> bool:
+ """Whether ``pipeline_class`` is one of Studio's in-tree pipelines (no diffusers class of that name)."""
+ return str(pipeline_class or "") in _IN_TREE_PIPELINES
+
+
+def _installed_transformers_version() -> Optional[str]:
+ try:
+ from importlib.metadata import version
+ installed = version("transformers")
+ except Exception: # noqa: BLE001 - not installed / unreadable metadata
+ return None
+ return installed.strip() if isinstance(installed, str) and installed.strip() else None
+
+
+def in_tree_pipeline_refusal(pipeline_class: str, family_name: str) -> Optional[str]:
+ """Why this environment cannot build the in-tree ``pipeline_class``, or None when it can. Import-free."""
+ vae_file, min_transformers, label = _IN_TREE_PIPELINES[pipeline_class]
+ installed = _installed_transformers_version()
+ if installed is None:
+ return f"'{family_name}' ({label}) needs transformers >= {min_transformers}, which is not installed."
+ if not _installed_at_least(installed, min_transformers):
+ return (
+ f"'{family_name}' ({label}) needs transformers >= {min_transformers} for its Qwen3.5 text encoder "
+ f"(transformers.Qwen3_5Model), but this environment has transformers {installed}. Update Unsloth Studio "
+ f"(or run: pip install -U 'transformers>={min_transformers}') and restart it."
+ )
+ if _diffusers_package_has_file(vae_file) is False:
+ return (
+ f"'{family_name}' ({label}) needs a diffusers with the FLUX.2 VAE (AutoencoderKLFlux2). "
+ f"{DIFFUSERS_UPDATE_REMEDY}"
+ )
+ return None
+
+
def _diffusers_package_has_file(relative: str) -> Optional[bool]:
"""Whether the installed diffusers package contains ``relative``; None when it cannot be located."""
try:
@@ -2809,6 +2944,11 @@ def assert_pipeline_class_available(
except Exception: # noqa: BLE001, S110 - optimisation only, and this module has no logger
pass
+ if in_tree_pipeline(pipeline_class):
+ reason = in_tree_pipeline_refusal(pipeline_class, family_name)
+ if reason is not None:
+ raise ValueError(reason)
+ return
try:
# diffusers' LTX-2 modules import a transformers class the pinned transformers lacks; see ltx2_import_compat.
from .ltx2_import_compat import ensure_ltx2_pipelines_importable, is_ltx2_pipeline_class
@@ -2939,6 +3079,8 @@ def family_pipeline_available(fam: Optional[DiffusionFamily]) -> bool:
# is False -- which would hide the model.
if not name:
return True
+ if in_tree_pipeline(name):
+ return in_tree_pipeline_refusal(name, getattr(fam, "name", name)) is None
probe_file = _PIPELINE_PROBE_FILES.get(name)
if probe_file is not None:
has_file = _diffusers_package_has_file(probe_file)
diff --git a/studio/backend/core/inference/diffusion_int8_fused.py b/studio/backend/core/inference/diffusion_int8_fused.py
index ff0a93374aa..c230729c6c5 100644
--- a/studio/backend/core/inference/diffusion_int8_fused.py
+++ b/studio/backend/core/inference/diffusion_int8_fused.py
@@ -1596,6 +1596,37 @@ def install(
return candidates
+def arm_linear(device: Any) -> bool:
+ """Arm ``int8_linear`` on ``device`` for a denoiser with no fused-MLP candidate (MOSS-Image calls it from its own
+ block forward): resolve torchao's ``safe_int_mm`` home and probe the act quant kernel. True when the probed kernel
+ serves this device; False keeps torchao's quant (``int8_linear`` still works, at the stock quant's speed)."""
+ global _ACTQ_HANDLE, _ACTQ_DEVICES, _INTMM_MODULE, _LINEAR_OFF
+ if int8_fused_disabled():
+ _LINEAR_OFF = True
+ return False
+ _LINEAR_OFF = False
+ try:
+ import torch
+
+ intmm = _resolve_intmm()
+ if intmm is None:
+ return False
+ _INTMM_MODULE = intmm
+ index = device.index if device.index is not None else torch.cuda.current_device()
+ if not _device_ok(index):
+ return False
+ actq = _act_quant_op() if _act_quant_device_ok(index) else None
+ except Exception: # noqa: BLE001 - optimisation only
+ return False
+ with _LOCK:
+ if actq is None:
+ _ACTQ_DEVICES = _ACTQ_DEVICES - {index}
+ return False
+ _ACTQ_HANDLE = actq
+ _ACTQ_DEVICES = _ACTQ_DEVICES | {index}
+ return True
+
+
def _swiglu_candidate(module: Any) -> bool:
return (
_swiglu_spec(module) is not None or _split_spec(module) is not None
diff --git a/studio/backend/core/inference/diffusion_lora.py b/studio/backend/core/inference/diffusion_lora.py
index ee5af6bbfe6..50cbf3067d3 100644
--- a/studio/backend/core/inference/diffusion_lora.py
+++ b/studio/backend/core/inference/diffusion_lora.py
@@ -667,6 +667,19 @@ _NATIVE_LORA_FAMILY_TOKENS = (
_DIFFUSERS_LORA_BAKED_QUANT = ("int8", "fp8")
# Prototype schemes with no validated LoRA path (and no shipped families needing one).
_DIFFUSERS_LORA_BLOCKED_QUANT = ("nvfp4", "mxfp8")
+# In-tree pipelines with no LoRA loader mixin (no load_lora_weights / set_adapters / unload_lora_weights).
+_DIFFUSERS_LORA_UNSUPPORTED_FAMILIES = ("moss-image",)
+
+
+def diffusers_lora_family_supported(family: Optional[str]) -> bool:
+ return (family or "").strip().lower() not in _DIFFUSERS_LORA_UNSUPPORTED_FAMILIES
+
+
+def diffusers_lora_family_message(family: Optional[str]) -> str:
+ return (
+ f"LoRA is not supported for {family} yet: its pipeline has no LoRA loader. "
+ "Clear the LoRA selection to load or generate."
+ )
def supports_lora(
@@ -691,6 +704,8 @@ def supports_lora(
fam = (family or "").lower()
if engine == "sd_cpp":
return any(tok in fam for tok in _NATIVE_LORA_FAMILY_TOKENS)
+ if not diffusers_lora_family_supported(fam):
+ return False
quant = (transformer_quant or "").lower()
if quant in _DIFFUSERS_LORA_BAKED_QUANT:
return True # load-time bake; adapters ride inside the compiled quantized build
diff --git a/studio/backend/core/inference/diffusion_memory.py b/studio/backend/core/inference/diffusion_memory.py
index 1649df23558..ffae0f233e8 100644
--- a/studio/backend/core/inference/diffusion_memory.py
+++ b/studio/backend/core/inference/diffusion_memory.py
@@ -2897,7 +2897,13 @@ PARTIAL_RESIDENT_ENV = "UNSLOTH_DIFFUSION_PARTIAL_RESIDENT"
# Worst measured CUDA MiB above the resident weights, one 1024x1024 image, encoder + every step + VAE decode, torchao
# int8 / fp8 denoisers on the compiled tiers (Qwen-Image-2.1: encoder 1849 streamed, denoise 1442, decode 1730).
# FLUX.1 / Z-Image: their worst phase is the untiled 16-channel VAE decode (_MEASURED_IMAGE_ACTIVATION_MIB).
-_MEASURED_IMAGE_PEAK_MIB: dict[str, int] = {"qwen-image-2.1": 1849, "flux.1": 2666, "z-image": 2666}
+# MOSS-Image: 2905 MiB worst-phase peak fully streamed (B200 capped at 16 GiB).
+_MEASURED_IMAGE_PEAK_MIB: dict[str, int] = {
+ "qwen-image-2.1": 1849,
+ "flux.1": 2666,
+ "z-image": 2666,
+ "moss-image": 2976,
+}
_MEASURED_PEAK_SPEED_MODES = ("default", "max")
_MEASURED_PEAK_MARGIN = 1.15
_MEASURED_PEAK_ROUND_MIB = 256
diff --git a/studio/backend/core/inference/diffusion_moss_fused.py b/studio/backend/core/inference/diffusion_moss_fused.py
new file mode 100644
index 00000000000..d2ec270e64d
--- /dev/null
+++ b/studio/backend/core/inference/diffusion_moss_fused.py
@@ -0,0 +1,399 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""MOSS-Image blocks inside the regional compile: one q/k/v GEMM, and int8 Linears through ``int8_linear``.
+
+ComfyUI's repacked MOSS files fuse ``proj.q/k/v`` into one ``proj.qkv`` Linear ("one GEMM and one activation
+quantization instead of three"). Here the three bias-free int8 projections become one Linear over the concatenated
+output rows, built at load from the checkpoint's own tensors: codes and per-row scales concatenate; ConvRot parts
+rotate the SAME input at one group, and the rotation acts on the shared input axis, so the fused Linear is itself a
+ConvRotLinear (one rotation + one act quant + one GEMM for q, k and v). Per-row scales make the fused output equal the
+three separate ones. The parts' weights become views of the fused one, so no memory is added. The block reads the
+fused Linear only inside the compiled forward and only while ``proj.q/k/v`` are still the modules it was built from
+(a LoRA wrapper keeps stock). bf16 keeps three GEMMs: one fused GEMM measured neutral there.
+
+int8 (plain or ConvRot) Linears also run through ``diffusion_int8_fused.int8_linear``: the module's own rotation, the
+probed Triton act quant (bit-identical to torchao's) and ``_int_mm`` with torchao's epilogue, i.e. torchao's eager math
+where the stock compiled path is Inductor's fp32 chain.
+
+Measured on an exclusive B200 (1024x1024, 30 steps, CFG 3, CUDA graphs, s/step): INT8-ConvRot 0.0769-0.0778 ->
+0.0752 with the runner -> 0.0722-0.0726 with the fused QKV too; plain INT8 0.0700 -> 0.0673-0.0678. Peak unchanged.
+
+``UNSLOTH_DIFFUSION_MOSS_QK_ROPE=1`` (opt-in) also runs the q/k RMSNorm + split-half RoPE + [B, H, S, D] relayout as
+one kernel below; it bought 0.7% on bf16 and nothing on int8, so it stays off.
+
+Kill switch: ``UNSLOTH_DIFFUSION_MOSS_FUSED=0`` (``UNSLOTH_DIFFUSION_MOSS_FUSED_QKV=0`` keeps only the int8 runner).
+"""
+
+from __future__ import annotations
+
+import functools
+import os
+import threading
+from typing import Any
+
+MOSS_FUSED_ENV = "UNSLOTH_DIFFUSION_MOSS_FUSED"
+MOSS_FUSED_QKV_ENV = "UNSLOTH_DIFFUSION_MOSS_FUSED_QKV"
+_LOCK = threading.Lock()
+
+
+def _off(name: str) -> bool:
+ return (os.environ.get(name) or "").strip().lower() in ("0", "off", "false", "no")
+
+
+def _on(name: str) -> bool:
+ return (os.environ.get(name) or "").strip().lower() in ("1", "on", "true", "yes")
+
+
+def moss_fused_disabled() -> bool:
+ return _off(MOSS_FUSED_ENV)
+
+
+def _blocks(root: Any) -> list:
+ from .diffusion_moss_image import _Block
+ return [m for m in root.modules() if type(m) is _Block]
+
+
+def _int8_runnable(linear: Any) -> bool:
+ from torch import nn
+
+ from .diffusion_convrot import is_rotated_linear
+ from .diffusion_int8_fused import _I8_GEMM_MARK, _plain_int8_weight
+
+ d = getattr(linear, "__dict__", {})
+ return (
+ (type(linear) is nn.Linear or is_rotated_linear(linear))
+ and "forward" not in d
+ and _I8_GEMM_MARK not in d
+ and _plain_int8_weight(linear.weight)
+ )
+
+
+def install(
+ transformer: Any,
+ logger: Any = None,
+ offload_active: bool = False,
+) -> dict:
+ """Before the first compile; deferred to the first forward when the weights are not on the GPU yet. A streamed
+ (offloaded) denoiser keeps the stock projections: its hooks own each Linear's weights."""
+ result = {"fused_qkv": 0, "int8_runner": 0, "qk_rope": 0}
+ if (
+ moss_fused_disabled()
+ or offload_active
+ or type(transformer).__name__ != "MOSSImageTransformer"
+ ):
+ uninstall(transformer)
+ return result
+ from .diffusion_int8_fused import resident_cuda_device, run_on_first_call
+
+ if resident_cuda_device(transformer) is not None:
+ return install_modules(transformer, logger)
+ run_on_first_call(transformer, "moss_fused", lambda t: install_modules(t, logger))
+ return {"fused_qkv": None, "int8_runner": None}
+
+
+def install_modules(root: Any, logger: Any = None) -> dict:
+ from .diffusion_int8_fused import arm_linear, int8_linear, resident_cuda_device
+ import torch
+
+ from .diffusion_moss_image import FUSED_QKV_ATTR, LINEAR_RUNNER_ATTR, QK_ROPE_ATTR
+ from .diffusion_zimage_fused import _fuse_linears, _share_storage
+
+ result = {"fused_qkv": 0, "int8_runner": 0, "qk_rope": 0}
+ dev = resident_cuda_device(root)
+ if dev is None or moss_fused_disabled():
+ return result
+ blocks = _blocks(root)
+ int8 = any(_int8_runnable(b.proj["q"]) for b in blocks)
+ armed = arm_linear(dev) if int8 else False
+ fuse = not _off(MOSS_FUSED_QKV_ENV)
+ index = dev.index if dev.index is not None else torch.cuda.current_device()
+ rope_op = qk_rope_op() if _on(MOSS_QK_ROPE_ENV) and qk_rope_device_ok(index) else None
+ with _LOCK:
+ for block in blocks:
+ norms_ok = all(
+ n.weight.dtype == torch.bfloat16 and n.weight.numel() == 128
+ for n in (block.q_norm, block.k_norm)
+ )
+ if rope_op is not None and norms_ok:
+ block.__dict__[QK_ROPE_ATTR] = rope_op
+ result["qk_rope"] += 1
+ else:
+ block.__dict__.pop(QK_ROPE_ATTR, None)
+ runner = int8_linear if int8 and _int8_runnable(block.proj["q"]) else None
+ if runner is not None:
+ block.__dict__[LINEAR_RUNNER_ATTR] = runner
+ result["int8_runner"] += 1
+ if not fuse or runner is None:
+ # bf16: one q/k/v GEMM measured neutral (B200, 0.0879 vs 0.0881 s/step, inside the A/A spread)
+ block.__dict__.pop(FUSED_QKV_ATTR, None)
+ continue
+ parts = [block.proj[n] for n in ("q", "k", "v")]
+ rec = block.__dict__.get(FUSED_QKV_ATTR)
+ if rec is None or any(a is not b for a, b in zip(rec[1:4], parts)):
+ fused = _fuse_linears(parts)
+ if fused is None or not _share_storage(fused, parts):
+ continue
+ block.__dict__[FUSED_QKV_ATTR] = (fused, *parts, runner)
+ result["fused_qkv"] += 1
+ if logger is not None:
+ logger.info(
+ "diffusion.moss_fused: one q/k/v GEMM on %d of %d block(s); int8 runner on %d (act quant kernel %s); "
+ "fused q/k norm + RoPE on %d",
+ result["fused_qkv"],
+ len(blocks),
+ result["int8_runner"],
+ "probed" if armed else "torchao's",
+ result["qk_rope"],
+ )
+ return result
+
+
+def uninstall(transformer: Any = None) -> None:
+ """Drop the fused Linears (the per-row views stay valid) and any pending install."""
+ if transformer is None:
+ return
+ from .diffusion_int8_fused import cancel_first_call
+ from .diffusion_moss_image import FUSED_QKV_ATTR, LINEAR_RUNNER_ATTR, QK_ROPE_ATTR
+
+ cancel_first_call(transformer, "moss_fused")
+ try:
+ blocks = _blocks(transformer)
+ except Exception: # noqa: BLE001
+ return
+ with _LOCK:
+ for block in blocks:
+ block.__dict__.pop(FUSED_QKV_ATTR, None)
+ block.__dict__.pop(LINEAR_RUNNER_ATTR, None)
+ block.__dict__.pop(QK_ROPE_ATTR, None)
+
+
+# Fuses q/k RMSNorm + RoPE + layout copy into one pass; keeps every eager bf16 rounding of the port.
+MOSS_QK_ROPE_ENV = "UNSLOTH_DIFFUSION_MOSS_QK_ROPE"
+_QK_ROPE_OP = "moss_qk_norm_rope"
+_HEADS_PER_PROGRAM = 8
+
+
[email protected]_cache(maxsize = 1)
+def _qk_rope_kernel() -> Any:
+ try:
+ import triton
+ import triton.language as tl
+ except Exception: # noqa: BLE001
+ return None
+
+ @triton.jit
+ def _norm_rope(
+ x_ptr,
+ w_ptr,
+ o_ptr,
+ base,
+ out,
+ h,
+ j,
+ c1,
+ s1,
+ c2,
+ s2,
+ mask,
+ EPS: tl.constexpr,
+ D: tl.constexpr,
+ ):
+ # rotate(x) = [-x2, x1]; each product and sum rounded to bf16 like eager
+ x1 = tl.load(x_ptr + base + h * D + j, mask = mask, other = 0.0).to(tl.float32)
+ x2 = tl.load(x_ptr + base + h * D + D // 2 + j, mask = mask, other = 0.0).to(tl.float32)
+ r = 1.0 / tl.sqrt_rn((tl.sum(x1 * x1, axis = 1) + tl.sum(x2 * x2, axis = 1)) / D + EPS)
+ w1 = tl.load(w_ptr + j).to(tl.float32)
+ w2 = tl.load(w_ptr + D // 2 + j).to(tl.float32)
+ n1 = ((x1 * r[:, None]).to(tl.bfloat16).to(tl.float32) * w1).to(tl.bfloat16).to(tl.float32)
+ n2 = ((x2 * r[:, None]).to(tl.bfloat16).to(tl.float32) * w2).to(tl.bfloat16).to(tl.float32)
+ o1 = (n1 * c1).to(tl.bfloat16).to(tl.float32) + (-n2 * s1).to(tl.bfloat16).to(tl.float32)
+ o2 = (n2 * c2).to(tl.bfloat16).to(tl.float32) + (n1 * s2).to(tl.bfloat16).to(tl.float32)
+ tl.store(o_ptr + out + j, o1.to(tl.bfloat16), mask = mask)
+ tl.store(o_ptr + out + D // 2 + j, o2.to(tl.bfloat16), mask = mask)
+
+ @triton.jit
+ def kernel(
+ q_ptr,
+ k_ptr,
+ v_ptr,
+ qw_ptr,
+ kw_ptr,
+ cos_ptr,
+ sin_ptr,
+ oq_ptr,
+ ok_ptr,
+ ov_ptr,
+ S,
+ H,
+ sqb,
+ sqs,
+ skb,
+ sks,
+ svb,
+ svs,
+ scs,
+ EPS: tl.constexpr,
+ D: tl.constexpr,
+ HB: tl.constexpr,
+ ):
+ row = tl.program_id(0)
+ b = row // S
+ s = row % S
+ h = tl.program_id(1) * HB + tl.arange(0, HB)[:, None]
+ j = tl.arange(0, D // 2)[None, :]
+ mask = h < H
+ c1 = tl.load(cos_ptr + s * scs + j).to(tl.float32)
+ s1 = tl.load(sin_ptr + s * scs + j).to(tl.float32)
+ c2 = tl.load(cos_ptr + s * scs + D // 2 + j).to(tl.float32)
+ s2 = tl.load(sin_ptr + s * scs + D // 2 + j).to(tl.float32)
+ out = ((b * H + h) * S + s) * D
+ _norm_rope(
+ q_ptr, qw_ptr, oq_ptr, b * sqb + s * sqs, out, h, j, c1, s1, c2, s2, mask, EPS, D
+ )
+ _norm_rope(
+ k_ptr, kw_ptr, ok_ptr, b * skb + s * sks, out, h, j, c1, s1, c2, s2, mask, EPS, D
+ )
+ jj = tl.arange(0, D)[None, :]
+ v = tl.load(v_ptr + b * svb + s * svs + h * D + jj, mask = mask, other = 0.0)
+ tl.store(ov_ptr + out + jj, v, mask = mask)
+
+ return kernel
+
+
+def _launch_qk_rope(q, k, v, qw, kw, cos, sin, heads):
+ """q / k / v: [B, S, H*D] bf16 (last dim contiguous); cos / sin: [1, S, 1, D]; returns [B, H, S, D] contiguous x3."""
+ import triton
+
+ batch, seq, width = q.shape
+ dim = width // heads
+ oq = q.new_empty((batch, heads, seq, dim))
+ ok = torch_empty_like(oq)
+ ov = torch_empty_like(oq)
+ grid = (batch * seq, triton.cdiv(heads, _HEADS_PER_PROGRAM))
+ _qk_rope_kernel()[grid](
+ q,
+ k,
+ v,
+ qw,
+ kw,
+ cos,
+ sin,
+ oq,
+ ok,
+ ov,
+ seq,
+ heads,
+ q.stride(0),
+ q.stride(1),
+ k.stride(0),
+ k.stride(1),
+ v.stride(0),
+ v.stride(1),
+ cos.stride(1),
+ EPS = 1e-6,
+ D = dim,
+ HB = _HEADS_PER_PROGRAM,
+ # ptxas otherwise FMA-fuses the rounded products into the sum on sm_100
+ enable_fp_fusion = False,
+ )
+ return oq, ok, ov
+
+
+def torch_empty_like(t):
+ import torch
+ return torch.empty_like(t)
+
+
+def reference_qk_rope(q, k, v, qw, kw, cos, sin, heads):
+ """The port's eager ops, in [B, H, S, D]."""
+ import torch
+
+ def norm(x, w):
+ y = x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim = True) + 1e-6)
+ return y.to(x.dtype) * w
+
+ def rot(x):
+ a, b = x.chunk(2, -1)
+ return x * cos + torch.cat((-b, a), -1) * sin
+
+ shape = (*q.shape[:2], heads, q.shape[-1] // heads)
+ qh = rot(norm(q.unflatten(-1, (heads, -1)), qw).reshape(shape))
+ kh = rot(norm(k.unflatten(-1, (heads, -1)), kw).reshape(shape))
+ return qh.transpose(1, 2), kh.transpose(1, 2), v.reshape(shape).transpose(1, 2)
+
+
[email protected]_cache(maxsize = 1)
+def qk_rope_op() -> Any:
+ """``unsloth_studio::moss_qk_norm_rope`` (opaque to Inductor, allocation only, CUDA-graph safe), or None."""
+ try:
+ import torch
+ except Exception: # noqa: BLE001
+ return None
+ ns = getattr(torch.ops, "unsloth_studio", None)
+ if ns is not None and hasattr(ns, _QK_ROPE_OP):
+ return getattr(ns, _QK_ROPE_OP)
+ if _qk_rope_kernel() is None:
+ return None
+ try:
+
+ @torch.library.custom_op(
+ f"unsloth_studio::{_QK_ROPE_OP}",
+ mutates_args = (),
+ schema = "(Tensor q, Tensor k, Tensor v, Tensor qw, Tensor kw, Tensor cos, Tensor sin, int heads)"
+ " -> (Tensor, Tensor, Tensor)",
+ )
+ def _op(q, k, v, qw, kw, cos, sin, heads):
+ return _launch_qk_rope(q, k, v, qw, kw, cos, sin, heads)
+
+ @_op.register_fake
+ def _(q, k, v, qw, kw, cos, sin, heads):
+ batch, seq, width = q.shape
+ out = q.new_empty((batch, heads, seq, width // heads))
+ return out, torch.empty_like(out), torch.empty_like(out)
+ except Exception: # noqa: BLE001
+ return None
+ return getattr(torch.ops.unsloth_studio, _QK_ROPE_OP)
+
+
[email protected]_cache(maxsize = 8)
+def qk_rope_device_ok(index: int) -> bool:
+ """The kernel runs and matches the port's eager ops on this device: every element within two bf16 ulps of its
+ rotated pair's magnitude, and at
+ most 0.5% of them off at all (the reduction order of the sum of squares), over ragged lengths and big values."""
+ try:
+ import torch
+
+ op = qk_rope_op()
+ if op is None:
+ return False
+ dev = torch.device("cuda", index)
+ g = torch.Generator(device = "cpu").manual_seed(7)
+ for seq, heads in ((37, 4), (1029, 24), (5, 3)):
+ width = heads * 128
+ qkv = (torch.randn(2, seq, 3 * width, generator = g) * 3).to(torch.bfloat16).to(dev)
+ qkv[:, :, :7] *= 300
+ q, k, v = qkv.chunk(3, -1)
+ qw = (torch.rand(128, generator = g) + 0.5).to(torch.bfloat16).to(dev)
+ kw = (torch.rand(128, generator = g) + 0.5).to(torch.bfloat16).to(dev)
+ ang = torch.rand(seq, 64, generator = g) * 50
+ cos = torch.cat((ang.cos(), ang.cos()), -1)[None, :, None].to(torch.bfloat16).to(dev)
+ sin = torch.cat((ang.sin(), ang.sin()), -1)[None, :, None].to(torch.bfloat16).to(dev)
+ got = op(q, k, v, qw, kw, cos, sin, heads)
+ ref = reference_qk_rope(q, k, v, qw, kw, cos, sin, heads)
+ if not torch.equal(got[2], ref[2]):
+ return False
+ for a, r in zip(got[:2], ref[:2]):
+ diff = (a.float() - r.float()).abs()
+ # A bf16 tie moves an output a few ulps of the pair magnitude.
+ rf = r.float()
+ pair = (
+ rf[..., : rf.shape[-1] // 2].square() + rf[..., rf.shape[-1] // 2 :].square()
+ ).sqrt()
+ ulp = (torch.cat((pair, pair), -1) * 2.0**-6).clamp_min(2.0**-14)
+ if bool((diff > ulp).any()) or float((diff > 0).float().mean()) > 5e-3:
+ return False
+ torch.cuda.synchronize(dev)
+ return True
+ except Exception: # noqa: BLE001
+ return False
diff --git a/studio/backend/core/inference/diffusion_moss_image.py b/studio/backend/core/inference/diffusion_moss_image.py
new file mode 100644
index 00000000000..bee913d3cc6
--- /dev/null
+++ b/studio/backend/core/inference/diffusion_moss_image.py
@@ -0,0 +1,1107 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+#
+# The transformer, scheduler and pipeline below are a port of OpenMOSS-Team's ``pipeline_moss_image.py``
+# (Apache-2.0, https://huggingface.co/OpenMOSS-Team/MOSS-Image-SFT), keeping its state-dict names and numerics.
+
+"""MOSS-Image (OpenMOSS-Team/MOSS-Image-SFT and -Base) as an in-tree diffusers pipeline.
+
+The official repos ship the transformer and scheduler as remote code (``model_index.json`` points both at the repo's
+``pipeline_moss_image.py``), and Studio never runs repo code: no ``trust_remote_code``, no ``custom_pipeline``. So the
+model lives here and the pipeline is assembled per component from safetensors and JSON only: ``MOSSImageTransformer``
+(this file) from ``transformer/``, the Qwen3.5 text encoder (``transformers.Qwen3_5Model``, transformers >= 5.3) from
+``text_encoder/``, the FLUX.2 small-decoder VAE (``diffusers.AutoencoderKLFlux2``) from ``vae/`` and the tokenizer.
+
+Numerics follow the reference op for op (fp32 RMSNorm with the weight applied after the cast, fp64 timestep sinusoid,
+fp32 RoPE phases cast to the activation dtype, fp32 noise, CFG and Euler update, fp32 VAE decode); the tests check the
+port bit for bit against the reference file on a tiny random model. What differs is only how the same values are
+reached:
+
+- RoPE tables are built once per (height, width, text length) and reused (the reference rebuilt them per forward).
+- The text refiner (two unmodulated text blocks) does not read the latent or the timestep, so the pipeline runs it
+ once per prompt per render (``refine_text``) and hands the refined tokens to every step (``refined_text``), unless
+ the transformer carries offload hooks (then every step refines, as the reference does).
+- One sample at its full text length takes no boolean-mask indexing and no ``torch.all`` mask check, so the forward
+ is free of host syncs (CUDA-graph capturable); the pipeline passes no mask when every row has the same length.
+- Attention goes through diffusers' dispatcher (``set_attention_backend`` reaches it), whose native backend is the
+ same ``scaled_dot_product_attention`` call.
+
+Sampling defaults follow ComfyUI's MOSS-Image workflow (the baseline): 1024x1024, 50 steps, CFG 3, shift 5, Euler
+over ``sgm_uniform`` sigmas, skip-layer guidance on block 22 for the negative pass, empty negative prompt. The
+reference pipeline snaps its sigmas to the 1000-point training grid instead; ``sigma_schedule = "training_grid"`` on
+the scheduler (or ``UNSLOTH_MOSS_SIGMAS=training_grid``) selects that.
+"""
+
+from __future__ import annotations
+
+import math
+import os
+from typing import Any, Callable, Optional
+
+import torch
+from torch import nn
+from torch.nn import functional as F
+
+from loggers import get_logger
+
+logger = get_logger(__name__)
+
+MOSS_IMAGE_FAMILY_NAME = "moss-image"
+MOSS_IMAGE_PIPELINE_CLASS = "MOSSImagePipeline"
+MOSS_IMAGE_TRANSFORMER_CLASS = "MOSSImageTransformer"
+MOSS_IMAGE_SCHEDULER_CLASS = "MOSSImageScheduler"
+# The Qwen3.5 text encoder class (``transformers.Qwen3_5Model``) first shipped in transformers 5.3.
+MIN_TRANSFORMERS = "5.3.0"
+SIGMAS_ENV = "UNSLOTH_MOSS_SIGMAS"
+SIGMA_SCHEDULES = ("sgm_uniform", "training_grid")
+# Set by diffusion_moss_fused, read only inside a compiled block.
+FUSED_QKV_ATTR = "_unsloth_moss_fused_qkv"
+LINEAR_RUNNER_ATTR = "_unsloth_moss_linear"
+QK_ROPE_ATTR = "_unsloth_moss_qk_rope"
+# Kill switch for the per-render text-refiner cache (``refine_text``): 0 refines on every step like the reference.
+TEXT_CACHE_ENV = "UNSLOTH_MOSS_TEXT_CACHE"
+DEFAULT_UNCOND_SKIP_LAYERS = (22,)
+
+
+def _env_off(name: str) -> bool:
+ return (os.environ.get(name) or "").strip().lower() in ("0", "off", "false", "no")
+
+
+def _compute_dtype(linear: nn.Module) -> torch.dtype:
+ """The dtype a Linear's input is cast to: the reference casts to ``weight.dtype``, which is the storage dtype under
+ diffusers' ``GGUFLinear`` (uint8), so a ``compute_dtype`` the layer declares wins. Same dtype for a plain Linear."""
+ declared = getattr(linear, "compute_dtype", None)
+ return declared if isinstance(declared, torch.dtype) else linear.weight.dtype
+
+
+class _RMSNorm(nn.Module):
+ def __init__(self, dim):
+ super().__init__()
+ self.weight = nn.Parameter(torch.ones(dim))
+
+ def forward(self, x):
+ y = x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim = True) + 1e-6)
+ return y.to(x.dtype) * self.weight
+
+
+def _situ(hidden, gate):
+ return (4.0 * torch.tanh(gate / 4.0) * torch.sigmoid(gate)) * (25.0 * torch.tanh(hidden / 25.0))
+
+
+def _validate_skip_layers(
+ layers,
+ depth,
+ name = "skip_layers",
+):
+ layers = () if layers is None else tuple(layers)
+ if any(type(index) is not int or not 0 <= index < depth for index in layers):
+ raise ValueError(f"{name} must contain integer block indices in [0, {depth}).")
+ return frozenset(layers)
+
+
+def _length_dynamic(*tensors) -> None:
+ """Hint dim 1 (the text length) of the refiner block inputs as dynamic. The pipeline refines the text once per render
+ OUTSIDE ``forward`` (``refine_text``), where Studio's per-forward prompt-length arming does not reach, so the
+ compiled text block otherwise specialised on the first length and recompiled on the uncond one. A no-op eagerly."""
+ try:
+ if torch.compiler.is_compiling():
+ return
+ from torch._dynamo import maybe_mark_dynamic
+ except Exception: # noqa: BLE001 - no dynamo, nothing to hint
+ return
+ for tensor in tensors:
+ if isinstance(tensor, torch.Tensor) and tensor.dim() > 1:
+ maybe_mark_dynamic(tensor, 1)
+
+
+def _rope_tables(rope):
+ """``(cos, sin)`` duplicated to the full head dim, shaped to broadcast over heads: the split-half layout the
+ reference's ``_rotate`` builds on every call, built once."""
+ sin, cos = rope.chunk(2, -1)
+ sin = torch.cat((sin, sin), -1).unsqueeze(-2)
+ cos = torch.cat((cos, cos), -1).unsqueeze(-2)
+ return cos, sin
+
+
+def _rotate(x, tables):
+ cos, sin = tables
+ a, b = x.chunk(2, -1)
+ return x * cos + torch.cat((-b, a), -1) * sin
+
+
+class _AttnProcessor:
+ """Holds the attention backend diffusers' ``set_attention_backend`` assigns (None = the dispatcher's active one)."""
+
+ _attention_backend = None
+ _parallel_config = None
+
+
+def _dispatch_attention():
+ try:
+ from diffusers.models.attention_dispatch import dispatch_attention_fn
+ except Exception: # noqa: BLE001 - an older diffusers without the dispatcher
+ return None
+ return dispatch_attention_fn
+
+
+_DISPATCH = _dispatch_attention()
+
+
+def _attention(
+ q,
+ k,
+ v,
+ tables,
+ heads,
+ backend = None,
+):
+ shape = (*q.shape[:2], heads, q.shape[-1] // heads)
+ q = _rotate(q.reshape(shape), tables)
+ k = _rotate(k.reshape(shape), tables)
+ v = v.reshape(shape)
+ if _DISPATCH is None:
+ out = F.scaled_dot_product_attention(
+ q.to(v.dtype).transpose(1, 2), k.to(v.dtype).transpose(1, 2), v.transpose(1, 2)
+ ).transpose(1, 2)
+ else:
+ out = _DISPATCH(q.to(v.dtype), k.to(v.dtype), v, backend = backend)
+ return out.flatten(2)
+
+
+def _attention_heads_first(
+ q,
+ k,
+ v,
+ backend = None,
+):
+ """Attention over already normalised and rotated [B, H, S, D] q / k / v (the dispatcher takes [B, S, H, D] views)."""
+ q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)
+ if _DISPATCH is None:
+ out = F.scaled_dot_product_attention(
+ q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)
+ ).transpose(1, 2)
+ else:
+ out = _DISPATCH(q, k, v, backend = backend)
+ return out.flatten(2)
+
+
+try: # the mixin is what ``ModelMixin.set_attention_backend`` looks for
+ from diffusers.models.attention import AttentionModuleMixin as _AttentionMixin
+except Exception: # noqa: BLE001 - older diffusers: attention stays on SDPA
+
+ class _AttentionMixin: # type: ignore[no-redef]
+ pass
+
+
+class _TextBlock(nn.Module, _AttentionMixin):
+ _supports_qkv_fusion = False
+
+ def __init__(self, dim, ffn_dim, heads):
+ super().__init__()
+ self.heads = heads
+ self.processor = _AttnProcessor()
+ self.norm1, self.norm2 = _RMSNorm(dim), _RMSNorm(dim)
+ self.q_norm, self.k_norm = _RMSNorm(dim // heads), _RMSNorm(dim // heads)
+ self.qkv = nn.Linear(dim, 3 * dim)
+ self.proj = nn.Linear(dim, dim)
+ self.ffn_gate, self.ffn_up = nn.Linear(dim, ffn_dim), nn.Linear(dim, ffn_dim)
+ self.ffn_down = nn.Linear(ffn_dim, dim)
+
+ def forward(self, x, rope):
+ q, k, v = self.qkv(self.norm1(x)).chunk(3, -1)
+ q = self.q_norm(q.unflatten(-1, (self.heads, -1))).flatten(2)
+ k = self.k_norm(k.unflatten(-1, (self.heads, -1))).flatten(2)
+ x = x + self.proj(_attention(q, k, v, rope, self.heads, self.processor._attention_backend))
+ y = self.norm2(x)
+ return x + self.ffn_down(_situ(self.ffn_up(y), self.ffn_gate(y)))
+
+
+class _TextProjection(nn.Module):
+ def __init__(self, text_dim, dim, ffn_dim, heads):
+ super().__init__()
+ self.proj = nn.Linear(text_dim, dim)
+ self.blocks = nn.ModuleList([_TextBlock(dim, ffn_dim, heads) for _ in range(2)])
+
+ def forward(self, x, rope):
+ x = self.proj(x.to(_compute_dtype(self.proj)))
+ _length_dynamic(x, *rope)
+ for block in self.blocks:
+ x = block(x, rope)
+ return x
+
+
+class _Block(nn.Module, _AttentionMixin):
+ _supports_qkv_fusion = False
+
+ def __init__(self, dim, ffn_dim, heads):
+ super().__init__()
+ self.heads = heads
+ self.processor = _AttnProcessor()
+ self.norm1, self.norm2 = _RMSNorm(dim), _RMSNorm(dim)
+ self.q_norm, self.k_norm = _RMSNorm(dim // heads), _RMSNorm(dim // heads)
+ self.proj = nn.ModuleDict(
+ {name: nn.Linear(dim, dim, bias = False) for name in ("q", "k", "v", "o")}
+ )
+ self.proj["ffn_up_gate"] = nn.Linear(dim, 2 * ffn_dim, bias = False)
+ self.proj["ffn_down"] = nn.Linear(ffn_dim, dim, bias = False)
+
+ def _linear(self, name, x):
+ run = self.__dict__.get(LINEAR_RUNNER_ATTR) if torch.compiler.is_compiling() else None
+ return self.proj[name](x) if run is None else run(self.proj[name], x)
+
+ def _qkv(self, y):
+ fused = self.__dict__.get(FUSED_QKV_ATTR) if torch.compiler.is_compiling() else None
+ if fused is not None:
+ linear, q, k, v, run = fused
+ # only while q / k / v are still the modules it was built from (a LoRA wrapper replaces them)
+ if self.proj["q"] is q and self.proj["k"] is k and self.proj["v"] is v:
+ return (linear(y) if run is None else run(linear, y)).chunk(3, -1)
+ return self._linear("q", y), self._linear("k", y), self._linear("v", y)
+
+ def forward(self, x, rope, modulation):
+ shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = modulation.unbind(1)
+ y = self.norm1(x) * (1 + scale_a[:, None]) + shift_a[:, None]
+ dtype = _compute_dtype(self.proj["q"])
+ y = y.to(dtype)
+ q, k, v = self._qkv(y)
+ fused = self.__dict__.get(QK_ROPE_ATTR) if torch.compiler.is_compiling() else None
+ if fused is not None and q.dtype == torch.bfloat16:
+ q, k, v = fused(
+ q, k, v, self.q_norm.weight, self.k_norm.weight, rope[0], rope[1], self.heads
+ )
+ attn = _attention_heads_first(q, k, v, self.processor._attention_backend)
+ else:
+ q = self.q_norm(q.unflatten(-1, (self.heads, -1))).flatten(2)
+ k = self.k_norm(k.unflatten(-1, (self.heads, -1))).flatten(2)
+ attn = _attention(q, k, v, rope, self.heads, self.processor._attention_backend)
+ x = x + self._linear("o", attn) * gate_a[:, None]
+ y = self.norm2(x) * (1 + scale_f[:, None]) + shift_f[:, None]
+ hidden, gate = self._linear("ffn_up_gate", y.to(dtype)).chunk(2, -1)
+ return x + self._linear("ffn_down", _situ(hidden, gate)) * gate_f[:, None]
+
+
+class _RoPE(nn.Module):
+ _MAX_CACHED = 8
+
+ def __init__(self, head_dim, base):
+ super().__init__()
+ self.num_bands = head_dim // 8
+ self.base = base
+ self.register_buffer("bands", self._default_bands(torch.device("cpu")))
+ self._tables: dict = {}
+
+ def _default_bands(self, device):
+ # Match training's CPU fp32 construction, including under meta init.
+ exponent = torch.arange(self.num_bands, device = "cpu", dtype = torch.float32) / self.num_bands
+ return (1 / (self.base**exponent)).to(device)
+
+ def _apply(
+ self,
+ fn,
+ recurse = True,
+ ):
+ super()._apply(fn, recurse = recurse)
+ # .float() cannot recover frequencies already rounded by .to(bf16).
+ self.bands = self._default_bands(self.bands.device)
+ self._tables = {}
+ return self
+
+ def _load_from_state_dict(self, *args, **kwargs):
+ super()._load_from_state_dict(*args, **kwargs)
+ # Frequencies are derived constants; the key stays for checkpoint compatibility.
+ self.bands = self._default_bands(self.bands.device)
+ self._tables = {}
+
+ def forward(self, height, width, text_length, device):
+ """The reference's phase table ``[sin | cos]`` in fp32, [1, H*W + L, head_dim]."""
+ if self.bands.dtype != torch.float32 or self.bands.device.type == "meta":
+ self.bands = self._default_bands(device)
+ bands = self.bands.to(device)
+ h, w = torch.meshgrid(
+ torch.arange(height, device = device), torch.arange(width, device = device), indexing = "ij"
+ )
+ image = (
+ torch.stack((torch.zeros_like(h), torch.full_like(h, text_length), h, w), -1)
+ .flatten(0, 1)
+ .float()
+ )
+ image[:, 2] -= (height - 1) / 2
+ image[:, 3] -= (width - 1) / 2
+ text = torch.zeros(text_length, 4, device = device, dtype = torch.float32)
+ text[:, 1] = torch.arange(text_length, device = device)
+ phase = torch.cat((image, text), 0).unsqueeze(-1) * bands
+ return torch.cat((phase.sin(), phase.cos()), -2).flatten(-2).unsqueeze(0)
+
+ def tables(self, height, width, text_length, device, dtype):
+ """``((cos, sin) for image + text, (cos, sin) for the text alone)`` in ``dtype``, cached per key."""
+ device = torch.device(device)
+ key = (int(height), int(width), int(text_length), device.type, device.index, dtype)
+ hit = self._tables.get(key)
+ if hit is not None:
+ return hit
+ rope = self.forward(height, width, text_length, device).to(dtype)
+ full = _rope_tables(rope)
+ text = _rope_tables(rope[:, height * width :])
+ if len(self._tables) >= self._MAX_CACHED:
+ self._tables.pop(next(iter(self._tables)))
+ entry = self._tables[key] = (full, text)
+ return entry
+
+
+def _model_bases():
+ from diffusers.configuration_utils import ConfigMixin
+ from diffusers.models.modeling_utils import ModelMixin
+ return ModelMixin, ConfigMixin
+
+
+_ModelMixin, _ConfigMixin = _model_bases()
+from diffusers.configuration_utils import register_to_config # noqa: E402
+
+
+class MOSSImageTransformer(_ModelMixin, _ConfigMixin):
+ """MOSS-Image DiT: a text refiner, then 42 single-stream joint blocks over [image ; text] sharing one AdaLN.
+
+ Each sample runs at its true text length, text refinement included, so padded tokens never reach attention."""
+
+ _repeated_blocks = ["_Block", "_TextBlock"]
+ _no_split_modules = ["_Block", "_TextBlock"]
+ _skip_layerwise_casting_patterns = ["rope", "norm"]
+
+ @register_to_config
+ def __init__(
+ self,
+ dim = 3072,
+ ffn_dim = 8192,
+ num_heads = 24,
+ num_layers = 42,
+ text_dim = 4096,
+ freq_dim = 256,
+ latent_channels = 128,
+ rope_base = 10000.0,
+ ):
+ super().__init__()
+ if dim % num_heads or (dim // num_heads) % 8 or freq_dim % 2:
+ raise ValueError(
+ "Require dim divisible by heads, head_dim divisible by 8, and even freq_dim."
+ )
+ self.video_embedder = nn.Linear(latent_channels, dim)
+ self.text_embedder = _TextProjection(text_dim, dim, ffn_dim, num_heads)
+ self.rope = _RoPE(dim // num_heads, rope_base)
+ self.time_embedding = nn.Sequential(
+ nn.Linear(freq_dim, dim), nn.SiLU(), nn.Linear(dim, dim)
+ )
+ self.shared_block_modulation = nn.Linear(dim, 6 * dim)
+ self.layers = nn.ModuleList([_Block(dim, ffn_dim, num_heads) for _ in range(num_layers)])
+ self.final_norm_video = _RMSNorm(dim)
+ self.final_linear_video = nn.Linear(dim, latent_channels, bias = False)
+ self.video_head_modulation = nn.Linear(dim, 2 * dim)
+
+ def load_training_state_dict(
+ self,
+ state_dict,
+ *,
+ prefix = "",
+ ):
+ """Load a consolidated raw/EMA model mapping, strictly; no silent key drops."""
+ if prefix:
+ if any(not key.startswith(prefix) for key in state_dict):
+ raise ValueError("All keys must start with the explicit prefix.")
+ state_dict = {key[len(prefix) :]: value for key, value in state_dict.items()}
+ return self.load_state_dict(state_dict, strict = True)
+
+ def _activation_dtype(self):
+ return _compute_dtype(self.video_embedder)
+
+ def refine_text(self, encoder_hidden_states):
+ """The text refiner over [B, L, text_dim] at full length L (no padding): [B, L, dim]. Depends on neither the
+ latent nor the timestep, so it is step-invariant; the RoPE text positions depend only on L."""
+ length = encoder_hidden_states.shape[1]
+ device = encoder_hidden_states.device
+ _, text_tables = self.rope.tables(1, 1, length, device, self._activation_dtype())
+ return self.text_embedder(encoder_hidden_states, text_tables)
+
+ def _time_modulation(self, timestep, batch, device):
+ times = torch.as_tensor(timestep, device = device, dtype = torch.float32).reshape(-1)
+ times = times.expand(batch)
+ # Training computes the sinusoid in float64 and feeds float32 into its MLP.
+ half = self.config.freq_dim // 2
+ freq = 10000 ** (-torch.arange(half, device = times.device, dtype = torch.float64) / half)
+ phase = times.double()[:, None] * freq
+ embedding = torch.cat((phase.cos(), phase.sin()), -1).float()
+ time = self.time_embedding(embedding.to(_compute_dtype(self.time_embedding[0]))).float()
+ mod = self.shared_block_modulation(
+ time.to(_compute_dtype(self.shared_block_modulation))
+ ).view(batch, 6, -1)
+ head = self.video_head_modulation(time.to(_compute_dtype(self.video_head_modulation))).view(
+ batch, 2, -1
+ )
+ return mod, head
+
+ def forward(
+ self,
+ hidden_states,
+ timestep,
+ encoder_hidden_states = None,
+ encoder_attention_mask = None,
+ return_dict = True,
+ skip_layers = None,
+ refined_text = None,
+ ):
+ """``refined_text`` ([B, L, dim], from ``refine_text``) replaces ``encoder_hidden_states`` and skips the text
+ refiner; give exactly one. A mask applies to ``encoder_hidden_states`` only (refined text is unpadded)."""
+ skip_layers = _validate_skip_layers(skip_layers, len(self.layers))
+ if hidden_states.ndim != 4 or hidden_states.shape[1] != self.config.latent_channels:
+ raise ValueError("hidden_states must be [B, latent_channels, H, W].")
+ batch, _, height, width = hidden_states.shape
+ if (encoder_hidden_states is None) == (refined_text is None):
+ raise ValueError("Give exactly one of encoder_hidden_states and refined_text.")
+ text_in = encoder_hidden_states if refined_text is None else refined_text
+ if text_in.ndim != 3 or text_in.shape[0] != batch:
+ raise ValueError("encoder_hidden_states must be [B, S, text_dim].")
+ mask = encoder_attention_mask
+ if mask is not None:
+ if refined_text is not None:
+ raise ValueError(
+ "refined_text is unpadded; pass no encoder_attention_mask with it."
+ )
+ # The reference checks a mask on every call; it costs a host sync, so only a mask that is given is checked.
+ if mask.shape != text_in.shape[:2] or not torch.all((mask == 0) | (mask == 1)):
+ raise ValueError("encoder_attention_mask must be a binary [B, S] mask.")
+ device = hidden_states.device
+ mod, head = self._time_modulation(timestep, batch, device)
+ dtype = self._activation_dtype()
+ outputs = []
+ for index in range(batch):
+ if mask is None:
+ context = text_in[index : index + 1]
+ else:
+ context = text_in[index : index + 1, mask[index].bool()]
+ length = context.shape[1]
+ if length == 0:
+ raise ValueError("Each sample needs at least one valid text token.")
+ video = hidden_states[index : index + 1].flatten(2).transpose(1, 2)
+ video = self.video_embedder(video.to(_compute_dtype(self.video_embedder)).contiguous())
+ # The reference casts RoPE to the video activation dtype.
+ full_tables, text_tables = self.rope.tables(height, width, length, device, video.dtype)
+ text = self.text_embedder(context, text_tables) if refined_text is None else context
+ x = torch.cat((video, text.to(video.dtype)), 1)
+ sample_mod = mod[index : index + 1]
+ for layer_index, layer in enumerate(self.layers):
+ if layer_index not in skip_layers:
+ x = layer(x, full_tables, sample_mod)
+ x = self.final_norm_video(x[:, : height * width].to(self.final_norm_video.weight.dtype))
+ x = x * (1 + head[index : index + 1, 1, None]) + head[index : index + 1, 0, None]
+ x = self.final_linear_video(x.to(_compute_dtype(self.final_linear_video)))
+ outputs.append(x.transpose(1, 2).reshape(1, -1, height, width))
+ sample = outputs[0] if batch == 1 else torch.cat(outputs)
+ if not return_dict:
+ return (sample,)
+ from diffusers.models.modeling_outputs import Transformer2DModelOutput
+
+ return Transformer2DModelOutput(sample = sample)
+
+
+from diffusers import FlowMatchEulerDiscreteScheduler # noqa: E402
+
+
+def _time_snr_shift(shift: float, t):
+ if shift == 1.0:
+ return t
+ return shift * t / (1 + (shift - 1) * t)
+
+
+def sgm_uniform_sigmas(
+ steps: int,
+ shift: float,
+ num_train_timesteps: int = 1000,
+) -> list:
+ """ComfyUI ``sgm_uniform`` over ``ModelSamplingDiscreteFlow`` (multiplier 1), in its fp32 ops, without the
+ terminal zero: ``linspace(sigma_max, sigma_min, steps + 1)[:-1]`` re-shifted, as ``comfy.samplers.normal_scheduler``
+ with ``sgm=True``."""
+ grid = _time_snr_shift(shift, torch.arange(1, num_train_timesteps + 1, 1) / num_train_timesteps)
+ sigma_min, sigma_max = grid[0], grid[-1]
+ timesteps = torch.linspace(sigma_max, sigma_min, steps + 1)[:-1]
+ return [float(_time_snr_shift(shift, ts)) for ts in timesteps]
+
+
+def training_grid_sigmas(
+ steps: int,
+ shift: float,
+ training_shift: float,
+ total: int = 1000,
+) -> list:
+ """The reference pipeline's schedule: shifted linspace snapped to the nearest of the training grid's sigmas."""
+
+ def grid(count, s):
+ lower = s / (total + s - 1)
+ values = torch.linspace(1, lower, count)
+ return s * values / (1 + (s - 1) * values)
+
+ sigmas = grid(steps, shift)
+ training = grid(total, training_shift).flip(0)
+ upper = torch.searchsorted(training, sigmas).clamp(max = total - 1)
+ lower = (upper - 1).clamp(min = 0)
+ nearest = torch.where(
+ (sigmas - training[lower]).abs() <= (training[upper] - sigmas).abs(), lower, upper
+ )
+ return torch.unique_consecutive(training[nearest]).tolist()
+
+
+def default_sigma_schedule() -> str:
+ raw = (os.environ.get(SIGMAS_ENV) or "").strip().lower()
+ return raw if raw in SIGMA_SCHEDULES else "sgm_uniform"
+
+
+class MOSSImageScheduler(FlowMatchEulerDiscreteScheduler):
+ """Flow-matching Euler at shift 5. ``sigma_schedule``: ``sgm_uniform`` (ComfyUI, the Studio default) or
+ ``training_grid`` (the reference pipeline's nearest-training-sigma snapping); None reads ``UNSLOTH_MOSS_SIGMAS``."""
+
+ @register_to_config
+ def __init__(
+ self,
+ num_train_timesteps = 1000,
+ shift = 5.0,
+ training_shift = 1.0,
+ sigma_schedule = None,
+ ):
+ super().__init__(num_train_timesteps = num_train_timesteps, shift = 1.0)
+ self.register_to_config(
+ shift = shift, training_shift = training_shift, sigma_schedule = sigma_schedule
+ )
+
+ def resolved_sigma_schedule(self) -> str:
+ chosen = getattr(self.config, "sigma_schedule", None)
+ return chosen if chosen in SIGMA_SCHEDULES else default_sigma_schedule()
+
+ def set_timesteps(
+ self,
+ num_inference_steps = 50,
+ device = None,
+ sigmas = None,
+ **kwargs,
+ ):
+ if sigmas is None:
+ if num_inference_steps is None or num_inference_steps < 1:
+ raise ValueError("Steps and shifts must be positive.")
+ if self.config.shift <= 0 or self.config.training_shift <= 0:
+ raise ValueError("Steps and shifts must be positive.")
+ if self.resolved_sigma_schedule() == "training_grid":
+ sigmas = training_grid_sigmas(
+ num_inference_steps,
+ self.config.shift,
+ self.config.training_shift,
+ self.config.num_train_timesteps,
+ )
+ else:
+ sigmas = sgm_uniform_sigmas(
+ num_inference_steps, self.config.shift, self.config.num_train_timesteps
+ )
+ # Already shifted: no second shift.
+ self._shift = 1.0
+ super().set_timesteps(device = device, sigmas = list(sigmas))
+
+
+from diffusers import DiffusionPipeline # noqa: E402
+
+
+def _offloaded(module: Any) -> bool:
+ """Accelerate / diffusers offload hooks move weights per call: the pre-refined text path calls a submodule outside
+ the forward, so an offloaded transformer keeps the reference per-step refine."""
+ if module is None:
+ return True
+ for sub in module.modules():
+ if getattr(sub, "_hf_hook", None) is not None:
+ return True
+ registry = getattr(sub, "_diffusers_hook", None)
+ hooks = getattr(registry, "hooks", None) or {}
+ if hooks:
+ return True
+ return False
+
+
+def comfy_noise(shape, generator, device):
+ """Initial fp32 noise as ComfyUI draws it: ``torch.randn`` on the CPU from the seed (``comfy.sample.prepare_noise``).
+
+ A CPU generator is used as given (the reference pipeline's ``randn_tensor`` draws a CPU generator's noise on the
+ CPU too, so the two agree bit for bit); an accelerator generator contributes only its seed, so a Studio seed renders
+ what ComfyUI renders for it. A list holds one generator per image; None draws from the global CPU stream."""
+ gens = list(generator) if isinstance(generator, (list, tuple)) else [generator]
+
+ def _cpu(g):
+ if g is None or g.device.type == "cpu":
+ return g
+ return torch.Generator("cpu").manual_seed(int(g.initial_seed()))
+
+ if len(gens) == 1:
+ noise = torch.randn(shape, generator = _cpu(gens[0]), dtype = torch.float32)
+ else:
+ if len(gens) != shape[0]:
+ raise ValueError(
+ f"Got {len(gens)} generators for {shape[0]} images; pass one per image."
+ )
+ row = (1, *shape[1:])
+ noise = torch.cat([torch.randn(row, generator = _cpu(g), dtype = torch.float32) for g in gens])
+ return noise.to(device)
+
+
+class MOSSImagePipeline(DiffusionPipeline):
+ model_cpu_offload_seq = "text_encoder->transformer->vae"
+ _optional_components = ["text_encoder", "tokenizer", "vae"]
+ _callback_tensor_inputs = ["latents"]
+
+ def __init__(
+ self,
+ transformer,
+ scheduler,
+ text_encoder = None,
+ tokenizer = None,
+ vae = None,
+ ):
+ super().__init__()
+ self.register_modules(
+ transformer = transformer,
+ scheduler = scheduler,
+ text_encoder = text_encoder,
+ tokenizer = tokenizer,
+ vae = vae,
+ )
+ from diffusers.image_processor import VaeImageProcessor
+
+ self.image_processor = VaeImageProcessor(vae_scale_factor = 16)
+ self._interrupt = False
+
+ @property
+ def interrupt(self) -> bool:
+ return self._interrupt
+
+ @torch.no_grad()
+ def encode_prompt(
+ self,
+ prompt,
+ max_sequence_length = 4096,
+ device = None,
+ ):
+ """``(embeds [B, L, C], mask [B, L] bool)``: each prompt through the chat template, encoded unpadded on its own
+ (Qwen3.5's gated-delta layers see no padding), then right-padded into one batch."""
+ if self.text_encoder is None or self.tokenizer is None:
+ raise ValueError("Text encoder/tokenizer required for string prompts.")
+ prompts = [prompt] if isinstance(prompt, str) else list(prompt)
+ if not prompts or not all(isinstance(p, str) for p in prompts):
+ raise ValueError("prompt must be a string or a nonempty list of strings.")
+ device = device or self._execution_device
+ rows = []
+ cache: dict = {}
+ for value in prompts:
+ if value in cache:
+ rows.append(cache[value])
+ continue
+ formatted = self.tokenizer.apply_chat_template(
+ [{"role": "user", "content": value}],
+ tokenize = False,
+ add_generation_prompt = True,
+ enable_thinking = True,
+ )
+ inputs = self.tokenizer(
+ formatted, return_tensors = "pt", truncation = True, max_length = max_sequence_length
+ ).to(device)
+ output = self.text_encoder(**inputs, use_cache = False, return_dict = True)
+ cache[value] = output.last_hidden_state[0]
+ rows.append(cache[value])
+ length = max(row.shape[0] for row in rows)
+ embeds = rows[0].new_zeros(len(rows), length, rows[0].shape[-1])
+ mask = torch.zeros(len(rows), length, device = embeds.device, dtype = torch.bool)
+ for index, row in enumerate(rows):
+ embeds[index, : row.shape[0]] = row
+ mask[index, : row.shape[0]] = True
+ return embeds, mask
+
+ def _branch(self, embeds, mask, refine: bool) -> dict:
+ """Transformer kwargs for one CFG branch. Equal-length rows need no mask (no host sync in the forward); with
+ ``refine`` the text refiner runs here, once for the whole render."""
+ full = mask is None or bool(mask.all())
+ kwargs: dict = {}
+ if full and refine:
+ kwargs["refined_text"] = self.transformer.refine_text(embeds)
+ elif full:
+ kwargs["encoder_hidden_states"] = embeds
+ else:
+ kwargs["encoder_hidden_states"] = embeds
+ kwargs["encoder_attention_mask"] = mask
+ return kwargs
+
+ @torch.no_grad()
+ def __call__(
+ self,
+ prompt = None,
+ *,
+ height = 1024,
+ width = 1024,
+ num_inference_steps = 50,
+ guidance_scale = 3.0,
+ negative_prompt = "",
+ num_images_per_prompt = 1,
+ generator = None,
+ latents = None,
+ prompt_embeds = None,
+ prompt_attention_mask = None,
+ negative_prompt_embeds = None,
+ negative_prompt_attention_mask = None,
+ max_sequence_length = 4096,
+ output_type = "pil",
+ return_dict = True,
+ uncond_skip_layers = DEFAULT_UNCOND_SKIP_LAYERS,
+ slg_layers = None,
+ slg_scale = 0.0,
+ slg_start = 0.0,
+ slg_stop = 1.0,
+ sigmas = None,
+ callback_on_step_end: Optional[Callable] = None,
+ callback_on_step_end_tensor_inputs = ("latents",),
+ ):
+ """Generate images. ``uncond_skip_layers`` (default block 22) skips those zero-based blocks in the negative
+ branch only (two forwards per step); None / () disables it. ``slg_layers`` with ``slg_scale > 0`` adds a third,
+ skipped positive forward and the fp32 correction ``slg_scale * (cond - cond_skip)``. Both are active where
+ ``slg_start <= step_index / num_steps < slg_stop``."""
+ self._interrupt = False
+ depth = len(self.transformer.layers)
+ uncond_skip = _validate_skip_layers(uncond_skip_layers, depth, "uncond_skip_layers")
+ slg = _validate_skip_layers(slg_layers, depth, "slg_layers")
+ # Tuples, not frozensets: a CUDA graph keys plain values, not objects.
+ uncond_skip_kw = tuple(sorted(uncond_skip))
+ slg_kw = tuple(sorted(slg))
+ if not math.isfinite(slg_scale) or slg_scale < 0:
+ raise ValueError("slg_scale must be finite and nonnegative.")
+ if not 0 <= slg_start <= slg_stop <= 1:
+ raise ValueError("Require 0 <= slg_start <= slg_stop <= 1.")
+ if height <= 0 or width <= 0 or height % 16 or width % 16:
+ raise ValueError("height and width must be positive multiples of 16.")
+ if output_type not in {"pil", "np", "pt", "latent"}:
+ raise ValueError("output_type must be pil, np, pt or latent.")
+ if num_images_per_prompt < 1 or not math.isfinite(guidance_scale) or guidance_scale < 0:
+ raise ValueError("Invalid image count or guidance scale.")
+ if not 1 <= max_sequence_length <= 4096:
+ raise ValueError("max_sequence_length must be between 1 and 4096.")
+ if (prompt is None) == (prompt_embeds is None):
+ raise ValueError("Provide exactly one of prompt and prompt_embeds.")
+ device = self._execution_device
+ if prompt_embeds is None:
+ prompt_embeds, prompt_attention_mask = self.encode_prompt(
+ prompt, max_sequence_length, device
+ )
+ batch = prompt_embeds.shape[0]
+
+ def prepare(embeds, mask):
+ if embeds.ndim != 3 or embeds.shape[0] != batch:
+ raise ValueError("Prompt embeddings must be [B, S, C] with matching batch size.")
+ if mask is None:
+ mask = torch.ones(embeds.shape[:2], dtype = torch.bool, device = embeds.device)
+ return (
+ embeds.to(device).repeat_interleave(num_images_per_prompt, 0),
+ mask.to(device).repeat_interleave(num_images_per_prompt, 0),
+ )
+
+ positive, positive_mask = prepare(prompt_embeds, prompt_attention_mask)
+ do_cfg = guidance_scale != 1.0
+ if do_cfg:
+ if negative_prompt_embeds is None:
+ negative_prompt = "" if negative_prompt is None else negative_prompt
+ negatives = (
+ [negative_prompt] * batch
+ if isinstance(negative_prompt, str)
+ else list(negative_prompt)
+ )
+ if len(negatives) != batch:
+ raise ValueError("negative_prompt must match prompt batch size.")
+ negative_prompt_embeds, negative_prompt_attention_mask = self.encode_prompt(
+ negatives, max_sequence_length, device
+ )
+ negative, negative_mask = prepare(
+ negative_prompt_embeds, negative_prompt_attention_mask
+ )
+ shape = (
+ batch * num_images_per_prompt,
+ self.transformer.config.latent_channels,
+ height // 16,
+ width // 16,
+ )
+ if latents is None:
+ latents = comfy_noise(shape, generator, device)
+ elif tuple(latents.shape) != shape:
+ raise ValueError(f"latents must have shape {shape}.")
+ else:
+ latents = latents.to(device = device, dtype = torch.float32)
+
+ refine = not _env_off(TEXT_CACHE_ENV) and not _offloaded(self.transformer)
+ cond_kw = self._branch(positive, positive_mask, refine)
+ uncond_kw = self._branch(negative, negative_mask, refine) if do_cfg else None
+
+ if sigmas is not None:
+ self.scheduler.set_timesteps(device = device, sigmas = list(sigmas))
+ else:
+ self.scheduler.set_timesteps(num_inference_steps, device = device)
+ timesteps = self.scheduler.timesteps
+ num_steps = len(timesteps)
+ for step_index, timestep in enumerate(self.progress_bar(timesteps)):
+ if self._interrupt:
+ continue
+ skip_active = slg_start <= step_index / num_steps < slg_stop
+ # CFG combines fp32 predictions, even when the DiT runs in bf16.
+ # return_dict=False: a CUDA-graph replay hands back a tuple, never a dataclass.
+ prediction = self.transformer(latents, timestep, **cond_kw, return_dict = False)[
+ 0
+ ].float()
+ conditional = prediction
+ if do_cfg:
+ skip_kwargs = (
+ {"skip_layers": uncond_skip_kw} if uncond_skip_kw and skip_active else {}
+ )
+ uncond = self.transformer(
+ latents, timestep, **uncond_kw, **skip_kwargs, return_dict = False
+ )[0].float()
+ prediction = uncond + guidance_scale * (prediction - uncond)
+ if slg_kw and slg_scale > 0 and skip_active:
+ skipped = self.transformer(
+ latents, timestep, **cond_kw, skip_layers = slg_kw, return_dict = False
+ )[0].float()
+ if not do_cfg:
+ prediction = skipped
+ prediction = prediction + slg_scale * (conditional - skipped)
+ latents = self.scheduler.step(prediction, timestep, latents, return_dict = False)[0]
+ if callback_on_step_end is not None:
+ # A loop, not a comprehension: before Python 3.12 a comprehension's locals() is its own scope.
+ callback_kwargs = {}
+ for name in callback_on_step_end_tensor_inputs:
+ callback_kwargs[name] = locals()[name]
+ callback_outputs = callback_on_step_end(self, step_index, timestep, callback_kwargs)
+ if callback_outputs is not None:
+ latents = callback_outputs.pop("latents", latents)
+ if output_type == "latent":
+ images = latents
+ else:
+ if self.vae is None:
+ raise ValueError("A FLUX.2 VAE is required for decoded output.")
+ images = self.vae_decode(latents)
+ images = self.image_processor.postprocess(images, output_type = output_type)
+ self.maybe_free_model_hooks()
+ from diffusers.pipelines.pipeline_utils import ImagePipelineOutput
+
+ return ImagePipelineOutput(images = images) if return_dict else (images,)
+
+ def vae_decode(self, latents):
+ """[B, 128, h, w] transformer latents -> [B, 3, 16h, 16w] in [-1, 1]: undo the VAE BatchNorm statistics, unpack
+ the 2x2 patches to 32 channels, decode in the VAE's dtype (fp32 by default)."""
+ z = latents.to(dtype = self.vae.dtype)
+ bn = self.vae.bn
+ eps = self.vae.config.batch_norm_eps
+ z = z * (bn.running_var + eps).sqrt().to(z).view(1, -1, 1, 1) + bn.running_mean.to(z).view(
+ 1, -1, 1, 1
+ )
+ b, c, h, w = z.shape
+ z = (
+ z.reshape(b, c // 4, 2, 2, h, w)
+ .permute(0, 1, 4, 2, 5, 3)
+ .reshape(b, c // 4, 2 * h, 2 * w)
+ )
+ return self.vae.decode(z, return_dict = False)[0].clamp(-1, 1)
+
+
+def transformers_version_ok() -> tuple[bool, Optional[str]]:
+ """``(new enough, installed version)`` for the Qwen3.5 text encoder."""
+ try:
+ from importlib.metadata import version
+ installed = version("transformers")
+ except Exception: # noqa: BLE001 - unreadable metadata: let the import decide
+ return True, None
+ try:
+ from packaging.version import Version
+ return Version(Version(installed).base_version) >= Version(MIN_TRANSFORMERS), installed
+ except Exception: # noqa: BLE001
+ return True, installed
+
+
+def transformers_refusal(installed: Optional[str]) -> str:
+ return (
+ f"MOSS-Image needs transformers >= {MIN_TRANSFORMERS} for its Qwen3.5 text encoder "
+ f"(transformers.Qwen3_5Model), but this environment has transformers {installed or 'unknown'}. "
+ "Update Unsloth Studio (or run: pip install -U 'transformers>=5.3') and restart it."
+ )
+
+
+def assert_transformers_supported() -> None:
+ ok, installed = transformers_version_ok()
+ if not ok:
+ raise ValueError(transformers_refusal(installed))
+ try:
+ from transformers import Qwen3_5Model # noqa: F401
+ except Exception: # noqa: BLE001
+ raise ValueError(transformers_refusal(installed)) from None
+
+
+def _live_cache_dir() -> Optional[str]:
+ try:
+ from utils.hf_cache_settings import active_hf_hub_cache
+ return active_hf_hub_cache()
+ except Exception: # noqa: BLE001 - outside the Studio tree the hub default applies
+ return None
+
+
+def _tokenizer_class(repo_id: str, common: dict[str, Any]) -> Any:
+ """The tokenizer class ``tokenizer/tokenizer_config.json`` names (Qwen2Tokenizer), else ``AutoTokenizer``.
+ transformers 5.x's ``AutoTokenizer`` on a repo id first reads a ROOT ``config.json``, which these repos do not
+ ship, so an offline load from the cache failed before reaching the tokenizer files."""
+ import json
+
+ import transformers
+
+ try:
+ if os.path.isdir(str(repo_id)):
+ path = os.path.join(str(repo_id), "tokenizer", "tokenizer_config.json")
+ else:
+ from huggingface_hub import hf_hub_download
+ path = hf_hub_download(
+ repo_id,
+ "tokenizer_config.json",
+ subfolder = "tokenizer",
+ cache_dir = common.get("cache_dir"),
+ token = common.get("token"),
+ local_files_only = bool(common.get("local_files_only")),
+ )
+ with open(path, encoding = "utf-8") as fh:
+ name = json.load(fh).get("tokenizer_class")
+ except Exception: # noqa: BLE001 - AutoTokenizer reports the real error
+ name = None
+ cls = getattr(transformers, str(name), None) if name else None
+ return cls if isinstance(cls, type) else transformers.AutoTokenizer
+
+
+def drop_vision_tower(text_encoder: Any) -> bool:
+ """Free Qwen3.5's vision tower (~0.4 B params): MOSS feeds the encoder text only, and the text forward never touches
+ it (images enter only through ``pixel_values``). True when it was dropped."""
+ visual = getattr(text_encoder, "visual", None)
+ if not isinstance(visual, nn.Module):
+ return False
+ try:
+ text_encoder.visual = None
+ except Exception: # noqa: BLE001 - a frozen module keeps its tower
+ return False
+ return True
+
+
+def _conv_offload_hook(conv: Any) -> Any:
+ registry = getattr(conv, "_diffusers_hook", None)
+ get = getattr(registry, "get_hook", None)
+ return get("group_offloading") if callable(get) else None
+
+
+def _gdn_conv_onload(module: Any, args: Any) -> None:
+ hook = _conv_offload_hook(module.conv1d)
+ if hook is None:
+ return
+ hook.pre_forward(module.conv1d)
+ stream = getattr(getattr(hook, "group", None), "stream", None)
+ if stream is not None:
+ stream.synchronize()
+
+
+def _gdn_conv_offload(module: Any, args: Any, output: Any) -> None:
+ hook = _conv_offload_hook(module.conv1d)
+ if hook is not None:
+ hook.post_forward(module.conv1d, None)
+
+
+def install_gdn_offload_guard(text_encoder: Any) -> int:
+ """Qwen3.5's gated-delta layers read ``self.conv1d.weight`` straight into the causal-conv kernel and never call
+ ``conv1d``, so a leaf-level group offload (Studio's text-encoder streaming) never onloads that weight. Hooks on each
+ gated-delta layer onload its conv for the layer's forward and offload it after; without a group-offload hook on the
+ conv they do nothing. Returns the number of layers guarded."""
+ count = 0
+ for module in text_encoder.modules():
+ if not type(module).__name__.endswith("GatedDeltaNet") or not isinstance(
+ getattr(module, "conv1d", None), nn.Module
+ ):
+ continue
+ if getattr(module, "_unsloth_conv_guard", False):
+ continue
+ module.register_forward_pre_hook(_gdn_conv_onload)
+ module.register_forward_hook(_gdn_conv_offload)
+ module._unsloth_conv_guard = True
+ count += 1
+ return count
+
+
+def load_moss_image_text_encoder(source: str, dtype, **common):
+ from transformers import Qwen3_5Model
+
+ text_encoder = Qwen3_5Model.from_pretrained(
+ source, subfolder = "text_encoder", dtype = dtype, **common
+ )
+ drop_vision_tower(text_encoder)
+ install_gdn_offload_guard(text_encoder)
+ return text_encoder.eval()
+
+
+def load_moss_image_pipeline(
+ repo_id: str,
+ dtype,
+ hf_token: Optional[str] = None,
+ transformer: Any = None,
+ text_encoder: Any = None,
+ local_files_only: bool = False,
+ check_cancelled: Optional[Callable[[], None]] = None,
+ vae_dtype: Any = None,
+) -> MOSSImagePipeline:
+ """A ready ``MOSSImagePipeline`` for ``repo_id`` (a repo id or a local diffusers folder), on CPU.
+
+ Loads safetensors and JSON only, per subfolder; the repo's ``pipeline_moss_image.py`` is never imported.
+ ``transformer`` / ``text_encoder`` let a quantised or pre-cast build hand in a component. The VAE decodes in fp32
+ unless ``vae_dtype`` says otherwise (the reference and ComfyUI's MOSS launcher both decode in fp32)."""
+ check_cancelled = check_cancelled or (lambda: None)
+ check_cancelled()
+ assert_transformers_supported()
+ import diffusers
+
+ common: dict[str, Any] = {"local_files_only": local_files_only}
+ cache_dir = _live_cache_dir()
+ if cache_dir and not os.path.isdir(str(repo_id)):
+ common["cache_dir"] = cache_dir
+ if hf_token:
+ common["token"] = hf_token
+ tokenizer = _tokenizer_class(repo_id, common).from_pretrained(
+ repo_id, subfolder = "tokenizer", **common
+ )
+ check_cancelled()
+ if text_encoder is None:
+ text_encoder = load_moss_image_text_encoder(repo_id, dtype, **common)
+ check_cancelled()
+ else:
+ # A pre-cast build (the hosted int8 ConvRot encoder) carries the vision tower and needs the same guard.
+ drop_vision_tower(text_encoder)
+ install_gdn_offload_guard(text_encoder)
+ if transformer is None:
+ transformer = MOSSImageTransformer.from_pretrained(
+ repo_id, subfolder = "transformer", torch_dtype = dtype, **common
+ )
+ check_cancelled()
+ transformer.eval()
+ vae = diffusers.AutoencoderKLFlux2.from_pretrained(
+ repo_id, subfolder = "vae", torch_dtype = vae_dtype or torch.float32, **common
+ )
+ check_cancelled()
+ scheduler = MOSSImageScheduler.from_pretrained(repo_id, subfolder = "scheduler", **common)
+ logger.info("diffusion.moss_image: assembled pipeline from %s per component", repo_id)
+ return MOSSImagePipeline(
+ transformer = transformer,
+ scheduler = scheduler,
+ text_encoder = text_encoder,
+ tokenizer = tokenizer,
+ vae = vae,
+ )
+
+
+def in_tree_class(name: str) -> Optional[type]:
+ """The in-tree class for a diffusers-style class name this module defines, else None."""
+ return {
+ MOSS_IMAGE_PIPELINE_CLASS: MOSSImagePipeline,
+ MOSS_IMAGE_TRANSFORMER_CLASS: MOSSImageTransformer,
+ MOSS_IMAGE_SCHEDULER_CLASS: MOSSImageScheduler,
+ }.get(name)
diff --git a/studio/backend/core/inference/diffusion_precision.py b/studio/backend/core/inference/diffusion_precision.py
index 00b7d28ea47..f0a0f49d793 100644
--- a/studio/backend/core/inference/diffusion_precision.py
+++ b/studio/backend/core/inference/diffusion_precision.py
@@ -699,6 +699,10 @@ def _cast_fp8(encoder: Any, target: Any) -> None:
if tied_name:
skip += (rf"^{re.escape(tied_name)}$",)
+ # (3) Qwen3.5 / Qwen3-Next gated-delta layers read ``linear_attn.conv1d.weight`` directly into the causal-conv
+ # kernel (causal-conv1d / fla installed) and never call the module, so the upcast hook would not run. Tiny tensor.
+ skip += (r"(^|\.)linear_attn\.conv1d$",)
+
apply_layerwise_casting(
encoder,
storage_dtype = torch.float8_e4m3fn,
diff --git a/studio/backend/core/inference/diffusion_preview.py b/studio/backend/core/inference/diffusion_preview.py
index 6b0fc7a0748..682c6c064b4 100644
--- a/studio/backend/core/inference/diffusion_preview.py
+++ b/studio/backend/core/inference/diffusion_preview.py
@@ -97,6 +97,9 @@ def latent_grid(latents: Any, spec: LatentRGB, height: int, width: int) -> Any:
"""First image / frame as a (gh, gw, D) view, or None. Packed tokens take their grid from the render
size, which must tile the sequence exactly."""
if spec.layout == "tokens":
+ if latents.ndim == 4 and latents.shape[1] == spec.channels:
+ # The same packed channels kept spatial (MOSS-Image runs FLUX.2 latents as [B, 128, h, w]).
+ return latents[0].permute(1, 2, 0)
gh, gw = int(height) // spec.down, int(width) // spec.down
if latents.ndim != 3 or latents.shape[-1] != spec.channels or gh <= 0 or gw <= 0:
return None
diff --git a/studio/backend/core/inference/diffusion_te_prequant.py b/studio/backend/core/inference/diffusion_te_prequant.py
index 80792a0be25..64b687466bd 100644
--- a/studio/backend/core/inference/diffusion_te_prequant.py
+++ b/studio/backend/core/inference/diffusion_te_prequant.py
@@ -150,6 +150,13 @@ TE_INT8_CONVROT_FILES: dict[str, dict[str, tuple[str, ...]]] = {
"flux-3-image-klein-base-text_encoder-INT8-ConvRot.safetensors",
),
},
+ # MOSS-Image Qwen3.5-9B: MLP + full-attention Linears int8; gated-delta, embeddings, vision stay bf16.
+ "moss-image": {
+ "text_encoder": (
+ "unsloth/MOSS-Image-SFT-FP8",
+ "MOSS-Image-SFT-text_encoder-INT8-ConvRot.safetensors",
+ ),
+ },
}
# family -> lowercased variant base -> {component: (repo_id, filename)}, for a variant whose encoder is a different
@@ -298,6 +305,8 @@ _TE_INT8_CONVROT_BUDGET_SCALES = {
"krea-2": 0.60,
"flux.3-klein": 0.60,
"flux.3-klein-base": 0.60,
+ # MOSS-Image's 11,488,171,720-byte file against its 15.9 GB table price (0.723), rounded up.
+ "moss-image": 0.73,
**{
name: 0.62
for name in ("wan2.2-ti2v-5b", "wan2.2-t2v-a14b", "wan2.1-t2v-1.3b", "wan2.1-t2v-14b")
@@ -656,6 +665,13 @@ _TE_EQUIVALENT_BASES: tuple[frozenset[str], ...] = (
"black-forest-labs/flux.2-klein-base-9b",
}
),
+ # MOSS-Image SFT and Base share identical text_encoder shards (LFS sha256).
+ frozenset(
+ {
+ "openmoss-team/moss-image-sft",
+ "openmoss-team/moss-image-base",
+ }
+ ),
)
diff --git a/studio/backend/core/inference/diffusion_text_encoder_trim.py b/studio/backend/core/inference/diffusion_text_encoder_trim.py
index 6b58487ff58..25357317236 100644
--- a/studio/backend/core/inference/diffusion_text_encoder_trim.py
+++ b/studio/backend/core/inference/diffusion_text_encoder_trim.py
@@ -54,6 +54,7 @@ _HIDDEN_STATE_ONLY_FAMILIES = frozenset(
# FLUX.3 Klein stacks hidden_states[4..32] (diffusers Flux3TextEncoderStep)
"flux.3-klein",
"flux.3-klein-base",
+ "moss-image",
}
)
diff --git a/studio/backend/core/inference/diffusion_transformer_quant.py b/studio/backend/core/inference/diffusion_transformer_quant.py
index cef4afba60a..fc04fda7a51 100644
--- a/studio/backend/core/inference/diffusion_transformer_quant.py
+++ b/studio/backend/core/inference/diffusion_transformer_quant.py
@@ -410,6 +410,8 @@ _FAMILY_SCHEME_DENY: dict[str, frozenset[str]] = _profile_table("scheme_deny", q
# ``FamilyProfile.auto_prefer``: a family's head of the ``auto`` order, ahead of ``_AUTO_LADDER``.
_FAMILY_AUTO_PREFER: dict[str, _AutoPrefer] = _profile_table("auto_prefer", quant = True)
+_FAMILY_AUTO_SKIP: dict[str, frozenset[str]] = _profile_table("auto_scheme_skip", quant = True)
+
# AUTO keeps bf16 while it fits for these (``FamilyProfile.auto_bf16_when_resident``: measured with both sides compiled,
# INT8 was at most ~5% faster and failed the default-on LPIPS bar); when bf16 would offload, INT8 is still picked.
@@ -1217,9 +1219,10 @@ def _auto_scheme_order(
if prefer.backend is None or _nvfp4_backend_is(device, prefer.backend):
head = prefer.schemes
order: list[str] = []
+ skip = _FAMILY_AUTO_SKIP.get(str(family or "").strip().lower(), frozenset())
# Filtered here so the NVFP4 switch reaches every reader of the order.
for scheme in without_nvfp4(head + tier):
- if scheme not in order:
+ if scheme not in order and scheme not in skip:
order.append(scheme)
return tuple(order)
diff --git a/studio/backend/core/inference/family_profile.py b/studio/backend/core/inference/family_profile.py
index 73227f23230..316a3fd0ce4 100644
--- a/studio/backend/core/inference/family_profile.py
+++ b/studio/backend/core/inference/family_profile.py
@@ -85,6 +85,8 @@ class FamilyProfile:
# Extra schemes denied for TRAINING on top of ``scheme_deny`` (nvfp4 is denied for training everywhere).
train_scheme_deny: frozenset[str] = frozenset()
auto_prefer: Optional[AutoPrefer] = None
+ # Schemes ``auto`` skips for this family (explicit requests still allowed).
+ auto_scheme_skip: frozenset[str] = frozenset()
# Reason auto keeps bf16 while bf16 fits (INT8 still picked once bf16 would offload); None = not this family.
auto_bf16_when_resident: Optional[str] = None
# Video: prequant DiT schemes AUTO seeds even where bf16 fits resident (else auto keeps resident bf16).
diff --git a/studio/backend/core/inference/sd_cpp_args.py b/studio/backend/core/inference/sd_cpp_args.py
index e14c50647cc..173026b8b7a 100644
--- a/studio/backend/core/inference/sd_cpp_args.py
+++ b/studio/backend/core/inference/sd_cpp_args.py
@@ -71,6 +71,7 @@ class SdCppModelFiles:
llm: Optional[str] = None
llm_vision: Optional[str] = None
qwen2vl: Optional[str] = None
+ tokenizer: Optional[str] = None
@dataclass(frozen = True)
@@ -669,6 +670,7 @@ def build_sd_cpp_command(
("--llm", files.llm),
("--llm_vision", files.llm_vision),
("--qwen2vl", files.qwen2vl),
+ ("--tokenizer", files.tokenizer),
):
if value:
cmd += [flag, value]
@@ -768,6 +770,8 @@ def build_sd_cpp_video_command(
cmd += ["--llm", files.llm]
if files.llm_vision:
cmd += ["--llm_vision", files.llm_vision]
+ if files.tokenizer:
+ cmd += ["--tokenizer", files.tokenizer]
# Reject incompatible partitions before loading the model.
if (params.init_img or params.end_img) and (
params.ref_images or params.ref_videos or params.ref_audios
@@ -906,6 +910,7 @@ def build_sd_cpp_server_command(
("--llm", files.llm),
("--llm_vision", files.llm_vision),
("--qwen2vl", files.qwen2vl),
+ ("--tokenizer", files.tokenizer),
):
if value:
cmd += [flag, value]
diff --git a/studio/backend/core/inference/sd_cpp_backend.py b/studio/backend/core/inference/sd_cpp_backend.py
index 5e9ba213ebd..b2dc45bd716 100644
--- a/studio/backend/core/inference/sd_cpp_backend.py
+++ b/studio/backend/core/inference/sd_cpp_backend.py
@@ -3551,6 +3551,7 @@ class SdCppDiffusionBackend:
llm = paths.get("llm"),
llm_vision = paths.get("llm_vision"),
qwen2vl = paths.get("qwen2vl"),
+ tokenizer = paths.get("tokenizer"),
)
device = (
off_torch.accelerator
@@ -4171,6 +4172,9 @@ class SdCppDiffusionBackend:
if offline:
tefile = _cached_text_encoder_alternative(terepo, tefile, kind) or tefile
specs.append((terepo, tefile, kind))
+ tokenizer = getattr(fam, "sd_cpp_tokenizer", None)
+ if tokenizer:
+ specs.append((tokenizer[0], tokenizer[1], "tokenizer"))
return specs
def _set_expected_bytes(
@@ -4337,6 +4341,8 @@ class SdCppDiffusionBackend:
repos = [state.repo_id, state.base_repo]
if fam.sd_cpp_vae:
repos.append(fam.sd_cpp_vae[0])
+ if getattr(fam, "sd_cpp_tokenizer", None):
+ repos.append(fam.sd_cpp_tokenizer[0])
# Same per-variant selection as _asset_specs (the header dim this load committed, else repo id AND GGUF
# filename) so the delete guard protects the encoder repo this load actually downloaded. Read off the
# state, never re-probed: this runs under _lock.
diff --git a/studio/backend/hub/utils/comfy_models.py b/studio/backend/hub/utils/comfy_models.py
index 2e0f40c22ef..b14fc36bb51 100644
--- a/studio/backend/hub/utils/comfy_models.py
+++ b/studio/backend/hub/utils/comfy_models.py
@@ -340,8 +340,10 @@ def loose_diffusion_checkpoints(folder: Path, *, entry_limit: Optional[int] = No
def _single_file_loadable(image_fam, video_fam, filename: str) -> bool:
"""Mirrors the loaders' single-file refusals (pipeline-only, dual-expert, unnamed MiniMax-H3)."""
- if image_fam is not None and not image_fam.pipeline_only:
- return True
+ if image_fam is not None:
+ return not (
+ image_fam.pipeline_only or getattr(image_fam, "native_transformer_files_only", False)
+ )
if video_fam is None or video_fam.is_moe:
return False
if not getattr(video_fam, "modular_workflow", None):
diff --git a/studio/backend/hub/utils/companion_assets.py b/studio/backend/hub/utils/companion_assets.py
index 3cd72d13927..3ff1ecf534c 100644
--- a/studio/backend/hub/utils/companion_assets.py
+++ b/studio/backend/hub/utils/companion_assets.py
@@ -328,6 +328,9 @@ def _sd_cpp_component_specs(
vae = getattr(fam, "sd_cpp_vae", None)
if vae and vae[0]:
specs.add((vae[0], vae[1]))
+ tokenizer = getattr(fam, "sd_cpp_tokenizer", None)
+ if tokenizer and tokenizer[0]:
+ specs.add((tokenizer[0], tokenizer[1]))
# EVERY set the family could pick: there is no header to read and a renamed FLUX.2-klein 9B file carries no size token, so guessing answers 4B and leaves the 9B encoder unprotected.
for encoder in sd_cpp_text_encoder_candidates(fam) or ():
if encoder and encoder[0]:
diff --git a/studio/backend/tests/data/moss_image/pipeline_moss_image_reference.py.txt b/studio/backend/tests/data/moss_image/pipeline_moss_image_reference.py.txt
new file mode 100644
index 00000000000..0e2966d2e67
--- /dev/null
+++ b/studio/backend/tests/data/moss_image/pipeline_moss_image_reference.py.txt
@@ -0,0 +1,443 @@
+# Reference copy of OpenMOSS-Team/MOSS-Image-SFT pipeline_moss_image.py (Apache-2.0,
+# https://huggingface.co/OpenMOSS-Team/MOSS-Image-SFT), unmodified below this header, kept as .txt so formatters leave it byte-identical. Test data only: the parity
+# tests run it as the numerical reference for core/inference/diffusion_moss_image.py; Studio never imports it.
+"""MOSSImage modeling and text-to-image pipeline (no Motor/TorchTitan imports).
+
+Dependencies: torch>=2.9, diffusers>=0.38, transformers>=5.3, safetensors,
+accelerate, Pillow. Copy this file anywhere and import it normally.
+
+ import torch
+ # Import MOSSImagePipeline from this file for component-level loading.
+ pipe = MOSSImagePipeline.from_components_pretrained(
+ transformer_path="exported_transformer", # save_pretrained directory
+ text_encoder_path="Qwen/Qwen3.5-9B",
+ vae_path="black-forest-labs/FLUX.2-klein-4B",
+ ).to("cuda")
+ pipe("A cat", generator=torch.Generator("cuda").manual_seed(42)).images[0].save("cat.png")
+
+Default guidance uses CFG 3 and skips block 22 only in the negative branch
+(zero-based main DiT block indices; two forwards per sampling step):
+ pipe("A cat", guidance_scale=3.0, uncond_skip_layers=[22])
+ pipe("A cat", uncond_skip_layers=[]) # disable negative-branch skipping
+Optional additional positive-branch SLG adds a third forward:
+ pipe("A cat", slg_layers=[24], slg_scale=3.0)
+The additional SLG layer/scale above illustrates the API, not a quality-tuned setting.
+
+For a consolidated training state dict, create MOSSImageTransformer and
+call load_training_state_dict(state), then save_pretrained("exported_transformer").
+Select raw or EMA weights explicitly before calling it; distributed checkpoint
+shards/optimizer states are not model state dicts. Defaults match the finalized dense MOSSImage model,
+not MoE, CFG-distilled, video, or reference-image variants. Prompt strings are
+already-rendered captions (dataset-specific quality/domain tags are not added).
+
+First-release defaults: bf16 DiT/text-encoder weights, fp32 RoPE frequencies
+and phase/sin/cos calculation, then RoPE cast to the video activation dtype.
+Initial Gaussian noise is sampled directly in fp32, including with bf16 weights.
+CFG and Euler latent updates use fp32; VAE decoding defaults to fp32. Scheduler
+shift defaults to 5.0. Exact numerical parity with the original fp32-master
+inference pipeline is not a release requirement.
+"""
+from __future__ import annotations
+
+import math
+from pathlib import Path
+
+import torch
+from torch import nn
+from torch.nn import functional as F
+from diffusers import AutoencoderKLFlux2, DiffusionPipeline, FlowMatchEulerDiscreteScheduler
+from diffusers.schedulers.scheduling_utils import SchedulerMixin # Required by custom-component discovery.
+from diffusers.configuration_utils import ConfigMixin, register_to_config
+from diffusers.image_processor import VaeImageProcessor
+from diffusers.models.modeling_utils import ModelMixin
+from diffusers.models.modeling_outputs import Transformer2DModelOutput
+from diffusers.pipelines.pipeline_utils import ImagePipelineOutput
+from diffusers.utils.torch_utils import randn_tensor
+from transformers import AutoModel, AutoTokenizer
+
+
+class _RMSNorm(nn.Module):
+ def __init__(self, dim):
+ super().__init__()
+ self.weight = nn.Parameter(torch.ones(dim))
+
+ def forward(self, x):
+ y = x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim=True) + 1e-6)
+ return y.to(x.dtype) * self.weight
+
+
+def _situ(hidden, gate):
+ return (4.0 * torch.tanh(gate / 4.0) * torch.sigmoid(gate)) * (25.0 * torch.tanh(hidden / 25.0))
+
+
+def _validate_skip_layers(layers, depth, name='skip_layers'):
+ layers = () if layers is None else tuple(layers)
+ if any(type(index) is not int or not 0 <= index < depth for index in layers):
+ raise ValueError(f'{name} must contain integer block indices in [0, {depth}).')
+ return frozenset(layers)
+
+
+def _rotate(x, rope):
+ sin, cos = rope.chunk(2, -1)
+ sin = torch.cat((sin, sin), -1).unsqueeze(-2)
+ cos = torch.cat((cos, cos), -1).unsqueeze(-2)
+ a, b = x.chunk(2, -1)
+ return x * cos + torch.cat((-b, a), -1) * sin
+
+
+def _attention(q, k, v, rope, heads):
+ shape = (*q.shape[:2], heads, q.shape[-1] // heads)
+ q = _rotate(q.reshape(shape), rope).transpose(1, 2)
+ k = _rotate(k.reshape(shape), rope).transpose(1, 2)
+ v = v.reshape(shape).transpose(1, 2)
+ return F.scaled_dot_product_attention(q.to(v.dtype), k.to(v.dtype), v).transpose(1, 2).flatten(2)
+
+
+class _TextBlock(nn.Module):
+ def __init__(self, dim, ffn_dim, heads):
+ super().__init__()
+ self.heads = heads
+ self.norm1, self.norm2 = _RMSNorm(dim), _RMSNorm(dim)
+ self.q_norm, self.k_norm = _RMSNorm(dim // heads), _RMSNorm(dim // heads)
+ self.qkv = nn.Linear(dim, 3 * dim)
+ self.proj = nn.Linear(dim, dim)
+ self.ffn_gate, self.ffn_up = nn.Linear(dim, ffn_dim), nn.Linear(dim, ffn_dim)
+ self.ffn_down = nn.Linear(ffn_dim, dim)
+
+ def forward(self, x, rope):
+ q, k, v = self.qkv(self.norm1(x)).chunk(3, -1)
+ q = self.q_norm(q.unflatten(-1, (self.heads, -1))).flatten(2)
+ k = self.k_norm(k.unflatten(-1, (self.heads, -1))).flatten(2)
+ x = x + self.proj(_attention(q, k, v, rope, self.heads))
+ y = self.norm2(x)
+ return x + self.ffn_down(_situ(self.ffn_up(y), self.ffn_gate(y)))
+
+
+class _TextProjection(nn.Module):
+ def __init__(self, text_dim, dim, ffn_dim, heads):
+ super().__init__()
+ self.proj = nn.Linear(text_dim, dim)
+ self.blocks = nn.ModuleList([_TextBlock(dim, ffn_dim, heads) for _ in range(2)])
+
+ def forward(self, x, rope):
+ x = self.proj(x.to(self.proj.weight.dtype))
+ for block in self.blocks:
+ x = block(x, rope)
+ return x
+
+
+class _Block(nn.Module):
+ def __init__(self, dim, ffn_dim, heads):
+ super().__init__()
+ self.heads = heads
+ self.norm1, self.norm2 = _RMSNorm(dim), _RMSNorm(dim)
+ self.q_norm, self.k_norm = _RMSNorm(dim // heads), _RMSNorm(dim // heads)
+ self.proj = nn.ModuleDict({
+ name: nn.Linear(dim, dim, bias=False) for name in ('q', 'k', 'v', 'o')
+ })
+ self.proj['ffn_up_gate'] = nn.Linear(dim, 2 * ffn_dim, bias=False)
+ self.proj['ffn_down'] = nn.Linear(ffn_dim, dim, bias=False)
+
+ def forward(self, x, rope, modulation):
+ shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = modulation.unbind(1)
+ y = self.norm1(x) * (1 + scale_a[:, None]) + shift_a[:, None]
+ dtype = self.proj['q'].weight.dtype
+ y = y.to(dtype)
+ q = self.q_norm(self.proj['q'](y).unflatten(-1, (self.heads, -1))).flatten(2)
+ k = self.k_norm(self.proj['k'](y).unflatten(-1, (self.heads, -1))).flatten(2)
+ x = x + self.proj['o'](_attention(q, k, self.proj['v'](y), rope, self.heads)) * gate_a[:, None]
+ y = self.norm2(x) * (1 + scale_f[:, None]) + shift_f[:, None]
+ hidden, gate = self.proj['ffn_up_gate'](y.to(dtype)).chunk(2, -1)
+ return x + self.proj['ffn_down'](_situ(hidden, gate)) * gate_f[:, None]
+
+
+class _RoPE(nn.Module):
+ def __init__(self, head_dim, base):
+ super().__init__()
+ self.num_bands = head_dim // 8
+ self.base = base
+ self.register_buffer('bands', self._default_bands(torch.device('cpu')))
+
+ def _default_bands(self, device):
+ # Match training's CPU fp32 construction, including under meta init.
+ exponent = torch.arange(self.num_bands, device='cpu', dtype=torch.float32) / self.num_bands
+ return (1 / (self.base ** exponent)).to(device)
+
+ def _apply(self, fn, recurse=True):
+ super()._apply(fn, recurse=recurse)
+ # .float() cannot recover frequencies already rounded by .to(bf16).
+ self.bands = self._default_bands(self.bands.device)
+ return self
+
+ def _load_from_state_dict(self, *args, **kwargs):
+ super()._load_from_state_dict(*args, **kwargs)
+ # Frequencies are derived constants; retain the key for checkpoint compatibility.
+ self.bands = self._default_bands(self.bands.device)
+
+ def forward(self, height, width, text_length, device):
+ # Diffusers/Accelerate can install tensors directly, bypassing load hooks.
+ if self.bands.dtype != torch.float32:
+ self.bands = self._default_bands(device)
+ # Coordinates: (r, f, h, w); sequence order is image followed by text.
+ h, w = torch.meshgrid(torch.arange(height, device=device), torch.arange(width, device=device), indexing='ij')
+ image = torch.stack((torch.zeros_like(h), torch.full_like(h, text_length), h, w), -1).flatten(0, 1).float()
+ image[:, 2] -= (height - 1) / 2
+ image[:, 3] -= (width - 1) / 2
+ text = torch.zeros(text_length, 4, device=device, dtype=torch.float32)
+ text[:, 1] = torch.arange(text_length, device=device)
+ phase = torch.cat((image, text), 0).unsqueeze(-1) * self.bands
+ return torch.cat((phase.sin(), phase.cos()), -2).flatten(-2).unsqueeze(0)
+
+
+class MOSSImageTransformer(ModelMixin, ConfigMixin):
+ """MOSSImage DiT with training-compatible state-dict names and SDPA attention.
+
+ Each sample is evaluated at its true text length, including text refinement;
+ padded tokens cannot influence either text or joint attention.
+ """
+ @register_to_config
+ def __init__(self, dim=3072, ffn_dim=8192, num_heads=24, num_layers=42,
+ text_dim=4096, freq_dim=256, latent_channels=128, rope_base=10000.0):
+ super().__init__()
+ if dim % num_heads or (dim // num_heads) % 8 or freq_dim % 2:
+ raise ValueError('Require dim divisible by heads, head_dim divisible by 8, and even freq_dim.')
+ self.video_embedder = nn.Linear(latent_channels, dim)
+ self.text_embedder = _TextProjection(text_dim, dim, ffn_dim, num_heads)
+ self.rope = _RoPE(dim // num_heads, rope_base)
+ self.time_embedding = nn.Sequential(nn.Linear(freq_dim, dim), nn.SiLU(), nn.Linear(dim, dim))
+ self.shared_block_modulation = nn.Linear(dim, 6 * dim)
+ self.layers = nn.ModuleList([_Block(dim, ffn_dim, num_heads) for _ in range(num_layers)])
+ self.final_norm_video = _RMSNorm(dim)
+ self.final_linear_video = nn.Linear(dim, latent_channels, bias=False)
+ self.video_head_modulation = nn.Linear(dim, 2 * dim)
+
+ def load_training_state_dict(self, state_dict, *, prefix=''):
+ """Load a consolidated raw/EMA model mapping, strictly; no silent key drops."""
+ if prefix:
+ if any(not key.startswith(prefix) for key in state_dict):
+ raise ValueError('All keys must start with the explicit prefix.')
+ state_dict = {key[len(prefix):]: value for key, value in state_dict.items()}
+ return self.load_state_dict(state_dict, strict=True)
+
+ def forward(self, hidden_states, timestep, encoder_hidden_states,
+ encoder_attention_mask=None, return_dict=True, skip_layers=None):
+ skip_layers = _validate_skip_layers(skip_layers, len(self.layers))
+ if hidden_states.ndim != 4 or hidden_states.shape[1] != self.config.latent_channels:
+ raise ValueError('hidden_states must be [B, latent_channels, H, W].')
+ batch, _, height, width = hidden_states.shape
+ if encoder_hidden_states.ndim != 3 or encoder_hidden_states.shape[0] != batch:
+ raise ValueError('encoder_hidden_states must be [B, S, text_dim].')
+ mask = encoder_attention_mask
+ if mask is None:
+ mask = torch.ones(encoder_hidden_states.shape[:2], dtype=torch.bool, device=hidden_states.device)
+ if mask.shape != encoder_hidden_states.shape[:2] or not torch.all((mask == 0) | (mask == 1)):
+ raise ValueError('encoder_attention_mask must be a binary [B, S] mask.')
+ times = torch.as_tensor(timestep, device=hidden_states.device, dtype=torch.float32).reshape(-1)
+ times = times.expand(batch)
+ # Training computes the sinusoid in float64 and feeds float32 into its MLP.
+ half = self.config.freq_dim // 2
+ freq = 10000 ** (-torch.arange(half, device=times.device, dtype=torch.float64) / half)
+ phase = times.double()[:, None] * freq
+ embedding = torch.cat((phase.cos(), phase.sin()), -1).float()
+ time = self.time_embedding(embedding.to(self.time_embedding[0].weight.dtype)).float()
+ mod = self.shared_block_modulation(time.to(self.shared_block_modulation.weight.dtype)).view(batch, 6, -1)
+ head = self.video_head_modulation(time.to(self.video_head_modulation.weight.dtype)).view(batch, 2, -1)
+ outputs = []
+ for index in range(batch):
+ context = encoder_hidden_states[index:index + 1, mask[index].bool()]
+ if context.shape[1] == 0:
+ raise ValueError('Each sample needs at least one valid text token.')
+ video = hidden_states[index:index + 1].flatten(2).transpose(1, 2)
+ video = self.video_embedder(video.to(self.video_embedder.weight.dtype).contiguous())
+ # Match the reference activation dtype under fp32 weights + autocast.
+ rope = self.rope(height, width, context.shape[1], hidden_states.device).to(video.dtype)
+ text = self.text_embedder(context, rope[:, height * width:])
+ x = torch.cat((video, text), 1)
+ for layer_index, layer in enumerate(self.layers):
+ if layer_index not in skip_layers:
+ x = layer(x, rope, mod[index:index + 1])
+ x = self.final_norm_video(x[:, :height * width].to(self.final_norm_video.weight.dtype))
+ x = x * (1 + head[index:index + 1, 1, None]) + head[index:index + 1, 0, None]
+ x = self.final_linear_video(x.to(self.final_linear_video.weight.dtype))
+ outputs.append(x.transpose(1, 2).reshape(1, -1, height, width))
+ sample = torch.cat(outputs)
+ return Transformer2DModelOutput(sample=sample) if return_dict else (sample,)
+
+
+class MOSSImageScheduler(FlowMatchEulerDiscreteScheduler):
+ """Euler integration using the training project's V2, nearest-grid schedule."""
+ @register_to_config
+ def __init__(self, num_train_timesteps=1000, shift=5.0, training_shift=1.0):
+ super().__init__(num_train_timesteps=num_train_timesteps, shift=1.0)
+ self.register_to_config(shift=shift, training_shift=training_shift)
+
+ def set_timesteps(self, num_inference_steps=50, device=None):
+ if num_inference_steps < 1 or self.config.shift <= 0 or self.config.training_shift <= 0:
+ raise ValueError('Steps and shifts must be positive.')
+ total = self.config.num_train_timesteps
+ def grid(count, shift):
+ lower = shift / (total + shift - 1)
+ values = torch.linspace(1, lower, count)
+ return shift * values / (1 + (shift - 1) * values)
+ sigmas = grid(num_inference_steps, self.config.shift)
+ training = grid(total, self.config.training_shift).flip(0)
+ upper = torch.searchsorted(training, sigmas).clamp(max=total - 1)
+ lower = (upper - 1).clamp(min=0)
+ nearest = torch.where((sigmas - training[lower]).abs() <= (training[upper] - sigmas).abs(), lower, upper)
+ sigmas = torch.unique_consecutive(training[nearest])
+ # Our sigmas have already been shifted and snapped. Avoid a second shift.
+ self._shift = 1.0
+ super().set_timesteps(device=device, sigmas=sigmas.tolist())
+
+
+class MOSSImagePipeline(DiffusionPipeline):
+ model_cpu_offload_seq = 'text_encoder->transformer->vae'
+ _optional_components = ['text_encoder', 'tokenizer', 'vae']
+
+ def __init__(self, transformer: MOSSImageTransformer,
+ scheduler: MOSSImageScheduler, text_encoder=None, tokenizer=None, vae=None):
+ super().__init__()
+ self.register_modules(transformer=transformer, scheduler=scheduler,
+ text_encoder=text_encoder, tokenizer=tokenizer, vae=vae)
+ self.image_processor = VaeImageProcessor(vae_scale_factor=16)
+
+ @classmethod
+ def from_components_pretrained(cls, transformer_path, text_encoder_path, vae_path,
+ torch_dtype=torch.bfloat16, vae_dtype=torch.float32,
+ local_files_only=False):
+ common = dict(local_files_only=local_files_only)
+ transformer = MOSSImageTransformer.from_pretrained(transformer_path, torch_dtype=torch_dtype, **common)
+ tokenizer = AutoTokenizer.from_pretrained(text_encoder_path, **common)
+ text_encoder = AutoModel.from_pretrained(text_encoder_path, dtype=torch_dtype, **common).eval()
+ subfolder = None if (Path(vae_path) / 'config.json').is_file() else 'vae'
+ vae = AutoencoderKLFlux2.from_pretrained(vae_path, subfolder=subfolder, torch_dtype=vae_dtype, **common).eval()
+ return cls(transformer.eval(), MOSSImageScheduler(), text_encoder, tokenizer, vae)
+
+ @torch.no_grad()
+ def encode_prompt(self, prompt, max_sequence_length=4096):
+ if self.text_encoder is None or self.tokenizer is None:
+ raise ValueError('Text encoder/tokenizer required for string prompts.')
+ prompts = [prompt] if isinstance(prompt, str) else list(prompt)
+ if not prompts or not all(isinstance(p, str) for p in prompts):
+ raise ValueError('prompt must be a string or a nonempty list of strings.')
+ rows = []
+ # Unpadded individual forwards also avoid padding effects in Qwen GDN.
+ for value in prompts:
+ formatted = self.tokenizer.apply_chat_template(
+ [{'role': 'user', 'content': value}], tokenize=False,
+ add_generation_prompt=True, enable_thinking=True)
+ inputs = self.tokenizer(formatted, return_tensors='pt', truncation=True,
+ max_length=max_sequence_length).to(self._execution_device)
+ output = self.text_encoder(**inputs, use_cache=False, return_dict=True)
+ rows.append(output.last_hidden_state[0])
+ length = max(row.shape[0] for row in rows)
+ embeds = rows[0].new_zeros(len(rows), length, rows[0].shape[-1])
+ mask = torch.zeros(len(rows), length, device=embeds.device, dtype=torch.bool)
+ for index, row in enumerate(rows):
+ embeds[index, :row.shape[0]] = row
+ mask[index, :row.shape[0]] = True
+ return embeds, mask
+
+ @torch.no_grad()
+ def __call__(self, prompt=None, *, height=1024, width=1024, num_inference_steps=50,
+ guidance_scale=3.0, negative_prompt='', num_images_per_prompt=1,
+ generator=None, latents=None, prompt_embeds=None, prompt_attention_mask=None,
+ negative_prompt_embeds=None, negative_prompt_attention_mask=None,
+ max_sequence_length=4096, output_type='pil', return_dict=True,
+ uncond_skip_layers=(22,), slg_layers=None, slg_scale=0.0,
+ slg_start=0.0, slg_stop=1.0):
+ """Generate images with optional full-block skipping (zero-based indices).
+
+ uncond_skip_layers defaults to (22,) and replaces CFG's negative-prompt
+ prediction with a skipped-block prediction, retaining two forwards per
+ step. Pass None or an empty sequence to disable skipping. slg_layers
+ adds a skipped positive-prompt forward and the fp32 correction
+ slg_scale * (cond - cond_skip) to CFG. With guidance_scale=1, active
+ SLG uses cond_skip + slg_scale * (cond - cond_skip), matching Diffusers'
+ default SkipLayerGuidance formulation, with two forwards and no negative
+ branch. Both options can combine. slg_scale=1 then gives normal cond.
+ Both uncond skipping and SLG are active where
+ slg_start <= step_index / num_steps < slg_stop;
+ the interval follows denoising order, from initial noise to final image.
+ Outside it, inference uses normal CFG without skipping.
+ slg_scale=0 or empty slg_layers disables the additional forward.
+ """
+ uncond_skip_layers = _validate_skip_layers(
+ uncond_skip_layers, len(self.transformer.layers), 'uncond_skip_layers')
+ slg_layers = _validate_skip_layers(slg_layers, len(self.transformer.layers), 'slg_layers')
+ if not math.isfinite(slg_scale) or slg_scale < 0:
+ raise ValueError('slg_scale must be finite and nonnegative.')
+ if not 0 <= slg_start <= slg_stop <= 1:
+ raise ValueError('Require 0 <= slg_start <= slg_stop <= 1.')
+ if height <= 0 or width <= 0 or height % 16 or width % 16:
+ raise ValueError('height and width must be positive multiples of 16.')
+ if output_type not in {'pil', 'np', 'pt', 'latent'}:
+ raise ValueError('output_type must be pil, np, pt or latent.')
+ if num_images_per_prompt < 1 or not math.isfinite(guidance_scale) or guidance_scale < 0:
+ raise ValueError('Invalid image count or guidance scale.')
+ if not 1 <= max_sequence_length <= 4096:
+ raise ValueError('max_sequence_length must be between 1 and 4096.')
+ if (prompt is None) == (prompt_embeds is None):
+ raise ValueError('Provide exactly one of prompt and prompt_embeds.')
+ device = self._execution_device
+ if prompt_embeds is None:
+ prompt_embeds, prompt_attention_mask = self.encode_prompt(prompt, max_sequence_length)
+ batch = prompt_embeds.shape[0]
+ def prepare(embeds, mask):
+ if embeds.ndim != 3 or embeds.shape[0] != batch:
+ raise ValueError('Prompt embeddings must be [B, S, C] with matching batch size.')
+ if mask is None:
+ mask = torch.ones(embeds.shape[:2], dtype=torch.bool, device=embeds.device)
+ return (embeds.to(device).repeat_interleave(num_images_per_prompt, 0),
+ mask.to(device).repeat_interleave(num_images_per_prompt, 0))
+ positive, positive_mask = prepare(prompt_embeds, prompt_attention_mask)
+ do_cfg = guidance_scale != 1.0
+ if do_cfg:
+ if negative_prompt_embeds is None:
+ negatives = [negative_prompt] * batch if isinstance(negative_prompt, str) else negative_prompt
+ if len(negatives) != batch:
+ raise ValueError('negative_prompt must match prompt batch size.')
+ negative_prompt_embeds, negative_prompt_attention_mask = self.encode_prompt(negatives, max_sequence_length)
+ negative, negative_mask = prepare(negative_prompt_embeds, negative_prompt_attention_mask)
+ shape = (batch * num_images_per_prompt, self.transformer.config.latent_channels, height // 16, width // 16)
+ if latents is None:
+ # Sample in fp32: promoting bf16 noise afterward cannot restore its precision.
+ latents = randn_tensor(shape, generator=generator, device=device, dtype=torch.float32)
+ elif tuple(latents.shape) != shape:
+ raise ValueError(f'latents must have shape {shape}.')
+ else:
+ latents = latents.to(device=device, dtype=torch.float32)
+ self.scheduler.set_timesteps(num_inference_steps, device=device)
+ num_steps = len(self.scheduler.timesteps)
+ for step_index, timestep in enumerate(self.progress_bar(self.scheduler.timesteps)):
+ skip_active = slg_start <= step_index / num_steps < slg_stop
+ # CFG must combine fp32 predictions, even when the DiT runs in bf16.
+ # Converting only the combined velocity would preserve rounding errors.
+ prediction = self.transformer(latents, timestep, positive, positive_mask).sample.float()
+ conditional = prediction
+ if do_cfg:
+ skip_kwargs = {'skip_layers': uncond_skip_layers} if uncond_skip_layers and skip_active else {}
+ uncond = self.transformer(latents, timestep, negative, negative_mask, **skip_kwargs).sample.float()
+ prediction = uncond + guidance_scale * (prediction - uncond)
+ if slg_layers and slg_scale > 0 and skip_active:
+ skipped = self.transformer(
+ latents, timestep, positive, positive_mask, skip_layers=slg_layers).sample.float()
+ if not do_cfg:
+ prediction = skipped
+ prediction = prediction + slg_scale * (conditional - skipped)
+ latents = self.scheduler.step(prediction, timestep, latents, return_dict=False)[0]
+ if output_type == 'latent':
+ images = latents
+ else:
+ if self.vae is None:
+ raise ValueError('A FLUX.2 VAE is required for decoded output.')
+ z = latents.to(dtype=self.vae.dtype)
+ bn = self.vae.bn
+ eps = self.vae.config.batch_norm_eps
+ z = z * (bn.running_var + eps).sqrt().to(z).view(1, -1, 1, 1) + bn.running_mean.to(z).view(1, -1, 1, 1)
+ b, c, h, w = z.shape
+ z = z.reshape(b, c // 4, 2, 2, h, w).permute(0, 1, 4, 2, 5, 3).reshape(b, c // 4, 2 * h, 2 * w)
+ images = self.vae.decode(z, return_dict=False)[0].clamp(-1, 1)
+ images = self.image_processor.postprocess(images, output_type=output_type)
+ self.maybe_free_model_hooks()
+ return ImagePipelineOutput(images=images) if return_dict else (images,)
diff --git a/studio/backend/tests/fixtures/diffusion_autoopt_parity.json b/studio/backend/tests/fixtures/diffusion_autoopt_parity.json
index 6234c524c4f..c61988adbe3 100644
--- a/studio/backend/tests/fixtures/diffusion_autoopt_parity.json
+++ b/studio/backend/tests/fixtures/diffusion_autoopt_parity.json
@@ -1,5 +1,5 @@
{
- "meta": {"base": "origin/main 5f38afbc2d (before diffusion_autoopt)", "dtypes": ["bfloat16", "float16"], "families": ["image|flux.1", "image|flux.1-kontext", "image|flux.2-dev", "image|flux.2-klein", "image|flux.3-klein", "image|flux.3-klein-base", "image|hidream-i1", "image|hunyuanimage-2.1", "image|ideogram-4", "image|krea-2", "image|lumina-2", "image|qwen-image", "image|qwen-image-2.1", "image|qwen-image-edit", "image|qwen-image-layered", "image|sdxl", "image|z-image", "video|hunyuanvideo-1.5", "video|hunyuanvideo-1.5-720p", "video|ltx-2", "video|ltx-2.5", "video|minimax-h3", "video|wan2.1-t2v-1.3b", "video|wan2.1-t2v-14b", "video|wan2.2-t2v-a14b", "video|wan2.2-ti2v-5b"], "speeds": ["off", "eager", "default", "max"]},
+ "meta": {"base": "origin/main 5f38afbc2d (before diffusion_autoopt)", "dtypes": ["bfloat16", "float16"], "families": ["image|flux.1", "image|flux.1-kontext", "image|flux.2-dev", "image|flux.2-klein", "image|flux.3-klein", "image|flux.3-klein-base", "image|hidream-i1", "image|hunyuanimage-2.1", "image|ideogram-4", "image|krea-2", "image|lumina-2", "image|moss-image", "image|qwen-image", "image|qwen-image-2.1", "image|qwen-image-edit", "image|qwen-image-layered", "image|sdxl", "image|z-image", "video|hunyuanvideo-1.5", "video|hunyuanvideo-1.5-720p", "video|ltx-2", "video|ltx-2.5", "video|minimax-h3", "video|wan2.1-t2v-1.3b", "video|wan2.1-t2v-14b", "video|wan2.2-t2v-a14b", "video|wan2.2-ti2v-5b"], "speeds": ["off", "eager", "default", "max"]},
"cases": {
"image|flux.1-kontext|default|bfloat16|offload=0|gguf=0": {"applied":[["compiled","cuda_graph","fp16_accum"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":false,"compile_calls":[{"dynamic":true,"fullgraph":true}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
"image|flux.1-kontext|default|bfloat16|offload=0|gguf=1": {"applied":[["compiled_dequant","cuda_graph","fp16_accum"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
@@ -688,6 +688,38 @@
"video|wan2.2-ti2v-5b|off|bfloat16|offload=0|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{}},
"video|wan2.2-ti2v-5b|off|bfloat16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{}},
"video|wan2.2-ti2v-5b|off|float16|offload=0|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{}},
- "video|wan2.2-ti2v-5b|off|float16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{}}
+ "video|wan2.2-ti2v-5b|off|float16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{}},
+ "image|moss-image|default|bfloat16|offload=0|gguf=0": {"applied":[["compiled","cuda_graph","fp16_accum"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":false,"compile_calls":[{"dynamic":true,"fullgraph":true}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|bfloat16|offload=0|gguf=1": {"applied":[["compiled_dequant","cuda_graph","fp16_accum"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|bfloat16|offload=1|gguf=0": {"applied":[["compiled","fp16_accum"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":false,"compile_calls":[{"dynamic":true,"fullgraph":false}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|bfloat16|offload=1|gguf=1": {"applied":[["compiled_dequant","fp16_accum"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|float16|offload=0|gguf=0": {"applied":[["cuda_graph"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|float16|offload=0|gguf=1": {"applied":[["compiled_dequant","cuda_graph"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|float16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|default|float16|offload=1|gguf=1": {"applied":[["compiled_dequant"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|bfloat16|offload=0|gguf=0": {"applied":[["fp16_accum"]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|bfloat16|offload=0|gguf=1": {"applied":[["fp16_accum"]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|bfloat16|offload=1|gguf=0": {"applied":[["fp16_accum"]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|bfloat16|offload=1|gguf=1": {"applied":[["fp16_accum"]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|float16|offload=0|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|float16|offload=0|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|float16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|eager|float16|offload=1|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|bfloat16|offload=0|gguf=0": {"applied":[["compiled","cuda_graph","fp16_accum","tf32"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":true,"compile_calls":[{"dynamic":null,"fullgraph":true,"mode":"max-autotune-no-cudagraphs"}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|bfloat16|offload=0|gguf=1": {"applied":[["compiled","cuda_graph","fp16_accum","tf32"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":true,"compile_calls":[{"dynamic":null,"fullgraph":true,"mode":"max-autotune-no-cudagraphs"}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|bfloat16|offload=1|gguf=0": {"applied":[["compiled","fp16_accum","tf32"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":true,"compile_calls":[{"dynamic":null,"fullgraph":false,"mode":"max-autotune-no-cudagraphs"}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|bfloat16|offload=1|gguf=1": {"applied":[["compiled","fp16_accum","tf32"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":true,"compile_calls":[{"dynamic":null,"fullgraph":false,"mode":"max-autotune-no-cudagraphs"}],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|float16|offload=0|gguf=0": {"applied":[["cuda_graph","fp16_accum","tf32"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|float16|offload=0|gguf=1": {"applied":[["compiled_dequant","cuda_graph","fp16_accum","tf32"]],"graph_after_placement":false,"graph_reason":"eligible","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|float16|offload=1|gguf=0": {"applied":[["fp16_accum","tf32"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|max|float16|offload=1|gguf=1": {"applied":[["compiled_dequant","fp16_accum","tf32"]],"graph_after_placement":true,"graph_reason":"armed after placement","keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"arch_fusions":true,"eager_fusions":true,"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|bfloat16|offload=0|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|bfloat16|offload=0|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|bfloat16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|bfloat16|offload=1|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|float16|offload=0|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|float16|offload=0|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|float16|offload=1|gguf=0": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}},
+ "image|moss-image|off|float16|offload=1|gguf=1": {"applied":[[]],"graph_after_placement":false,"graph_reason":null,"keys":[["channels_last","compiled","compiled_dequant","compiled_vae_decode","cuda_graph","cudnn_benchmark","fp16_accum","fused_qkv","int8_gemm","rocm_query_chunks","tf32","vae_fp16_decode","vae_fused","vae_single_frame"]],"modules":{"transformer":{"auto_dynamic":null,"compile_calls":[],"int8_gemm":false,"static":false}},"patches":{"flux2_rope":false,"qwenimage21_fast_step":false,"rocm_fused":false}}
}
}
diff --git a/studio/backend/tests/fixtures/diffusion_family_data_freeze.json b/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
index 3d900505095..a034411df4c 100644
--- a/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
+++ b/studio/backend/tests/fixtures/diffusion_family_data_freeze.json
@@ -64,6 +64,10 @@
"steps": 30
}
},
+ "moss-image": {
+ "OpenMOSS-Team/MOSS-Image-Base": null,
+ "OpenMOSS-Team/MOSS-Image-SFT": null
+ },
"qwen-image": {
"Qwen/Qwen-Image": {
"default": 2,
@@ -203,6 +207,10 @@
1.0
]
},
+ "moss-image": [
+ 50,
+ 3.0
+ ],
"qwen-image": [
20,
4.0
@@ -388,6 +396,11 @@
"filter_reduction_configs": false,
"filter_reduction_configs_archs": []
},
+ "moss-image": {
+ "cudnn_benchmark": false,
+ "filter_reduction_configs": false,
+ "filter_reduction_configs_archs": []
+ },
"qwen-image": {
"cudnn_benchmark": false,
"filter_reduction_configs": false,
@@ -2359,6 +2372,126 @@
"preview_key": null,
"te_int8_effective": "fp8"
},
+ "moss-image": {
+ "auto": {
+ "convrot_filename": null,
+ "convrot_repo": null,
+ "convrot_spec": [
+ 0,
+ []
+ ],
+ "convrot_variant_repos": [],
+ "exclude": [
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "auto_bf16_when_resident": "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)",
+ "bf16": {
+ "convrot_filename": null,
+ "convrot_repo": null,
+ "convrot_spec": [
+ 0,
+ []
+ ],
+ "convrot_variant_repos": [],
+ "exclude": [
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "convrot_default_on": true,
+ "fp8": {
+ "convrot_filename": null,
+ "convrot_repo": null,
+ "convrot_spec": [
+ 0,
+ []
+ ],
+ "convrot_variant_repos": [],
+ "exclude": [
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "int8": {
+ "convrot_filename": "MOSS-Image-SFT-INT8-ConvRot.safetensors",
+ "convrot_repo": "unsloth/MOSS-Image-SFT-FP8",
+ "convrot_spec": [
+ 256,
+ [
+ "proj.q",
+ "proj.k",
+ "proj.v",
+ "proj.o",
+ "proj.ffn_up_gate",
+ "proj.ffn_down"
+ ]
+ ],
+ "convrot_variant_repos": [
+ "unsloth/MOSS-Image-Base-FP8"
+ ],
+ "exclude": [
+ "norm",
+ "_mod",
+ "modulation",
+ "timestep_embed",
+ "guidance_embed",
+ "time_text_embed",
+ "pooled",
+ "time_embed",
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "mxfp8": {
+ "convrot_filename": null,
+ "convrot_repo": null,
+ "convrot_spec": [
+ 0,
+ []
+ ],
+ "convrot_variant_repos": [],
+ "exclude": [
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "nvfp4": {
+ "convrot_filename": null,
+ "convrot_repo": null,
+ "convrot_spec": [
+ 0,
+ []
+ ],
+ "convrot_variant_repos": [],
+ "exclude": [
+ "text_embedder",
+ "time_embedding",
+ "modulation"
+ ],
+ "pad": [],
+ "zero_row": []
+ },
+ "page": "image",
+ "preview_key": "flux2",
+ "te_int8_effective": "fp8"
+ },
"qwen-image": {
"auto": {
"convrot_filename": null,
@@ -3428,6 +3561,8 @@
"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)",
"hidream-i1": "INT8 is barely faster than compiled bf16 on a card that holds bf16 and changes image detail",
"lumina-2": "INT8 is no faster than compiled bf16 on a card that holds bf16 and changes image detail",
+ "moss-image": "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)",
+ "moss-image-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)",
"z-image": "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)"
},
"family_auto_prefer": {
@@ -3520,6 +3655,12 @@
"hunyuanvideo-1.5-720p": [
"nvfp4"
],
+ "moss-image": [
+ "nvfp4"
+ ],
+ "moss-image-base": [
+ "nvfp4"
+ ],
"qwen-image": [
"mxfp8",
"nvfp4"
@@ -3559,6 +3700,28 @@
"time_embed"
],
"int8_family_convrot": {
+ "moss-image": [
+ 256,
+ [
+ "proj.q",
+ "proj.k",
+ "proj.v",
+ "proj.o",
+ "proj.ffn_up_gate",
+ "proj.ffn_down"
+ ]
+ ],
+ "moss-image-base": [
+ 256,
+ [
+ "proj.q",
+ "proj.k",
+ "proj.v",
+ "proj.o",
+ "proj.ffn_up_gate",
+ "proj.ffn_down"
+ ]
+ ],
"qwen-image-2.1": [
256,
[
@@ -3587,14 +3750,20 @@
},
"int8_family_convrot_default_on": [
"ltx-2.5",
+ "moss-image",
+ "moss-image-base",
"qwen-image-2.1",
"z-image"
],
"int8_family_convrot_filename": {
+ "moss-image": "MOSS-Image-SFT-INT8-ConvRot.safetensors",
+ "moss-image-base": "MOSS-Image-SFT-INT8-ConvRot.safetensors",
"qwen-image-2.1": "Qwen-Image-2.1-INT8-ConvRot.safetensors",
"z-image": "Z-Image-Turbo-INT8-ConvRot.safetensors"
},
"int8_family_convrot_repo": {
+ "moss-image": "unsloth/MOSS-Image-SFT-FP8",
+ "moss-image-base": "unsloth/MOSS-Image-SFT-FP8",
"qwen-image-2.1": "unsloth/Qwen-Image-2.1-FP8",
"z-image": "unsloth/Z-Image-Turbo-FP8"
},
@@ -3602,6 +3771,12 @@
"ltx-2.5": [
"unsloth/LTX-2.5-FP8"
],
+ "moss-image": [
+ "unsloth/MOSS-Image-Base-FP8"
+ ],
+ "moss-image-base": [
+ "unsloth/MOSS-Image-Base-FP8"
+ ],
"qwen-image-2.1": [
"unsloth/Qwen-Image-2.1-Turbo-FP8"
]
@@ -3698,6 +3873,7 @@
"hunyuanvideo-1.5-720p": "hv15",
"ltx-2": "ltx2",
"ltx-2.5": "ltx2",
+ "moss-image": "flux2",
"qwen-image": "qwen_image",
"qwen-image-2.1": "qwen_image_21",
"qwen-image-edit": "qwen_image",
@@ -3732,6 +3908,7 @@
"krea-2": "Krea 2",
"ltx-2": "LTX-2",
"minimax-h3": "MiniMax-H3",
+ "moss-image": "MOSS-Image",
"qwen-image": "Qwen-Image",
"sdxl": "SDXL",
"wan2.2-ti2v-5b": "Wan2.2 TI2V 5B",
@@ -3764,6 +3941,8 @@
"ideogram-ai/ideogram-4-nf4-diffusers",
"krea/krea-2-raw",
"krea/krea-2-turbo",
+ "openmoss-team/moss-image-base",
+ "openmoss-team/moss-image-sft",
"qwen/qwen-image",
"qwen/qwen-image-2.1",
"qwen/qwen-image-2.1-turbo",
diff --git a/studio/backend/tests/fixtures/vae_tile_configs.json b/studio/backend/tests/fixtures/vae_tile_configs.json
index 0b7330b8df9..44c12696441 100644
--- a/studio/backend/tests/fixtures/vae_tile_configs.json
+++ b/studio/backend/tests/fixtures/vae_tile_configs.json
@@ -879,6 +879,56 @@
"use_quant_conv": true
}
},
+ "moss-image": {
+ "kind": "image",
+ "repo": "OpenMOSS-Team/MOSS-Image-SFT",
+ "revision": "f972bd4b4b9e3852133cc5da954d87bc32cd8fb9",
+ "config": {
+ "_class_name": "AutoencoderKLFlux2",
+ "_diffusers_version": "0.38.0",
+ "act_fn": "silu",
+ "batch_norm_eps": 0.0001,
+ "batch_norm_momentum": 0.1,
+ "block_out_channels": [
+ 128,
+ 256,
+ 512,
+ 512
+ ],
+ "decoder_block_out_channels": [
+ 96,
+ 192,
+ 384,
+ 384
+ ],
+ "down_block_types": [
+ "DownEncoderBlock2D",
+ "DownEncoderBlock2D",
+ "DownEncoderBlock2D",
+ "DownEncoderBlock2D"
+ ],
+ "force_upcast": true,
+ "in_channels": 3,
+ "latent_channels": 32,
+ "layers_per_block": 2,
+ "mid_block_add_attention": true,
+ "norm_num_groups": 32,
+ "out_channels": 3,
+ "patch_size": [
+ 2,
+ 2
+ ],
+ "sample_size": 1024,
+ "up_block_types": [
+ "UpDecoderBlock2D",
+ "UpDecoderBlock2D",
+ "UpDecoderBlock2D",
+ "UpDecoderBlock2D"
+ ],
+ "use_post_quant_conv": true,
+ "use_quant_conv": true
+ }
+ },
"sdxl": {
"kind": "image",
"repo": "stabilityai/stable-diffusion-xl-base-1.0",
diff --git a/studio/backend/tests/test_artifact_mirrors.py b/studio/backend/tests/test_artifact_mirrors.py
index c7892764f64..4d663175352 100644
--- a/studio/backend/tests/test_artifact_mirrors.py
+++ b/studio/backend/tests/test_artifact_mirrors.py
@@ -21,6 +21,8 @@ def test_the_new_targets_are_artifact_mirrors_and_the_old_full_mirrors_are_not()
"unsloth/Qwen-Image-Edit-2509-GGUF",
"unsloth/Qwen-Image-Layered-GGUF",
"unsloth/Qwen-Image-2.1-Turbo-FP8",
+ "unsloth/MOSS-Image-SFT-FP8",
+ "unsloth/MOSS-Image-Base-FP8",
):
assert df.mirror_hosts_artifacts(repo), repo
assert df.mirror_hosts_artifacts(repo.lower()), repo
diff --git a/studio/backend/tests/test_cached_gguf_routes.py b/studio/backend/tests/test_cached_gguf_routes.py
index 053950fc175..5f9354f28d0 100644
--- a/studio/backend/tests/test_cached_gguf_routes.py
+++ b/studio/backend/tests/test_cached_gguf_routes.py
@@ -5516,12 +5516,18 @@ def test_every_listing_probe_class_has_a_version_gate():
_FAMILIES,
_PIPELINE_MIN_DIFFUSERS,
family_probe_class,
+ in_tree_pipeline,
)
from core.inference.video_families import _FAMILIES as _VIDEO_FAMILIES
predates_every_release = {"StableDiffusionXLPipeline"}
probed = {family_probe_class(fam) for fam in (*_FAMILIES, *_VIDEO_FAMILIES)}
- ungated = sorted(name for name in probed if name and name not in _PIPELINE_MIN_DIFFUSERS)
+ # In-tree pipelines are no diffusers class; in_tree_pipeline_refusal gates them instead.
+ ungated = sorted(
+ name
+ for name in probed
+ if name and name not in _PIPELINE_MIN_DIFFUSERS and not in_tree_pipeline(name)
+ )
assert set(ungated) <= predates_every_release, ungated
diff --git a/studio/backend/tests/test_diffusion_autoopt_core.py b/studio/backend/tests/test_diffusion_autoopt_core.py
index c7791492595..7f621d8d444 100644
--- a/studio/backend/tests/test_diffusion_autoopt_core.py
+++ b/studio/backend/tests/test_diffusion_autoopt_core.py
@@ -204,6 +204,7 @@ def test_builtin_specs_cover_every_former_install_site():
"qwenimage_real_rope",
"zimage_fused",
"qwenimage21_fused",
+ "moss_fused",
}
# the former hand-kept teardown list is now a registry group
assert {s.name for s in ap.specs(group = ap.GROUP_FUSED_DIT)} == {
@@ -212,6 +213,7 @@ def test_builtin_specs_cover_every_former_install_site():
"qwenimage_real_rope",
"zimage_fused",
"qwenimage21_fused",
+ "moss_fused",
}
# every spec answers its module's existing kill switch name
envs = {s.name: s.env for s in ap.specs()}
@@ -224,6 +226,7 @@ def test_builtin_specs_cover_every_former_install_site():
"qwenimage_real_rope",
"zimage_fused",
"qwenimage21_fused",
+ "moss_fused",
]
diff --git a/studio/backend/tests/test_diffusion_autoopt_parity.py b/studio/backend/tests/test_diffusion_autoopt_parity.py
index 787c0099080..2ece6b8ce44 100644
--- a/studio/backend/tests/test_diffusion_autoopt_parity.py
+++ b/studio/backend/tests/test_diffusion_autoopt_parity.py
@@ -68,7 +68,13 @@ class _Patch:
def _real_class(name: str) -> Any:
try:
import diffusers
- return getattr(diffusers, name, None)
+
+ found = getattr(diffusers, name, None)
+ if found is None:
+ # Studio's in-tree pipelines (MOSS-Image) define their own denoiser class.
+ from core.inference.diffusion_moss_image import in_tree_class
+ found = in_tree_class(name)
+ return found
except Exception: # noqa: BLE001
return None
diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.py
index 97741bafa5c..35f7d4d2de9 100644
--- a/studio/backend/tests/test_diffusion_backend.py
+++ b/studio/backend/tests/test_diffusion_backend.py
@@ -203,7 +203,7 @@ def _all_cached(monkeypatch):
def test_gated_mirror_table_round_trips():
"""Both directions, exact case: canonical_base must hand back a real repo id."""
- assert len(_MIRROR_PAIRS) == 32
+ assert len(_MIRROR_PAIRS) == 34
for upstream, mirror in _MIRROR_PAIRS:
assert mirror_repo(upstream) == mirror
assert canonical_base(mirror) == upstream
@@ -223,7 +223,7 @@ def test_only_the_genuinely_gated_half_reads_as_gated():
is mirrored, and which the Hub serves anonymously.
"""
assert len(_GATED_MIRROR_PAIRS) == 14
- assert len(_UNGATED_MIRROR_PAIRS) == 18
+ assert len(_UNGATED_MIRROR_PAIRS) == 20
for upstream, _mirror in _GATED_MIRROR_PAIRS:
assert upstream_is_gated(upstream), upstream
assert upstream_is_gated(upstream.upper()), upstream
diff --git a/studio/backend/tests/test_diffusion_lora.py b/studio/backend/tests/test_diffusion_lora.py
index 5b1edba809c..a3ff0ac0c61 100644
--- a/studio/backend/tests/test_diffusion_lora.py
+++ b/studio/backend/tests/test_diffusion_lora.py
@@ -499,6 +499,42 @@ def test_scan_local_tolerates_bad_sidecar(tmp_path, monkeypatch):
assert entry.families == () and entry.weight_default == 1.0
+def test_moss_image_has_no_lora_loader_so_lora_is_gated_off(monkeypatch):
+ import threading
+
+ pytest.importorskip("torch")
+ pytest.importorskip("diffusers")
+
+ from core.inference.diffusion_families import detect_family
+ from core.inference.diffusion_moss_image import MOSSImagePipeline
+
+ assert not hasattr(MOSSImagePipeline, "load_lora_weights")
+ for quant in (None, "int8", "fp8"):
+ assert not dl.supports_lora(
+ engine = "diffusers", family = "moss-image", model_kind = "pipeline", transformer_quant = quant
+ )
+ assert dl.supports_lora(
+ engine = "diffusers", family = "flux.1", model_kind = "pipeline", transformer_quant = None
+ )
+ fam = detect_family("OpenMOSS-Team/MOSS-Image-SFT")
+ assert fam is not None and fam.name == "moss-image"
+
+ backend = _backend()
+ monkeypatch.setattr(backend, "validate_load_request", lambda *a, **k: fam)
+ # Refused before anything loads; a weight-0 selection is not active and is not refused here.
+ for load in (backend.begin_load, backend.load_pipeline):
+ with pytest.raises(ValueError, match = "no LoRA loader"):
+ load("OpenMOSS-Team/MOSS-Image-SFT", loras = [("styleA", 1.0)])
+ # A generation-time selection on a loaded MOSS pipe is refused the same way, for every quant.
+ for quant in (None, "int8"):
+ state = types.SimpleNamespace(
+ pipe = _FakePipe(), family = fam, kind = "pipeline", transformer_quant = quant, hf_token = None
+ )
+ with pytest.raises(ValueError, match = "no LoRA loader"):
+ backend._apply_loras(state, [("styleA", 1.0)], threading.Event())
+ backend._apply_loras(state, [("styleA", 0.0)], threading.Event())
+
+
def _peft_add_adapter(layer, name, config):
# PEFT >= 0.19 takes ``config``; 0.18 takes every option positionally (``use_rslora`` required). Pass by keyword
# what the installed signature names, read off ``config``, so the helper holds across PEFT 0.18.0 -> 0.20.0.
diff --git a/studio/backend/tests/test_diffusion_moss_fused.py b/studio/backend/tests/test_diffusion_moss_fused.py
new file mode 100644
index 00000000000..88d17e18e68
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_moss_fused.py
@@ -0,0 +1,197 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""MOSS-Image speed-layer fusions (diffusion_moss_fused): one q/k/v GEMM, the int8 runner hook and the fused q/k
+RMSNorm + RoPE kernel. The block reads them only inside a compiled forward, so the tests force that branch."""
+
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+from unittest import mock
+
+import pytest
+
+_BACKEND = Path(__file__).resolve().parents[1]
+if str(_BACKEND) not in sys.path:
+ sys.path.insert(0, str(_BACKEND))
+
+
[email protected](scope = "module")
+def mods():
+ torch = pytest.importorskip("torch")
+ diffusers = pytest.importorskip("diffusers")
+ pytest.importorskip("transformers")
+ if not hasattr(diffusers, "AutoencoderKLFlux2"):
+ pytest.skip("diffusers without AutoencoderKLFlux2")
+ from core.inference import diffusion_moss_fused as fused
+ from core.inference import diffusion_moss_image as moss
+
+ return torch, moss, fused
+
+
+def _tiny(torch, moss):
+ torch.manual_seed(0)
+ return moss.MOSSImageTransformer(
+ dim = 256, ffn_dim = 512, num_heads = 2, num_layers = 3, text_dim = 64
+ ).eval()
+
+
+def _inputs(torch):
+ g = torch.Generator().manual_seed(1)
+ return (
+ torch.randn(1, 128, 4, 4, generator = g),
+ torch.tensor([500.0]),
+ torch.randn(1, 7, 64, generator = g),
+ )
+
+
+def _compiling(torch, moss):
+ return mock.patch.object(torch.compiler, "is_compiling", lambda: True), mock.patch.object(
+ moss, "_length_dynamic", lambda *a: None
+ )
+
+
+def _fuse_all(
+ moss,
+ fused,
+ transformer,
+ runner = None,
+):
+ from core.inference.diffusion_zimage_fused import _fuse_linears, _share_storage
+ for block in fused._blocks(transformer):
+ parts = [block.proj[n] for n in ("q", "k", "v")]
+ linear = _fuse_linears(parts)
+ assert linear is not None and _share_storage(linear, parts)
+ block.__dict__[moss.FUSED_QKV_ATTR] = (linear, *parts, runner)
+
+
+def test_fused_qkv_matches_the_three_projections(mods):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ x, t, e = _inputs(torch)
+ with torch.no_grad():
+ ref = transformer(x, t, e, return_dict = False)[0]
+ _fuse_all(moss, fused, transformer)
+ a, b = _compiling(torch, moss)
+ with a, b:
+ out = transformer(x, t, e, return_dict = False)[0]
+ eager = transformer(x, t, e, return_dict = False)[0]
+ assert torch.equal(out, ref) and torch.equal(eager, ref)
+ # the parts are views of the fused weight: no second copy
+ block = fused._blocks(transformer)[0]
+ linear = block.__dict__[moss.FUSED_QKV_ATTR][0]
+ assert (
+ block.proj["q"].weight.untyped_storage().data_ptr()
+ == linear.weight.untyped_storage().data_ptr()
+ )
+
+
+def test_a_replaced_projection_drops_back_to_stock(mods):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ x, t, e = _inputs(torch)
+ with torch.no_grad():
+ _fuse_all(moss, fused, transformer)
+ block = fused._blocks(transformer)[0]
+ replacement = torch.nn.Linear(256, 256, bias = False)
+ replacement.weight.copy_(block.proj["q"].weight * 2)
+ block.proj["q"] = replacement
+ a, b = _compiling(torch, moss)
+ with a, b:
+ out = transformer(x, t, e, return_dict = False)[0]
+ ref = transformer(x, t, e, return_dict = False)[0]
+ assert torch.equal(out, ref)
+
+
+def test_the_runner_reaches_every_block_linear(mods):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ x, t, e = _inputs(torch)
+ seen = []
+
+ def runner(linear, value):
+ seen.append(linear)
+ return linear(value)
+
+ with torch.no_grad():
+ ref = transformer(x, t, e, return_dict = False)[0]
+ for block in fused._blocks(transformer):
+ block.__dict__[moss.LINEAR_RUNNER_ATTR] = runner
+ a, b = _compiling(torch, moss)
+ with a, b:
+ out = transformer(x, t, e, return_dict = False)[0]
+ assert torch.equal(out, ref)
+ assert len(seen) == 6 * 3
+
+
+def test_uninstall_and_kill_switch_clear_the_blocks(mods, monkeypatch):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ _fuse_all(moss, fused, transformer)
+ for block in fused._blocks(transformer):
+ block.__dict__[moss.QK_ROPE_ATTR] = object()
+ monkeypatch.setenv(fused.MOSS_FUSED_ENV, "0")
+ assert fused.install(transformer) == {"fused_qkv": 0, "int8_runner": 0, "qk_rope": 0}
+ for block in fused._blocks(transformer):
+ assert not {moss.FUSED_QKV_ATTR, moss.LINEAR_RUNNER_ATTR, moss.QK_ROPE_ATTR} & set(
+ block.__dict__
+ )
+
+
+def test_other_denoisers_and_offload_are_left_alone(mods):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ assert fused.install(torch.nn.Linear(2, 2)) == {"fused_qkv": 0, "int8_runner": 0, "qk_rope": 0}
+ assert fused.install(transformer, offload_active = True) == {
+ "fused_qkv": 0,
+ "int8_runner": 0,
+ "qk_rope": 0,
+ }
+
+
+def test_bf16_keeps_three_gemms_and_rope_is_opt_in(mods, monkeypatch):
+ torch, moss, fused = mods
+ transformer = _tiny(torch, moss)
+ from core.inference import diffusion_int8_fused as i8
+
+ # resident on a (pretend) CUDA device with plain bf16 weights: no runner, so no fused q/k/v and no RoPE kernel
+ monkeypatch.setattr(i8, "resident_cuda_device", lambda m: torch.device("cuda", 0))
+ monkeypatch.setattr(fused, "qk_rope_device_ok", lambda index: True)
+ monkeypatch.setattr(fused, "qk_rope_op", lambda: object())
+ monkeypatch.delenv(fused.MOSS_QK_ROPE_ENV, raising = False)
+ assert fused.install_modules(transformer) == {"fused_qkv": 0, "int8_runner": 0, "qk_rope": 0}
+ monkeypatch.setenv(fused.MOSS_QK_ROPE_ENV, "1")
+ assert (
+ fused.install_modules(transformer)["qk_rope"] == 0
+ ) # fp32 tiny norms: not the bf16 kernel's input
+ for block in fused._blocks(transformer):
+ for n in (block.q_norm, block.k_norm):
+ n.weight.data = n.weight.data.bfloat16()
+ assert fused.install_modules(transformer)["qk_rope"] == 3
+
+
+def test_the_patch_is_registered_for_the_moss_class():
+ from core.inference.diffusion_autoopt import patches
+ spec = next(s for s in patches.specs() if s.name == "moss_fused")
+ assert spec.classes == ("MOSSImageTransformer",) and spec.env == "UNSLOTH_DIFFUSION_MOSS_FUSED"
+
+
+def test_qk_rope_kernel_is_the_eager_ops(mods):
+ torch, moss, fused = mods
+ if not torch.cuda.is_available():
+ pytest.skip("needs CUDA")
+ if fused.qk_rope_op() is None:
+ pytest.skip("no Triton")
+ assert fused.qk_rope_device_ok(torch.cuda.current_device())
+ # block level: the fused path through a real (tiny, bf16) transformer equals the stock eager block
+ transformer = _tiny(torch, moss).to("cuda", torch.bfloat16)
+ x, t, e = (v.to("cuda") for v in _inputs(torch))
+ with torch.no_grad():
+ ref = transformer(x.bfloat16(), t, e.bfloat16(), return_dict = False)[0]
+ for block in fused._blocks(transformer):
+ block.__dict__[moss.QK_ROPE_ATTR] = fused.qk_rope_op()
+ a, b = _compiling(torch, moss)
+ with a, b:
+ out = transformer(x.bfloat16(), t, e.bfloat16(), return_dict = False)[0]
+ assert (out.float() - ref.float()).abs().max() <= 2**-5 * ref.float().abs().max()
diff --git a/studio/backend/tests/test_diffusion_moss_image.py b/studio/backend/tests/test_diffusion_moss_image.py
new file mode 100644
index 00000000000..f112790e78c
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_moss_image.py
@@ -0,0 +1,575 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""MOSS-Image: the in-tree port against the official ``pipeline_moss_image.py`` (kept as test data), bit for bit on
+a tiny random model, plus the scheduler, noise, family row and availability gate. CPU only; the model tests skip
+without diffusers (or a diffusers without the FLUX.2 VAE)."""
+
+from __future__ import annotations
+
+import importlib.machinery
+import importlib.util
+import json
+import os
+import sys
+from pathlib import Path
+
+import pytest
+
+_BACKEND = Path(__file__).resolve().parents[1]
+if str(_BACKEND) not in sys.path:
+ sys.path.insert(0, str(_BACKEND))
+
+REFERENCE = Path(__file__).with_name("data") / "moss_image" / "pipeline_moss_image_reference.py.txt"
+
+
+def _needs_model_stack():
+ torch = pytest.importorskip("torch")
+ diffusers = pytest.importorskip("diffusers")
+ pytest.importorskip("transformers")
+ if not hasattr(diffusers, "AutoencoderKLFlux2"):
+ pytest.skip("diffusers without AutoencoderKLFlux2")
+ return torch
+
+
[email protected](scope = "module")
+def moss():
+ _needs_model_stack()
+ from core.inference import diffusion_moss_image
+ return diffusion_moss_image
+
+
[email protected](scope = "module")
+def ref():
+ _needs_model_stack()
+ loader = importlib.machinery.SourceFileLoader("moss_image_reference", str(REFERENCE))
+ spec = importlib.util.spec_from_loader("moss_image_reference", loader)
+ module = importlib.util.module_from_spec(spec)
+ loader.exec_module(module)
+ return module
+
+
+_TINY = dict(dim = 256, ffn_dim = 512, num_heads = 2, num_layers = 24, text_dim = 64)
+
+
[email protected](scope = "module")
+def transformers_pair(moss, ref):
+ import torch
+
+ torch.manual_seed(0)
+ reference = ref.MOSSImageTransformer(**_TINY)
+ for p in reference.parameters():
+ torch.nn.init.normal_(p, std = 0.02)
+ reference = reference.to(torch.bfloat16).eval()
+ ours = moss.MOSSImageTransformer(**_TINY).to(torch.bfloat16).eval()
+ missing, unexpected = ours.load_state_dict(reference.state_dict(), strict = True)
+ assert not missing and not unexpected
+ return reference, ours
+
+
+def _inputs(
+ batch = 1,
+ lengths = (13,),
+ h = 4,
+ w = 6,
+ seed = 1,
+):
+ import torch
+
+ g = torch.Generator().manual_seed(seed)
+ latents = torch.randn(batch, 128, h, w, generator = g)
+ length = max(lengths)
+ embeds = torch.randn(batch, length, _TINY["text_dim"], generator = g).to(torch.bfloat16)
+ mask = torch.zeros(batch, length, dtype = torch.bool)
+ for i, n in enumerate(lengths):
+ mask[i, :n] = True
+ return latents, embeds, mask
+
+
+def test_state_dict_keys_match_the_reference(transformers_pair):
+ reference, ours = transformers_pair
+ assert list(reference.state_dict()) == list(ours.state_dict())
+
+
[email protected]("skip", [None, [22], [0, 7, 23]])
+def test_forward_is_bitwise_the_reference(transformers_pair, skip):
+ import torch
+
+ reference, ours = transformers_pair
+ latents, embeds, _ = _inputs()
+ t = torch.tensor(731.0)
+ with torch.no_grad():
+ want = reference(latents, t, embeds, skip_layers = skip).sample
+ got = ours(latents, t, embeds, skip_layers = skip).sample
+ refined = ours(latents, t, refined_text = ours.refine_text(embeds), skip_layers = skip).sample
+ assert torch.equal(want, got)
+ assert torch.equal(want, refined)
+
+
+def test_masked_batch_is_bitwise_the_reference(transformers_pair):
+ import torch
+
+ reference, ours = transformers_pair
+ latents, embeds, mask = _inputs(batch = 2, lengths = (5, 11))
+ with torch.no_grad():
+ want = reference(latents, torch.tensor(500.0), embeds, mask).sample
+ got = ours(latents, torch.tensor(500.0), embeds, mask).sample
+ assert torch.equal(want, got)
+
+
+def test_unmasked_forward_takes_no_host_sync(transformers_pair, monkeypatch):
+ """No mask: no ``torch.all`` check and no boolean indexing, so the forward can be CUDA-graph captured."""
+ import torch
+
+ _, ours = transformers_pair
+ latents, embeds, _ = _inputs()
+
+ def _boom(*args, **kwargs):
+ raise AssertionError("host-syncing mask check ran")
+
+ monkeypatch.setattr(torch, "all", _boom)
+ with torch.no_grad():
+ ours(latents, torch.tensor(10.0), embeds, return_dict = False)
+
+
+def test_rope_tables_are_cached_and_rebuilt_on_moves(transformers_pair):
+ import torch
+
+ _, ours = transformers_pair
+ a = ours.rope.tables(4, 6, 9, "cpu", torch.bfloat16)
+ b = ours.rope.tables(4, 6, 9, "cpu", torch.bfloat16)
+ assert a is b
+ ours.float().to(torch.bfloat16)
+ assert ours.rope.bands.dtype == torch.float32
+ assert ours.rope.tables(4, 6, 9, "cpu", torch.bfloat16) is not a
+
+
+def test_refined_text_and_mask_are_exclusive(transformers_pair):
+ import torch
+
+ _, ours = transformers_pair
+ latents, embeds, mask = _inputs()
+ with pytest.raises(ValueError):
+ ours(latents, torch.tensor(1.0))
+ with pytest.raises(ValueError):
+ ours(latents, torch.tensor(1.0), embeds, refined_text = ours.refine_text(embeds))
+ with pytest.raises(ValueError):
+ ours(
+ latents,
+ torch.tensor(1.0),
+ refined_text = ours.refine_text(embeds),
+ encoder_attention_mask = mask,
+ )
+
+
[email protected]("steps", [1, 2, 8, 28, 50])
+def test_training_grid_matches_the_reference_scheduler(moss, ref, steps):
+ import torch
+
+ want = ref.MOSSImageScheduler()
+ got = moss.MOSSImageScheduler(sigma_schedule = "training_grid")
+ want.set_timesteps(steps)
+ got.set_timesteps(steps)
+ assert torch.equal(want.sigmas, got.sigmas)
+ assert torch.equal(want.timesteps, got.timesteps)
+
+
+def _comfy_sgm_uniform(steps, shift):
+ """comfy.samplers.normal_scheduler(sgm=True) over ModelSamplingDiscreteFlow(shift, multiplier=1), restated."""
+ import torch
+
+ def sigma(t):
+ return shift * t / (1 + (shift - 1) * t)
+
+ grid = sigma(torch.arange(1, 1001, 1) / 1000)
+ timesteps = torch.linspace(grid[-1], grid[0], steps + 1)[:-1]
+ return [float(sigma(ts)) for ts in timesteps] + [0.0]
+
+
[email protected]("steps", [4, 20, 50])
+def test_default_schedule_is_comfyui_sgm_uniform(moss, monkeypatch, steps):
+ import torch
+
+ monkeypatch.delenv(moss.SIGMAS_ENV, raising = False)
+ sched = moss.MOSSImageScheduler()
+ sched.set_timesteps(steps)
+ want = torch.tensor(_comfy_sgm_uniform(steps, 5.0), dtype = torch.float32)
+ assert sched.sigmas.shape == want.shape
+ assert torch.allclose(sched.sigmas, want, atol = 1e-7, rtol = 0)
+ assert float(sched.sigmas[0]) == 1.0 and float(sched.sigmas[-1]) == 0.0
+ assert torch.allclose(sched.timesteps, want[:-1] * 1000, atol = 1e-3, rtol = 0)
+
+
+def test_env_selects_the_training_grid(moss, ref, monkeypatch):
+ import torch
+
+ monkeypatch.setenv(moss.SIGMAS_ENV, "training_grid")
+ got = moss.MOSSImageScheduler()
+ want = ref.MOSSImageScheduler()
+ got.set_timesteps(50)
+ want.set_timesteps(50)
+ assert torch.equal(got.sigmas, want.sigmas)
+
+
+def test_repo_scheduler_config_loads(moss, tmp_path):
+ config = {
+ "_class_name": "MOSSImageScheduler",
+ "_diffusers_version": "0.38.0",
+ "base_image_seq_len": 256,
+ "num_train_timesteps": 1000,
+ "shift": 5.0,
+ "training_shift": 1.0,
+ "use_dynamic_shifting": False,
+ }
+ (tmp_path / "scheduler_config.json").write_text(json.dumps(config))
+ sched = moss.MOSSImageScheduler.from_pretrained(str(tmp_path))
+ assert sched.config.shift == 5.0 and sched.resolved_sigma_schedule() in moss.SIGMA_SCHEDULES
+
+
+def test_cpu_generator_noise_is_the_reference_draw(moss):
+ import torch
+ from diffusers.utils.torch_utils import randn_tensor
+
+ shape = (2, 128, 4, 6)
+ want = randn_tensor(
+ shape,
+ generator = torch.Generator("cpu").manual_seed(5),
+ device = torch.device("cpu"),
+ dtype = torch.float32,
+ )
+ got = moss.comfy_noise(shape, torch.Generator("cpu").manual_seed(5), "cpu")
+ assert torch.equal(want, got)
+ gens = [torch.Generator("cpu").manual_seed(s) for s in (1, 2)]
+ want = randn_tensor(
+ shape,
+ generator = [torch.Generator("cpu").manual_seed(s) for s in (1, 2)],
+ device = torch.device("cpu"),
+ dtype = torch.float32,
+ )
+ assert torch.equal(want, moss.comfy_noise(shape, gens, "cpu"))
+
+
+def test_accelerator_generator_draws_comfyui_cpu_noise_from_its_seed(moss):
+ import torch
+
+ class _FakeCudaGenerator:
+ device = torch.device("cuda", 0)
+
+ def initial_seed(self):
+ return 42
+
+ got = moss.comfy_noise((1, 128, 4, 4), _FakeCudaGenerator(), "cpu")
+ want = torch.randn((1, 128, 4, 4), generator = torch.Generator("cpu").manual_seed(42))
+ assert torch.equal(got, want)
+ with pytest.raises(ValueError):
+ moss.comfy_noise((3, 128, 4, 4), [_FakeCudaGenerator(), _FakeCudaGenerator()], "cpu")
+
+
[email protected](scope = "module")
+def pipelines(moss, ref, transformers_pair):
+ import torch
+ from diffusers import AutoencoderKLFlux2
+
+ torch.manual_seed(0)
+ vae = AutoencoderKLFlux2(
+ block_out_channels = [32, 32, 32, 32],
+ decoder_block_out_channels = [16, 16, 16, 16],
+ norm_num_groups = 8,
+ layers_per_block = 1,
+ latent_channels = 32,
+ ).eval()
+ reference, ours = transformers_pair
+ ref_pipe = ref.MOSSImagePipeline(reference, ref.MOSSImageScheduler(), vae = vae)
+ our_pipe = moss.MOSSImagePipeline(
+ ours, moss.MOSSImageScheduler(sigma_schedule = "training_grid"), vae = vae
+ )
+ return ref_pipe, our_pipe
+
+
+ "kwargs",
+ [
+ {},
+ {"guidance_scale": 1.0},
+ {"uncond_skip_layers": []},
+ {"slg_layers": [3], "slg_scale": 2.0, "slg_start": 0.2, "slg_stop": 0.8},
+ {"num_images_per_prompt": 2},
+ ],
+ ids = ["cfg_skip22", "no_cfg", "plain_cfg", "slg", "two_images"],
+)
+def test_pipeline_is_bitwise_the_reference(pipelines, kwargs):
+ import torch
+
+ ref_pipe, our_pipe = pipelines
+ _, embeds, _ = _inputs(lengths = (9,), seed = 3)
+ _, negative, _ = _inputs(lengths = (4,), seed = 4)
+ outs = []
+ for pipe in (ref_pipe, our_pipe):
+ pipe.set_progress_bar_config(disable = True)
+ outs.append(
+ pipe(
+ prompt_embeds = embeds,
+ negative_prompt_embeds = negative,
+ height = 64,
+ width = 96,
+ num_inference_steps = 3,
+ generator = torch.Generator("cpu").manual_seed(0),
+ output_type = "pt",
+ **kwargs,
+ ).images
+ )
+ assert torch.equal(outs[0], outs[1])
+
+
+def test_text_cache_off_is_bitwise_the_cached_render(moss, pipelines, monkeypatch):
+ import torch
+
+ _, our_pipe = pipelines
+ _, embeds, _ = _inputs(lengths = (9,), seed = 3)
+ _, negative, _ = _inputs(lengths = (4,), seed = 4)
+
+ def run():
+ return our_pipe(
+ prompt_embeds = embeds,
+ negative_prompt_embeds = negative,
+ height = 32,
+ width = 32,
+ num_inference_steps = 2,
+ generator = torch.Generator("cpu").manual_seed(0),
+ output_type = "latent",
+ ).images
+
+ cached = run()
+ monkeypatch.setenv(moss.TEXT_CACHE_ENV, "0")
+ assert torch.equal(cached, run())
+
+
+def test_step_callback_and_interrupt(pipelines):
+ import torch
+
+ _, our_pipe = pipelines
+ _, embeds, _ = _inputs(lengths = (9,), seed = 3)
+ seen = []
+
+ def on_step(pipe, i, t, kw):
+ seen.append(i)
+ assert set(kw) == {"latents"}
+ if i == 1:
+ pipe._interrupt = True
+ return kw
+
+ our_pipe(
+ prompt_embeds = embeds,
+ guidance_scale = 1.0,
+ height = 32,
+ width = 32,
+ num_inference_steps = 4,
+ output_type = "latent",
+ callback_on_step_end = on_step,
+ )
+ assert seen == [0, 1]
+
+
+def test_skip_layers_reach_the_transformer_as_a_tuple(pipelines, monkeypatch):
+ """CUDA graphs key plain values; a frozenset kwarg would make every uncond call uncapturable."""
+ import torch
+
+ _, our_pipe = pipelines
+ _, embeds, _ = _inputs(lengths = (9,), seed = 3)
+ calls = []
+ forward = our_pipe.transformer.forward
+
+ def spy(*args, **kwargs):
+ calls.append(kwargs.get("skip_layers"))
+ assert kwargs.get("return_dict") is False
+ return forward(*args, **kwargs)
+
+ monkeypatch.setattr(our_pipe.transformer, "forward", spy)
+ _, negative, _ = _inputs(lengths = (4,), seed = 4)
+ our_pipe(
+ prompt_embeds = embeds,
+ negative_prompt_embeds = negative,
+ height = 32,
+ width = 32,
+ num_inference_steps = 1,
+ output_type = "latent",
+ )
+ assert calls == [None, (22,)]
+
+
+def test_family_detects_both_repos_and_a_local_pipeline(tmp_path):
+ from core.inference import diffusion_families as df
+
+ for repo in ("OpenMOSS-Team/MOSS-Image-SFT", "OpenMOSS-Team/MOSS-Image-Base"):
+ fam = df.detect_family(repo)
+ assert fam is not None and fam.name == "moss-image", repo
+ local = tmp_path / "f972bd4b4b9e"
+ local.mkdir()
+ (local / "model_index.json").write_text(json.dumps({"_class_name": "MOSSImagePipeline"}))
+ fam = df.detect_family_for_pick(str(local))
+ assert fam is not None and fam.name == "moss-image"
+
+
+def test_family_row_defaults_and_trust():
+ from core.inference import diffusion_families as df
+ from core.inference.family_profile import trusted_repo_ids
+
+ fam = df.detect_family("OpenMOSS-Team/MOSS-Image-SFT")
+ assert fam.native_transformer_files_only and not fam.pipeline_only
+ assert fam.fp16_incompatible and fam.uses_negative_prompt
+ assert (
+ fam.pipeline_class == "MOSSImagePipeline"
+ and fam.transformer_class == "MOSSImageTransformer"
+ )
+ for repo in ("OpenMOSS-Team/MOSS-Image-SFT", "OpenMOSS-Team/MOSS-Image-Base", "moss-image"):
+ assert df.default_generation_params(repo) == (50, 3.0), repo
+ trusted = trusted_repo_ids(df._FAMILIES)
+ assert {"openmoss-team/moss-image-sft", "openmoss-team/moss-image-base"} <= trusted
+
+
+def test_in_tree_pipeline_refuses_old_transformers(monkeypatch):
+ from core.inference import diffusion_families as df
+
+ fam = df.detect_family("OpenMOSS-Team/MOSS-Image-SFT")
+ monkeypatch.setattr(df, "_installed_transformers_version", lambda: "4.57.6")
+ assert df.family_pipeline_available(fam) is False
+ with pytest.raises(ValueError, match = r"transformers >= 5\.3"):
+ df.assert_pipeline_class_available(fam.pipeline_class, fam.name)
+ monkeypatch.setattr(df, "_installed_transformers_version", lambda: "5.5.0")
+ monkeypatch.setattr(df, "_diffusers_package_has_file", lambda rel: True)
+ assert df.family_pipeline_available(fam) is True
+ df.assert_pipeline_class_available(fam.pipeline_class, fam.name)
+
+
+def test_prompt_length_policy_arms_the_moss_blocks():
+ from core.inference import diffusion_dynamic_text as dt
+ assert (
+ dt.PROMPT_LENGTH_POLICY["MOSSImageTransformer"]
+ == dt._FAMILY_SOURCES["MOSSImageTransformer"]
+ )
+ assert "L['x']" in dt._FAMILY_SOURCES["MOSSImageTransformer"]
+
+
+def test_in_tree_classes_resolve(moss):
+ from core.inference.diffusion import _family_model_class
+
+ import diffusers
+
+ assert _family_model_class(diffusers, "MOSSImagePipeline") is moss.MOSSImagePipeline
+ assert _family_model_class(diffusers, "MOSSImageTransformer") is moss.MOSSImageTransformer
+ assert moss.MOSSImageTransformer._repeated_blocks == ["_Block", "_TextBlock"]
+ assert moss.MOSS_IMAGE_FAMILY_NAME == "moss-image"
+
+
+def test_drop_vision_tower(moss):
+ import torch
+
+ class _Encoder(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.visual = torch.nn.Linear(2, 2)
+ self.language_model = torch.nn.Linear(2, 2)
+
+ enc = _Encoder()
+ assert moss.drop_vision_tower(enc) is True
+ assert enc.visual is None and "visual" not in dict(enc.named_children())
+ assert moss.drop_vision_tower(enc) is False
+
+
+def test_loader_never_reads_repo_code(moss):
+ """The assembly names its components itself; no trust_remote_code / custom_pipeline anywhere in the module."""
+ source = Path(moss.__file__).read_text(encoding = "utf-8")
+ assert "trust_remote_code" not in source.replace("no ``trust_remote_code``", "")
+ assert "custom_pipeline =" not in source and "custom_pipeline=" not in source
+
+
+def _callback_block_source() -> str:
+ """The pipeline's step-end callback block, wrapped as a function of the names it reads."""
+ import ast
+ import textwrap
+
+ tree = ast.parse(
+ (_BACKEND / "core" / "inference" / "diffusion_moss_image.py").read_text(encoding = "utf-8")
+ )
+ pipe = next(
+ n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "MOSSImagePipeline"
+ )
+ call = next(n for n in pipe.body if isinstance(n, ast.FunctionDef) and n.name == "__call__")
+ block = next(
+ n
+ for n in ast.walk(call)
+ if isinstance(n, ast.If) and ast.unparse(n.test) == "callback_on_step_end is not None"
+ )
+ return (
+ "def run(self, step_index, timestep, latents, callback_on_step_end, callback_on_step_end_tensor_inputs):\n"
+ + textwrap.indent(ast.unparse(block), " ")
+ + "\n return latents\n"
+ + "seen = []\n"
+ + "out = run(None, 0, 1, 'L', lambda p, i, t, kw: seen.append(dict(kw)), ['latents'])\n"
+ + "assert seen == [{'latents': 'L'}] and out == 'L', (seen, out)\n"
+ )
+
+
+def _old_pythons():
+ import shutil
+ import subprocess
+
+ found = []
+ for name in ("python3.9", "python3.10", "python3.11"):
+ path = os.environ.get("MOSS_OLD_PYTHON") if name == "python3.11" else None
+ if not path:
+ path = shutil.which(name)
+ # A pyenv / asdf shim for an uninstalled version is on PATH but exits nonzero: not an interpreter.
+ if (
+ path
+ and subprocess.run(
+ [path, "-I", "-c", "pass"], capture_output = True, timeout = 60
+ ).returncode
+ ):
+ path = None
+ if path:
+ found.append(path)
+ return found
+
+
+def test_step_callback_reads_tensors_from_the_call_scope():
+ """Before Python 3.12 a comprehension's ``locals()`` is its own scope, so ``locals()["latents"]`` raised KeyError."""
+ import subprocess
+
+ import ast
+
+ tree = ast.parse(
+ (_BACKEND / "core" / "inference" / "diffusion_moss_image.py").read_text(encoding = "utf-8")
+ )
+ comps = (ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp)
+ for comp in (n for n in ast.walk(tree) if isinstance(n, comps)):
+ calls = [
+ c for c in ast.walk(comp) if isinstance(c, ast.Call) and ast.unparse(c.func) == "locals"
+ ]
+ assert not calls, f"locals() inside a comprehension at line {comp.lineno}"
+ src = _callback_block_source()
+ exec(compile(src, "<moss-callback>", "exec"), {})
+ for exe in _old_pythons():
+ proc = subprocess.run([exe, "-I", "-c", src], capture_output = True, text = True, timeout = 60)
+ assert proc.returncode == 0, f"{exe}: {proc.stderr}"
+
+
+def test_step_callback_latents_are_bchw(pipelines):
+ _, our_pipe = pipelines
+ _, embeds, _ = _inputs(lengths = (9,), seed = 3)
+ shapes = []
+
+ def on_step(pipe, i, t, kw):
+ shapes.append(tuple(kw["latents"].shape))
+ return kw
+
+ our_pipe(
+ prompt_embeds = embeds,
+ guidance_scale = 1.0,
+ height = 32,
+ width = 48,
+ num_inference_steps = 2,
+ output_type = "latent",
+ callback_on_step_end = on_step,
+ )
+ assert shapes == [(1, 128, 2, 3)] * 2
diff --git a/studio/backend/tests/test_diffusion_moss_image_quant.py b/studio/backend/tests/test_diffusion_moss_image_quant.py
new file mode 100644
index 00000000000..0badaf212f4
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_moss_image_quant.py
@@ -0,0 +1,448 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""MOSS-Image quantisation wiring: the frozen keep-list and ConvRot spec the hosted builds were made with, the hosted
+file names for SFT and Base, the shared text encoder, the automatic scheme order, and a clean fallback when the hosted
+repos are unreachable. CPU only, no network."""
+
+from __future__ import annotations
+
+import json
+import sys
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+_BACKEND = Path(__file__).resolve().parents[1]
+if str(_BACKEND) not in sys.path:
+ sys.path.insert(0, str(_BACKEND))
+
+SFT = "OpenMOSS-Team/MOSS-Image-SFT"
+BASE = "OpenMOSS-Team/MOSS-Image-Base"
+SFT_REPO = "unsloth/MOSS-Image-SFT-FP8"
+BASE_REPO = "unsloth/MOSS-Image-Base-FP8"
+KEEP = ("text_embedder", "time_embedding", "modulation")
+
+
+def _fam(repo = SFT):
+ from core.inference.diffusion_families import detect_family
+
+ fam = detect_family(repo)
+ assert fam is not None and fam.name == "moss-image"
+ return fam
+
+
+def offline_empty(tmp_path, monkeypatch):
+ """No mirror, no Hub, an empty cache: the hosted repos are unreachable."""
+ monkeypatch.setenv("HF_HUB_OFFLINE", "1")
+ monkeypatch.delenv("UNSLOTH_DIFFUSION_PREQUANT_MIRROR", raising = False)
+ cache = tmp_path / "hub"
+ cache.mkdir()
+ return str(cache)
+
+
+def test_keep_list_and_convrot_spec_match_the_hosted_builds():
+ fam = _fam()
+ assert fam.profile.quant_exclude_tokens == KEEP
+ spec = fam.profile.int8_convrot
+ assert spec.group == 256 and spec.default_on
+ assert spec.suffixes == (
+ "proj.q",
+ "proj.k",
+ "proj.v",
+ "proj.o",
+ "proj.ffn_up_gate",
+ "proj.ffn_down",
+ )
+ assert spec.repo == SFT_REPO and spec.variant_repos == (BASE_REPO,)
+ assert spec.filename == "MOSS-Image-SFT-INT8-ConvRot.safetensors"
+
+
[email protected]("family", ["moss-image", "moss-image-base"])
[email protected]("scheme", ["int8", "fp8"])
+def test_quant_tables_answer_to_both_recorded_family_names(family, scheme):
+ """The Base files record family "moss-image-base": the validator's exclude check reads it from the file."""
+ from core.inference import diffusion_transformer_quant as tq
+
+ tokens = tq.exclude_tokens_for_scheme(scheme, family)
+ assert set(KEEP) <= set(tokens)
+ # The hosted files' recorded exclude lists (int8 adds the generic M = 1 tokens; fp8 is the keep-list alone).
+ recorded = {
+ "int8": [
+ "norm",
+ "_mod",
+ "modulation",
+ "timestep_embed",
+ "guidance_embed",
+ "time_text_embed",
+ "pooled",
+ "time_embed",
+ "text_embedder",
+ "time_embedding",
+ "modulation",
+ ],
+ "fp8": list(KEEP),
+ }[scheme]
+ assert set(tokens) <= set(recorded)
+
+
+ "base, repo, names",
+ [
+ (
+ SFT,
+ SFT_REPO,
+ {
+ "int8": (
+ "MOSS-Image-SFT-INT8-ConvRot.safetensors",
+ "MOSS-Image-SFT-INT8.safetensors",
+ ),
+ "fp8": ("MOSS-Image-SFT-FP8.safetensors",),
+ },
+ ),
+ (
+ BASE,
+ BASE_REPO,
+ {
+ "int8": (
+ "MOSS-Image-Base-INT8-ConvRot.safetensors",
+ "MOSS-Image-Base-INT8.safetensors",
+ ),
+ "fp8": ("MOSS-Image-Base-FP8.safetensors",),
+ },
+ ),
+ ],
+)
+def test_each_base_resolves_its_own_hosted_files(base, repo, names):
+ from core.inference.diffusion_prequant import candidate_filenames_of, resolve_prequant_source
+
+ fam = _fam(base)
+ for scheme, expected in names.items():
+ source = resolve_prequant_source(fam, scheme, base_repo = base)
+ assert source is not None and source.location == repo, (base, scheme)
+ candidates = candidate_filenames_of(source)
+ positions = [candidates.index(n) for n in expected]
+ assert positions == sorted(positions), candidates
+ other = "SFT" if "Base" in repo else "Base"
+ assert not any(f"MOSS-Image-{other}-" in n for n in candidates), candidates
+ # Base never takes SFT's artifact for a scheme it has no row for
+ assert resolve_prequant_source(fam, "nvfp4", base_repo = base) is None
+
+
[email protected]("family", ["moss-image", "moss-image-base"])
+def test_auto_never_picks_fp8_but_an_explicit_fp8_is_allowed(family):
+ from core.inference import diffusion_transformer_quant as tq
+
+ for cap in ((10, 0), (9, 0), (8, 9), (8, 0)):
+ order = tq._auto_scheme_order(family, "cuda", cap)
+ assert order and order[0] == "int8", (cap, order)
+ assert "fp8" not in order and "mxfp8" not in order, (cap, order)
+ assert not tq._family_denied(family, "fp8")
+ assert "fp8" in tq._auto_scheme_order("qwen-image-2.1", "cuda", (10, 0))
+
+
+def test_auto_keeps_the_5b_dit_bf16_while_it_fits():
+ """Small-DiT rule (as FLUX.2 / FLUX.3-klein, Z-Image): bf16 while a resident plan fits, INT8-ConvRot once bf16
+ would offload, an explicit precision honoured (the rule reads only AUTO)."""
+ from types import SimpleNamespace
+
+ from core.inference.diffusion import _auto_keeps_bf16_reason, _auto_quant_eager_reason
+ from core.inference.diffusion_families import (
+ SMALL_DIT_BF16_WHEN_RESIDENT,
+ small_dit_rule_excludes,
+ )
+ from core.inference.diffusion_transformer_quant import auto_bf16_when_resident_reason
+
+ fam = _fam()
+ assert auto_bf16_when_resident_reason("moss-image") == SMALL_DIT_BF16_WHEN_RESIDENT
+ # Both 5B bases sit inside the rule; only FLUX.2 klein 9B variants are carved out.
+ for base in ("OpenMOSS-Team/MOSS-Image-SFT", "OpenMOSS-Team/MOSS-Image-Base"):
+ assert not small_dit_rule_excludes(base)
+ assert _auto_keeps_bf16_reason(fam, "pipeline", base) is not None
+ assert _auto_keeps_bf16_reason(fam, "pipeline") is not None
+ assert _auto_keeps_bf16_reason(fam, "gguf") is None
+
+ def plan(policy, required, budget):
+ return SimpleNamespace(
+ offload_policy = policy,
+ estimates = {"resident_required_mib": required, "safe_device_budget_mib": budget},
+ )
+
+ assert _auto_quant_eager_reason(fam, plan("none", 36060, 156376)) is not None
+ assert _auto_quant_eager_reason(fam, plan("none", 36060, 20000)) is None
+ assert _auto_quant_eager_reason(fam, plan("group", 36060, 20000)) is None
+
+
+def test_text_encoder_file_and_equivalence():
+ from core.inference import diffusion_te_prequant as tp
+ from core.inference.diffusion_precision import resolve_te_quant_request
+ from core.inference.diffusion_text_encoder_trim import family_trims_lm_head
+
+ fam = _fam()
+ assert fam.te_quant_auto == "int8"
+ mode, auto = resolve_te_quant_request(None, fam.te_quant_auto)
+ assert mode == "int8" and auto
+ source = tp.resolve_te_prequant_source(fam, "text_encoder", "int8")
+ assert source.location == SFT_REPO
+ assert source.filename == "MOSS-Image-SFT-text_encoder-INT8-ConvRot.safetensors"
+ assert (
+ source.fallback_filenames == ()
+ and tp.resolve_te_prequant_source(fam, "text_encoder", "fp8") is None
+ )
+ assert family_trims_lm_head("moss-image")
+ # Base reuses the SFT file: the validator accepts the SFT base_model_id on a Base load, and nothing else
+ assert tp.te_base_equivalent(SFT, BASE) and tp.te_base_equivalent(BASE, SFT)
+ assert not tp.te_base_equivalent(SFT, "Qwen/Qwen3.5-9B")
+ # 11,488,171,720 bytes against the 15.9 GB table price: 0.723, rounded up
+ assert 0.723 < tp.te_int8_convrot_budget_scale(fam) < 0.74
+
+
+def test_absent_hosted_repos_fall_back_to_runtime_quant_and_the_dense_encoder(offline_empty):
+ from core.inference import diffusion_te_prequant as tp
+ from core.inference.diffusion_denoiser_prequant import (
+ denoiser_prequant_cached,
+ denoiser_prequant_pipe_kwargs,
+ )
+ for base in (SFT, BASE):
+ fam = _fam(base)
+ for scheme in ("int8", "fp8"):
+ assert not denoiser_prequant_cached(
+ fam, scheme, base_repo = base, cache_dir = offline_empty
+ )
+ # {} = the released denoiser is loaded and quantised at runtime (int8 rotates with the same spec)
+ assert (
+ denoiser_prequant_pipe_kwargs(
+ fam,
+ base,
+ scheme = scheme,
+ dtype = None,
+ device = "cpu",
+ local_files_only = True,
+ cache_dir = offline_empty,
+ )
+ == {}
+ )
+ sources = {"text_encoder": tp.resolve_te_prequant_source(fam, "text_encoder", "int8")}
+ assert tp.te_prequant_sources_confirmed(sources, local_files_only = True) is False
+
+
+def test_automatic_text_encoder_without_the_file_stays_dense_bf16():
+ torch = pytest.importorskip("torch")
+ from core.inference.diffusion_auto_policy import RESOLVED_FELL_BACK
+ from core.inference.diffusion_precision import AutoTeQuant, quantize_text_encoders
+
+ encoder = torch.nn.Sequential(torch.nn.Linear(256, 256))
+ weight = encoder[0].weight
+ pipe = SimpleNamespace(text_encoder = encoder)
+ target = SimpleNamespace(device = "cpu", dtype = torch.bfloat16)
+ outcome = quantize_text_encoders(pipe, target, mode = AutoTeQuant("int8"), family = "moss-image")
+ assert outcome.mode is None and outcome.status == RESOLVED_FELL_BACK
+ assert pipe.text_encoder[0].weight is weight and weight.dtype == torch.float32
+
+
+def test_denoiser_seed_resolves_the_in_tree_class(monkeypatch):
+ """The hosted builds are seeded into the pipeline assembly; the class is Studio's port, not a diffusers attr."""
+ pytest.importorskip("torch")
+ diffusers = pytest.importorskip("diffusers")
+ if not hasattr(diffusers, "AutoencoderKLFlux2"):
+ pytest.skip("diffusers without AutoencoderKLFlux2")
+ from core.inference import diffusion_denoiser_prequant as ddp
+ from core.inference import diffusion_prequant as pq
+ from core.inference.diffusion_moss_image import MOSSImageTransformer
+
+ seen = {}
+ sentinel = object()
+
+ def fake_load(transformer_cls, base_repo, source, **kwargs):
+ seen["cls"], seen["base"], seen["family"] = transformer_cls, base_repo, kwargs.get("family")
+ return sentinel
+
+ monkeypatch.setattr(pq, "load_prequantized_transformer", fake_load)
+ monkeypatch.setattr(
+ ddp,
+ "denoiser_prequant_source",
+ lambda *a, **k: SimpleNamespace(kind = "repo", location = BASE_REPO),
+ )
+ out = ddp.denoiser_prequant_pipe_kwargs(
+ _fam(BASE), BASE, scheme = "int8", dtype = None, device = "cpu"
+ )
+ assert out == {"transformer": sentinel}
+ assert seen == {"cls": MOSSImageTransformer, "base": BASE, "family": "moss-image"}
+
+
+def test_a_handed_in_encoder_gets_the_vision_drop_and_offload_guard(monkeypatch):
+ torch = pytest.importorskip("torch")
+ diffusers = pytest.importorskip("diffusers")
+ if not hasattr(diffusers, "AutoencoderKLFlux2"):
+ pytest.skip("diffusers without AutoencoderKLFlux2")
+ from core.inference import diffusion_moss_image as moss
+
+ class FakeGatedDeltaNet(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.conv1d = torch.nn.Conv1d(4, 4, 3, groups = 4)
+
+ class Encoder(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.visual = torch.nn.Linear(2, 2)
+ self.layer = FakeGatedDeltaNet()
+
+ class Stub:
+ @classmethod
+ def from_pretrained(cls, *args, **kwargs):
+ return cls()
+
+ monkeypatch.setattr(moss, "assert_transformers_supported", lambda: None)
+ monkeypatch.setattr(moss, "_tokenizer_class", lambda repo, common: Stub)
+ monkeypatch.setattr(
+ moss.MOSSImageScheduler, "from_pretrained", classmethod(lambda cls, *a, **k: "sched")
+ )
+ monkeypatch.setattr(
+ diffusers.AutoencoderKLFlux2, "from_pretrained", classmethod(lambda cls, *a, **k: "vae")
+ )
+ monkeypatch.setattr(moss, "MOSSImagePipeline", lambda **kw: kw)
+ encoder = Encoder()
+ parts = moss.load_moss_image_pipeline(
+ "OpenMOSS-Team/MOSS-Image-SFT",
+ torch.bfloat16,
+ transformer = torch.nn.Linear(2, 2),
+ text_encoder = encoder,
+ local_files_only = True,
+ )
+ assert parts["text_encoder"] is encoder and encoder.visual is None
+ assert getattr(encoder.layer, "_unsloth_conv_guard", False)
+
+
+def test_tokenizer_class_comes_from_its_config(tmp_path, offline_empty):
+ pytest.importorskip("torch")
+ pytest.importorskip("diffusers")
+ transformers = pytest.importorskip("transformers")
+ from core.inference import diffusion_moss_image as moss
+
+ (tmp_path / "tokenizer").mkdir()
+ (tmp_path / "tokenizer" / "tokenizer_config.json").write_text(
+ json.dumps({"tokenizer_class": "Qwen2Tokenizer"})
+ )
+ assert moss._tokenizer_class(str(tmp_path), {}) is transformers.Qwen2Tokenizer
+ assert moss._tokenizer_class(str(tmp_path / "missing"), {}) is transformers.AutoTokenizer
+ assert (
+ moss._tokenizer_class(SFT, {"local_files_only": True, "cache_dir": offline_empty})
+ is transformers.AutoTokenizer
+ )
+
+
+def test_activation_casts_read_a_declared_compute_dtype():
+ """diffusers' GGUFLinear stores uint8 weights and declares ``compute_dtype``; a plain Linear casts to its weight."""
+ torch = pytest.importorskip("torch")
+ diffusers = pytest.importorskip("diffusers")
+ if not hasattr(diffusers, "AutoencoderKLFlux2"):
+ pytest.skip("diffusers without AutoencoderKLFlux2")
+ from core.inference.diffusion_moss_image import _compute_dtype
+
+ plain = torch.nn.Linear(2, 2).to(torch.bfloat16)
+ assert _compute_dtype(plain) is torch.bfloat16
+ packed = torch.nn.Linear(2, 2)
+ packed.compute_dtype = torch.bfloat16
+ assert _compute_dtype(packed) is torch.bfloat16
+
+
+def test_measured_peak_seeds_the_quantised_placement():
+ """The compiled-tier seed keeps a 5 GB int8 denoiser resident on a 16 GB card instead of the flat 8 GiB estimate."""
+ from core.inference.diffusion_memory import measured_image_runtime_mib
+
+ assert measured_image_runtime_mib("moss-image", "default") == 3584
+ assert measured_image_runtime_mib("moss-image", "max") == 3584
+ assert measured_image_runtime_mib("moss-image", "off", dense_transformer_mib = 9800) is None
+
+
+def test_moss_denies_nvfp4():
+ # NVFP4 W4A4 scored LPIPS 0.383 against bf16 (SFT, 12 prompts); INT8-ConvRot 0.058.
+ for repo in (SFT, BASE):
+ assert "nvfp4" in _fam(repo).profile.scheme_deny
+
+
+def test_fp8_te_cast_keeps_the_gated_delta_conv1d_in_compute_dtype():
+ """Qwen3.5's gated-delta layers hand ``linear_attn.conv1d.weight`` straight to the causal-conv kernel without
+ calling the module, so the layerwise upcast hook never runs: the cast must leave conv1d out."""
+ torch = pytest.importorskip("torch")
+ pytest.importorskip("diffusers.hooks")
+
+ try:
+ from transformers import Qwen3_5TextConfig, Qwen3_5TextModel
+ except ImportError:
+ pytest.skip("transformers without Qwen3.5 (MOSS-Image needs >= 5.3)")
+ from core.inference.diffusion_precision import _cast_fp8
+
+ cfg = Qwen3_5TextConfig(
+ vocab_size = 128,
+ hidden_size = 64,
+ intermediate_size = 128,
+ num_hidden_layers = 2,
+ layer_types = ["linear_attention", "full_attention"],
+ num_attention_heads = 2,
+ num_key_value_heads = 1,
+ head_dim = 32,
+ linear_num_key_heads = 1,
+ linear_num_value_heads = 2,
+ linear_key_head_dim = 16,
+ linear_value_head_dim = 16,
+ linear_conv_kernel_dim = 4,
+ )
+ encoder = Qwen3_5TextModel(cfg).to(torch.bfloat16)
+ _cast_fp8(encoder, SimpleNamespace(dtype = torch.bfloat16))
+
+ convs = [m for n, m in encoder.named_modules() if n.endswith("linear_attn.conv1d")]
+ assert convs, "the tiny config built no gated-delta layer"
+ assert all(m.weight.dtype == torch.bfloat16 for m in convs)
+ fp8 = [
+ m
+ for m in encoder.modules()
+ if isinstance(m, torch.nn.Linear) and m.weight.dtype == torch.float8_e4m3fn
+ ]
+ assert fp8, "the cast stored no Linear in fp8"
+
+
+def test_local_moss_pipeline_needs_its_transformer_shards(tmp_path):
+ """The manifest names MOSS's own ``pipeline_moss_image`` module for the transformer and scheduler; the in-tree port
+ reads them as diffusers components, so a config-only transformer/ (an interrupted copy) is not complete."""
+ from core.inference.diffusion_families import local_pipeline_components_are_complete
+
+ def write(rel, text = "{}"):
+ path = tmp_path / rel
+ path.parent.mkdir(parents = True, exist_ok = True)
+ path.write_text(text, encoding = "utf-8")
+
+ write(
+ "model_index.json",
+ json.dumps(
+ {
+ "_class_name": "MOSSImagePipeline",
+ "scheduler": ["pipeline_moss_image", "MOSSImageScheduler"],
+ "text_encoder": ["transformers", "Qwen3_5Model"],
+ "tokenizer": ["transformers", "Qwen2Tokenizer"],
+ "transformer": ["pipeline_moss_image", "MOSSImageTransformer"],
+ "vae": ["diffusers", "AutoencoderKLFlux2"],
+ }
+ ),
+ )
+ write("scheduler/scheduler_config.json")
+ write("text_encoder/config.json")
+ write("text_encoder/model.safetensors", "x")
+ write("tokenizer/tokenizer_config.json")
+ write("tokenizer/tokenizer.json")
+ write("vae/config.json")
+ write("vae/diffusion_pytorch_model.safetensors", "x")
+ write("transformer/config.json")
+ write("transformer/pipeline_moss_image.py", "# remote code")
+ assert not local_pipeline_components_are_complete(tmp_path, "model_index.json")
+ shard = "diffusion_pytorch_model-00001-of-00001.safetensors"
+ write(
+ "transformer/diffusion_pytorch_model.safetensors.index.json",
+ json.dumps({"weight_map": {"w": shard}}),
+ )
+ assert not local_pipeline_components_are_complete(tmp_path, "model_index.json")
+ write(f"transformer/{shard}", "x")
+ assert local_pipeline_components_are_complete(tmp_path, "model_index.json")
diff --git a/studio/backend/tests/test_diffusion_pipeline_prequant.py b/studio/backend/tests/test_diffusion_pipeline_prequant.py
index 4e5279d981d..5dfff048568 100644
--- a/studio/backend/tests/test_diffusion_pipeline_prequant.py
+++ b/studio/backend/tests/test_diffusion_pipeline_prequant.py
@@ -1408,6 +1408,9 @@ def test_a_measured_bf16_family_quantises_auto_when_the_budget_is_unmeasured(
("black-forest-labs/flux-3-image-klein-base", True),
("black-forest-labs/FLUX.2-dev", False),
("Qwen/Qwen-Image", False),
+ # A 5B DiT: INT8-ConvRot doubles LPIPS against the eager port, so bf16 while it fits.
+ ("OpenMOSS-Team/MOSS-Image-SFT", True),
+ ("OpenMOSS-Team/MOSS-Image-Base", True),
],
)
def test_the_bf16_rule_names_only_the_measured_families(repo, kept):
diff --git a/studio/backend/tests/test_diffusion_preview.py b/studio/backend/tests/test_diffusion_preview.py
index 2f549825be6..d9b936c72fc 100644
--- a/studio/backend/tests/test_diffusion_preview.py
+++ b/studio/backend/tests/test_diffusion_preview.py
@@ -352,3 +352,47 @@ def test_a_half_size_stage_unpacks_with_its_own_grid(monkeypatch):
assert (prev.height, prev.width) == (704, 1280)
finally:
prev.finish()
+
+
+def test_moss_image_bchw_latents_use_the_flux2_map():
+ # MOSS-Image denoises FLUX.2's 128 packed channels as [B, 128, h, w], not [B, N, 128] tokens.
+ spec = DP.factors_for("moss-image")
+ assert spec is not None and spec.channels == 128 and spec.patch == 2
+ lat = torch.randn(1, 128, 4, 6)
+ grid = DP.latent_grid(lat, spec, height = 64, width = 96)
+ assert grid is not None and torch.equal(grid, lat[0].permute(1, 2, 0))
+ tokens = lat.flatten(2).transpose(1, 2)
+ assert torch.equal(grid, DP.latent_grid(tokens, spec, 64, 96))
+ w = torch.tensor(spec.weight)
+ b = torch.tensor(spec.bias)
+ assert tuple(DP.project(grid, w, b, spec.patch).shape) == (8, 12, 3)
+ assert DP.latent_grid(torch.randn(1, 32, 4, 6), spec, 64, 96) is None
+
+
[email protected](not torch.cuda.is_available(), reason = "needs CUDA")
+def test_cuda_preview_publishes_moss_image_bchw_latents(monkeypatch):
+ monkeypatch.delenv(DP.PREVIEW_ENV, raising = False)
+ got = []
+ done = threading.Event()
+
+ def publish(url, seq):
+ got.append((url, seq))
+ done.set()
+
+ prev = DP.LatentPreviewer.create(
+ family = "moss-image",
+ requested = None,
+ height = 128,
+ width = 128,
+ device = "cuda",
+ publish = publish,
+ min_interval_s = 0.0,
+ )
+ assert prev is not None
+ try:
+ prev.on_step(torch.randn(1, 128, 8, 8, device = "cuda"), None, final = True)
+ assert not prev.failed
+ assert done.wait(10)
+ finally:
+ prev.finish()
+ assert got and got[0][0].startswith("data:image/jpeg;base64,")
diff --git a/studio/backend/tests/test_diffusion_prompt_hint.py b/studio/backend/tests/test_diffusion_prompt_hint.py
index cd755e97735..9ddc58a1fe1 100644
--- a/studio/backend/tests/test_diffusion_prompt_hint.py
+++ b/studio/backend/tests/test_diffusion_prompt_hint.py
@@ -33,7 +33,7 @@ from test_diffusion_backend import ( # noqa: E402,F401
from test_mlx_diffusion_backend import _load as _load_mlx, env # noqa: E402,F401
from test_sd_cpp_backend import _loaded_backend as _loaded_sd_cpp_backend # noqa: E402
-_TIPPED = {"flux.1-kontext", "qwen-image-edit", "qwen-image-2.1", "ideogram-4"}
+_TIPPED = {"flux.1-kontext", "qwen-image-edit", "qwen-image-2.1", "ideogram-4", "moss-image"}
_HINT = "Describe the scene first."
diff --git a/studio/backend/tests/test_diffusion_prompt_length_policy.py b/studio/backend/tests/test_diffusion_prompt_length_policy.py
index 5b40d3e2b9b..bba81ea0f2c 100644
--- a/studio/backend/tests/test_diffusion_prompt_length_policy.py
+++ b/studio/backend/tests/test_diffusion_prompt_length_policy.py
@@ -20,6 +20,7 @@ VARIABLE = (
"QwenImage21Transformer2DModel",
"ZImageTransformer2DModel",
"MiniMaxH3Transformer3DModel",
+ "MOSSImageTransformer",
)
@@ -70,6 +71,8 @@ def test_the_compile_arms_only_its_own_sources():
"QwenImage21Transformer2DModel",
"QwenImageTransformer2DModel",
"MiniMaxH3Transformer3DModel",
+ # MOSS-Image never pads its prompt (true text length per branch), so it is armed from day one.
+ "MOSSImageTransformer",
}
for name, sources in dt._FAMILY_SOURCES.items():
assert dt.PROMPT_LENGTH_POLICY[name] == sources, name
diff --git a/studio/backend/tests/test_diffusion_shift_override.py b/studio/backend/tests/test_diffusion_shift_override.py
index 153281487b9..87ed0afcb0d 100644
--- a/studio/backend/tests/test_diffusion_shift_override.py
+++ b/studio/backend/tests/test_diffusion_shift_override.py
@@ -60,6 +60,7 @@ EXPECTED_NODES = {
"lumina-2": SHIFT_NODE_SD3,
"hunyuanimage-2.1": SHIFT_NODE_SD3,
"hidream-i1": SHIFT_NODE_SD3,
+ "moss-image": SHIFT_NODE_SD3,
# Not offered: Ideogram 4 runs its own mu/std preset, SDXL is not flow matching.
"ideogram-4": None,
"sdxl": None,
diff --git a/studio/backend/tests/test_diffusion_speed.py b/studio/backend/tests/test_diffusion_speed.py
index 59f418cb82c..dd87b980747 100644
--- a/studio/backend/tests/test_diffusion_speed.py
+++ b/studio/backend/tests/test_diffusion_speed.py
@@ -646,6 +646,7 @@ def test_cudnn_benchmark_opt_out_image_families():
"flux.2-klein",
"z-image",
"sdxl",
+ "moss-image",
}
diff --git a/studio/backend/tests/test_diffusion_te_auto_int8_budget.py b/studio/backend/tests/test_diffusion_te_auto_int8_budget.py
index 7060aa27214..5774d49bb19 100644
--- a/studio/backend/tests/test_diffusion_te_auto_int8_budget.py
+++ b/studio/backend/tests/test_diffusion_te_auto_int8_budget.py
@@ -131,6 +131,7 @@ _AUTO_INT8_FILE_BYTES = {
"flux.3-klein": 5_247_076_320,
"flux.3-klein-base": 5_247_076_320,
"qwen-image-2.1": 9_349_769_248,
+ "moss-image": 11_488_171_720,
}
diff --git a/studio/backend/tests/test_image_fetch_ids_unsloth.py b/studio/backend/tests/test_image_fetch_ids_unsloth.py
index 5973ab2fa02..05ef10f714d 100644
--- a/studio/backend/tests/test_image_fetch_ids_unsloth.py
+++ b/studio/backend/tests/test_image_fetch_ids_unsloth.py
@@ -67,9 +67,11 @@ _CARD_BASES: dict[str, str] = {
"unsloth/FLUX.2-klein-base-9B-GGUF": "black-forest-labs/FLUX.2-klein-base-9B",
"unsloth/flux-3-image-klein-GGUF": "black-forest-labs/flux-3-image-klein",
"unsloth/flux-3-image-klein-base-GGUF": "black-forest-labs/flux-3-image-klein-base",
+ "unsloth/MOSS-Image-SFT-GGUF": "OpenMOSS-Team/MOSS-Image-SFT",
+ "unsloth/MOSS-Image-Base-GGUF": "OpenMOSS-Team/MOSS-Image-Base",
}
-# These five have no separate mirror repo: the official diffusers folders live inside the model's existing unsloth
+# These have no separate mirror repo: the official diffusers folders live inside the model's existing unsloth
# repo (-FP8 where one exists, else -GGUF).
_NEW_PAIRS = (
("Qwen/Qwen-Image-2.1-Turbo", "unsloth/Qwen-Image-2.1-Turbo-FP8"),
@@ -77,6 +79,8 @@ _NEW_PAIRS = (
("Qwen/Qwen-Image-Edit-2509", "unsloth/Qwen-Image-Edit-2509-GGUF"),
("Qwen/Qwen-Image-Layered", "unsloth/Qwen-Image-Layered-GGUF"),
("Tongyi-MAI/Z-Image", "unsloth/Z-Image-GGUF"),
+ ("OpenMOSS-Team/MOSS-Image-SFT", "unsloth/MOSS-Image-SFT-FP8"),
+ ("OpenMOSS-Team/MOSS-Image-Base", "unsloth/MOSS-Image-Base-FP8"),
)
# Repacks built by ComfyUI (or its unsloth/* copies). No image load may fetch from one, not even through the
@@ -188,6 +192,22 @@ def test_every_sd_cpp_asset_fetch_id_is_unsloth():
assert not bad, "sd-cli asset fetch ids outside unsloth/*:\n" + "\n".join(map(str, bad))
+def test_moss_image_components_come_from_its_fp8_repos():
+ fam = detect_family("OpenMOSS-Team/MOSS-Image-SFT")
+ assert fam is not None and fam.name == "moss-image"
+ assert fam.base_repo == "OpenMOSS-Team/MOSS-Image-SFT"
+ for variant in ("SFT", "Base"):
+ upstream, mirror = (
+ f"OpenMOSS-Team/MOSS-Image-{variant}",
+ f"unsloth/MOSS-Image-{variant}-FP8",
+ )
+ assert _fetch_id(upstream) == mirror
+ assert df.mirror_hosts_artifacts(mirror)
+ sd_cpp = {r[1]: r[3] for r in _sd_cpp_fetch_ids() if r[0] == "moss-image"}
+ assert sd_cpp["sd_cpp:vae"] == "unsloth/MOSS-Image-SFT-FP8"
+ assert fam.sd_cpp_tokenizer == ("unsloth/MOSS-Image-SFT-FP8", "tokenizer/tokenizer.json")
+
+
def test_layered_sd_cpp_vae_comes_from_the_mirror():
"""Layered's sd-cli VAE is read from its own base, so the base mirror is what keeps it inside unsloth/*."""
fam = detect_family("", override = "qwen-image-layered")
diff --git a/studio/backend/tests/test_sd_cpp_moss_image.py b/studio/backend/tests/test_sd_cpp_moss_image.py
new file mode 100644
index 00000000000..35c46ccc98a
--- /dev/null
+++ b/studio/backend/tests/test_sd_cpp_moss_image.py
@@ -0,0 +1,227 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Native (sd.cpp) MOSS-Image: the assets sd-cli is handed (FLUX.2 small-decoder VAE, Qwen3.5-9B, and the
+tokenizer.json sd.cpp's MOSS conditioner requires on --tokenizer), the generic optional --tokenizer plumbing, and
+the routing rule that a MOSS GGUF loads only natively, from a build that carries the architecture."""
+
+from __future__ import annotations
+
+import pytest
+
+from core.inference import diffusion_engine_router as r
+from core.inference.diffusion_families import (
+ _FAMILIES,
+ detect_family,
+ family_gguf_loadable,
+ family_sd_cpp_supported,
+ sd_cpp_text_encoders_for,
+)
+from core.inference.sd_cpp_args import (
+ SdCppGenParams,
+ SdCppModelFiles,
+ SdCppVideoGenParams,
+ build_sd_cpp_command,
+ build_sd_cpp_server_command,
+ build_sd_cpp_video_command,
+)
+from core.inference.sd_cpp_backend import SdCppDiffusionBackend, sd_cpp_binary_runs_family
+from core.inference.sd_cpp_engine import ENGINE_DIFFUSERS, ENGINE_SD_CPP
+
+from .test_diffusion_engine_router import ( # noqa: F401 -- the autouse fixture resets router state
+ _clean_env_and_state,
+ _set_binary,
+ _set_device,
+ _set_runnable,
+)
+from .test_sd_cpp_backend import _FakeEngine
+
+_REPO = "unsloth/MOSS-Image-SFT-GGUF"
+_GGUF = "MOSS-Image-SFT-Q4_K_M.gguf"
+_SNAP = "unsloth/MOSS-Image-SFT-FP8"
+_TE = ("unsloth/Qwen3.5-9B-GGUF", "Qwen3.5-9B-Q8_0.gguf", "llm")
+_TOKENIZER = (_SNAP, "tokenizer/tokenizer.json")
+
+
+def _moss():
+ fam = detect_family(_REPO)
+ assert fam is not None and fam.name == "moss-image"
+ return fam
+
+
+def test_the_family_row_carries_the_native_assets():
+ fam = _moss()
+ assert family_sd_cpp_supported(fam) and family_gguf_loadable(fam)
+ assert not fam.pipeline_only and fam.native_transformer_files_only
+ # MOSS's own VAE (the FLUX.2 small decoder), not FLUX.2-dev's; sd.cpp detects its layout itself.
+ assert fam.sd_cpp_vae == (_SNAP, "vae/diffusion_pytorch_model.safetensors")
+ assert fam.sd_cpp_vae_format is None
+ # sd.cpp's MOSS default shift (5) is the reference recipe, so no override.
+ assert fam.sd_cpp_flow_shift is None
+ assert sd_cpp_text_encoders_for(fam, _REPO, _GGUF) == (_TE,)
+ assert fam.sd_cpp_tokenizer == _TOKENIZER
+ assert fam.sd_cpp_arch_marker == "MOSS-Image"
+ # The Base GGUF resolves to the same row and fetches the same (identical) companions.
+ assert detect_family("unsloth/MOSS-Image-Base-GGUF") is fam
+
+
+def test_only_moss_declares_a_tokenizer_or_native_only_transformer_files():
+ assert [f.name for f in _FAMILIES if f.sd_cpp_tokenizer] == ["moss-image"]
+ assert [f.name for f in _FAMILIES if f.native_transformer_files_only] == ["moss-image"]
+
+
+def test_the_asset_specs_fetch_the_tokenizer():
+ specs = SdCppDiffusionBackend(engine = _FakeEngine())._asset_specs(_REPO, _GGUF, _moss())
+ assert specs == [
+ (_REPO, _GGUF, "diffusion_model"),
+ (_SNAP, "vae/diffusion_pytorch_model.safetensors", "vae"),
+ _TE,
+ (*_TOKENIZER, "tokenizer"),
+ ]
+ z = detect_family("z-image")
+ assert not any(
+ kind == "tokenizer"
+ for _r, _f, kind in SdCppDiffusionBackend(engine = _FakeEngine())._asset_specs(
+ "unsloth/Z-Image-Turbo-GGUF", "z-image-turbo-Q4_K_M.gguf", z
+ )
+ )
+
+
+def test_the_tokenizer_is_a_protected_companion_file():
+ from hub.utils.companion_assets import _sd_cpp_component_specs
+ assert _TOKENIZER in _sd_cpp_component_specs(_moss(), _REPO, _GGUF)
+
+
+_FILES = dict(diffusion_model = "/m/moss.gguf", vae = "/m/vae.safetensors", llm = "/m/qwen.gguf")
+
+
+def _tokenizer_arg(cmd):
+ return [cmd[i + 1] for i, flag in enumerate(cmd) if flag == "--tokenizer"]
+
+
[email protected]("tokenizer", [None, "/m/tokenizer.json"])
+def test_every_builder_passes_the_optional_tokenizer(tokenizer):
+ files = SdCppModelFiles(**_FILES, tokenizer = tokenizer)
+ expected = [tokenizer] if tokenizer else []
+ cli = build_sd_cpp_command(
+ binary = "sd-cli", files = files, params = SdCppGenParams(prompt = "x"), output_path = "/o/x.png"
+ )
+ server = build_sd_cpp_server_command(
+ binary = "sd-server", files = files, host = "127.0.0.1", port = 1234
+ )
+ video = build_sd_cpp_video_command(
+ binary = "sd-cli",
+ files = files,
+ params = SdCppVideoGenParams(prompt = "x", width = 448, height = 256, num_frames = 17, fps = 24),
+ output_path = "/o/x.webm",
+ )
+ for cmd in (cli, server, video):
+ assert _tokenizer_arg(cmd) == expected
+
+
+def test_the_tokenizer_defaults_to_none():
+ assert SdCppModelFiles(**_FILES).tokenizer is None
+
+
+def _binary(tmp_path, name, *, marker):
+ path = tmp_path / name
+ path.write_bytes(b"ELF-ish padding " * 64 + (b"MOSS-Image" if marker else b"") + b" trailing")
+ return str(path)
+
+
+def test_the_marker_is_the_model_version_string(tmp_path):
+ fam = _moss()
+ assert sd_cpp_binary_runs_family(_binary(tmp_path, "new", marker = True), fam)
+ assert not sd_cpp_binary_runs_family(_binary(tmp_path, "old", marker = False), fam)
+
+
[email protected]("backend", ["cuda", "cpu"])
+def test_a_moss_gguf_goes_native_even_on_a_gpu_host(monkeypatch, tmp_path, backend):
+ _set_device(monkeypatch, backend)
+ _set_runnable(monkeypatch)
+ _set_binary(monkeypatch, _binary(tmp_path, "sd-cli", marker = True))
+ fam = _moss()
+ assert r.predict_engine(fam, model_kind = "gguf") == ENGINE_SD_CPP
+ r.select_and_activate_engine(fam, model_kind = "gguf")
+ assert r.active_engine_name() == ENGINE_SD_CPP
+ if backend == "cuda":
+ assert r.predict_engine(fam, model_kind = "pipeline") == ENGINE_DIFFUSERS
+
+
+def test_a_build_without_moss_is_refused_not_sent_to_diffusers(monkeypatch, tmp_path):
+ _set_device(monkeypatch, "cuda")
+ _set_runnable(monkeypatch)
+ # Installs off: the resident pre-975 build is what this host has.
+ monkeypatch.setenv("UNSLOTH_DIFFUSION_SD_CPP_INSTALL", "0")
+ _set_binary(monkeypatch, _binary(tmp_path, "sd-cli-old", marker = False))
+ fam = _moss()
+ fallbacks = []
+ with pytest.raises(ValueError, match = "no usable sd.cpp build here supports 'moss-image'"):
+ r.predict_engine(fam, model_kind = "gguf")
+ with pytest.raises(ValueError, match = "predates 'moss-image' support"):
+ r.select_and_activate_engine(
+ fam, model_kind = "gguf", before_fallback = lambda: fallbacks.append(1)
+ )
+ assert fallbacks == []
+
+
+def test_an_explicit_diffusers_pick_of_a_moss_gguf_is_refused(monkeypatch):
+ fam = _moss()
+ with pytest.raises(ValueError, match = "Diffusers cannot load a moss-image GGUF"):
+ r.refuse_engine_pick(fam, engine = "diffusers", model_kind = "gguf")
+ # The pipeline repo on diffusers is fine where diffusers can build it (transformers >= 5.3).
+ monkeypatch.setattr(r, "family_pipeline_available", lambda _fam: True)
+ r.refuse_engine_pick(fam, engine = "diffusers", model_kind = "pipeline")
+
+
+ "env, value",
+ [("UNSLOTH_DIFFUSION_SD_CPP", "0"), ("UNSLOTH_DIFFUSION_ENGINE", "diffusers")],
+)
+def test_the_native_disable_wins_and_diffusers_refuses_the_gguf(monkeypatch, env, value):
+ # The operator's switch is honoured as everywhere else; the diffusers loader then refuses the GGUF before eviction.
+ monkeypatch.setenv(env, value)
+ _set_device(monkeypatch, "cuda")
+ assert r.predict_engine(_moss(), model_kind = "gguf") == ENGINE_DIFFUSERS
+ from core.inference.diffusion import DiffusionBackend
+
+ with pytest.raises(ValueError, match = "only on the native stable-diffusion.cpp engine"):
+ DiffusionBackend().validate_load_request(_REPO, gguf_filename = _GGUF, model_kind = "gguf")
+
+
+def test_the_route_validation_passes_a_gguf_native_can_take(monkeypatch, tmp_path):
+ # Diffusers validation runs before routing: pass a GGUF bound for sd.cpp, refuse it once native cannot take it.
+ from core.inference.diffusion import DiffusionBackend
+
+ _set_device(monkeypatch, "cuda")
+ _set_runnable(monkeypatch)
+ monkeypatch.setenv("UNSLOTH_DIFFUSION_SD_CPP_INSTALL", "0")
+ _set_binary(monkeypatch, _binary(tmp_path, "sd-cli", marker = True))
+ fam = DiffusionBackend().validate_load_request(_REPO, gguf_filename = _GGUF, model_kind = "gguf")
+ assert fam.name == "moss-image"
+ assert r.family_buildable_here(fam, model_kind = "gguf", gguf_filename = _GGUF)
+
+ _set_binary(monkeypatch, _binary(tmp_path, "sd-cli-old", marker = False))
+ assert not r.family_buildable_here(fam, model_kind = "gguf", gguf_filename = _GGUF)
+ with pytest.raises(ValueError, match = "only on the native stable-diffusion.cpp engine"):
+ DiffusionBackend().validate_load_request(_REPO, gguf_filename = _GGUF, model_kind = "gguf")
+ assert r.family_buildable_here(fam, model_kind = "pipeline") == r.family_pipeline_available(fam)
+
+
+def test_diffusers_refuses_a_moss_single_file_transformer(monkeypatch, tmp_path):
+ from core.inference.diffusion import DiffusionBackend
+
+ # Even with a capable native build: sd.cpp takes only the GGUF, so a loose transformer file has no engine.
+ _set_device(monkeypatch, "cuda")
+ _set_runnable(monkeypatch)
+ _set_binary(monkeypatch, _binary(tmp_path, "sd-cli", marker = True))
+ with pytest.raises(ValueError, match = "single-file transformer only on the native"):
+ DiffusionBackend().validate_load_request(
+ _REPO, gguf_filename = "MOSS-Image-SFT-INT8.safetensors", model_kind = "single_file"
+ )
+
+
+def test_a_loose_moss_safetensors_is_not_offered():
+ from hub.utils.comfy_models import _single_file_loadable
+ assert not _single_file_loadable(_moss(), None, "MOSS-Image-SFT-INT8.safetensors")
+ assert _single_file_loadable(detect_family("z-image"), None, "z_image_turbo_bf16.safetensors")
diff --git a/studio/backend/tests/test_video_unsloth_mirrors.py b/studio/backend/tests/test_video_unsloth_mirrors.py
index 61c34a66838..89c5faadd96 100644
--- a/studio/backend/tests/test_video_unsloth_mirrors.py
+++ b/studio/backend/tests/test_video_unsloth_mirrors.py
@@ -100,7 +100,7 @@ def test_video_pairs_stay_out_of_the_image_companion_table():
"""The image companion table (companion_assets reads it) is unchanged; video ids only feed the id lookups."""
image_ids = {rid.lower() for pair in _MIRROR_PAIRS for rid in pair}
assert not image_ids & {rid.lower() for pair in _VIDEO_MIRROR_PAIRS for rid in pair}
- assert len(_MIRROR_PAIRS) == len(_GATED_MIRROR_PAIRS) + len(_UNGATED_MIRROR_PAIRS) == 32
+ assert len(_MIRROR_PAIRS) == len(_GATED_MIRROR_PAIRS) + len(_UNGATED_MIRROR_PAIRS) == 34
def _video_fetch_ids() -> dict[str, str]:
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index d0309de0a52..22278c834ca 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -389,8 +389,12 @@ export const BUDGET = {
// (a CI build measured about 100 B over a local one). Raised by that.
// Install Training button under the greyed Train row's hint: +424 B raw / +141 B transfer against ee55c3c9a
// (6,920,512 -> 6,920,936 B raw, 2,084,382 -> 2,084,523 B transfer, Node 24, 97 chunks). Raised by that.
- transferBytes: 2_084_837,
- rawBytes: 6_921_245,
+ // MOSS-Image (#14552): its two image catalog groups, family label and generation defaults sit in the eager
+ // catalog the chat chunk imports: +1,298 B raw / +181 B transfer (6,919,946 -> 6,921,244 B raw, 2,084,333 ->
+ // 2,084,514 B transfer against 4e2838ec5f, 2026-10-22, one machine and build, Node 24, chunk count unchanged
+ // at 97). Raised by that growth so main's margin stays where it was.
+ transferBytes: 2_085_018,
+ rawBytes: 6_922_543,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/images/image-generation-defaults.ts b/studio/frontend/src/features/images/image-generation-defaults.ts
index 53858a95ffc..dff66849bbb 100644
--- a/studio/frontend/src/features/images/image-generation-defaults.ts
+++ b/studio/frontend/src/features/images/image-generation-defaults.ts
@@ -58,6 +58,9 @@ const MODEL_DEFAULTS: Array<{
{ match: "qwen-image", steps: 20, guidance: 4 },
{ match: "z-image", steps: 25, guidance: 3 },
{ match: "ideogram", steps: 20, guidance: 7 },
+ { match: "moss-image", steps: 50, guidance: 3 },
+ { match: "moss_image", steps: 50, guidance: 3 },
+ { match: "mossimage", steps: 50, guidance: 3 },
{ match: "lumina", steps: 50, guidance: 4 },
{ match: "hunyuanimage", steps: 50, guidance: 3.25 },
{ match: "hidream-i1-dev", steps: 28, guidance: 0 },
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/family-override.ts b/studio/frontend/src/features/model-picker/components/model-selector/family-override.ts
index 669e4dcaaa3..b7752612fd8 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/family-override.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/family-override.ts
@@ -22,6 +22,7 @@ const LABELS: Readonly<Record<string, string>> = {
"hunyuanimage-2.1": "HunyuanImage 2.1",
"hidream-i1": "HiDream-I1",
"ideogram-4": "Ideogram 4",
+ "moss-image": "MOSS-Image",
sdxl: "SDXL",
"minimax-h3": "MiniMax-H3",
"ltx-2": "LTX-2",
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
index a19145a3098..5984600add2 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
@@ -177,6 +177,21 @@ assert.equal(
IMAGE_CATALOG.find((g) => g.pinToTop)?.canonicalId,
"black-forest-labs/flux-3-image-klein",
);
+// MOSS-Image: a dense row on the OpenMOSS id (fetched from the FP8 repo), with sd.cpp GGUF rows in the same group.
+for (const [base, fp8Repo, ggufRepo, name] of [
+ ["OpenMOSS-Team/MOSS-Image-SFT", "unsloth/MOSS-Image-SFT-FP8", "unsloth/MOSS-Image-SFT-GGUF", "MOSS-Image"],
+ ["OpenMOSS-Team/MOSS-Image-Base", "unsloth/MOSS-Image-Base-FP8", "unsloth/MOSS-Image-Base-GGUF", "MOSS-Image Base"],
+] as const) {
+ const group = groupForRepoId(base, IMAGE_CATALOG);
+ assert.ok(group, base);
+ assert.equal(groupForRepoId(fp8Repo, IMAGE_CATALOG), group, fp8Repo);
+ assert.equal(groupForRepoId(ggufRepo, IMAGE_CATALOG), group, ggufRepo);
+ assert.equal(curatedRowLabelFor(base, IMAGE_CATALOG, "dense-quant", ["int8", "fp8"])?.name, `${name} (Fast FP8)`);
+ assert.equal(
+ curatedRowLabelFor(ggufRepo, IMAGE_CATALOG, "dense-quant", ["fp8"])?.name,
+ `${ggufRepo.split("/")[1]} (Slow)`,
+ );
+}
// The pinned Fast FP8 row must survive "Fit on device" on a 24 GB card (6.0 GiB DiT + 8.6 GiB companions, under
// the 70% rule), else the Slow GGUF row takes the top. A host without the dense quant sizes it in bf16 and drops it.
for (const id of ["black-forest-labs/flux-3-image-klein", "black-forest-labs/flux-3-image-klein-base"]) {
@@ -325,6 +340,8 @@ for (const id of [
"unsloth/FLUX.2-klein-9B-GGUF",
"unsloth/flux-3-image-klein-GGUF",
"unsloth/flux-3-image-klein-base-GGUF",
+ "unsloth/MOSS-Image-SFT-GGUF",
+ "unsloth/MOSS-Image-Base-GGUF",
"unsloth/Qwen-Image-Edit-2511-GGUF",
"unsloth/FLUX.1-Kontext-dev-GGUF",
...Object.keys(OLD_SAFETENSORS_MODELS),
@@ -1290,6 +1307,8 @@ const PREQUANT_ROWS = [
["Qwen/Qwen-Image-2.1-Turbo", "unsloth/Qwen-Image-2.1-Turbo-FP8"],
["black-forest-labs/flux-3-image-klein", "unsloth/flux-3-image-klein-FP8"],
["black-forest-labs/flux-3-image-klein-base", "unsloth/flux-3-image-klein-base-FP8"],
+ ["OpenMOSS-Team/MOSS-Image-SFT", "unsloth/MOSS-Image-SFT-FP8"],
+ ["OpenMOSS-Team/MOSS-Image-Base", "unsloth/MOSS-Image-Base-FP8"],
] as const;
for (const [id, repo] of PREQUANT_ROWS) {
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
index 00152a94a0f..df7131df8b2 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
@@ -304,6 +304,40 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
gguf("unsloth/flux-3-image-klein-base-GGUF"),
],
},
+ {
+ // MOSS-Image SFT: the bf16 row keeps the OpenMOSS id but fetches from unsloth/MOSS-Image-SFT-FP8 (upstream
+ // folders mirrored byte-identical). GGUFs are sd.cpp only, VAE and tokenizer from the same repo.
+ canonicalId: "OpenMOSS-Team/MOSS-Image-SFT",
+ displayName: "MOSS-Image",
+ description: "Text-to-image",
+ scope: "image",
+ aliases: ["unsloth/MOSS-Image-SFT-FP8"],
+ artifacts: [
+ // Resident bf16 GiB, as the fit rule reads it: 9.47 DiT + 15.63 Qwen3.5-9B + 0.23 VAE.
+ bf16Pipeline("OpenMOSS-Team/MOSS-Image-SFT", 25.33, {
+ totalParams: 5082308624,
+ prequantRepo: "unsloth/MOSS-Image-SFT-FP8",
+ prequantSizeGb: { fp8: 5.04, int8: 5.04 },
+ }),
+ gguf("unsloth/MOSS-Image-SFT-GGUF"),
+ ],
+ },
+ {
+ // The pre-SFT checkpoint, laid out the same way from its own FP8 repo.
+ canonicalId: "OpenMOSS-Team/MOSS-Image-Base",
+ displayName: "MOSS-Image Base",
+ description: "Text-to-image",
+ scope: "image",
+ aliases: ["unsloth/MOSS-Image-Base-FP8"],
+ artifacts: [
+ bf16Pipeline("OpenMOSS-Team/MOSS-Image-Base", 25.33, {
+ totalParams: 5082308624,
+ prequantRepo: "unsloth/MOSS-Image-Base-FP8",
+ prequantSizeGb: { fp8: 5.04, int8: 5.04 },
+ }),
+ gguf("unsloth/MOSS-Image-Base-GGUF"),
+ ],
+ },
{
canonicalId: "unsloth/Qwen-Image-2.1",
displayName: "Qwen-Image 2.1",
diff --git a/studio/frontend/tests/image-generation-defaults.test.ts b/studio/frontend/tests/image-generation-defaults.test.ts
index 9ee1ad703df..cf1f04e6fe9 100644
--- a/studio/frontend/tests/image-generation-defaults.test.ts
+++ b/studio/frontend/tests/image-generation-defaults.test.ts
@@ -304,6 +304,20 @@ test("an explicit FLUX.3 family decides the recipe over a name that reads as the
);
});
+test("MOSS-Image SFT and Base get ComfyUI's MOSS recipe", () => {
+ for (const id of [
+ "OpenMOSS-Team/MOSS-Image-SFT",
+ "OpenMOSS-Team/MOSS-Image-Base",
+ "someone/moss_image_sft_merge",
+ "moss-image",
+ ]) {
+ assert.deepEqual(defaultsFor(id), { steps: 50, guidance: 3 }, id);
+ }
+ const key = defaultsKeyFor("/models/my-model", "moss-image");
+ assert.equal(key, "moss-image");
+ assert.deepEqual(defaultsFor(key), { steps: 50, guidance: 3 });
+});
+
test("a GGUF filename naming Krea 2 Raw outranks its multi-checkpoint repo", () => {
const repo = "realrebelai/KREA-2_GGUFs";
const raw = { steps: 52, guidance: 3.5 };