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.
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { useIsAccountOwner } from "@/features/auth";
import { SettingsRow } from "@/features/settings/components/settings-row";
import { SettingsSection } from "@/features/settings/components/settings-section";
import { useT } from "@/i18n";
import { isEngineGpuDevice, useGpuDevices } from "@/hooks/use-gpu-info";
import { formatGiB } from "@/lib/memory/format";
import { cn } from "@/lib/utils";
import { useEffect, useRef, useState } from "react";
import {
type EngineStatus,
type InferenceEngine,
changeEngine,
precisionAfterEngineSwitch,
isEngineReady,
supportsPrecision,
wslNoticeKey,
} from "../api/engines";
import { useEngines } from "../hooks/use-engines";
const names = { vllm: "vLLM", sglang: "SGLang" };
const PRECISION_LABELS = [
["bf16", "BF16 (16-bit)"],
["fp16", "FP16 (16-bit)"],
["int4", "4-bit"],
["int8", "INT8 (8-bit)"],
["fp8", "FP8 (8-bit)"],
] as const;
// Ceilings below Studio's measured budget; it never raises the engine past what is free.
const GPU_MEMORY_LIMITS = [0.95, 0.9, 0.8, 0.7, 0.5, 0.3] as const;
export function EngineInstall({
engine,
management = false,
centered = false,
onUse,
}: {
engine: EngineStatus;
management?: boolean;
/** The load dialog: centred, and the install prompt (size, Install and load) shown up front. */
centered?: boolean;
onUse?: () => void;
}) {
const t = useT();
const isOwner = useIsAccountOwner();
const useAfterInstall = useRef(false);
// Off once an install starts, until it fails or stops: the next poll may not show it running yet.
const [autoConfirm, setAutoConfirm] = useState(true);
useEffect(() => {
if (
engine.job.state === "error" ||
engine.job.state === "cancelled" ||
engine.job.state === "waiting"
) {
useAfterInstall.current = false;
setAutoConfirm(true);
}
if (
useAfterInstall.current &&
engine.installed &&
engine.job.state === "success"
) {
useAfterInstall.current = false;
onUse?.();
}
}, [engine.installed, engine.job.state, onUse]);
const [confirm, setConfirm] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const running = engine.job.state === "running";
const canInstall = !engine.in_use && isOwner && !engine.unsupported_reason;
// The dialog's user already asked to load, so skip the extra "Install engine" click there.
const showConfirm =
confirm ||
(centered && autoConfirm && canInstall && !engine.installed && !running);
const action = async (
operation: "install" | "cancel" | "remove" | "rollback",
) => {
setBusy(true);
setError("");
try {
if (operation === "install") {
useAfterInstall.current = !!onUse;
setAutoConfirm(false);
}
if (operation === "cancel") {
useAfterInstall.current = false;
}
await changeEngine(engine.engine, operation);
setConfirm(false);
} catch (err) {
useAfterInstall.current = false;
setAutoConfirm(true);
setError(err instanceof Error ? err.message : "Engine operation failed");
} finally {
setBusy(false);
}
};
return (
<div className={cn("space-y-2 text-ui-12", centered && "text-center")}>
{/* Settings shows these under the engine name (engineNote). */}
{!management && !(isOwner || engine.installed) && (
<p className="text-muted-foreground">
{t("managedEngines.ownerRequired")}
</p>
)}
{!management && engine.unsupported_reason && (
<p className="text-muted-foreground">{engine.unsupported_reason}</p>
)}
{running ? (
<output
aria-live="polite"
className={cn(
"flex items-center gap-3",
centered ? "justify-center" : "justify-between",
)}
>
<span>{t("managedEngines.installing")}</span>
<Button
size="sm"
variant="outline"
disabled={busy || !isOwner}
onClick={() => void action("cancel")}
>
{t("managedEngines.cancelInstall")}
</Button>
</output>
) : (
<div
className={cn(
"flex flex-wrap items-center gap-2",
centered && "justify-center",
)}
>
{engine.installed && (
<span>
{t("managedEngines.installed", {
version: engine.installed_version ?? "",
})}
</span>
)}
{(!engine.installed || !engine.current || management) &&
!(centered && showConfirm) && (
<Button
size="sm"
variant="outline"
disabled={busy || !canInstall}
onClick={() => setConfirm(true)}
>
{engine.installed
? t(
engine.current
? "managedEngines.repair"
: "managedEngines.update",
)
: t("managedEngines.install")}
</Button>
)}
{management && isOwner && engine.installed && (
<Button
size="sm"
variant="destructive"
disabled={busy || engine.in_use}
onClick={() => void action("remove")}
>
{t("managedEngines.remove")}
</Button>
)}
{management && isOwner && engine.can_rollback && (
<Button
size="sm"
variant="ghost"
disabled={busy || engine.in_use}
onClick={() => void action("rollback")}
>
{t("managedEngines.rollback")}
</Button>
)}
</div>
)}
{management && engine.in_use && (
<p className="text-muted-foreground">{t("managedEngines.inUse")}</p>
)}
{showConfirm && (
<section
aria-label={t("managedEngines.installTitle", {
engine: names[engine.engine],
})}
className={cn(
centered ? "space-y-1 text-muted-foreground" : "space-y-3 rounded-lg border p-3",
)}
>
<p>
{engine.download_bytes
? t("managedEngines.confirmSized", {
engine: names[engine.engine],
version: engine.version,
size: formatGiB(engine.download_bytes / 1024 ** 3),
})
: t("managedEngines.confirm", {
engine: names[engine.engine],
version: engine.version,
})}
</p>
{wslNoticeKey(engine) && (
<p>{t(wslNoticeKey(engine)!, { engine: names[engine.engine] })}</p>
)}
<p>{t("managedEngines.background")}</p>
<div className={cn("flex gap-2", centered && "justify-center pt-3")}>
<Button
variant="dark"
size={centered ? "default" : "sm"}
disabled={busy}
onClick={() => void action("install")}
>
{onUse
? t("managedEngines.installAndLoad")
: t("managedEngines.install")}
</Button>
{!centered && (
<Button size="sm" variant="ghost" onClick={() => setConfirm(false)}>
{t("common.cancel")}
</Button>
)}
</div>
</section>
)}
{(error || engine.job.state === "error") && (
<p role="alert" className="whitespace-pre-wrap text-destructive">
{error || t("managedEngines.failed")}
</p>
)}
{engine.job.state === "error" && (
<details>
<summary>{t("managedEngines.details")}</summary>
<pre className="max-h-48 overflow-auto whitespace-pre-wrap text-ui-11">
{engine.job.message}
</pre>
</details>
)}
{engine.job.state === "cancelled" && (
<output>{t("managedEngines.cancelled")}</output>
)}
{engine.job.state === "waiting" && (
<output className="whitespace-pre-wrap">{engine.job.message}</output>
)}
</div>
);
}
export function InferenceEnginesSection() {
const t = useT();
const isOwner = useIsAccountOwner();
const { engines, error } = useEngines();
// Why the engine can't be installed, shown under its name.
const engineNote = (engine: EngineStatus) =>
engine.unsupported_reason ||
(isOwner || engine.installed ? undefined : t("managedEngines.ownerRequired"));
const rows = (
<>
{error && <p role="alert">{error}</p>}
{engines.map((engine) => (
<SettingsRow
key={engine.engine}
label={names[engine.engine]}
description={engineNote(engine)}
>
<EngineInstall engine={engine} management={true} />
</SettingsRow>
))}
</>
);
return (
<SettingsSection
title={t("managedEngines.title")}
description={t("managedEngines.description")}
>
{rows}
</SettingsSection>
);
}
export function InferenceEnginePicker({
value,
onChange,
onReadyChange,
onUse,
gpuIds,
parallelism = "tensor",
onParallelismChange,
precision = "auto",
onPrecisionChange,
kvCacheDtype = "auto",
onKvCacheDtypeChange,
gpuMemoryUtilization = null,
onGpuMemoryUtilizationChange,
seed = null,
onSeedChange,
onGpuChange,
}: {
value: InferenceEngine;
onChange: (engine: InferenceEngine) => void;
onReadyChange: (ready: boolean) => void;
onUse: () => void;
gpuIds?: number[] | null;
parallelism?: "tensor" | "pipeline" | "data";
onParallelismChange: (mode: "tensor" | "pipeline" | "data") => void;
precision?: "auto" | "bf16" | "fp16" | "int4" | "int8" | "fp8";
onPrecisionChange: (
precision: "auto" | "bf16" | "fp16" | "int4" | "int8" | "fp8",
) => void;
kvCacheDtype?: "auto" | "fp8";
onKvCacheDtypeChange: (dtype: "auto" | "fp8") => void;
gpuMemoryUtilization?: number | null;
onGpuMemoryUtilizationChange: (fraction: number | null) => void;
seed?: number | null;
onSeedChange: (seed: number | null) => void;
onGpuChange: (ids: number[]) => void;
}) {
const { engines, error } = useEngines();
const t = useT();
const devices = useGpuDevices(true)?.filter(isEngineGpuDevice);
// The backend's default for an unpicked GPU, so a first pick cannot keep a hidden GPU 0.
const selectedGpuIds = gpuIds?.length
? gpuIds
: devices?.length
? [devices[0].index]
: [0];
const selected = engines.find((engine) => engine.engine === value);
const ready = value === "auto" || isEngineReady(selected);
useEffect(() => {
onReadyChange(ready);
}, [ready, onReadyChange]);
// A precision kept from another GPU (INT4 saved on NVIDIA, opened on AMD) resets once the
// engine's status says it is unsupported, not only on an engine switch.
useEffect(() => {
if (!selected) return;
const kept = precisionAfterEngineSwitch(precision, selected);
if (kept !== precision) onPrecisionChange(kept);
}, [selected, precision, onPrecisionChange]);
if (
value === "auto" &&
engines.length > 0 &&
engines.every((engine) => engine.unsupported_reason)
) {
return null;
}
return (
<div className="space-y-2 rounded-lg border p-3">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.picker")}</span>
<Select
value={value}
onValueChange={(next) => {
const kept = precisionAfterEngineSwitch(
precision,
engines.find((engine) => engine.engine === next),
);
if (kept !== precision) onPrecisionChange(kept);
onChange(next as InferenceEngine);
}}
>
<SelectTrigger
className="w-44"
aria-label={t("managedEngines.picker")}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="auto">{t("managedEngines.default")}</SelectItem>
{engines.map((engine) => (
<SelectItem
key={engine.engine}
value={engine.engine}
disabled={!!engine.unsupported_reason}
>
{names[engine.engine]}
{engine.installed
? ""
: ` (${t("managedEngines.installRequired")})`}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{error && (
<p role="alert" className="text-ui-12">
{error}
</p>
)}
{selected && (
<>
{selected.platform !== "rocm" && (
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.scope")}
</p>
)}
{devices && devices.length > 0 && (
<div
role="group"
aria-label={t("managedEngines.gpu")}
className="space-y-2"
>
<span className="text-ui-13">{t("managedEngines.gpu")}</span>
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.gpuHelp")}
</p>
{devices.map((device) => {
const checked = selectedGpuIds.includes(device.index);
return (
<label
key={device.index}
className="flex items-center justify-between gap-3 text-ui-12"
>
<span className="min-w-0">
{device.index}: {device.name}
</span>
<Switch
className="shrink-0"
aria-label={`GPU ${device.index}: ${device.name}`}
checked={checked}
disabled={checked && selectedGpuIds.length === 1}
onCheckedChange={(next) =>
onGpuChange(
next
? [...selectedGpuIds, device.index]
: selectedGpuIds.filter(
(id) => id !== device.index,
),
)
}
/>
</label>
);
})}
{selectedGpuIds.length > 1 && (
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.parallelism")}</span>
<Select
value={parallelism}
onValueChange={(value) =>
onParallelismChange(value as typeof parallelism)
}
>
<SelectTrigger
className="w-52"
aria-label={t("managedEngines.parallelism")}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="tensor">
{t("managedEngines.tensor")}
</SelectItem>
<SelectItem value="pipeline">
{t("managedEngines.pipeline")}
</SelectItem>
<SelectItem value="data">
{t("managedEngines.data")}
</SelectItem>
</SelectContent>
</Select>
</div>
<p className="text-ui-12 text-muted-foreground">
{parallelism === "tensor"
? t("managedEngines.tensorParallel", {
count: selectedGpuIds.length,
})
: parallelism === "pipeline"
? t("managedEngines.pipelineHelp")
: t("managedEngines.dataHelp")}
</p>
</div>
)}
</div>
)}
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.precision")}</span>
<Select
value={precision}
onValueChange={(value) =>
onPrecisionChange(value as typeof precision)
}
>
<SelectTrigger
className="w-44"
aria-label={t("managedEngines.precision")}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="auto">
{t("managedEngines.precisionAuto")}
</SelectItem>
{PRECISION_LABELS.map(([value, label]) => (
<SelectItem
key={value}
value={value}
disabled={!supportsPrecision(selected, value)}
>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.precisionHelp")}
</p>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.kvCache")}</span>
<Select
value={kvCacheDtype}
onValueChange={(value) =>
onKvCacheDtypeChange(value as typeof kvCacheDtype)
}
>
<SelectTrigger
className="w-44"
aria-label={t("managedEngines.kvCache")}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="auto">
{t("managedEngines.precisionAuto")}
</SelectItem>
<SelectItem value="fp8">
{t("managedEngines.kvCacheFp8")}
</SelectItem>
</SelectContent>
</Select>
</div>
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.kvCacheHelp")}
</p>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.gpuMemory")}</span>
<Select
value={
gpuMemoryUtilization == null
? "auto"
: String(gpuMemoryUtilization)
}
onValueChange={(value) =>
onGpuMemoryUtilizationChange(
value === "auto" ? null : Number(value),
)
}
>
<SelectTrigger
className="w-44"
aria-label={t("managedEngines.gpuMemory")}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="auto">
{t("managedEngines.gpuMemoryAuto")}
</SelectItem>
{/* A stored value off the preset list stays selectable. */}
{[
...new Set([
...GPU_MEMORY_LIMITS,
...(gpuMemoryUtilization == null
? []
: [gpuMemoryUtilization]),
]),
]
.sort((a, b) => b - a)
.map((fraction) => (
<SelectItem key={fraction} value={String(fraction)}>
{`${Math.round(fraction * 100)}%`}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.gpuMemoryHelp")}
</p>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 text-ui-13">
<span>{t("managedEngines.seed")}</span>
<Input
className="w-44"
type="number"
inputMode="numeric"
min={0}
max={2 ** 31 - 1}
step={1}
aria-label={t("managedEngines.seed")}
placeholder={t("managedEngines.seedPlaceholder")}
value={seed ?? ""}
onChange={(event) => {
const raw = event.target.value.trim();
const next = Number(raw);
onSeedChange(
raw !== "" &&
Number.isInteger(next) &&
next >= 0 &&
next <= 2 ** 31 - 1
? next
: null,
);
}}
/>
</div>
<p className="text-ui-12 text-muted-foreground">
{t("managedEngines.seedHelp")}
</p>
</div>
<EngineInstall
key={selected.engine}
engine={selected}
onUse={onUse}
/>
</>
)}
</div>
);
}