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.

4.8 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 { registerBundlerResolver } from "./helpers/kit.ts";

registerBundlerResolver();
const { initialTrainingConfigState } = await import(
  "../src/features/training/stores/training-config-policy.ts"
);
const { mergeTrainingConfig } = await import(
  "../src/features/training/stores/training-config-persistence.ts"
);
const { buildTrainingMethodPatch } = await import(
  "../src/features/training/stores/training-method-transition.ts"
);

test("rehydrated CPT provenance survives the next method switch", () => {
  const serialized = JSON.stringify({
    trainingMethod: "cpt",
    learningRate: 0.000031,
    datasetFormat: "raw",
    trainingMethodProvenance: {
      learningRateManuallySet: true,
      modelAdapterLearningRate: 0.00001,
      datasetFormatBeforeCpt: "sharegpt",
      targetModulesBeforeCpt: null,
      trainOnCompletionsBeforeCpt: true,
    },
  });
  const rehydrated = mergeTrainingConfig(
    JSON.parse(serialized),
    initialTrainingConfigState as never,
  );
  const state = {
    ...rehydrated,
    ...buildTrainingMethodPatch(rehydrated, "qlora"),
  };

  assert.equal(state.trainingMethod, "qlora");
  assert.equal(state.learningRate, 0.000031);
  assert.equal(state.datasetFormat, "sharegpt");
  assert.equal(state.trainOnCompletions, true);
  assert.equal(state.trainingMethodProvenance.datasetFormatBeforeCpt, null);
});

test("rehydrated model learning rate is restored for adapter methods", () => {
  const serialized = JSON.stringify({
    trainingMethod: "full",
    learningRate: 0.00002,
    datasetFormat: "auto",
    trainingMethodProvenance: {
      learningRateManuallySet: false,
      modelAdapterLearningRate: 0.00001,
      datasetFormatBeforeCpt: null,
      targetModulesBeforeCpt: null,
    },
  });
  const rehydrated = mergeTrainingConfig(
    JSON.parse(serialized),
    initialTrainingConfigState as never,
  );
  const state = {
    ...rehydrated,
    ...buildTrainingMethodPatch(rehydrated, "lora"),
  };

  assert.equal(state.learningRate, 0.00001);
});

test("CPT preserves all-linear model defaults instead of Llama target names", () => {
  const state = {
    ...initialTrainingConfigState,
    trainingMethod: "qlora" as const,
    targetModules: ["all-linear"],
    datasetFormat: "chatml" as const,
  };
  const patch = buildTrainingMethodPatch(state, "cpt");

  assert.deepEqual(patch.targetModules, [
    "all-linear",
    "embed_tokens",
    "lm_head",
  ]);
  assert.equal(patch.trainingMethodProvenance?.targetModulesBeforeCpt?.[0], "all-linear");
});

test("switching away from CPT restores pre-CPT target modules", () => {
  const state = {
    ...initialTrainingConfigState,
    trainingMethod: "cpt" as const,
    targetModules: ["all-linear", "embed_tokens", "lm_head"],
    datasetFormat: "raw" as const,
    trainingMethodProvenance: {
      learningRateManuallySet: false,
      modelAdapterLearningRate: null,
      datasetFormatBeforeCpt: "chatml" as const,
      targetModulesBeforeCpt: ["all-linear"],
      loraRankBeforeCpt: null,
      loraAlphaBeforeCpt: null,
      loraVariantBeforeCpt: null,
      trainOnCompletionsBeforeCpt: null,
    },
  };
  const patch = buildTrainingMethodPatch(state, "qlora");

  assert.deepEqual(patch.targetModules, ["all-linear"]);
  assert.equal(patch.datasetFormat, "chatml");
  assert.equal(patch.trainingMethodProvenance?.targetModulesBeforeCpt, null);
});

test("switching away from CPT restores the pre-CPT LoRA rank, alpha and variant", () => {
  const state = {
    ...initialTrainingConfigState,
    trainingMethod: "qlora" as const,
    loraRank: 8,
    loraAlpha: 8,
    loraVariant: "lora" as const,
  };
  const cptState = { ...state, ...buildTrainingMethodPatch(state, "cpt") };

  assert.equal(cptState.loraRank, 128);
  assert.equal(cptState.trainingMethodProvenance.loraRankBeforeCpt, 8);

  const restored = {
    ...cptState,
    ...buildTrainingMethodPatch(cptState, "qlora"),
  };

  assert.equal(restored.loraRank, 8);
  assert.equal(restored.loraAlpha, 8);
  assert.equal(restored.loraVariant, "lora");
  assert.equal(restored.trainingMethodProvenance.loraRankBeforeCpt, null);
});

test("switching away from CPT turns train on completions back on", () => {
  const state = {
    ...initialTrainingConfigState,
    trainingMethod: "qlora" as const,
    datasetFormat: "auto" as const,
    trainOnCompletions: true,
  };
  const cptState = { ...state, ...buildTrainingMethodPatch(state, "cpt") };

  assert.equal(cptState.trainOnCompletions, false);

  const restored = {
    ...cptState,
    ...buildTrainingMethodPatch(cptState, "qlora"),
  };

  assert.equal(restored.trainOnCompletions, true);
  assert.equal(
    restored.trainingMethodProvenance.trainOnCompletionsBeforeCpt,
    null,
  );
});