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.

5.3 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

/**
 * `memoryTotalGb` is `total_memory / 1024**3`, so its label must say GiB; the field name
 * cannot change without churning the API.
 */

import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import test from "node:test";

const SRC = new URL("../src/", import.meta.url);

const RENDERERS = [
  "features/model-picker/components/model-config-page.tsx",
  "features/images/images-page.tsx",
  "features/video/video-page.tsx",
  "features/studio/wizard/run-preview-card.tsx",
  "features/hub/hub-page.tsx",
  "features/settings/tabs/about-tab.tsx",
];

// `[^`$]*` keeps the match inside one literal segment, so an unrelated "GB"
// later in the file cannot be paired with an earlier total.
const SUFFIXED = /\$\{[^}]*(?:memoryTotalGb|vramTotalGb|systemRamTotalGb|gpuMemoryGb)[^}]*\}[^`$]*?\b(GiB|GB)\b/g;

test("VRAM and RAM totals are labelled GiB, not GB", () => {
  const offenders: string[] = [];

  for (const relative of RENDERERS) {
    const source = readFileSync(new URL(relative, SRC), "utf8");
    const matches = [...source.matchAll(SUFFIXED)];

    // Per file, not summed, so one renderer cannot drop to zero matches.
    assert.ok(matches.length > 0, `${relative}: no labelled total found`);

    for (const match of matches) {
      if (match[1] !== "GiB") {
        offenders.push(`${relative}: ${match[0].trim()}`);
      }
    }
  }

  assert.deepEqual(
    offenders,
    [],
    `these render a GiB value with a GB suffix:\n${offenders.join("\n")}`,
  );
});

// The floating monitor formats through monitor-memory.tsx.
const HELPER_RENDERERS = [
  "components/monitor-memory.tsx",
  "features/settings/tabs/resources-tab.tsx",
];

// `{est}` stays decimal GB: estimateLoadingVram divides by 1e9.
const LOCALES = new URL("../src/i18n/locales/", import.meta.url);
// Binary first: GiB has to win the alternation before GB can match its tail.
const UNIT = /\{total\}\s*(GiB|Gio|ГиБ|GB|Go|ГБ)/;
const BINARY_UNITS = ["GiB", "Gio", "ГиБ"];

test("localized GPU totals name a binary unit", () => {
  const files = readdirSync(LOCALES).filter((f) => f.endsWith(".ts"));
  assert.ok(files.length > 0, "no locales found");

  for (const file of files) {
    const source = readFileSync(new URL(file, LOCALES), "utf8");
    for (const line of source.split("\n")) {
      if (!/\b(vramNeeds|vramTight):/.test(line)) continue;
      const unit = line.match(UNIT);
      assert.ok(unit, `${file}: no unit after {total} in ${line.trim()}`);
      assert.ok(
        BINARY_UNITS.includes(unit[1]),
        `${file}: {total} is binary but is labelled ${unit[1]}`,
      );
    }
  }
});

test("helper-formatted totals pick the GiB helper, not the disk one", () => {
  for (const relative of HELPER_RENDERERS) {
    const source = readFileSync(new URL(relative, SRC), "utf8");

    const helper = source.match(/function formatGiB[\s\S]*?\n}/);
    assert.ok(helper, `${relative}: no formatGiB helper`);
    assert.match(helper[0], /`.*GiB`/, `${relative}: formatGiB does not suffix GiB`);

    // formatGb is the decimal disk one, so no memory value may reach it.
    for (const call of source.matchAll(/(?<!function )formatGb\(([^)]*)\)/g)) {
      assert.match(call[1], /disk/i, `${relative}: ${call[0]} formats memory as decimal GB`);
    }
  }
});

// Both memory surfaces format through one module dividing by 1024 throughout. Listed by name
// so deleting a formatter fails here.
const BINARY_FORMATTERS: [string, string][] = [
  ["lib/memory/format.ts", "formatGiB"],
  ["lib/memory/format.ts", "formatBytesGiB"],
  ["lib/memory/format.ts", "formatKvRate"],
];

test("every shared memory formatter names a binary unit", () => {
  for (const [relative, fn] of BINARY_FORMATTERS) {
    const source = readFileSync(new URL(relative, SRC), "utf8");
    const helper = source.match(
      new RegExp(`export function ${fn}[\\s\\S]*?\\n}`),
    );
    assert.ok(helper, `${relative}: no ${fn}`);
    assert.doesNotMatch(
      helper[0],
      /(?<!Ki|Mi|Gi)B["'`]/,
      `${relative}: ${fn} labels a binary value with a decimal unit`,
    );
    assert.match(
      helper[0],
      /(KiB|MiB|GiB)/,
      `${relative}: ${fn} names no binary unit at all`,
    );
  }
});

// Back-compat aliases could be redefined as functions and shadow the shared formatter.
const ALIAS_ONLY = [
  "lib/model-memory.ts",
  "features/model-picker/model-config/memory-fit.ts",
];

test("the surfaces alias the shared formatters rather than redefining them", () => {
  for (const relative of ALIAS_ONLY) {
    const source = readFileSync(new URL(relative, SRC), "utf8");
    assert.doesNotMatch(
      source,
      /export function formatMemoryGb\b/,
      `${relative}: formatMemoryGb is defined here again. There were once two of ` +
        `these, with the same name and signature but different input units and ` +
        `different labels, and importing the wrong one typechecked cleanly.`,
    );
    assert.doesNotMatch(
      source,
      /export function formatKvRate\b/,
      `${relative}: formatKvRate is defined here again`,
    );
    assert.match(
      source,
      /from "\.\.?\/[^"]*memory\/format\.ts"/,
      `${relative}: no longer sources its formatters from the shared module`,
    );
  }
});