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.

fix(model): harden revision verification

KunjShah95f6b35349cfde
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts
index 264ca9af..f5bc8e69 100644
--- a/laya-ts/src/agent.ts
+++ b/laya-ts/src/agent.ts
@@ -549,7 +549,7 @@ export class Agent extends HookRegistry {
numThreads?: number;
/** Per-language temperature overrides; see AgentOptions.lang_temperatures. */
lang_temperatures?: AgentOptions["lang_temperatures"];
- /** Commit SHA/branch/tag to fetch; published checkpoints pin to a reviewed SHA by default. */
+ /** Optional commit SHA/branch/tag to fetch; omitted uses the Hub default and existing cache. */
revision?: string | null;
/**
* Opt-in `{artifact name: SHA-256 hexdigest}` check before any artifact is parsed
@@ -594,7 +594,9 @@ export class Agent extends HookRegistry {
tokenizerJson = bundle.tokenizerJson;
dir = bundle.dir;
revision = bundle.revision;
- provider = await createNodeProvider(dir, { device: opts?.device, numThreads: opts?.numThreads });
+ provider = await createNodeProvider(dir, {
+ device: opts?.device, numThreads: opts?.numThreads, expectedSha256: opts?.expectedSha256,
+ });
}
if (!tokenizerJson) {
throw new Error(
diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.ts
index 5d6916a1..3b802628 100644
--- a/laya-ts/src/providers.ts
+++ b/laya-ts/src/providers.ts
@@ -1,20 +1,17 @@
/** ONNX session shim: Node (onnxruntime-node) + browser (onnxruntime-web).
* Lazy imports only — unit tests with a fake provider never touch onnxruntime. */
-/** Reviewed commit SHAs of the published checkpoints (mirror of laya/revisions.py).
- * Loads of these repos pin to the reviewed revision instead of mutable `main`; bump a
- * pin only after the new revision has been reviewed. */
+/** Opt-in reviewed commit SHAs of the published checkpoints (mirror of laya/revisions.py).
+ * They are not applied implicitly, so existing Hub/offline caches keep working. */
export const PINNED_REVISIONS: Record<string, string> = {
"convaiinnovations/laya": "55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851",
"convaiinnovations/laya-multilingual": "e4e9ddf21a7b1903b7acffd8814ad4307bf63a67",
"convaiinnovations/laya-typed-decisions": "1a793eb568e6718f15941d08f85432581df534e3",
};
-/** Explicit `revision` wins; published repos fall back to their reviewed pin; anything
- * else keeps the hub default (`main`). */
-export function resolveRevision(repoOrId: string, revision?: string | null): string | null {
- if (revision) return revision;
- return PINNED_REVISIONS[repoOrId] ?? null;
+/** Return an explicit revision unchanged; otherwise preserve the Hub default and cache. */
+export function resolveRevision(_repoOrId: string, revision?: string | null): string | null {
+ return revision || null;
}
/** SHA-256 hex via Web Crypto (browsers and modern Node expose globalThis.crypto). */
@@ -29,7 +26,11 @@ async function sha256Hex(data: ArrayBuffer | Uint8Array): Promise<string> {
/** Reject absolute or escaping digest paths before they ever reach the filesystem. */
function normaliseDigestPath(rel: string): string {
- const norm = String(rel).replace(/\\/g, "/").replace(/^\/+/, "");
+ const raw = String(rel).replace(/\\/g, "/");
+ if (/^(?:[A-Za-z]:|\/)/.test(raw)) {
+ throw new Error(`laya-ts: unsafe absolute path in expectedSha256: ${JSON.stringify(rel)}`);
+ }
+ const norm = raw;
if (!norm || norm === ".." || norm.startsWith("../") || norm.includes("/../")) {
throw new Error(`laya-ts: unsafe path in expectedSha256: ${JSON.stringify(rel)}`);
}
@@ -157,7 +158,10 @@ function isOomError(e: unknown): boolean {
}
/** Online-first fetch: try network, cache on success, fall back to CacheStorage. */
-async function fetchArrayBuffer(url: string): Promise<ArrayBuffer> {
+async function fetchArrayBuffer(
+ url: string,
+ onHeaders?: (response: Response) => void,
+): Promise<ArrayBuffer> {
const g = globalThis as unknown as { caches?: any };
let cache: any = null;
let hit: any = null;
@@ -180,6 +184,7 @@ async function fetchArrayBuffer(url: string): Promise<ArrayBuffer> {
if (cache) {
try {
const res = await fetch(url);
+ onHeaders?.(res);
if (res.ok) {
try {
await cache.put(url, res.clone());
@@ -208,6 +213,7 @@ async function fetchArrayBuffer(url: string): Promise<ArrayBuffer> {
throw new Error(`fetch failed for ${url}`);
}
const res = await fetch(url);
+ onHeaders?.(res);
if (!res.ok) throw new Error(`fetch failed for ${url}: ${res.status}`);
return await res.arrayBuffer();
}
@@ -241,8 +247,8 @@ export async function loadNodeBundle(
if (st.isDirectory()) dir = sub ? path.join(dir, sub) : dir;
else dir = path.dirname(dir);
} catch {
- // Pin published repos to their reviewed commit SHA instead of mutable `main`; the
- // revision joins the cache key so differently-pinned artifacts never collide.
+ // An explicit revision joins the cache key so differently-pinned artifacts never collide;
+ // otherwise the existing Hub-default cache is reused.
const revision = resolveRevision(modelDirOrRepo, opts?.revision);
resolvedRevision = revision;
const cache = path.join(
@@ -350,8 +356,12 @@ export async function loadWebBundle(
): Promise<WebBundle> {
const revision = resolveRevision(repoOrUrl, opts?.revision);
const base = baseUrlFor(repoOrUrl, opts?.subfolder ?? null, revision);
+ let reportedRevision = revision;
const fetchVerifiedJson = async (rel: string): Promise<unknown> => {
- const buf = await fetchArrayBuffer(`${base}/${rel}`);
+ const buf = await fetchArrayBuffer(`${base}/${rel}`, (response) => {
+ const commit = response.headers?.get?.("x-repo-commit");
+ if (commit) reportedRevision = commit;
+ });
if (opts?.expectedSha256) await expectDigest(rel, buf, opts.expectedSha256);
return JSON.parse(new TextDecoder().decode(buf));
};
@@ -372,7 +382,7 @@ export async function loadWebBundle(
// Try the next supported Hugging Face layout.
}
}
- return { dir: base, cfg, tokenizerJson, revision };
+ return { dir: base, cfg, tokenizerJson, revision: reportedRevision };
}
export async function createNodeProvider(
@@ -382,19 +392,16 @@ export async function createNodeProvider(
const spec = "onnxruntime-" + "node";
const ort: any = await import(/* @vite-ignore */ spec);
applyNumThreads(ort, opts?.numThreads);
- try {
- const fs: typeof import("node:fs/promises") = await import("node:fs/promises");
- const path: typeof import("node:path") = await import("node:path");
- for (const f of ["encoder.onnx", "head.onnx"]) {
- const p = path.join(modelDir, f);
- try {
- await fs.stat(p);
- } catch {
- throw new Error(`Incompatible model: '${f}' not found in ${JSON.stringify(modelDir)} (expected ${p}).`);
- }
+ const fs: typeof import("node:fs/promises") = await import("node:fs/promises");
+ const path: typeof import("node:path") = await import("node:path");
+ for (const f of ["encoder.onnx", "head.onnx"]) {
+ const p = path.join(modelDir, f);
+ try {
+ await fs.stat(p);
+ } catch {
+ throw new Error(`Incompatible model: '${f}' not found in ${JSON.stringify(modelDir)} (expected ${p}).`);
}
- } catch (e) {
- if (e instanceof Error && e.message.includes("not found in")) throw e;
+ if (opts?.expectedSha256) await expectDigest(f, await fs.readFile(p), opts.expectedSha256);
}
const dev = String(opts?.device ?? "cpu").toLowerCase();
const want = dev === "cuda" ? "cuda" : dev === "dml" ? "dml" : "cpu";
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index cd7efdd2..deeb131b 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -122,6 +122,8 @@ export interface RouterOptions {
loader?: AgentLoader;
/** Optional hub revision (commit SHA/branch/tag) applied to every checkpoint load. */
revision?: string | null;
+ /** Per-model revision overrides, keyed by model name or alias. */
+ revisions?: Record<string, string | null>;
hooks?: HookArg;
onPredictStart?: PredictHook;
onPredictEnd?: PredictHook;
@@ -157,6 +159,7 @@ export class Router extends HookRegistry {
device: string | null;
token: string | null | undefined;
revision: string | null;
+ revisions: Partial<Record<ModelName, string | null>>;
maxLoaded: number;
default: ModelName;
autoTaskDetection: boolean;
@@ -182,9 +185,12 @@ export class Router extends HookRegistry {
}
this.device = opts.device ?? null;
this.token = opts.token ?? (typeof process !== "undefined" ? process.env?.["HF_TOKEN"] : undefined);
- // Optional hub revision applied to every checkpoint load; published checkpoints pin
- // to a reviewed SHA even without it (see PINNED_REVISIONS in providers.ts).
+ // Optional hub revision applied to every checkpoint load. Per-model overrides support
+ // standalone repositories whose reviewed commits differ.
this.revision = opts.revision ?? null;
+ this.revisions = Object.fromEntries(
+ Object.entries(opts.revisions ?? {}).map(([name, value]) => [normaliseName(name), value]),
+ ) as Partial<Record<ModelName, string | null>>;
this.maxLoaded = Math.max(1, Math.trunc(Number(opts.maxLoaded ?? opts.max_loaded ?? 2)));
this.default = normaliseName(opts.default ?? "english");
this.autoTaskDetection = Boolean(opts.autoTaskDetection ?? opts.auto_task_detection ?? false);
@@ -209,14 +215,18 @@ export class Router extends HookRegistry {
} else {
const { Agent } = await import("./agent.js");
const spec = this.models[key];
- agent = await (Agent as unknown as {
- load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
- }).load(spec.repo, {
+ const revision = Object.prototype.hasOwnProperty.call(this.revisions, key)
+ ? this.revisions[key]
+ : this.revision;
+ const opts: Record<string, unknown> = {
subfolder: spec.subfolder,
device: this.device ?? undefined,
token: this.token ?? undefined,
- revision: this.revision ?? undefined,
- });
+ };
+ if (revision) opts.revision = revision;
+ agent = await (Agent as unknown as {
+ load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
+ }).load(spec.repo, opts);
}
this._agents.set(key, agent);
this._order.push(key);
diff --git a/laya-ts/tests/identifier-complexity.test.ts b/laya-ts/tests/identifier-complexity.test.ts
index 5c54d62f..d3218573 100644
--- a/laya-ts/tests/identifier-complexity.test.ts
+++ b/laya-ts/tests/identifier-complexity.test.ts
@@ -97,12 +97,18 @@ describe("identifier stripping: complexity", () => {
// above carry the guarantee -- they are 250x and 1500x apart.
it("4x the input costs under 8x the time (linear ~4, quadratic ~16)", () => {
- const best = (n: number) =>
- Math.min(...[0, 1, 2].map(() => elapsed(() => latinProfile("a".repeat(n)))));
- const small = best(12_500);
- const large = best(50_000);
- // performance.now() granularity makes a sub-0.05 ms baseline meaningless; the
- // absolute ceilings above already carry the guarantee in that case.
- if (small > 0.05) expect(large / small).toBeLessThan(8);
+ // Compare equal total input sizes in batches. Timing one 12.5k call on a shared
+ // runner is below the useful clock granularity and can turn scheduler jitter into an
+ // 11x ratio; batching 4x/1x repetitions keeps the same 4x-size comparison while
+ // giving both sides enough work to measure.
+ const best = (n: number, repeats: number) => {
+ const input = "a".repeat(n);
+ return Math.min(...[0, 1, 2].map(() => elapsed(() => {
+ for (let i = 0; i < repeats; i++) latinProfile(input);
+ })));
+ };
+ const small = best(12_500, 40);
+ const large = best(50_000, 10);
+ expect(large / small).toBeLessThan(8);
});
});
diff --git a/laya-ts/tests/revision-pinning.test.ts b/laya-ts/tests/revision-pinning.test.ts
index 90188ca7..367704fd 100644
--- a/laya-ts/tests/revision-pinning.test.ts
+++ b/laya-ts/tests/revision-pinning.test.ts
@@ -5,7 +5,7 @@ import * as os from "node:os";
import * as path from "node:path";
import { Agent } from "../src/agent.js";
import { Router } from "../src/router.js";
-import { PINNED_REVISIONS, loadNodeBundle, resolveRevision } from "../src/providers.js";
+import { PINNED_REVISIONS, createNodeProvider, loadNodeBundle, loadWebBundle, resolveRevision } from "../src/providers.js";
const fakeProvider = () => ({
async runEncoder(_b: any) { return { lastHidden: [[1, 0], [0, 1]] }; },
@@ -39,13 +39,12 @@ afterEach(() => {
});
describe("resolveRevision", () => {
- it("explicit revision wins over the pin", () => {
+ it("explicit revision is returned", () => {
expect(resolveRevision("convaiinnovations/laya", "abc123")).toBe("abc123");
});
- it("published repos fall back to the reviewed pin", () => {
- for (const [repo, sha] of Object.entries(PINNED_REVISIONS)) {
- expect(sha).toMatch(/^[0-9a-f]{40}$/);
- expect(resolveRevision(repo)).toBe(sha);
+ it("published repos keep the hub default without an explicit pin", () => {
+ for (const repo of Object.keys(PINNED_REVISIONS)) {
+ expect(resolveRevision(repo)).toBeNull();
}
});
it("unknown repos keep the hub default", () => {
@@ -54,7 +53,7 @@ describe("resolveRevision", () => {
});
describe("loadNodeBundle pinning", () => {
- it("fetches published checkpoints at the pinned revision and joins it to the cache key", async () => {
+ it("keeps the hub default unless a revision is explicitly requested", async () => {
const urls: string[] = [];
vi.stubGlobal("fetch", vi.fn(async (url: string) => {
urls.push(url);
@@ -62,13 +61,12 @@ describe("loadNodeBundle pinning", () => {
? new StubResponse(cfgFile["rl_agent_config.json"])
: new StubResponse(null, 404);
}));
- const pin = PINNED_REVISIONS["convaiinnovations/laya"];
- const cacheDir = path.join(os.homedir(), ".cache", "laya-ts", "hf", "convaiinnovations__laya", "root", pin);
+ const cacheDir = path.join(os.homedir(), ".cache", "laya-ts", "hf", "convaiinnovations__laya", "root");
try {
const bundle = await loadNodeBundle("convaiinnovations/laya");
- expect(urls[0]).toContain(`/resolve/${pin}/`);
+ expect(urls[0]).toContain("/resolve/main/");
expect(bundle.dir).toBe(cacheDir);
- // The hub reports the exact commit served; that wins over the requested pin.
+ // The hub reports the exact commit served even when no pin was requested.
expect(bundle.revision).toBe("abc123");
expect(bundle.cfg.act_costs).toEqual({ a: 0 });
} finally {
@@ -76,6 +74,33 @@ describe("loadNodeBundle pinning", () => {
}
});
+ it("uses an explicit revision in both the URL and cache key", async () => {
+ const urls: string[] = [];
+ vi.stubGlobal("fetch", vi.fn(async (url: string) => {
+ urls.push(url);
+ return url.endsWith("rl_agent_config.json")
+ ? new StubResponse(cfgFile["rl_agent_config.json"])
+ : new StubResponse(null, 404);
+ }));
+ try {
+ const bundle = await loadNodeBundle("convaiinnovations/laya", { revision: "abc123" });
+ expect(urls[0]).toContain("/resolve/abc123/");
+ expect(bundle.dir).toContain(path.join("root", "abc123"));
+ } finally {
+ fs.rmSync(path.join(os.homedir(), ".cache", "laya-ts", "hf", "convaiinnovations__laya"), { recursive: true, force: true });
+ }
+ });
+
+ it("loadWebBundle reports the x-repo-commit header", async () => {
+ vi.stubGlobal("fetch", vi.fn(async (url: string) => {
+ return url.endsWith("rl_agent_config.json")
+ ? new StubResponse(cfgFile["rl_agent_config.json"])
+ : new StubResponse(null, 404);
+ }));
+ const bundle = await loadWebBundle("convaiinnovations/laya");
+ expect(bundle.revision).toBe("abc123");
+ });
+
it("unpinned repos keep the mutable main default", async () => {
const urls: string[] = [];
vi.stubGlobal("fetch", vi.fn(async (url: string) => {
@@ -126,11 +151,19 @@ describe("loadNodeBundle expectedSha256", () => {
it("escaping digest paths are rejected", async () => {
const dir = makeCheckpoint(cfgFile);
- for (const rel of ["../evil", "..", "a/../../evil"]) {
+ for (const rel of ["../evil", "..", "a/../../evil", "/absolute/evil", "C:\\absolute\\evil"]) {
await expect(loadNodeBundle(dir, { expectedSha256: { [rel]: "0".repeat(64) } }))
- .rejects.toThrow(/unsafe path/);
+ .rejects.toThrow(/unsafe (?:absolute )?path/);
}
});
+
+ it("createNodeProvider refuses mismatched ONNX digests before creating sessions", async () => {
+ const encoder = new TextEncoder().encode("encoder");
+ const dir = makeCheckpoint({ "encoder.onnx": encoder, "head.onnx": new TextEncoder().encode("head") });
+ await expect(createNodeProvider(dir, {
+ expectedSha256: { "encoder.onnx": "0".repeat(64) },
+ })).rejects.toThrow(/SHA-256 mismatch/);
+ });
});
describe("revision plumbing", () => {
@@ -140,8 +173,15 @@ describe("revision plumbing", () => {
expect(new Agent({ provider: fakeProvider(), cfg: {} }).revision).toBeNull();
});
- it("Router stores an explicit revision", () => {
- expect(new Router({ revision: "abc123" }).revision).toBe("abc123");
+ it("Router stores global and per-model revisions", () => {
+ const router = new Router({
+ revision: "default",
+ revisions: { ml: "multi-sha", typed: "typed-sha" },
+ });
+ expect(router.revision).toBe("default");
+ expect(router.revisions.multilingual).toBe("multi-sha");
+ expect(router.revisions["typed-decisions"]).toBe("typed-sha");
expect(new Router().revision).toBeNull();
+ expect(new Router().revisions).toEqual({});
});
});
diff --git a/laya/agent.py b/laya/agent.py
index c16f7169..db38e40d 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -228,13 +228,12 @@ class Agent(HookRegistry):
):
"""Load a Laya checkpoint.
- `revision` pins the Hub download to an explicit commit SHA/branch/tag. The
- published convaiinnovations/* checkpoints default to the reviewed SHA pinned in
- `laya.revisions.PINNED_REVISIONS` instead of mutable `main`; pass `revision` to
- override. `expected_sha256` ({path relative to the checkpoint dir: hexdigest})
+ `revision` optionally pins the Hub download to an explicit commit SHA/branch/tag;
+ when omitted, huggingface_hub's normal default and existing offline cache are used.
+ `expected_sha256` ({path relative to the checkpoint dir: hexdigest})
verifies artifact integrity before any weight is parsed or executed; it is opt-in
- and applies to local directories too. A missing artifact or digest mismatch raises
- `ValueError` and loading is refused.
+ and applies to local directories too. A missing artifact raises `FileNotFoundError`
+ and a digest mismatch raises `ValueError`; either error refuses the load.
`fast=True` swaps the encoder/head forward for the TileLang fast path (CUDA only, needs
`pip install laya[fast]`); see `Agent.accelerate`.
@@ -294,8 +293,7 @@ class Agent(HookRegistry):
)
# Verify integrity before any file in the checkpoint is parsed or executed.
- if expected_sha256:
- verify_digests(model_dir, expected_sha256)
+ verify_digests(model_dir, expected_sha256)
_fix_tokenizer_config(model_dir)
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 986076d0..0f9c4182 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -54,13 +54,13 @@ class ONNXAgent(HookRegistry):
(used to load the tokenizer and config).
onnx_path: Path to the exported .onnx file.
subfolder: Optional subfolder if downloading from a repo bundle.
- revision: Optional Hub revision (commit SHA/branch/tag). Published
- convaiinnovations/* checkpoints default to the reviewed SHA pinned
- in `laya.revisions.PINNED_REVISIONS` instead of mutable `main`.
+ revision: Optional Hub revision (commit SHA/branch/tag). When omitted,
+ huggingface_hub's normal default and existing offline cache are used.
expected_sha256: Optional {path relative to the checkpoint dir: hexdigest}
verified before any checkpoint file is parsed; opt-in, and applies
- to local directories too. A missing artifact or digest mismatch
- raises `ValueError` and loading is refused.
+ to local directories too. A missing artifact raises
+ `FileNotFoundError` and a digest mismatch raises `ValueError`; either
+ error refuses the load.
hooks (HookArg): Opt-in prediction hooks; see `laya.hooks`.
on_predict_start (PredictHookArg): An opt-in start hook, run before inference.
on_predict_end (PredictHookArg): An opt-in end hook, run after inference.
@@ -106,8 +106,7 @@ class ONNXAgent(HookRegistry):
)
# Verify integrity before any file in the checkpoint is parsed or executed.
- if expected_sha256:
- verify_digests(model_dir, expected_sha256)
+ verify_digests(model_dir, expected_sha256, onnx_path=onnx_path)
cfg_path = os.path.join(model_dir, "rl_agent_config.json")
if not os.path.exists(cfg_path):
diff --git a/laya/revisions.py b/laya/revisions.py
index 58863440..91014716 100644
--- a/laya/revisions.py
+++ b/laya/revisions.py
@@ -1,19 +1,18 @@
-"""Supply-chain integrity for published checkpoints: pinned revisions and digest checks.
+"""Supply-chain integrity for published checkpoints: opt-in pins and digest checks.
-Runtime loaders default to the mutable ``main`` revision of a Hub repo. For the published
-checkpoints that is a silent-behavior-change risk: a compromised or accidentally changed
-model repository would change routing, confidence, guardrail, and ONNX behavior with no
-code change on the user's side. Loading a published repo without an explicit revision
-therefore pins to the reviewed commit SHA below, and any loader accepts an optional
-SHA-256 map to verify artifact integrity before weights reach the runtime.
+Runtime loaders keep the Hub default revision unless the caller supplies one. This preserves
+compatibility with existing offline caches, including ``HF_HUB_OFFLINE=1`` deployments. The
+reviewed commit SHAs below are available for callers that opt in, and every loader accepts an
+optional SHA-256 map to verify artifact integrity before weights reach the runtime.
"""
import hashlib
+import json
+import ntpath
import os
from typing import Dict, Optional
-# Reviewed commit SHAs of the published checkpoints. Bump a pin only after the new
-# revision has been reviewed; treat an unexpected upstream SHA change as a prompt to
-# review, not to blindly re-pin.
+# Opt-in reviewed commit SHAs of the published checkpoints. They are not applied
+# implicitly, so existing Hub/offline caches keep working; pass one explicitly to pin a load.
PINNED_REVISIONS: Dict[str, str] = {
"convaiinnovations/laya": "55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851",
"convaiinnovations/laya-multilingual": "e4e9ddf21a7b1903b7acffd8814ad4307bf63a67",
@@ -24,12 +23,10 @@ PINNED_REVISIONS: Dict[str, str] = {
def resolve_revision(model_id_or_path: str, revision: Optional[str] = None) -> Optional[str]:
"""Pick the revision to download.
- An explicit `revision` always wins; a published repo falls back to its reviewed pin;
- anything else returns None so the Hub default (`main`) applies unchanged.
+ An explicit `revision` is returned unchanged. Otherwise None is returned so
+ huggingface_hub applies its normal default, preserving existing online/offline caches.
"""
- if revision:
- return revision
- return PINNED_REVISIONS.get(model_id_or_path)
+ return revision or None
def snapshot_revision(path: str) -> Optional[str]:
@@ -45,18 +42,38 @@ def snapshot_revision(path: str) -> Optional[str]:
return None
-def verify_digests(model_dir: str, expected: Dict[str, str]) -> None:
+def verify_digests(
+ model_dir: str,
+ expected: Optional[Dict[str, str]] = None,
+ onnx_path: Optional[str] = None,
+) -> None:
"""Verify SHA-256 digests of files under `model_dir` against {relpath: hexdigest}.
Raises FileNotFoundError when a listed file is absent and ValueError on a digest
mismatch or an unsafe (absolute or escaping) relative path. Verification runs before
any weight is parsed or executed, so a tampered artifact never reaches the runtime.
"""
+ if expected is None:
+ raw = os.environ.get("LAYA_SHA256_DIGESTS", "").strip()
+ if not raw:
+ return
+ try:
+ expected = json.loads(raw)
+ except json.JSONDecodeError as exc:
+ raise ValueError("LAYA_SHA256_DIGESTS must be a JSON object of artifact->sha256") from exc
+ if not isinstance(expected, dict):
+ raise ValueError("expected_sha256 must be a mapping of artifact paths to digests")
for rel, want in expected.items():
- rel_norm = str(rel).replace("\\", "/").lstrip("/")
- if not rel_norm or rel_norm == ".." or rel_norm.startswith("../") or "/../" in rel_norm:
- raise ValueError("laya: unsafe path in expected digests: %r" % (rel,))
- path = os.path.join(model_dir, rel_norm)
+ raw_rel = str(rel).replace("\\", "/")
+ if rel in ("onnx", "onnx_path") and onnx_path:
+ path = onnx_path
+ else:
+ if raw_rel.startswith("/") or os.path.isabs(raw_rel) or ntpath.isabs(raw_rel):
+ raise ValueError("laya: unsafe absolute path in expected digests: %r" % (rel,))
+ rel_norm = raw_rel.lstrip("/")
+ if not rel_norm or rel_norm == ".." or rel_norm.startswith("../") or "/../" in rel_norm:
+ raise ValueError("laya: unsafe path in expected digests: %r" % (rel,))
+ path = os.path.join(model_dir, rel_norm)
if not os.path.isfile(path):
raise FileNotFoundError(
"laya: cannot verify %r: no such file under %s" % (rel, model_dir))
diff --git a/laya/router.py b/laya/router.py
index 556bbfd8..c761cf02 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -198,6 +198,11 @@ class Router(HookRegistry):
r = Router(preload=True, device="cuda")
r.preload(["english", "multilingual"]) # or just the two you serve
+ Hub revisions are opt-in. `revision` applies one commit to every model;
+ `revisions={"english": "...", "multilingual": "..."}` overrides that per model,
+ which is useful when standalone repositories were reviewed at different commits.
+ Without either, huggingface_hub's normal default and existing offline cache are used.
+
Hooks are opt-in and run at the Router level: `on_route` sees the routing decision,
`on_load` / `on_evict` see model lifecycle, and `on_predict_start` / `on_predict_end`
wrap the whole route+infer call. See `laya.hooks`.
@@ -215,6 +220,7 @@ class Router(HookRegistry):
device: Optional[str] = None,
token: Optional[str] = None,
revision: Optional[str] = None,
+ revisions: Optional[Dict[str, Optional[str]]] = None,
max_loaded: int = 2,
default: str = "english",
auto_task_detection: bool = False,
@@ -238,9 +244,12 @@ class Router(HookRegistry):
self.device = device
self.token = token or os.environ.get("HF_TOKEN")
# Optional Hub revision (commit SHA/branch/tag) applied to every checkpoint load.
- # The published checkpoints pin to a reviewed SHA even without this; see
- # `laya.revisions.PINNED_REVISIONS`.
+ # `revisions` overrides it per normalized model name, for standalone repos whose
+ # reviewed commits differ.
self.revision = revision
+ self.revisions: Dict[str, Optional[str]] = {
+ normalise_name(k): v for k, v in (revisions or {}).items()
+ }
self.max_loaded = max(1, int(max_loaded))
self.default = normalise_name(default)
self.auto_task_detection = bool(auto_task_detection)
@@ -272,8 +281,11 @@ class Router(HookRegistry):
return self._agents[key]
from .agent import Agent
repo, sub = _split(self.models[key])
- agent = Agent(repo, device=self.device, token=self.token, subfolder=sub,
- revision=self.revision)
+ kwargs = {"device": self.device, "token": self.token, "subfolder": sub}
+ model_revision = self.revisions.get(key, self.revision)
+ if model_revision is not None:
+ kwargs["revision"] = model_revision
+ agent = Agent(repo, **kwargs)
self._agents[key] = agent
self._order.append(key)
evicted = self._evict_locked()
diff --git a/laya/serve.py b/laya/serve.py
index 8e73a93d..dc362901 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -242,7 +242,7 @@ def create_app(router: Optional[Any] = None):
return {
"status": "ok",
"loaded": router.loaded,
- "revisions": router.loaded_revisions,
+ "revisions": getattr(router, "loaded_revisions", {}),
"device": os.environ.get("LAYA_DEVICE") or "auto",
}
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 7b66d6ea..9a2342c7 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -64,8 +64,9 @@ for label, fn in (("Agent.__init__", Agent.__init__), ("load", load),
for param, default in HOOK_KEYS.items():
check_param(label, fn, param, default)
-# Router keeps lang_guess too
+# Router keeps lang_guess and explicit per-model revisions too
check_param("Router.__init__", Router.__init__, "lang_guess", None)
+check_param("Router.__init__", Router.__init__, "revisions", None)
# --------------------------------------------------------------- predict surfaces
for label, fn in (("Agent.predict_batch", Agent.predict_batch),
diff --git a/tests/test_revision_pinning.py b/tests/test_revision_pinning.py
index a0f8b55f..176e8dd4 100644
--- a/tests/test_revision_pinning.py
+++ b/tests/test_revision_pinning.py
@@ -38,14 +38,12 @@ def _capturing_snapshot(captured):
class ResolveRevisionTests(unittest.TestCase):
- def test_explicit_revision_wins(self):
+ def test_explicit_revision_is_returned(self):
self.assertEqual(resolve_revision("convaiinnovations/laya", "abc123"), "abc123")
- def test_published_repos_fall_back_to_the_pin(self):
- for repo, sha in PINNED_REVISIONS.items():
- self.assertEqual(resolve_revision(repo), sha)
- self.assertTrue(all(c in "0123456789abcdef" for c in sha), repo)
- self.assertEqual(len(sha), 40, repo)
+ def test_published_repos_keep_the_hub_default_without_an_explicit_pin(self):
+ for repo in PINNED_REVISIONS:
+ self.assertIsNone(resolve_revision(repo))
def test_unknown_repo_keeps_the_hub_default(self):
self.assertIsNone(resolve_revision("acme/custom-model"))
@@ -85,18 +83,30 @@ class VerifyDigestsTests(unittest.TestCase):
verify_digests(self.dir, {"absent.bin": self.digest})
def test_escaping_paths_rejected(self):
- for rel in ("../evil", "..", "a/../../evil", "\\..\\evil"):
+ for rel in ("../evil", "..", "a/../../evil", "\\..\\evil", "/absolute/evil", "C:\\absolute\\evil"):
with self.assertRaises(ValueError, msg=rel):
verify_digests(self.dir, {rel: self.digest})
+ def test_digest_map_can_come_from_environment(self):
+ with patch.dict(os.environ, {"LAYA_SHA256_DIGESTS": json.dumps({"weights.bin": self.digest})}):
+ verify_digests(self.dir)
+
+ def test_external_onnx_digest_is_supported(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ path = os.path.join(tmp, "model.onnx")
+ with open(path, "wb") as f:
+ f.write(b"onnx")
+ digest = hashlib.sha256(b"onnx").hexdigest()
+ verify_digests(self.dir, {"onnx": digest}, onnx_path=path)
+
class AgentPinningTests(unittest.TestCase):
- def test_hub_load_pins_published_repo(self):
+ def test_hub_load_keeps_the_default_revision(self):
captured = {}
with patch("huggingface_hub.snapshot_download", _capturing_snapshot(captured)):
with self.assertRaises(_StopLoad):
Agent("convaiinnovations/laya")
- self.assertEqual(captured["revision"], PINNED_REVISIONS["convaiinnovations/laya"])
+ self.assertNotIn("revision", captured)
def test_explicit_revision_overrides_the_pin(self):
captured = {}
@@ -128,9 +138,44 @@ class AgentPinningTests(unittest.TestCase):
class RouterRevisionTests(unittest.TestCase):
- def test_router_stores_an_explicit_revision(self):
- self.assertEqual(Router(revision="abc123").revision, "abc123")
+ def test_router_stores_explicit_revisions(self):
+ router = Router(revision="default", revisions={"ml": "multi-sha", "typed": "typed-sha"})
+ self.assertEqual(router.revision, "default")
+ self.assertEqual(router.revisions["multilingual"], "multi-sha")
+ self.assertEqual(router.revisions["typed-decisions"], "typed-sha")
self.assertIsNone(Router().revision)
+ self.assertEqual(Router().revisions, {})
+
+ def test_router_forwards_only_the_selected_per_model_revision(self):
+ import laya.agent
+
+ captured = []
+
+ class FakeAgent:
+ def __init__(self, repo, **kwargs):
+ captured.append((repo, kwargs))
+
+ with patch.object(laya.agent, "Agent", FakeAgent):
+ router = Router(revision="default", revisions={"multilingual": "multi-sha"})
+ router.load("english")
+ router.load("multi")
+
+ self.assertEqual(captured[0][1]["revision"], "default")
+ self.assertEqual(captured[1][1]["revision"], "multi-sha")
+
+ def test_router_omits_revision_when_none_is_configured(self):
+ import laya.agent
+
+ captured = {}
+
+ class FakeAgent:
+ def __init__(self, repo, **kwargs):
+ captured.update(kwargs)
+
+ with patch.object(laya.agent, "Agent", FakeAgent):
+ Router().load("english")
+
+ self.assertNotIn("revision", captured)
def test_loaded_revisions_reports_resident_agents(self):
router = Router()
diff --git a/tests/test_serve.py b/tests/test_serve.py
index f2b80abb..d128f8d0 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -201,10 +201,14 @@ def test_body_read_preserves_parse_error_codes(monkeypatch):
assert r.status_code == 400, (payload, r.status_code)
-def test_health(monkeypatch):
+def test_health_supports_router_without_loaded_revisions(monkeypatch):
+ # FakeRouter deliberately has no loaded_revisions attribute. Injected test or
+ # embedding routers predating revision reporting must remain health-compatible.
client, _ = _client(monkeypatch)
r = client.get("/health")
- assert r.status_code == 200 and r.json()["status"] == "ok"
+ assert r.status_code == 200
+ assert r.json()["status"] == "ok"
+ assert r.json()["revisions"] == {}
def test_helpers():