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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(ts): loop-based tensor plumbing, cure spread overflow
nnlgsakib9a64729fdda0
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.tsindex 323bad34..86e184ca 100644--- a/laya-ts/src/common.ts+++ b/laya-ts/src/common.ts@@ -126,6 +126,12 @@ export function tempBucket(qtype: number, k: number): string {const size = k <= 2 ? "2" : k <= 5 ? "3-5" : k <= 10 ? "6-10" : "11+";return `${["choice", "score", "noul"][qtype]}:${size}`;}+/** Max of a length list without spread (Math.max(...arr) throws RangeError past ~100k args). */+export function maxOf(values: ArrayLike<number>, fallback = 0): number {+ let m = fallback;+ for (let i = 0; i < values.length; i++) if (values[i] > m) m = values[i];+ return m;+}export interface CollateItem {ids: number[];markers: number[];@@ -148,8 +154,12 @@ export interface CollatedBatch {export function collateItems(batch: CollateItem[][], padId: number): CollatedBatch | null {const items = (batch ?? []).flat();if (items.length === 0) return null;- const L = Math.max(...items.map((it) => it.ids.length));- const K = Math.max(...items.map((it) => it.markers.length));+ let L = 0;+ let K = 0;+ for (const it of items) {+ if (it.ids.length > L) L = it.ids.length;+ if (it.markers.length > K) K = it.markers.length;+ }const hasTarget = items.some((it) => "target" in it);const inputIds = items.map((it) => [...it.ids, ...Array(L - it.ids.length).fill(padId)]);const attentionMask = items.map((it) => [...Array(it.ids.length).fill(1), ...Array(L - it.ids.length).fill(0)]);diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.tsindex 01670908..79dee573 100644--- a/laya-ts/src/providers.ts+++ b/laya-ts/src/providers.ts@@ -15,27 +15,47 @@ export interface SessionProvider {}function toNested(data: ArrayLike<number | bigint | boolean>, dims: number[]): any {- const flat = Array.from(data as any, (v: any) => (typeof v === "bigint" ? Number(v) : v));+ // Single-pass copy + precomputed steps; no per-node slice/reduce.+ let total = 1;+ for (const d of dims) total *= d;+ const flat = new Array(total);+ for (let i = 0; i < total; i++) {+ const v = (data as any)[i];+ flat[i] = typeof v === "bigint" ? Number(v) : v;+ }if (dims.length === 0) return flat[0];+ const steps: number[] = new Array(dims.length);+ for (let d = 0; d < dims.length; d++) {+ let s = 1;+ for (let k = d + 1; k < dims.length; k++) s *= dims[k];+ steps[d] = s;+ }const rec = (d: number, off: number): any => {if (d === dims.length - 1) return flat.slice(off, off + dims[d]);- const step = dims.slice(d + 1).reduce((a, b) => a * b, 1);- const out: any[] = [];- for (let i = 0; i < dims[d]; i++) out.push(rec(d + 1, off + i * step));+ const out: any[] = new Array(dims[d]);+ for (let i = 0; i < dims[d]; i++) out[i] = rec(d + 1, off + i * steps[d]);return out;};return rec(0, 0);}function i64(ort: any, arr: number[] | number[][], dims: number[]): any {- const flat = (arr as any).flat(Infinity).map((v: number) => BigInt(Math.trunc(v)));- return new ort.Tensor("int64", BigInt64Array.from(flat), dims);+ // Rank <= 2 by construction; direct loop avoids flat(Infinity) intermediates.+ const out = new BigInt64Array(dims.reduce((a, b) => a * b, 1));+ let p = 0;+ if (Array.isArray((arr as any)[0])) {+ for (const row of arr as number[][]) for (const v of row) out[p++] = BigInt(Math.trunc(v));+ } else {+ for (const v of arr as number[]) out[p++] = BigInt(Math.trunc(v));+ }+ return new ort.Tensor("int64", out, dims);}/** Encoder feeds: input_ids + attention_mask (int64). */export function feed(ort: any, b: Batch): Record<string, any> {const n = b.inputIds.length;- const L = Math.max(1, ...b.inputIds.map((r) => r.length));+ let L = 1;+ for (const r of b.inputIds) if (r.length > L) L = r.length;return {input_ids: i64(ort, b.inputIds, [n, L]),attention_mask: i64(ort, b.attentionMask, [n, L]),@@ -45,13 +65,22 @@ export function feed(ort: any, b: Batch): Record<string, any> {/** Head feeds: encoder hidden + marker_pos/mask + qtype. */export function feedHead(ort: any, hidden: number[][][] | any, b: Batch): Record<string, any> {const n = b.markerPos.length;- const k = Math.max(1, ...b.markerPos.map((r) => r.length));- const H = new ort.Tensor(- "float32",- Float32Array.from((hidden as any).flat(Infinity).map(Number)),- [(hidden as any).length ?? n, (hidden as any)[0]?.length ?? 1, (hidden as any)[0]?.[0]?.length ?? 1],- );+ let k = 1;+ for (const r of b.markerPos) if (r.length > k) k = r.length;+ // Direct flatten into typed arrays; no flat(Infinity)+map intermediates.+ const nH = (hidden as any).length ?? n;const S = (hidden as any)[0]?.length ?? 1;+ const Hd = (hidden as any)[0]?.[0]?.length ?? 1;+ const flatH = new Float32Array(nH * S * Hd);+ let p = 0;+ for (let i = 0; i < nH; i++) {+ const bi = (hidden as any)[i] ?? [];+ for (let j = 0; j < S; j++) {+ const hj = bi[j] ?? [];+ for (let h = 0; h < Hd; h++) flatH[p++] = Number(hj[h] ?? 0);+ }+ }+ const H = new ort.Tensor("float32", flatH, [nH, S, Hd]);// Pad/trim mask rows to S so the mask always matches hidden_states even// if a caller passes unpadded rows.const maskRows = b.attentionMask.map((r) => {@@ -62,11 +91,13 @@ export function feedHead(ort: any, hidden: number[][][] | any, b: Batch): Recordreturn {hidden_states: H,marker_pos: i64(ort, b.markerPos, [n, k]),- marker_mask: new ort.Tensor(- "bool",- Uint8Array.from((b.markerMask as any).flat(Infinity).map((v: any) => (v ? 1 : 0))),- [n, k],- ),+ marker_mask: new ort.Tensor("bool", (() => {+ const out = new Uint8Array(n * k);+ let q = 0;+ for (const row of b.markerMask as unknown as boolean[][])+ for (let j = 0; j < k; j++) out[q++] = row[j] ? 1 : 0;+ return out;+ })(), [n, k]),qtype: i64(ort, b.qtype.map((v) => [v]), [n, 1]),// Padding mask for the head transformer (py DecisionModel.forward).// Without it, batch mates of unequal length corrupt each other's markers.diff --git a/laya-ts/src/shortlist.ts b/laya-ts/src/shortlist.tsindex 89b8d0f0..1aa67eb3 100644--- a/laya-ts/src/shortlist.ts+++ b/laya-ts/src/shortlist.ts@@ -1,4 +1,4 @@-import { renderOptions, serializeState } from "./common.js";+import { maxOf, renderOptions, serializeState } from "./common.js";import type { Batch } from "./providers.js";import type { QuestionDef, SystemOneResult } from "./agent.js";@@ -189,7 +189,7 @@ export function embedFnFromAgent(agent: unknown, maxLength = 512, batchSize = 32for (let s = 0; s < rows.length; s += batchSize) {const chunk = rows.slice(s, s + batchSize);const ids = chunk.map((t) => tok.encode(t).slice(0, maxLength));- const L = Math.max(1, ...ids.map((r) => r.length));+ const L = maxOf(ids.map((r) => r.length), 1);const pad = tok.padId ?? 0;const batch: Batch = {inputIds: ids.map((r) => [...r, ...Array(L - r.length).fill(pad)]),diff --git a/laya-ts/tests/flat-parity.test.ts b/laya-ts/tests/flat-parity.test.tsnew file mode 100644index 00000000..7c5ab323--- /dev/null+++ b/laya-ts/tests/flat-parity.test.ts@@ -0,0 +1,47 @@+import { describe, expect, it } from "vitest";+import { feed, feedHead } from "../src/providers.js";++const ort = {+ Tensor: class {+ type: string;+ data: any;+ dims: number[];+ constructor(type: string, data: any, dims: number[]) {+ this.type = type;+ this.data = data;+ this.dims = dims;+ }+ },+};++describe("flat parity", () => {+ it("feedHead flattens nested hidden without value drift", () => {+ const hidden = [+ [+ [1, 2],+ [3, 4],+ ],+ ];+ const batch: any = {+ inputIds: [[7, 8]],+ attentionMask: [[1, 1]],+ markerPos: [[0, 1]],+ markerMask: [[true, true]],+ qtype: [0],+ };+ const out = feedHead(ort, hidden, batch);+ expect(out.hidden_states.dims).toEqual([1, 2, 2]);+ expect(Array.from(out.hidden_states.data)).toEqual([1, 2, 3, 4]);+ });+ it("feed builds int64 ids+mask", () => {+ const out = feed(ort, {+ inputIds: [[5, 6]],+ attentionMask: [[1, 1]],+ markerPos: [[0]],+ markerMask: [[true]],+ qtype: [0],+ } as any);+ expect(out.input_ids.dims).toEqual([1, 2]);+ expect(Array.from(out.input_ids.data)).toEqual([5n, 6n]);+ });+});diff --git a/laya-ts/tests/maxlen.test.ts b/laya-ts/tests/maxlen.test.tsnew file mode 100644index 00000000..1be38e8f--- /dev/null+++ b/laya-ts/tests/maxlen.test.ts@@ -0,0 +1,14 @@+import { describe, expect, it } from "vitest";+import { maxOf } from "../src/common.js";++describe("maxOf", () => {+ it("returns max without spread overflow", () => {+ expect(maxOf([1, 5, 3], 1)).toBe(5);+ expect(maxOf([], 1)).toBe(1);+ });+ it("handles 300k rows without RangeError", () => {+ const lens = new Array(300_000).fill(1);+ lens[123456] = 77;+ expect(maxOf(lens, 1)).toBe(77);+ });+});