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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// 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 {
applyPerModelConfigToRuntime,
currentRuntimePerModelConfig,
perModelConfigsEqual,
} from "@/features/model-picker/model-config/apply-per-model-config";
import {
CONTEXT_LENGTH_MIN,
DEFAULT_PER_MODEL_CONFIG,
MAX_SEQ_LENGTH_MAX,
DEFAULT_MAX_SEQ_LENGTH,
KV_CACHE_DTYPES,
mlxKvQuantLabel,
normalizeMlxKvQuant,
N_BATCH_MAX,
N_BATCH_MIN,
N_PARALLEL_MAX,
N_PARALLEL_MIN,
isReasoningBudgetMessageValid,
canonicalizeLoadMode,
isServedByLlamaCpp,
isServedByMlx,
normalizeCacheRam,
normalizeCtxCheckpoints,
normalizeMaxSeqLength,
savedContextPin,
type PerModelConfig,
} from "@/features/model-picker/model-config/per-model-config";
import {
DRAFT_N_MAX_SPEC_TYPES,
SPECULATIVE_TYPES,
} from "@/lib/speculative-modes";
import {
GPU_LAYERS_AUTO,
useChatRuntimeStore,
normalizeSpeculativeType,
} from "../stores/chat-runtime-store";
import { usePlatformStore } from "@/config/env";
import { capturedContextLength } from "./preset-policy";
export type PresetLoadConfig = Pick<
PerModelConfig,
| "customContextLength"
| "maxSeqLength"
| "kvCacheDtype"
| "mlxKvQuant"
| "speculativeType"
| "specDraftNMax"
| "nParallel"
| "reasoningBudget"
| "reasoningBudgetMessage"
| "nBatch"
| "nUbatch"
| "loadMode"
| "specDraftCacheDtype"
| "ctxCheckpoints"
| "cacheRam"
| "tensorParallel"
| "disableVision"
| "gpuMemoryMode"
| "gpuLayers"
| "nCpuMoe"
>;
const VALID_KV_CACHE_DTYPES = new Set<string>(KV_CACHE_DTYPES);
const VALID_SPECULATIVE_TYPES = new Set<string>(SPECULATIVE_TYPES);
export const EMPTY_PRESET_LOAD_CONFIG: PresetLoadConfig = {
customContextLength: null,
maxSeqLength: null,
kvCacheDtype: null,
mlxKvQuant: null,
speculativeType: null,
specDraftNMax: null,
nParallel: null,
reasoningBudget: -1,
reasoningBudgetMessage: "",
nBatch: null,
nUbatch: null,
loadMode: null,
specDraftCacheDtype: null,
ctxCheckpoints: null,
cacheRam: null,
tensorParallel: false,
disableVision: false,
};
function toComparablePerModelConfig(
config: PresetLoadConfig,
): PerModelConfig {
// Compared as a pin: the same preset elsewhere stores the length in the other field.
const pin = savedContextPin(config);
return {
...DEFAULT_PER_MODEL_CONFIG,
...config,
customContextLength: pin,
maxSeqLength: null,
chatTemplateOverride: null,
selectedGpuIds: null,
};
}
/** One bound for capture and read-back so replays agree; min is the control's own minimum. */
function requestableContextLength(value: unknown): number | null {
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
return null;
}
return Math.min(MAX_SEQ_LENGTH_MAX, Math.max(CONTEXT_LENGTH_MIN, Math.floor(value)));
}
export function normalizePresetLoadConfig(
raw: unknown,
): PresetLoadConfig | undefined {
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
return undefined;
}
const partial = raw as Record<string, unknown>;
const rawSpecType =
typeof partial.speculativeType === "string"
? normalizeSpeculativeType(partial.speculativeType)
: null;
const speculativeType = rawSpecType ?? null;
const specDraftNMax =
speculativeType != null &&
DRAFT_N_MAX_SPEC_TYPES.has(speculativeType) &&
typeof partial.specDraftNMax === "number" &&
Number.isFinite(partial.specDraftNMax)
? Math.max(1, Math.min(16, Math.round(partial.specDraftNMax)))
: null;
const gpuMemoryMode =
partial.gpuMemoryMode === "manual" ? ("manual" as const) : undefined;
let gpuLayers: number | undefined;
if (typeof partial.gpuLayers === "number" && Number.isFinite(partial.gpuLayers)) {
gpuLayers = partial.gpuLayers < 0 ? GPU_LAYERS_AUTO : Math.floor(partial.gpuLayers);
}
let nCpuMoe: number | undefined;
if (typeof partial.nCpuMoe === "number" && Number.isFinite(partial.nCpuMoe)) {
nCpuMoe = Math.max(0, Math.floor(partial.nCpuMoe));
}
const normalized: PresetLoadConfig = {
customContextLength: requestableContextLength(partial.customContextLength),
maxSeqLength: normalizeMaxSeqLength(partial.maxSeqLength as number | null),
mlxKvQuant: normalizeMlxKvQuant(partial.mlxKvQuant, partial.mlxKvBits),
kvCacheDtype:
typeof partial.kvCacheDtype === "string" &&
VALID_KV_CACHE_DTYPES.has(partial.kvCacheDtype)
? partial.kvCacheDtype
: null,
speculativeType:
speculativeType && VALID_SPECULATIVE_TYPES.has(speculativeType)
? speculativeType
: null,
specDraftNMax,
nParallel:
typeof partial.nParallel === "number" &&
Number.isFinite(partial.nParallel)
? Math.max(
N_PARALLEL_MIN,
Math.min(N_PARALLEL_MAX, Math.round(partial.nParallel)),
)
: null,
reasoningBudget:
typeof partial.reasoningBudget === "number" &&
Number.isFinite(partial.reasoningBudget)
? Math.max(
-1,
Math.min(2_147_483_647, Math.trunc(partial.reasoningBudget)),
)
: -1,
reasoningBudgetMessage:
typeof partial.reasoningBudgetMessage === "string" &&
isReasoningBudgetMessageValid(partial.reasoningBudgetMessage)
? partial.reasoningBudgetMessage
: "",
nBatch:
typeof partial.nBatch === "number" && Number.isFinite(partial.nBatch)
? Math.max(N_BATCH_MIN, Math.min(N_BATCH_MAX, Math.round(partial.nBatch)))
: null,
nUbatch:
typeof partial.nUbatch === "number" && Number.isFinite(partial.nUbatch)
? Math.max(N_BATCH_MIN, Math.min(N_BATCH_MAX, Math.round(partial.nUbatch)))
: null,
// Normalized so old or hand-edited presets cannot carry values the panel cannot show.
loadMode: canonicalizeLoadMode(partial.loadMode),
specDraftCacheDtype:
typeof partial.specDraftCacheDtype === "string" &&
VALID_KV_CACHE_DTYPES.has(partial.specDraftCacheDtype)
? partial.specDraftCacheDtype
: null,
ctxCheckpoints: normalizeCtxCheckpoints(partial.ctxCheckpoints),
cacheRam: normalizeCacheRam(partial.cacheRam),
tensorParallel:
typeof partial.tensorParallel === "boolean"
? partial.tensorParallel
: false,
disableVision:
typeof partial.disableVision === "boolean"
? partial.disableVision
: false,
...(gpuMemoryMode ? { gpuMemoryMode } : {}),
...(gpuLayers !== undefined ? { gpuLayers } : {}),
...(nCpuMoe !== undefined ? { nCpuMoe } : {}),
};
const coalesced = coalesceDefaultLoadKnobs(normalized);
return hasPresetLoadConfig(coalesced) ? coalesced : undefined;
}
export function hasPresetLoadConfig(
config?: PresetLoadConfig | null,
): boolean {
return !isSamePresetLoadConfig(config, EMPTY_PRESET_LOAD_CONFIG);
}
export function isSamePresetLoadConfig(
a?: PresetLoadConfig | null,
b?: PresetLoadConfig | null,
): boolean {
return perModelConfigsEqual(
toComparablePerModelConfig({ ...EMPTY_PRESET_LOAD_CONFIG, ...a }),
toComparablePerModelConfig({ ...EMPTY_PRESET_LOAD_CONFIG, ...b }),
);
}
export function capturePresetLoadConfig(): PresetLoadConfig | undefined {
const snapshot = currentRuntimePerModelConfig({ includeMaxSeqLength: true });
const store = useChatRuntimeStore.getState();
const isGguf = isServedByLlamaCpp({
loadedIsGguf: store.loadedIsGguf,
activeGgufVariant: store.activeGgufVariant,
activeNativePathToken: store.activeNativePathToken,
checkpoint: store.params.checkpoint,
});
const platform = usePlatformStore.getState();
const isMlx = isServedByMlx(isGguf, platform.deviceType, platform.chatOnlyReason);
const effectiveContextLength = requestableContextLength(
capturedContextLength({
isGguf,
controlPin: snapshot.customContextLength,
loadedContextLength: store.loadedContextLength,
}),
);
// Diffusion GGUFs capture context too; only reasoning flags are suppressed.
const capturesReasoning = isGguf && !store.loadedIsDiffusion;
const captured: PresetLoadConfig = {
customContextLength: effectiveContextLength ?? null,
maxSeqLength: isMlx ? null : normalizeMaxSeqLength(snapshot.maxSeqLength),
kvCacheDtype: snapshot.kvCacheDtype ?? null,
mlxKvQuant: snapshot.mlxKvQuant ?? null,
speculativeType: normalizeSpeculativeType(snapshot.speculativeType),
specDraftNMax: snapshot.specDraftNMax ?? null,
nParallel: snapshot.nParallel ?? null,
reasoningBudget: capturesReasoning ? snapshot.reasoningBudget : -1,
reasoningBudgetMessage: capturesReasoning
? snapshot.reasoningBudgetMessage
: "",
nBatch: snapshot.nBatch ?? null,
nUbatch: snapshot.nUbatch ?? null,
loadMode: snapshot.loadMode ?? null,
specDraftCacheDtype: snapshot.specDraftCacheDtype ?? null,
ctxCheckpoints: snapshot.ctxCheckpoints ?? null,
cacheRam: snapshot.cacheRam ?? null,
tensorParallel: snapshot.tensorParallel ?? false,
disableVision: snapshot.disableVision ?? false,
...(snapshot.gpuMemoryMode === "manual"
? { gpuMemoryMode: "manual" as const }
: {}),
...(snapshot.gpuLayers != null && snapshot.gpuLayers >= 0
? { gpuLayers: snapshot.gpuLayers }
: snapshot.gpuMemoryMode === "manual"
? { gpuLayers: GPU_LAYERS_AUTO }
: {}),
...(snapshot.nCpuMoe != null && snapshot.nCpuMoe > 0
? { nCpuMoe: snapshot.nCpuMoe }
: {}),
};
const coalesced = coalesceDefaultLoadKnobs(captured);
return hasPresetLoadConfig(coalesced) ? coalesced : undefined;
}
function coalesceDefaultLoadKnobs(
captured: PresetLoadConfig,
): PresetLoadConfig {
const result: PresetLoadConfig = { ...captured };
if (normalizeMaxSeqLength(result.maxSeqLength) === DEFAULT_MAX_SEQ_LENGTH) {
result.maxSeqLength = null;
}
const speculativeType = normalizeSpeculativeType(result.speculativeType);
if (speculativeType == null || speculativeType === "auto") {
result.speculativeType = null;
}
if (
(result.gpuLayers == null || result.gpuLayers < 0) &&
result.gpuMemoryMode !== "manual"
) {
delete result.gpuLayers;
}
if ((result.nCpuMoe ?? 0) === 0) {
delete result.nCpuMoe;
}
return result;
}
export function applyPresetLoadConfig(config?: PresetLoadConfig | null): void {
if (config == null) {
return;
}
const store = useChatRuntimeStore.getState();
applyPerModelConfigToRuntime(
{
...DEFAULT_PER_MODEL_CONFIG,
maxSeqLength: normalizeMaxSeqLength(config.maxSeqLength) ?? DEFAULT_MAX_SEQ_LENGTH,
customContextLength: config.customContextLength ?? null,
kvCacheDtype: config.kvCacheDtype ?? null,
mlxKvQuant: config.mlxKvQuant ?? null,
speculativeType: config.speculativeType ?? null,
specDraftNMax: config.specDraftNMax ?? null,
nParallel: config.nParallel ?? null,
nBatch: config.nBatch ?? null,
nUbatch: config.nUbatch ?? null,
loadMode: config.loadMode ?? null,
specDraftCacheDtype: config.specDraftCacheDtype ?? null,
ctxCheckpoints: config.ctxCheckpoints ?? null,
cacheRam: config.cacheRam ?? null,
reasoningBudget: config.reasoningBudget ?? -1,
reasoningBudgetMessage: config.reasoningBudgetMessage ?? "",
tensorParallel: config.tensorParallel ?? false,
disableVision: config.disableVision ?? false,
chatTemplateOverride: null,
// Not carried by presets: enabling it always goes through its confirmation.
mlxInt8Prefill: store.mlxInt8Prefill,
mlxAnePrefill: store.mlxAnePrefill,
mlxFastVerify: store.mlxFastVerify,
mlxPleOffload: store.mlxPleOffload,
mlxExpertOffload: store.mlxExpertOffload,
gpuMemoryMode: config.gpuMemoryMode,
gpuLayers: config.gpuLayers,
nCpuMoe: config.nCpuMoe,
selectedGpuIds: store.selectedGpuIds,
selectedGpuIndexKind: store.selectedGpuIndexKind,
},
{ isDiffusion: store.loadedIsDiffusion },
);
}
export function formatPresetLoadConfigSummary(
config?: PresetLoadConfig | null,
): string | null {
if (!config || !hasPresetLoadConfig(config)) {
return null;
}
const parts: string[] = [];
if (config.customContextLength != null) {
parts.push(`Ctx ${config.customContextLength.toLocaleString()}`);
}
if (config.kvCacheDtype) {
parts.push(`KV ${config.kvCacheDtype}`);
}
if (config.mlxKvQuant) {
parts.push(`MLX KV ${mlxKvQuantLabel(config.mlxKvQuant)}`);
}
if (config.speculativeType && config.speculativeType !== "auto") {
parts.push(`Spec ${config.speculativeType}`);
}
if (config.nParallel != null) {
parts.push(`${config.nParallel} slots`);
}
if (config.reasoningBudget !== -1) {
parts.push(`Reasoning ${config.reasoningBudget}`);
}
// A marker, not the text, or a message-only preset summarises to null.
if (config.reasoningBudgetMessage) {
parts.push("Budget msg");
}
if (config.nBatch != null) {
parts.push(`Batch ${config.nBatch}`);
}
if (config.nUbatch != null) {
parts.push(`uBatch ${config.nUbatch}`);
}
if (config.loadMode) {
parts.push(`Load ${config.loadMode}`);
}
if (config.specDraftCacheDtype) {
parts.push(`Draft KV ${config.specDraftCacheDtype}`);
}
if (config.ctxCheckpoints != null) {
parts.push(`${config.ctxCheckpoints} checkpoints`);
}
if (config.cacheRam != null) {
parts.push(`Cache RAM ${config.cacheRam}`);
}
if (config.gpuMemoryMode === "manual") {
parts.push("GPU manual");
}
if (config.gpuLayers != null && config.gpuLayers >= 0) {
parts.push(`${config.gpuLayers} layers`);
}
if (config.tensorParallel) {
parts.push("TP");
}
if (config.disableVision) {
parts.push("No vision");
}
return parts.length > 0 ? parts.join(" · ") : null;
}