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: run Laya on the GPU by default and free it on unload (#13479)

Nilayc762842e44d9
diff --git a/studio/backend/core/systemone/laya_runtime.py b/studio/backend/core/systemone/laya_runtime.py
index b14717f1402..a8c56b5c0a9 100644
--- a/studio/backend/core/systemone/laya_runtime.py
+++ b/studio/backend/core/systemone/laya_runtime.py
@@ -16,6 +16,7 @@ from pathlib import Path
from typing import Any
from .catalog import CHECKPOINTS, GGUF_COMPANIONS, LOCAL_NAME, MLX_COMPANIONS, Checkpoint
+from .laya_worker import LayaWorker, LayaWorkerError
logger = logging.getLogger(__name__)
@@ -71,7 +72,6 @@ class Unavailable(Exception):
def _device() -> str:
from utils.systemone_settings import get_device as preferred_device
- # CPU unless asked: a CUDA context opened in the backend process is never returned (see core.rag.embeddings._device).
if preferred_device() != "gpu":
return "cpu"
from utils.hardware.hardware import DeviceType, get_device
@@ -861,6 +861,7 @@ def _evict(external: bool = False) -> None:
_generation += 1
_unloads += external
_close(agent)
+ del agent
_release_memory()
@@ -1038,6 +1039,28 @@ def _load_checkpoint(checkpoint: Checkpoint):
# While training holds the GPU, a GPU model loads on CPU, as dictation does; see _misplaced.
for_training = _training_active() and preferred_device() == "gpu"
device = "cpu" if for_training else _device()
+ if device in ("cuda", "xpu"):
+ try:
+ agent = LayaWorker(root, checkpoint, device, cancelled = _training_active)
+ return agent, agent.device
+ except LayaWorkerError:
+ if not _training_active():
+ raise
+ device, for_training = "cpu", True
+ agent, placed = _build_laya(root, checkpoint, device, for_training)
+ if placed != "cpu" and _training_active():
+ del agent
+ _release_memory()
+ return _build_laya(root, checkpoint, "cpu", True)
+ return agent, placed
+
+
+def _build_laya(
+ root: Path,
+ checkpoint: Checkpoint,
+ device: str,
+ for_training: bool = False,
+):
folder = root / checkpoint.subfolder if checkpoint.subfolder else root
fp16_checkpoint = checkpoint.name in CHECKPOINTS or _stored_fp16(folder)
if device == "mlx":
@@ -1051,9 +1074,6 @@ def _load_checkpoint(checkpoint: Checkpoint):
agent = _load_laya(
str(root), subfolder = checkpoint.subfolder, device = "cpu", embedding_dtype = weights
)
- # Training may have started while this built.
- if device != "cpu" and _training_active():
- device, for_training = "cpu", True
_place(agent, torch.device(device), fp16_checkpoint)
agent.__dict__["_unsloth_for_training"] = for_training
return agent, str(agent.device.type)
@@ -1426,11 +1446,16 @@ def evict_for_gpu() -> None:
_close(agent, release_gpu = False)
+def _holds_gpu() -> bool:
+ # A worker that fell back to CPU still holds its CUDA or XPU context until it ends.
+ return _device_name not in (None, "cpu") or isinstance(_agent, LayaWorker)
+
+
def _misplaced() -> bool:
# The resident model follows the same rule: off the GPU while training holds it, back on it after.
if getattr(_agent, "_unsloth_for_training", False):
return not _training_active()
- return _device_name not in (None, "cpu") and _training_active()
+ return _holds_gpu() and _training_active()
def _clef_blocked_by_training(checkpoint: Checkpoint) -> None:
@@ -2163,7 +2188,7 @@ def _decide(
questions: dict[str, dict[str, Any]],
images: list[str] | None = None,
) -> dict[str, Any]:
- global _last_used
+ global _last_used, _device_name
agent = _agent_for(checkpoint, LOAD_WAIT_S)
if not _run_lock.acquire(timeout = RUN_WAIT_S):
raise Unavailable(529, "overloaded", "System One is busy; retry shortly", retry_after = 1)
@@ -2190,9 +2215,16 @@ def _decide(
return _decide_clef(checkpoint, agent, state, questions)
laya_questions = {name: _to_laya(q) for name, q in questions.items()}
try:
- result, truncated = _predict(agent, state, laya_questions)
+ if isinstance(agent, LayaWorker):
+ result, truncated = agent.predict(state, laya_questions)
+ _device_name = agent.device
+ else:
+ result, truncated = _predict(agent, state, laya_questions)
except ValueError as exc:
raise Unavailable(422, "invalid_request_error", str(exc)) from None
+ except LayaWorkerError as exc:
+ threading.Thread(target = _evict_agent, args = (agent,), daemon = True).start()
+ raise Unavailable(503, "model_unavailable", str(exc), retry_after = 5) from None
finally:
_run_lock.release()
return {
@@ -2263,6 +2295,7 @@ def status() -> dict[str, Any]:
# Laya never falls back, so a reason left by an earlier Clef is not its status.
"fallback_reason": _fallback_reason if _loaded and _loaded.layout != "laya" else None,
"device": _device_name,
+ "holds_gpu": _holds_gpu(),
"loading_model": _loading.name if _loading else None,
# Kept for the settings API: laya is vendored, so there is never an install in flight.
"installing": False,
@@ -2288,12 +2321,14 @@ def unload() -> bool:
_generation += 1
_unloads += 1
_close(agent)
+ # Dropped first, so an in-process MLX or MPS Laya is collected before the allocator caches are cleared.
+ del agent
_release_memory()
return was_loaded
def shutdown() -> None:
- """Studio shutdown: end a llama.cpp decision server (in-process models go with the process)."""
+ """Studio shutdown: end a llama.cpp decision server or Laya worker (in-process models go with the process)."""
global _agent, _loaded, _device_name, _generation, _unloads
with _state_lock:
timer = _idle_timer
@@ -2301,7 +2336,7 @@ def shutdown() -> None:
timer.cancel()
with _run_lock:
_unloads += 1
- if not _is_native(_loaded):
+ if not _is_native(_loaded) and not isinstance(_agent, LayaWorker):
return
agent, _agent, _loaded, _device_name = _agent, None, None, None
_generation += 1
diff --git a/studio/backend/core/systemone/laya_worker.py b/studio/backend/core/systemone/laya_worker.py
new file mode 100644
index 00000000000..86c022234e1
--- /dev/null
+++ b/studio/backend/core/systemone/laya_worker.py
@@ -0,0 +1,142 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Laya on CUDA, ROCm or XPU, in a spawn child: a CUDA context is only returned when its process ends."""
+
+from __future__ import annotations
+
+import multiprocessing as mp
+import time
+from pathlib import Path
+from typing import Any, Callable
+
+_CTX = mp.get_context("spawn")
+LOAD_TIMEOUT_S = 600.0
+DECIDE_TIMEOUT_S = 300.0
+CLOSE_TIMEOUT_S = 10.0
+
+
+class LayaWorkerError(RuntimeError):
+ pass
+
+
+def run_laya_worker(conn, root: str, checkpoint, device: str) -> None:
+ from core.systemone import laya_runtime
+
+ try:
+ agent, _ = laya_runtime._build_laya(Path(root), checkpoint, device)
+ except BaseException as exc:
+ conn.send(("error", f"{type(exc).__name__}: {exc}"))
+ return
+ conn.send(("ready", agent.device.type))
+ while True:
+ try:
+ message = conn.recv()
+ except (EOFError, OSError):
+ return
+ if message[0] == "close":
+ return
+ _, state, questions = message
+ try:
+ # The device rides along: an out-of-memory decision moves the model to CPU here, not in the parent.
+ answer = laya_runtime._predict(agent, state, questions)
+ conn.send(("ok", (answer, agent.device.type)))
+ except ValueError as exc:
+ conn.send(("invalid", str(exc)))
+ except BaseException as exc:
+ conn.send(("error", f"{type(exc).__name__}: {exc}"))
+
+
+class LayaWorker:
+ def __init__(self, root: Path, checkpoint, device: str, cancelled: Callable[[], bool]):
+ self._broken: str | None = None
+ from utils.native_path_leases import (
+ native_path_secret_removed_for_child_start,
+ run_without_native_path_secret,
+ )
+ from utils.process_lifetime import (
+ adopt_pid,
+ is_process_shutting_down,
+ spawn_on_lifetime_thread,
+ )
+
+ if is_process_shutting_down():
+ raise LayaWorkerError("Studio is shutting down; the Laya worker was not started.")
+ self._conn, child = _CTX.Pipe()
+ with native_path_secret_removed_for_child_start():
+ self._process = _CTX.Process(
+ target = run_without_native_path_secret,
+ args = (run_laya_worker, child, str(root), checkpoint, device),
+ name = "systemone-laya",
+ daemon = True,
+ )
+ # Linux PDEATHSIG follows the spawning thread, and this runs on a loader thread that ends after the load.
+ spawn_on_lifetime_thread(self._process.start)
+ adopt_pid(self._process.pid)
+ child.close()
+ try:
+ deadline = time.monotonic() + LOAD_TIMEOUT_S
+ while not self._conn.poll(1.0):
+ if is_process_shutting_down():
+ raise LayaWorkerError("The Laya worker was stopped while it loaded.")
+ if cancelled():
+ raise LayaWorkerError("A training run took the GPU while Laya loaded.")
+ if time.monotonic() > deadline:
+ raise LayaWorkerError(
+ f"The Laya worker did not answer within {LOAD_TIMEOUT_S:.0f}s"
+ )
+ kind, payload = self._receive(0)
+ if kind != "ready":
+ raise LayaWorkerError(payload)
+ if cancelled():
+ raise LayaWorkerError("A training run took the GPU while Laya loaded.")
+ except BaseException:
+ # A child still building cannot read "close", and training may already be claiming the GPU.
+ self.close(grace = 0)
+ raise
+ self.device = payload
+
+ def _receive(self, timeout: float):
+ try:
+ if not self._conn.poll(timeout):
+ raise LayaWorkerError(f"The Laya worker did not answer within {timeout:.0f}s")
+ return self._conn.recv()
+ except (EOFError, OSError):
+ raise LayaWorkerError(
+ f"The Laya worker exited (code {self._process.exitcode})"
+ ) from None
+
+ def predict(self, state, questions: dict[str, dict[str, Any]]) -> tuple[dict[str, Any], bool]:
+ # After a timeout the late answer is still in the pipe, so the worker is never asked again.
+ if self._broken is not None:
+ raise LayaWorkerError(self._broken)
+ try:
+ self._conn.send(("decide", state, questions))
+ kind, payload = self._receive(DECIDE_TIMEOUT_S)
+ except (OSError, ValueError, LayaWorkerError) as exc:
+ self._broken = (
+ str(exc)
+ if isinstance(exc, LayaWorkerError)
+ else f"The Laya worker exited (code {self._process.exitcode})"
+ )
+ raise LayaWorkerError(self._broken) from None
+ if kind == "invalid":
+ raise ValueError(payload)
+ if kind != "ok":
+ raise LayaWorkerError(payload)
+ answer, self.device = payload
+ return answer
+
+ def close(self, grace: float | None = None) -> None:
+ try:
+ self._conn.send(("close",))
+ except (OSError, ValueError):
+ pass
+ self._process.join(CLOSE_TIMEOUT_S if grace is None else grace)
+ if self._process.is_alive():
+ self._process.kill()
+ self._process.join(CLOSE_TIMEOUT_S)
+ self._conn.close()
+ if not self._process.is_alive():
+ from utils.process_lifetime import forget_pid
+ forget_pid(self._process.pid)
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py
index 805f4e3880a..518072a644c 100644
--- a/studio/backend/routes/training.py
+++ b/studio/backend/routes/training.py
@@ -2289,7 +2289,7 @@ async def start_training(
try:
from core.systemone import laya_runtime
- if laya_runtime.status()["device"] not in (None, "cpu"):
+ if laya_runtime.status()["holds_gpu"]:
logger.info("Unloading the Decision API model to free GPU memory for training")
laya_runtime.unload()
except Exception as e:
diff --git a/studio/backend/tests/test_decision_serving.py b/studio/backend/tests/test_decision_serving.py
index 9c5fdcf2037..22deb8c28f6 100644
--- a/studio/backend/tests/test_decision_serving.py
+++ b/studio/backend/tests/test_decision_serving.py
@@ -148,7 +148,7 @@ def gpu(home, monkeypatch):
monkeypatch.setattr(hardware, "get_device", lambda: hardware.DeviceType.CUDA)
monkeypatch.setattr(torch_device_probe, "device_can_allocate", lambda device: True)
# The real loader around a stand-in model; placing it only records where it would go.
- state = SimpleNamespace(training = False, placed = [], on_build = None)
+ state = SimpleNamespace(training = False, placed = [], on_build = None, oom = False, closed = 0)
def build(path, **kwargs):
if state.on_build:
@@ -159,7 +159,24 @@ def gpu(home, monkeypatch):
state.placed.append(device.type)
agent.device = device
+ class Worker:
+ def __init__(self, root, checkpoint, device, cancelled):
+ build(root)
+ if cancelled():
+ raise laya_runtime.LayaWorkerError("A training run took the GPU while Laya loaded.")
+ state.placed.append(device)
+ self.device = device
+
+ def predict(self, decision_state, questions):
+ if state.oom:
+ self.device = "cpu"
+ return _answer(self, decision_state, questions)
+
+ def close(self):
+ state.closed += 1
+
monkeypatch.setattr(laya_runtime, "_load_checkpoint", _REAL_LOAD)
+ monkeypatch.setattr(laya_runtime, "LayaWorker", Worker)
monkeypatch.setattr(laya_runtime, "_load_laya", build)
monkeypatch.setattr(laya_runtime, "_place", place)
monkeypatch.setattr(laya_runtime, "_training_active", lambda: state.training)
@@ -184,6 +201,28 @@ def test_the_model_returns_to_the_gpu_after_training(home, client, gpu):
assert client.get("/api/settings/systemone").json()["loaded_device"] == "cuda"
+def test_a_gpu_worker_that_fell_back_to_cpu_reports_cpu(home, client, gpu):
+ assert _put(client, enabled = True, model = _fine_tune(home, "laya_served_1")).status_code == 200
+ assert _post(client).status_code == 200
+ assert client.get("/api/settings/systemone").json()["loaded_device"] == "cuda"
+ gpu.oom = True
+ assert _post(client).status_code == 200
+ assert client.get("/api/settings/systemone").json()["loaded_device"] == "cpu"
+ assert laya_runtime.status()["holds_gpu"]
+ gpu.training = True
+ assert _post(client).status_code == 200
+ assert gpu.closed == 1
+ assert gpu.placed == ["cuda", "cpu"]
+
+
+def test_shutdown_ends_a_gpu_worker(home, client, gpu):
+ assert _put(client, enabled = True, model = _fine_tune(home, "laya_served_1")).status_code == 200
+ assert _post(client).status_code == 200
+ laya_runtime.shutdown()
+ assert gpu.closed == 1
+ assert client.get("/api/settings/systemone").json()["loaded_device"] is None
+
+
def test_training_takes_the_gpu_from_a_load_in_flight_or_a_model_it_missed(home, client, gpu):
def training_starts():
gpu.training, gpu.on_build = True, None
diff --git a/studio/backend/tests/test_orchestrator_unload_cancel.py b/studio/backend/tests/test_orchestrator_unload_cancel.py
index 6fc4e7890e5..d3b4a54b4a3 100644
--- a/studio/backend/tests/test_orchestrator_unload_cancel.py
+++ b/studio/backend/tests/test_orchestrator_unload_cancel.py
@@ -3884,6 +3884,7 @@ def test_every_long_lived_spawner_consults_the_shutdown_latch():
"core/inference/video_ltx25.py",
"core/rag/embed_llama_server.py",
"core/systemone/native_worker.py",
+ "core/systemone/laya_worker.py",
"core/training/training.py",
}
# Adopters this change deliberately leaves ungated, listed so the completeness check
diff --git a/studio/backend/tests/test_systemone.py b/studio/backend/tests/test_systemone.py
index b36bb021ef0..a1a7fa1d61a 100644
--- a/studio/backend/tests/test_systemone.py
+++ b/studio/backend/tests/test_systemone.py
@@ -5,6 +5,7 @@ import asyncio
import os
import sys
import threading
+import time
import weakref
from types import SimpleNamespace
@@ -380,7 +381,10 @@ def test_env_kill_switch_beats_the_setting(client, monkeypatch, runtime):
assert runtime == []
-def test_settings_report_choices_and_residency(client):
+def test_settings_report_choices_and_residency(client, monkeypatch):
+ from utils.hardware import hardware
+
+ monkeypatch.setattr(hardware, "get_device", lambda: hardware.DeviceType.CPU)
settings = client.get("/api/settings/systemone").json()
assert settings["enabled"] is True
assert settings["model"] == "laya-multilingual"
@@ -703,10 +707,163 @@ def test_hub_checkpoint_downloads_only_its_subfolder_into_studio_cache(monkeypat
assert all(p.startswith("multilingual/") for p in seen["allow_patterns"])
-def test_device_defaults_to_cpu():
+ "kind, device", [("CUDA", "gpu"), ("XPU", "gpu"), ("MLX", "gpu"), ("CPU", "cpu")]
+)
+def test_device_defaults_to_the_gpu_where_laya_runs_in_its_own_process(
+ client, monkeypatch, kind, device
+):
+ from utils.hardware import hardware
+
+ monkeypatch.setattr(hardware, "get_device", lambda: getattr(hardware.DeviceType, kind))
+ assert client.get("/api/settings/systemone").json()["device"] == device
+ assert client.put("/api/settings/systemone", json = {"device": "cpu"}).status_code == 200
+ assert client.get("/api/settings/systemone").json()["device"] == "cpu"
assert laya_runtime._device() == "cpu"
+def _tiny_laya_checkpoint(tmp_path):
+ import json
+
+ from safetensors.torch import save_file
+ from tokenizers import Tokenizer, models, pre_tokenizers
+ from transformers import PreTrainedTokenizerFast
+
+ encoder_dir, cfg = _tiny_laya_encoder(tmp_path)
+ words = ["[PAD]", "[SEP]", "[CLS]", "[UNK]", "[MASK]"] + [f"w{i}" for i in range(295)]
+ backend = Tokenizer(models.WordLevel({w: i for i, w in enumerate(words)}, unk_token = "[UNK]"))
+ backend.pre_tokenizer = pre_tokenizers.Whitespace()
+ PreTrainedTokenizerFast(
+ tokenizer_object = backend,
+ pad_token = "[PAD]",
+ sep_token = "[SEP]",
+ cls_token = "[CLS]",
+ unk_token = "[UNK]",
+ mask_token = "[MASK]",
+ ).save_pretrained(tmp_path / "tokenizer")
+ model = laya_runtime._laya().common.build_model(cfg, encoder_dir = encoder_dir)
+ save_file(
+ {k: v.clone().contiguous() for k, v in model.state_dict().items()},
+ str(tmp_path / "model.safetensors"),
+ )
+ (tmp_path / "rl_agent_config.json").write_text(json.dumps(cfg))
+ return tmp_path
+
+
[email protected]("evict", [False, True])
+def test_an_unloaded_in_process_laya_is_gone_before_device_caches_clear(
+ runtime, monkeypatch, evict
+):
+ class MLXLaya:
+ device = "mlx"
+
+ agent = MLXLaya()
+ alive = weakref.ref(agent)
+ cleared = []
+ monkeypatch.setattr(laya_runtime, "_agent", agent)
+ monkeypatch.setattr(laya_runtime, "_loaded", catalog.CHECKPOINTS["laya-multilingual"])
+ monkeypatch.setattr(laya_runtime, "_device_name", "mlx")
+ monkeypatch.setattr(laya_runtime, "_release_memory", lambda: cleared.append(alive() is None))
+ del agent
+ laya_runtime._evict() if evict else laya_runtime.unload()
+ assert cleared == [True]
+
+
+def test_laya_worker_answers_like_laya_in_process(tmp_path):
+ pytest.importorskip("torch")
+ from core.systemone.laya_worker import LayaWorker
+
+ folder = _tiny_laya_checkpoint(tmp_path)
+ checkpoint = catalog.Checkpoint(catalog.LOCAL_NAME, str(folder), None, "")
+ questions = {name: laya_runtime._to_laya(q) for name, q in QUESTIONS.items()}
+ expected = _REAL_PREDICT(
+ laya_runtime._build_laya(folder, checkpoint, "cpu")[0], "w1 w2 w3", questions
+ )
+ started = []
+ # Built on a thread that then ends, as the runtime's loader does: the child must outlive that thread.
+ loader = threading.Thread(
+ target = lambda: started.append(
+ LayaWorker(folder, checkpoint, "cpu", cancelled = lambda: False)
+ )
+ )
+ loader.start()
+ loader.join()
+ worker = started[0]
+ time.sleep(1)
+ try:
+ assert worker._process.is_alive()
+ assert worker.predict("w1 w2 w3", questions) == expected
+ assert worker.device == "cpu"
+ finally:
+ worker.close()
+ assert not worker._process.is_alive()
+
+
[email protected](not hasattr(os, "mkfifo"), reason = "a FIFO holds the child inside its load")
+def test_a_cancelled_laya_load_ends_its_child_at_once(tmp_path, monkeypatch):
+ from core.systemone import laya_worker
+
+ # The child blocks opening the weights, as a long GPU load would, so it never reads "close".
+ os.mkfifo(tmp_path / "model.safetensors")
+ monkeypatch.setattr(laya_worker, "CLOSE_TIMEOUT_S", 60.0)
+ checkpoint = catalog.Checkpoint(catalog.LOCAL_NAME, str(tmp_path), None, "")
+ polls = []
+ started = time.monotonic()
+ with pytest.raises(laya_worker.LayaWorkerError, match = "training run"):
+ laya_worker.LayaWorker(
+ tmp_path, checkpoint, "cpu", cancelled = lambda: polls.append(1) or len(polls) > 2
+ )
+ assert time.monotonic() - started < 30
+
+
+def test_gpu_laya_answers_from_its_own_process_that_unload_ends(client, monkeypatch, tmp_path):
+ torch = pytest.importorskip("torch")
+ if not torch.cuda.is_available():
+ pytest.skip("needs a CUDA or ROCm GPU")
+ initialized = torch.cuda.is_initialized()
+ monkeypatch.setattr(laya_runtime, "_load_checkpoint", _REAL_LOAD)
+ monkeypatch.setattr(laya_runtime, "_predict", _REAL_PREDICT)
+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 600.0)
+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", str(_tiny_laya_checkpoint(tmp_path)))
+ response = _post(client, state = "w1 w2 w3")
+ assert response.status_code == 200, response.text
+ settings = client.get("/api/settings/systemone").json()
+ assert (settings["device"], settings["loaded_device"]) == ("gpu", "cuda")
+ assert torch.cuda.is_initialized() == initialized
+ process = laya_runtime._agent._process
+ assert process.is_alive()
+ assert client.post("/api/settings/systemone/unload").status_code == 200
+ assert not process.is_alive()
+
+
[email protected]("device", ["mlx", "mps"])
+def test_an_apple_gpu_load_that_training_overtook_lands_on_cpu(monkeypatch, tmp_path, device):
+ from utils import systemone_settings as settings
+
+ training, built = [False], []
+
+ def build(
+ root,
+ checkpoint,
+ where,
+ for_training = False,
+ ):
+ built.append(where)
+ training[0] = True
+ return SimpleNamespace(device = where), where
+
+ monkeypatch.setattr(settings, "get_device", lambda: "gpu")
+ monkeypatch.setattr(laya_runtime, "_device", lambda: device)
+ monkeypatch.setattr(laya_runtime, "_training_active", lambda: training[0])
+ monkeypatch.setattr(laya_runtime, "_checkpoint_dir", lambda checkpoint: tmp_path)
+ monkeypatch.setattr(laya_runtime, "_laya", lambda: None)
+ monkeypatch.setattr(laya_runtime, "_evict", lambda: None)
+ monkeypatch.setattr(laya_runtime, "_build_laya", build)
+ checkpoint = catalog.Checkpoint(catalog.LOCAL_NAME, str(tmp_path), None, "")
+ agent, placed = _REAL_LOAD(checkpoint)
+ assert (built, placed) == ([device, "cpu"], "cpu")
+
+
def test_gpu_on_apple_silicon_runs_mlx_and_falls_back_to_mps(monkeypatch):
torch = pytest.importorskip("torch")
from utils.hardware import hardware
diff --git a/studio/backend/utils/systemone_settings.py b/studio/backend/utils/systemone_settings.py
index ddf857c451c..6a1632a5fe8 100644
--- a/studio/backend/utils/systemone_settings.py
+++ b/studio/backend/utils/systemone_settings.py
@@ -99,7 +99,13 @@ def get_device() -> str:
if device_locked():
return "gpu" if _env(ENV_DEVICE).lower() in ("gpu", "auto") else "cpu"
stored = _owner_setting(DEVICE_KEY)
- return stored if stored in DEVICES else "cpu"
+ if stored in DEVICES:
+ return stored
+ from utils.hardware.hardware import DeviceType, get_device as detected_device
+
+ # Only where unloading gives all of Laya's memory back: its own process, or MLX.
+ gpu = (DeviceType.CUDA, DeviceType.XPU, DeviceType.MLX)
+ return "gpu" if detected_device() in gpu else "cpu"
def clef_device() -> str:
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 054ce29725a..55848f246ae 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -3009,7 +3009,7 @@ export const ar = {
modelClef: "Clef",
recommended: "موصى به",
device: "التشغيل على",
- deviceDescription: "وحدة GPU أسرع في الإجابة، لكنها تحتفظ بذاكرتها محجوزة حتى إعادة التشغيل.",
+ deviceDescription: "وحدة GPU أسرع في الإجابة، وتحرر ذاكرتها عند إلغاء تحميل النموذج.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "بيئة التشغيل",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index f291e48ba93..e9eb2324e11 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -3054,7 +3054,7 @@ export const de = {
modelClef: "Clef",
recommended: "Empfohlen",
device: "Ausführen auf",
- deviceDescription: "Die GPU antwortet schneller, hält ihren Speicher aber bis zum Neustart reserviert.",
+ deviceDescription: "Die GPU antwortet schneller und gibt ihren Speicher frei, wenn das Modell entladen wird.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Laufzeit",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index 6943a3c4204..8f3e6484ccc 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -3049,7 +3049,7 @@ export const en = {
modelClef: "Clef",
recommended: "Recommended",
device: "Run on",
- deviceDescription: "GPU answers faster, but keeps its memory reserved until restart.",
+ deviceDescription: "GPU answers faster and frees its memory when the model unloads.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Runtime",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index 8ec2311438b..57379ba4822 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -3047,7 +3047,7 @@ export const es = {
modelClef: "Clef",
recommended: "Recomendado",
device: "Ejecutar en",
- deviceDescription: "La GPU responde más rápido, pero mantiene su memoria reservada hasta reiniciar.",
+ deviceDescription: "La GPU responde más rápido y libera su memoria cuando se descarga el modelo.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Entorno de ejecución",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index 75c408ae609..c749803a058 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -3055,7 +3055,7 @@ export const fr = {
modelClef: "Clef",
recommended: "Recommandé",
device: "Exécuter sur",
- deviceDescription: "Le GPU répond plus vite, mais garde sa mémoire réservée jusqu'au redémarrage.",
+ deviceDescription: "Le GPU répond plus vite et libère sa mémoire quand le modèle est déchargé.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Moteur d'exécution",
diff --git a/studio/frontend/src/i18n/locales/he.ts b/studio/frontend/src/i18n/locales/he.ts
index 60c61f0980e..e3c479ba29c 100644
--- a/studio/frontend/src/i18n/locales/he.ts
+++ b/studio/frontend/src/i18n/locales/he.ts
@@ -3118,7 +3118,7 @@ export const he = {
recommended: "מומלץ",
device: "הרץ על",
deviceDescription:
- "GPU עונה מהר יותר, אך שומר את הזיכרון שלו שמור עד להפעלה מחדש.",
+ "GPU עונה מהר יותר ומשחרר את הזיכרון שלו כשהמודל נפרק.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "סביבת ריצה",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index 28b760fe2f7..fbd24b87856 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -3016,7 +3016,7 @@ export const hi = {
modelClef: "Clef",
recommended: "सुझाया गया",
device: "यहाँ चलाएँ",
- deviceDescription: "GPU तेज़ जवाब देता है, लेकिन रीस्टार्ट होने तक अपनी मेमोरी आरक्षित रखता है।",
+ deviceDescription: "GPU तेज़ जवाब देता है और मॉडल अनलोड होने पर अपनी मेमोरी खाली कर देता है।",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "रनटाइम",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index fde773c0fad..fa9ea9d8caf 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -2964,7 +2964,7 @@ export const it = {
modelClef: "Clef",
recommended: "Consigliato",
device: "Esegui su",
- deviceDescription: "La GPU risponde più velocemente, ma tiene la memoria riservata fino al riavvio.",
+ deviceDescription: "La GPU risponde più velocemente e libera la memoria quando il modello viene scaricato.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Runtime",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index 4a84b16e713..d96463cf2f9 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -2979,7 +2979,7 @@ export const ja = {
modelClef: "Clef",
recommended: "推奨",
device: "実行先",
- deviceDescription: "GPU の方が高速ですが、再起動するまでメモリを確保し続けます。",
+ deviceDescription: "GPU の方が高速で、モデルをアンロードするとメモリを解放します。",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "ランタイム",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index c43eb0c7a6f..24eeb83d76a 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -2999,7 +2999,7 @@ export const ko = {
modelClef: "Clef",
recommended: "추천",
device: "실행 위치",
- deviceDescription: "GPU가 더 빠르지만 다시 시작할 때까지 메모리를 계속 차지합니다.",
+ deviceDescription: "GPU가 더 빠르며, 모델을 언로드하면 메모리를 반환합니다.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "런타임",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index 204c70278fe..7cf06278a2b 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -3026,7 +3026,7 @@ export const ptBR = {
modelClef: "Clef",
recommended: "Recomendado",
device: "Executar em",
- deviceDescription: "A GPU responde mais rápido, mas mantém a memória reservada até reiniciar.",
+ deviceDescription: "A GPU responde mais rápido e libera a memória quando o modelo é descarregado.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Runtime",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index 07284b6f6ca..fa92bfdfe2d 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -3021,7 +3021,7 @@ export const ru = {
modelClef: "Clef",
recommended: "Рекомендуется",
device: "Запускать на",
- deviceDescription: "GPU отвечает быстрее, но держит память зарезервированной до перезапуска.",
+ deviceDescription: "GPU отвечает быстрее и освобождает память, когда модель выгружается.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Среда выполнения",
diff --git a/studio/frontend/src/i18n/locales/sv.ts b/studio/frontend/src/i18n/locales/sv.ts
index c3a30589a4d..0475333a27b 100644
--- a/studio/frontend/src/i18n/locales/sv.ts
+++ b/studio/frontend/src/i18n/locales/sv.ts
@@ -3218,7 +3218,7 @@ export const sv = {
recommended: "Rekommenderas",
device: "Kör på",
deviceDescription:
- "GPU svarar snabbare men håller sitt minne reserverat tills omstart.",
+ "GPU svarar snabbare och frigör sitt minne när modellen avlastas.",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "Körmiljö",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index a2e6aeff663..2ae2fca1d06 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -2958,7 +2958,7 @@ export const zhCN = {
modelClef: "Clef",
recommended: "推荐",
device: "运行于",
- deviceDescription: "GPU 响应更快,但在重启前会一直占用显存。",
+ deviceDescription: "GPU 响应更快,卸载模型后会释放显存。",
deviceCpu: "CPU",
deviceGpu: "GPU",
backend: "运行时",