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: LiquidAI d1 and Decider decision models, train LFM3 into d1 (#16519)

Long Yixinge27c6806ad77
diff --git a/studio/backend/core/systemone/catalog.py b/studio/backend/core/systemone/catalog.py
index 86bfedcec2c..394cece49a1 100644
--- a/studio/backend/core/systemone/catalog.py
+++ b/studio/backend/core/systemone/catalog.py
@@ -39,9 +39,16 @@ class Checkpoint:
return Path(self.source).expanduser().is_dir()
-def _gguf(name: str, label: str, repo: str, description: str, download_bytes: int) -> Checkpoint:
+def _gguf(
+ name: str,
+ label: str,
+ repo: str,
+ description: str,
+ download_bytes: int,
+ audio: bool = False,
+) -> Checkpoint:
return Checkpoint(
- name, repo, None, description, download_bytes, "gguf", "llama.cpp", label = label
+ name, repo, None, description, download_bytes, "gguf", "llama.cpp", label = label, audio = audio
)
@@ -155,6 +162,46 @@ CHECKPOINTS = {
"Supersonic Julia-1 (Q8_0 GGUF, llama.cpp only): the smallest, text only, runs on CPU.",
168_166_496,
),
+ _gguf(
+ "d1-omni-600m",
+ "d1 Omni 600M",
+ "LiquidAI/d1-omni-600M-GGUF",
+ "LiquidAI d1 Omni 600M (Q8_0 GGUF): reads images, and hears audio on MLX.",
+ 407_207_584 + 262_791_232,
+ audio = True,
+ ),
+ _gguf(
+ "d1-2b",
+ "d1 2B",
+ "LiquidAI/d1-2B-GGUF",
+ "LiquidAI d1 2B on LFM3-VL (Q8_0 GGUF): reads images.",
+ 1_846_240_576 + 203_031_584,
+ ),
+ _gguf(
+ "d1-omni-2b",
+ "d1 Omni 2B",
+ "LiquidAI/d1-Omni-2B-GGUF",
+ "LiquidAI d1 Omni 2B on LFM3-Omni (Q8_0 GGUF): reads images, and hears audio on MLX.",
+ 1_846_241_184 + 326_104_096,
+ audio = True,
+ ),
+ _gguf(
+ "d1-3b",
+ "d1 3B",
+ "LiquidAI/d1-3B-GGUF",
+ "LiquidAI d1 3B on LFM2.5-VL (Q8_0 GGUF): reads images.",
+ 2_874_781_280 + 583_109_728,
+ ),
+ Checkpoint(
+ "decider-2b",
+ "nativ-community/decider-2b",
+ None,
+ "Decider 2B on Qwen3.5 (MLX only): text only, needs Apple Silicon.",
+ 3_783_689_675,
+ "gguf",
+ "mlx",
+ label = "Decider 2B",
+ ),
)
}
@@ -264,6 +311,50 @@ GGUF_COMPANIONS = {
None,
("1ea6a7e87156eeeda88cb7a36a61265b37ba7b993897b7289b99aea5b5e47069",),
),
+ (
+ "d1-omni-600m",
+ "LiquidAI/d1-omni-600M-GGUF",
+ "8eb2d2a1c5404bba8a34a88bd8e159c242197d51",
+ "d1-omni-600M-Q8_0.gguf",
+ "mmproj-d1-omni-600M-Q8_0.gguf",
+ (
+ "cd94463f4cac9c700ec6750df8df005bb6846de410e159d1f5e985e1171693d3",
+ "df887978550cdbb45f0f57d7b436a0f844689f9f561395a144ced500606c3a89",
+ ),
+ ),
+ (
+ "d1-2b",
+ "LiquidAI/d1-2B-GGUF",
+ "e968f6575f0d4fad928312d7af15cad90fd365c0",
+ "d1-2B-Q8_0.gguf",
+ "mmproj-d1-2B-Q8_0.gguf",
+ (
+ "073313d92b53b0ac8193db6e906cb3c78a98183d244b15b7b8f5426115c6738c",
+ "62cc6d74fd1e5b1d6b48518566bf4c0cfd2468079a0c096ea323b74eed01e0d1",
+ ),
+ ),
+ (
+ "d1-omni-2b",
+ "LiquidAI/d1-Omni-2B-GGUF",
+ "c53ae0b1f92b17065e9158d34019c9ca1ea5bcf8",
+ "d1-Omni-2B-Q8_0.gguf",
+ "mmproj-d1-Omni-2B-Q8_0.gguf",
+ (
+ "aedf1a7c341b4281979423dbc39e9c3d3ad1da8834156c8e75af5399ca3b0861",
+ "31778160ac0ec70f9b59f296aa1c8e70cbd1eb7e3a3ff873d695aaa4c7b8d674",
+ ),
+ ),
+ (
+ "d1-3b",
+ "LiquidAI/d1-3B-GGUF",
+ "bb1e436ea78eb96a3f1acb6da865f70c2fbeb563",
+ "d1-3B-Q8_0.gguf",
+ "mmproj-d1-3B-Q8_0.gguf",
+ (
+ "2f0942d5a5f64cf69c3356d2be439b71644b1f0eb3a580912a5a0179eeaba77d",
+ "2505920ce464b7c54acd8b7b74a931c0c671f06d0c8833760b2fbceeb253e92a",
+ ),
+ ),
)
},
}
@@ -299,6 +390,15 @@ def _qwen_base(repo: str, revision: str, shards: int, size: int, *extra: str) ->
_ADAPTER_FILES = ("adapter_config.json", "adapter_model.safetensors")
+_D1_WEIGHTS = ("model.safetensors", "model.safetensors.index.json")
+_D1_PROCESSOR_FILES = (
+ "config.json",
+ *_D1_WEIGHTS,
+ "chat_template.jinja",
+ "processor_config.json",
+ "tokenizer.json",
+ "tokenizer_config.json",
+)
MLX_COMPANIONS = {
"julia-1": MlxCompanion(
@@ -387,6 +487,49 @@ MLX_COMPANIONS = {
),
28_599_913_943,
),
+ "d1-omni-600m": MlxCompanion(
+ "d1",
+ "LiquidAI/d1-omni-600M-MLX-8bit",
+ "d927e69f5e5db82ad25ae1172f7fd18f0013b6df",
+ ("config.json", *_D1_WEIGHTS, "tokenizer.json", "tokenizer_config.json"),
+ 731_348_970,
+ ),
+ "d1-2b": MlxCompanion(
+ "d1",
+ "LiquidAI/d1-2B-MLX-8bit",
+ "4977063d330b998e7849c39964749c8159a8adf7",
+ (*_D1_PROCESSOR_FILES, "calibration.json"),
+ 2_234_060_291,
+ ),
+ "d1-omni-2b": MlxCompanion(
+ "d1",
+ "LiquidAI/d1-Omni-2B-MLX-8bit",
+ "f0ad5bf18bfc47671aa8dc24036b980a6851c948",
+ (*_D1_PROCESSOR_FILES, "calibration.json"),
+ 2_731_606_810,
+ ),
+ "d1-3b": MlxCompanion(
+ "d1",
+ "LiquidAI/d1-3B-MLX-8bit",
+ "9409cbf3bd5067e1a9b17437b1970f6a0c299ccd",
+ _D1_PROCESSOR_FILES,
+ 3_736_722_967,
+ ),
+ "decider-2b": MlxCompanion(
+ "mlx_vlm",
+ "nativ-community/decider-2b",
+ "acbae4ecce4dbcc0aea8c5a501c9f54008458a70",
+ (
+ "config.json",
+ "decider_config.json",
+ "generation_config.json",
+ "chat_template.jinja",
+ "model.safetensors",
+ "tokenizer.json",
+ "tokenizer_config.json",
+ ),
+ 3_783_689_675,
+ ),
}
# Names TypeSafe's and OpenJev's SDKs send by default, so an unmodified client reaches the configured model.
@@ -428,7 +571,7 @@ def _owner_outputs() -> Path:
def _fine_tune_in(root: Path, folder_name: str) -> Checkpoint | None:
- from utils.models.model_config import clef_folder_kind
+ from utils.models.model_config import clef_folder_kind, is_d1_folder
from .laya_runtime import is_cached
@@ -443,12 +586,15 @@ def _fine_tune_in(root: Path, folder_name: str) -> Checkpoint | None:
# A NUL byte or a symlink loop in a caller's name.
return None
if clef_folder_kind(folder) is not None:
+ d1 = is_d1_folder(folder)
checkpoint = Checkpoint(
CLEF_FINE_TUNE_PREFIX + folder_name,
str(folder),
None,
- "Clef fine-tuned in Unsloth.",
+ "d1 fine-tuned in Unsloth." if d1 else "Clef fine-tuned in Unsloth.",
layout = "clef",
+ # Only the MLX engine reads a d1; the PyTorch Clef loader would add an untrained head.
+ backend = "mlx" if d1 else "pytorch",
)
else:
checkpoint = Checkpoint(
@@ -528,9 +674,13 @@ def default_checkpoint() -> Checkpoint | Connection:
if (checkpoint := fine_tune(configured)) is not None:
return checkpoint
subfolder = os.environ.get("UNSLOTH_SYSTEMONE_SUBFOLDER", "").strip() or None
- from utils.models.model_config import CLEF_MARKERS
+ from utils.models.model_config import CLEF_MARKERS, is_d1_folder
folder = Path(configured).expanduser()
+ if subfolder is None and is_d1_folder(folder):
+ return Checkpoint(
+ LOCAL_NAME, configured, None, "Local d1 checkpoint.", layout = "clef", backend = "mlx"
+ )
if subfolder is None and all((folder / name).is_file() for name in CLEF_MARKERS):
return Checkpoint(LOCAL_NAME, configured, None, "Local Clef checkpoint.", layout = "clef")
return Checkpoint(LOCAL_NAME, configured, subfolder, "Local Laya checkpoint.")
diff --git a/studio/backend/core/systemone/laya_runtime.py b/studio/backend/core/systemone/laya_runtime.py
index a126e6887f6..dacad80b806 100644
--- a/studio/backend/core/systemone/laya_runtime.py
+++ b/studio/backend/core/systemone/laya_runtime.py
@@ -163,13 +163,23 @@ def _engine_available() -> bool:
return True
+def _zoo_serves(family: str) -> bool:
+ # An unsloth-zoo older than this catalog lacks d1 / mlx_vlm: such entries fall back to llama.cpp or are unavailable.
+ families = getattr(sys.modules.get("unsloth_zoo.mlx.decision"), "FAMILIES", None)
+ return families is None or family in families
+
+
def _mlx_target(checkpoint: Checkpoint) -> Checkpoint | None:
"""What unsloth-zoo's MLX engine serves in process on Apple Silicon: a merged Clef's own folder, or the source repo of a GGUF entry."""
if not _engine_available():
return None
if _is_mlx(checkpoint):
- # Only a catalog entry is its own MLX form; a target has none further.
- return checkpoint if CHECKPOINTS.get(checkpoint.name) == checkpoint else None
+ return (
+ checkpoint
+ if (checkpoint.is_local or CHECKPOINTS.get(checkpoint.name) == checkpoint)
+ and _zoo_serves(_mlx_family(checkpoint))
+ else None
+ )
if checkpoint.layout == "clef":
if checkpoint.is_local:
from utils.models.model_config import clef_folder_kind
@@ -177,7 +187,11 @@ def _mlx_target(checkpoint: Checkpoint) -> Checkpoint | None:
return None
return dataclasses.replace(checkpoint, backend = MLX)
companion = MLX_COMPANIONS.get(checkpoint.name)
- if companion is None or CHECKPOINTS.get(checkpoint.name) != checkpoint:
+ if (
+ companion is None
+ or CHECKPOINTS.get(checkpoint.name) != checkpoint
+ or not _zoo_serves(companion.family)
+ ):
return None
return dataclasses.replace(
checkpoint,
@@ -190,7 +204,16 @@ def _mlx_target(checkpoint: Checkpoint) -> Checkpoint | None:
def _mlx_reads(checkpoint: Checkpoint, media: str) -> bool:
"""Whether the MLX form of an entry reads `media` ("images" or "audio"): a Clef with that tower and its processor, on an unsloth-zoo that reads it."""
- if checkpoint.layout != "clef" or _mlx_target(checkpoint) is None:
+ if _mlx_target(checkpoint) is None:
+ return False
+ if checkpoint.layout == GGUF:
+ companion = MLX_COMPANIONS.get(checkpoint.name)
+ return (
+ companion is not None
+ and companion.family == "d1"
+ and (media == "images" or checkpoint.audio)
+ )
+ if checkpoint.layout != "clef":
return False
try:
from unsloth_zoo.mlx.decision import ClefModel
@@ -209,12 +232,12 @@ def _mlx_reads(checkpoint: Checkpoint, media: str) -> bool:
return False
if not isinstance(config, dict):
return False
- # Qwen3-Omni keeps its towers under the thinker, Phi-4-multimodal under `embd_layer`; Nemotron Omni hears through a sound tower.
+ # Qwen3-Omni: towers under thinker; Phi-4-mm: `embd_layer`; Nemotron Omni: sound tower; LFM3-Omni: audio encoder.
parts = (config, config.get("thinker_config"), config.get("embd_layer"))
towers = (
("vision_config", "image_embd_layer")
if media == "images"
- else ("audio_config", "sound_config", "audio_embd_layer")
+ else ("audio_config", "audio_encoder_config", "sound_config", "audio_embd_layer")
)
return any(
isinstance(part, dict) and part.get(tower) is not None for part in parts for tower in towers
@@ -232,7 +255,14 @@ def _mlx_repos(companion) -> tuple:
def _mlx_family(checkpoint: Checkpoint) -> str:
- return "clef" if checkpoint.layout == "clef" else MLX_COMPANIONS[checkpoint.name].family
+ if checkpoint.layout != "clef":
+ return MLX_COMPANIONS[checkpoint.name].family
+ from utils.models.model_config import is_d1_folder
+ return (
+ "d1"
+ if checkpoint.is_local and is_d1_folder(Path(checkpoint.source).expanduser())
+ else "clef"
+ )
def _wants_gpu(checkpoint) -> bool:
@@ -327,7 +357,7 @@ def _local_folder(checkpoint: Checkpoint) -> Path:
def _native_target(checkpoint: Checkpoint) -> Checkpoint | None:
"""The llama.cpp form of an entry: ggml-org's GGUF for a stock Clef, a current export for a Clef or Laya folder, a GGUF entry itself."""
if checkpoint.layout == GGUF:
- return checkpoint
+ return None if _is_mlx(checkpoint) else checkpoint
if _is_native(checkpoint):
return None
if checkpoint.is_local:
@@ -1051,7 +1081,12 @@ class _EngineAgent:
# state and questions out its own way, so what the model reads can differ from this count by the layout's overhead.
text = state if isinstance(state, str) else json.dumps(state, ensure_ascii = False)
text += json.dumps(questions, ensure_ascii = False)
- tokenizer = getattr(self.model, "tokenizer", None)
+ processor = getattr(self.model, "processor", None)
+ tokenizer = (
+ getattr(self.model, "tokenizer", None)
+ or getattr(processor, "tokenizer", None)
+ or (processor if hasattr(processor, "encode") else None)
+ )
if tokenizer is not None:
ids = tokenizer.encode(text, add_special_tokens = False)
if len(getattr(ids, "ids", ids)) > _ENGINE_TOKENS:
diff --git a/studio/backend/core/training/decision_trainer.py b/studio/backend/core/training/decision_trainer.py
index c27ac9274d7..4ee7544ceb4 100644
--- a/studio/backend/core/training/decision_trainer.py
+++ b/studio/backend/core/training/decision_trainer.py
@@ -433,6 +433,31 @@ def _is_mlx() -> bool:
return hardware.DEVICE == hardware.DeviceType.MLX
+def _zoo_trains_d1() -> bool:
+ try:
+ from unsloth_zoo.mlx import decision
+ except Exception:
+ return False
+ return hasattr(decision, "load_language_model_as_d1")
+
+
+def _lm_decision_head(model_name: str, token: str | None) -> str:
+ # An unsloth-zoo without the d1 trainer keeps the Clef head these models trained into before.
+ if not _is_mlx() or not _zoo_trains_d1():
+ return "clef"
+ try:
+ folder = Path(model_name).expanduser()
+ if folder.is_dir():
+ config = folder / "config.json"
+ else:
+ from huggingface_hub import hf_hub_download
+ config = hf_hub_download(model_name, "config.json", token = token)
+ model_type = json.loads(Path(config).read_text(encoding = "utf-8")).get("model_type")
+ except (OSError, ValueError, AttributeError):
+ return "clef"
+ return "d1" if model_type in ("lfm3_vl", "lfm3_omni") else "clef"
+
+
def _peak_memory_gb() -> float | None:
import torch
@@ -587,7 +612,6 @@ def _run(event_queue: Any, stop_queue: Any, config: dict, output_dir: str) -> No
model_name = config["model_name"]
subfolder = config.get("model_subfolder") or None
- # "llm": a plain text or vision LLM that gets a fresh Clef joint schema head.
llm = config.get("decision_layout") == "llm"
clef = llm or config.get("decision_layout") == "clef"
hf_token = _worker_hf_token(config)
@@ -612,11 +636,12 @@ def _run(event_queue: Any, stop_queue: Any, config: dict, output_dir: str) -> No
check_stop()
status("Loading decision model...")
if llm:
+ head = config.get("decision_head") or _lm_decision_head(model_name, hf_token)
model, tokenizer = FastDecisionModel.from_pretrained(
model_name,
- decision_head = "clef",
+ decision_head = head,
max_seq_length = config.get("max_seq_length") or None,
- load_in_4bit = use_lora and bool(config.get("load_in_4bit")),
+ load_in_4bit = use_lora and bool(config.get("load_in_4bit")) and head != "d1",
full_finetuning = not use_lora,
token = hf_token or None,
use_gradient_checkpointing = gradient_checkpointing,
@@ -809,7 +834,7 @@ def _run(event_queue: Any, stop_queue: Any, config: dict, output_dir: str) -> No
model.decision_config["training"] = {
"base": model_name,
"subfolder": subfolder,
- "decision_head": "clef (new)" if llm else None,
+ "decision_head": f"{head} (new)" if llm else None,
"method": ("qlora" if clef and config.get("load_in_4bit") else "lora")
if use_lora
else "full",
diff --git a/studio/backend/core/training/training.py b/studio/backend/core/training/training.py
index a6a3d5103b9..125085f2c04 100644
--- a/studio/backend/core/training/training.py
+++ b/studio/backend/core/training/training.py
@@ -294,6 +294,7 @@ def _build_training_worker_config(values: dict[str, Any]) -> dict[str, Any]:
"is_decision": values.get("is_decision", False),
"model_subfolder": values.get("model_subfolder"),
"decision_layout": values.get("decision_layout"),
+ "decision_head": values.get("decision_head"),
"num_epochs": values.get("num_epochs", 3),
"learning_rate": values.get("learning_rate", "2e-4"),
"embedding_learning_rate": values.get("embedding_learning_rate"),
diff --git a/studio/backend/hub/modelscope/upstream.py b/studio/backend/hub/modelscope/upstream.py
index da6dc711599..78ca18a2d38 100644
--- a/studio/backend/hub/modelscope/upstream.py
+++ b/studio/backend/hub/modelscope/upstream.py
@@ -117,6 +117,26 @@ _PINNED_COMMITS = {
"ggml-org/julia-1-gguf",
"16fee17949206fbf58da9347daea44d792a81211",
): "75a35d1ee9701238e520fce8a3c8c5a546f63c65",
+ (
+ "model",
+ "liquidai/d1-omni-600m-gguf",
+ "8eb2d2a1c5404bba8a34a88bd8e159c242197d51",
+ ): "34f9c859701c5a1b041644078fc7216cc24f9705",
+ (
+ "model",
+ "liquidai/d1-2b-gguf",
+ "e968f6575f0d4fad928312d7af15cad90fd365c0",
+ ): "6eb1050a62fcfd4b9349c596f8b04d67e39c1bd1",
+ (
+ "model",
+ "liquidai/d1-omni-2b-gguf",
+ "c53ae0b1f92b17065e9158d34019c9ca1ea5bcf8",
+ ): "d0a359d1e918b90329fa449baa8519f7c7c2a2ef",
+ (
+ "model",
+ "liquidai/d1-3b-gguf",
+ "bb1e436ea78eb96a3f1acb6da865f70c2fbeb563",
+ ): "49d07e1e9b33827c7dfbdeaeb3437828d63d7e67",
}
diff --git a/studio/backend/models/training.py b/studio/backend/models/training.py
index 1fb07245726..49503345d0d 100644
--- a/studio/backend/models/training.py
+++ b/studio/backend/models/training.py
@@ -670,6 +670,10 @@ class TrainingStartRequest(BaseModel):
None,
description = "Set by the server from the checkpoint files; a caller's value is replaced",
)
+ decision_head: Optional[Literal["clef", "d1"]] = Field(
+ None,
+ description = "What a language model trains into: a Clef head, or a d1 on MLX for LFM3-VL and LFM3-Omni, the default there",
+ )
enable_wandb: bool = Field(False, description = "Enable Weights & Biases logging")
wandb_token: Optional[str] = Field(None, description = "W&B token")
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index 95dd115242f..b020c9f0c37 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -1719,7 +1719,8 @@ def _clef_availability(checkpoint, reason: Optional[str]) -> dict:
only_mlx = getattr(checkpoint, "backend", None) == laya_runtime.MLX
if only_mlx or getattr(checkpoint, "layout", "laya") == laya_runtime.GGUF:
- only = {} if only_mlx else {"llama_cpp_only": True}
+ # A non-Clef MLX-only entry (Decider) is flagged too, so its runtime row stays reachable.
+ only = {} if only_mlx and checkpoint.layout == "clef" else {"llama_cpp_only": True}
# Selectable under a PyTorch runtime: the runtime row then says to switch it.
try:
laya_runtime.select(checkpoint, preference = "auto")
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py
index 0521934b7ff..6b83de37f50 100644
--- a/studio/backend/routes/training.py
+++ b/studio/backend/routes/training.py
@@ -2226,6 +2226,7 @@ async def _admit_training_request(
"is_decision": request.is_decision,
"model_subfolder": request.model_subfolder,
"decision_layout": request.decision_layout,
+ "decision_head": request.decision_head,
"enable_wandb": request.enable_wandb,
"wandb_token": request.wandb_token or "",
"wandb_project": request.wandb_project or "",
diff --git a/studio/backend/tests/test_decision_native.py b/studio/backend/tests/test_decision_native.py
index 9f8bbbe22c4..b6ef7a68bc3 100644
--- a/studio/backend/tests/test_decision_native.py
+++ b/studio/backend/tests/test_decision_native.py
@@ -1189,7 +1189,7 @@ def _cache_blob(
def test_gguf_entries_are_listed_as_llama_cpp_only(home, client, stub):
- gguf = {n for n, c in catalog.CHECKPOINTS.items() if c.layout == "gguf"}
+ gguf = {n for n, c in catalog.CHECKPOINTS.items() if c.layout == "gguf" and c.backend != "mlx"}
assert gguf == {
"kev-0.8b",
"kev-4b",
@@ -1199,6 +1199,10 @@ def test_gguf_entries_are_listed_as_llama_cpp_only(home, client, stub):
"openjev",
"laya-gguf",
"julia-1",
+ "d1-omni-600m",
+ "d1-2b",
+ "d1-omni-2b",
+ "d1-3b",
}
for name in gguf:
checkpoint, companion = catalog.CHECKPOINTS[name], catalog.GGUF_COMPANIONS[name]
diff --git a/studio/backend/tests/test_decision_serving.py b/studio/backend/tests/test_decision_serving.py
index 18509155033..74a18493933 100644
--- a/studio/backend/tests/test_decision_serving.py
+++ b/studio/backend/tests/test_decision_serving.py
@@ -1168,7 +1168,7 @@ def test_clef_serves_on_nvidia_and_amd_gpus(home, client, clef, monkeypatch, kin
assert all(
m["available"] and m["unavailable_reason"] is None
for m in models
- if not m["llama_cpp_only"] and m["name"] != "clef-omni"
+ if not m["llama_cpp_only"] and m["name"] not in ("clef-omni", "decider-2b")
)
@@ -1658,6 +1658,113 @@ def test_a_clef_with_its_audio_tower_hears_through_the_mlx_engine(
asyncio.run(systemone._decide(connection, "s", asked, None, [clip]))
+def test_decider_is_served_only_by_the_mlx_engine(home, client, engine):
+ entry = catalog.CHECKPOINTS["decider-2b"]
+ assert laya_runtime._mlx_family(laya_runtime._mlx_target(entry)) == "mlx_vlm"
+ assert laya_runtime.select(entry)[0].backend == "mlx"
+ plan = laya_runtime.download_plan(entry)
+ assert plan["repo"] == entry.source and "decider_config.json" in plan["files"]
+ assert not laya_runtime._mlx_reads(entry, "images") and not laya_runtime._mlx_reads(
+ entry, "audio"
+ )
+ with pytest.raises(laya_runtime.Unavailable, match = "served only by MLX"):
+ laya_runtime.select(entry, preference = "llama.cpp")
+ listed = {m["name"]: m for m in client.get("/api/settings/systemone").json()["models"]}
+ assert listed["decider-2b"]["llama_cpp_only"] and not listed["clef-omni"]["llama_cpp_only"]
+
+
+def test_a_d1_is_served_by_the_mlx_engine_and_hears_only_on_omni(home, client, engine):
+ clip = "data:audio/wav;base64,AA=="
+ for name, hears in (
+ ("d1-omni-600m", True),
+ ("d1-omni-2b", True),
+ ("d1-2b", False),
+ ("d1-3b", False),
+ ):
+ entry = catalog.CHECKPOINTS[name]
+ assert laya_runtime._mlx_family(laya_runtime._mlx_target(entry)) == "d1"
+ assert (
+ laya_runtime._mlx_reads(entry, "images")
+ and laya_runtime._mlx_reads(entry, "audio") == hears
+ )
+ repo = catalog.MLX_COMPANIONS[name]
+ assert "config.json" in repo.files and "model.safetensors" in repo.files
+ assert laya_runtime.select(catalog.CHECKPOINTS["d1-2b"], audio = None)[0].backend == "mlx"
+ with pytest.raises(laya_runtime.Unavailable, match = "does not hear audio here"):
+ laya_runtime.select(catalog.CHECKPOINTS["d1-2b"], audio = [clip])
+ assert not laya_runtime._mlx_reads(catalog.CHECKPOINTS["kev-4b"], "images")
+
+
+def test_an_older_zoo_without_d1_keeps_llama_cpp_for_d1_and_no_decider(home, client, engine):
+ sys.modules["unsloth_zoo.mlx.decision"].FAMILIES = dict.fromkeys(("laya", "kev", "clef"))
+ d1 = catalog.CHECKPOINTS["d1-2b"]
+ assert laya_runtime._mlx_target(d1) is None
+ assert laya_runtime._native_target(d1) == d1
+ assert laya_runtime._mlx_target(catalog.CHECKPOINTS["decider-2b"]) is None
+ assert laya_runtime._mlx_target(catalog.CHECKPOINTS["kev-4b"]) is not None
+
+
+def test_a_trained_d1_is_found_and_served_as_a_d1(home, client, engine, monkeypatch):
+ folder = home / "d1_ft"
+ folder.mkdir()
+ config = {
+ "architectures": ["D1Model"],
+ "model_type": "lfm3_omni",
+ "vision_config": {},
+ "audio_encoder_config": {},
+ }
+ (folder / "config.json").write_text(json.dumps(config), encoding = "utf-8")
+ (folder / "processor_config.json").write_text("{}", encoding = "utf-8")
+ (folder / "model.safetensors").write_bytes(b"")
+ engine.cached.add(catalog.CLEF_FINE_TUNE_PREFIX + "d1_ft")
+ tuned = catalog.fine_tune(catalog.CLEF_FINE_TUNE_PREFIX + "d1_ft")
+ assert tuned.layout == "clef" and tuned.description.startswith("d1 fine-tuned")
+ assert laya_runtime._mlx_family(laya_runtime._mlx_target(tuned)) == "d1"
+ zoo = sys.modules["unsloth_zoo.mlx.decision"]
+ zoo.ClefModel = SimpleNamespace(takes_images = True, takes_audio = True)
+ assert laya_runtime.input_modalities(tuned) == ["text", "image", "audio"]
+ assert laya_runtime.select(tuned)[0].backend == "mlx"
+ for runtime in ("pytorch", "llama.cpp"):
+ with pytest.raises(laya_runtime.Unavailable, match = "served only by MLX"):
+ laya_runtime.select(tuned, preference = runtime)
+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", str(folder))
+ local = catalog.default_checkpoint()
+ assert (local.name, local.layout, local.backend) == (catalog.LOCAL_NAME, "clef", "mlx")
+ for stock in (
+ {
+ "model_type": "lfm2_vl",
+ "architectures": ["Lfm2VlForConditionalGeneration"],
+ "auto_map": {"AutoModel": "modeling_d1.D1Model"},
+ },
+ {
+ "model_type": "d1_omni",
+ "architectures": ["D1OmniModel"],
+ "auto_map": {"AutoModel": "modeling_d1.D1OmniModel"},
+ },
+ ):
+ (folder / "config.json").write_text(json.dumps(stock), encoding = "utf-8")
+ local = catalog.default_checkpoint()
+ assert (local.layout, local.backend, laya_runtime._mlx_family(local)) == (
+ "clef",
+ "mlx",
+ "d1",
+ )
+ plain = catalog.fine_tune(_clef_fine_tune(home, "clef_ft"))
+ assert laya_runtime._mlx_family(laya_runtime._mlx_target(plain)) == "clef"
+
+
[email protected]("wrapped", [True, False])
+def test_a_processor_only_model_keeps_the_engine_token_limit(engine, wrapped):
+ tokens = SimpleNamespace(encode = lambda text, add_special_tokens = False: [0] * len(text))
+ agent = laya_runtime._EngineAgent.__new__(laya_runtime._EngineAgent)
+ processor = SimpleNamespace(tokenizer = tokens) if wrapped else tokens
+ agent.model, agent.positions = SimpleNamespace(processor = processor), None
+ with pytest.raises(laya_runtime.Unavailable, match = "longer than"):
+ agent.decide(
+ "x" * (laya_runtime._ENGINE_TOKENS + 1), {"q": {"type": "noul", "instructions": "i"}}
+ )
+
+
def test_clef_omni_is_served_by_the_mlx_engine_or_not_at_all(home, client, engine, monkeypatch):
omni, clip = catalog.CHECKPOINTS["clef-omni"], "data:audio/wav;base64,AA=="
zoo = sys.modules["unsloth_zoo.mlx.decision"]
diff --git a/studio/backend/tests/test_decision_training.py b/studio/backend/tests/test_decision_training.py
index 6195feffa14..b555ea62e97 100644
--- a/studio/backend/tests/test_decision_training.py
+++ b/studio/backend/tests/test_decision_training.py
@@ -885,3 +885,31 @@ def test_held_out_decisions_count_every_clef_question():
laya = [{"row": 0}, {"row": 1}]
clef = [{"row": 0, "labels": [0, 1, 2]}, {"row": 1, "labels": [1]}]
assert (_decision_count(laya), _decision_count(clef)) == (2, 4)
+
+
+ "model_type, mlx, zoo_d1, head",
+ [
+ ("lfm3_omni", True, True, "d1"),
+ ("lfm3_vl", True, True, "d1"),
+ ("lfm3", True, True, "clef"),
+ ("qwen3_5", True, True, "clef"),
+ ("lfm3_omni", False, True, "clef"),
+ ("lfm3_vl", True, False, "clef"),
+ ],
+)
+def test_lfm3_vision_and_omni_train_as_a_d1_on_mlx(
+ tmp_path, monkeypatch, model_type, mlx, zoo_d1, head
+):
+ from core.training import decision_trainer
+
+ monkeypatch.setattr(decision_trainer, "_is_mlx", lambda: mlx)
+ monkeypatch.setattr(decision_trainer, "_zoo_trains_d1", lambda: zoo_d1)
+ (tmp_path / "config.json").write_text(json.dumps({"model_type": model_type}), encoding = "utf-8")
+ assert decision_trainer._lm_decision_head(str(tmp_path), None) == head
+
+
+def test_an_unreadable_language_model_keeps_the_clef_head(tmp_path, monkeypatch):
+ from core.training import decision_trainer
+ monkeypatch.setattr(decision_trainer, "_is_mlx", lambda: True)
+ assert decision_trainer._lm_decision_head(str(tmp_path), None) == "clef"
diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.py
index 79358f2f703..4802abef13c 100644
--- a/studio/backend/utils/models/model_config.py
+++ b/studio/backend/utils/models/model_config.py
@@ -3664,8 +3664,25 @@ def clef_files_kind(has) -> Optional[str]:
return None
+def is_d1_folder(folder: Path) -> bool:
+ try:
+ config = json.loads((folder / "config.json").read_text(encoding = "utf-8"))
+ except (OSError, ValueError):
+ return False
+ if not isinstance(config, dict):
+ return False
+ # Markers unsloth-zoo's d1 loader reads: d1-3B only in auto_map, d1-omni by model_type.
+ automodel = str((config.get("auto_map") or {}).get("AutoModel", ""))
+ return (
+ config.get("model_type") == "d1_omni"
+ or "D1Model" in (config.get("architectures") or ())
+ or automodel.endswith("D1Model")
+ )
+
+
def clef_folder_kind(folder: Path) -> Optional[str]:
- return clef_files_kind(lambda name: (folder / name).is_file())
+ kind = clef_files_kind(lambda name: (folder / name).is_file())
+ return kind or ("merged" if is_d1_folder(folder) else None)
def _folder_decision_layout(folder: Path) -> Optional[str]:
diff --git a/tests/test_decision_mlx_guards.py b/tests/test_decision_mlx_guards.py
index 9b698defcb2..bd4d6f83598 100644
--- a/tests/test_decision_mlx_guards.py
+++ b/tests/test_decision_mlx_guards.py
@@ -2,7 +2,9 @@
# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
import importlib.util
+import json
import sys
+import types
from pathlib import Path
import pytest
@@ -44,3 +46,159 @@ def test_adapters_refuse_a_full_finetune_before_loading(decision_mlx, monkeypatc
monkeypatch.setattr(decision_mlx, "_decision_zoo", never)
with pytest.raises(ValueError, match = "save_pretrained_merged"):
decision_mlx._load_clef(tmp_path, None, False, True, None, False, {})
+
+
+def _d1_zoo(tmp_path, family = None):
+ pipeline = types.SimpleNamespace(
+ family = "d1", processor = "processor", model = types.SimpleNamespace(freeze = lambda: None)
+ )
+ network = lambda: types.SimpleNamespace(train = lambda: None)
+ calls = {}
+ zoo = types.SimpleNamespace(
+ FAMILIES = {"d1": types.SimpleNamespace(matches = lambda folder: family == "d1")},
+ detect_family = lambda folder: pytest.fail(
+ "detect_family refuses inputs the Clef loader takes"
+ ),
+ load_language_model_as_d1 = lambda folder, token = None: pipeline,
+ load_language_model_as_clef = lambda *args, **kwargs: pytest.fail(
+ "a d1 was given a Clef head"
+ ),
+ D1Network = lambda pipeline, checkpointing: network(),
+ d1_training_network = lambda pipeline, **kwargs: (calls.update(lora = kwargs), network())[1],
+ clef_training_network = lambda *args, **kwargs: pytest.fail("a d1 took the Clef network"),
+ d1_training_item = lambda *args: calls.update(item = args)
+ or {"slots": [], "input_ids": [0] * 8},
+ clef_training_item = lambda *args, **kwargs: pytest.fail("a d1 took the Clef item"),
+ save_d1_model = lambda *args: calls.update(saved = args),
+ save_clef_model = lambda *args: pytest.fail("a d1 took the Clef saver"),
+ )
+ (tmp_path / "config.json").write_text("{}", encoding = "utf-8")
+ return zoo, pipeline, calls
+
+
+def _load_d1(
+ decision_mlx,
+ tmp_path,
+ head = "d1",
+ load_in_4bit = False,
+):
+ return decision_mlx._load_lm_as_clef(
+ tmp_path, None, load_in_4bit, False, None, None, False, False, 0, decision_head = head
+ )
+
+
[email protected]("head, family", [("d1", None), ("clef", "d1")])
+def test_a_d1_trains_saves_and_tokenizes_as_one(decision_mlx, monkeypatch, tmp_path, head, family):
+ zoo, pipeline, calls = _d1_zoo(tmp_path, family)
+ monkeypatch.setattr(decision_mlx, "_decision_zoo", lambda: zoo)
+ monkeypatch.setattr(
+ decision_mlx,
+ "_decision_annotate",
+ lambda model, **attributes: [setattr(model, *pair) for pair in attributes.items()]
+ and model,
+ )
+ model, tokenizer = _load_d1(decision_mlx, tmp_path, head)
+ assert tokenizer == "processor" and model.decision_config["layout"] == "d1"
+ common = types.SimpleNamespace(clamp_temperature = float)
+ monkeypatch.setattr(decision_mlx, "_laya", lambda: types.SimpleNamespace(common = common))
+ decision_mlx._clef_item(zoo, pipeline, "s", {"q": {}}, 64, ["image"], ["clip"])
+ assert calls["item"] == (pipeline, "s", {"q": {}}, ["image"], ["clip"])
+ decision_mlx._clef_item(zoo, pipeline, "s", {"q": {}}, 8, ["image"], ["clip"])
+ with pytest.raises(ValueError, match = "past max_seq_length 7"):
+ decision_mlx._clef_item(zoo, pipeline, "s", {"q": {}}, 7, ["image"], ["clip"])
+ model = decision_mlx.FastDecisionModel.get_peft_model(model, r = 8, lora_alpha = 8)
+ assert calls["lora"]["r"] == 8 and model.is_clef and model._unsloth_lora
+ model.encoder = types.SimpleNamespace(
+ trainable_parameters = dict, named_modules = list, freeze = lambda: None
+ )
+ decision_mlx._decision_save_merged(model, "out")
+ assert calls["saved"] == (pipeline, "out", tmp_path, None)
+ model.decision_config.update(head_temperature = 2.0, temperature = [1.0, 1.5, 3.0])
+ decision_mlx._decision_save_merged(model, "out")
+ assert calls["saved"][3] == {"choice": 2.0, "score": 3.0, "noul": 6.0}
+
+
+def test_a_reloaded_d1_answers_at_its_calibration_until_it_trains(
+ decision_mlx, monkeypatch, tmp_path
+):
+ zoo, *_ = _d1_zoo(tmp_path)
+ monkeypatch.setattr(decision_mlx, "_decision_zoo", lambda: zoo)
+ monkeypatch.setattr(
+ decision_mlx,
+ "_decision_annotate",
+ lambda model, **attributes: [setattr(model, *pair) for pair in attributes.items()]
+ and model,
+ )
+ buckets = {"noul": 8.0, "choice:small": 2.0}
+ (tmp_path / "calibration.json").write_text(
+ json.dumps({"temperatures": buckets, "default": 4.0}), encoding = "utf-8"
+ )
+ model, _ = _load_d1(decision_mlx, tmp_path)
+ config = model.decision_config
+ served = [config["head_temperature"] * t for t in config["temperature"]]
+ assert dict(zip(decision_mlx.QUESTION_TYPES, served)) == {
+ "choice": 2.0,
+ "score": 4.0,
+ "noul": 8.0,
+ }
+ for fitted in (
+ {"noul": 2.0, "choice:small": 10.0, "default": 1.0},
+ {"noul": 10.0, "choice:small": 1.0, "default": 2.0},
+ ):
+ (tmp_path / "calibration.json").write_text(
+ json.dumps({"temperatures": fitted, "default": fitted["default"]}), encoding = "utf-8"
+ )
+ config = decision_mlx._d1_temperatures(tmp_path)
+ assert all(0.5 <= t <= 5.0 for t in config["temperature"])
+ tuned = decision_mlx.FastDecisionModel.get_peft_model(model, r = 8, lora_alpha = 8)
+ assert (
+ tuned.decision_config["head_temperature"] == 1.0
+ and tuned.decision_config["temperature"] == [1.0] * 3
+ )
+ full, _ = decision_mlx._load_lm_as_clef(
+ tmp_path, None, False, True, None, None, False, False, 0, decision_head = "d1"
+ )
+ assert full.decision_config["head_temperature"] == 1.0
+
+
+def test_a_d1_score_answers_its_median_level(decision_mlx):
+ zoo = types.SimpleNamespace(
+ d1_answer = lambda pipeline, question, probabilities: {"type": "score", "score": 1.0}
+ )
+ predicted = decision_mlx._d1_predicted(
+ zoo, None, {"type": "score"}, {"0": 0.40, "1": 0.35, "2": 0.25}
+ )
+ assert predicted["score"] == 1.0 and predicted["answer"] == 1
+
+
+def test_a_language_model_keeps_the_clef_loader(decision_mlx, monkeypatch, tmp_path):
+ zoo, *_ = _d1_zoo(tmp_path)
+ zoo.load_language_model_as_clef = lambda *args, **kwargs: types.SimpleNamespace(
+ tokenizer = "t",
+ model = types.SimpleNamespace(freeze = lambda: None),
+ head = types.SimpleNamespace(unfreeze = lambda: None),
+ )
+ zoo.ClefNetwork = lambda pipeline, checkpointing: types.SimpleNamespace(train = lambda: None)
+ monkeypatch.setattr(decision_mlx, "_decision_zoo", lambda: zoo)
+ monkeypatch.setattr(decision_mlx, "_decision_annotate", lambda model, **attributes: model)
+ assert _load_d1(decision_mlx, tmp_path, "clef")[1] == "t"
+
+
+def test_a_d1_ignores_4bit_out_loud_and_other_heads_are_refused(
+ decision_mlx, monkeypatch, tmp_path
+):
+ zoo, *_ = _d1_zoo(tmp_path)
+ monkeypatch.setattr(decision_mlx, "_decision_zoo", lambda: zoo)
+ monkeypatch.setattr(decision_mlx, "_decision_annotate", lambda model, **attributes: model)
+ with pytest.warns(UserWarning, match = "load_in_4bit"):
+ _load_d1(decision_mlx, tmp_path, load_in_4bit = True)
+ with pytest.raises(ValueError, match = "must be one of"):
+ _load_d1(decision_mlx, tmp_path, "other")
+
+
+def test_an_older_zoo_is_told_to_upgrade_for_a_d1(decision_mlx, monkeypatch, tmp_path):
+ zoo, *_ = _d1_zoo(tmp_path)
+ del zoo.load_language_model_as_d1
+ monkeypatch.setattr(decision_mlx, "_decision_zoo", lambda: zoo)
+ with pytest.raises(ImportError, match = "newer unsloth-zoo"):
+ _load_d1(decision_mlx, tmp_path)
diff --git a/unsloth/models/decision_mlx.py b/unsloth/models/decision_mlx.py
index 72588c99b66..c9b574f7068 100644
--- a/unsloth/models/decision_mlx.py
+++ b/unsloth/models/decision_mlx.py
@@ -367,7 +367,7 @@ def _clef_save(
**kwargs,
) -> None:
# Adapters plus the head, as Unsloth's other models; a full finetune has none, so it saves merged.
- if not getattr(self, "_unsloth_lora", False):
+ if not getattr(self, "_unsloth_lora", False) or _is_d1(self._unsloth_pipeline):
return self.save_pretrained_merged(save_directory, tokenizer)
config = {**self.decision_config, "fine_tuned": True}
_decision_zoo().save_clef_adapter(
@@ -453,6 +453,12 @@ def _clef_item(
images,
audio = (),
):
+ if _is_d1(pipeline):
+ # A d1 reads questions beside the whole state: skip an over-limit record, never truncate.
+ item = zoo.d1_training_item(pipeline, state, questions, images, audio)
+ if max_length is not None and len(item["input_ids"]) > max_length:
+ raise ValueError(f"{len(item['input_ids'])} tokens is past max_seq_length {max_length}")
+ return item
media = {name: value for name, value in (("images", images), ("audio", audio)) if value}
taken = inspect.signature(zoo.clef_training_item).parameters
for name in media:
@@ -520,7 +526,19 @@ def _decision_save_merged(
frozen = getattr(self, "_unsloth_frozen_backbone", ())
FastDecisionModel.unfreeze_backbone(self)
try:
- if _is_clef(self):
+ if _is_d1(self._unsloth_pipeline if _is_clef(self) else None):
+ # A d1 has no head to fold temperatures into.
+ common = _laya().common
+ fitted = {
+ kind: config["head_temperature"] * common.clamp_temperature(value)
+ for kind, value in zip(QUESTION_TYPES, config["temperature"])
+ }
+ if set(fitted.values()) == {1.0}:
+ fitted = None
+ _decision_zoo().save_d1_model(
+ self._unsloth_pipeline, save_directory, self._unsloth_source, fitted
+ )
+ elif _is_clef(self):
_decision_zoo().save_clef_model(
self._unsloth_pipeline, save_directory, self._unsloth_source, config
)
@@ -560,16 +578,29 @@ _CLEF_ATTRIBUTES = (
)
+def _is_d1(pipeline) -> bool:
+ return getattr(pipeline, "family", None) == "d1"
+
+
+def _training_network(zoo, pipeline):
+ return zoo.d1_training_network if _is_d1(pipeline) else zoo.clef_training_network
+
+
def _clef_network(pipeline, folder, config, full_finetuning, gradient_checkpointing):
zoo = _decision_zoo()
# A checkpoint saved as adapters comes with them, and they go on training.
adapters = hasattr(pipeline, "base_folder")
+ tokenizer = pipeline.processor if _is_d1(pipeline) else pipeline.tokenizer
if full_finetuning or adapters:
- network = zoo.clef_training_network(
+ network = _training_network(zoo, pipeline)(
pipeline,
full_finetuning = bool(full_finetuning),
gradient_checkpointing = gradient_checkpointing,
)
+ elif _is_d1(pipeline):
+ pipeline.model.freeze()
+ network = zoo.D1Network(pipeline, gradient_checkpointing)
+ network.train()
else:
# Until get_peft_model adds adapters, only the joint head trains.
pipeline.model.freeze()
@@ -583,10 +614,10 @@ def _clef_network(pipeline, folder, config, full_finetuning, gradient_checkpoint
_unsloth_pipeline = pipeline,
_unsloth_source = getattr(pipeline, "base_folder", folder),
_unsloth_full_finetuning = bool(full_finetuning),
- _saved_temp_tokenizer = pipeline.tokenizer,
+ _saved_temp_tokenizer = tokenizer,
_unsloth_lora = adapters,
)
- return network, pipeline.tokenizer
+ return network, tokenizer
def _load_clef(
@@ -622,6 +653,67 @@ def _load_clef(
return _clef_network(pipeline, folder, config, full_finetuning, gradient_checkpointing)
+def _d1_predicted(zoo, pipeline, question, probabilities) -> dict:
+ # A d1's answer is its median level, not the argmax.
+ answer = zoo.d1_answer(pipeline, question, probabilities)
+ predicted = _predicted(question, answer, probabilities)
+ if question["type"] == "score":
+ predicted["answer"] = int(answer["score"])
+ return predicted
+
+
+def _d1_temperatures(folder) -> dict:
+ calibration = folder / "calibration.json" if folder else None
+ if calibration is None or not calibration.is_file():
+ return {"head_temperature": 1.0, "temperature": [1.0] * 3}
+ saved = json.loads(calibration.read_text(encoding = "utf-8"))
+ score, buckets = float(saved.get("default", 1.0)), saved["temperatures"]
+ served = {
+ "choice": buckets.get("choice:small", score),
+ "score": score,
+ "noul": buckets.get("noul", score),
+ }
+ # Per-type temperatures are clamped to [0.5, 5] on read.
+ head = min(served.values()) * 2
+ return {
+ "head_temperature": head,
+ "temperature": [served[kind] / head for kind in QUESTION_TYPES],
+ }
+
+
+def _load_lm_as_d1(
+ zoo,
+ folder,
+ model_name,
+ max_seq_length,
+ load_in_4bit,
+ full_finetuning,
+ token,
+ revision,
+ gradient_checkpointing,
+):
+ if not hasattr(zoo, "load_language_model_as_d1"):
+ raise ImportError(
+ "Unsloth: training a d1 needs a newer unsloth-zoo. "
+ "Upgrade with `pip install -U unsloth-zoo`."
+ )
+ if load_in_4bit:
+ warnings.warn(
+ "Unsloth: a d1 trains at its checkpoint's precision, so load_in_4bit is ignored."
+ )
+ pipeline = zoo.load_language_model_as_d1(folder, token = token)
+ config = {
+ "layout": "d1",
+ "max_len": int(max_seq_length or CLEF_MAX_LEN),
+ "base_model": str(model_name),
+ **({"base_revision": revision} if revision else {}),
+ **_d1_temperatures(folder),
+ }
+ if full_finetuning:
+ config.update(_d1_temperatures(None))
+ return _clef_network(pipeline, folder, config, full_finetuning, gradient_checkpointing)
+
+
def _load_lm_as_clef(
model_name,
max_seq_length,
@@ -637,9 +729,10 @@ def _load_lm_as_clef(
head_config = None,
**kwargs,
):
- # A plain language model plus a new joint schema head, as unsloth/models/decision_from_lm.py builds one.
- if decision_head != "clef":
- raise ValueError(f"Unsloth: decision_head must be one of ('clef',), not {decision_head!r}.")
+ if decision_head not in ("clef", "d1"):
+ raise ValueError(
+ f"Unsloth: decision_head must be one of ('clef', 'd1'), not {decision_head!r}."
+ )
if kwargs:
raise NotImplementedError(
f"Unsloth: decision models on MLX do not support {', '.join(sorted(kwargs))}."
@@ -660,8 +753,22 @@ def _load_lm_as_clef(
f"Unsloth: {folder} holds LoRA adapters over a base model, which MLX does not load. "
"Merge them into the base model first."
)
+ zoo = _decision_zoo()
+ d1 = getattr(zoo, "FAMILIES", {}).get("d1")
+ if decision_head == "d1" or (d1 is not None and d1.matches(folder)):
+ return _load_lm_as_d1(
+ zoo,
+ folder,
+ model_name,
+ max_seq_length,
+ load_in_4bit,
+ full_finetuning,
+ token,
+ revision,
+ gradient_checkpointing,
+ )
load_in_4bit = bool(load_in_4bit) and not full_finetuning
- pipeline = _decision_zoo().load_language_model_as_clef(
+ pipeline = zoo.load_language_model_as_clef(
folder,
head_width = head_width,
head_config = head_config,
@@ -806,7 +913,7 @@ class FastDecisionModel:
if _is_clef(model):
if getattr(model, "_unsloth_lora", False):
raise RuntimeError("Unsloth: You already added LoRA adapters to your model!")
- network = _decision_zoo().clef_training_network(
+ network = _training_network(_decision_zoo(), model._unsloth_pipeline)(
model._unsloth_pipeline,
r = r,
lora_alpha = lora_alpha,
@@ -817,6 +924,8 @@ class FastDecisionModel:
gradient_checkpointing = bool(use_gradient_checkpointing),
)
attributes = {name: getattr(model, name) for name in _CLEF_ATTRIBUTES}
+ if _is_d1(model._unsloth_pipeline):
+ model.decision_config.update(_d1_temperatures(None))
return _decision_annotate(network, **attributes, _unsloth_lora = True)
_decision_zoo().add_lora_adapters(
model,
@@ -938,6 +1047,10 @@ class FastDecisionModel:
answers = {}
for (name, question), row, scale, options in zip(questions.items(), logits, scales, keys):
probabilities = dict(zip(options, torch.softmax(row / scale, -1).tolist()))
+ pipeline = getattr(model, "_unsloth_pipeline", None)
+ if _is_d1(pipeline):
+ answers[name] = _d1_predicted(zoo, pipeline, question, probabilities)
+ continue
answers[name] = _predicted(
question, systemone_answer(question, probabilities), probabilities
)
@@ -1058,7 +1171,7 @@ class DecisionTrainer:
)
if gradient_checkpointing and not clef:
model.gradient_checkpointing = True
- if clef and getattr(model, "_unsloth_lora", False):
+ if clef and getattr(model, "_unsloth_lora", False) and not _is_d1(model._unsloth_pipeline):
# Lets a checkpoint be written as the adapters save_pretrained writes.
config = model.decision_config
origin = (