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
// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
import {
isHfCacheSnapshotPath,
isOllamaLinkPath,
isStandaloneGgufPath,
normalizeGgufVariantIdentity,
normalizeModelIdentity,
publicModelId,
} from "@/features/hub/lib/model-identity";
import { looksLikeLocalPath } from "@/lib/local-path";
// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
export {
isNativeFileLabel,
isOllamaLinkPath,
isOllamaModelId,
isStandaloneGgufPath,
ggufVariantsMatch,
modelDisplayName,
normalizeGgufVariantIdentity,
normalizeModelIdentity,
publicModelId,
residentModelIdMatches,
} from "@/features/hub/lib/model-identity";
const MODEL_STORAGE_KEY_PREFIX = "v2:";
type ParsedModelStorageKey = {
modelId: string;
ggufVariant: string;
};
function parseVersionedModelStorageKey(
key: string,
): ParsedModelStorageKey | null {
if (!key.startsWith(MODEL_STORAGE_KEY_PREFIX)) {
return null;
}
try {
const parsed = JSON.parse(key.slice(MODEL_STORAGE_KEY_PREFIX.length));
if (
!Array.isArray(parsed) ||
parsed.length !== 2 ||
typeof parsed[0] !== "string" ||
typeof parsed[1] !== "string"
) {
return null;
}
return { modelId: parsed[0], ggufVariant: parsed[1] };
} catch {
return null;
}
}
/** False when the API auto-switch can never load it (bare LoRA, materialized Ollama link). */
export function apiAutoSwitchMayLoad(
ids: readonly string[],
isLora: boolean,
): boolean {
return !isLora && !ids.some(isOllamaLinkPath);
}
/** /status reports the snapshot path while the picker keys the repo id, and the backend folds the
* two only for a quant, so a bare snapshot-path row would survive Forget. */
export function cachedRepoConfigId(
modelId: string,
ggufVariant: string | null | undefined,
): string | null {
return !ggufVariant &&
!isStandaloneGgufPath(modelId) &&
isHfCacheSnapshotPath(modelId)
? publicModelId(modelId)
: null;
}
export function modelStorageKey(
modelId: string,
ggufVariant?: string | null,
): string {
return `${MODEL_STORAGE_KEY_PREFIX}${JSON.stringify([
normalizeModelIdentity(modelId),
normalizeGgufVariantIdentity(ggufVariant),
])}`;
}
export function modelIdFromStorageKey(key: string): string | null {
const parsed = parseVersionedModelStorageKey(key);
if (parsed) {
return parsed.modelId;
}
const separator = key.lastIndexOf("::");
return separator >= 0 ? key.slice(0, separator) : null;
}
export function ggufVariantFromStorageKey(key: string): string | null {
const parsed = parseVersionedModelStorageKey(key);
if (parsed) {
return parsed.ggufVariant;
}
const separator = key.lastIndexOf("::");
return separator >= 0 ? key.slice(separator + 2) : null;
}
// Mirrors split_quant_suffix in openai_auto_switch_settings.py; the bpw modifier is optional.
const BPW_SUFFIX = /-[0-9]+(?:\.[0-9]+)?bpw$/i;
// One source for the anchored test and the scan below. Mirrors GGUF_QUANT_RE in utils/gguf_names.py.
const QUANT_TOKEN_SOURCE =
"(UD-)?(MXFP[0-9]+(?:_[A-Z0-9]+)*|NVFP[0-9]+(?:_[A-Z0-9]+)*(?=(?:-[0-9]+(?:[.][0-9]+)?bpw)?(?:-[0-9]{3,}-of-[0-9]{3,})?(?:[.]gguf)?$)|IQ[0-9]+_[A-Z]+(?:_[A-Z0-9]+)?|P?TQ[0-9]+_[0-9]+|Q[0-9]+_K_[A-Z]+|P?Q[0-9]+_(?:[0-9]+(?:_G[0-9]+)?|G[0-9]+)|Q[0-9]+_K|BF16|F16|F32)";
const KNOWN_QUANT = new RegExp(`^${QUANT_TOKEN_SOURCE}$`, "i");
const QUANT_TOKEN = new RegExp(QUANT_TOKEN_SOURCE, "gi");
const MAX_QUANT_SUFFIX_LEN = 64;
// Mirrors _GGUF_SPLIT_SUFFIX_RE in studio/backend/hub/utils/gguf.py.
const GGUF_SPLIT_SUFFIX = /-[0-9]{3,}-of-[0-9]{3,}/gi;
const BACKSLASHES = /\\/g;
// A float precision labels a file only when nothing sharper does, as _select_quant_match.
const FLOAT_PRECISION_QUANTS: ReadonlySet<string> = new Set([
"BF16",
"F16",
"F32",
]);
/** Mirrors _gguf_stem in studio/backend/hub/utils/gguf.py, for a bare filename. */
function ggufStem(filename: string): string {
const dot = filename.lastIndexOf(".");
const withoutExtension = dot >= 0 ? filename.slice(0, dot) : filename;
return withoutExtension.replace(GGUF_SPLIT_SUFFIX, "").trim();
}
/** Mirrors extract_quant_label in gguf.py, for a bare filename. */
export function ggufQuantLabel(filename: string): string {
const stem = ggufStem(filename);
let fallback: RegExpExecArray | null = null;
for (const match of stem.matchAll(QUANT_TOKEN)) {
if (FLOAT_PRECISION_QUANTS.has(match[2].toUpperCase())) {
fallback ??= match;
continue;
}
return `${match[1] ?? ""}${match[2]}`;
}
if (fallback) {
return `${fallback[1] ?? ""}${fallback[2]}`;
}
return stem || "gguf";
}
/** The suffix must look like a real quant, so POSIX colons and drive letters are left alone. */
export function splitQuantSuffix(value: string): [string, string] | null {
const separator = value.lastIndexOf(":");
if (separator <= 0 || separator === value.length - 1) {
return null;
}
const head = value.slice(0, separator);
const tail = value.slice(separator + 1);
if (tail.includes("/") || tail.includes("\\")) {
return null;
}
if (
tail.length <= MAX_QUANT_SUFFIX_LEN &&
KNOWN_QUANT.test(tail.replace(BPW_SUFFIX, ""))
) {
return [head, tail];
}
if (!head.toLowerCase().endsWith(".gguf")) {
return null;
}
// A colon is legal in a POSIX filename, so only the exact label counts as a variant.
const filename = head.replace(BACKSLASHES, "/").split("/").pop() ?? head;
return tail.toLowerCase() === ggufQuantLabel(filename).toLowerCase()
? [head, tail]
: null;
}
/**
* `[modelId, variant]` for an override key, or `[value, null]`. Non-path ids split on the last
* colon; paths stay whole since a colon is legal in a POSIX filename, as the backend resolver does.
*/
export function splitModelOverrideKey(value: string): [string, string | null] {
const quant = splitQuantSuffix(value);
if (quant) {
return quant;
}
const separator = value.lastIndexOf(":");
if (
separator <= 0 ||
separator === value.length - 1 ||
looksLikeLocalPath(value)
) {
return [value, null];
}
return [value.slice(0, separator), value.slice(separator + 1)];
}