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(ts): make split ONNX loadable in the browser

nnlgsakib293db178a706
diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.ts
index d423ae85..5b788845 100644
--- a/laya-ts/src/providers.ts
+++ b/laya-ts/src/providers.ts
@@ -508,6 +508,18 @@ export async function createNodeProvider(
};
}
+/** Best-effort fetch of a `<model>.data` sidecar; null when the model is single-file. */
+async function fetchSidecar(url: string): Promise<{ path: string; data: Uint8Array } | null> {
+ const name = `${url.split("/").pop()}.data`;
+ const sidecarUrl = `${url.replace(/\/+$/, "").split("/").slice(0, -1).join("/")}/${name}`;
+ try {
+ const buf = await fetchArrayBuffer(sidecarUrl);
+ return { path: name, data: new Uint8Array(buf) };
+ } catch {
+ return null;
+ }
+}
+
export async function createWebProvider(
modelUrl: string,
opts?: ProviderOptions,
@@ -535,27 +547,69 @@ export async function createWebProvider(
await expectDigest("encoder.onnx", encBuf, opts.expectedSha256);
await expectDigest("head.onnx", headBuf, opts.expectedSha256);
}
+ // Split ONNX references its weights relatively ("encoder.onnx.data"); buffered
+ // sessions have no filesystem, so mount the sidecar via externalData.
+ const encSidecar = await fetchSidecar(encUrl);
+ const headSidecar = await fetchSidecar(headUrl);
+ const encExtra = encSidecar ? { externalData: [encSidecar] } : {};
+ const headExtra = headSidecar ? { externalData: [headSidecar] } : {};
let enc: any;
try {
+ // "basic" skips the Skip+LayerNorm fusion whose fused Beta shape the
+ // WebGPU kernel rejects; unfused LayerNormalization runs fine on GPU.
enc = await ort.InferenceSession.create(new Uint8Array(encBuf), {
executionProviders: ["webgpu", "wasm"],
+ graphOptimizationLevel: "basic",
+ ...encExtra,
});
} catch (e) {
enc = await ort.InferenceSession.create(new Uint8Array(encBuf), {
executionProviders: ["wasm"],
+ ...encExtra,
});
}
const head = await ort.InferenceSession.create(new Uint8Array(headBuf), {
executionProviders: ["wasm"],
+ ...headExtra,
});
+ // Lazy WASM encoder: some graphs pass WebGPU session creation but hit an
+ // unsupported kernel at run time (e.g. SkipLayerNormalization shape gaps).
+ // On the first such failure we build a WASM session and stick with it.
+ let encWasm: any = null;
+ const runEncoderOn = async (session: any, b: Batch) => {
+ const out = await session.run(feed(ort, b));
+ const t = pickOutput(out, ["last_hidden_state", "lastHidden", "hidden_states"]);
+ return { lastHidden: toNested(t.data, t.dims) };
+ };
return {
runEncoder: async (b) => {
+ if (encWasm) {
+ try {
+ return await runEncoderOn(encWasm, b);
+ } catch (e) {
+ if (isOomError(e)) throw new Error(`${(e as Error).message} (out of memory; try fewer questions per call)`);
+ throw e;
+ }
+ }
try {
- const out = await enc.run(feed(ort, b));
- const t = pickOutput(out, ["last_hidden_state", "lastHidden", "hidden_states"]);
- return { lastHidden: toNested(t.data, t.dims) };
+ return await runEncoderOn(enc, b);
} catch (e) {
if (isOomError(e)) throw new Error(`${(e as Error).message} (WebGPU out of memory; WASM fallback already active)`);
+ if (/\[webgpu\]/i.test(String((e as Error)?.message ?? e))) {
+ console.warn(
+ `laya: WebGPU encoder run failed (${String((e as Error)?.message ?? e)}); falling back to WASM.`,
+ );
+ encWasm ??= await ort.InferenceSession.create(new Uint8Array(encBuf), {
+ executionProviders: ["wasm"],
+ ...encExtra,
+ });
+ try {
+ return await runEncoderOn(encWasm, b);
+ } catch (e2) {
+ if (isOomError(e2)) throw new Error(`${(e2 as Error).message} (out of memory; try fewer questions per call)`);
+ throw e2;
+ }
+ }
throw e;
}
},