Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

6.3 KB
Raw Blame
import { describe, expect, it, vi, afterEach } from "vitest";

const ortMocks = vi.hoisted(() => ({ create: vi.fn() }));

vi.mock("onnxruntime-web", () => ({
  Tensor: class {
    constructor(
      public type: string,
      public data: unknown,
      public dims: number[],
    ) {}
  },
  env: {},
  InferenceSession: { create: (...args: unknown[]) => ortMocks.create(...args) },
}));

import { createWebProvider, loadWebBundle } from "../src/providers.js";

const batch: any = {
  inputIds: [[1]],
  attentionMask: [[1]],
  markerPos: [[0]],
  markerMask: [[true]],
  qtype: [0],
};

function fetchStub() {
  const calls: string[] = [];
  const files: Record<string, Uint8Array> = {
    "https://example.com/m/encoder.onnx": new Uint8Array([1, 2, 3]),
    "https://example.com/m/encoder.onnx.data": new Uint8Array([4, 5]),
    "https://example.com/m/head.onnx": new Uint8Array([6]),
  };
  const fetchMock = vi.fn(async (url: string) => {
    calls.push(url);
    const body = files[url];
    if (!body) return { ok: false, status: 404, arrayBuffer: async () => new ArrayBuffer(0) };
    return { ok: true, status: 200, arrayBuffer: async () => body.buffer.slice(0) as ArrayBuffer };
  });
  vi.stubGlobal("fetch", fetchMock);
  return { calls, fetchMock };
}

afterEach(() => {
  vi.unstubAllGlobals();
  vi.clearAllMocks();
});

describe("web provider external data", () => {
  it("mounts the sidecar via externalData", async () => {
    fetchStub();
    ortMocks.create.mockImplementation(async (_buf: unknown, opts: any) => ({
      run: async () => ({ lastHidden: { data: new Float32Array([1, 2]), dims: [1, 1, 2] } }),
      usedOpts: opts,
    }));
    await createWebProvider("https://example.com/m");
    const encCall = ortMocks.create.mock.calls.find((c) =>
      (c[1] as any)?.executionProviders?.includes("webgpu"),
    );
    expect(encCall).toBeDefined();
    const ext = (encCall![1] as any).externalData;
    expect(ext).toHaveLength(1);
    expect(ext[0].path).toBe("encoder.onnx.data");
    expect(Array.from(ext[0].data as Uint8Array)).toEqual([4, 5]);
  });

  it("re-reads encoder bytes on a [WebGPU] run failure and sticks with WASM", async () => {
    const { calls } = fetchStub();
    const webgpuErr = new Error('[WebGPU] Kernel "SkipLayerNormalization" failed. Error: Beta must be 1D');
    const goodRun = async () => ({ lastHidden: { data: new Float32Array([1, 2]), dims: [1, 1, 2] } });
    ortMocks.create.mockImplementation(async (_buf: unknown, opts: any) =>
      (opts as any)?.executionProviders?.includes("webgpu")
        ? { run: async () => { throw webgpuErr; } }
        : { run: goodRun },
    );
    const provider = await createWebProvider("https://example.com/m");
    const encFetches = () => calls.filter((u) => u === "https://example.com/m/encoder.onnx").length;
    expect(encFetches()).toBe(1);
    const out = await provider.runEncoder(batch);
    expect(out.lastHidden).toEqual([[[1, 2]]]);
    // Fallback re-reads instead of reusing 1GB+ retained buffers.
    expect(encFetches()).toBe(2);
    const wasmCreates = ortMocks.create.mock.calls.filter(
      (c) => !(c[1] as any)?.executionProviders?.includes("webgpu"),
    );
    expect(wasmCreates.length).toBeGreaterThanOrEqual(2); // head + fallback encoder
    const encWasm = wasmCreates.find((c) => (c[1] as any)?.externalData?.[0]?.path === "encoder.onnx.data");
    expect(encWasm).toBeDefined();
    // Second run goes straight to WASM: no new session, no new fetch.
    await provider.runEncoder(batch);
    expect(encFetches()).toBe(2);
  });
});

describe("web provider URL slashes", () => {
  function stubFiles() {
    const fetchMock = vi.fn(async (_url: string) => ({
      ok: true,
      status: 200,
      arrayBuffer: async () => new TextEncoder().encode("{}").buffer,
    }));
    vi.stubGlobal("fetch", fetchMock);
    ortMocks.create.mockResolvedValue({ run: async () => ({}) });
    return fetchMock;
  }

  it.each([
    ["https://example.com/m///", "/v1//child///", "https://example.com/m/v1//child"],
    ["https://example.com/m", "", "https://example.com/m"],
    ["https://example.com/m", null, "https://example.com/m"],
    ["https://example.com/m///", "///", "https://example.com/m/"],
    ["https://example.com/m/\n", "/sub/\n", "https://example.com/m/\n/sub/\n"],
    ["https://example.com/m", "/∕sub\\///", "https://example.com/m/∕sub\\"],
    ["owner/model", "/v1//child///", "https://huggingface.co/owner/model/resolve/pinned/v1//child"],
    ["owner/model", "///", "https://huggingface.co/owner/model/resolve/pinned/"],
  ])("keeps bundle URL semantics for %s and subfolder %s", async (repo, subfolder, base) => {
    const fetchMock = stubFiles();
    const bundle = await loadWebBundle(repo!, { subfolder, revision: "pinned" });
    expect(bundle.dir).toBe(base);
    expect(fetchMock.mock.calls.map(call => call[0])).toEqual([
      `${base}/rl_agent_config.json`, `${base}/tokenizer.json`,
    ]);
  });

  it.each([
    ["https://example.com/m///", "https://example.com/m"],
    ["//example.com/a//b///", "//example.com/a//b"],
    ["", ""],
    ["///", ""],
    ["https://example.com/m/\n", "https://example.com/m/\n"],
    ["https://example.com/m∕\\///", "https://example.com/m∕\\"],
  ])("keeps model and sidecar paths for %s", async (url, base) => {
    const fetchMock = stubFiles();
    await createWebProvider(url);
    expect(fetchMock.mock.calls.map(call => call[0])).toEqual([
      `${base}/encoder.onnx`, `${base}/encoder.onnx.data`,
      `${base}/head.onnx`, `${base}/head.onnx.data`,
    ]);
    expect(ortMocks.create.mock.calls.map(call => call[1].externalData[0].path))
      .toEqual(["encoder.onnx.data", "head.onnx.data"]);
  });

  it("handles long interior slash runs before a non-slash suffix", async () => {
    const fetchMock = stubFiles();
    const slashes = "/".repeat(100_000);
    const repo = `https://example.com/${slashes}model`;
    const subfolder = `${slashes}sub${slashes}leaf${slashes}`;
    const base = `${repo}/sub${slashes}leaf`;
    const bundle = await loadWebBundle(`${repo}${slashes}`, { subfolder });
    expect(bundle.dir).toBe(base);
    await createWebProvider(`${base}${slashes}`);
    expect(fetchMock.mock.calls.map(call => call[0])).toEqual([
      `${base}/rl_agent_config.json`, `${base}/tokenizer.json`,
      `${base}/encoder.onnx`, `${base}/encoder.onnx.data`,
      `${base}/head.onnx`, `${base}/head.onnx.data`,
    ]);
  });
});