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.

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

import {
  CPT_LORA_HYPERPARAMS,
  CPT_TARGET_MODULES,
  DEFAULT_HYPERPARAMS,
  LR_DEFAULT_CPT,
  LR_DEFAULT_FULL,
  LR_DEFAULT_LORA,
} from "@/config/training";
// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's unrelated React exports.
import { stageLegacyHfTokenForMigration } from "@/features/hub/stores/hf-token-store";
import { isTrainingMethod } from "@/types/training";
import type { DatasetFormat } from "@/types/training";
import type {
  LoraVariant,
  TrainingConfigState,
  TrainingConfigStore,
  TrainingMethodProvenance,
} from "../types/config";
import { drafterStateFrom } from "../lib/drafter-config";
import {
  createHfBrowseDatasetSelection,
  createUploadBrowseDatasetSelection,
  datasetSourceInvariantPatch,
} from "./training-config-policy";

export const TRAINING_CONFIG_PERSISTENCE_NAME = "unsloth_training_config_v1";
export const TRAINING_CONFIG_PERSISTENCE_VERSION = 24;

const NON_PERSISTED_STATE_KEYS: ReadonlySet<keyof TrainingConfigState> =
  new Set([
    "userEditRevision",
    "isCheckingVision",
    "isLoadingModelDefaults",
    "modelDefaultsError",
    "isCheckingDataset",
    "isDatasetImage",
    "isDatasetAudio",
    "datasetCheckFailed",
    "manualDatasetOptionsValid",
    "trainOnCompletionsDefaultPendingFor",
    "maxPositionEmbeddings",
    "decisionCheckpoints",
    "decisionLayout",
    "s3Config",
    "wandbToken",
    "drafterCapability",
  ]);

export function partializeTrainingConfig(
  state: TrainingConfigStore,
): Partial<TrainingConfigStore> {
  const partial = Object.fromEntries(
    Object.entries(state).filter(([key, value]) => {

      if (key === "hfToken") return false;
      if (typeof value === "function") {
        return false;
      }
      if (
        key === "trainOnCompletions" &&
        typeof state.trainOnCompletionsDefaultPendingFor === "string" &&
        state.trainOnCompletionsDefaultPendingFor === state.selectedModel
      ) {
        return false;
      }
      return !NON_PERSISTED_STATE_KEYS.has(key as keyof TrainingConfigState);
    }),
  ) as Partial<TrainingConfigStore>;
  return {
    ...partial,
    ...datasetSourceInvariantPatch(state),
  };
}

type PersistedTrainingConfig = Record<string, unknown>;

function migrateThroughVersion8(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (
    version < 2 &&
    state.datasetSubset == null &&
    state.datasetConfig != null
  ) {
    state.datasetSubset = state.datasetConfig;
  }
  Reflect.deleteProperty(state, "datasetConfig");
  if (version < 3 && state.modelDefaultsAppliedFor == null) {
    state.modelDefaultsAppliedFor = null;
  }
  if (version < 4 && state.optimizerType == null) {
    state.optimizerType = DEFAULT_HYPERPARAMS.optimizerType;
  }
  if (version < 5 && state.lrSchedulerType == null) {
    state.lrSchedulerType = DEFAULT_HYPERPARAMS.lrSchedulerType;
  }
  if (version < 6 && state.datasetEvalSplit == null) {
    state.datasetEvalSplit = null;
  }
  if (version < 7) {
    state.datasetSliceStart ??= null;
    state.datasetSliceEnd ??= null;
  }
  if (version < 8) {
    state.datasetSystemPrompt ??= "";
    state.datasetLabelMapping ??= {};
    state.datasetAdvisorNotification ??= null;
  }
}

function migrateThroughVersion12(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 9 && state.weightDecay === 0.01) {
    state.weightDecay = DEFAULT_HYPERPARAMS.weightDecay;
  }
  if (version < 10 && state.trainingMethod === "cpt") {
    state.loraRank = 128;
    state.loraAlpha = 32;
    state.loraVariant = "rslora";
    state.targetModules = CPT_TARGET_MODULES;
    state.datasetFormat = "raw";
    if (state.learningRate == null || state.learningRate === LR_DEFAULT_LORA) {
      state.learningRate = LR_DEFAULT_CPT;
    }
  }
  if (version < 11) {
    state.datasetStreaming ??= false;
  }
  if (version < 12) {
    const legacyToken =
      typeof state.hfToken === "string" ? state.hfToken.trim() : "";
    if (legacyToken) {
      stageLegacyHfTokenForMigration(legacyToken);
    }
    // Keep the legacy value until authenticated migration confirms the backend write.
  }
}

function migrateThroughVersion16(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 13) {
    state.modelKnownCached ??= false;
    state.modelLocalPath ??= null;
    state.modelFormat ??= null;
    state.datasetKnownCached ??= false;
    state.datasetLocalPath ??= null;
  }
  if (version < 14) {
    const dataset = typeof state.dataset === "string" ? state.dataset : null;
    const uploadedFile =
      typeof state.uploadedFile === "string" ? state.uploadedFile : null;
    state.browseDatasetSelection =
      state.datasetSource === "upload"
        ? createUploadBrowseDatasetSelection(uploadedFile)
        : createHfBrowseDatasetSelection(dataset, {
            knownCached: state.datasetKnownCached === true,
            localPath:
              typeof state.datasetLocalPath === "string"
                ? state.datasetLocalPath
                : null,
          });
  }
  if (version < 15) {
    state.isEmbeddingModel = state.modelType === "embeddings";
  }
  if (version < 16) {
    state.datasetUserTemplate = undefined;
    state.datasetAssistantTemplate = undefined;
  }
}

function migrateThroughVersion17(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 17) {
    state.modelDefaultsAppliedFor =
      typeof state.selectedModel === "string" && state.selectedModel.length > 0
        ? state.selectedModel
        : null;
    state.advancedSettingsBaseline = null;
  }
}

function migrateThroughVersion18(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 18) {
    Reflect.deleteProperty(state, "wandbToken");
  }
}

function defaultLearningRateForMethod(method: unknown): number {
  if (method === "full") {
    return LR_DEFAULT_FULL;
  }
  if (method === "cpt") {
    return LR_DEFAULT_CPT;
  }
  return LR_DEFAULT_LORA;
}

function inferLegacyLearningRateWasManuallySet(
  state: PersistedTrainingConfig,
): boolean {
  return (
    typeof state.learningRate === "number" &&
    Number.isFinite(state.learningRate) &&
    state.learningRate !== defaultLearningRateForMethod(state.trainingMethod)
  );
}

function migrateThroughVersion19(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 19) {
    state.trainingMethodProvenance = {
      learningRateManuallySet: inferLegacyLearningRateWasManuallySet(state),
      modelAdapterLearningRate: null,
      datasetFormatBeforeCpt: null,
      targetModulesBeforeCpt: null,
      loraRankBeforeCpt: null,
      loraAlphaBeforeCpt: null,
      loraVariantBeforeCpt: null,
      trainOnCompletionsBeforeCpt: null,
    } satisfies TrainingMethodProvenance;
  }
}

function migrateThroughVersion21(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version < 21) {
    // currentStep belonged to the removed onboarding wizard and would otherwise persist forever.
    Reflect.deleteProperty(state, "currentStep");
  }
}

// Recover the pre-CPT slots from the defaults advancedSettingsBaseline froze.
function migrateThroughVersion22(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version >= 22 || state.trainingMethod !== "cpt") return;
  const provenance = state.trainingMethodProvenance;
  if (typeof provenance !== "object" || provenance === null) return;
  // mergeTrainingConfig's own identity test, empty string included.
  if (
    typeof state.modelDefaultsAppliedFor !== "string" ||
    state.modelDefaultsAppliedFor.length === 0 ||
    state.modelDefaultsAppliedFor !== state.selectedModel
  ) {
    return;
  }
  const baseline = state.advancedSettingsBaseline;
  if (typeof baseline !== "object" || baseline === null) return;
  const { loraRank, loraAlpha, loraVariant } = baseline as Record<
    string,
    unknown
  >;
  // Exactly the CPT triple: captured after CPT applied, so it says nothing.
  if (
    loraRank === CPT_LORA_HYPERPARAMS.loraRank &&
    loraAlpha === CPT_LORA_HYPERPARAMS.loraAlpha &&
    loraVariant === CPT_LORA_HYPERPARAMS.loraVariant
  ) {
    return;
  }
  // Gaps only: a real pre-CPT edit outranks the defaults the baseline froze.
  const record = provenance as Record<string, unknown>;
  if (positiveIntOrNull(record.loraRankBeforeCpt) === null) {
    record.loraRankBeforeCpt = positiveIntOrNull(loraRank);
  }
  if (positiveIntOrNull(record.loraAlphaBeforeCpt) === null) {
    record.loraAlphaBeforeCpt = positiveIntOrNull(loraAlpha);
  }
  if (!isLoraVariant(record.loraVariantBeforeCpt)) {
    record.loraVariantBeforeCpt = isLoraVariant(loraVariant)
      ? loraVariant
      : null;
  }
}

// Drafter training arrived in 23: older state has no drafter keys, and a hand-edited or
// partially written one must never turn the phase on.
function migrateThroughVersion23(
  state: PersistedTrainingConfig,
  version: number,
): void {
  if (version >= 23) return;
  Object.assign(
    state,
    drafterStateFrom({
      enabled: state.drafterEnabled,
      strategy: state.drafterStrategy,
      data_source: state.drafterDataSource,
      max_examples: state.drafterMaxExamples,
      epochs: state.drafterEpochs,
      learning_rate: state.drafterLearningRate,
      draft_steps: state.drafterDraftSteps,
    }),
  );
}

function isDatasetFormat(value: unknown): value is DatasetFormat {
  return (
    value === "auto" ||
    value === "alpaca" ||
    value === "chatml" ||
    value === "sharegpt" ||
    value === "raw"
  );
}

function isLoraVariant(value: unknown): value is LoraVariant {
  return (
    value === "lora" ||
    value === "rslora" ||
    value === "loftq" ||
    value === "dora"
  );
}

function positiveIntOrNull(value: unknown): number | null {
  return typeof value === "number" && Number.isInteger(value) && value > 0
    ? value
    : null;
}

function normalizeTrainingMethodProvenance(
  value: unknown,
  persistedState: PersistedTrainingConfig,
): TrainingMethodProvenance {
  const provenance =
    value !== null && typeof value === "object"
      ? (value as Partial<TrainingMethodProvenance>)
      : {};
  const modelAdapterLearningRate = provenance.modelAdapterLearningRate;
  const datasetFormatBeforeCpt = provenance.datasetFormatBeforeCpt;
  const targetModulesBeforeCpt = provenance.targetModulesBeforeCpt;
  const wasCpt = persistedState.trainingMethod === "cpt";
  return {
    learningRateManuallySet:
      typeof provenance.learningRateManuallySet === "boolean"
        ? provenance.learningRateManuallySet
        : inferLegacyLearningRateWasManuallySet(persistedState),
    modelAdapterLearningRate:
      typeof modelAdapterLearningRate === "number" &&
      Number.isFinite(modelAdapterLearningRate) &&
      modelAdapterLearningRate > 0
        ? modelAdapterLearningRate
        : null,
    datasetFormatBeforeCpt:
      wasCpt &&
      isDatasetFormat(datasetFormatBeforeCpt) &&
      datasetFormatBeforeCpt !== "raw"
        ? datasetFormatBeforeCpt
        : null,
    targetModulesBeforeCpt:
      wasCpt &&
      Array.isArray(targetModulesBeforeCpt) &&
      targetModulesBeforeCpt.length > 0
        ? [...targetModulesBeforeCpt]
        : null,
    loraRankBeforeCpt: wasCpt
      ? positiveIntOrNull(provenance.loraRankBeforeCpt)
      : null,
    loraAlphaBeforeCpt: wasCpt
      ? positiveIntOrNull(provenance.loraAlphaBeforeCpt)
      : null,
    loraVariantBeforeCpt:
      wasCpt && isLoraVariant(provenance.loraVariantBeforeCpt)
        ? provenance.loraVariantBeforeCpt
        : null,
    trainOnCompletionsBeforeCpt:
      wasCpt && typeof provenance.trainOnCompletionsBeforeCpt === "boolean"
        ? provenance.trainOnCompletionsBeforeCpt
        : null,
  };
}

function normalizeSettingsBeforeDecision(
  value: unknown,
): TrainingConfigState["settingsBeforeDecision"] {
  if (value === null || typeof value !== "object") return null;
  const { trainingMethod, datasetStreaming } = value as Record<string, unknown>;
  return isTrainingMethod(trainingMethod)
    ? { trainingMethod, datasetStreaming: datasetStreaming === true }
    : null;
}

export function migrateTrainingConfig(
  persisted: unknown,
  version: number,
): TrainingConfigStore {
  const state = persisted as PersistedTrainingConfig;
  migrateThroughVersion8(state, version);
  migrateThroughVersion12(state, version);
  migrateThroughVersion16(state, version);
  migrateThroughVersion17(state, version);
  migrateThroughVersion18(state, version);
  migrateThroughVersion19(state, version);
  migrateThroughVersion21(state, version);
  migrateThroughVersion22(state, version);
  migrateThroughVersion23(state, version);
  return state as unknown as TrainingConfigStore;
}

export function mergeTrainingConfig(
  persisted: unknown,
  current: TrainingConfigStore,
): TrainingConfigStore {
  const persistedRecord = { ...(persisted as PersistedTrainingConfig) };
  delete persistedRecord.hfToken;
  const persistedState = persistedRecord as Partial<TrainingConfigState>;
  const modelDefaultsAppliedFor =
    typeof persistedState.modelDefaultsAppliedFor === "string" &&
    persistedState.modelDefaultsAppliedFor.length > 0 &&
    persistedState.modelDefaultsAppliedFor === persistedState.selectedModel
      ? persistedState.modelDefaultsAppliedFor
      : null;
  const advancedSettingsBaseline = modelDefaultsAppliedFor
    ? (persistedState.advancedSettingsBaseline ?? null)
    : null;
  const persistedTrainOnCompletions =
    typeof persistedState.trainOnCompletions === "boolean"
      ? persistedState.trainOnCompletions
      : undefined;
  const baselineTrainOnCompletions =
    typeof advancedSettingsBaseline?.trainOnCompletions === "boolean"
      ? advancedSettingsBaseline.trainOnCompletions
      : undefined;
  const trainOnCompletions =
    persistedTrainOnCompletions ??
    baselineTrainOnCompletions ??
    current.trainOnCompletions;
  const trainOnCompletionsDefaultPendingFor =
    persistedTrainOnCompletions === undefined &&
    baselineTrainOnCompletions === undefined
      ? modelDefaultsAppliedFor
      : null;
  const choices = persistedState.trainingToggleChoices;
  const trainingToggleChoices: TrainingConfigState["trainingToggleChoices"] = {};
  for (const key of ["packing", "trainOnCompletions"] as const) {
    const explicit = choices?.[key];
    if (typeof explicit === "boolean") trainingToggleChoices[key] = explicit;
    // Older drafts did not record provenance. Preserve saved booleans instead of guessing away an opt-out.
    else if (choices === undefined && typeof persistedState[key] === "boolean") {
      trainingToggleChoices[key] = persistedState[key];
    }
  }
  const merged: TrainingConfigStore = {
    ...current,
    ...persistedState,
    wandbToken: current.wandbToken,
    trainOnCompletions,
    trainingToggleChoices,
    modelDefaultsAppliedFor,
    advancedSettingsBaseline,
    trainOnCompletionsDefaultPendingFor,
    trainingMethodProvenance: normalizeTrainingMethodProvenance(
      persistedState.trainingMethodProvenance,
      persistedRecord,
    ),
    trainingMethod: isTrainingMethod(persistedState.trainingMethod)
      ? persistedState.trainingMethod
      : current.trainingMethod,
    settingsBeforeDecision: normalizeSettingsBeforeDecision(
      persistedState.settingsBeforeDecision,
    ),
    trainAsDecision: persistedState.trainAsDecision === true,
  };
  return {
    ...merged,
    ...datasetSourceInvariantPatch(merged),
  };
}