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.

2.1 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 {
  type TransferSample,
  appendSample,
  computeTransferStats,
} from "../src/lib/transfer-stats.ts";

import { readSrc } from "./helpers/kit.ts";

const MB = 1e6;

test("a generation change clears the rate samples before the next one is taken", () => {
  const source = readSrc("features/hub/download-manager/poll-loop.ts");
  const clear = source.indexOf(
    "rt.speedSamples.length = 0",
    source.indexOf("generationChanged) {"),
  );
  const sample = source.indexOf("applySpeedSample(rt,");
  assert.ok(clear > 0, "a generation change should clear the samples");
  assert.ok(sample > 0, "the speed sample should still be taken");
  assert.ok(
    clear < sample,
    "clearing after sampling would price the new run on the old buffer",
  );
});

test("a restart does not make the resumed transfer look slow", () => {
  const rates = (clearOnGeneration: boolean) => {
    const samples: TransferSample[] = [];
    const out: number[] = [];
    let bytes = 0;
    const poll = (t: number, b: number, generationChanged: boolean) => {
      if (generationChanged && clearOnGeneration) samples.length = 0;
      appendSample(samples, t, b);
      const stats = computeTransferStats(samples, 100_000 * MB);
      out.push(stats.stable ? stats.rateBytesPerSecond : 0);
    };
    for (let t = 0; t <= 60; t += 1) {
      bytes = t * 100 * MB;
      poll(t, bytes, false);
    }
    const after: number[] = [];
    for (let t = 73; t <= 84; t += 1) {
      poll(t, bytes + (t - 73) * 100 * MB, t === 73);
      after.push(out[out.length - 1]);
    }
    return after;
  };

  const stale = rates(false).filter((r) => r > 0);
  const cleared = rates(true).filter((r) => r > 0);
  assert.ok(
    Math.min(...stale) < 20 * MB,
    "without the reset the restart gap should still drag the rate down",
  );
  assert.ok(
    Math.min(...cleared) > 95 * MB,
    `after the reset the resumed transfer should read ~100 MB/s, got ${(Math.min(...cleared) / MB).toFixed(1)}`,
  );
});