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.

11.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

import assert from "node:assert/strict";
import test from "node:test";
import ts from "@typescript/typescript6";
import { explicitFamily } from "../src/features/model-picker/components/model-selector/family-override.ts";
import {
  componentFilesMatch,
  matchesRememberedModel,
  forgetStaleImageComponents,
  readImageModel,
  recalledComponentFiles,
  rememberImageModel,
} from "../src/features/images/image-model-recall.ts";
import { splitComponentFileList } from "../src/features/images/component-files.ts";
import { installLocalStorageFake, readSrc } from "./helpers/kit.ts";

const { storage } = installLocalStorageFake();

test("recalling a quantized model carries the selected adapters into its load", async () => {
  const source = readSrc("features/images/images-page.tsx");
  const tree = ts.createSourceFile(
    "images-page.tsx",
    source,
    ts.ScriptTarget.Latest,
    true,
    ts.ScriptKind.TSX,
  );
  const names = new Set([
    "bakedLorasFor",
    "currentLoadAdvanced",
    "handleGenerateWithRecall",
  ]);
  const declarations: string[] = [];
  function visit(node: ts.Node) {
    if (ts.isVariableDeclaration(node) && names.has(node.name.getText(tree)))
      declarations.push(`const ${node.getText(tree)};`);
    ts.forEachChild(node, visit);
  }
  visit(tree);
  assert.equal(declarations.length, names.size);
  const { outputText } = ts.transpileModule(
    declarations.join("\n") +
      "\nreturn { handleGenerateWithRecall, currentLoadAdvanced };",
    { compilerOptions: { target: ts.ScriptTarget.ES2022 } },
  );
  const recall = (overrides: Record<string, unknown>) => {
    const loads: unknown[][] = [];
    const scope = {
      useCallback: (fn: unknown) => fn,
      lastLoad: { current: null },
      status: { loaded: false, repo_id: null },
      loras: [],
      familyOverride: "auto",
      explicitFamily,
      cpuOffload: false,
      speedMode: "auto",
      transformerQuant: "auto",
      textEncoderQuant: "auto",
      attentionBackend: "auto",
      memoryMode: "auto",
      transformerCache: "auto",
      selectedGpu: "auto",
      gpuChoices: [],
      textEncoderFiles: "",
      vaeFile: "",
      splitComponentFileList,
      recalledComponentFiles,
      forgetStaleImageComponents,
      busy: null,
      external: null,
      imagePresets: { hydrated: true },
      prompt: "a teapot",
      pendingRecalledGeneration: { current: null },
      oversizedOnce: { current: false },
      loadSeq: { current: 3 },
      workflow: "txt2img",
      handleGenerate: () => {
        throw new Error("generation must wait for the load");
      },
      handleLoad: async (...args: unknown[]) => {
        loads.push(args);
        return true;
      },
      ...overrides,
    };
    const callbacks = new Function(...Object.keys(scope), outputText)(
      ...Object.values(scope),
    );
    return { scope, loads, callbacks };
  };
  for (const quant of ["int8", "fp8"]) {
    const model = {
      repoId: "unsloth/model-GGUF",
      kind: "gguf",
      filename: "model-Q8_0.gguf",
    };
    const { scope, loads, callbacks } = recall({
      loras: [
        { id: " org/style ", weight: 0.7 },
        { id: "", weight: 1 },
        { id: "org/off", weight: 0 },
      ],
      familyOverride: "qwen-image",
      transformerQuant: quant,
      textEncoderQuant: "int8",
      rememberedModel: model,
    });
    await callbacks.handleGenerateWithRecall();
    const pending = scope.pendingRecalledGeneration.current as { allowOversized?: boolean } | null;
    assert.equal(pending?.allowOversized, false);
    assert.equal(loads.length, 1);
    assert.equal(loads[0][0], model.repoId);
    assert.deepEqual(loads[0][1], {
      kind: model.kind,
      filename: model.filename,
    });
    const advanced = loads[0][2] as
      | {
          loras?: unknown;
          transformer_quant?: string;
          text_encoder_quant?: string;
          family_override?: string;
        }
      | undefined;
    assert.deepEqual(advanced?.loras, [{ id: "org/style", weight: 0.7 }]);
    assert.equal(advanced?.transformer_quant, quant);
    assert.equal(advanced?.text_encoder_quant, "int8");
    assert.equal(advanced?.family_override, undefined);
    assert.equal(
      callbacks.currentLoadAdvanced("org/different-model").loras,
      undefined,
    );
  }

  const { loads, callbacks } = recall({
    rememberedModel: { repoId: "/cache/models--org--custom/snapshots/abc", kind: "pipeline", familyOverride: "flux.1" },
  });
  await callbacks.handleGenerateWithRecall();
  assert.equal((loads[0][2] as { family_override?: string }).family_override, "flux.1");

  // A recalled build loads with its own encoder / VAE files, never the live fields.
  type Files = { text_encoder_file?: string[]; vae_file?: string };
  const supplied = {
    repoId: "/comfy/models/diffusion_models",
    kind: "gguf",
    filename: "z.gguf",
    textEncoderFiles: ["../text_encoders/qwen_3_4b.safetensors"],
    vaeFile: "../vae/ae.safetensors",
  };
  const withFiles = recall({ rememberedModel: supplied, textEncoderFiles: "live.safetensors", vaeFile: "live_vae.safetensors" });
  await withFiles.callbacks.handleGenerateWithRecall();
  assert.deepEqual((withFiles.loads[0][2] as Files).text_encoder_file, supplied.textEncoderFiles);
  assert.equal((withFiles.loads[0][2] as Files).vae_file, supplied.vaeFile);
  // After a refresh with the model still resident, the stored paths survive (status names only basenames).
  const resident = recall({
    rememberedModel: supplied,
    status: {
      loaded: true,
      repo_id: supplied.repoId,
      model_kind: "gguf",
      gguf_filename: "z.gguf",
      component_files: { text_encoder: "qwen_3_4b.safetensors", vae: "ae.safetensors" },
    },
    matchesRememberedModel,
    componentFilesMatch,
    withEngagedFamily: (m: Record<string, unknown>) => m,
    rememberImageModel: (m: unknown) => storage.setItem("unsloth:images:last-model", JSON.stringify(m)),
    setRememberedModel: () => {},
    handleGenerate: async () => {},
  });
  await resident.callbacks.handleGenerateWithRecall();
  assert.deepEqual(readImageModel()?.textEncoderFiles, supplied.textEncoderFiles);
  assert.equal(readImageModel()?.vaeFile, supplied.vaeFile);
  // Another client reloaded the same checkpoint without the files: the stale paths are dropped.
  const reloaded = recall({
    rememberedModel: supplied,
    status: { loaded: true, repo_id: supplied.repoId, model_kind: "gguf", gguf_filename: "z.gguf", component_files: null },
    matchesRememberedModel,
    componentFilesMatch,
    withEngagedFamily: (m: Record<string, unknown>) => m,
    rememberImageModel: (m: unknown) => storage.setItem("unsloth:images:last-model", JSON.stringify(m)),
    setRememberedModel: () => {},
    handleGenerate: async () => {},
  });
  await reloaded.callbacks.handleGenerateWithRecall();
  assert.equal(readImageModel()?.textEncoderFiles, undefined);
  assert.equal(readImageModel()?.vaeFile, undefined);
  // Same for this tab's own last load once the resident build no longer uses its files.
  const ownStale = recall({
    rememberedModel: null,
    lastLoad: { current: supplied },
    status: { loaded: true, repo_id: supplied.repoId, model_kind: "gguf", gguf_filename: "z.gguf", component_files: null },
    matchesRememberedModel,
    componentFilesMatch,
    withEngagedFamily: (m: Record<string, unknown>) => m,
    rememberImageModel: (m: unknown) => storage.setItem("unsloth:images:last-model", JSON.stringify(m)),
    setRememberedModel: () => {},
    handleGenerate: async () => {},
  });
  await ownStale.callbacks.handleGenerateWithRecall();
  assert.equal(readImageModel()?.textEncoderFiles, undefined);
  const without = recall({ rememberedModel: { ...supplied, textEncoderFiles: undefined, vaeFile: undefined }, textEncoderFiles: "live.safetensors" });
  await without.callbacks.handleGenerateWithRecall();
  assert.equal((without.loads[0][2] as Files).text_encoder_file, undefined);
  assert.equal((without.loads[0][2] as Files).vae_file, undefined);
});

test("recall keeps supplied encoder / VAE file paths", () => {
  const model = {
    repoId: "/comfy/models/diffusion_models",
    kind: "gguf" as const,
    filename: "z.gguf",
    textEncoderFiles: ["../text_encoders/clip_l.safetensors", "../text_encoders/t5xxl.safetensors"],
    vaeFile: "../vae/ae.safetensors",
  };
  rememberImageModel(model);
  assert.deepEqual(readImageModel(), model);
  storage.setItem("unsloth:images:last-model", JSON.stringify({ ...model, textEncoderFiles: [1, ""], vaeFile: 2 }));
  const read = readImageModel();
  assert.equal(read?.textEncoderFiles, undefined);
  assert.equal(read?.vaeFile, undefined);
});

test("recall keeps an explicit family and drops Auto", () => {
  const model = {
    repoId: "/cache/models--org--custom/snapshots/abc",
    kind: "pipeline" as const,
    familyOverride: "flux.1",
  };
  rememberImageModel(model);
  assert.deepEqual(readImageModel(), model);
  rememberImageModel({ ...model, familyOverride: "auto" });
  assert.equal(readImageModel()?.familyOverride, undefined);
});

test("recall keeps the exact GGUF artifact", () => {
  const model = {
    repoId: "unsloth/model-GGUF",
    kind: "gguf" as const,
    filename: "model-Q4.gguf",
  };
  rememberImageModel(model);
  assert.deepEqual(readImageModel(), model);
  assert.equal(
    matchesRememberedModel(model, {
      loaded: true,
      repo_id: model.repoId,
      model_kind: "gguf",
      gguf_filename: "model-Q8.gguf",
    }),
    false,
  );
  assert.equal(
    matchesRememberedModel(model, {
      loaded: true,
      repo_id: model.repoId,
      model_kind: "gguf",
      gguf_filename: model.filename,
    }),
    true,
  );
});

test("invalid or incomplete stored targets cannot trigger automatic loading", () => {
  for (const value of [
    "broken",
    "null",
    "{}",
    '{"repoId":"org/model","kind":"gguf"}',
    '{"repoId":"org/model","kind":"unknown"}',
  ]) {
    storage.setItem("unsloth:images:last-model", value);
    assert.equal(readImageModel(), null);
  }
});

test("a new model pick cancels recalled generation and retires the staged load", () => {
  const source = readSrc("features/images/images-page.tsx");
  const tree = ts.createSourceFile(
    "images-page.tsx",
    source,
    ts.ScriptTarget.Latest,
    true,
    ts.ScriptKind.TSX,
  );
  let declaration: ts.VariableDeclaration | undefined;
  function visit(node: ts.Node) {
    if (ts.isVariableDeclaration(node) && node.name.getText(tree) === "beginPick") {
      assert.equal(declaration, undefined, "beginPick must have one owner");
      declaration = node;
    }
    ts.forEachChild(node, visit);
  }
  visit(tree);
  assert.ok(declaration);
  const { outputText } = ts.transpileModule(
    `const ${declaration.getText(tree)}; beginPick();`,
    { compilerOptions: { target: ts.ScriptTarget.ES2022 } },
  );
  const scope = {
    useCallback: (fn: unknown) => fn,
    pendingRecalledGeneration: { current: { model: "previous" } },
    pickSeq: { current: 4 },
    pendingStagedLoad: { current: { token: 4 } },
    pendingLoadEntries: { current: ["previous"] },
    stagedLoadDeferred: { current: true },
    stagedQuantRevert: { current: { prev: "Q8_0" } },
    pickToast: { dismissed: 0, dismissAll() { this.dismissed += 1; } },
    // An older pick's Load anyway toast and dialog retire with it.
    loadAnyway: { dismissed: 0 },
    dismissLoadAnyway: () => {
      scope.loadAnyway.dismissed += 1;
    },
  };
  new Function(...Object.keys(scope), outputText)(...Object.values(scope));
  assert.equal(scope.pickToast.dismissed, 1);
  assert.equal(scope.loadAnyway.dismissed, 1);
  assert.equal(scope.pendingRecalledGeneration.current, null);
  assert.equal(scope.pickSeq.current, 5);
  assert.equal(scope.pendingStagedLoad.current, null);
  assert.equal(scope.pendingLoadEntries.current, null);
  assert.equal(scope.stagedLoadDeferred.current, false);
  assert.equal(scope.stagedQuantRevert.current, null);
});