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.

9.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

// Mirrors resolve_model_override_key and _folded_override_matches in
// utils/openai_auto_switch_settings.py; picker and API auto-switch must agree.

import assert from "node:assert/strict";
import test from "node:test";

import { registerStoreStubResolver } from "./helpers/kit.ts";

registerStoreStubResolver();

const {
  fromApiOverride,
  resolveStoredExtraArgs,
  resolveStoredOverride,
  toApiOverride,
} = await import("../src/features/model-picker/api/model-overrides.ts");

const ARGS = ["--numa", "distribute"];

test("engine choices round trip and returning to Default clears a stale local choice", () => {
  const config = fromApiOverride({ engine: "sglang" });
  assert.equal(config.engine, "sglang");
  assert.equal(toApiOverride(config).engine, "sglang");
  assert.equal(toApiOverride({ ...config, engine: "auto" }).engine, "auto");
  assert.equal(fromApiOverride({}, config).engine, "auto");
});

test("an exact key wins", () => {
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "unsloth/Model-GGUF:q4_k_m": { llama_extra_args: ARGS } },
      ["unsloth/Model-GGUF:q4_k_m"],
    ),
    { tokens: ARGS, explicit: true },
  );
});

test("a repo id and its quant fold by case", () => {
  // The browser lowercases the quant before storing.
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "unsloth/Model-GGUF:Q4_K_M": { llama_extra_args: ARGS } },
      ["unsloth/model-gguf:q4_k_m"],
    ),
    { tokens: ARGS, explicit: true },
  );
});

test("a POSIX path stays case-sensitive", () => {
  // Distinct case-sensitive POSIX files must not share arguments.
  assert.deepEqual(
    resolveStoredExtraArgs({ "/models/Foo.gguf": { llama_extra_args: ARGS } }, [
      "/models/foo.gguf",
    ]),
    { tokens: [], explicit: false },
  );
});

test("a colon inside a POSIX filename is not a quant", () => {
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "/models/foo:bar.gguf": { llama_extra_args: ARGS } },
      ["/models/foo:Bar.gguf"],
    ),
    { tokens: [], explicit: false },
  );
});

test("a Windows path folds", () => {
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "C:\\Models\\Foo.gguf": { llama_extra_args: ARGS } },
      ["c:\\models\\foo.gguf"],
    ),
    { tokens: ARGS, explicit: true },
  );
});

test("a separator and a trailing slash do not make a different key", () => {
  for (const key of [
    "C:\\Models\\Foo.gguf",
    "c:/models/foo.gguf",
    "C:/Models/Foo.gguf/",
  ]) {
    assert.deepEqual(
      resolveStoredExtraArgs({ [key]: { llama_extra_args: ARGS } }, [
        "c:\\models\\foo.gguf",
      ]),
      { tokens: ARGS, explicit: true },
      key,
    );
  }
});

test("a UNC share folds however it is spelled", () => {
  // A forward-slash UNC path still starts with //, which the server treats as Windows.
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "\\\\Server\\Share\\Foo.gguf": { llama_extra_args: ARGS } },
      ["//server/share/foo.gguf"],
    ),
    { tokens: ARGS, explicit: true },
  );
});

test("a WSL drive mount folds like the Windows volume it is", () => {
  // /mnt/<letter> is a Windows path seen through Linux, so the server folds its case.
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "/mnt/c/models/foo.gguf": { llama_extra_args: ARGS } },
      ["/mnt/C/Models/Foo.gguf"],
    ),
    { tokens: ARGS, explicit: true },
  );
  assert.deepEqual(
    resolveStoredExtraArgs(
      { "/mnt/storage/models/foo.gguf": { llama_extra_args: ARGS } },
      ["/mnt/storage/models/Foo.gguf"],
    ),
    { tokens: [], explicit: false },
  );
});

test("two keys that fold together resolve to nothing", () => {
  // Ambiguous on purpose: picking by enumeration order would apply another model's settings.
  assert.deepEqual(
    resolveStoredExtraArgs(
      {
        "unsloth/Model-GGUF": { llama_extra_args: ARGS },
        "unsloth/model-gguf": { llama_extra_args: ["--top-k", "20"] },
      },
      ["unsloth/MODEL-gguf"],
    ),
    { tokens: [], explicit: false },
  );
});

test("the first entry that exists is the one read, fields and all", () => {
  // The server stops at the first non-empty override, even if it has no arguments.
  assert.deepEqual(
    resolveStoredExtraArgs(
      {
        "unsloth/model-gguf:q4_k_m": { max_seq_length: 4096 },
        "unsloth/model-gguf": { llama_extra_args: ARGS },
      },
      ["unsloth/model-gguf:q4_k_m", "unsloth/model-gguf"],
    ),
    { tokens: [], explicit: false },
  );
});

test("an empty entry is skipped rather than stopping the search", () => {
  assert.deepEqual(
    resolveStoredExtraArgs(
      {
        "unsloth/model-gguf:q4_k_m": {},
        "unsloth/model-gguf": { llama_extra_args: ARGS },
      },
      ["unsloth/model-gguf:q4_k_m", "unsloth/model-gguf"],
    ),
    { tokens: ARGS, explicit: true },
  );
});

test("no row at all is no arguments, not an error", () => {
  assert.deepEqual(resolveStoredExtraArgs({}, ["unsloth/model-gguf"]), {
    tokens: [],
    explicit: false,
  });
});

test("a row that carries an empty list is explicit, not absent", () => {
  // [] is the tombstone for a cleared box; reading it as absent lets /load resurrect old flags.
  assert.deepEqual(
    resolveStoredExtraArgs(
      {
        "unsloth/model-gguf:q4_k_m": { llama_extra_args: [] },
        "unsloth/model-gguf": { llama_extra_args: ARGS },
      },
      ["unsloth/model-gguf:q4_k_m", "unsloth/model-gguf"],
    ),
    { tokens: [], explicit: true },
  );
});

test("a matched row with other fields but no arguments is not explicit", () => {
  assert.deepEqual(
    resolveStoredExtraArgs({ "unsloth/model-gguf": { max_seq_length: 4096 } }, [
      "unsloth/model-gguf",
    ]),
    { tokens: [], explicit: false },
  );
});

test("the full resolved row uses the same identity rules as extra arguments", () => {
  const row = {
    custom_context_length: 32768,
    kv_cache_dtype: "q8_0",
  };
  assert.equal(
    resolveStoredOverride({ "unsloth/Model-GGUF:Q4_K_M": row }, [
      "unsloth/model-gguf:q4_k_m",
    ]),
    row,
  );
});

test("a server override converts into one normalized picker config", () => {
  const config = fromApiOverride({
    custom_context_length: 32768,
    kv_cache_dtype: "q8_0",
    n_parallel: 4,
    gpu_memory_mode: "manual",
    gpu_layers: 20,
    gpu_ids: [1],
    llama_extra_args: [],
  });
  assert.equal(config.customContextLength, 32768);
  assert.equal(config.kvCacheDtype, "q8_0");
  assert.equal(config.nParallel, 4);
  assert.equal(config.gpuMemoryMode, "manual");
  assert.equal(config.gpuLayers, 20);
  assert.deepEqual(config.selectedGpuIds, [1]);
  assert.equal(config.selectedGpuIndexKind, "physical");
  assert.deepEqual(config.llamaExtraArgs, []);
});

test("server hydration preserves a local Vulkan ordinal pin", () => {
  const local = fromApiOverride({});
  local.selectedGpuIds = [1];
  local.selectedGpuIndexKind = "vulkan";

  const config = fromApiOverride({ kv_cache_dtype: "q8_0" }, local);
  assert.equal(config.kvCacheDtype, "q8_0");
  assert.deepEqual(config.selectedGpuIds, [1]);
  assert.equal(config.selectedGpuIndexKind, "vulkan");
});

test("a server physical GPU pin replaces a local Vulkan pin", () => {
  const local = fromApiOverride({});
  local.selectedGpuIds = [1];
  local.selectedGpuIndexKind = "vulkan";

  const config = fromApiOverride({ gpu_ids: [0] }, local);
  assert.deepEqual(config.selectedGpuIds, [0]);
  assert.equal(config.selectedGpuIndexKind, "physical");
});

test("a server row states which index space its pin is in", () => {
  const vulkan = fromApiOverride({ gpu_ids: [1], gpu_index_kind: "vulkan" });
  assert.deepEqual(vulkan.selectedGpuIds, [1]);
  assert.equal(vulkan.selectedGpuIndexKind, "vulkan");
  // Absent stays physical so rows written before the field still read back.
  const legacy = fromApiOverride({ gpu_ids: [1] });
  assert.equal(legacy.selectedGpuIndexKind, "physical");
});

test("a pin travels to the server with its index space", () => {
  const physical = toApiOverride({
    ...fromApiOverride({}),
    selectedGpuIds: [1],
    selectedGpuIndexKind: "physical",
  });
  assert.deepEqual(physical.gpu_ids, [1]);
  assert.equal("gpu_index_kind" in physical, false);

  const vulkan = toApiOverride({
    ...fromApiOverride({}),
    selectedGpuIds: [1],
    selectedGpuIndexKind: "vulkan",
  });
  assert.deepEqual(vulkan.gpu_ids, [1]);
  assert.equal(vulkan.gpu_index_kind, "vulkan");
});

test("a row that carries less than the local config does not erase the rest", () => {
  // The server mirror is best-effort, so hydration must not overwrite local fields it lacks.
  const local = fromApiOverride({
    custom_context_length: 4096,
    kv_cache_dtype: "q8_0",
  });
  const config = fromApiOverride({ tensor_parallel: true }, local);
  assert.equal(config.customContextLength, 4096);
  assert.equal(config.kvCacheDtype, "q8_0");
  assert.equal(config.tensorParallel, true);
});

test("the row still wins for every field it does carry", () => {
  const local = fromApiOverride({ custom_context_length: 4096 });
  const config = fromApiOverride({ custom_context_length: 32768 }, local);
  assert.equal(config.customContextLength, 32768);
});

test("a cleared extra-arguments box survives a row that carries no arguments", () => {
  // [] is a decision that stops fallback to a broader row.
  const local = fromApiOverride({ llama_extra_args: [] });
  assert.deepEqual(local.llamaExtraArgs, []);
  const config = fromApiOverride({ kv_cache_dtype: "q8_0" }, local);
  assert.deepEqual(config.llamaExtraArgs, []);
});

for (const mode of ["tensor", "pipeline", "data"] as const) {
  test(`${mode} mode and GPU order round-trip through server overrides`, () => {
    const config = fromApiOverride({ engine: "vllm", engine_parallelism: mode, gpu_ids: [1, 0] });
    assert.equal(config.engineParallelism, mode);
    const saved = toApiOverride(config);
    assert.equal(saved.engine_parallelism, mode);
    assert.deepEqual(saved.gpu_ids, [1, 0]);
  });
}