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.

unsloth run: add --engine-parallelism and --gpus for multi-GPU serving (#13838)

Daniel Han16ec45112b9a
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index fa84f510536..61b275683dd 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -1695,6 +1695,18 @@ def _enforce_password_change_before_exposure(
conn.close()
+def _parse_gpu_ids(value: Optional[str]) -> Optional[List[int]]:
+ if value is None or not value.strip():
+ return None
+ try:
+ ids = [int(part) for part in value.split(",") if part.strip()]
+ except ValueError:
+ raise ValueError(f"--gpus takes comma-separated GPU indices such as 0,1, not {value!r}.")
+ if any(i < 0 for i in ids) or len(set(ids)) != len(ids):
+ raise ValueError(f"--gpus needs distinct, non-negative GPU indices, not {value!r}.")
+ return ids or None
+
+
def _load_model_via_http(
port: int,
api_key: str,
@@ -1711,6 +1723,8 @@ def _load_model_via_http(
request_host: str = "127.0.0.1",
engine: str = "auto",
engine_precision: str = "auto",
+ engine_parallelism: str = "tensor",
+ gpu_ids: Optional[List[int]] = None,
) -> dict:
import json
import urllib.request
@@ -1739,6 +1753,10 @@ def _load_model_via_http(
if engine != "auto":
payload["engine"] = engine
payload["engine_precision"] = engine_precision
+ if engine_parallelism != "tensor":
+ payload["engine_parallelism"] = engine_parallelism
+ if gpu_ids:
+ payload["gpu_ids"] = list(gpu_ids)
data = json.dumps(payload).encode()
url = f"http://{_url_host(request_host)}:{port}/api/inference/load"
@@ -2608,6 +2626,25 @@ def run(
"`--password -` to read one line from stdin. A literal value is visible in the "
"process list and shell history. Rotate later with `unsloth studio reset-password`.",
),
+ engine_parallelism: Literal["tensor", "pipeline", "data"] = typer.Option(
+ "tensor",
+ "--engine-parallelism",
+ rich_help_panel = _RUN_PANEL_MODEL,
+ help = (
+ "How --engine vllm/sglang uses several GPUs: tensor splits each layer, pipeline "
+ "splits the layers, data runs one replica per GPU and spreads requests across "
+ "them. pipeline and data use every visible GPU unless --gpus narrows it."
+ ),
+ ),
+ gpus: Optional[str] = typer.Option(
+ None,
+ "--gpus",
+ rich_help_panel = _RUN_PANEL_MODEL,
+ help = (
+ "Comma-separated GPU indices to load the model on, for example 0,1 (the "
+ "Studio GPU picker). Default: automatic selection."
+ ),
+ ),
):
"""Start Unsloth, load a model, print an API key -- one-liner server.
@@ -2625,6 +2662,7 @@ def run(
unsloth studio run --model unsloth/Qwen3-1.7B-GGUF --temperature 0.7 --seed 42 --parallel 8
unsloth studio run --model some-model --chat-template-file /path/to/tpl.jinja
unsloth studio run --model unsloth/Qwen3-27B-GGUF --gguf-variant Q8_0 --tensor-parallel
+ unsloth studio run --model unsloth/Qwen3-8B --engine vllm --engine-parallelism data
"""
# Passed via env, so an older re-exec target ignores it instead of treating it as a llama-server arg.
inherited_start_api_key_marker = _consume_start_api_key_marker_env()
@@ -2716,6 +2754,17 @@ def run(
err = True,
)
raise typer.Exit(2)
+ if engine == "auto" and engine_parallelism != "tensor":
+ typer.echo(
+ "Error: --engine-parallelism applies only to --engine vllm or --engine sglang.",
+ err = True,
+ )
+ raise typer.Exit(2)
+ try:
+ gpu_ids = _parse_gpu_ids(gpus)
+ except ValueError as exc:
+ typer.echo(f"Error: {exc}", err = True)
+ raise typer.Exit(2)
_require_bind_host(host)
@@ -2866,6 +2915,11 @@ def run(
args.append("--tensor-parallel" if tensor_parallel else "--no-tensor-parallel")
if engine != "auto":
args.extend(["--engine", engine, "--engine-precision", engine_precision])
+ # Only when set: an older studio-venv CLI would hand unknown flags to llama-server.
+ if engine_parallelism != "tensor":
+ args.extend(["--engine-parallelism", engine_parallelism])
+ if gpu_ids:
+ args.extend(["--gpus", ",".join(str(i) for i in gpu_ids)])
if verbose:
args.append("--verbose")
if extra_llama_args:
@@ -2943,6 +2997,10 @@ def run(
except RuntimeError as exc:
typer.echo(f"Error: {exc}", err = True)
raise typer.Exit(1)
+ if engine_parallelism != "tensor" and not gpu_ids:
+ # The engine otherwise gets one GPU, which makes pipeline / data a no-op.
+ from utils.hardware.hardware import resolve_requested_gpu_ids
+ gpu_ids = resolve_requested_gpu_ids(None)
if not silent:
typer.echo(f"Loading model: {model}...")
try:
@@ -2961,6 +3019,8 @@ def run(
request_host = request_host,
engine = engine,
engine_precision = engine_precision,
+ engine_parallelism = engine_parallelism,
+ gpu_ids = gpu_ids,
)
except RuntimeError as exc:
typer.echo(f"Error: {exc}", err = True)
diff --git a/unsloth_cli/tests/test_studio_run_engine_flag.py b/unsloth_cli/tests/test_studio_run_engine_flag.py
index 976c52cabbd..49712b106b4 100644
--- a/unsloth_cli/tests/test_studio_run_engine_flag.py
+++ b/unsloth_cli/tests/test_studio_run_engine_flag.py
@@ -192,3 +192,42 @@ def test_wsl_notice_follows_its_state(wsl, expected):
def test_linux_has_no_wsl_notice():
assert studio_mod._engine_install_notice("vllm", {"host": "local"}) is None
+
+
+def test_parallelism_and_gpu_options_are_registered_on_run():
+ sig = inspect.signature(studio_mod.run)
+ assert set(sig.parameters["engine_parallelism"].default.param_decls) == {"--engine-parallelism"}
+ assert sig.parameters["engine_parallelism"].default.default == "tensor"
+ assert set(sig.parameters["gpus"].default.param_decls) == {"--gpus"}
+ assert sig.parameters["gpus"].default.default is None
+
+
+def test_engine_parallelism_and_gpus_reach_the_load_payload(monkeypatch):
+ payload = _capture_payload(
+ monkeypatch, engine = "vllm", engine_parallelism = "data", gpu_ids = [0, 2]
+ )
+ assert payload["engine_parallelism"] == "data" and payload["gpu_ids"] == [0, 2]
+
+
+def test_gpus_reach_a_gguf_load_payload(monkeypatch):
+ payload = _capture_payload(monkeypatch, gpu_ids = [1])
+ assert payload["gpu_ids"] == [1] and "engine_parallelism" not in payload
+
+
+def test_default_tensor_parallelism_is_not_sent(monkeypatch):
+ payload = _capture_payload(monkeypatch, engine = "vllm")
+ assert "engine_parallelism" not in payload and "gpu_ids" not in payload
+
+
+ ("value", "expected"),
+ [(None, None), ("", None), ("0", [0]), ("0,1, 3", [0, 1, 3]), ("2,", [2])],
+)
+def test_parse_gpu_ids(value, expected):
+ assert studio_mod._parse_gpu_ids(value) == expected
+
+
[email protected]("value", ["a", "0,0", "-1", "0;1"])
+def test_parse_gpu_ids_rejects_bad_values(value):
+ with pytest.raises(ValueError, match = "--gpus"):
+ studio_mod._parse_gpu_ids(value)
diff --git a/unsloth_cli/tests/test_studio_run_parallel_flag.py b/unsloth_cli/tests/test_studio_run_parallel_flag.py
index 21d82880d23..f7fdb06bd4a 100644
--- a/unsloth_cli/tests/test_studio_run_parallel_flag.py
+++ b/unsloth_cli/tests/test_studio_run_parallel_flag.py
@@ -458,6 +458,38 @@ def test_reexec_omits_default_gpu_memory_mode(monkeypatch):
assert "--gpu-memory-mode" not in captured[0]["argv"]
+def test_reexec_forwards_engine_parallelism_and_gpus(monkeypatch):
+ result, captured = _invoke_run(
+ monkeypatch,
+ ["--model", "unsloth/Qwen3-8B", "--engine", "vllm"]
+ + ["--engine-parallelism", "data", "--gpus", "0,2"],
+ )
+ assert len(captured) == 1, result.output
+ argv = captured[0]["argv"]
+ assert _value_after(argv, "--engine-parallelism") == "data", argv
+ assert _value_after(argv, "--gpus") == "0,2", argv
+
+
+def test_reexec_omits_default_engine_parallelism_and_gpus(monkeypatch):
+ result, captured = _invoke_run(monkeypatch, _BASE)
+ assert len(captured) == 1, result.output
+ assert "--engine-parallelism" not in captured[0]["argv"]
+ assert "--gpus" not in captured[0]["argv"]
+
+
+ ("extra", "message"),
+ [
+ (["--engine-parallelism", "data"], "--engine-parallelism applies only to --engine"),
+ (["--gpus", "0,x"], "--gpus takes comma-separated GPU indices"),
+ ],
+)
+def test_bad_engine_parallelism_or_gpus_is_refused_before_reexec(monkeypatch, extra, message):
+ result, captured = _invoke_run(monkeypatch, _BASE + extra)
+ assert result.exit_code == 2 and not captured
+ assert message in result.output
+
+
def test_reexec_forwards_speculative_options(monkeypatch):
result, captured = _invoke_run(
monkeypatch,