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.

16.8 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Pydantic schemas for Export API."""

from pathlib import Path, PureWindowsPath

from pydantic import BaseModel, Field, field_validator
from typing import Annotated, List, Optional, Literal, Dict, Any, Union


def _validate_save_directory(value: str) -> str:
    """Validate save_directory — allows absolute paths (user may want a different drive)."""
    if value is None:
        raise ValueError("save_directory is required")
    raw = str(value).strip()
    if not raw:
        raise ValueError("save_directory must not be empty")
    if "\x00" in raw:
        raise ValueError("save_directory may not contain null bytes")
    if any(ch in raw for ch in ("\r", "\n")):
        raise ValueError("save_directory may not contain control characters")
    path = Path(raw).expanduser()
    path_parts = (*path.parts, *PureWindowsPath(raw).parts, *raw.replace("\\", "/").split("/"))
    if any(len(part) > 255 for part in path_parts if part not in ("", ".", "/", "\\")):
        raise ValueError("save_directory path components must be <= 255 characters")
    if (
        ".." in path.parts
        or ".." in PureWindowsPath(raw).parts
        or ".." in raw.replace("\\", "/").split("/")
    ):
        raise ValueError("save_directory may not contain '..' segments")
    return raw


class LoadCheckpointRequest(BaseModel):
    """Request for loading a checkpoint into the export backend."""

    checkpoint_path: str = Field(..., description = "Path to the checkpoint directory")
    max_seq_length: int = Field(
        2048,
        ge = 128,
        le = 32768,
        description = "Maximum sequence length for loading the model",
    )
    load_in_4bit: bool = Field(
        True,
        description = "Whether to load the model in 4-bit quantization",
    )
    offload_layers: Union[Literal["auto"], Annotated[int, Field(ge = 0, le = 1024)]] = Field(
        "auto",
        description = "Decoder layers kept in host RAM and streamed to the GPU during export "
        "(0 = off; 'auto' = only when a 4-bit load does not fit in GPU memory).",
    )
    trust_remote_code: bool = Field(
        False,
        description = "Allow loading models with custom code. Only enable for checkpoints/base models you trust.",
    )
    approved_remote_code_fingerprint: Optional[str] = Field(
        None,
        description = "sha256 fingerprint from the remote-code scan, pinning user approval of this exact custom-code version.",
    )
    hf_token: Optional[str] = Field(
        None,
        description = "Hugging Face token used to scan/load gated checkpoints and their base models.",
    )
    op_id: Optional[str] = Field(
        None,
        max_length = 128,
        description = "Caller's id for this operation; POST /api/export/cancel with it as expected_op_id stops only it.",
    )


class ExportCancelRequest(BaseModel):
    expected_op_id: Optional[str] = Field(
        None,
        max_length = 128,
        description = "Stop only the operation started with this op_id; one not started yet is refused when it starts.",
    )


class ExportStatusResponse(BaseModel):
    """Current export backend status."""

    current_checkpoint: Optional[str] = Field(
        None,
        description = "Path to the currently loaded checkpoint, if any",
    )
    is_vision: bool = Field(
        False,
        description = "True if the loaded checkpoint is a vision model",
    )
    is_peft: bool = Field(
        False,
        description = "True if the loaded checkpoint is a PEFT (LoRA) model",
    )
    is_export_active: bool = Field(
        False,
        description = "True while a load / export / cleanup operation is running",
    )
    # Polled after a Cloudflare 524 cuts off a blocking export POST.
    active_op_kind: Optional[str] = Field(
        None,
        description = "Kind of the currently running op (load_checkpoint / export_* / cleanup)",
    )
    last_op_seq: int = Field(
        0,
        description = "Monotonic counter of finished ops; client baseline to detect 'my op finished'",
    )
    last_op_kind: Optional[str] = Field(
        None,
        description = "Kind of the most recently finished op",
    )
    last_op_status: Optional[str] = Field(
        None,
        description = "Outcome of the most recently finished op: success / error / cancelled",
    )
    last_op_output_path: Optional[str] = Field(
        None,
        description = "Output path of the most recently finished op, if it produced one",
    )
    last_op_error: Optional[str] = Field(
        None,
        description = "Error message of the most recently finished op, if it failed",
    )
    decision: Optional[Dict[str, Any]] = Field(
        None,
        description = "{layout, adapter_only} when the loaded checkpoint is a decision model",
    )


class DecisionExportInfo(BaseModel):
    """GGUF export options for a decision model (Clef or Laya) checkpoint."""

    is_decision: bool = Field(True, description = "True for a decision model checkpoint")
    layout: Literal["clef", "laya"] = Field(..., description = "Decision checkpoint layout")
    adapter_only: bool = Field(
        False,
        description = "True for a Clef folder holding LoRA adapters only (merged at export time)",
    )
    eligible: Optional[bool] = Field(
        None,
        description = "False when llama.cpp cannot serve this model; None when only the export can tell",
    )
    reason: Optional[str] = Field(None, description = "Why the checkpoint is not eligible")
    quantizations: List[str] = Field(..., description = "Allowed GGUF quantizations, default first")
    default_quantization: str = Field(..., description = "Default GGUF quantization")
    output_dir: str = Field(..., description = "Where the GGUF files are written (<run folder>/gguf)")
    existing_export: Optional[Dict[str, Any]] = Field(
        None,
        description = "Content of an earlier gguf/export.json, if valid",
    )


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):
    """Generic response for export operations."""

    success: bool = Field(..., description = "True if the operation succeeded")
    message: str = Field(..., description = "Human-readable status or error message")
    details: Optional[Dict[str, Any]] = Field(
        default = None,
        description = "Optional extra details about the operation",
    )


class ReapOptions(BaseModel):
    """Opt-in REAP expert pruning (arXiv:2510.13999) applied to an MoE model before it is exported."""

    sparsity: float = Field(
        0.25,
        gt = 0,
        lt = 1,
        description = "Fraction of the routed experts to remove in every MoE layer.",
    )
    num_calibration_samples: int = Field(
        512,
        ge = 8,
        le = 4096,
        description = "Calibration conversations used to score the experts.",
    )
    dataset: Optional[str] = Field(
        None,
        max_length = 200,
        pattern = r"^[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)?$",
        description = "Hugging Face dataset id for calibration (default mlabonne/open-perfectblend).",
    )


class MoeInfoResponse(BaseModel):
    """Routed-expert counts for a model, or None when it is not a routed MoE."""

    model: str
    num_experts: Optional[int] = None
    top_k: Optional[int] = None
    n_group: Optional[int] = Field(None, description = "Router groups (group-limited routing).")
    topk_group: Optional[int] = Field(None, description = "Groups each token may route into.")


class ExportCommonOptions(BaseModel):
    """Common options for export operations that save locally and/or push to Hub."""

    save_directory: str = Field(
        ...,
        description = "Local directory where the exported artifacts will be written",
    )

    @field_validator("save_directory", mode = "before")
    @classmethod
    def _check_save_directory(cls, v):
        return _validate_save_directory(v)

    push_to_hub: bool = Field(
        False,
        description = "If True, also push the exported model to the Hugging Face Hub",
    )
    repo_id: Optional[str] = Field(
        None,
        description = "Hugging Face Hub repository ID (username/model-name)",
    )
    hf_token: Optional[str] = Field(
        None,
        description = "Hugging Face access token used for Hub operations",
    )
    private: bool = Field(
        False,
        description = "If True, create a private repository on the Hub (where applicable)",
    )
    base_model_id: Optional[str] = Field(
        None,
        description = "HuggingFace model ID of the base model (for model card metadata)",
    )
    expected_checkpoint: Optional[str] = Field(
        None,
        max_length = 4096,
        description = "Export only while this checkpoint is still the loaded one. A client that loads and then "
        "exports in two calls sends it so another load in between is refused instead of exported.",
    )
    op_id: Optional[str] = Field(
        None,
        max_length = 128,
        description = "Caller's id for this operation; POST /api/export/cancel with it as expected_op_id stops only it.",
    )


class ExportMergedModelRequest(ExportCommonOptions):
    """Request for exporting a merged PEFT model."""

    format_type: Literal[
        "16-bit (FP16)",
        "4-bit (FP4)",
        "FP8 (compressed-tensors)",
        "NVFP4 (compressed-tensors)",
    ] = Field(
        "16-bit (FP16)",
        description = "Export precision / format for the merged model. The compressed-tensors "
        "options run llm-compressor for vLLM (FP8 is data-free; NVFP4 calibrates).",
    )
    compressed_method: Optional[str] = Field(
        None,
        description = "Optional quantized-export alias. Either a compressed-tensors scheme "
        "(e.g. 'fp8', 'fp8_static', 'w8a8', 'w4a16', 'mxfp4', 'mxfp8', 'nvfp4' - NVIDIA only) "
        "from unsloth.save COMPRESSED_EXPORT_SCHEMES, or a portable torchao alias "
        "('torchao_fp8', 'torchao_int8') from TORCHAO_EXPORT_SCHEMES that needs no NVIDIA GPU. "
        "When set, it overrides format_type. Lets the export UI expose the full set of formats "
        "beyond the quick buttons.",
    )
    install_missing_dependencies: bool = Field(
        False,
        description = "User consent to install llm-compressor (or its shadow runtime) for "
        "compressed-tensors export.",
    )
    reap: Optional[ReapOptions] = Field(
        None, description = "Prune MoE experts with REAP before exporting (off when omitted)."
    )


class ExportBaseModelRequest(ExportCommonOptions):
    """Request for exporting a non-PEFT (base) model."""

    reap: Optional[ReapOptions] = Field(
        None, description = "Prune MoE experts with REAP before exporting (off when omitted)."
    )


class ExportGGUFRequest(BaseModel):
    """Request for exporting the current model to GGUF format."""

    expected_checkpoint: Optional[str] = Field(
        None,
        max_length = 4096,
        description = "Export only while this checkpoint is still the loaded one. A client that loads and then "
        "exports in two calls sends it so another load in between is refused instead of exported.",
    )
    op_id: Optional[str] = Field(
        None,
        max_length = 128,
        description = "Caller's id for this operation; POST /api/export/cancel with it as expected_op_id stops only it.",
    )

    save_directory: str = Field(
        ...,
        description = "Directory where GGUF files will be saved",
    )

    @field_validator("save_directory", mode = "before")
    @classmethod
    def _check_save_directory(cls, v):
        return _validate_save_directory(v)

    quantization_method: Union[str, List[str]] = Field(
        "Q4_K_M",
        description = 'GGUF quantization method(s). A single method (e.g. "Q4_K_M") or a list '
        '(e.g. ["Q4_K_M", "Q8_0"]) to produce multiple GGUFs from one model load.',
    )
    push_to_hub: bool = Field(
        False,
        description = "If True, also push GGUF artifacts to the Hugging Face Hub",
    )
    repo_id: Optional[str] = Field(
        None,
        description = "Hugging Face Hub repository ID for GGUF upload",
    )
    hf_token: Optional[str] = Field(
        None,
        description = "Hugging Face token for GGUF upload",
    )
    imatrix: bool = Field(
        False,
        description = "Use an importance matrix (auto-downloads the upstream unsloth GGUF "
        "imatrix). Required for the IQ low-bit quants such as iq2_xxs / iq4_xs.",
    )
    imatrix_path: Optional[str] = Field(
        None,
        description = "Path to a custom imatrix file; overrides the auto-download when set.",
    )
    private: bool = Field(
        False,
        description = "If True, create a private Hugging Face Hub repository",
    )
    npu_q4nx: bool = Field(
        False,
        description = "Also convert one Q4_0 / Q4_1 / Q4_K_M GGUF to FastFlowLM's Q4NX for the "
        "AMD Ryzen AI NPU, written to <save_directory>/npu-q4nx.",
    )
    reap: Optional[ReapOptions] = Field(
        None, description = "Prune MoE experts with REAP before converting (off when omitted)."
    )


class ConvertQ4NXRequest(BaseModel):
    """Convert a GGUF that already exists to FastFlowLM Q4NX, without loading a model."""

    save_directory: str = Field(..., description = "Directory the Q4NX folder is written into")

    @field_validator("save_directory", mode = "before")
    @classmethod
    def _check_save_directory(cls, v):
        return _validate_save_directory(v)

    gguf_path: Optional[str] = Field(None, description = "A local .gguf file")
    repo_id: Optional[str] = Field(None, description = "Hub repo holding the GGUF, with filename")
    filename: Optional[str] = Field(None, description = "GGUF file in repo_id")
    base_model: str = Field(
        ...,
        description = "The original (non-GGUF) Hub repo or local model folder; FastFlowLM needs "
        "its config.json and tokenizer files.",
    )
    hf_token: Optional[str] = Field(None, description = "Hugging Face token for gated repos")


class ExportDiffusionLoRARequest(BaseModel):
    """Copy an image-generation LoRA from the Images catalog into an export folder."""

    lora_id: str = Field(
        ..., description = "Local adapter id as listed by /api/models/diffusion-loras"
    )
    save_directory: str = Field(..., description = "Directory the adapter is copied into")

    @field_validator("save_directory", mode = "before")
    @classmethod
    def _check_save_directory(cls, v):
        return _validate_save_directory(v)


class ExportLoRAAdapterRequest(ExportCommonOptions):
    """Request for exporting only the LoRA adapter (not merged)."""

    adapter_format: Optional[Literal["mlx", "peft"]] = Field(
        None,
        description = "On-disk adapter format. Omitted resolves per platform "
        "(Apple-silicon MLX servers write the native MLX format, CUDA servers "
        "write the native PEFT format — omission always preserves the "
        "platform's native output), except with gguf=True, where the adapter "
        "files are always "
        "PEFT (GGUF LoRA files are built from that format). Explicit 'peft' "
        "on an MLX server converts the adapter; explicit 'mlx' on a non-MLX "
        "server — or combined with gguf=True — is an error.",
    )
    gguf: bool = Field(
        False,
        description = "If True, also convert the adapter to a GGUF LoRA file "
        "(llama.cpp convert_lora_to_gguf.py), loadable with `llama-cli --lora ...`.",
    )
    gguf_outtype: Literal["q8_0", "f16", "bf16", "f32"] = Field(
        "q8_0",
        description = "GGUF LoRA output float type (only used when gguf=True). "
        "Q8_0 falls back to F16 per tensor for dims not divisible by the block size (32).",
    )


class LlmCompressorExportProbeResponse(BaseModel):
    ready: bool
    needs_consent: bool
    consent_kind: Optional[Literal["shadow", "workspace"]] = None
    install_summary: Optional[str] = None
    workspace_install_command: str
    shadow_path: str
    autoinstall_disabled: bool
    shadow_disabled: bool
    offline: bool
    blocked_reason: Optional[str] = None
    python_executable: str
    has_pip: bool
    has_uv: bool