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: train and chat with every text-diffusion LM family (#13342)

Daniel Han2e718dcaea37
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py
index aec6246850d..98ec9cbe6de 100644
--- a/studio/backend/core/export/export.py
+++ b/studio/backend/core/export/export.py
@@ -710,6 +710,15 @@ class ExportBackend:
self.is_peft = False
self._audio_type = None
self.decision = None
+ self.diffusion_lm_profile = None
+
+ def _diffusion_refusal(self, method: str) -> Optional[str]:
+ profile = getattr(self, "diffusion_lm_profile", None)
+ if not profile:
+ return None
+ from utils.models.diffusion_lm import export_refusal
+
+ return export_refusal(profile, method)
def cleanup_memory(self):
"""Offload and delete all models from memory"""
@@ -779,6 +788,7 @@ class ExportBackend:
logger.info(f"Loading checkpoint: {checkpoint_path}")
self.cleanup_memory()
+ self.diffusion_lm_profile = None
# Before the base / audio / vision probes: a decision run never reaches the chat loaders.
from core.export.decision import decision_kind
@@ -824,6 +834,18 @@ class ExportBackend:
self.is_vision = not self._audio_type and is_vision_model(
model_id, hf_token = probe_token, local_files_only = local_files_only
)
+ from utils.models.diffusion_lm import diffusion_lm_profile_for_model
+
+ self.diffusion_lm_profile = (
+ None
+ if self._audio_type
+ else diffusion_lm_profile_for_model(
+ model_id, hf_token = probe_token, local_files_only = local_files_only
+ )
+ )
+ if self.diffusion_lm_profile:
+ # DiffusionGemma ships a vision tower; its text-diffusion load is FastModel's.
+ self.is_vision = False
if self._audio_type == "csm":
from unsloth import FastModel
@@ -905,6 +927,20 @@ class ExportBackend:
**_device_map_kw,
)
+ elif self.diffusion_lm_profile:
+ from unsloth import FastModel
+ logger.info(f"Loading as text-diffusion model ({self.diffusion_lm_profile})...")
+ model, tokenizer = FastModel.from_pretrained(
+ model_name = checkpoint_path,
+ max_seq_length = max_seq_length,
+ dtype = None,
+ **_load_in_4bit_kwargs(load_in_4bit),
+ trust_remote_code = trust_remote_code,
+ token = token,
+ local_files_only = local_files_only,
+ **_device_map_kw,
+ )
+
elif self.is_vision:
logger.info("Loading as vision model...")
model, processor = FastVisionModel.from_pretrained(
@@ -952,6 +988,12 @@ class ExportBackend:
f"Restored Hub model identity for legacy adapter export: {restored_repo_id}"
)
+ # A Hub adapter has no config.json for the probe, but core loads a diffusion base through its
+ # diffusion path: classify from what loaded, so the merged / GGUF refusals still apply.
+ if not self.diffusion_lm_profile and not _IS_MLX:
+ from utils.models.diffusion_lm import loaded_diffusion_profile
+ self.diffusion_lm_profile = loaded_diffusion_profile(model)
+
# The MLX GGUF LoRA converter honors the approved load decision.
self.trust_remote_code = bool(trust_remote_code)
self.current_model = model
@@ -960,6 +1002,8 @@ class ExportBackend:
if self._audio_type:
model_type = f"Audio ({self._audio_type})"
+ elif self.diffusion_lm_profile:
+ model_type = f"Text-diffusion ({self.diffusion_lm_profile})"
elif self.is_vision:
model_type = "Vision"
else:
@@ -1131,6 +1175,9 @@ class ExportBackend:
return False, _export_runtime_message(), None
if not self.current_model or not self.current_tokenizer:
return False, "No model loaded. Please select a checkpoint first.", None
+ refusal = self._diffusion_refusal("merged")
+ if refusal:
+ return False, refusal, None
# save_pretrained_merged is a no-op merge for non-PEFT base models, so one path covers both.
@@ -1576,6 +1623,9 @@ class ExportBackend:
)
if not self.current_model or not self.current_tokenizer:
return False, "No model loaded. Please select a checkpoint first.", None
+ refusal = self._diffusion_refusal("gguf")
+ if refusal:
+ return False, refusal, None
# Older unsloth builds raise on an unexpected imatrix_file kwarg even for a plain no-imatrix export.
if imatrix_file and not _imatrix_export_supported(self.current_model.save_pretrained_gguf):
@@ -2131,6 +2181,9 @@ class ExportBackend:
return False, _export_runtime_message(), None
if not self.current_model or not self.current_tokenizer:
return False, "No model loaded. Please select a checkpoint first.", None
+ refusal = self._diffusion_refusal("gguf") if gguf else None
+ if refusal:
+ return False, refusal, None
if not self.is_peft:
return False, "This is not a PEFT model. No adapter to export.", None
diff --git a/studio/backend/core/inference/inference.py b/studio/backend/core/inference/inference.py
index c4622f3a114..3db5be503af 100644
--- a/studio/backend/core/inference/inference.py
+++ b/studio/backend/core/inference/inference.py
@@ -1024,6 +1024,19 @@ class InferenceBackend:
exact_model_name = _exact_model_name_for_load(config, load_in_4bit)
use_exact_model_name = exact_model_name is not None
load_path = exact_model_name or config.path
+ from utils.models.diffusion_lm import (
+ diffusion_lm_profile_for_model,
+ loaded_diffusion_profile,
+ )
+
+ diffusion_lm_profile = diffusion_lm_profile_for_model(
+ load_path, hf_token = _hf_token_for_loader(hf_token)
+ )
+ if not diffusion_lm_profile and config.is_lora and config.base_model:
+ # A Hub LoRA has no config.json: classify its base before the vision branch claims DiffusionGemma.
+ diffusion_lm_profile = diffusion_lm_profile_for_model(
+ config.base_model, hf_token = _hf_token_for_loader(hf_token)
+ )
# A LoRA loads its base model.
offload_kwargs = _offload_kwargs_for_fit(
config.base_model if config.is_lora and config.base_model else load_path,
@@ -1032,7 +1045,39 @@ class InferenceBackend:
_hf_token_for_loader(hf_token),
mapped = not use_exact_model_name,
)
- if config.is_vision:
+ if diffusion_lm_profile:
+ # Text-diffusion LMs (LLaDA, Dream, DiffusionGemma, ...) decode with their own sampler,
+ # never left-to-right generate(); DiffusionGemma's vision tower is not used here.
+ from unsloth import FastModel
+
+ model, tokenizer = FastModel.from_pretrained(
+ model_name = load_path,
+ max_seq_length = max_seq_length,
+ dtype = dtype,
+ **_load_in_4bit_kwargs(load_in_4bit),
+ device_map = device_map,
+ token = _hf_token_for_loader(hf_token),
+ trust_remote_code = trust_remote_code,
+ use_exact_model_name = use_exact_model_name,
+ )
+ FastModel.for_inference(model)
+ loaded_profile = loaded_diffusion_profile(model)
+ if not loaded_profile:
+ # As training does: an autoregressive load must never reach the diffusion sampler.
+ raise RuntimeError(
+ f"'{load_path}' is a text-diffusion model ({diffusion_lm_profile}), but the installed "
+ "Unsloth loaded it as an ordinary model. Update with `unsloth studio update`."
+ )
+ self.models[model_name]["diffusion_lm_profile"] = loaded_profile
+ self.models[model_name]["is_vision"] = False
+ self.models[model_name]["model"] = model
+ self.models[model_name]["tokenizer"] = tokenizer
+ logger.info(
+ f"Loaded text-diffusion model for chat (profile: "
+ f"{self.models[model_name]['diffusion_lm_profile']})"
+ )
+
+ elif config.is_vision:
model, processor = _load_with_offload_fallback(
FastVisionModel.from_pretrained,
model_name = load_path,
@@ -1101,6 +1146,12 @@ class InferenceBackend:
self.models[model_name]["model"] = model
self.models[model_name]["tokenizer"] = tokenizer
+ # A Hub LoRA has no config.json for the probe above, but core still loads a diffusion base
+ # through its diffusion path: decode it with its sampler, never left-to-right.
+ loaded_profile = loaded_diffusion_profile(model)
+ if loaded_profile:
+ self.models[model_name]["diffusion_lm_profile"] = loaded_profile
+ logger.info(f"Loaded text-diffusion model for chat (profile: {loaded_profile})")
raise_if_offloaded(self.models[model_name]["model"], device_map, "Inference")
self.models[model_name]["context_length"] = runtime_context_length(
@@ -1564,6 +1615,22 @@ class InferenceBackend:
raise RuntimeError("No active model")
model_info = self.models[self.active_model_name]
+ if model_info.get("diffusion_lm_profile"):
+ template_messages = (
+ [{"role": "system", "content": system_prompt}] + messages
+ if system_prompt
+ else messages
+ )
+ yield from self._generate_diffusion_lm(
+ model_info,
+ messages = _without_image_parts(template_messages),
+ temperature = temperature,
+ max_new_tokens = max_new_tokens,
+ cancel_event = cancel_event,
+ _adapter_state = _adapter_state,
+ stop = stop,
+ )
+ return
is_vision = model_info.get("is_vision", False)
tokenizer = model_info.get("tokenizer") or model_info.get("processor")
tokenizer = getattr(tokenizer, "tokenizer", tokenizer)
@@ -2537,6 +2604,66 @@ class InferenceBackend:
)
yield cleaned
+ def _generate_diffusion_lm(
+ self,
+ model_info: dict,
+ prompt: Optional[str] = None,
+ messages: Optional[list] = None,
+ temperature: float = 0.0,
+ max_new_tokens: Optional[int] = 256,
+ cancel_event = None,
+ _adapter_state = None,
+ stop: Optional[list] = None,
+ ) -> Generator[str, None, None]:
+ """A text-diffusion LM fills a whole block at once, so the answer arrives as one completion: there is
+ no left-to-right token stream to forward. Holds the generation lock until the sampler returns, and
+ the sampler polls ``cancel_event`` between denoising steps."""
+ # No token stats come back from the sampler; a previous request's must not be reported for this one.
+ self.last_generation_stats = None
+ from utils.models.diffusion_lm import require_diffusion_api
+
+ if messages:
+ # Same choke point as the left-to-right renderer: role / turn markup inside message text is
+ # neutralised before the sampler renders the chat template.
+ from core.inference.chat_template_helpers import neutralize_for_render
+ tokenizer = model_info["tokenizer"]
+ messages, _, _ = neutralize_for_render(
+ getattr(tokenizer, "tokenizer", tokenizer), messages, None
+ )
+ generate_text = getattr(require_diffusion_api("FastDiffusionModel"), "generate_text", None)
+ if generate_text is None:
+ raise RuntimeError(
+ "Unsloth: chatting with a text-diffusion model needs a newer Unsloth "
+ "(FastDiffusionModel.generate_text). Update with `unsloth studio update`."
+ )
+ try:
+ from unsloth.models.diffusion_samplers import (
+ DiffusionGenerationCancelled as cancelled_error,
+ )
+ except ImportError: # an older core: no cancellation error to catch
+ cancelled_error = ()
+ with self._generation_lock:
+ if _adapter_state is not None:
+ self._apply_adapter_state(_adapter_state)
+ try:
+ text = generate_text(
+ model_info["model"],
+ model_info["tokenizer"],
+ prompt = prompt,
+ messages = messages,
+ max_new_tokens = int(max_new_tokens or 256),
+ temperature = float(temperature or 0.0),
+ stop_callback = (lambda: cancel_event is not None and cancel_event.is_set()),
+ )
+ except cancelled_error:
+ return
+ text = text or ""
+ for sequence in stop or ():
+ if sequence and sequence in text:
+ text = text[: text.index(sequence)]
+ if text and not (cancel_event is not None and cancel_event.is_set()):
+ yield text
+
def generate_stream(
self,
prompt: str,
@@ -2572,6 +2699,17 @@ class InferenceBackend:
self.last_generation_stats = None
model_info = self.models[self.active_model_name]
+ if model_info.get("diffusion_lm_profile"):
+ yield from self._generate_diffusion_lm(
+ model_info,
+ prompt = prompt,
+ temperature = temperature,
+ max_new_tokens = max_new_tokens,
+ cancel_event = cancel_event,
+ _adapter_state = _adapter_state,
+ stop = stop,
+ )
+ return
model = model_info["model"]
# For VLMs the stored "tokenizer" is actually the processor. Unwrap to
# the real tokenizer so TextIteratorStreamer's skip_prompt /
diff --git a/studio/backend/core/inference/worker.py b/studio/backend/core/inference/worker.py
index 5e7fa0a59b1..b719b600364 100644
--- a/studio/backend/core/inference/worker.py
+++ b/studio/backend/core/inference/worker.py
@@ -2054,7 +2054,11 @@ def run_inference_process(
if _hw.DEVICE == _hw.DeviceType.MLX and not _native_audio_worker:
try:
from core.inference.mlx_inference import MLXInferenceBackend, _init_mlx_distributed
+ from utils.models.diffusion_lm import mlx_refusal
+ refusal = mlx_refusal(model_name, _config_hf_token(config))
+ if refusal:
+ raise RuntimeError(refusal)
backend = MLXInferenceBackend()
if config.get("mlx_distributed"):
group, rank, size = _init_mlx_distributed()
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index 33cf69cd9aa..29830d06308 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -72,6 +72,11 @@ from utils.third_party_source import (
)
from utils.models import is_vision_model, detect_audio_type_checked
+from utils.models.diffusion_lm import (
+ diffusion_lm_profile_for_model,
+ loaded_diffusion_profile,
+ require_diffusion_api,
+)
from utils.models.model_identity import restore_hf_cache_repo_identity
from utils.models.unsloth_mirror import unsloth_public_mirror
from utils.models.model_config import _env_offline
@@ -102,6 +107,17 @@ from .dataset_bounds import bound_dataset_rows, world_size_env_report, world_siz
logger = get_logger(__name__)
+_STUDIO_DEFAULT_TARGETS = (
+ "q_proj",
+ "k_proj",
+ "v_proj",
+ "o_proj",
+ "gate_proj",
+ "up_proj",
+ "down_proj",
+)
+_CPT_EMBEDDING_TARGETS = ("embed_tokens", "lm_head")
+
# Streaming eval has no __len__, so cap it or an unbounded eval iterates the whole source on every eval step.
STREAMING_EVAL_MAX_SAMPLES = 500
@@ -141,6 +157,39 @@ def _hf_stdout_progress_disabled() -> bool:
return not _verbose_logging_requested()
+_EMBEDDING_GETTERS = {"embed_tokens": "get_input_embeddings", "lm_head": "get_output_embeddings"}
+
+
+def _embedding_module_path(model, name: str):
+ """Full module path of the model's input embedding / output head for Studio's generic CPT names."""
+ getter = getattr(model, _EMBEDDING_GETTERS.get(name, ""), None)
+ if not callable(getter):
+ return None
+ try:
+ module = getter()
+ except Exception:
+ return None
+ if module is None:
+ return None
+ return next((path for path, m in model.named_modules() if m is module and path), None)
+
+
+def _tokenizer_blocks_map_workers(tokenizer) -> bool:
+ """True when `dataset_num_proc` workers cannot receive this tokenizer. dill pickles a remote-code
+ tokenizer class by value (transformers 5.5 does not set its module on the parent package), dragging
+ in module globals up to huggingface_hub's live HTTP client: "cannot pickle 'SSLContext' object"
+ (Dream's DreamTokenizer). Stock tokenizer classes pickle by reference, so only those are probed."""
+ inner = getattr(tokenizer, "tokenizer", tokenizer)
+ if not str(getattr(type(inner), "__module__", "")).startswith("transformers_modules"):
+ return False
+ try:
+ import dill
+ dill.dumps(tokenizer)
+ except Exception:
+ return True
+ return False
+
+
# Keys that only ever appear on Trainer's end-of-run / end-of-eval summary record.
_TRAINER_SUMMARY_KEYS = (
"train_runtime",
@@ -504,6 +553,7 @@ class UnslothTrainer:
self.is_audio = False
self.is_audio_vlm = False
self._audio_type = None
+ self.diffusion_lm_profile = None
self._use_gradient_checkpointing = "unsloth"
self._offload_layers = 0
self._prefetch_depth = 2
@@ -596,13 +646,15 @@ class UnslothTrainer:
else False
)
self.is_vlm = not self.is_audio_vlm and vision and is_dataset_image
+ self._detect_diffusion_lm(lookup_name, hf_token, local_files_only, model_revision)
logger.info(
- "pre_detect: audio_type=%s, is_audio=%s, is_audio_vlm=%s, is_vlm=%s",
+ "pre_detect: audio_type=%s, is_audio=%s, is_audio_vlm=%s, is_vlm=%s, diffusion_lm=%s",
self._audio_type,
self.is_audio,
self.is_audio_vlm,
self.is_vlm,
+ self.diffusion_lm_profile,
)
# Load tokenizer/processor (CPU only, no VRAM). Whisper needs AutoProcessor; others AutoTokenizer (CSM loads
@@ -718,6 +770,25 @@ class UnslothTrainer:
def spark_tts_hub_repo(self) -> str:
return _spark_tts_hub_repo(self._spark_tts_repo_source[0])
+ def _detect_diffusion_lm(self, lookup_name, hf_token, local_files_only, model_revision):
+ """Text-diffusion LMs train with their own denoising objective on the text path, never the VLM one
+ (DiffusionGemma ships a vision tower the diffusion objective does not use)."""
+ self.diffusion_lm_profile = (
+ None
+ if self.is_audio or self.is_audio_vlm
+ else diffusion_lm_profile_for_model(
+ lookup_name,
+ hf_token = hf_token,
+ local_files_only = local_files_only,
+ revision = model_revision,
+ )
+ )
+ if self.diffusion_lm_profile and self.is_vlm:
+ self.is_vlm = False
+ self._record_warning(
+ "Text-diffusion models train on text only here; the dataset's images are ignored."
+ )
+
def add_progress_callback(self, callback: Callable[[TrainingProgress], None]):
self.progress_callbacks.append(callback)
@@ -1254,6 +1325,7 @@ class UnslothTrainer:
else False
)
self.is_vlm = not self.is_audio_vlm and vision and is_dataset_image
+ self._detect_diffusion_lm(metadata_name, hf_token, local_files_only, model_revision)
self.model_name = model_name
self.max_seq_length = max_seq_length
@@ -1273,7 +1345,15 @@ class UnslothTrainer:
)
model_display = model_name.split("/")[-1] if "/" in model_name else model_name
- model_type_label = "audio" if self.is_audio else ("vision" if self.is_vlm else "text")
+ model_type_label = (
+ "audio"
+ if self.is_audio
+ else (
+ "vision"
+ if self.is_vlm
+ else ("text-diffusion" if self.diffusion_lm_profile else "text")
+ )
+ )
self._update_progress(
status_message = f"Loading {model_type_label} model... {model_display}"
)
@@ -1473,6 +1553,34 @@ class UnslothTrainer:
)
logger.info("Loaded audio VLM model (FastModel)")
+ elif self.diffusion_lm_profile:
+ # FastModel routes every registered text-diffusion family to its diffusion loader.
+ from unsloth import FastModel
+
+ self.model, self.tokenizer = FastModel.from_pretrained(
+ model_name = lookup_name,
+ max_seq_length = max_seq_length,
+ dtype = _auto_dtype,
+ load_in_4bit = load_in_4bit,
+ device_map = device_map,
+ full_finetuning = full_finetuning,
+ token = hf_token,
+ trust_remote_code = trust_remote_code,
+ revision = model_revision,
+ use_exact_model_name = model_revision is not None,
+ use_gradient_checkpointing = use_gradient_checkpointing,
+ on_model_resolved = on_model_resolved,
+ )
+ loaded = loaded_diffusion_profile(self.model)
+ if loaded is None:
+ raise RuntimeError(
+ f"Unsloth: {model_name} looks like a text-diffusion model "
+ f"({self.diffusion_lm_profile}) but loaded as an autoregressive one, so it "
+ "would train on the wrong objective. Update Unsloth (`unsloth studio update`)."
+ )
+ self.diffusion_lm_profile = loaded
+ logger.info(f"Loaded text-diffusion model (profile: {loaded})")
+
elif self.is_vlm:
self.model, self.tokenizer = FastVisionModel.from_pretrained(
model_name = lookup_name,
@@ -1520,6 +1628,12 @@ class UnslothTrainer:
**self._offload_load_kwargs(device_map),
)
logger.info("Loaded text model")
+ # The config probe misses a Hub LoRA (no config.json), but core loads a diffusion base through
+ # its diffusion path: train it with its own objective, never next-token SFT.
+ loaded = loaded_diffusion_profile(self.model)
+ if loaded:
+ self.diffusion_lm_profile = loaded
+ logger.info(f"Loaded text-diffusion model (profile: {loaded})")
raise_if_offloaded(self.model, device_map, "Unsloth training")
@@ -1536,7 +1650,12 @@ class UnslothTrainer:
if self.should_stop:
return False
- if full_finetuning:
+ if full_finetuning and self.diffusion_lm_profile:
+ from unsloth import FastModel
+ FastModel.for_training(
+ self.model, use_gradient_checkpointing = bool(use_gradient_checkpointing)
+ )
+ elif full_finetuning:
self.model.for_training(
use_gradient_checkpointing = use_gradient_checkpointing,
)
@@ -1619,6 +1738,79 @@ class UnslothTrainer:
finally:
self._source_code_retried = False
+ def _prepare_diffusion_lora(
+ self,
+ target_modules,
+ modules_to_save,
+ lora_r,
+ lora_alpha,
+ lora_dropout,
+ use_gradient_checkpointing,
+ use_rslora,
+ use_loftq,
+ use_dora,
+ ) -> bool:
+ """LoRA for a text-diffusion model through its profile. Studio's default target list names
+ autoregressive projections (LLaDA has none of o/gate/down_proj, and the MoE families must keep their
+ experts and router frozen), so the default list means "the profile's own targets"."""
+ from unsloth import FastModel
+
+ unsupported = [name for name, on in (("DoRA", use_dora), ("LoftQ", use_loftq)) if on]
+ if unsupported:
+ raise ValueError(
+ f"{' and '.join(unsupported)} {'is' if len(unsupported) == 1 else 'are'} not supported "
+ "for text-diffusion models yet. Turn it off and train with LoRA."
+ )
+ requested = (
+ [target_modules] if isinstance(target_modules, str) else list(target_modules or [])
+ )
+ if not requested or set(requested) - set(_CPT_EMBEDDING_TARGETS) == set(
+ _STUDIO_DEFAULT_TARGETS
+ ):
+ target_modules = None
+ elif requested == ["all-linear"]:
+ target_modules = "all-linear"
+ else:
+ target_modules = [m for m in requested if m != "all-linear"] or "all-linear"
+ leaf_names = {name.rsplit(".", 1)[-1] for name, _ in self.model.named_modules()}
+ if modules_to_save:
+ # LLaDA names them transformer.wte / transformer.ff_out; the full path keeps each block's ff_out out.
+ resolved = {
+ m: m if m in leaf_names else _embedding_module_path(self.model, m)
+ for m in modules_to_save
+ }
+ kept = list(dict.fromkeys(path for path in resolved.values() if path))
+ dropped = sorted(m for m, path in resolved.items() if not path)
+ if dropped:
+ logger.info(
+ f"Text-diffusion model has no {dropped}; training them is skipped for this model.\n"
+ )
+ modules_to_save = kept or None
+ logger.info(
+ f"Text-diffusion LoRA ({self.diffusion_lm_profile}): target modules = "
+ f"{'profile default' if target_modules is None else target_modules}\n"
+ )
+ peft_kwargs = dict(
+ r = lora_r,
+ target_modules = target_modules,
+ lora_alpha = lora_alpha,
+ lora_dropout = lora_dropout,
+ bias = "none",
+ use_gradient_checkpointing = bool(use_gradient_checkpointing),
+ random_state = 3407,
+ )
+ if modules_to_save:
+ peft_kwargs["modules_to_save"] = modules_to_save
+ if use_rslora:
+ peft_kwargs["use_rslora"] = True
+ self.model = FastModel.get_peft_model(self.model, **peft_kwargs)
+ if self.should_stop:
+ logger.info("Stopped during LoRA configuration\n")
+ return False
+ self._update_progress(status_message = "LoRA adapters configured")
+ logger.info("LoRA adapters configured successfully\n")
+ return True
+
def prepare_model_for_training(
self,
use_lora: bool = True,
@@ -1691,6 +1883,19 @@ class UnslothTrainer:
self._update_progress(status_message = LOFTQ_SKIPPED_ON_QUANTIZED)
use_loftq = False
+ if getattr(self, "diffusion_lm_profile", None):
+ return self._prepare_diffusion_lora(
+ target_modules = target_modules,
+ modules_to_save = modules_to_save,
+ lora_r = lora_r,
+ lora_alpha = lora_alpha,
+ lora_dropout = lora_dropout,
+ use_gradient_checkpointing = use_gradient_checkpointing,
+ use_rslora = use_rslora,
+ use_loftq = use_loftq,
+ use_dora = use_dora,
+ )
+
# LoRA/QLoRA. "all-linear" is a PEFT keyword targeting every linear layer.
if isinstance(target_modules, list) and "all-linear" in target_modules:
if len(target_modules) == 1:
@@ -4500,7 +4705,14 @@ class UnslothTrainer:
# to keep dataset workers off a process holding audio/CUDA state; pass None otherwise, so the shared
# policy sizes it from CPU affinity and cgroup quota rather than host os.cpu_count().
"dataset_num_proc": dataset_map_num_proc(
- 1 if (self.is_audio or self.is_audio_vlm or self._cuda_audio_used) else None,
+ 1
+ if (
+ self.is_audio
+ or self.is_audio_vlm
+ or self._cuda_audio_used
+ or _tokenizer_blocks_map_workers(self.tokenizer)
+ )
+ else None,
serial_as_none = False,
),
"max_seq_length": training_args.get("max_seq_length", 2048),
@@ -4633,6 +4845,10 @@ class UnslothTrainer:
elif self._audio_type == "dac":
config_args["packing"] = False
logger.info("Applied DAC overrides: packing=False\n")
+ if getattr(self, "diffusion_lm_profile", None) and config_args.get("packing"):
+ # Packed rows share one bidirectional attention window, so documents would see each other.
+ config_args["packing"] = False
+ logger.info("Text-diffusion model: sequence packing turned off\n")
# Plain-text single-pass runs tokenize in the DataLoader workers instead of a blocking .map(); everything
# else stays eager.
@@ -4696,7 +4912,47 @@ class UnslothTrainer:
logger.info("Unwrapping Processor → raw tokenizer for text-only SFTTrainer")
sft_tokenizer = self.tokenizer.tokenizer
- if is_cpt:
+ if getattr(self, "diffusion_lm_profile", None):
+ # Same SFT data path; the loss is the family's denoising objective. CPT = every token
+ # supervised, which is what the objective does when no label is -100.
+ DiffusionTrainer = require_diffusion_api("DiffusionTrainer")
+ DiffusionConfig = require_diffusion_api("DiffusionConfig")
+ fields = getattr(DiffusionConfig, "__dataclass_fields__", {})
+ diffusion_args = {k: v for k, v in config_args.items() if k in fields}
+ if (
+ "max_seq_length" in config_args
+ and "max_seq_length" not in fields
+ and "max_length" in fields
+ ):
+ # Unpatched TRL (CPU runs) only knows max_length: keep the chosen context length.
+ diffusion_args.setdefault("max_length", config_args["max_seq_length"])
+ embedding_lr = training_args.get("embedding_learning_rate") if is_cpt else None
+ if embedding_lr is not None:
+ if "embedding_learning_rate" not in fields:
+ raise RuntimeError(
+ "Continued pretraining of a text-diffusion model with an embedding "
+ "learning rate needs a newer Unsloth. Upgrade with: "
+ "`pip install -U unsloth unsloth_zoo`."
+ )
+ diffusion_args["embedding_learning_rate"] = embedding_lr
+ skipped = sorted(set(config_args) - set(diffusion_args))
+ if skipped:
+ logger.info(f"DiffusionConfig has no {skipped}; not passed\n")
+ trainer_kwargs = {
+ "model": self.model,
+ "tokenizer": sft_tokenizer,
+ "train_dataset": dataset["dataset"],
+ "data_collator": data_collator,
+ "args": DiffusionConfig(**diffusion_args),
+ }
+ if eval_dataset is not None:
+ trainer_kwargs["eval_dataset"] = eval_dataset
+ self.trainer = DiffusionTrainer(**trainer_kwargs)
+ logger.info(
+ f"Text-diffusion trainer: {type(self.trainer).__name__} "
+ f"(profile {self.diffusion_lm_profile})\n"
+ )
+ elif is_cpt:
try:
from unsloth import (
UnslothTrainer as _UnslothCPTTrainer,
diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.py
index b21cb20629b..63e3312853f 100644
--- a/studio/backend/core/training/worker.py
+++ b/studio/backend/core/training/worker.py
@@ -519,6 +519,13 @@ def _verify_config_pins(config: dict, event_queue: Any) -> bool:
return True
+def _mlx_diffusion_lm_error(config: dict, model_load_target: str) -> str | None:
+ from utils.models.diffusion_lm import mlx_refusal
+ return mlx_refusal(
+ model_load_target, _worker_hf_token(config), revision = config.get("model_revision")
+ )
+
+
def _validate_training_worker_config(config: dict, event_queue: Any) -> bool:
if not _verify_config_pins(config, event_queue):
return False
@@ -841,6 +848,36 @@ def _pre_detect_training_model(
load_in_4bit = _pre_detect_load_in_4bit(config, model_load_name, hf_token),
)
_check_finetune_targets_after_detect(trainer, config)
+ _apply_diffusion_lm_overrides(trainer, config)
+
+
+_LORA_DEFAULT_LR = 2e-4
+# Near-duplicate head rows for <|im_end|> / <tool_call> / ... (cosine 0.97): a 2e-4 LoRA swaps them in chat.
+_DIFFUSION_LM_LORA_LR = {"dream": 5e-5, "sdar": 5e-5}
+
+
+def _apply_diffusion_lm_overrides(trainer, config: dict) -> None:
+ """Text-diffusion runs never pack: packed rows would share one bidirectional attention window. Set before
+ the row bound and online tokenization read the flag, so their accounting matches what trains."""
+ profile = getattr(trainer, "diffusion_lm_profile", None)
+ if not profile:
+ return
+ config["diffusion_lm_profile"] = profile
+ if config.get("packing"):
+ config["packing"] = False
+ logger.info("Text-diffusion model (%s): sequence packing turned off", profile)
+ lowered = _DIFFUSION_LM_LORA_LR.get(profile)
+ try:
+ at_default = float(config.get("learning_rate", _LORA_DEFAULT_LR)) == _LORA_DEFAULT_LR
+ except (TypeError, ValueError):
+ at_default = False # the trainer reports the invalid value itself
+ if lowered is not None and at_default:
+ config["learning_rate"] = lowered
+ logger.info(
+ "Text-diffusion model (%s): learning rate 2e-4 lowered to %g; set any other value to override",
+ profile,
+ lowered,
+ )
_NOTHING_TO_TRAIN = (
@@ -3855,6 +3892,12 @@ def run_training_process(*, event_queue: Any, stop_queue: Any, config: dict) ->
_hw.detect_hardware()
if mlx_backend_requested or should_use_mlx_training_backend(device = _hw.DEVICE):
+ diffusion_error = _mlx_diffusion_lm_error(config, model_load_target)
+ if diffusion_error:
+ event_queue.put(
+ {"type": "error", "error": diffusion_error, "stack": "", "ts": time.time()}
+ )
+ return
run_mlx_training_process(
event_queue = event_queue,
stop_queue = stop_queue,
@@ -4386,6 +4429,24 @@ def run_training_process(*, event_queue: Any, stop_queue: Any, config: dict) ->
)
return
+ if config.get("offload_layers"):
+ from utils.models.diffusion_lm import diffusion_lm_profile_for_model
+
+ # The diffusion load has no block swapping; off before the budget below sizes itself to offloading.
+ _diffusion = diffusion_lm_profile_for_model(
+ model_load_target, _worker_hf_token(config), revision = config.get("model_revision")
+ )
+ if _diffusion:
+ config["offload_layers"] = 0
+ event_queue.put(
+ {
+ "type": "warning",
+ "message": f"Layer offloading is not available for text-diffusion models ({_diffusion}); "
+ "training without it.",
+ "ts": time.time(),
+ }
+ )
+
# Offload layers sizes "auto" to what the allocator may use, so a budget makes the run fit in it,
# and two runs on one card can each take their share.
# ── 2b. Training VRAM budget ──
diff --git a/studio/backend/models/export.py b/studio/backend/models/export.py
index 93473d839d8..69281b599c7 100644
--- a/studio/backend/models/export.py
+++ b/studio/backend/models/export.py
@@ -148,11 +148,22 @@ class DecisionExportInfo(BaseModel):
)
+class DiffusionLmExportInfo(BaseModel):
+ """Export limits for a text-diffusion LM checkpoint (LLaDA, Dream, SDAR, DiffusionGemma, ...)."""
+
+ profile: str = Field(..., description = "Text-diffusion family of the checkpoint's base model")
+ disabled_methods: List[str] = Field(
+ default_factory = list, description = "Export methods this checkpoint cannot use"
+ )
+ reason: Optional[str] = Field(None, description = "Why those methods are unavailable")
+
+
class ExportDecisionInfoResponse(BaseModel):
"""Decision-model export info for a checkpoint path; decision is None for any other model."""
checkpoint_path: str
decision: Optional[DecisionExportInfo] = None
+ diffusion_lm: Optional[DiffusionLmExportInfo] = None
class ExportOperationResponse(BaseModel):
diff --git a/studio/backend/routes/export.py b/studio/backend/routes/export.py
index a3d88288959..3dd58212e84 100644
--- a/studio/backend/routes/export.py
+++ b/studio/backend/routes/export.py
@@ -391,6 +391,7 @@ def _decision_export_details(output_path: Optional[str]) -> Optional[Dict[str, A
async def get_decision_export_info(
checkpoint_path: str = Query(..., description = "Local checkpoint folder"),
current_subject: str = Depends(get_current_subject),
+ allow_ambient: bool = Depends(allow_ambient_hf_token),
):
"""GGUF export options for a decision model checkpoint (Clef / Laya); decision is null otherwise."""
validate_job_paths({"checkpoint_path": checkpoint_path})
@@ -401,7 +402,20 @@ async def get_decision_export_info(
except Exception as e:
logger.warning(f"Could not inspect {checkpoint_path} for decision export: {e}")
decision = None
- return ExportDecisionInfoResponse(checkpoint_path = checkpoint_path, decision = decision)
+ from utils.models.diffusion_lm import diffusion_lm_export_limits
+
+ diffusion_lm = (
+ None
+ if decision is not None
+ else await asyncio.to_thread(
+ diffusion_lm_export_limits,
+ checkpoint_path,
+ hf_token_arg(None, allow_ambient_token = allow_ambient),
+ )
+ )
+ return ExportDecisionInfoResponse(
+ checkpoint_path = checkpoint_path, decision = decision, diffusion_lm = diffusion_lm
+ )
@router.post("/export/merged", response_model = ExportOperationResponse)
diff --git a/studio/backend/tests/test_diffusion_lm_studio.py b/studio/backend/tests/test_diffusion_lm_studio.py
new file mode 100644
index 00000000000..f45344099ae
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_lm_studio.py
@@ -0,0 +1,813 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Text-diffusion LMs (LLaDA, LLaDA-MoE, Dream, Nemotron-Labs-Diffusion, LLaDA2, SDAR, DiffusionGemma) in
+Studio: detection, LoRA targets, trainer choice, packing, export limits and chat dispatch. Every other model
+keeps its existing path."""
+
+from __future__ import annotations
+
+import importlib.util
+import inspect
+import json
+import threading
+import sys
+import types
+from types import SimpleNamespace
+
+import pytest
+
+from utils.models import diffusion_lm
+from utils.models.diffusion_lm import (
+ DIFFUSION_LM_PROFILES,
+ diffusion_lm_export_limits,
+ diffusion_lm_profile,
+ diffusion_lm_profile_for_model,
+ export_refusal,
+)
+
+# Raw config.json fields of the published checkpoints (tiny copies carry the same ones).
+DIFFUSION_CONFIGS = {
+ "llada": {"model_type": "llada", "architectures": ["LLaDAModelLM"]},
+ "llada_moe": {"model_type": "llada", "architectures": ["LLaDAMoEModelLM"]},
+ "dream": {"model_type": "Dream", "architectures": ["DreamModel"]},
+ "nemotron_labs_diffusion": {
+ "model_type": "nemotron_labs_diffusion",
+ "architectures": ["NemotronLabsDiffusionModel"],
+ },
+ "llada2": {"model_type": "llada2_moe", "architectures": ["LLaDA2MoeModelLM"]},
+ "sdar": {"model_type": "sdar", "architectures": ["SDARForCausalLM"]},
+ "diffusion_gemma": {
+ "model_type": "diffusion_gemma4",
+ "architectures": ["DiffusionGemma4ForBlockDiffusion"],
+ },
+}
+AUTOREGRESSIVE_CONFIGS = [
+ {"model_type": "llama", "architectures": ["LlamaForCausalLM"]},
+ {"model_type": "qwen3", "architectures": ["Qwen3ForCausalLM"]},
+ {"model_type": "gemma3", "architectures": ["Gemma3ForConditionalGeneration"]},
+ {"model_type": "gemma4", "architectures": ["Gemma4ForConditionalGeneration"]},
+ # Autoregressive Nemotron shares the backbone name, never the diffusion profile.
+ {"model_type": "nemotron", "architectures": ["NemotronForCausalLM"]},
+ {"model_type": "qwen3_moe", "architectures": ["Qwen3MoeForCausalLM"]},
+]
+
+# Need torch and unsloth_zoo, which the cross-platform and backend CI legs run some tests without.
+_needs_torch = pytest.mark.skipif(
+ any(importlib.util.find_spec(name) is None for name in ("torch", "unsloth_zoo")),
+ reason = "needs torch and unsloth_zoo",
+)
+
+
[email protected]("profile", sorted(DIFFUSION_CONFIGS))
+def test_each_family_is_detected_from_raw_config(profile):
+ assert diffusion_lm_profile(DIFFUSION_CONFIGS[profile]) == profile
+
+
[email protected]("config", AUTOREGRESSIVE_CONFIGS, ids = lambda c: c["model_type"])
+def test_autoregressive_models_are_not_diffusion(config):
+ assert diffusion_lm_profile(config) is None
+
+
+def test_legacy_diffusion_gemma_type_and_odd_inputs():
+ assert diffusion_lm_profile({"model_type": "diffusion_gemma"}) == "diffusion_gemma"
+ assert diffusion_lm_profile({"architectures": "SDARForCausalLM"}) == "sdar"
+ assert diffusion_lm_profile(None) is None
+ assert diffusion_lm_profile({}) is None
+
+
+@_needs_torch
+def test_mirror_matches_the_core_registry():
+ """Same names, same model_types / architectures, same order as Unsloth's own profile registry."""
+ profiles_mod = pytest.importorskip("unsloth.models.diffusion_profiles")
+ core = [
+ (p.name, tuple(p.model_types), tuple(p.architectures))
+ for p in profiles_mod.diffusion_profiles()
+ ]
+ assert core == [(n, tuple(m), tuple(a)) for n, m, a in DIFFUSION_LM_PROFILES]
+ for name, cfg in DIFFUSION_CONFIGS.items():
+ resolved = profiles_mod.resolve_diffusion_profile(SimpleNamespace(**cfg))
+ assert resolved is not None and resolved.name == name
+
+
+def _write_config(path, config):
+ path.mkdir(parents = True, exist_ok = True)
+ (path / "config.json").write_text(json.dumps(config), encoding = "utf-8")
+ return path
+
+
+def test_local_checkpoint_and_adapter_resolve_to_the_base(tmp_path):
+ base = _write_config(tmp_path / "base", DIFFUSION_CONFIGS["sdar"])
+ assert diffusion_lm_profile_for_model(str(base)) == "sdar"
+ adapter = tmp_path / "adapter"
+ adapter.mkdir()
+ (adapter / "adapter_config.json").write_text(
+ json.dumps({"base_model_name_or_path": str(base)}), encoding = "utf-8"
+ )
+ assert diffusion_lm_profile_for_model(str(adapter)) == "sdar"
+ ar = _write_config(tmp_path / "ar", AUTOREGRESSIVE_CONFIGS[0])
+ assert diffusion_lm_profile_for_model(str(ar)) is None
+ # An unreadable folder is "not detected", never an exception.
+ assert diffusion_lm_profile_for_model(str(tmp_path / "missing")) is None
+
+
+def test_adapter_pointing_at_itself_does_not_loop(tmp_path):
+ adapter = tmp_path / "adapter"
+ adapter.mkdir()
+ (adapter / "adapter_config.json").write_text(
+ json.dumps({"base_model_name_or_path": str(adapter)}), encoding = "utf-8"
+ )
+ assert diffusion_lm_profile_for_model(str(adapter)) is None
+
+
+def test_export_refusals():
+ assert export_refusal(None, "gguf") is None
+ for profile in DIFFUSION_CONFIGS:
+ assert "GGUF" in export_refusal(profile, "gguf")
+ assert export_refusal(profile, "lora") is None
+ assert "DiffusionGemma" in export_refusal("diffusion_gemma", "merged")
+ assert "LoRA adapter" in export_refusal("llada", "merged")
+
+
+def test_export_limits_for_a_checkpoint_folder(tmp_path):
+ gemma = _write_config(tmp_path / "gemma", DIFFUSION_CONFIGS["diffusion_gemma"])
+ assert diffusion_lm_export_limits(str(gemma))["disabled_methods"] == ["merged", "gguf"]
+ llada = _write_config(tmp_path / "llada", DIFFUSION_CONFIGS["llada"])
+ limits = diffusion_lm_export_limits(str(llada))
+ assert limits["profile"] == "llada" and limits["disabled_methods"] == ["merged", "gguf"]
+ assert (
+ diffusion_lm_export_limits(str(_write_config(tmp_path / "q", AUTOREGRESSIVE_CONFIGS[1])))
+ is None
+ )
+
+
+def _export_backend(profile):
+ from core.export import export as export_mod
+
+ backend = export_mod.ExportBackend.__new__(export_mod.ExportBackend)
+ backend.decision = None
+ backend.current_model = object()
+ backend.current_tokenizer = object()
+ backend.is_peft = True
+ backend.diffusion_lm_profile = profile
+ return backend, export_mod
+
+
+def test_export_backend_refuses_before_any_conversion(monkeypatch):
+ backend, export_mod = _export_backend("diffusion_gemma")
+ monkeypatch.setattr(export_mod, "_export_runtime_available", lambda: True)
+ ok, message, path = backend.export_gguf("/nonexistent/out")
+ assert not ok and "GGUF" in message and path is None
+ ok, message, _ = backend.export_merged_model("/nonexistent/out")
+ assert not ok and "DiffusionGemma" in message
+ ok, message, _ = backend.export_lora_adapter("/nonexistent/out", gguf = True)
+ assert not ok and "GGUF" in message
+
+
+def test_export_load_detects_before_the_vision_branch():
+ from core.export import export as export_mod
+ src = inspect.getsource(export_mod.ExportBackend.load_checkpoint)
+ assert src.index("elif self.diffusion_lm_profile:") < src.index("elif self.is_vision:")
+
+
+def _trainer(profile = "llada", model = None):
+ from core.training.trainer import UnslothTrainer
+
+ t = UnslothTrainer.__new__(UnslothTrainer)
+ t.diffusion_lm_profile = profile
+ t.model = model
+ t.should_stop = False
+ t.is_audio = t.is_audio_vlm = False
+ t.is_vlm = False
+ t.warnings = []
+ t._record_warning = t.warnings.append
+ t._update_progress = lambda **_: None
+ return t
+
+
+class _TinyLLaDA:
+ def named_modules(self):
+ for name in (
+ "transformer.blocks.0.q_proj",
+ "transformer.blocks.0.ff_out",
+ "transformer.wte",
+ ):
+ yield name, None
+
+
+def captured_peft(monkeypatch):
+ import unsloth
+
+ calls = []
+
+ def fake_get_peft_model(model, **kwargs):
+ calls.append(kwargs)
+ return model
+
+ monkeypatch.setattr(unsloth.FastModel, "get_peft_model", staticmethod(fake_get_peft_model))
+ return calls
+
+
+_DEFAULT = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
+
+
+@_needs_torch
+ "requested, expected",
+ [
+ (None, None),
+ ([], None),
+ (_DEFAULT, None),
+ (list(reversed(_DEFAULT)), None),
+ (_DEFAULT + ["embed_tokens", "lm_head"], None),
+ (["q_proj", "v_proj"], ["q_proj", "v_proj"]),
+ (["all-linear"], "all-linear"),
+ ("all-linear", "all-linear"),
+ ],
+)
+def test_lora_targets_default_to_the_profile(captured_peft, requested, expected):
+ t = _trainer(model = _TinyLLaDA())
+ assert t._prepare_diffusion_lora(requested, None, 8, 16, 0.0, True, False, False, False)
+ assert captured_peft[-1]["target_modules"] == expected
+ assert "use_rslora" not in captured_peft[-1]
+
+
+@_needs_torch
+def test_cpt_embedding_names_resolve_to_the_models_own_modules(captured_peft):
+ import torch.nn as nn
+
+ class LLaDALike(nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.transformer = nn.Module()
+ self.transformer.wte = nn.Embedding(8, 4)
+ self.transformer.ff_out = nn.Linear(4, 8)
+ self.transformer.blocks = nn.ModuleList([nn.Module()])
+ self.transformer.blocks[0].ff_out = nn.Linear(4, 4)
+
+ def get_input_embeddings(self):
+ return self.transformer.wte
+
+ def get_output_embeddings(self):
+ return self.transformer.ff_out
+
+ t = _trainer(model = LLaDALike())
+ t._prepare_diffusion_lora(
+ None, ["embed_tokens", "lm_head"], 8, 16, 0.0, True, True, False, False
+ )
+ assert captured_peft[-1]["modules_to_save"] == ["transformer.wte", "transformer.ff_out"]
+ assert captured_peft[-1]["use_rslora"] is True
+ t = _trainer(model = _TinyLLaDA())
+ t._prepare_diffusion_lora(
+ None, ["embed_tokens", "lm_head"], 8, 16, 0.0, True, False, False, False
+ )
+ assert "modules_to_save" not in captured_peft[-1]
+
+
+@_needs_torch
[email protected]("dora, loftq", [(True, False), (False, True)])
+def test_unsupported_lora_options_fail_loudly(captured_peft, dora, loftq):
+ with pytest.raises(ValueError, match = "not supported for text-diffusion"):
+ _trainer(model = _TinyLLaDA())._prepare_diffusion_lora(
+ None, None, 8, 16, 0.0, True, False, loftq, dora
+ )
+ assert captured_peft == []
+
+
+@_needs_torch
+def test_prepare_routes_diffusion_before_any_other_branch(captured_peft):
+ t = _trainer(model = SimpleNamespace(named_modules = lambda: iter(()), config = object()))
+ t._use_gradient_checkpointing = "unsloth"
+ t._audio_type = "csm" # would otherwise take an audio branch
+ assert t.prepare_model_for_training(use_lora = True, target_modules = list(_DEFAULT))
+ assert captured_peft[-1]["target_modules"] is None
+
+
+@_needs_torch
+def test_detection_keeps_diffusion_gemma_off_the_vision_path(monkeypatch):
+ from core.training import trainer as trainer_mod
+
+ monkeypatch.setattr(
+ trainer_mod, "diffusion_lm_profile_for_model", lambda *a, **k: "diffusion_gemma"
+ )
+ t = _trainer(profile = None)
+ t.is_vlm = True
+ t._detect_diffusion_lm("google/diffusiongemma-26B-A4B-it", None, False, None)
+ assert t.diffusion_lm_profile == "diffusion_gemma" and t.is_vlm is False
+ assert t.warnings and "text only" in t.warnings[0]
+
+
+@_needs_torch
+def test_detection_skips_audio_models(monkeypatch):
+ from core.training import trainer as trainer_mod
+
+ monkeypatch.setattr(trainer_mod, "diffusion_lm_profile_for_model", lambda *a, **k: "llada")
+ t = _trainer(profile = None)
+ t.is_audio = True
+ t._detect_diffusion_lm("x", None, False, None)
+ assert t.diffusion_lm_profile is None
+
+
+@_needs_torch
+def test_autoregressive_detection_is_a_no_op(monkeypatch):
+ from core.training import trainer as trainer_mod
+
+ monkeypatch.setattr(trainer_mod, "diffusion_lm_profile_for_model", lambda *a, **k: None)
+ t = _trainer(profile = None)
+ t.is_vlm = True
+ t._detect_diffusion_lm("unsloth/gemma-3-4b-it", None, False, None)
+ assert t.diffusion_lm_profile is None and t.is_vlm is True and t.warnings == []
+
+
+def test_worker_turns_packing_off_once_and_only_for_diffusion():
+ from core.training.worker import _apply_diffusion_lm_overrides
+
+ config = {"packing": True, "learning_rate": 1e-4}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = "sdar"), config)
+ assert config == {"packing": False, "diffusion_lm_profile": "sdar", "learning_rate": 1e-4}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = "sdar"), config)
+ assert config == {"packing": False, "diffusion_lm_profile": "sdar", "learning_rate": 1e-4}
+ ar = {"packing": True}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = None), ar)
+ assert ar == {"packing": True}
+ _apply_diffusion_lm_overrides(SimpleNamespace(), ar)
+ assert ar == {"packing": True}
+
+
+def test_default_lora_learning_rate_is_lowered_only_for_dream_and_sdar():
+ from core.training.worker import _apply_diffusion_lm_overrides
+
+ for profile in ("dream", "sdar"):
+ for default in ("2e-4", 0.0002, None):
+ config = {} if default is None else {"learning_rate": default}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = profile), config)
+ assert config["learning_rate"] == 5e-5
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = profile), config)
+ assert config["learning_rate"] == 5e-5
+ for chosen in ("1e-4", 3e-4, 2e-5):
+ config = {"learning_rate": chosen}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = profile), config)
+ assert config["learning_rate"] == chosen
+ for profile in (
+ "llada",
+ "llada_moe",
+ "llada2",
+ "nemotron_labs_diffusion",
+ "diffusion_gemma",
+ None,
+ ):
+ config = {"learning_rate": "2e-4"}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = profile), config)
+ assert config["learning_rate"] == "2e-4"
+ config = {"learning_rate": "not a number"}
+ _apply_diffusion_lm_overrides(SimpleNamespace(diffusion_lm_profile = "dream"), config)
+ assert config["learning_rate"] == "not a number"
+
+
+@_needs_torch
+def test_unpicklable_remote_tokenizer_runs_the_map_in_process():
+ import ssl
+ from core.training import trainer as trainer_mod
+
+ def remote_tokenizer(**attrs):
+ cls = type(
+ "DreamTokenizer", (), {"__module__": "transformers_modules.dream.tokenization_dream"}
+ )
+ tok = cls()
+ tok.__dict__.update(attrs)
+ return tok
+
+ probe = trainer_mod._tokenizer_blocks_map_workers
+ unpicklable = remote_tokenizer(session = ssl.create_default_context())
+ assert probe(unpicklable)
+ assert probe(SimpleNamespace(tokenizer = unpicklable))
+ assert not probe(remote_tokenizer(vocab = {"a": 0}))
+ assert not probe(SimpleNamespace(session = ssl.create_default_context()))
+ src = inspect.getsource(trainer_mod.UnslothTrainer)
+ assert "or _tokenizer_blocks_map_workers(self.tokenizer)" in src
+
+
+@_needs_torch
+def test_diffusion_cpt_passes_the_embedding_learning_rate():
+ from core.training import trainer as trainer_mod
+
+ src = inspect.getsource(trainer_mod.UnslothTrainer)
+ branch = src[src.index('require_diffusion_api("DiffusionConfig")') : src.index("elif is_cpt:")]
+ assert 'diffusion_args["embedding_learning_rate"] = embedding_lr' in branch
+ assert 'training_args.get("embedding_learning_rate") if is_cpt else None' in branch
+
+
+def test_mlx_refuses_text_diffusion_before_its_trainer_and_chat_backend(monkeypatch):
+ from core.inference import worker as inference_worker
+ from core.training import worker
+ from utils.models import diffusion_lm
+
+ profiles = {"diffusion/llada": "llada", "org/llama": None}
+ monkeypatch.setattr(
+ diffusion_lm, "diffusion_lm_profile_for_model", lambda name, *a, **k: profiles[name]
+ )
+ assert "llada" in worker._mlx_diffusion_lm_error({}, "diffusion/llada")
+ assert worker._mlx_diffusion_lm_error({}, "org/llama") is None
+ src = inspect.getsource(worker.run_training_process)
+ assert src.index("_mlx_diffusion_lm_error(") < src.index("run_mlx_training_process(")
+ chat = inspect.getsource(inference_worker)
+ assert chat.index("mlx_refusal(model_name") < chat.index("backend = MLXInferenceBackend()")
+
+
+def test_diffusion_turns_offload_off_before_the_vram_budget_reads_it():
+ from core.training import worker
+
+ src = inspect.getsource(worker.run_training_process)
+ off = src.index('config["offload_layers"] = 0')
+ assert (
+ src.rindex("diffusion_lm_profile_for_model(", 0, off)
+ < off
+ < src.index("_wants_budget = bool(")
+ )
+
+
+def test_pre_detect_applies_the_overrides_before_the_row_bound():
+ from core.training import worker
+ src = inspect.getsource(worker._pre_detect_training_model)
+ assert "_apply_diffusion_lm_overrides(trainer, config)" in src
+
+
+@_needs_torch
+def test_text_path_builds_diffusion_trainer_before_cpt_and_sft():
+ from core.training import trainer as trainer_mod
+
+ src = inspect.getsource(trainer_mod.UnslothTrainer._train_worker)
+ diffusion = src.index("# Same SFT data path; the loss is the family's denoising objective")
+ assert (
+ diffusion
+ < src.index("elif is_cpt:")
+ < src.index(
+ '"args": SFTConfig(**config_args),\n }\n if eval_dataset'
+ )
+ )
+ assert 'require_diffusion_api("DiffusionTrainer")' in src
+ assert 'require_diffusion_api("DiffusionConfig")' in src
+ assert src.index("Text-diffusion model: sequence packing turned off") < src.index(
+ "self._configure_online_tokenization("
+ )
+
+
+@_needs_torch
+def test_load_uses_fastmodel_and_refuses_a_silent_autoregressive_load():
+ from core.training import trainer as trainer_mod
+
+ src = inspect.getsource(trainer_mod.UnslothTrainer.load_model)
+ branch = src[src.index("elif self.diffusion_lm_profile:") : src.index("elif self.is_vlm:\n")]
+ assert "FastModel.from_pretrained(" in branch
+ assert "loaded_diffusion_profile(self.model)" in branch and "raise RuntimeError" in branch
+ for kwarg in (
+ "trust_remote_code",
+ "revision",
+ "token",
+ "load_in_4bit",
+ "full_finetuning",
+ "device_map",
+ ):
+ assert f"{kwarg} = " in branch, kwarg
+
+
+def test_loaded_profile_comes_from_the_config_not_a_stamp():
+ from utils.models.diffusion_lm import loaded_diffusion_profile
+
+ # The diffusion loader marks _unsloth_slow_diffusion; it does not stamp a profile name.
+ cfg = SimpleNamespace(**DIFFUSION_CONFIGS["dream"])
+ assert (
+ loaded_diffusion_profile(SimpleNamespace(_unsloth_slow_diffusion = True, config = cfg))
+ == "dream"
+ )
+ llada_moe = SimpleNamespace(**DIFFUSION_CONFIGS["llada_moe"])
+ assert (
+ loaded_diffusion_profile(SimpleNamespace(_unsloth_slow_diffusion = True, config = llada_moe))
+ == "llada_moe"
+ )
+ # An autoregressive load is never reported as diffusion, whatever its config says.
+ assert loaded_diffusion_profile(SimpleNamespace(config = cfg)) is None
+ assert loaded_diffusion_profile(None) is None
+
+
+@_needs_torch
+def test_missing_core_api_names_the_update(monkeypatch):
+ import unsloth
+ monkeypatch.delattr(unsloth, "DefinitelyNotThere", raising = False)
+ with pytest.raises(RuntimeError, match = "unsloth studio update"):
+ diffusion_lm.require_diffusion_api("DefinitelyNotThere")
+
+
+class _Sampler:
+ calls: list = []
+
+ @staticmethod
+ def generate_text(
+ model,
+ tokenizer,
+ prompt = None,
+ messages = None,
+ stop_callback = None,
+ **kwargs,
+ ):
+ _Sampler.calls.append(
+ {"prompt": prompt, "messages": messages, "stop": stop_callback(), **kwargs}
+ )
+ return "The answer is 4.<|end|> trailing"
+
+
+def _inference_backend(monkeypatch, profile = "llada"):
+ from core.inference import inference as inference_mod
+
+ backend = inference_mod.InferenceBackend.__new__(inference_mod.InferenceBackend)
+ backend.models = {
+ "m": {"model": object(), "tokenizer": object(), "diffusion_lm_profile": profile}
+ }
+ backend.active_model_name = "m"
+ backend._generation_lock = threading.Lock()
+ backend.last_generation_stats = None
+ applied = []
+ backend._apply_adapter_state = applied.append
+ monkeypatch.setattr(diffusion_lm, "require_diffusion_api", lambda name: _Sampler)
+ _Sampler.calls = []
+ return backend, applied
+
+
+@_needs_torch
+def test_chat_uses_the_diffusion_sampler_as_one_completion(monkeypatch):
+ backend, applied = _inference_backend(monkeypatch)
+ backend.last_generation_stats = {"prompt_tokens": 999} # left over from an earlier generation
+ out = list(
+ backend._generate_chat_response_inner(
+ messages = [{"role": "user", "content": "2+2?"}],
+ system_prompt = "Be brief.",
+ max_new_tokens = 64,
+ temperature = 0.0,
+ _adapter_state = True,
+ stop = ["<|end|>"],
+ )
+ )
+ assert out == ["The answer is 4."]
+ call = _Sampler.calls[-1]
+ assert call["messages"][0] == {"role": "system", "content": "Be brief."}
+ assert call["max_new_tokens"] == 64 and call["stop"] is False and applied == [True]
+ assert backend.last_generation_stats is None
+
+
+@_needs_torch
+def test_raw_prompt_stream_uses_the_sampler_too(monkeypatch):
+ backend, _ = _inference_backend(monkeypatch)
+ assert list(backend.generate_stream("Q: 2+2?\nA:", max_new_tokens = 8)) == [
+ "The answer is 4.<|end|> trailing"
+ ]
+ assert _Sampler.calls[-1]["prompt"] == "Q: 2+2?\nA:"
+
+
+@_needs_torch
+def test_cancelled_chat_returns_nothing(monkeypatch):
+ backend, _ = _inference_backend(monkeypatch)
+ cancel = threading.Event()
+ cancel.set()
+ assert list(backend.generate_stream("x", cancel_event = cancel)) == []
+ assert _Sampler.calls[-1]["stop"] is True
+
+
+@_needs_torch
+def test_sampler_holds_the_generation_lock(monkeypatch):
+ backend, _ = _inference_backend(monkeypatch)
+ held = {}
+
+ def generate_text(model, tokenizer, **kwargs):
+ held["locked"] = backend._generation_lock.locked()
+ return "ok"
+
+ monkeypatch.setattr(
+ diffusion_lm,
+ "require_diffusion_api",
+ lambda name: SimpleNamespace(generate_text = generate_text),
+ )
+ assert list(backend.generate_stream("x")) == ["ok"]
+ assert held == {"locked": True} and not backend._generation_lock.locked()
+
+
+@_needs_torch
+def test_older_core_without_sampler_fails_clearly(monkeypatch):
+ backend, _ = _inference_backend(monkeypatch)
+ monkeypatch.setattr(diffusion_lm, "require_diffusion_api", lambda name: SimpleNamespace())
+ with pytest.raises(RuntimeError, match = "generate_text"):
+ list(backend.generate_stream("x"))
+
+
+@_needs_torch
+def test_sampler_cancellation_error_ends_the_chat_quietly(monkeypatch):
+ class Cancelled(RuntimeError):
+ pass
+
+ class _Cancelling(_Sampler):
+ @staticmethod
+ def generate_text(*args, **kwargs):
+ raise Cancelled("cancelled")
+
+ backend, _ = _inference_backend(monkeypatch)
+ monkeypatch.setattr(diffusion_lm, "require_diffusion_api", lambda name: _Cancelling)
+ samplers = types.ModuleType("unsloth.models.diffusion_samplers")
+ samplers.DiffusionGenerationCancelled = Cancelled
+ monkeypatch.setitem(sys.modules, "unsloth.models.diffusion_samplers", samplers)
+ assert list(backend.generate_stream("x", cancel_event = threading.Event())) == []
+ lock_free = backend._generation_lock.acquire(blocking = False)
+ assert lock_free
+ backend._generation_lock.release()
+
+
+@_needs_torch
+def test_text_path_load_still_catches_a_diffusion_model():
+ # A Hub LoRA has no config.json for the probe; core loads its diffusion base anyway.
+ from core.inference import inference as inference_mod
+ from core.training import trainer as trainer_mod
+
+ train_src = inspect.getsource(trainer_mod.UnslothTrainer.load_model)
+ text_branch = train_src[train_src.index('logger.info("Loaded text model")') :]
+ assert "loaded_diffusion_profile(self.model)" in text_branch.split("raise_if_offloaded")[0]
+ chat_src = inspect.getsource(inference_mod.InferenceBackend.load_model)
+ tail = chat_src[chat_src.rindex("FastLanguageModel.for_inference(model)") :]
+ assert "loaded_diffusion_profile(model)" in tail.split("raise_if_offloaded")[0]
+
+
+@_needs_torch
+def test_chat_classifies_a_hub_lora_by_its_base_before_the_vision_branch():
+ from core.inference import inference as inference_mod
+
+ src = inspect.getsource(inference_mod.InferenceBackend.load_model)
+ base_probe = src.index("diffusion_lm_profile_for_model(\n config.base_model")
+ diffusion_load = src.index("FastModel.from_pretrained(", base_probe)
+ assert diffusion_load < src.index("elif config.is_vision:", base_probe)
+
+
+def test_chat_diffusion_load_refuses_an_unmarked_model_and_keeps_the_token_boundary():
+ from core.inference import inference as inference_mod
+
+ src = inspect.getsource(inference_mod.InferenceBackend.load_model)
+ branch = src[src.index("diffusion_lm_profile = diffusion_lm_profile_for_model(") :]
+ branch = branch[: branch.index('self.models[model_name]["is_vision"] = False')]
+ # False (caller denied the ambient credential) must reach the probe as False, not None.
+ assert "hf_token = _hf_token_for_loader(hf_token)\n" in branch
+ assert "if not loaded_profile:" in branch and "raise RuntimeError(" in branch
+ assert "or diffusion_lm_profile" not in branch
+
+
+def test_export_limits_probe_uses_the_callers_credential(monkeypatch):
+ from routes import export as export_routes
+ from utils.models import diffusion_lm
+
+ seen = []
+ monkeypatch.setattr(
+ diffusion_lm,
+ "diffusion_lm_profile_for_model",
+ lambda name, hf_token = None, *a, **k: seen.append(hf_token) or "llada",
+ )
+ assert diffusion_lm.diffusion_lm_export_limits("/ckpt", False)["profile"] == "llada"
+ assert seen == [False]
+ src = inspect.getsource(export_routes.get_decision_export_info)
+ assert "hf_token_arg(None, allow_ambient_token = allow_ambient)" in src
+
+
+def test_mlx_refusal_resolves_a_hub_adapter_to_its_base(monkeypatch, tmp_path):
+ import huggingface_hub
+ from utils.models import diffusion_lm
+
+ profiles = {
+ "someone/llada-lora": None,
+ "GSAI-ML/LLaDA-8B-Instruct": "llada",
+ "org/llama-lora": None,
+ }
+ bases = {"someone/llada-lora": "GSAI-ML/LLaDA-8B-Instruct", "org/llama-lora": "org/llama"}
+ monkeypatch.setattr(
+ diffusion_lm, "diffusion_lm_profile_for_model", lambda name, *a, **k: profiles.get(name)
+ )
+
+ def hf_hub_download(repo_id, filename, **kwargs):
+ path = tmp_path / repo_id.replace("/", "__")
+ path.write_text(json.dumps({"base_model_name_or_path": bases[repo_id]}))
+ return str(path)
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", hf_hub_download)
+ assert "llada" in diffusion_lm.mlx_refusal("someone/llada-lora")
+ assert diffusion_lm.mlx_refusal("org/llama-lora") is None
+
+
+ "architecture",
+ [
+ "LLaDAModelLM",
+ "LLaDAMoEModel",
+ "LLaDAMoEModelLM",
+ "DreamModel",
+ "NemotronLabsDiffusionModel",
+ "LLaDA2MoeModelLM",
+ "SDARForCausalLM",
+ "DiffusionGemmaForBlockDiffusion",
+ ],
+)
+def test_text_diffusion_checkpoints_use_the_550_transformers_tier(tmp_path, architecture):
+ from utils.transformers_version import get_transformers_tier
+
+ folder = tmp_path / architecture
+ folder.mkdir()
+ (folder / "config.json").write_text(
+ json.dumps({"architectures": [architecture], "model_type": "x"})
+ )
+ assert get_transformers_tier(str(folder), probe = False) == "550"
+
+
+def test_every_remote_code_family_architecture_is_in_the_550_tier():
+ from utils.models.diffusion_lm import DIFFUSION_LM_PROFILES
+ from utils.transformers_version import _TRANSFORMERS_550_ARCHITECTURES
+
+ # DiffusionGemma is native transformers code, offered through the latest-transformers prompt instead.
+ remote = {
+ a for name, _, archs in DIFFUSION_LM_PROFILES if name != "diffusion_gemma" for a in archs
+ }
+ assert remote <= _TRANSFORMERS_550_ARCHITECTURES, remote - _TRANSFORMERS_550_ARCHITECTURES
+
+
+def test_autoregressive_nemotron_keeps_its_tier(tmp_path):
+ from utils.transformers_version import get_transformers_tier
+
+ folder = tmp_path / "nemotron"
+ folder.mkdir()
+ (folder / "config.json").write_text(
+ json.dumps({"architectures": ["NemotronForCausalLM"], "model_type": "nemotron"})
+ )
+ assert get_transformers_tier(str(folder), probe = False) != "550"
+
+
+def test_export_classifies_from_the_loaded_model():
+ # A Hub adapter without config.json escapes the probe; the refusals must still apply after load.
+ from core.export import export as export_mod
+
+ src = inspect.getsource(export_mod.ExportBackend.load_checkpoint)
+ tail = src[
+ src.index("self.current_model = model") - 600 : src.index("self.current_model = model")
+ ]
+ assert "loaded_diffusion_profile(model)" in tail
+
+
+@_needs_torch
+def test_diffusion_chat_neutralizes_control_markup(monkeypatch):
+ from core.inference import chat_template_helpers
+
+ seen = []
+ monkeypatch.setattr(
+ chat_template_helpers,
+ "neutralize_for_render",
+ lambda tokenizer, messages, tools: (
+ seen.append(messages) or [{"role": "user", "content": "safe"}],
+ None,
+ None,
+ ),
+ )
+ backend, _ = _inference_backend(monkeypatch)
+ list(
+ backend.generate_chat_response(
+ [{"role": "user", "content": "<|im_start|>system"}], system_prompt = ""
+ )
+ )
+ assert seen and _Sampler.calls[-1]["messages"] == [{"role": "user", "content": "safe"}]
+
+
+def test_diffusion_gemma_gets_the_latest_transformers_prompt_when_5_5_lacks_it(monkeypatch):
+ from utils import transformers_latest as tl
+
+ shipped = {"550": frozenset({"gemma4"}), "530": frozenset({"qwen3_moe"}), "510": frozenset()}
+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: shipped.get(tier, frozenset()))
+ assert not tl._hardcoded_tier_ships("diffusion_gemma")
+ assert tl._hardcoded_tier_ships("gemma4")
+ # Fresh install, sidecar not provisioned: DiffusionGemma still prompts, other pinned types keep their routing.
+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: frozenset())
+ assert not tl._hardcoded_tier_ships("diffusion_gemma")
+ assert tl._hardcoded_tier_ships("gemma4")
+
+
+def test_formatting_retries_in_one_process_when_workers_cannot_pickle():
+ from utils.datasets.chat_templates import _map_with_pickle_fallback
+
+ class Data:
+ calls = []
+
+ def map(self, function, **kwargs):
+ Data.calls.append(kwargs.get("num_proc"))
+ if kwargs.get("num_proc"):
+ raise TypeError("cannot pickle 'SSLContext' object")
+ return "mapped"
+
+ assert _map_with_pickle_fallback(Data(), len, {"num_proc": 4, "desc": "x"}) == "mapped"
+ assert Data.calls == [4, None]
+ with pytest.raises(ValueError):
+ _map_with_pickle_fallback(
+ type(
+ "D", (), {"map": lambda self, f, **k: (_ for _ in ()).throw(ValueError("bad row"))}
+ )(),
+ len,
+ {"num_proc": 4},
+ )
diff --git a/studio/backend/tests/test_export_absolute_paths.py b/studio/backend/tests/test_export_absolute_paths.py
index 34acbe04311..bde1c78c938 100644
--- a/studio/backend/tests/test_export_absolute_paths.py
+++ b/studio/backend/tests/test_export_absolute_paths.py
@@ -176,6 +176,7 @@ def _install_lightweight_backend_stubs(monkeypatch):
utils_models.is_embedding_model = lambda *args, **kwargs: False
utils_models.ModelConfig = object
monkeypatch.setitem(sys.modules, "utils.models", utils_models)
+ _load_module("utils.models.diffusion_lm", "utils/models/diffusion_lm.py", monkeypatch)
_load_module(
"utils.models.model_identity",
"utils/models/model_identity.py",
diff --git a/studio/backend/tests/test_hf_optional_file_probe.py b/studio/backend/tests/test_hf_optional_file_probe.py
index dabd741b582..f881e134a3e 100644
--- a/studio/backend/tests/test_hf_optional_file_probe.py
+++ b/studio/backend/tests/test_hf_optional_file_probe.py
@@ -46,7 +46,7 @@ _GUARDED = {
"core/inference/llama_cpp.py": {"_fetch_swa_entry_from_hf": "hf_file_definitely_absent"},
"picker/service.py": {"read_default_chat_template": "get_paths_info"},
"utils/models/model_config.py": {
- "_raw_config_has_vision_config": "hf_file_definitely_absent",
+ "_read_raw_config_json": "hf_file_definitely_absent",
"get_base_model_from_lora_identifier": "hf_file_definitely_absent",
},
"utils/security/consent.py": {"_load_remote_code_configs": "hf_file_definitely_absent"},
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index b8a6cc3b525..1b19c48f1c9 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -62,6 +62,19 @@ _CUSTOM_PROMPT_TEMPLATE_ERROR = (
)
+def _map_with_pickle_fallback(dataset, function, map_kwargs):
+ """dataset.map, retried in one process when the worker processes cannot receive the function: a
+ remote-code tokenizer class is pickled by value and can drag in unpicklable module state (an HTTP
+ client's SSLContext), which fails the whole multi-process map."""
+ try:
+ return dataset.map(function, **map_kwargs)
+ except Exception as exc:
+ if (map_kwargs.get("num_proc") or 1) <= 1 or "pickle" not in str(exc).lower():
+ raise
+ logger.warning(f"Formatting in one process: the worker processes could not be started ({exc})")
+ return dataset.map(function, **{k: v for k, v in map_kwargs.items() if k != "num_proc"})
+
+
def _custom_prompt_template_error(custom_prompt_template):
if custom_prompt_template is None:
return None
@@ -668,7 +681,7 @@ def apply_chat_template_to_dataset(
dataset_map_kwargs['num_proc'] = num_proc
dataset_map_kwargs['desc'] = "Applying template to Alpaca format"
- formatted_dataset = dataset.map(_format_alpaca, **dataset_map_kwargs)
+ formatted_dataset = _map_with_pickle_fallback(dataset, _format_alpaca, dataset_map_kwargs)
return {
"dataset": formatted_dataset,
@@ -794,7 +807,7 @@ def apply_chat_template_to_dataset(
threading.Thread(target = _poll_tqdm, daemon = True).start()
- formatted_dataset = dataset.map(_format_chatml, **dataset_map_kwargs)
+ formatted_dataset = _map_with_pickle_fallback(dataset, _format_chatml, dataset_map_kwargs)
if _tqdm_monitor_stop is not None:
_tqdm_monitor_stop.set()
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index 575e077d1b1..e54a0975ac2 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -5744,6 +5744,20 @@ def estimate_required_model_memory_gb(
vram_config.optimizer,
)
breakdown.gradients += compute_gradient_bytes(extra_params)
+ # Text-diffusion objectives that run [noisy ; clean] or a second self-conditioning pass hold
+ # about twice an autoregressive model's activations.
+ from utils.models.diffusion_lm import ACTIVATION_FACTOR, diffusion_lm_profile
+
+ diffusion_profile = diffusion_lm_profile(
+ {
+ "model_type": getattr(config, "model_type", None),
+ "architectures": getattr(config, "architectures", None),
+ }
+ )
+ activation_factor = ACTIVATION_FACTOR.get(diffusion_profile, 1.0)
+ if activation_factor != 1.0:
+ breakdown.activations = int(breakdown.activations * activation_factor)
+ metadata["diffusion_lm_profile"] = diffusion_profile
required_gb = breakdown.total / (1024**3)
metadata["required_gb"] = round(required_gb, 3)
metadata["estimation_mode"] = "detailed"
diff --git a/studio/backend/utils/models/diffusion_lm.py b/studio/backend/utils/models/diffusion_lm.py
new file mode 100644
index 00000000000..c0d786b93e6
--- /dev/null
+++ b/studio/backend/utils/models/diffusion_lm.py
@@ -0,0 +1,226 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Text-diffusion language models (LLaDA, Dream, SDAR, DiffusionGemma, ...), detected from config.json alone.
+
+Torch-free mirror of the profile matching in ``unsloth/models/diffusion_profiles.py``; same names, same
+order (a profile's ``model_types`` and ``architectures`` are both tried before the next profile, and
+LLaDA-MoE precedes LLaDA because both report ``model_type = "llada"``). ``test_diffusion_lm_studio.py``
+keeps the two in sync. Not to be confused with Studio's image / video diffusion pipelines.
+"""
+
+from pathlib import Path
+from typing import Mapping, Optional
+
+from loggers import get_logger
+
+logger = get_logger(__name__)
+
+# (profile name, model_types, architectures), in core registry order.
+DIFFUSION_LM_PROFILES: tuple = (
+ ("llada2", ("llada2_moe",), ("LLaDA2MoeModelLM",)),
+ ("sdar", ("sdar",), ("SDARForCausalLM",)),
+ (
+ "diffusion_gemma",
+ ("diffusion_gemma", "diffusion_gemma4"),
+ (
+ "DiffusionGemmaForBlockDiffusion",
+ "DiffusionGemma4ModelForBlockDiffusion",
+ "DiffusionGemma4ForBlockDiffusion",
+ ),
+ ),
+ ("llada_moe", (), ("LLaDAMoEModel", "LLaDAMoEModelLM")),
+ ("llada", ("llada",), ("LLaDAModelLM",)),
+ ("dream", ("Dream", "dream"), ("DreamModel",)),
+ ("nemotron_labs_diffusion", ("nemotron_labs_diffusion",), ("NemotronLabsDiffusionModel",)),
+)
+
+# Tied encoder / decoder base weights with separate adapters: merging would add both deltas to both.
+NO_MERGE_PROFILES = frozenset({"diffusion_gemma"})
+
+# Rough training-memory factor over an autoregressive model of the same size: LLaDA2 / SDAR attend over
+# [noisy ; clean] (twice the length), DiffusionGemma runs a self-conditioning pass plus the encoder.
+ACTIVATION_FACTOR = {"llada2": 2.0, "sdar": 2.0, "diffusion_gemma": 2.0}
+
+
+def diffusion_lm_profile(config: Optional[Mapping]) -> Optional[str]:
+ """Profile name for a raw config.json dict, or None for any other model."""
+ if not isinstance(config, Mapping):
+ return None
+ model_type = config.get("model_type")
+ architectures = config.get("architectures") or ()
+ if isinstance(architectures, str):
+ architectures = (architectures,)
+ for name, model_types, archs in DIFFUSION_LM_PROFILES:
+ if model_type in model_types or any(a in archs for a in architectures):
+ return name
+ return None
+
+
+def _local_adapter_base(model_name: str) -> Optional[str]:
+ try:
+ import json
+
+ path = Path(model_name).expanduser() / "adapter_config.json"
+ if not path.is_file():
+ return None
+ return json.loads(path.read_text(encoding = "utf-8-sig")).get("base_model_name_or_path")
+ except Exception:
+ return None
+
+
+def _hub_adapter_base(
+ model_name: str,
+ hf_token = None,
+ revision: Optional[str] = None,
+) -> Optional[str]:
+ try:
+ from utils.paths import is_local_path
+
+ if is_local_path(model_name):
+ return None
+ import json
+ from huggingface_hub import hf_hub_download
+
+ path = hf_hub_download(model_name, "adapter_config.json", token = hf_token, revision = revision)
+ return json.loads(Path(path).read_text(encoding = "utf-8-sig")).get("base_model_name_or_path")
+ except Exception:
+ return None
+
+
+def diffusion_lm_profile_for_model(
+ model_name: str,
+ hf_token: Optional[str] = None,
+ local_files_only: bool = False,
+ revision: Optional[str] = None,
+ _depth: int = 0,
+) -> Optional[str]:
+ """Profile name for a model id / local path (a local LoRA folder resolves to its base). Never raises:
+ an unreadable config means "not detected here", and the loaded model is checked again after load."""
+ try:
+ from utils.models.model_config import _read_raw_config_json
+ from utils.paths import is_local_path, normalize_path
+
+ if is_local_path(model_name):
+ local = Path(normalize_path(model_name)).expanduser()
+ if not (local / "config.json").is_file():
+ base = _local_adapter_base(str(local))
+ if not base or _depth >= 2 or base == model_name:
+ return None
+ return diffusion_lm_profile_for_model(
+ base, hf_token, local_files_only, None, _depth = _depth + 1
+ )
+ return diffusion_lm_profile(
+ _read_raw_config_json(model_name, hf_token, local_files_only, revision)
+ )
+ except Exception as exc:
+ logger.debug("Diffusion LM detection skipped for '%s': %s", model_name, exc)
+ return None
+
+
+def mlx_refusal(
+ model_name: str,
+ hf_token = None,
+ revision: Optional[str] = None,
+) -> Optional[str]:
+ """Why the MLX backend cannot take this model, or None: it has no denoising objective or sampler, so
+ it would train / decode a text-diffusion LM as an autoregressive one."""
+ profile = diffusion_lm_profile_for_model(model_name, hf_token, revision = revision)
+ if not profile:
+ # A Hub LoRA has no config.json; off MLX the post-load check catches it, here nothing loads first.
+ base = _hub_adapter_base(model_name, hf_token, revision)
+ profile = base and diffusion_lm_profile_for_model(base, hf_token)
+ if not profile:
+ return None
+ return (
+ f"Text-diffusion models ({profile}) are not supported by the MLX backend on Apple Silicon yet: "
+ "it has no denoising objective or sampler. Use an NVIDIA or AMD GPU for this model."
+ )
+
+
+def loaded_diffusion_profile(model) -> Optional[str]:
+ """Profile name of a model loaded through Unsloth's diffusion loader (``_unsloth_slow_diffusion``), else
+ None. Resolved from the model's config with the core registry, then this mirror; PEFT wrappers forward
+ both attributes."""
+ if model is None or not getattr(model, "_unsloth_slow_diffusion", False):
+ return None
+ config = getattr(model, "config", None)
+ try:
+ from unsloth.models.diffusion_profiles import resolve_diffusion_profile
+ profile = resolve_diffusion_profile(config)
+ if profile is not None:
+ return profile.name
+ except Exception as exc:
+ logger.debug("Core diffusion profile lookup failed: %s", exc)
+ return diffusion_lm_profile(_config_dict(config)) or "unknown"
+
+
+def _config_dict(config) -> Optional[dict]:
+ if config is None:
+ return None
+ return {
+ "model_type": getattr(config, "model_type", None),
+ "architectures": getattr(config, "architectures", None),
+ }
+
+
+GGUF_REFUSAL = (
+ "GGUF export is not available for text-diffusion models ({profile}): llama.cpp would serve them as "
+ "ordinary left-to-right models. Export the LoRA adapter instead."
+)
+MERGE_REFUSAL = (
+ "Merged export is not available for DiffusionGemma: its encoder and decoder share base weights but "
+ "train separate adapters, so merging would add both deltas to both. Export the LoRA adapter instead."
+)
+# The diffusion loader keeps the plain transformers model, which has no Unsloth merged-save path.
+MERGE_REFUSAL_GENERIC = (
+ "Merged export is not available for text-diffusion models ({profile}) yet. Export the LoRA adapter "
+ "instead; it loads on top of the base model with Unsloth or PEFT."
+)
+
+
+def export_refusal(profile: Optional[str], method: str) -> Optional[str]:
+ """Why ``method`` ("gguf", "merged", "lora", "base") cannot export a ``profile`` model; None if it can."""
+ if not profile:
+ return None
+ if method == "gguf":
+ return GGUF_REFUSAL.format(profile = profile)
+ if method == "merged":
+ if profile in NO_MERGE_PROFILES:
+ return MERGE_REFUSAL
+ return MERGE_REFUSAL_GENERIC.format(profile = profile)
+ return None
+
+
+def diffusion_lm_export_limits(checkpoint_path: str, hf_token = None) -> Optional[dict]:
+ """``{profile, disabled_methods, reason}`` for a text-diffusion checkpoint folder, else None. Cache-only:
+ the base model was read when it trained, and a picker probe must not reach the network. ``hf_token``
+ is the caller's (False = denied the ambient credential), so the cache guard sees the same boundary."""
+ profile = diffusion_lm_profile_for_model(checkpoint_path, hf_token, local_files_only = True)
+ if not profile:
+ return None
+ disabled = [m for m in ("merged", "gguf") if export_refusal(profile, m)]
+ return {
+ "profile": profile,
+ "disabled_methods": disabled,
+ "reason": " ".join(export_refusal(profile, m) for m in disabled),
+ }
+
+
+def require_diffusion_api(name: str):
+ """A core-Unsloth diffusion helper, or a clear error naming the Unsloth version needed."""
+ import unsloth
+
+ obj = getattr(unsloth, name, None)
+ if obj is None:
+ try:
+ from unsloth.models import diffusion as _diffusion
+ obj = getattr(_diffusion, name, None)
+ except Exception:
+ obj = None
+ if obj is None:
+ raise RuntimeError(
+ f"Unsloth: this Studio build trains text-diffusion models but the installed Unsloth has no "
+ f"`{name}`. Update with `unsloth studio update` (or `pip install -U unsloth unsloth_zoo`)."
+ )
+ return obj
diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.py
index 82d51710f4d..6c5c7d5358d 100644
--- a/studio/backend/utils/models/model_config.py
+++ b/studio/backend/utils/models/model_config.py
@@ -822,65 +822,78 @@ def _current_cached_snapshot(
return None
-def _raw_config_has_vision_config(
+def _read_raw_config_json(
model_name: str,
hf_token: Optional[str] = None,
local_files_only: bool = False,
revision: Optional[str] = None,
-) -> Optional[bool]:
- try:
- current = (
- None
- if revision is not None
- else _current_cached_snapshot(model_name, hf_token, local_files_only)
- )
- if is_local_path(model_name):
- config_path = Path(normalize_path(model_name)).expanduser() / "config.json"
- elif current is not None and (current[0] / "config.json").is_file():
- # The current commit's own copy: skips the absence probe and the freshness check.
- config_path = current[0] / "config.json"
- elif current is not None and "config.json" not in current[1]:
- # The document just said the repo publishes none; the probe below re-asks.
+) -> Optional[dict]:
+ """config.json as a dict, read without executing any repo code; None when the repo publishes none.
+ Raises on read errors: callers decide what an unreadable config means."""
+ current = (
+ None
+ if revision is not None
+ else _current_cached_snapshot(model_name, hf_token, local_files_only)
+ )
+ if is_local_path(model_name):
+ config_path = Path(normalize_path(model_name)).expanduser() / "config.json"
+ elif current is not None and (current[0] / "config.json").is_file():
+ # The current commit's own copy: skips the absence probe and the freshness check.
+ config_path = current[0] / "config.json"
+ elif current is not None and "config.json" not in current[1]:
+ # The document just said the repo publishes none; the probe below re-asks.
+ logger.debug("'%s' has no config.json on the Hub", model_name)
+ return None
+ else:
+ from huggingface_hub import hf_hub_download
+ from utils.hf_probe import hf_file_definitely_absent
+
+ # Avoid caching the expected 404 for GGUF repos without config.json.
+ if not local_files_only and hf_file_definitely_absent(
+ model_name,
+ "config.json",
+ revision = revision,
+ token = hf_token,
+ ):
logger.debug("'%s' has no config.json on the Hub", model_name)
return None
- else:
- from huggingface_hub import hf_hub_download
- from utils.hf_probe import hf_file_definitely_absent
-
- # Avoid caching the expected 404 for GGUF repos without config.json.
- if not local_files_only and hf_file_definitely_absent(
- model_name,
- "config.json",
- revision = revision,
- token = hf_token,
- ):
- logger.debug("'%s' has no config.json on the Hub", model_name)
- return None
- download_kwargs = {
- "repo_id": model_name,
- "filename": "config.json",
- "token": hf_token,
- "local_files_only": local_files_only,
- "cache_dir": active_hf_hub_cache(),
- }
- if revision is not None:
- download_kwargs["revision"] = revision
- # Measured: hf_hub_download falls back to the cache even with
- # local_files_only=False, never consulting the credential, so a planted entry
- # plus a dead endpoint returns a private config.json to a token that cannot read it.
- if cached_read_refused(
+ download_kwargs = {
+ "repo_id": model_name,
+ "filename": "config.json",
+ "token": hf_token,
+ "local_files_only": local_files_only,
+ "cache_dir": active_hf_hub_cache(),
+ }
+ if revision is not None:
+ download_kwargs["revision"] = revision
+ # Measured: hf_hub_download falls back to the cache even with
+ # local_files_only=False, never consulting the credential, so a planted entry
+ # plus a dead endpoint returns a private config.json to a token that cannot read it.
+ if cached_read_refused(
+ hf_token,
+ repo_id = model_name,
+ is_cached = lambda: _config_json_already_cached(model_name, revision),
+ ):
+ return None
+ config_path = Path(
+ call_with_anonymous_retry(
+ lambda token: hf_hub_download(**{**download_kwargs, "token": token}),
hf_token,
- repo_id = model_name,
- is_cached = lambda: _config_json_already_cached(model_name, revision),
- ):
- return None
- config_path = Path(
- call_with_anonymous_retry(
- lambda token: hf_hub_download(**{**download_kwargs, "token": token}),
- hf_token,
- )
)
- config = json.loads(config_path.read_text(encoding = "utf-8-sig"))
+ )
+ return json.loads(config_path.read_text(encoding = "utf-8-sig"))
+
+
+def _raw_config_has_vision_config(
+ model_name: str,
+ hf_token: Optional[str] = None,
+ local_files_only: bool = False,
+ revision: Optional[str] = None,
+) -> Optional[bool]:
+ try:
+ config = _read_raw_config_json(model_name, hf_token, local_files_only, revision)
+ if config is None:
+ return None
architectures = config.get("architectures") or []
model_type = config.get("model_type")
explicit_vision = (
diff --git a/studio/backend/utils/transformers_latest.py b/studio/backend/utils/transformers_latest.py
index 7569621bced..1bbaebe695f 100644
--- a/studio/backend/utils/transformers_latest.py
+++ b/studio/backend/utils/transformers_latest.py
@@ -448,6 +448,27 @@ def _hardcoded_model_types() -> frozenset[str]:
)
+# Pinned to 5.5 for its mlx-vlm processor, which has no model for the type: prompt even before the sidecar exists.
+_TIER_ROUTED_WITHOUT_MODEL = frozenset({"diffusion_gemma"})
+
+
+def _hardcoded_tier_ships(model_type: str) -> bool:
+ """A hardcoded tier counts as handling *model_type* unless that sidecar is installed and its
+ CONFIG_MAPPING lacks it: DiffusionGemma is routed to 5.5 for the mlx-vlm processor, but 5.5 has
+ no DiffusionGemma model, so a CUDA load needs the latest-transformers prompt."""
+ if model_type not in _hardcoded_model_types() or model_type in _TIER_ROUTED_WITHOUT_MODEL:
+ return False
+ for tier, types in (
+ ("530", _TRANSFORMERS_530_MODEL_TYPES),
+ ("550", _TRANSFORMERS_550_MODEL_TYPES),
+ ("510", _TRANSFORMERS_510_MODEL_TYPES),
+ ):
+ if model_type in types:
+ installed = _config_model_types(tier)
+ return not installed or model_type in installed
+ return True
+
+
def check_upgrade_for_model(model_name: str, hf_token: str | None = None) -> dict | None:
"""Upgrade signal for *model_name*, or None when current routing already handles it.
@@ -477,11 +498,10 @@ def check_upgrade_for_model(model_name: str, hf_token: str | None = None) -> dic
# Without a readable base mapping every type looks brand new; bail out.
if not _config_model_types("default"):
return None
- hardcoded = _hardcoded_model_types()
missing = [
candidate
for candidate in candidates
- if candidate not in hardcoded
+ if not _hardcoded_tier_ships(candidate)
and not any(candidate in _config_model_types(tier) for tier in _TIER_RANK)
]
if not missing:
diff --git a/studio/backend/utils/transformers_version.py b/studio/backend/utils/transformers_version.py
index 239482946a8..65ad56dabe6 100644
--- a/studio/backend/utils/transformers_version.py
+++ b/studio/backend/utils/transformers_version.py
@@ -359,6 +359,16 @@ _TRANSFORMERS_457_ARCHITECTURES: frozenset[str] = frozenset(
# Architecture classes / model_type values requiring transformers 5.5.0 (via config.json).
_TRANSFORMERS_550_ARCHITECTURES: set[str] = {
"DiffusionGemmaForBlockDiffusion",
+ # Remote-code text-diffusion LMs: their tokenizers are 5.x-only, and 5.3.0 lacks helpers their code imports
+ # (Nemotron-Labs-Diffusion needs transformers.utils.generic.check_model_inputs). Matched by architecture,
+ # since Nemotron-Labs-Diffusion shares model_type "nemotron" with the autoregressive Nemotron models.
+ "LLaDAModelLM",
+ "LLaDAMoEModel",
+ "LLaDAMoEModelLM",
+ "DreamModel",
+ "NemotronLabsDiffusionModel",
+ "LLaDA2MoeModelLM",
+ "SDARForCausalLM",
"Gemma4ForConditionalGeneration",
"KimiK3ForConditionalGeneration",
"LocateAnythingForConditionalGeneration",
diff --git a/studio/frontend/src/features/export/api/export-api.ts b/studio/frontend/src/features/export/api/export-api.ts
index a201fdf9513..c11e190885b 100644
--- a/studio/frontend/src/features/export/api/export-api.ts
+++ b/studio/frontend/src/features/export/api/export-api.ts
@@ -106,19 +106,43 @@ export interface DecisionExportInfo {
existing_export: ({ quantizations?: string[] } & Record<string, unknown>) | null;
}
-/** Decision export info for a local checkpoint folder; null for any other model. */
-export async function fetchDecisionExportInfo(
+/** Export limits of a text-diffusion LM checkpoint (LLaDA, Dream, DiffusionGemma, ...). */
+export interface DiffusionLmExportInfo {
+ profile: string;
+ disabled_methods: ("merged" | "lora" | "gguf")[];
+ reason: string | null;
+}
+
+export interface CheckpointExportProbe {
+ decision: DecisionExportInfo | null;
+ diffusionLm: DiffusionLmExportInfo | null;
+}
+
+/** Decision and text-diffusion export info for a local checkpoint folder; nulls for any other model. */
+export async function fetchCheckpointExportProbe(
checkpointPath: string,
signal?: AbortSignal,
-): Promise<DecisionExportInfo | null> {
+): Promise<CheckpointExportProbe> {
const response = await authFetch(
`/api/export/decision-info?checkpoint_path=${encodeURIComponent(checkpointPath)}`,
{ signal },
);
- const body = await parseJson<{ decision: DecisionExportInfo | null }>(
- response,
- );
- return body.decision ?? null;
+ const body = await parseJson<{
+ decision: DecisionExportInfo | null;
+ diffusion_lm?: DiffusionLmExportInfo | null;
+ }>(response);
+ return {
+ decision: body.decision ?? null,
+ diffusionLm: body.diffusion_lm ?? null,
+ };
+}
+
+/** Decision export info for a local checkpoint folder; null for any other model. */
+export async function fetchDecisionExportInfo(
+ checkpointPath: string,
+ signal?: AbortSignal,
+): Promise<DecisionExportInfo | null> {
+ return (await fetchCheckpointExportProbe(checkpointPath, signal)).decision;
}
export async function fetchCheckpoints(): Promise<CheckpointListResponse> {
diff --git a/studio/frontend/src/features/export/export-page.tsx b/studio/frontend/src/features/export/export-page.tsx
index ae75b1c8ac8..3e610b2ebb5 100644
--- a/studio/frontend/src/features/export/export-page.tsx
+++ b/studio/frontend/src/features/export/export-page.tsx
@@ -66,8 +66,9 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useShallow } from "zustand/react/shallow";
import {
type DecisionExportInfo,
+ type DiffusionLmExportInfo,
type ModelCheckpoints,
- fetchDecisionExportInfo,
+ fetchCheckpointExportProbe,
} from "./api/export-api";
import { adapterCompatibilityTip, type AdapterFormat } from "./constants";
import { ExportRunPanel } from "./components/export-run-panel";
@@ -524,15 +525,16 @@ export function ExportPage() {
const [decisionState, setDecisionState] = useState<{
path: string;
info: DecisionExportInfo | null;
+ diffusionLm?: DiffusionLmExportInfo | null;
} | null>(null);
useEffect(() => {
if (!decisionProbePath) {
return;
}
const controller = new AbortController();
- fetchDecisionExportInfo(decisionProbePath, controller.signal)
- .then((info) => {
- setDecisionState({ path: decisionProbePath, info });
+ fetchCheckpointExportProbe(decisionProbePath, controller.signal)
+ .then(({ decision: info, diffusionLm }) => {
+ setDecisionState({ path: decisionProbePath, info, diffusionLm });
if (!info) {
return;
}
@@ -561,6 +563,15 @@ export function ExportPage() {
? decisionState.info
: null;
const isDecision = decisionInfo != null;
+ // Text-diffusion LMs: no GGUF (llama.cpp would serve them left to right); DiffusionGemma also no merge.
+ const diffusionLmInfo =
+ decisionProbePath && decisionState?.path === decisionProbePath
+ ? (decisionState.diffusionLm ?? null)
+ : null;
+ const diffusionDisabledMethods = useMemo<ExportMethod[]>(
+ () => diffusionLmInfo?.disabled_methods ?? [],
+ [diffusionLmInfo],
+ );
const decisionIneligible = decisionInfo?.eligible === false;
const decisionQuantOptions = useMemo(
() =>
@@ -633,6 +644,15 @@ export function ExportPage() {
if (!effectiveIsAdapter && effectiveIsQuantized && exportMethod !== null) {
setExportMethod(null);
}
+ if (
+ exportMethod !== null &&
+ diffusionDisabledMethods.includes(exportMethod)
+ ) {
+ setExportMethod(null);
+ }
+ if (diffusionDisabledMethods.includes("gguf") && loraAsGguf) {
+ setLoraAsGguf(false);
+ }
// The GGUF LoRA target only applies to an adapter checkpoint on a non-Mac host.
if ((!effectiveIsAdapter || isMacHost) && ggufTarget !== "model") {
setGgufTarget("model");
@@ -643,6 +663,8 @@ export function ExportPage() {
exportMethod,
isMacHost,
ggufTarget,
+ diffusionDisabledMethods,
+ loraAsGguf,
]);
const handleSourceTabChange = useCallback((next: string) => {
@@ -760,6 +782,7 @@ export function ExportPage() {
selectedExportSource &&
exportMethod &&
!exportUnsupported &&
+ !diffusionDisabledMethods.includes(exportMethod) &&
!hubMultiFormat &&
(exportMethod !== "gguf" || ggufAsLora || quantLevels.length > 0) &&
(exportMethod !== "merged" || selectedFormats.length > 0) &&
@@ -837,6 +860,7 @@ export function ExportPage() {
if (!source || !exportMethod) return;
// No supported accelerator (or PyTorch/MLX missing): the backend would reject anyway; don't submit.
if (exportUnsupported) return;
+ if (diffusionDisabledMethods.includes(exportMethod)) return;
// GGUF with no quant, or merged with no format, would run an empty export; require at least one.
if (exportMethod === "gguf" && !ggufAsLora && quantLevels.length === 0)
return;
@@ -981,6 +1005,7 @@ export function ExportPage() {
runExport,
isDecision,
decisionIneligible,
+ diffusionDisabledMethods,
t,
]);
@@ -1539,9 +1564,10 @@ export function ExportPage() {
? ["merged", "lora"]
: !effectiveIsAdapter && effectiveIsQuantized
? ["merged", "lora", "gguf"]
- : effectiveIsAdapter
- ? []
- : ["lora"]
+ : [
+ ...(effectiveIsAdapter ? [] : (["lora"] as const)),
+ ...diffusionDisabledMethods,
+ ]
}
disabledReason={
exportUnsupported
@@ -1550,9 +1576,16 @@ export function ExportPage() {
? t("exportDecision.ggufOnly")
: !effectiveIsAdapter && effectiveIsQuantized
? "Pre-quantized (BNB 4-bit) models cannot be exported without LoRA adapters"
- : effectiveIsAdapter
- ? undefined
- : "LoRA-only export needs a LoRA adapter checkpoint"
+ : [
+ effectiveIsAdapter
+ ? null
+ : "LoRA-only export needs a LoRA adapter checkpoint",
+ diffusionDisabledMethods.length > 0
+ ? diffusionLmInfo?.reason
+ : null,
+ ]
+ .filter(Boolean)
+ .join(" ") || undefined
}
/>
@@ -1711,7 +1744,12 @@ export function ExportPage() {
variant={loraAsGguf ? "default" : "outline"}
size="sm"
onClick={() => setLoraAsGguf(true)}
- title="llama.cpp GGUF LoRA, loadable with `llama-cli --lora`."
+ disabled={diffusionDisabledMethods.includes("gguf")}
+ title={
+ diffusionDisabledMethods.includes("gguf")
+ ? (diffusionLmInfo?.reason ?? undefined)
+ : "llama.cpp GGUF LoRA, loadable with `llama-cli --lora`."
+ }
>
GGUF adapter
</Button>
diff --git a/studio/frontend/src/features/hub/lib/model-capabilities.ts b/studio/frontend/src/features/hub/lib/model-capabilities.ts
index ed5fbba8064..7f9f6a19d53 100644
--- a/studio/frontend/src/features/hub/lib/model-capabilities.ts
+++ b/studio/frontend/src/features/hub/lib/model-capabilities.ts
@@ -77,6 +77,20 @@ const DIFFUSION_TAGS = new Set([
"unconditional-image-generation",
]);
+// Text-diffusion language models (LLaDA, Dream, SDAR, DiffusionGemma, ...) are text generators that
+// can carry a bare "diffusion" tag; only an image / video tag or family name makes them image gen.
+const TEXT_DIFFUSION_LM_TAGS = new Set([
+ "llada",
+ "llada2_moe",
+ "dream",
+ "sdar",
+ "nemotron_labs_diffusion",
+ "diffusion_gemma",
+ "diffusion_gemma4",
+]);
+const TEXT_DIFFUSION_LM_ID_RE =
+ /(?:^|[-_/])(?:llada|dream[-_]v\d|diffucoder|sdar[-_]\d|diffusiongemma|nemotron[-_]labs[-_]diffusion)/;
+
const CODE_TAGS = new Set([
"code",
"code-generation",
@@ -212,12 +226,19 @@ export function detectCapabilities(
) {
out.push({ key: "embedding", label: "Embeddings" });
}
- if (
- hasAny(DIFFUSION_TAGS) ||
+ const imageGenName =
/stable[-_]?diffusion|\bsdxl\b|\bflux\b|qwen[-_]?image|hunyuan[-_]?(?:video|image)|wan2|latent[-_]?consistency|[-_]lcm\b|dreamshaper/.test(
lowerId,
- )
- ) {
+ );
+ const textDiffusionLm =
+ !imageGenName &&
+ (hasAny(TEXT_DIFFUSION_LM_TAGS) || TEXT_DIFFUSION_LM_ID_RE.test(lowerId));
+ const imageGenTagged = textDiffusionLm
+ ? [...DIFFUSION_TAGS].some(
+ (tag) => tag !== "diffusion" && (tagSet.has(tag) || pipeline === tag),
+ )
+ : hasAny(DIFFUSION_TAGS);
+ if (imageGenTagged || imageGenName) {
out.push({ key: "diffusion", label: "Image/video gen" });
}
const languageCodes = new Set<string>();
diff --git a/studio/frontend/tests/text-diffusion-lm-capabilities.test.ts b/studio/frontend/tests/text-diffusion-lm-capabilities.test.ts
new file mode 100644
index 00000000000..4a05d80cee4
--- /dev/null
+++ b/studio/frontend/tests/text-diffusion-lm-capabilities.test.ts
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import { registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+
+const { detectCapabilities } = await import(
+ "../src/features/hub/lib/model-capabilities.ts"
+);
+
+const keys = (tags: string[], pipeline: string | undefined, id: string) =>
+ detectCapabilities(tags, pipeline, id).map((c) => c.key);
+
+test("text-diffusion LMs with a bare diffusion tag are not image/video generators", () => {
+ for (const [id, tags] of [
+ ["GSAI-ML/LLaDA-8B-Instruct", ["llada", "diffusion"]],
+ ["Dream-org/Dream-v0-Instruct-7B", ["diffusion"]],
+ ["JetLM/SDAR-1.7B-Chat", ["sdar", "diffusion"]],
+ ["google/diffusiongemma-26B-A4B-it", ["diffusion"]],
+ ["nvidia/Nemotron-Labs-Diffusion-3B", ["diffusion"]],
+ ] as const) {
+ assert.ok(!keys([...tags], "text-generation", id).includes("diffusion"), id);
+ }
+});
+
+test("image and video diffusion models keep the image/video badge", () => {
+ assert.ok(keys(["diffusion"], undefined, "someone/my-model").includes("diffusion"));
+ assert.ok(keys(["diffusers"], "text-to-image", "black-forest-labs/FLUX.1-dev").includes("diffusion"));
+ assert.ok(keys([], undefined, "Lykon/dreamshaper-8").includes("diffusion"));
+ // A text-diffusion family name does not hide a real image-generation tag.
+ assert.ok(keys(["diffusers", "llada"], "text-to-image", "x/llada-image").includes("diffusion"));
+});
diff --git a/tests/python/test_loader_resolution_reporting.py b/tests/python/test_loader_resolution_reporting.py
index 21fac8709b0..dd6028b0023 100644
--- a/tests/python/test_loader_resolution_reporting.py
+++ b/tests/python/test_loader_resolution_reporting.py
@@ -139,7 +139,7 @@ def test_training_forwards_observer_through_each_loader_and_retry():
"self.load_model",
)
]
- assert len(calls) == 9
+ assert len(calls) == 10
for call in calls:
assert any(
k.arg == "on_model_resolved" and ast.unparse(k.value) == "on_model_resolved"
diff --git a/tests/studio/test_spark_tts_bicodec_handoff.py b/tests/studio/test_spark_tts_bicodec_handoff.py
index fdf8a359c54..6f6bb3ee7fd 100644
--- a/tests/studio/test_spark_tts_bicodec_handoff.py
+++ b/tests/studio/test_spark_tts_bicodec_handoff.py
@@ -81,6 +81,9 @@ def _run(
_spark_tts_repo_dir = repo_dir,
_spark_tts_repo_source = None,
_update_progress = lambda **kwargs: None,
+ # pre_detect also classifies text-diffusion LMs; a Spark-TTS repo is never one.
+ _detect_diffusion_lm = lambda *args: None,
+ diffusion_lm_profile = None,
_resolve_audio_columns = lambda *args: {
"audio_col": "audio",
"text_col": "text",