@margoengine/compute 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +804 -2
  4. package/dist/array-C29noZq7.js +166 -0
  5. package/dist/array.d.ts +109 -0
  6. package/dist/autodiff.d.ts +22 -0
  7. package/dist/autodiff.js +143 -0
  8. package/dist/autotune.d.ts +100 -0
  9. package/dist/autotune.js +179 -0
  10. package/dist/compat-YK57ZCbA.js +4 -0
  11. package/dist/compat.d.ts +2 -0
  12. package/dist/context-CCvs3wkC.js +2479 -0
  13. package/dist/context.d.ts +376 -0
  14. package/dist/cpu-D4UQwJ9L.js +871 -0
  15. package/dist/cpu-kernel.wasm +0 -0
  16. package/dist/cpu-load-XFcM1TXj.js +5 -0
  17. package/dist/cpu-load.d.ts +3 -0
  18. package/dist/cpu-runtime.d.ts +8 -0
  19. package/dist/cpu-wasm.d.ts +42 -0
  20. package/dist/cpu-wasm.js +161 -0
  21. package/dist/cpu.d.ts +138 -0
  22. package/dist/decomp-kernel.wasm +0 -0
  23. package/dist/decomp.d.ts +42 -0
  24. package/dist/decomp.js +158 -0
  25. package/dist/distance.d.ts +85 -0
  26. package/dist/distance.js +256 -0
  27. package/dist/dtype.d.ts +36 -0
  28. package/dist/errors-CDgOgzCd.js +7 -0
  29. package/dist/expr-t3jm8acM.js +996 -0
  30. package/dist/expr.d.ts +196 -0
  31. package/dist/fft.d.ts +33 -0
  32. package/dist/fft.js +168 -0
  33. package/dist/frame.d.ts +76 -0
  34. package/dist/frame.js +85 -0
  35. package/dist/graph.d.ts +75 -0
  36. package/dist/graph.js +174 -0
  37. package/dist/histogram.d.ts +17 -0
  38. package/dist/index.d.ts +17 -0
  39. package/dist/index.js +4 -0
  40. package/dist/kernel.d.ts +65 -0
  41. package/dist/linalg.d.ts +21 -0
  42. package/dist/linalg.js +149 -0
  43. package/dist/nn-extra.d.ts +130 -0
  44. package/dist/nn-extra.js +322 -0
  45. package/dist/nn.d.ts +385 -0
  46. package/dist/nn.js +704 -0
  47. package/dist/onnx-image-D2qfYsXf.js +66 -0
  48. package/dist/onnx-image.d.ts +10 -0
  49. package/dist/onnx-kernel.wasm +0 -0
  50. package/dist/onnx-regex-DaesIdrm.js +331 -0
  51. package/dist/onnx-regex.d.ts +2 -0
  52. package/dist/onnx.d.ts +167 -0
  53. package/dist/onnx.js +885 -0
  54. package/dist/quant.d.ts +59 -0
  55. package/dist/quant.js +242 -0
  56. package/dist/random.d.ts +64 -0
  57. package/dist/random.js +150 -0
  58. package/dist/reduce.d.ts +34 -0
  59. package/dist/scan.d.ts +26 -0
  60. package/dist/select.d.ts +38 -0
  61. package/dist/shaders.d.ts +80 -0
  62. package/dist/shape.d.ts +23 -0
  63. package/dist/shape.js +134 -0
  64. package/dist/signal.d.ts +29 -0
  65. package/dist/signal.js +178 -0
  66. package/dist/sort.d.ts +38 -0
  67. package/dist/sparse.d.ts +136 -0
  68. package/dist/sparse.js +273 -0
  69. package/dist/stream.d.ts +39 -0
  70. package/dist/stream.js +100 -0
  71. package/dist/subgroups.d.ts +27 -0
  72. package/dist/subgroups.js +191 -0
  73. package/dist/usingCtx-igdMnAhr.js +57 -0
  74. package/dist/wgsl.d.ts +68 -0
  75. package/package.json +146 -4
package/dist/onnx.js ADDED
@@ -0,0 +1,885 @@
1
+ import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
2
+ //#region src/onnx.ts
3
+ var r = /* @__PURE__ */ ".float32.uint8.int8.uint16.int16.int32.int64.string.bool.float16.float64.uint32.uint64.complex64.complex128.bfloat16.float8e4m3fn.float8e4m3fnuz.float8e5m2.float8e5m2fnuz.uint4.int4.float4e2m1.float8e8m0.uint2.int2.float6e2m3.float6e3m2".split("."), i = [
4
+ 0,
5
+ 4,
6
+ 1,
7
+ 1,
8
+ 2,
9
+ 2,
10
+ 4,
11
+ 8,
12
+ 0,
13
+ 1,
14
+ 2,
15
+ 8,
16
+ 4,
17
+ 8,
18
+ 8,
19
+ 16,
20
+ 2,
21
+ 1,
22
+ 1,
23
+ 1,
24
+ 1,
25
+ .5,
26
+ .5,
27
+ .5,
28
+ 1,
29
+ .25,
30
+ .25,
31
+ .75,
32
+ .75
33
+ ], a = [
34
+ null,
35
+ Float32Array,
36
+ Uint8Array,
37
+ Int8Array,
38
+ Uint16Array,
39
+ Int16Array,
40
+ Int32Array,
41
+ BigInt64Array,
42
+ null,
43
+ Uint8Array,
44
+ Uint16Array,
45
+ Float64Array,
46
+ Uint32Array,
47
+ BigUint64Array,
48
+ Float32Array,
49
+ Float64Array,
50
+ Uint16Array,
51
+ Uint8Array,
52
+ Uint8Array,
53
+ Uint8Array,
54
+ Uint8Array,
55
+ Uint8Array,
56
+ Uint8Array,
57
+ Uint8Array,
58
+ Uint8Array,
59
+ Uint8Array,
60
+ Uint8Array,
61
+ Uint8Array,
62
+ Uint8Array
63
+ ], o = new TextEncoder(), s = new TextDecoder("utf-8", { fatal: !0 });
64
+ function c(e) {
65
+ throw t(`ONNX: ${e}`, "compute");
66
+ }
67
+ function l(t) {
68
+ if (t.some((e) => !Number.isSafeInteger(e) || e < 0 || e > 2147483647) && c("dimensions must be integers in 0..2147483647"), t.includes(0)) return 0;
69
+ let n = t.reduce((e, t) => e * t, 1);
70
+ if (!Number.isSafeInteger(n) || n > 16777216) throw e("ONNX tensor exceeds 16777216 elements; split the tensor", "compute");
71
+ return n;
72
+ }
73
+ var u = class {
74
+ constructor(e, t, n) {
75
+ let o = r.indexOf(e), s = l(n);
76
+ if (o < 1 && c(`unknown tensor dtype ${String(e)}`), o === 8) (!Array.isArray(t) || t.some((e) => typeof e != "string") || t.length !== s) && c("string data must contain one string per element"), this.data = Object.freeze([...t]);
77
+ else {
78
+ let n = a[o];
79
+ (!n || !(t instanceof n) || t.byteLength !== Math.ceil(s * i[o])) && c(`${e} data has the wrong typed array or length`), this.data = t.slice(), o === 9 && this.data.some((e) => e > 1) && c("bool data must contain only 0 or 1");
80
+ }
81
+ this.dtype = e, this.shape = Object.freeze([...n]);
82
+ }
83
+ };
84
+ function d(e) {
85
+ e?.throwIfAborted();
86
+ }
87
+ async function f(e, t) {
88
+ if (d(t), e.protocol === "file:") return (await import(
89
+ /* @vite-ignore */
90
+ /* webpackIgnore: true */
91
+ "node:fs/promises"
92
+ )).readFile(e, { signal: t });
93
+ let r = await fetch(e, { signal: t });
94
+ if (!r.ok) throw n("failed", `ONNX load failed: HTTP ${r.status}`, "compute");
95
+ let i = new Uint8Array(await r.arrayBuffer());
96
+ return d(t), i;
97
+ }
98
+ function p(e, t, n) {
99
+ return JSON.stringify([
100
+ e === "ai.onnx" ? "" : e,
101
+ t,
102
+ n
103
+ ]);
104
+ }
105
+ function m(e) {
106
+ return e.flatMap((e) => e.kind === 5 ? [e.value] : e.kind === 10 ? e.value : []);
107
+ }
108
+ function h(e) {
109
+ let t = new Set(e.inputs.filter(Boolean));
110
+ for (let n of m(e.attributes)) {
111
+ let e = /* @__PURE__ */ new Set([
112
+ ...n.inputs.map((e) => e.name),
113
+ ...n.initializers.map((e) => e.name),
114
+ ...n.nodes.flatMap((e) => e.outputs)
115
+ ]);
116
+ for (let r of n.nodes) for (let n of h(r)) e.has(n) || t.add(n);
117
+ for (let r of n.outputs) e.has(r.name) || t.add(r.name);
118
+ }
119
+ return t;
120
+ }
121
+ var g = class {
122
+ #e;
123
+ #t;
124
+ #n = /* @__PURE__ */ new Map();
125
+ #r = /* @__PURE__ */ new Map();
126
+ #i = /* @__PURE__ */ new Map();
127
+ #a = /* @__PURE__ */ new WeakMap();
128
+ #o = /* @__PURE__ */ new WeakMap();
129
+ #s = /* @__PURE__ */ new Map();
130
+ #c = !1;
131
+ #l = !1;
132
+ constructor(e, t) {
133
+ this.#e = e, this.#t = t, this.inputs = Object.freeze(t.graph.inputs), this.outputs = Object.freeze(t.graph.outputs), this.metadata = Object.freeze({ ...t.metadata }), this.producer = t.producer, this.irVersion = t.irVersion;
134
+ for (let e of t.functions) {
135
+ let t = p(e.domain, e.name, e.overload);
136
+ this.#n.has(t) && c(`duplicate local function ${e.domain}:${e.name}`), this.#n.set(t, e);
137
+ }
138
+ this.#g(t.graph, t.opsets, /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), 0);
139
+ }
140
+ get work() {
141
+ return this.#u(), {
142
+ simdGroups: this.#e.simd_groups(),
143
+ liveBytes: this.#e.live_bytes(),
144
+ validatedFunctions: this.#s.size
145
+ };
146
+ }
147
+ #u() {
148
+ if (this.#c) throw n("disposed", "ONNX model is disposed; load a new session", "compute");
149
+ }
150
+ #d() {
151
+ return s.decode(new Uint8Array(this.#e.memory.buffer, this.#e.message_ptr(), this.#e.message_len()));
152
+ }
153
+ #f(e) {
154
+ if (e < 0) throw n("failed", `ONNX: ${this.#d()}`, "compute");
155
+ return e;
156
+ }
157
+ #p(e) {
158
+ this.#u();
159
+ let t = this.#e.reserve(e.length);
160
+ t === 4294967295 && this.#f(-1), new Uint8Array(this.#e.memory.buffer, t, e.length).set(e);
161
+ }
162
+ #m(e) {
163
+ this.#p(new Uint8Array(Uint32Array.from(e).buffer));
164
+ }
165
+ #h(e, t) {
166
+ if (!e.attributes.some((e) => e.reference)) return {
167
+ id: e.attributeSet,
168
+ attrs: e.attributes
169
+ };
170
+ let n = JSON.stringify([e.attributeSet, t]), r = this.#r.get(n);
171
+ return r || (this.#m(t), r = {
172
+ id: this.#f(this.#e.resolve_attributes(e.attributeSet, t.length)),
173
+ attrs: JSON.parse(this.#d())
174
+ }, this.#r.set(n, r)), r;
175
+ }
176
+ #g(e, t, r, i, a) {
177
+ a > 64 && c("graph/function nesting exceeds 64");
178
+ let o = 0, s = new Set(r), l = /* @__PURE__ */ new Set();
179
+ for (let t of e.inputs) (!t.name || l.has(t.name)) && c(`duplicate graph input ${t.name}`), l.add(t.name), s.add(t.name);
180
+ let u = /* @__PURE__ */ new Set();
181
+ for (let t of e.initializers) (!t.name || u.has(t.name)) && c(`duplicate or unnamed initializer ${t.name}`), u.add(t.name), l.add(t.name), s.add(t.name);
182
+ for (let u of e.nodes) {
183
+ for (let e of u.inputs) e && !s.has(e) && c(`${u.name || u.op} reads unavailable value ${e}`);
184
+ let e = u.domain === "ai.onnx" ? "" : u.domain, d = t[e] ?? (this.irVersion < 3 && !e ? 1 : void 0);
185
+ d === void 0 && c(`no opset import for domain ${e}`);
186
+ let f = this.#n.get(p(e, u.op, u.overload));
187
+ if (f) {
188
+ let t = p(e, u.op, u.overload);
189
+ i.has(t) && c(`recursive local function ${u.op}`), (u.inputs.length !== f.inputs.length || u.outputs.length !== f.outputs.length) && c(`function ${u.op} arity differs`);
190
+ let n = this.#s.get(t);
191
+ if (n === void 0) {
192
+ let e = new Set(i);
193
+ e.add(t), n = 1 + this.#g({
194
+ name: f.name,
195
+ nodes: f.nodes,
196
+ inputs: f.inputs.map((e) => ({
197
+ name: e,
198
+ type: null
199
+ })),
200
+ outputs: f.outputs.map((e) => ({
201
+ name: e,
202
+ type: null
203
+ })),
204
+ initializers: []
205
+ }, f.opsets, /* @__PURE__ */ new Set(), e, a + 1), this.#s.set(t, n);
206
+ }
207
+ a + n > 64 && c("graph/function nesting exceeds 64"), o = Math.max(o, n);
208
+ } else if (!(e === "ai.onnx.ml" && [
209
+ "ZipMap",
210
+ "CastMap",
211
+ "DictVectorizer"
212
+ ].includes(u.op) || !e && [
213
+ "If",
214
+ "Loop",
215
+ "Scan",
216
+ "SequenceEmpty",
217
+ "SequenceConstruct",
218
+ "SequenceAt",
219
+ "SequenceErase",
220
+ "SequenceInsert",
221
+ "SequenceLength",
222
+ "SplitToSequence",
223
+ "ConcatFromSequence",
224
+ "Optional",
225
+ "OptionalGetElement",
226
+ "OptionalHasElement",
227
+ "SequenceMap",
228
+ "StringNormalizer",
229
+ "RegexFullMatch",
230
+ "ImageDecoder"
231
+ ].includes(u.op)) && !this.#e.supported(u.kernelIndex, d)) throw n("unsupported", `ONNX operator ${e || "ai.onnx"}:${u.op} opset ${d} has no kernel or model-local function`, "compute");
232
+ for (let e of m(u.attributes)) o = Math.max(o, 1 + this.#g(e, t, s, i, a + 1));
233
+ for (let e of u.outputs) e && ((l.has(e) || r.has(e)) && c(`value ${e} is assigned more than once`), l.add(e), s.add(e));
234
+ }
235
+ let d = /* @__PURE__ */ new Set();
236
+ for (let t of e.outputs) (!s.has(t.name) || d.has(t.name)) && c(`missing or duplicate graph output ${t.name}`), d.add(t.name);
237
+ return o;
238
+ }
239
+ #_(e, t) {
240
+ let n = new Uint8Array(Uint32Array.from(e.shape).buffer), i;
241
+ if (e.dtype === "string") {
242
+ let t = e.data.map((e) => o.encode(e)), n = 4 + t.reduce((e, t) => e + 4 + t.length, 0);
243
+ i = new Uint8Array(n);
244
+ let r = new DataView(i.buffer);
245
+ r.setUint32(0, t.length, !0);
246
+ let a = 4;
247
+ for (let e of t) r.setUint32(a, e.length, !0), i.set(e, a + 4), a += 4 + e.length;
248
+ } else {
249
+ let t = e.data;
250
+ i = new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
251
+ }
252
+ let a = new Uint8Array(n.length + i.length);
253
+ a.set(n), a.set(i, n.length), this.#p(a);
254
+ let s = this.#f(this.#e.create_tensor(r.indexOf(e.dtype), e.shape.length, a.length));
255
+ return t.add(s), s;
256
+ }
257
+ #v(e) {
258
+ this.#f(this.#e.describe(e));
259
+ let t = JSON.parse(this.#d()), n = r[t.kind];
260
+ if (t.kind === 8) return new u(n, t.strings, t.shape);
261
+ let i = this.#e.data_ptr(e);
262
+ i === 4294967295 && this.#f(-1);
263
+ let o = this.#e.data_len(e), s = a[t.kind], c = new s(this.#e.memory.buffer.slice(i, i + o));
264
+ return t.kind === 9 && c.forEach((e, t) => {
265
+ c[t] = Number(e !== 0);
266
+ }), new u(n, c, t.shape);
267
+ }
268
+ #y(e, t, n = 0, r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Map()) {
269
+ if (n > 64 && c("input container nesting exceeds 64"), e === null) return null;
270
+ if (i.has(e)) return i.get(e);
271
+ r.has(e) && c("input containers must be acyclic"), r.add(e);
272
+ let a;
273
+ if (e instanceof u) a = this.#_(e, t);
274
+ else if (Array.isArray(e)) a = e.map((e) => this.#y(e, t, n + 1, r, i));
275
+ else if (e instanceof Map) {
276
+ let o = /* @__PURE__ */ new Map();
277
+ for (let [a, s] of e) {
278
+ typeof a != "string" && typeof a != "bigint" && (typeof a != "number" || !Number.isSafeInteger(a)) && c("map keys must be strings, bigint integers or safe integer numbers");
279
+ let e = typeof a == "number" ? BigInt(a) : a;
280
+ typeof e == "bigint" && (e < -(1n << 63n) || e >= 1n << 64n) && c("map key is outside integer64 storage"), o.has(e) && c("map contains duplicate integer keys represented as number and bigint"), o.set(e, this.#y(s, t, n + 1, r, i));
281
+ }
282
+ a = o;
283
+ } else return c("feeds must contain OnnxTensor, sequences, maps or null");
284
+ return r.delete(e), i.set(e, a), a;
285
+ }
286
+ #b(e, t = /* @__PURE__ */ new Map()) {
287
+ if (e === null) return null;
288
+ if (t.has(e)) return t.get(e);
289
+ let n;
290
+ return n = typeof e == "number" ? this.#v(e) : Array.isArray(e) ? e.map((e) => this.#b(e, t)) : new Map([...e].map(([e, n]) => [e, this.#b(n, t)])), t.set(e, n), n;
291
+ }
292
+ #x(e, t, n, i = /* @__PURE__ */ new WeakMap()) {
293
+ if (!t) return;
294
+ if (typeof e == "object" && e) {
295
+ let n = i.get(e) ?? /* @__PURE__ */ new Set();
296
+ if (n.has(t)) return;
297
+ n.add(t), i.set(e, n);
298
+ }
299
+ if (t.tag === "optional") {
300
+ e !== null && this.#x(e, t.element, n, i);
301
+ return;
302
+ }
303
+ if (t.tag === "sequence") {
304
+ if (Array.isArray(e) || c("sequence value expected"), t.element?.tag === "tensor") {
305
+ let n = this.#o.get(e);
306
+ n !== void 0 && n !== t.element.kind && c("sequence element type differs from its declaration"), this.#o.set(e, t.element.kind);
307
+ }
308
+ for (let r of e) this.#x(r, t.element, n, i);
309
+ return;
310
+ }
311
+ if (t.tag === "map") {
312
+ e instanceof Map || c("map value expected"), t.element?.tag === "tensor" && this.#a.set(e, t.element.kind);
313
+ for (let [r, a] of e) {
314
+ if ((t.keyKind === 8 ? typeof r != "string" : typeof r != "bigint" && typeof r != "number") && c("map key has the wrong type"), t.keyKind !== 8) {
315
+ let e = BigInt(r), n = t.keyKind === 6 || t.keyKind === 12 ? 32n : 64n, i = t.keyKind === 12 || t.keyKind === 13, a = i ? 0n : -(1n << n - 1n), o = i ? 1n << n : 1n << n - 1n;
316
+ (e < a || e >= o) && c("map key is outside its declared integer dtype");
317
+ }
318
+ this.#x(a, t.element, n, i);
319
+ }
320
+ return;
321
+ }
322
+ typeof e != "number" && c("tensor value expected"), this.#f(this.#e.describe(e));
323
+ let a = JSON.parse(this.#d());
324
+ a.kind !== t.kind && c(`tensor type ${r[a.kind]} differs from declared ${r[t.kind]}`), t.shape && (a.shape.length !== t.shape.length && c("tensor rank differs from declared shape"), t.shape.forEach((e, t) => {
325
+ if (typeof e == "number" && e !== a.shape[t] && c(`tensor dimension ${t} differs from declared shape`), typeof e == "string" && e) {
326
+ let r = n.get(e);
327
+ r !== void 0 && r !== a.shape[t] && c(`symbolic dimension ${e} is inconsistent`), n.set(e, a.shape[t]);
328
+ }
329
+ }));
330
+ }
331
+ #S(e) {
332
+ return typeof e != "number" && c("tensor input expected"), e;
333
+ }
334
+ #C(e, t) {
335
+ return t !== void 0 && this.#o.set(e, t), e;
336
+ }
337
+ #w(e) {
338
+ let t = this.#v(this.#S(e));
339
+ (l(t.shape) !== 1 || t.dtype === "string") && c("numeric scalar input expected");
340
+ let n = t.data[0], r = Number(n);
341
+ return Number.isSafeInteger(r) || c("control/index scalar must be a safe integer"), r;
342
+ }
343
+ #T(e) {
344
+ return this.#f(this.#e.describe(this.#S(e))), JSON.parse(this.#d());
345
+ }
346
+ #E(e, t, n, r, i) {
347
+ let a = this.#f(this.#e.slice_tensor(this.#S(e), t, n, Number(r)));
348
+ return i.owned.add(a), a;
349
+ }
350
+ #D(e, t, n, r) {
351
+ this.#m(e.map((e) => this.#S(e)));
352
+ let i = this.#f(this.#e.concat_tensors(t, Number(n), e.length));
353
+ return r.owned.add(i), i;
354
+ }
355
+ #O(e, t, n) {
356
+ let r = e.find((e) => e.name === t);
357
+ if (!r) return n;
358
+ r.kind !== 2 && c(`attribute ${t} must be an integer`);
359
+ let i = Number(r.value);
360
+ return Number.isSafeInteger(i) || c(`attribute ${t} is outside safe integer range`), i;
361
+ }
362
+ #k(e, t) {
363
+ let n = e.find((e) => e.name === t);
364
+ if (n) return (n.kind !== 7 || !Array.isArray(n.value)) && c(`attribute ${t} must be integers`), n.value.map((e) => {
365
+ let n = Number(e);
366
+ return Number.isSafeInteger(n) || c(`attribute ${t} has an unsafe integer`), n;
367
+ });
368
+ }
369
+ #A(e, t, i) {
370
+ if (!e || e.tag !== "tensor" || !e.shape || e.shape.some((e) => typeof e != "number")) throw n("unsupported", "ONNX empty scan needs static element shape information; export static scan output dimensions", "compute");
371
+ let o = e.shape, s = t < 0 ? t + o.length + 1 : t;
372
+ (s < 0 || s > o.length) && c("scan output axis is outside its rank");
373
+ let l = [...o];
374
+ l.splice(s, 0, 0);
375
+ let d = a[e.kind], f = e.kind === 8 ? [] : new d(0);
376
+ return this.#_(new u(r[e.kind], f, l), i.owned);
377
+ }
378
+ #j(e) {
379
+ let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = (e) => {
380
+ if (typeof e == "number") t.add(e);
381
+ else if (e !== null) {
382
+ if (n.has(e)) return;
383
+ n.add(e), (Array.isArray(e) || e instanceof Map) && e.forEach(r);
384
+ }
385
+ };
386
+ for (let t of e.frames) for (let e of t.values()) r(e);
387
+ for (let n of e.owned) t.has(n) || (this.#f(this.#e.drop_tensor(n)), e.owned.delete(n));
388
+ }
389
+ #M(e, t, n, i) {
390
+ let o = (e, t) => this.#O(n, e, t), s = (e) => {
391
+ let t = n.find((t) => t.name === e);
392
+ if (t) return (t.kind !== 8 || !Array.isArray(t.value) || t.value.some((e) => typeof e != "string")) && c(`${e} must be a string list`), t.value;
393
+ }, d = (e) => {
394
+ let t = n.find((t) => t.name === e);
395
+ if (t) return (t.kind !== 7 || !Array.isArray(t.value)) && c(`${e} must be an integer list`), t.value.map((e) => BigInt(String(e)));
396
+ }, f = (e, t) => {
397
+ let r = n.find((t) => t.name === e);
398
+ return r ? ((r.kind !== 3 || typeof r.value != "string") && c(`${e} must be a string`), r.value) : t;
399
+ }, p = e.op === "ZipMap" ? ["classlabels_int64s", "classlabels_strings"] : e.op === "DictVectorizer" ? ["int64_vocabulary", "string_vocabulary"] : [
400
+ "cast_to",
401
+ "map_form",
402
+ "max_map"
403
+ ];
404
+ if ((n.some((e) => !p.includes(e.name)) || t.length !== 1 || e.outputs.length !== 1) && c(`${e.op} has invalid attributes or arity`), e.op === "ZipMap") {
405
+ let e = this.#v(this.#S(t[0]));
406
+ (e.dtype !== "float32" || e.shape.length !== 2) && c("ZipMap needs float32[N,C]");
407
+ let n = d("classlabels_int64s"), r = s("classlabels_strings");
408
+ !!n == !!r && c("ZipMap requires exactly one class label list");
409
+ let a = n ?? r;
410
+ return (a.length !== e.shape[1] || new Set(a).size !== a.length) && c("ZipMap labels must be unique and match the class dimension"), [Array.from({ length: e.shape[0] }, (t, n) => {
411
+ let r = /* @__PURE__ */ new Map();
412
+ return a.forEach((t, o) => r.set(t, this.#_(new u("float32", Float32Array.of(Number(e.data[n * a.length + o])), []), i.owned))), this.#a.set(r, 1), r;
413
+ })];
414
+ }
415
+ let m = t[0];
416
+ m instanceof Map || c(`${e.op} needs a map`);
417
+ let h = [...m].map(([t, n]) => {
418
+ let r = this.#v(this.#S(n));
419
+ return l(r.shape) !== 1 && c(`${e.op} needs scalar map values`), {
420
+ key: t,
421
+ value: r
422
+ };
423
+ }), g = h.length ? r.indexOf(h[0].value.dtype) : this.#a.get(m);
424
+ (g === void 0 || g === 0) && c("an empty map requires its declared value dtype"), h.some(({ value: e }) => r.indexOf(e.dtype) !== g) && c("map value dtypes differ");
425
+ let _ = g, v, y;
426
+ if (e.op === "DictVectorizer") {
427
+ let e = d("int64_vocabulary"), t = s("string_vocabulary");
428
+ !!e == !!t && c("DictVectorizer requires one vocabulary");
429
+ let n = e ?? t;
430
+ new Set(n).size !== n.length && c("DictVectorizer vocabulary must be unique");
431
+ let r = new Map(n.map((e, t) => [e, t]));
432
+ v = [1, n.length];
433
+ let i = a[_];
434
+ y = _ === 8 ? Array(n.length).fill("") : new i(n.length);
435
+ for (let { key: e, value: t } of h) {
436
+ let n = typeof e == "number" ? BigInt(e) : e, i = r.get(n);
437
+ i === void 0 && c("DictVectorizer input key is absent from the vocabulary"), y[i] = t.data[0];
438
+ }
439
+ } else {
440
+ let e = f("cast_to", "TO_FLOAT"), t = f("map_form", "DENSE");
441
+ _ = e === "TO_FLOAT" ? 1 : e === "TO_INT64" ? 7 : e === "TO_STRING" ? 8 : 0, (!_ || !["DENSE", "SPARSE"].includes(t)) && c("CastMap has an invalid cast_to or map_form"), h.some(({ key: e }) => typeof e != "number" && typeof e != "bigint") && c("CastMap keys must be int64"), h.sort((e, t) => BigInt(e.key) < BigInt(t.key) ? -1 : +(BigInt(e.key) > BigInt(t.key)));
442
+ let n = t === "DENSE" ? h.length : o("max_map", 1);
443
+ n < 0 && c("CastMap max_map must be nonnegative"), l([n]), v = [n], y = _ === 8 ? Array(n).fill("") : _ === 7 ? new BigInt64Array(n) : new Float32Array(n), h.forEach(({ key: e, value: r }, i) => {
444
+ let a = t === "DENSE" ? i : Number(e);
445
+ (!Number.isSafeInteger(a) || a < 0 || a >= n) && c("CastMap sparse key is outside max_map");
446
+ let o = r.data[0];
447
+ if (_ === 8) y[a] = String(o);
448
+ else if (_ === 7) {
449
+ let e = Number(o);
450
+ Number.isFinite(e) || c("CastMap integer conversion needs finite numeric data"), y[a] = typeof o == "string" && /^[+-]?\d+$/.test(o) ? BigInt(o) : BigInt(Math.trunc(e));
451
+ } else {
452
+ let e = Number(o);
453
+ typeof o == "string" && (!o.trim() || Number.isNaN(e)) && c("CastMap string is not numeric"), y[a] = e;
454
+ }
455
+ });
456
+ }
457
+ return [this.#_(new u(r[_], y, v), i.owned)];
458
+ }
459
+ async #N(e, t, i, o, s, l, d, f, p) {
460
+ if (e.domain === "ai.onnx.ml" && [
461
+ "ZipMap",
462
+ "DictVectorizer",
463
+ "CastMap"
464
+ ].includes(e.op)) return ((s[e.domain] ?? 0) < 1 || (s[e.domain] ?? 0) > 5) && c("ai.onnx.ml opset must be in 1..5"), this.#M(e, t, i, d);
465
+ let m = (e, t) => this.#O(i, e, t), h = (e) => (Array.isArray(e) || c("sequence input expected"), e), g = (e, t) => {
466
+ let n = m(e, t);
467
+ return n !== 0 && n !== 1 && c(`${e} must be 0 or 1`), !!n;
468
+ }, _ = (t) => {
469
+ let n = i.find((e) => e.name === t);
470
+ return (!n || n.kind !== 5) && c(`${e.op} requires graph attribute ${t}`), n.value;
471
+ }, v = (e) => !!this.#f(this.#e.scalar_bool(this.#S(e))), y = (e, t) => this.#_(new u(e, e === "int64" ? BigInt64Array.of(BigInt(t)) : Uint8Array.of(t), []), d.owned);
472
+ switch (e.op) {
473
+ case "StringNormalizer": {
474
+ ((s[""] ?? 1) < 10 || t.length !== 1 || e.outputs.length !== 1) && c("StringNormalizer needs opset >=10 and one input/output");
475
+ let n = this.#v(this.#S(t[0]));
476
+ (n.dtype !== "string" || !(n.shape.length === 1 || n.shape.length === 2 && n.shape[0] === 1)) && c("StringNormalizer needs string[C] or string[1,C]"), i.some((e) => ![
477
+ "case_change_action",
478
+ "is_case_sensitive",
479
+ "locale",
480
+ "stopwords"
481
+ ].includes(e.name)) && c("unknown StringNormalizer attribute");
482
+ let r = (e, t) => {
483
+ let n = i.find((t) => t.name === e);
484
+ return n ? ((n.kind !== 3 || typeof n.value != "string") && c(`${e} must be a string`), n.value) : t;
485
+ }, a = r("case_change_action", "NONE");
486
+ [
487
+ "NONE",
488
+ "LOWER",
489
+ "UPPER"
490
+ ].includes(a) || c("invalid StringNormalizer case action");
491
+ let o = r("locale", "en-US"), l = o === "C" || o === "POSIX" ? "en-US" : o.replaceAll("_", "-").replace(/\..*$/, "");
492
+ try {
493
+ Intl.getCanonicalLocales(l);
494
+ } catch {
495
+ c("StringNormalizer locale is invalid");
496
+ }
497
+ let f = i.find((e) => e.name === "stopwords");
498
+ f && (f.kind !== 8 || !Array.isArray(f.value) || f.value.some((e) => typeof e != "string")) && c("stopwords must be strings");
499
+ let p = g("is_case_sensitive", 0), m = (e) => p ? e : e.toLocaleLowerCase(l), h = new Set((f?.value ?? []).map(m)), _ = n.data.filter((e) => !h.has(m(e))).map((e) => a === "LOWER" ? e.toLocaleLowerCase(l) : a === "UPPER" ? e.toLocaleUpperCase(l) : e);
500
+ _.length || _.push("");
501
+ let v = n.shape.length === 1 ? [_.length] : [1, _.length];
502
+ return [this.#_(new u("string", _, v), d.owned)];
503
+ }
504
+ case "RegexFullMatch": {
505
+ ((s[""] ?? 1) < 20 || t.length !== 1 || e.outputs.length !== 1) && c("RegexFullMatch needs opset >=20 and one input/output");
506
+ let n = i.find((e) => e.name === "pattern") ?? {
507
+ kind: 3,
508
+ value: ""
509
+ };
510
+ (i.some((e) => e.name !== "pattern") || n.kind !== 3 || typeof n.value != "string") && c("RegexFullMatch takes one string attribute, pattern");
511
+ let r = this.#v(this.#S(t[0]));
512
+ r.dtype !== "string" && c("RegexFullMatch needs a string tensor");
513
+ let { fullMatcher: a } = await import("./onnx-regex-DaesIdrm.js"), o = a(n.value), l = Uint8Array.from(r.data, (e) => Number(o(e)));
514
+ return [this.#_(new u("bool", l, r.shape), d.owned)];
515
+ }
516
+ case "ImageDecoder": {
517
+ ((s[""] ?? 1) < 20 || t.length !== 1 || e.outputs.length !== 1) && c("ImageDecoder needs opset >=20 and one input/output");
518
+ let n = i.find((e) => e.name === "pixel_format") ?? {
519
+ kind: 3,
520
+ value: "RGB"
521
+ };
522
+ (i.some((e) => e.name !== "pixel_format") || n.kind !== 3 || ![
523
+ "RGB",
524
+ "BGR",
525
+ "Grayscale"
526
+ ].includes(n.value)) && c("ImageDecoder takes one attribute, pixel_format: RGB, BGR or Grayscale");
527
+ let r = this.#v(this.#S(t[0]));
528
+ r.dtype !== "uint8" && c("ImageDecoder needs a uint8 tensor");
529
+ let { decodeImagePixels: a } = await import("./onnx-image-D2qfYsXf.js"), o = await a(r.data, n.value);
530
+ return [this.#_(new u("uint8", o.data, o.shape), d.owned)];
531
+ }
532
+ case "SequenceMap": {
533
+ (s[""] ?? 1) < 17 && c("SequenceMap requires opset >=17");
534
+ let r = h(t[0]), a = _("body");
535
+ (a.inputs.length !== t.length || a.outputs.length !== e.outputs.length || i.length !== 1) && c("SequenceMap body input/output count differs");
536
+ for (let e of t) Array.isArray(e) && e.length !== r.length && c("SequenceMap sequence lengths differ");
537
+ if (r.length > d.max) throw n("failed", "ONNX SequenceMap exceeds maxIterations; increase its safety bound", "compute");
538
+ let u = Array.from({ length: a.outputs.length }, () => []);
539
+ d.frames.push(/* @__PURE__ */ new Map([["mapped", u]]));
540
+ try {
541
+ for (let e = 0; e < r.length; e++) {
542
+ let n = new Map(o);
543
+ t.forEach((t, r) => n.set(a.inputs[r].name, Array.isArray(t) ? t[e] : t)), (await this.#F(a, n, s, l, d, f + 1, p)).forEach((e, t) => u[t].push(e)), this.#j(d);
544
+ }
545
+ return u.map((e, t) => this.#C(e, a.outputs[t].type?.kind));
546
+ } finally {
547
+ d.frames.pop();
548
+ }
549
+ }
550
+ case "Identity": return t.length !== 1 && c("Identity expects one input"), (e.outputs.length !== 1 || i.length) && c("Identity expects one output and no attributes"), typeof t[0] == "object" && (s[""] ?? s["ai.onnx"] ?? 1) < (t[0] === null ? 16 : 14) && c("container Identity needs a newer ONNX opset"), [t[0]];
551
+ case "SequenceEmpty": {
552
+ let e = m("dtype", 1);
553
+ return (e < 1 || !r[e]) && c("SequenceEmpty dtype must be a supported tensor type"), [this.#C([], e)];
554
+ }
555
+ case "SequenceConstruct":
556
+ t.length || c("SequenceConstruct needs at least one tensor");
557
+ {
558
+ let e = this.#T(t[0]).kind;
559
+ for (let n of t) this.#T(n).kind !== e && c("sequence tensor element types differ");
560
+ return [this.#C([...t], e)];
561
+ }
562
+ case "SequenceLength": return [y("int64", h(t[0]).length)];
563
+ case "SequenceAt": {
564
+ let e = h(t[0]), n = this.#w(t[1]), r = n < 0 ? n + e.length : n;
565
+ return (r < 0 || r >= e.length) && c("SequenceAt position is outside its sequence"), [e[r]];
566
+ }
567
+ case "SequenceInsert": {
568
+ let e = h(t[0]), n = t[2] == null ? e.length : this.#w(t[2]), r = n < 0 ? n + e.length : n;
569
+ (r < 0 || r > e.length) && c("SequenceInsert position is outside its sequence");
570
+ let i = this.#S(t[1]), a = this.#o.get(e) ?? (e.length ? this.#T(e[0]).kind : void 0);
571
+ a !== void 0 && a !== this.#T(i).kind && c("SequenceInsert element type differs");
572
+ let o = [...e];
573
+ return o.splice(r, 0, i), [this.#C(o, a ?? this.#T(i).kind)];
574
+ }
575
+ case "SequenceErase": {
576
+ let e = h(t[0]), n = t[1] == null ? -1 : this.#w(t[1]), r = n < 0 ? n + e.length : n;
577
+ (r < 0 || r >= e.length) && c("SequenceErase position is outside its sequence");
578
+ let i = [...e];
579
+ return i.splice(r, 1), [this.#C(i, this.#o.get(e))];
580
+ }
581
+ case "ConcatFromSequence": return [this.#D(h(t[0]), m("axis", 0), g("new_axis", 0), d)];
582
+ case "SplitToSequence": {
583
+ let e = this.#S(t[0]), n = this.#T(e).shape, i = m("axis", 0);
584
+ i < 0 && (i += n.length), (i < 0 || i >= n.length) && c("SplitToSequence axis is outside its rank");
585
+ let o = g("keepdims", 1), s;
586
+ if (t[1] != null) {
587
+ let e = this.#v(this.#S(t[1]));
588
+ if (e.dtype !== "int32" && e.dtype !== "int64" && c("SplitToSequence split must be int32/int64"), e.shape.length === 0) {
589
+ let e = this.#w(t[1]);
590
+ e <= 0 && c("SplitToSequence scalar split must be positive"), s = Array.from({ length: Math.ceil(n[i] / e) }, (t, r) => Math.min(e, n[i] - r * e));
591
+ } else e.shape.length !== 1 && c("SplitToSequence split must be scalar or rank one"), s = Array.from(e.data, (e) => Number(e));
592
+ } else s = Array(n[i]).fill(1);
593
+ (s.some((e) => !Number.isSafeInteger(e) || e < 0) || s.reduce((e, t) => e + t, 0) !== n[i]) && c("SplitToSequence lengths must be nonnegative and sum to its axis"), !o && s.some((e) => e !== 1) && c("SplitToSequence keepdims=0 needs unit splits");
594
+ let l = [], f = 0;
595
+ for (let t of s) {
596
+ let s = Array.from({ length: t }, (t, n) => this.#E(e, i, f + n, o, d));
597
+ if (s.length) l.push(s.length === 1 ? s[0] : this.#D(s, i, !1, d));
598
+ else {
599
+ let t = [...n];
600
+ t[i] = 0;
601
+ let o = a[this.#T(e).kind];
602
+ l.push(this.#_(new u(r[this.#T(e).kind], o ? new o(0) : [], t), d.owned));
603
+ }
604
+ f += t;
605
+ }
606
+ return [this.#C(l, this.#T(e).kind)];
607
+ }
608
+ case "Optional": return [t[0] ?? null];
609
+ case "OptionalHasElement": return [y("bool", Number(t[0] != null))];
610
+ case "OptionalGetElement": return t[0] ?? c("OptionalGetElement received an empty optional"), [t[0]];
611
+ case "Loop": {
612
+ let e = _("body"), r = t.slice(2);
613
+ (e.inputs.length !== r.length + 2 || e.outputs.length < r.length + 1) && c("Loop body input/output count differs from carried values");
614
+ let i = [...r], a = t[1] == null || v(t[1]), u = t[0] == null ? void 0 : this.#w(t[0]), m = Array.from({ length: e.outputs.length - i.length - 1 }, () => []), h = 0, g = /* @__PURE__ */ new Map([["current", i], ["scanned", m]]);
615
+ d.frames.push(g);
616
+ try {
617
+ for (; a && (u === void 0 || h < u);) {
618
+ if (h >= d.max) throw n("failed", "ONNX Loop reached maxIterations; increase its explicit safety bound", "compute");
619
+ let t = new Map(o);
620
+ t.set(e.inputs[0].name, y("int64", h)), t.set(e.inputs[1].name, y("bool", Number(a))), i.forEach((n, r) => t.set(e.inputs[r + 2].name, n));
621
+ let c = await this.#F(e, t, s, l, d, f + 1, p);
622
+ a = v(c[0]), i = c.slice(1, 1 + r.length), m.forEach((e, t) => e.push(c[1 + r.length + t])), g.set("current", i), this.#j(d), h++;
623
+ }
624
+ return [...i, ...m.map((t, n) => t.length ? this.#D(t, 0, !0, d) : this.#A(e.outputs[1 + r.length + n].type, 0, d))];
625
+ } finally {
626
+ d.frames.pop();
627
+ }
628
+ }
629
+ case "Scan": {
630
+ let r = s[""] ?? 1;
631
+ if (r < 8 && c("Scan needs opset >=8"), r === 8) return this.#P(e, t, i, o, s, l, d, f, p);
632
+ let a = _("body"), u = m("num_scan_inputs", 0);
633
+ (u < 1 || u > t.length) && c("Scan num_scan_inputs must identify its final inputs");
634
+ let h = t.length - u, g = t.slice(0, h), v = t.slice(h);
635
+ (a.inputs.length !== t.length || a.outputs.length < h) && c("Scan body input/output count differs");
636
+ let y = this.#k(i, "scan_input_axes") ?? Array(u).fill(0), b = this.#k(i, "scan_input_directions") ?? Array(u).fill(0), x = a.outputs.length - h, S = this.#k(i, "scan_output_axes") ?? Array(x).fill(0), C = this.#k(i, "scan_output_directions") ?? Array(x).fill(0);
637
+ (y.length !== u || b.length !== u || S.length !== x || C.length !== x || [...b, ...C].some((e) => e !== 0 && e !== 1)) && c("Scan axes/direction lists have invalid lengths or flags");
638
+ let w = v.map((e, t) => {
639
+ let n = this.#T(e).shape, r = y[t] < 0 ? y[t] + n.length : y[t];
640
+ return (r < 0 || r >= n.length) && c("Scan input axis is outside its rank"), y[t] = r, n[r];
641
+ });
642
+ if (w.some((e) => e !== w[0]) && c("Scan input axis lengths differ"), w[0] > d.max) throw n("failed", "ONNX Scan exceeds maxIterations; increase its explicit safety bound", "compute");
643
+ let T = Array.from({ length: x }, () => []), E = /* @__PURE__ */ new Map([["current", g], ["scanned", T]]);
644
+ d.frames.push(E);
645
+ try {
646
+ for (let e = 0; e < w[0]; e++) {
647
+ let t = new Map(o);
648
+ g.forEach((e, n) => t.set(a.inputs[n].name, e)), v.forEach((n, r) => t.set(a.inputs[h + r].name, this.#E(n, y[r], b[r] ? w[r] - 1 - e : e, !1, d)));
649
+ let n = await this.#F(a, t, s, l, d, f + 1, p);
650
+ g = n.slice(0, h), T.forEach((e, t) => e.push(n[h + t])), E.set("current", g), this.#j(d);
651
+ }
652
+ return [...g, ...T.map((e, t) => e.length ? this.#D(C[t] ? [...e].reverse() : e, S[t], !0, d) : this.#A(a.outputs[h + t].type, S[t], d))];
653
+ } finally {
654
+ d.frames.pop();
655
+ }
656
+ }
657
+ default: return;
658
+ }
659
+ }
660
+ async #P(e, t, r, i, a, o, s, l, u) {
661
+ let d = r.find((e) => e.name === "body");
662
+ (!d || d.kind !== 5) && c("Scan requires graph attribute body"), r.some((e) => ![
663
+ "body",
664
+ "directions",
665
+ "num_scan_inputs"
666
+ ].includes(e.name)) && c("unknown Scan opset 8 attribute");
667
+ let f = d.value, p = this.#O(r, "num_scan_inputs", 0);
668
+ (p < 1 || p > t.length - 1) && c("Scan num_scan_inputs must identify its final inputs");
669
+ let m = t.length - 1 - p, h = t.slice(1, 1 + m), g = t.slice(1 + m), _ = e.outputs.length - m;
670
+ (f.inputs.length !== m + p || _ < 0 || f.outputs.length !== e.outputs.length) && c("Scan body input/output count differs");
671
+ let v = this.#k(r, "directions") ?? Array(p).fill(0);
672
+ (v.length !== p || v.some((e) => e !== 0 && e !== 1)) && c("Scan directions must hold one 0 or 1 per scan input");
673
+ let [y, b] = this.#T(g[0]).shape;
674
+ (!y || !b) && c("Scan opset 8 needs at least one batch and one sequence step");
675
+ for (let e of g) {
676
+ let { shape: t } = this.#T(e);
677
+ (t[0] !== y || t[1] !== b) && c("Scan batch or sequence lengths differ");
678
+ }
679
+ for (let e of h) this.#T(e).shape[0] !== y && c("Scan states need the batch size of the scan inputs");
680
+ let x = Array(y).fill(b);
681
+ if (t[0] !== null) {
682
+ let e = this.#v(this.#S(t[0]));
683
+ (e.dtype !== "int64" && e.dtype !== "int32" || e.shape.length !== 1 || e.shape[0] !== y) && c("Scan sequence_lens must be an int32 or int64 vector with one length per batch"), x = Array.from(e.data, Number), x.some((e) => e < 0 || e > b) && c("Scan sequence_lens must lie in 0..the sequence length");
684
+ }
685
+ if (y * b > s.max) throw n("failed", "ONNX Scan exceeds maxIterations; increase its explicit safety bound", "compute");
686
+ let S = h.map(() => []), C = Array.from({ length: _ }, () => []), w = /* @__PURE__ */ new Map([["finals", S], ["stacked", C]]);
687
+ s.frames.push(w);
688
+ try {
689
+ for (let e = 0; e < y; e++) {
690
+ let t = h.map((t) => this.#E(t, 0, e, !1, s)), n = g.map((t) => this.#E(t, 0, e, !1, s)), r = Array.from({ length: _ }, () => []), c = x[e];
691
+ w.set("current", t).set("rows", n).set("scanned", r);
692
+ for (let e = 0; e < b; e++) {
693
+ let d = new Map(i);
694
+ t.forEach((e, t) => d.set(f.inputs[t].name, e)), n.forEach((t, n) => {
695
+ let r = v[n] && e < c ? c - 1 - e : e;
696
+ d.set(f.inputs[m + n].name, this.#E(t, 0, r, !1, s));
697
+ });
698
+ let p = await this.#F(f, d, a, o, s, l + 1, u);
699
+ e < c && (t = p.slice(0, m)), r.forEach((e, t) => e.push(p[m + t])), w.set("current", t), this.#j(s);
700
+ }
701
+ t.forEach((e, t) => S[t].push(e)), r.forEach((e, t) => C[t].push(this.#D(e, 0, !0, s)));
702
+ }
703
+ return [...S, ...C].map((e) => this.#D(e, 0, !0, s));
704
+ } finally {
705
+ s.frames.pop();
706
+ }
707
+ }
708
+ async run(e, t = {}) {
709
+ if (this.#u(), d(t.signal), this.#l) throw n("failed", "ONNX session is already running; await its run or use another session", "compute");
710
+ let r = t.maxIterations ?? 1e4, i = t.maxNodes ?? 1e6, a = t.yieldEvery ?? 16;
711
+ (!Number.isSafeInteger(r) || r < 1 || !Number.isSafeInteger(a) || a < 1 || !Number.isSafeInteger(i) || i < 1) && c("maxIterations, maxNodes and yieldEvery must be positive integers");
712
+ let o = t.outputs ?? this.outputs.map((e) => e.name);
713
+ (new Set(o).size !== o.length || o.some((e) => !this.outputs.some((t) => t.name === e))) && c("requested outputs must be unique declared graph outputs");
714
+ let s = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Map();
715
+ this.#l = !0;
716
+ try {
717
+ let n = /* @__PURE__ */ new Map(), i = new Map(this.#t.graph.initializers.map((e) => [e.name, e.id]));
718
+ for (let t of Object.keys(e)) this.inputs.some((e) => e.name === t) || c(`unknown model input ${t}`);
719
+ for (let t of this.inputs) {
720
+ let r = Object.hasOwn(e, t.name) ? this.#y(e[t.name], s) : i.get(t.name) ?? (t.type?.tag === "optional" ? null : void 0);
721
+ r === void 0 && c(`missing model input ${t.name}`), this.#x(r, t.type, l), n.set(t.name, r);
722
+ }
723
+ let u = {
724
+ owned: s,
725
+ options: t,
726
+ max: r,
727
+ yieldEvery: a,
728
+ nodes: 0,
729
+ symbols: l,
730
+ frames: []
731
+ }, d = await this.#F(this.#t.graph, n, this.#t.opsets, [], u, 0, ""), f = Object.create(null);
732
+ for (let e of o) {
733
+ let t = this.outputs.findIndex((t) => t.name === e);
734
+ f[e] = this.#b(d[t]);
735
+ }
736
+ return Object.freeze(f);
737
+ } finally {
738
+ if (!this.#c) for (let e of s) this.#f(this.#e.drop_tensor(e));
739
+ this.#l = !1;
740
+ }
741
+ }
742
+ async #F(e, t, r, i, a, o, s) {
743
+ o > 64 && c("runtime graph nesting exceeds 64");
744
+ let l = new Map(t);
745
+ for (let t of e.initializers) (!l.has(t.name) || !e.inputs.some((e) => e.name === t.name)) && l.set(t.name, t.id);
746
+ for (let t of e.inputs) {
747
+ let e = l.get(t.name);
748
+ e === void 0 && c(`missing graph input ${t.name}`), this.#x(e, t.type, a.symbols);
749
+ }
750
+ let u = /* @__PURE__ */ new Map();
751
+ for (let t of e.nodes) for (let e of h(t)) u.set(e, (u.get(e) ?? 0) + 1);
752
+ for (let t of e.outputs) u.set(t.name, (u.get(t.name) ?? 0) + 1);
753
+ for (let e of l.keys()) u.has(e) || l.delete(e);
754
+ a.frames.push(l);
755
+ try {
756
+ for (let t of e.nodes) {
757
+ let e = `${s}/${t.kernelIndex}`;
758
+ if (this.#u(), d(a.options.signal), ++a.nodes > (a.options.maxNodes ?? 1e6)) throw n("failed", "ONNX inference reached maxNodes; increase its explicit work bound", "compute");
759
+ a.nodes % a.yieldEvery === 0 && (await new Promise((e) => setTimeout(e, 0)), this.#u(), d(a.options.signal));
760
+ let f = t.inputs.map((e) => e ? l.get(e) : null);
761
+ f.some((e) => e === void 0) && c(`missing input while executing ${t.op}`);
762
+ let m = this.#h(t, i), g = this.#n.get(p(t.domain, t.op, t.overload)), _;
763
+ if (g) {
764
+ for (let e of g.requiredAttributes) !m.attrs.some((t) => t.name === e) && !g.defaults.some((t) => t.name === e) && c(`function ${g.name} is missing attribute ${e}`);
765
+ let t = new Map(g.inputs.map((e, t) => [e, f[t]]));
766
+ _ = await this.#F({
767
+ name: g.name,
768
+ nodes: g.nodes,
769
+ inputs: g.inputs.map((e) => ({
770
+ name: e,
771
+ type: null
772
+ })),
773
+ outputs: g.outputs.map((e) => ({
774
+ name: e,
775
+ type: null
776
+ })),
777
+ initializers: []
778
+ }, t, g.opsets, [m.id, g.defaultSet], a, o + 1, e);
779
+ } else if (t.op === "If") {
780
+ let t = f[0];
781
+ typeof t != "number" && c("If condition must be a bool tensor");
782
+ let n = !!this.#f(this.#e.scalar_bool(t)), s = m.attrs.find((e) => e.name === (n ? "then_branch" : "else_branch"));
783
+ (!s || s.kind !== 5) && c("If requires then_branch and else_branch graphs");
784
+ let u = s.value;
785
+ u.inputs.length && c("If branch graphs must capture values rather than declare inputs"), _ = await this.#F(u, l, r, i, a, o + 1, e);
786
+ } else if ((_ = await this.#N(t, f, m.attrs, l, r, i, a, o, e)) === void 0) {
787
+ f.some((e) => e !== null && typeof e != "number") && c(`${t.op} needs tensor inputs`);
788
+ let n = r[t.domain === "ai.onnx" ? "" : t.domain] ?? 1;
789
+ if (this.#m(f.map((e) => e === null ? 0 : e)), /^(RandomUniform|RandomNormal|Bernoulli|Multinomial|Dropout)/.test(t.op)) {
790
+ let t = this.#i.get(e);
791
+ t === void 0 && (this.#i.size >= 1e6 && c("random call sites exceed one million"), t = this.#i.size, this.#i.set(e, t)), this.#e.random_stream(t);
792
+ }
793
+ let i = this.#f(this.#e.execute(t.kernelIndex, m.id, n, f.length, t.outputs.length));
794
+ _ = Array.from({ length: i }, (e, t) => {
795
+ let n = this.#e.output(t);
796
+ return a.owned.add(n), n;
797
+ });
798
+ }
799
+ _.length < t.outputs.length && c(`${t.op} returned fewer outputs than declared`), t.outputs.forEach((e, t) => {
800
+ e && l.set(e, _[t]);
801
+ });
802
+ for (let e of h(t)) {
803
+ let t = u.get(e) - 1;
804
+ u.set(e, t), t === 0 && l.delete(e);
805
+ }
806
+ for (let e of t.outputs) u.has(e) || l.delete(e);
807
+ this.#j(a);
808
+ }
809
+ return e.outputs.map((e) => {
810
+ let t = l.get(e.name);
811
+ return t === void 0 && c(`graph output ${e.name} is unavailable`), this.#x(t, e.type, a.symbols), t;
812
+ });
813
+ } finally {
814
+ a.frames.pop();
815
+ }
816
+ }
817
+ dispose() {
818
+ this.#c || (this.#c = !0, this.#e.release(), this.#r.clear(), this.#i.clear());
819
+ }
820
+ [Symbol.dispose]() {
821
+ this.dispose();
822
+ }
823
+ };
824
+ async function _(e, t = {}) {
825
+ d(t.signal);
826
+ let r = new URL("./onnx-kernel.wasm?no-inline",
827
+ import.meta.url
828
+ ), i;
829
+ try {
830
+ let a = new URL(r);
831
+ a.protocol === "file:" && (a.search = "");
832
+ let o = await f(a, t.signal), { instance: l } = await WebAssembly.instantiate(o);
833
+ i = l.exports;
834
+ let u = () => s.decode(new Uint8Array(i.memory.buffer, i.message_ptr(), i.message_len())), p = (e) => {
835
+ if (e < 0) throw n("failed", `ONNX: ${u()}`, "compute");
836
+ return e;
837
+ };
838
+ t.memoryLimit !== void 0 && (Number.isSafeInteger(t.memoryLimit) || c("memoryLimit must be an integer byte count"), p(i.memory_limit(t.memoryLimit)));
839
+ let m = typeof e == "string" ? new URL(e, globalThis.location?.href) : e instanceof URL ? e : void 0, h = m ? await f(m, t.signal) : e instanceof ArrayBuffer ? new Uint8Array(e) : e, _ = (e) => {
840
+ let t = i.reserve(e.length);
841
+ t === 4294967295 && p(-1), new Uint8Array(i.memory.buffer, t, e.length).set(e);
842
+ };
843
+ _(h), p(i.load_model(h.length));
844
+ let v = JSON.parse(u()), y = crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(2));
845
+ i.random_seed(y[0], y[1]);
846
+ let b = /* @__PURE__ */ new Set(), x = /* @__PURE__ */ new Map(), S = async (e) => {
847
+ if (!b.has(e.id)) {
848
+ if (d(t.signal), e.externalStorage) {
849
+ let n = e.external, r = n.location;
850
+ r || c("external tensor has no location");
851
+ let a = (e, t) => e === void 0 ? t : ((!/^\d+$/.test(e) || !Number.isSafeInteger(Number(e))) && c("external offset/length must be safe nonnegative decimal integers"), Number(e)), o = a(n.offset, 0), s = a(n.length, e.bytes);
852
+ s !== e.bytes && c("external data length differs from tensor storage size");
853
+ let l;
854
+ if (t.externalData) l = await t.externalData({
855
+ location: r,
856
+ offset: o,
857
+ length: s,
858
+ checksum: n.checksum,
859
+ signal: t.signal
860
+ });
861
+ else {
862
+ m || c("external tensor needs a URL source or externalData resolver"), /^[a-z][a-z0-9+.-]*:|^[\\/]|(?:^|[\\/])\.\.(?:[\\/]|$)/i.test(r) && c("external location must be a relative path without parent traversal; use externalData for other locations");
863
+ let e = new URL(r, m);
864
+ e.origin !== m.origin && c("external location must have the model origin; use externalData for another origin");
865
+ let n = e.href, i = x.get(n);
866
+ i || (i = await f(e, t.signal), x.set(n, i)), o + s > i.length && c("external tensor range exceeds its file"), l = i.subarray(o, o + s);
867
+ }
868
+ d(t.signal), (!(l instanceof Uint8Array) || l.length !== s) && c("externalData must return exactly the requested bytes"), _(l), p(i.fill_external(e.id, l.length));
869
+ }
870
+ e.sparseValues && e.sparseIndices && (await S(e.sparseValues), await S(e.sparseIndices), p(i.fill_sparse(e.id, e.sparseValues.id, e.sparseIndices.id))), b.add(e.id);
871
+ }
872
+ }, C = async (e) => {
873
+ if (e && typeof e == "object") {
874
+ if ("id" in e && "shape" in e && "kind" in e) await S(e);
875
+ else for (let t of Object.values(e)) if (Array.isArray(t)) for (let e of t) await C(e);
876
+ else await C(t);
877
+ }
878
+ };
879
+ return await C(v), d(t.signal), new g(i, v);
880
+ } catch (e) {
881
+ throw i?.release(), d(t.signal), e;
882
+ }
883
+ }
884
+ //#endregion
885
+ export { g as OnnxModel, u as OnnxTensor, _ as loadOnnx };