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.

17.0 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 {
  getHfDatasetsServerBase,
  getHfEndpoint,
  hasDatasetsServer,
} from "@/lib/hf-endpoint";
import type { ImageCheckpointFile } from "../download-manager/required-assets";
import { LruMap } from "./lru-map";
import { fetchWithTimeout } from "./network";
import { fingerprintToken } from "./token-fingerprint";

export interface DatasetSizeInfo {
  numBytesOriginal: number | null;
  numBytesParquet: number | null;
  numRows: number | null;
  numSplits: number | null;
}

interface DatasetSizeApiResponse {
  size?: {
    dataset?: {
      num_bytes_original_files?: number;
      num_bytes_parquet_files?: number;
      num_rows?: number;
    };
    splits?: unknown[];
  };
}

// Transient misses cache briefly; "longlived" covers datasets-server 404; "permanent" a missing repo.
const TRANSIENT_MISS_TTL_MS = 60_000;
const LONGLIVED_MISS_TTL_MS = 24 * 60 * 60 * 1000;
const FETCH_TIMEOUT_MS = 10_000;

type SizeCacheEntry<T> =
  | { kind: "value"; value: T }
  | { kind: "miss-permanent" }
  | { kind: "miss-transient"; until: number };

type LoadResult<T> =
  | { value: T }
  | { miss: "permanent" | "transient" | "longlived" };

type InflightSizeEntry<T> = {
  promise: Promise<T | null>;
  controller: AbortController;
  consumers: number;
  settled: boolean;
  cancelledByConsumers: boolean;
};

function readSizeCache<T>(
  cache: LruMap<string, SizeCacheEntry<T>>,
  key: string,
): SizeCacheEntry<T> | null {
  const entry = cache.get(key);
  if (!entry) return null;
  if (entry.kind === "miss-transient" && Date.now() >= entry.until) {
    cache.delete(key);
    return null;
  }
  return entry;
}

function fetchCachedSize<T>(
  cacheKey: string,
  cache: LruMap<string, SizeCacheEntry<T>>,
  inflight: Map<string, InflightSizeEntry<T>>,
  load: (signal: AbortSignal) => Promise<LoadResult<T>>,
  debugContext: { label: string; id: string },
  signal?: AbortSignal,
): Promise<T | null> {
  if (signal?.aborted) {
    return Promise.resolve(null);
  }
  const entry = readSizeCache(cache, cacheKey);
  if (entry) {
    return Promise.resolve(entry.kind === "value" ? entry.value : null);
  }
  const existing = inflight.get(cacheKey);
  if (existing && !existing.cancelledByConsumers) return attachInflight(existing, signal);

  let timedOut = false;
  const controller = new AbortController();
  const inflightEntry: InflightSizeEntry<T> = {
    promise: Promise.resolve(null),
    controller,
    consumers: 0,
    settled: false,
    cancelledByConsumers: false,
  };
  const timeout = setTimeout(() => {
    timedOut = true;
    controller.abort();
  }, FETCH_TIMEOUT_MS);

  inflightEntry.promise = (async (): Promise<T | null> => {
    try {
      const result = await load(controller.signal);
      if (inflightEntry.cancelledByConsumers) return null;
      if ("value" in result) {
        cache.set(cacheKey, { kind: "value", value: result.value });
        return result.value;
      }
      if (result.miss === "permanent") {
        cache.set(cacheKey, { kind: "miss-permanent" });
      } else {
        const ttl =
          result.miss === "longlived"
            ? LONGLIVED_MISS_TTL_MS
            : TRANSIENT_MISS_TTL_MS;
        cache.set(cacheKey, {
          kind: "miss-transient",
          until: Date.now() + ttl,
        });
      }
      return null;
    } catch (err) {
      if (
        import.meta.env.DEV &&
        (!inflightEntry.cancelledByConsumers || timedOut)
      ) {
        console.debug(`${debugContext.label} size lookup failed`, {
          id: debugContext.id,
          error: err,
        });
      }
      if (!inflightEntry.cancelledByConsumers || timedOut) {
        cache.set(cacheKey, {
          kind: "miss-transient",
          until: Date.now() + TRANSIENT_MISS_TTL_MS,
        });
      }
      return null;
    } finally {
      clearTimeout(timeout);
      inflightEntry.settled = true;
      if (inflight.get(cacheKey) === inflightEntry) inflight.delete(cacheKey);
    }
  })();

  inflight.set(cacheKey, inflightEntry);
  return attachInflight(inflightEntry, signal);
}

function attachInflight<T>(
  entry: InflightSizeEntry<T>,
  signal?: AbortSignal,
): Promise<T | null> {
  entry.consumers += 1;
  let released = false;
  const release = () => {
    if (released) return;
    released = true;
    entry.consumers = Math.max(0, entry.consumers - 1);
    if (entry.consumers === 0 && !entry.settled) {
      entry.cancelledByConsumers = true;
      entry.controller.abort();
    }
  };

  if (!signal) {
    return entry.promise.finally(release);
  }

  if (signal.aborted) {
    release();
    return Promise.resolve(null);
  }

  const activeSignal = signal;
  return new Promise((resolve) => {
    function cleanup() {
      activeSignal.removeEventListener("abort", onAbort);
    }
    function onAbort() {
      cleanup();
      release();
      resolve(null);
    }
    activeSignal.addEventListener("abort", onAbort, { once: true });
    entry.promise.then(
      (value) => {
        cleanup();
        release();
        resolve(value);
      },
      () => {
        cleanup();
        release();
        resolve(null);
      },
    );
  });
}

const datasetCache = new LruMap<string, SizeCacheEntry<DatasetSizeInfo>>(128);
const datasetInflight = new Map<string, InflightSizeEntry<DatasetSizeInfo>>();

export function fetchDatasetSize(
  repoId: string,
  signal?: AbortSignal,
): Promise<DatasetSizeInfo | null>;
export function fetchDatasetSize(
  repoId: string,
  token?: string,
  signal?: AbortSignal,
): Promise<DatasetSizeInfo | null>;
export function fetchDatasetSize(
  repoId: string,
  tokenOrSignal?: string | AbortSignal,
  signal?: AbortSignal,
): Promise<DatasetSizeInfo | null> {
  if (!hasDatasetsServer()) return Promise.resolve(null);
  const resolvedToken =
    typeof tokenOrSignal === "string" ? tokenOrSignal : undefined;
  const resolvedSignal =
    signal ?? (typeof tokenOrSignal === "string" ? undefined : tokenOrSignal);
  // Server in the key: a 404 here is cached for 24 hours.
  const cacheKey = `${getHfDatasetsServerBase()}::${repoId}::${fingerprintToken(resolvedToken)}`;
  return fetchCachedSize<DatasetSizeInfo>(
    cacheKey,
    datasetCache,
    datasetInflight,
    async (signal) => {
      const res = await fetchWithTimeout(
        `${getHfDatasetsServerBase()}/size?dataset=${encodeURIComponent(repoId)}`,
        {
          signal,
          headers: resolvedToken
            ? { Authorization: `Bearer ${resolvedToken}` }
            : undefined,
        },
        FETCH_TIMEOUT_MS,
      );
      if (!res.ok) {
        // datasets-server 404 often means "not processed yet".
        return { miss: res.status === 404 ? "longlived" : "transient" };
      }
      const data = (await res.json()) as DatasetSizeApiResponse;
      const ds = data.size?.dataset;
      if (!ds) return { miss: "longlived" };
      return {
        value: {
          numBytesOriginal: ds.num_bytes_original_files ?? null,
          numBytesParquet: ds.num_bytes_parquet_files ?? null,
          numRows: ds.num_rows ?? null,
          numSplits: Array.isArray(data.size?.splits)
            ? (data.size?.splits.length ?? null)
            : null,
        },
      };
    },
    { label: "Dataset", id: repoId },
    resolvedSignal,
  );
}

export interface ModelSizeInfo {
  totalBytes: number | null;
  weightsBytes: number | null;
  revision?: string;
  imageCheckpointCandidates?: ImageCheckpointFile[];
}

interface ModelInfoApiResponse {
  sha?: string;
  config?: { diffusers?: { _class_name?: string } };
  siblings?: Array<{
    rfilename?: string;
    size?: number;
  }>;
}

type ModelSibling = NonNullable<ModelInfoApiResponse["siblings"]>[number];

const SNAPSHOT_WEIGHT_FILE_RE =
  /\.(safetensors|bin|pt|pth|ckpt|h5|msgpack|npz)$/i;
const SNAPSHOT_NON_BIN_WEIGHT_FILE_RE =
  /\.(safetensors|pt|pth|ckpt|h5|msgpack|npz)$/i;
const SNAPSHOT_BIN_WEIGHT_PREFIX_RE = /^(model|pytorch_model|adapter_model).*\.bin$/i;
// [0-9], not \d, to match the backend's SHARDED_SAFETENSORS_RE in snapshot_filters.py.
const SHARDED_SAFETENSORS_RE = /^model[-_][0-9]+-of-[0-9]+\.safetensors$/;
const SAFETENSORS_INDEX = "model.safetensors.index.json";
const DUPLICATE_WEIGHT_FORMAT_RE =
  /^(?:(?:original|metal|coreml)\/|(?:tf_model.*\.h5|flax_model.*\.msgpack)$|(?:tf_model\.h5|flax_model\.msgpack)\.index\.json$|rust_model\.ot$)/s;
// Mirrors redundant_torch_bin_files in snapshot_filters.py: a dtype variant is redundant only when
// the same variant ships as safetensors. Both shard layouts are recognised.
const BIN_WEIGHT_RES = [
  /^pytorch_model(?:\.([A-Za-z0-9_]+))?(?:[-_][0-9]+-of-[0-9]+)?\.bin$/,
  /^pytorch_model[-_][0-9]+-of-[0-9]+\.([A-Za-z0-9_]+)\.bin$/,
];
const BIN_INDEX_RE =
  /^pytorch_model(?:\.([A-Za-z0-9_]+))?\.bin\.index(?:\.([A-Za-z0-9_]+))?\.json$/;

function variantShipsAsSafetensors(names: string[], variant: string | undefined): boolean {
  if (!variant) return true;
  if (names.includes(`model.${variant}.safetensors`)) return true;
  // transformers' _add_variant puts the variant second-to-last.
  if (!names.includes(`model.safetensors.index.${variant}.json`)) return false;
  const v = variant.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  const sharded = new RegExp(
    `^(?:model\\.${v}[-_][0-9]+-of-[0-9]+\\.safetensors|model[-_][0-9]+-of-[0-9]+\\.${v}\\.safetensors)$`,
  );
  return names.some((n) => sharded.test(n));
}

function redundantTorchBinFiles(names: string[]): Set<string> {
  const out = new Set<string>();
  for (const name of names) {
    let variant: string | undefined;
    const weight = BIN_WEIGHT_RES.map((re) => re.exec(name)).find((m) => m);
    if (weight) {
      variant = weight[1];
    } else {
      const index = BIN_INDEX_RE.exec(name);
      if (!index) continue;
      variant = index[1] ?? index[2];
    }
    if (variantShipsAsSafetensors(names, variant)) out.add(name);
  }
  return out;
}

// Mirrors pipeline_pickle_files (diffusers_safetensors.py, via diffusers_pickle_files in snapshot_filters.py):
// a diffusers pipeline repo skips a .pt / .pth / .ckpt / .pkl inside a component folder or beside its same-stem
// .safetensors twin (a root pickle with no twin is the only copy and stays), and a .bin beside safetensors weights.
const PIPELINE_MANIFESTS = ["model_index.json", "modular_model_index.json"];
const NEVER_READ_PICKLE_RE = /\.(pt|pth|ckpt|pkl|pickle)$/i;
// Mirrors _MODEL_SAFETENSORS: only a component's own non-variant weights make its .bin redundant.
const MODEL_SAFETENSORS_RE =
  /^(diffusion_pytorch_model|model)(-\d+-of-\d+)?\.safetensors(\.index\.json)?$/;

function folderOf(name: string): string {
  const slash = name.lastIndexOf("/");
  return slash >= 0 ? name.slice(0, slash) : "";
}

function diffusersPickleFiles(names: string[]): Set<string> {
  const out = new Set<string>();
  if (!names.some((n) => PIPELINE_MANIFESTS.includes(n))) return out;
  const safetensorsFolders = new Set(
    names
      .filter((n) => MODEL_SAFETENSORS_RE.test(basename(n)))
      .map(folderOf),
  );
  const safetensorsStems = new Set(
    names
      .filter((n) => n.toLowerCase().endsWith(".safetensors"))
      .map((n) => n.slice(0, -".safetensors".length).toLowerCase()),
  );
  for (const n of names) {
    const lower = n.toLowerCase();
    if (NEVER_READ_PICKLE_RE.test(lower)) {
      if (folderOf(n) !== "" || safetensorsStems.has(lower.slice(0, lower.lastIndexOf(".")))) {
        out.add(n);
      }
    } else if (
      (lower.endsWith(".bin") || lower.endsWith(".bin.index.json")) &&
      safetensorsFolders.has(folderOf(n))
    ) {
      out.add(n);
    }
  }
  return out;
}

// Mirrors PRECISION_FOLDER_SOURCE and precision_variant_subfolders in snapshot_filters.py.
const PRECISION_FOLDER_SOURCE =
  "(?:mlx[-_]?)?(?:[0-9]{1,2}[-_]?bits?(?:[-_][a-z0-9]+)*|bf16|fp16|f16|fp32|f32|fp8|mxfp4|nvfp4)";
const PRECISION_FOLDER_RE = new RegExp(`^(?:${PRECISION_FOLDER_SOURCE})$`, "i");

export function isPrecisionFolderName(name: string): boolean {
  return PRECISION_FOLDER_RE.test(name);
}

function precisionVariantFolders(names: string[]): Set<string> {
  const files = new Map<string, string[]>();
  for (const name of names) {
    const slash = name.indexOf("/");
    const folder = name.slice(0, slash);
    const rest = name.slice(slash + 1);
    if (slash <= 0 || !rest || rest.includes("/")) continue;
    if (!PRECISION_FOLDER_RE.test(folder)) continue;
    files.set(folder, [...(files.get(folder) ?? []), rest]);
  }
  const out = new Set<string>();
  for (const [folder, rest] of files) {
    if (
      rest.includes("config.json") &&
      rest.some((n) => n.toLowerCase().endsWith(".safetensors"))
    ) {
      out.add(folder);
    }
  }
  return out;
}

function basename(path: string): string {
  return path.split("/").pop() ?? path;
}

function shipsTransformersWeights(siblings: ModelSibling[]): boolean {
  return siblings.some((s) => {
    const base = basename(s.rfilename ?? "").toLowerCase();
    if (base.startsWith("consolidated")) return false;
    return (
      SNAPSHOT_NON_BIN_WEIGHT_FILE_RE.test(base) ||
      SNAPSHOT_BIN_WEIGHT_PREFIX_RE.test(base)
    );
  });
}

// One numbered shard is not a loadable checkpoint. Mirrors repo_ships_root_safetensors.
function shipsRootSafetensors(siblings: ModelSibling[]): boolean {
  const names = siblings.map((s) => s.rfilename ?? "");
  if (names.some((n) => n === "model.safetensors")) return true;
  return (
    names.includes(SAFETENSORS_INDEX) &&
    names.some((n) => SHARDED_SAFETENSORS_RE.test(n))
  );
}

function isSnapshotIgnored(
  filename: string,
  skipConsolidated: boolean,
  skipDuplicateFormats: boolean,
  redundantBins: Set<string>,
  precisionFolders: Set<string> = new Set(),
  pipelinePickles: Set<string> = new Set(),
): boolean {
  const lower = filename.toLowerCase();
  const folder = filename.includes("/") ? filename.slice(0, filename.indexOf("/")) : "";
  return (
    lower.endsWith(".gguf") ||
    lower.endsWith(".onnx") ||
    lower.startsWith("onnx/") ||
    lower.startsWith("openvino/") ||
    lower.startsWith("mlx/") ||
    lower.endsWith(".bin.index.json.bak") ||
    pipelinePickles.has(filename) ||
    (skipConsolidated && lower.startsWith("consolidated")) ||
    (skipDuplicateFormats &&
      (DUPLICATE_WEIGHT_FORMAT_RE.test(filename) || redundantBins.has(filename) ||
        precisionFolders.has(folder)))
  );
}

const modelCache = new LruMap<string, SizeCacheEntry<ModelSizeInfo>>(128);
const modelInflight = new Map<string, InflightSizeEntry<ModelSizeInfo>>();

export function fetchModelSize(
  repoId: string,
  token?: string,
  signal?: AbortSignal,
): Promise<ModelSizeInfo | null> {
  // Endpoint in the key: a model 404 is cached for the whole session.
  const cacheKey = `${getHfEndpoint()}::${repoId}::${fingerprintToken(token)}`;
  return fetchCachedSize<ModelSizeInfo>(
    cacheKey,
    modelCache,
    modelInflight,
    async (signal) => {
      const path = repoId.split("/").map(encodeURIComponent).join("/");
      const res = await fetchWithTimeout(
        `${getHfEndpoint()}/api/models/${path}?blobs=true`,
        {
          signal,
          headers: token ? { Authorization: `Bearer ${token}` } : undefined,
        },
        FETCH_TIMEOUT_MS,
      );
      if (!res.ok) {
        return { miss: res.status === 404 ? "permanent" : "transient" };
      }
      const data = (await res.json()) as ModelInfoApiResponse;
      const siblings = data.siblings ?? [];
      // SDXL repos can package alternate whole checkpoints alongside their Diffusers component folders.
      const imageCheckpointCandidates = data.config?.diffusers?._class_name === "StableDiffusionXLPipeline"
        ? siblings.flatMap(({ rfilename, size }) =>
            rfilename && /^[^/\\]+\.safetensors$/i.test(rfilename) && !/[-_]\d+-of-\d+\./i.test(rfilename)
              ? [{ filename: rfilename, bytes: size ?? null }]
              : [],
          )
        : [];
      const skipConsolidated = shipsTransformersWeights(siblings);
      const skipDuplicateFormats = shipsRootSafetensors(siblings);
      const names = siblings.map((s) => s.rfilename ?? "");
      const redundantBins = redundantTorchBinFiles(names);
      const precisionFolders = names.includes("config.json")
        ? precisionVariantFolders(names)
        : new Set<string>();
      const pipelinePickles = diffusersPickleFiles(names);
      let total = 0;
      let weights = 0;
      for (const s of siblings) {
        if (typeof s.size !== "number") continue;
        const filename = s.rfilename ?? "";
        if (
          filename &&
          isSnapshotIgnored(
            filename,
            skipConsolidated,
            skipDuplicateFormats,
            redundantBins,
            precisionFolders,
            pipelinePickles,
          )
        ) {
          continue;
        }
        total += s.size;
        if (filename && SNAPSHOT_WEIGHT_FILE_RE.test(filename)) {
          weights += s.size;
        }
      }
      return {
        value: {
          totalBytes: total > 0 ? total : null,
          weightsBytes: weights > 0 ? weights : null,
          ...(imageCheckpointCandidates.length ? { imageCheckpointCandidates, revision: data.sha } : {}),
        },
      };
    },
    { label: "Model", id: repoId },
    signal,
  );
}