@bevyra/avatar 0.1.1 → 1.0.1
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.
- package/README.md +24 -26
- package/cli.js +25 -0
- package/index.d.ts +14 -21
- package/index.js +88 -95
- package/package.json +6 -4
- package/dist/assets/bevyra-core-Bskcl7l_.js +0 -195
- package/dist/assets/clientOnlyAvatarWorker-C6wTIDwp.js +0 -2119
- package/dist/assets/clientOnlySourceWorker-CNVdGzNy.js +0 -7347
- package/dist/assets/ort.wasm.min-BcBtKW3v.js +0 -1400
- package/dist/assets/ort.webgpu.min-BqBLsdhU.js +0 -1856
- package/dist/assets/rustAvatarWorker-vggdUXxI.js +0 -1702
- package/dist/assets/teacherCorrectorWorker-Deu0CCPs.js +0 -1406
- package/dist/assets/workerHelpers-BkWezgrb.js +0 -31
- package/dist/bevyra-avatar-sdk.js +0 -3367
- package/dist/ort/ort-wasm-simd-threaded.jsep.mjs +0 -136
- package/dist/ort/ort-wasm-simd-threaded.jsep.wasm +0 -0
- package/dist/ort/ort-wasm-simd-threaded.mjs +0 -68
- package/dist/ort/ort-wasm-simd-threaded.wasm +0 -0
- package/dist/runtime/bevyra-core.js +0 -1515
- package/dist/runtime/bevyra-core.wasm +0 -0
- package/dist/runtime/closure_router.onnx +0 -0
- package/dist/runtime/runtime-manifest.json +0 -27
- package/dist/sdk-manifest.json +0 -70
|
@@ -1,1856 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* ONNX Runtime Web v1.23.2
|
|
3
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
-
* Licensed under the MIT License.
|
|
5
|
-
*/
|
|
6
|
-
var pt = Object.defineProperty, Xr = Object.getOwnPropertyDescriptor, Kr = Object.getOwnPropertyNames, Yr = Object.prototype.hasOwnProperty, Jr = ((e) => typeof require < "u" ? require : typeof Proxy < "u" ? new Proxy(e, { get: (t, r) => (typeof require < "u" ? require : t)[r] }) : e)(function(e) {
|
|
7
|
-
if (typeof require < "u") return require.apply(this, arguments);
|
|
8
|
-
throw Error('Dynamic require of "' + e + '" is not supported');
|
|
9
|
-
}), b = (e, t) => () => (e && (t = e(e = 0)), t), Be = (e, t) => {
|
|
10
|
-
for (var r in t) pt(e, r, { get: t[r], enumerable: !0 });
|
|
11
|
-
}, Qr = (e, t, r, a) => {
|
|
12
|
-
if (t && typeof t == "object" || typeof t == "function") for (let n of Kr(t)) !Yr.call(e, n) && n !== r && pt(e, n, { get: () => t[n], enumerable: !(a = Xr(t, n)) || a.enumerable });
|
|
13
|
-
return e;
|
|
14
|
-
}, dt = (e) => Qr(pt({}, "__esModule", { value: !0 }), e), we, K, le, Rt, nr, ar = b(() => {
|
|
15
|
-
we = /* @__PURE__ */ new Map(), K = [], le = (e, t, r) => {
|
|
16
|
-
if (t && typeof t.init == "function" && typeof t.createInferenceSessionHandler == "function") {
|
|
17
|
-
let a = we.get(e);
|
|
18
|
-
if (a === void 0) we.set(e, { backend: t, priority: r });
|
|
19
|
-
else {
|
|
20
|
-
if (a.priority > r) return;
|
|
21
|
-
if (a.priority === r && a.backend !== t) throw new Error(`cannot register backend "${e}" using priority ${r}`);
|
|
22
|
-
}
|
|
23
|
-
if (r >= 0) {
|
|
24
|
-
let n = K.indexOf(e);
|
|
25
|
-
n !== -1 && K.splice(n, 1);
|
|
26
|
-
for (let o = 0; o < K.length; o++) if (we.get(K[o]).priority <= r) {
|
|
27
|
-
K.splice(o, 0, e);
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
K.push(e);
|
|
31
|
-
}
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
throw new TypeError("not a valid backend");
|
|
35
|
-
}, Rt = async (e) => {
|
|
36
|
-
let t = we.get(e);
|
|
37
|
-
if (!t) return "backend not found.";
|
|
38
|
-
if (t.initialized) return t.backend;
|
|
39
|
-
if (t.aborted) return t.error;
|
|
40
|
-
{
|
|
41
|
-
let r = !!t.initPromise;
|
|
42
|
-
try {
|
|
43
|
-
return r || (t.initPromise = t.backend.init(e)), await t.initPromise, t.initialized = !0, t.backend;
|
|
44
|
-
} catch (a) {
|
|
45
|
-
return r || (t.error = `${a}`, t.aborted = !0), t.error;
|
|
46
|
-
} finally {
|
|
47
|
-
delete t.initPromise;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}, nr = async (e) => {
|
|
51
|
-
let t = e.executionProviders || [], r = t.map((u) => typeof u == "string" ? u : u.name), a = r.length === 0 ? K : r, n, o = [], s = /* @__PURE__ */ new Set();
|
|
52
|
-
for (let u of a) {
|
|
53
|
-
let l = await Rt(u);
|
|
54
|
-
typeof l == "string" ? o.push({ name: u, err: l }) : (n || (n = l), n === l && s.add(u));
|
|
55
|
-
}
|
|
56
|
-
if (!n) throw new Error(`no available backend found. ERR: ${o.map((u) => `[${u.name}] ${u.err}`).join(", ")}`);
|
|
57
|
-
for (let { name: u, err: l } of o) r.includes(u) && console.warn(`removing requested execution provider "${u}" from session options because it is not available: ${l}`);
|
|
58
|
-
let i = t.filter((u) => s.has(typeof u == "string" ? u : u.name));
|
|
59
|
-
return [n, new Proxy(e, { get: (u, l) => l === "executionProviders" ? i : Reflect.get(u, l) })];
|
|
60
|
-
};
|
|
61
|
-
}), en = b(() => {
|
|
62
|
-
ar();
|
|
63
|
-
}), or, tn = b(() => {
|
|
64
|
-
or = "1.23.2";
|
|
65
|
-
}), _e, M, sr = b(() => {
|
|
66
|
-
tn(), _e = "warning", M = { wasm: {}, webgl: {}, webgpu: {}, versions: { common: or }, set logLevel(e) {
|
|
67
|
-
if (e !== void 0) {
|
|
68
|
-
if (typeof e != "string" || ["verbose", "info", "warning", "error", "fatal"].indexOf(e) === -1) throw new Error(`Unsupported logging level: ${e}`);
|
|
69
|
-
_e = e;
|
|
70
|
-
}
|
|
71
|
-
}, get logLevel() {
|
|
72
|
-
return _e;
|
|
73
|
-
} }, Object.defineProperty(M, "logLevel", { enumerable: !0 });
|
|
74
|
-
}), U, rn = b(() => {
|
|
75
|
-
sr(), U = M;
|
|
76
|
-
}), ir, ur, nn = b(() => {
|
|
77
|
-
ir = (e, t) => {
|
|
78
|
-
let r = typeof document < "u" ? document.createElement("canvas") : new OffscreenCanvas(1, 1);
|
|
79
|
-
r.width = e.dims[3], r.height = e.dims[2];
|
|
80
|
-
let a = r.getContext("2d");
|
|
81
|
-
if (a != null) {
|
|
82
|
-
let n, o;
|
|
83
|
-
t?.tensorLayout !== void 0 && t.tensorLayout === "NHWC" ? (n = e.dims[2], o = e.dims[3]) : (n = e.dims[3], o = e.dims[2]);
|
|
84
|
-
let s = t?.format !== void 0 ? t.format : "RGB", i = t?.norm, u, l;
|
|
85
|
-
i === void 0 || i.mean === void 0 ? u = [255, 255, 255, 255] : typeof i.mean == "number" ? u = [i.mean, i.mean, i.mean, i.mean] : (u = [i.mean[0], i.mean[1], i.mean[2], 0], i.mean[3] !== void 0 && (u[3] = i.mean[3])), i === void 0 || i.bias === void 0 ? l = [0, 0, 0, 0] : typeof i.bias == "number" ? l = [i.bias, i.bias, i.bias, i.bias] : (l = [i.bias[0], i.bias[1], i.bias[2], 0], i.bias[3] !== void 0 && (l[3] = i.bias[3]));
|
|
86
|
-
let f = o * n, p = 0, d = f, h = f * 2, w = -1;
|
|
87
|
-
s === "RGBA" ? (p = 0, d = f, h = f * 2, w = f * 3) : s === "RGB" ? (p = 0, d = f, h = f * 2) : s === "RBG" && (p = 0, h = f, d = f * 2);
|
|
88
|
-
for (let v = 0; v < o; v++) for (let C = 0; C < n; C++) {
|
|
89
|
-
let y = (e.data[p++] - l[0]) * u[0], c = (e.data[d++] - l[1]) * u[1], A = (e.data[h++] - l[2]) * u[2], m = w === -1 ? 255 : (e.data[w++] - l[3]) * u[3];
|
|
90
|
-
a.fillStyle = "rgba(" + y + "," + c + "," + A + "," + m + ")", a.fillRect(C, v, 1, 1);
|
|
91
|
-
}
|
|
92
|
-
if ("toDataURL" in r) return r.toDataURL();
|
|
93
|
-
throw new Error("toDataURL is not supported");
|
|
94
|
-
} else throw new Error("Can not access image data");
|
|
95
|
-
}, ur = (e, t) => {
|
|
96
|
-
let r = typeof document < "u" ? document.createElement("canvas").getContext("2d") : new OffscreenCanvas(1, 1).getContext("2d"), a;
|
|
97
|
-
if (r != null) {
|
|
98
|
-
let n, o, s;
|
|
99
|
-
t?.tensorLayout !== void 0 && t.tensorLayout === "NHWC" ? (n = e.dims[2], o = e.dims[1], s = e.dims[3]) : (n = e.dims[3], o = e.dims[2], s = e.dims[1]);
|
|
100
|
-
let i = t !== void 0 && t.format !== void 0 ? t.format : "RGB", u = t?.norm, l, f;
|
|
101
|
-
u === void 0 || u.mean === void 0 ? l = [255, 255, 255, 255] : typeof u.mean == "number" ? l = [u.mean, u.mean, u.mean, u.mean] : (l = [u.mean[0], u.mean[1], u.mean[2], 255], u.mean[3] !== void 0 && (l[3] = u.mean[3])), u === void 0 || u.bias === void 0 ? f = [0, 0, 0, 0] : typeof u.bias == "number" ? f = [u.bias, u.bias, u.bias, u.bias] : (f = [u.bias[0], u.bias[1], u.bias[2], 0], u.bias[3] !== void 0 && (f[3] = u.bias[3]));
|
|
102
|
-
let p = o * n;
|
|
103
|
-
if (t !== void 0 && (t.format !== void 0 && s === 4 && t.format !== "RGBA" || s === 3 && t.format !== "RGB" && t.format !== "BGR")) throw new Error("Tensor format doesn't match input tensor dims");
|
|
104
|
-
let d = 4, h = 0, w = 1, v = 2, C = 3, y = 0, c = p, A = p * 2, m = -1;
|
|
105
|
-
i === "RGBA" ? (y = 0, c = p, A = p * 2, m = p * 3) : i === "RGB" ? (y = 0, c = p, A = p * 2) : i === "RBG" && (y = 0, A = p, c = p * 2), a = r.createImageData(n, o);
|
|
106
|
-
for (let E = 0; E < o * n; h += d, w += d, v += d, C += d, E++) a.data[h] = (e.data[y++] - f[0]) * l[0], a.data[w] = (e.data[c++] - f[1]) * l[1], a.data[v] = (e.data[A++] - f[2]) * l[2], a.data[C] = m === -1 ? 255 : (e.data[m++] - f[3]) * l[3];
|
|
107
|
-
} else throw new Error("Can not access image data");
|
|
108
|
-
return a;
|
|
109
|
-
};
|
|
110
|
-
}), Ie, lr, fr, pr, dr, hr, an = b(() => {
|
|
111
|
-
ht(), Ie = (e, t) => {
|
|
112
|
-
if (e === void 0) throw new Error("Image buffer must be defined");
|
|
113
|
-
if (t.height === void 0 || t.width === void 0) throw new Error("Image height and width must be defined");
|
|
114
|
-
if (t.tensorLayout === "NHWC") throw new Error("NHWC Tensor layout is not supported yet");
|
|
115
|
-
let { height: r, width: a } = t, n = t.norm ?? { mean: 255, bias: 0 }, o, s;
|
|
116
|
-
typeof n.mean == "number" ? o = [n.mean, n.mean, n.mean, n.mean] : o = [n.mean[0], n.mean[1], n.mean[2], n.mean[3] ?? 255], typeof n.bias == "number" ? s = [n.bias, n.bias, n.bias, n.bias] : s = [n.bias[0], n.bias[1], n.bias[2], n.bias[3] ?? 0];
|
|
117
|
-
let i = t.format !== void 0 ? t.format : "RGBA", u = t.tensorFormat !== void 0 && t.tensorFormat !== void 0 ? t.tensorFormat : "RGB", l = r * a, f = u === "RGBA" ? new Float32Array(l * 4) : new Float32Array(l * 3), p = 4, d = 0, h = 1, w = 2, v = 3, C = 0, y = l, c = l * 2, A = -1;
|
|
118
|
-
i === "RGB" && (p = 3, d = 0, h = 1, w = 2, v = -1), u === "RGBA" ? A = l * 3 : u === "RBG" ? (C = 0, c = l, y = l * 2) : u === "BGR" && (c = 0, y = l, C = l * 2);
|
|
119
|
-
for (let m = 0; m < l; m++, d += p, w += p, h += p, v += p) f[C++] = (e[d] + s[0]) / o[0], f[y++] = (e[h] + s[1]) / o[1], f[c++] = (e[w] + s[2]) / o[2], A !== -1 && v !== -1 && (f[A++] = (e[v] + s[3]) / o[3]);
|
|
120
|
-
return u === "RGBA" ? new W("float32", f, [1, 4, r, a]) : new W("float32", f, [1, 3, r, a]);
|
|
121
|
-
}, lr = async (e, t) => {
|
|
122
|
-
let r = typeof HTMLImageElement < "u" && e instanceof HTMLImageElement, a = typeof ImageData < "u" && e instanceof ImageData, n = typeof ImageBitmap < "u" && e instanceof ImageBitmap, o = typeof e == "string", s, i = t ?? {}, u = () => {
|
|
123
|
-
if (typeof document < "u") return document.createElement("canvas");
|
|
124
|
-
if (typeof OffscreenCanvas < "u") return new OffscreenCanvas(1, 1);
|
|
125
|
-
throw new Error("Canvas is not supported");
|
|
126
|
-
}, l = (f) => typeof HTMLCanvasElement < "u" && f instanceof HTMLCanvasElement || f instanceof OffscreenCanvas ? f.getContext("2d") : null;
|
|
127
|
-
if (r) {
|
|
128
|
-
let f = u();
|
|
129
|
-
f.width = e.width, f.height = e.height;
|
|
130
|
-
let p = l(f);
|
|
131
|
-
if (p != null) {
|
|
132
|
-
let d = e.height, h = e.width;
|
|
133
|
-
if (t !== void 0 && t.resizedHeight !== void 0 && t.resizedWidth !== void 0 && (d = t.resizedHeight, h = t.resizedWidth), t !== void 0) {
|
|
134
|
-
if (i = t, t.tensorFormat !== void 0) throw new Error("Image input config format must be RGBA for HTMLImageElement");
|
|
135
|
-
i.tensorFormat = "RGBA", i.height = d, i.width = h;
|
|
136
|
-
} else i.tensorFormat = "RGBA", i.height = d, i.width = h;
|
|
137
|
-
p.drawImage(e, 0, 0), s = p.getImageData(0, 0, h, d).data;
|
|
138
|
-
} else throw new Error("Can not access image data");
|
|
139
|
-
} else if (a) {
|
|
140
|
-
let f, p;
|
|
141
|
-
if (t !== void 0 && t.resizedWidth !== void 0 && t.resizedHeight !== void 0 ? (f = t.resizedHeight, p = t.resizedWidth) : (f = e.height, p = e.width), t !== void 0 && (i = t), i.format = "RGBA", i.height = f, i.width = p, t !== void 0) {
|
|
142
|
-
let d = u();
|
|
143
|
-
d.width = p, d.height = f;
|
|
144
|
-
let h = l(d);
|
|
145
|
-
if (h != null) h.putImageData(e, 0, 0), s = h.getImageData(0, 0, p, f).data;
|
|
146
|
-
else throw new Error("Can not access image data");
|
|
147
|
-
} else s = e.data;
|
|
148
|
-
} else if (n) {
|
|
149
|
-
if (t === void 0) throw new Error("Please provide image config with format for Imagebitmap");
|
|
150
|
-
let f = u();
|
|
151
|
-
f.width = e.width, f.height = e.height;
|
|
152
|
-
let p = l(f);
|
|
153
|
-
if (p != null) {
|
|
154
|
-
let d = e.height, h = e.width;
|
|
155
|
-
return p.drawImage(e, 0, 0, h, d), s = p.getImageData(0, 0, h, d).data, i.height = d, i.width = h, Ie(s, i);
|
|
156
|
-
} else throw new Error("Can not access image data");
|
|
157
|
-
} else {
|
|
158
|
-
if (o) return new Promise((f, p) => {
|
|
159
|
-
let d = u(), h = l(d);
|
|
160
|
-
if (!e || !h) return p();
|
|
161
|
-
let w = new Image();
|
|
162
|
-
w.crossOrigin = "Anonymous", w.src = e, w.onload = () => {
|
|
163
|
-
d.width = w.width, d.height = w.height, h.drawImage(w, 0, 0, d.width, d.height);
|
|
164
|
-
let v = h.getImageData(0, 0, d.width, d.height);
|
|
165
|
-
i.height = d.height, i.width = d.width, f(Ie(v.data, i));
|
|
166
|
-
};
|
|
167
|
-
});
|
|
168
|
-
throw new Error("Input data provided is not supported - aborted tensor creation");
|
|
169
|
-
}
|
|
170
|
-
if (s !== void 0) return Ie(s, i);
|
|
171
|
-
throw new Error("Input data provided is not supported - aborted tensor creation");
|
|
172
|
-
}, fr = (e, t) => {
|
|
173
|
-
let { width: r, height: a, download: n, dispose: o } = t, s = [1, a, r, 4];
|
|
174
|
-
return new W({ location: "texture", type: "float32", texture: e, dims: s, download: n, dispose: o });
|
|
175
|
-
}, pr = (e, t) => {
|
|
176
|
-
let { dataType: r, dims: a, download: n, dispose: o } = t;
|
|
177
|
-
return new W({ location: "gpu-buffer", type: r ?? "float32", gpuBuffer: e, dims: a, download: n, dispose: o });
|
|
178
|
-
}, dr = (e, t) => {
|
|
179
|
-
let { dataType: r, dims: a, download: n, dispose: o } = t;
|
|
180
|
-
return new W({ location: "ml-tensor", type: r ?? "float32", mlTensor: e, dims: a, download: n, dispose: o });
|
|
181
|
-
}, hr = (e, t, r) => new W({ location: "cpu-pinned", type: e, data: t, dims: r ?? [t.length] });
|
|
182
|
-
}), te, ve, Ge, cr, on = b(() => {
|
|
183
|
-
te = /* @__PURE__ */ new Map([["float32", Float32Array], ["uint8", Uint8Array], ["int8", Int8Array], ["uint16", Uint16Array], ["int16", Int16Array], ["int32", Int32Array], ["bool", Uint8Array], ["float64", Float64Array], ["uint32", Uint32Array], ["int4", Uint8Array], ["uint4", Uint8Array]]), ve = /* @__PURE__ */ new Map([[Float32Array, "float32"], [Uint8Array, "uint8"], [Int8Array, "int8"], [Uint16Array, "uint16"], [Int16Array, "int16"], [Int32Array, "int32"], [Float64Array, "float64"], [Uint32Array, "uint32"]]), Ge = !1, cr = () => {
|
|
184
|
-
if (!Ge) {
|
|
185
|
-
Ge = !0;
|
|
186
|
-
let e = typeof BigInt64Array < "u" && BigInt64Array.from, t = typeof BigUint64Array < "u" && BigUint64Array.from, r = globalThis.Float16Array, a = typeof r < "u" && r.from;
|
|
187
|
-
e && (te.set("int64", BigInt64Array), ve.set(BigInt64Array, "int64")), t && (te.set("uint64", BigUint64Array), ve.set(BigUint64Array, "uint64")), a ? (te.set("float16", r), ve.set(r, "float16")) : te.set("float16", Uint16Array);
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
}), wr, yr, sn = b(() => {
|
|
191
|
-
ht(), wr = (e) => {
|
|
192
|
-
let t = 1;
|
|
193
|
-
for (let r = 0; r < e.length; r++) {
|
|
194
|
-
let a = e[r];
|
|
195
|
-
if (typeof a != "number" || !Number.isSafeInteger(a)) throw new TypeError(`dims[${r}] must be an integer, got: ${a}`);
|
|
196
|
-
if (a < 0) throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${a}`);
|
|
197
|
-
t *= a;
|
|
198
|
-
}
|
|
199
|
-
return t;
|
|
200
|
-
}, yr = (e, t) => {
|
|
201
|
-
switch (e.location) {
|
|
202
|
-
case "cpu":
|
|
203
|
-
return new W(e.type, e.data, t);
|
|
204
|
-
case "cpu-pinned":
|
|
205
|
-
return new W({ location: "cpu-pinned", data: e.data, type: e.type, dims: t });
|
|
206
|
-
case "texture":
|
|
207
|
-
return new W({ location: "texture", texture: e.texture, type: e.type, dims: t });
|
|
208
|
-
case "gpu-buffer":
|
|
209
|
-
return new W({ location: "gpu-buffer", gpuBuffer: e.gpuBuffer, type: e.type, dims: t });
|
|
210
|
-
case "ml-tensor":
|
|
211
|
-
return new W({ location: "ml-tensor", mlTensor: e.mlTensor, type: e.type, dims: t });
|
|
212
|
-
default:
|
|
213
|
-
throw new Error(`tensorReshape: tensor location ${e.location} is not supported`);
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
}), W, ht = b(() => {
|
|
217
|
-
nn(), an(), on(), sn(), W = class {
|
|
218
|
-
constructor(e, t, r) {
|
|
219
|
-
cr();
|
|
220
|
-
let a, n;
|
|
221
|
-
if (typeof e == "object" && "location" in e) switch (this.dataLocation = e.location, a = e.type, n = e.dims, e.location) {
|
|
222
|
-
case "cpu-pinned": {
|
|
223
|
-
let s = te.get(a);
|
|
224
|
-
if (!s) throw new TypeError(`unsupported type "${a}" to create tensor from pinned buffer`);
|
|
225
|
-
if (!(e.data instanceof s)) throw new TypeError(`buffer should be of type ${s.name}`);
|
|
226
|
-
this.cpuData = e.data;
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
case "texture": {
|
|
230
|
-
if (a !== "float32") throw new TypeError(`unsupported type "${a}" to create tensor from texture`);
|
|
231
|
-
this.gpuTextureData = e.texture, this.downloader = e.download, this.disposer = e.dispose;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
case "gpu-buffer": {
|
|
235
|
-
if (a !== "float32" && a !== "float16" && a !== "int32" && a !== "int64" && a !== "uint32" && a !== "uint8" && a !== "bool" && a !== "uint4" && a !== "int4") throw new TypeError(`unsupported type "${a}" to create tensor from gpu buffer`);
|
|
236
|
-
this.gpuBufferData = e.gpuBuffer, this.downloader = e.download, this.disposer = e.dispose;
|
|
237
|
-
break;
|
|
238
|
-
}
|
|
239
|
-
case "ml-tensor": {
|
|
240
|
-
if (a !== "float32" && a !== "float16" && a !== "int32" && a !== "int64" && a !== "uint32" && a !== "uint64" && a !== "int8" && a !== "uint8" && a !== "bool" && a !== "uint4" && a !== "int4") throw new TypeError(`unsupported type "${a}" to create tensor from MLTensor`);
|
|
241
|
-
this.mlTensorData = e.mlTensor, this.downloader = e.download, this.disposer = e.dispose;
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
default:
|
|
245
|
-
throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`);
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
let s, i;
|
|
249
|
-
if (typeof e == "string") if (a = e, i = r, e === "string") {
|
|
250
|
-
if (!Array.isArray(t)) throw new TypeError("A string tensor's data must be a string array.");
|
|
251
|
-
s = t;
|
|
252
|
-
} else {
|
|
253
|
-
let u = te.get(e);
|
|
254
|
-
if (u === void 0) throw new TypeError(`Unsupported tensor type: ${e}.`);
|
|
255
|
-
if (Array.isArray(t)) {
|
|
256
|
-
if (e === "float16" && u === Uint16Array || e === "uint4" || e === "int4") throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${u.name} as data.`);
|
|
257
|
-
e === "uint64" || e === "int64" ? s = u.from(t, BigInt) : s = u.from(t);
|
|
258
|
-
} else if (t instanceof u) s = t;
|
|
259
|
-
else if (t instanceof Uint8ClampedArray) if (e === "uint8") s = Uint8Array.from(t);
|
|
260
|
-
else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");
|
|
261
|
-
else if (e === "float16" && t instanceof Uint16Array && u !== Uint16Array) s = new globalThis.Float16Array(t.buffer, t.byteOffset, t.length);
|
|
262
|
-
else throw new TypeError(`A ${a} tensor's data must be type of ${u}`);
|
|
263
|
-
}
|
|
264
|
-
else if (i = t, Array.isArray(e)) {
|
|
265
|
-
if (e.length === 0) throw new TypeError("Tensor type cannot be inferred from an empty array.");
|
|
266
|
-
let u = typeof e[0];
|
|
267
|
-
if (u === "string") a = "string", s = e;
|
|
268
|
-
else if (u === "boolean") a = "bool", s = Uint8Array.from(e);
|
|
269
|
-
else throw new TypeError(`Invalid element type of data array: ${u}.`);
|
|
270
|
-
} else if (e instanceof Uint8ClampedArray) a = "uint8", s = Uint8Array.from(e);
|
|
271
|
-
else {
|
|
272
|
-
let u = ve.get(e.constructor);
|
|
273
|
-
if (u === void 0) throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);
|
|
274
|
-
a = u, s = e;
|
|
275
|
-
}
|
|
276
|
-
if (i === void 0) i = [s.length];
|
|
277
|
-
else if (!Array.isArray(i)) throw new TypeError("A tensor's dims must be a number array");
|
|
278
|
-
n = i, this.cpuData = s, this.dataLocation = "cpu";
|
|
279
|
-
}
|
|
280
|
-
let o = wr(n);
|
|
281
|
-
if (this.cpuData && o !== this.cpuData.length && !((a === "uint4" || a === "int4") && Math.ceil(o / 2) === this.cpuData.length)) throw new Error(`Tensor's size(${o}) does not match data length(${this.cpuData.length}).`);
|
|
282
|
-
this.type = a, this.dims = n, this.size = o;
|
|
283
|
-
}
|
|
284
|
-
static async fromImage(e, t) {
|
|
285
|
-
return lr(e, t);
|
|
286
|
-
}
|
|
287
|
-
static fromTexture(e, t) {
|
|
288
|
-
return fr(e, t);
|
|
289
|
-
}
|
|
290
|
-
static fromGpuBuffer(e, t) {
|
|
291
|
-
return pr(e, t);
|
|
292
|
-
}
|
|
293
|
-
static fromMLTensor(e, t) {
|
|
294
|
-
return dr(e, t);
|
|
295
|
-
}
|
|
296
|
-
static fromPinnedBuffer(e, t, r) {
|
|
297
|
-
return hr(e, t, r);
|
|
298
|
-
}
|
|
299
|
-
toDataURL(e) {
|
|
300
|
-
return ir(this, e);
|
|
301
|
-
}
|
|
302
|
-
toImageData(e) {
|
|
303
|
-
return ur(this, e);
|
|
304
|
-
}
|
|
305
|
-
get data() {
|
|
306
|
-
if (this.ensureValid(), !this.cpuData) throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");
|
|
307
|
-
return this.cpuData;
|
|
308
|
-
}
|
|
309
|
-
get location() {
|
|
310
|
-
return this.dataLocation;
|
|
311
|
-
}
|
|
312
|
-
get texture() {
|
|
313
|
-
if (this.ensureValid(), !this.gpuTextureData) throw new Error("The data is not stored as a WebGL texture.");
|
|
314
|
-
return this.gpuTextureData;
|
|
315
|
-
}
|
|
316
|
-
get gpuBuffer() {
|
|
317
|
-
if (this.ensureValid(), !this.gpuBufferData) throw new Error("The data is not stored as a WebGPU buffer.");
|
|
318
|
-
return this.gpuBufferData;
|
|
319
|
-
}
|
|
320
|
-
get mlTensor() {
|
|
321
|
-
if (this.ensureValid(), !this.mlTensorData) throw new Error("The data is not stored as a WebNN MLTensor.");
|
|
322
|
-
return this.mlTensorData;
|
|
323
|
-
}
|
|
324
|
-
async getData(e) {
|
|
325
|
-
switch (this.ensureValid(), this.dataLocation) {
|
|
326
|
-
case "cpu":
|
|
327
|
-
case "cpu-pinned":
|
|
328
|
-
return this.data;
|
|
329
|
-
case "texture":
|
|
330
|
-
case "gpu-buffer":
|
|
331
|
-
case "ml-tensor": {
|
|
332
|
-
if (!this.downloader) throw new Error("The current tensor is not created with a specified data downloader.");
|
|
333
|
-
if (this.isDownloading) throw new Error("The current tensor is being downloaded.");
|
|
334
|
-
try {
|
|
335
|
-
this.isDownloading = !0;
|
|
336
|
-
let t = await this.downloader();
|
|
337
|
-
return this.downloader = void 0, this.dataLocation = "cpu", this.cpuData = t, e && this.disposer && (this.disposer(), this.disposer = void 0), t;
|
|
338
|
-
} finally {
|
|
339
|
-
this.isDownloading = !1;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
default:
|
|
343
|
-
throw new Error(`cannot get data from location: ${this.dataLocation}`);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
dispose() {
|
|
347
|
-
if (this.isDownloading) throw new Error("The current tensor is being downloaded.");
|
|
348
|
-
this.disposer && (this.disposer(), this.disposer = void 0), this.cpuData = void 0, this.gpuTextureData = void 0, this.gpuBufferData = void 0, this.mlTensorData = void 0, this.downloader = void 0, this.isDownloading = void 0, this.dataLocation = "none";
|
|
349
|
-
}
|
|
350
|
-
ensureValid() {
|
|
351
|
-
if (this.dataLocation === "none") throw new Error("The tensor is disposed.");
|
|
352
|
-
}
|
|
353
|
-
reshape(e) {
|
|
354
|
-
if (this.ensureValid(), this.downloader || this.disposer) throw new Error("Cannot reshape a tensor that owns GPU resource.");
|
|
355
|
-
return yr(this, e);
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
}), z, mr = b(() => {
|
|
359
|
-
ht(), z = W;
|
|
360
|
-
}), st, We, fe, pe, ne, ae, gr = b(() => {
|
|
361
|
-
sr(), st = (e, t) => {
|
|
362
|
-
(typeof M.trace > "u" ? !M.wasm.trace : !M.trace) || console.timeStamp(`${e}::ORT::${t}`);
|
|
363
|
-
}, We = (e, t) => {
|
|
364
|
-
let r = new Error().stack?.split(/\r\n|\r|\n/g) || [], a = !1;
|
|
365
|
-
for (let n = 0; n < r.length; n++) {
|
|
366
|
-
if (a && !r[n].includes("TRACE_FUNC")) {
|
|
367
|
-
let o = `FUNC_${e}::${r[n].trim().split(" ")[1]}`;
|
|
368
|
-
t && (o += `::${t}`), st("CPU", o);
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
r[n].includes("TRACE_FUNC") && (a = !0);
|
|
372
|
-
}
|
|
373
|
-
}, fe = (e) => {
|
|
374
|
-
(typeof M.trace > "u" ? !M.wasm.trace : !M.trace) || We("BEGIN", e);
|
|
375
|
-
}, pe = (e) => {
|
|
376
|
-
(typeof M.trace > "u" ? !M.wasm.trace : !M.trace) || We("END", e);
|
|
377
|
-
}, ne = (e) => {
|
|
378
|
-
(typeof M.trace > "u" ? !M.wasm.trace : !M.trace) || console.time(`ORT::${e}`);
|
|
379
|
-
}, ae = (e) => {
|
|
380
|
-
(typeof M.trace > "u" ? !M.wasm.trace : !M.trace) || console.timeEnd(`ORT::${e}`);
|
|
381
|
-
};
|
|
382
|
-
}), br, un = b(() => {
|
|
383
|
-
ar(), mr(), gr(), br = class vr {
|
|
384
|
-
constructor(t) {
|
|
385
|
-
this.handler = t;
|
|
386
|
-
}
|
|
387
|
-
async run(t, r, a) {
|
|
388
|
-
fe(), ne("InferenceSession.run");
|
|
389
|
-
let n = {}, o = {};
|
|
390
|
-
if (typeof t != "object" || t === null || t instanceof z || Array.isArray(t)) throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");
|
|
391
|
-
let s = !0;
|
|
392
|
-
if (typeof r == "object") {
|
|
393
|
-
if (r === null) throw new TypeError("Unexpected argument[1]: cannot be null.");
|
|
394
|
-
if (r instanceof z) throw new TypeError("'fetches' cannot be a Tensor");
|
|
395
|
-
if (Array.isArray(r)) {
|
|
396
|
-
if (r.length === 0) throw new TypeError("'fetches' cannot be an empty array.");
|
|
397
|
-
s = !1;
|
|
398
|
-
for (let l of r) {
|
|
399
|
-
if (typeof l != "string") throw new TypeError("'fetches' must be a string array or an object.");
|
|
400
|
-
if (this.outputNames.indexOf(l) === -1) throw new RangeError(`'fetches' contains invalid output name: ${l}.`);
|
|
401
|
-
n[l] = null;
|
|
402
|
-
}
|
|
403
|
-
if (typeof a == "object" && a !== null) o = a;
|
|
404
|
-
else if (typeof a < "u") throw new TypeError("'options' must be an object.");
|
|
405
|
-
} else {
|
|
406
|
-
let l = !1, f = Object.getOwnPropertyNames(r);
|
|
407
|
-
for (let p of this.outputNames) if (f.indexOf(p) !== -1) {
|
|
408
|
-
let d = r[p];
|
|
409
|
-
(d === null || d instanceof z) && (l = !0, s = !1, n[p] = d);
|
|
410
|
-
}
|
|
411
|
-
if (l) {
|
|
412
|
-
if (typeof a == "object" && a !== null) o = a;
|
|
413
|
-
else if (typeof a < "u") throw new TypeError("'options' must be an object.");
|
|
414
|
-
} else o = r;
|
|
415
|
-
}
|
|
416
|
-
} else if (typeof r < "u") throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");
|
|
417
|
-
for (let l of this.inputNames) if (typeof t[l] > "u") throw new Error(`input '${l}' is missing in 'feeds'.`);
|
|
418
|
-
if (s) for (let l of this.outputNames) n[l] = null;
|
|
419
|
-
let i = await this.handler.run(t, n, o), u = {};
|
|
420
|
-
for (let l in i) if (Object.hasOwnProperty.call(i, l)) {
|
|
421
|
-
let f = i[l];
|
|
422
|
-
f instanceof z ? u[l] = f : u[l] = new z(f.type, f.data, f.dims);
|
|
423
|
-
}
|
|
424
|
-
return ae("InferenceSession.run"), pe(), u;
|
|
425
|
-
}
|
|
426
|
-
async release() {
|
|
427
|
-
return this.handler.dispose();
|
|
428
|
-
}
|
|
429
|
-
static async create(t, r, a, n) {
|
|
430
|
-
fe(), ne("InferenceSession.create");
|
|
431
|
-
let o, s = {};
|
|
432
|
-
if (typeof t == "string") {
|
|
433
|
-
if (o = t, typeof r == "object" && r !== null) s = r;
|
|
434
|
-
else if (typeof r < "u") throw new TypeError("'options' must be an object.");
|
|
435
|
-
} else if (t instanceof Uint8Array) {
|
|
436
|
-
if (o = t, typeof r == "object" && r !== null) s = r;
|
|
437
|
-
else if (typeof r < "u") throw new TypeError("'options' must be an object.");
|
|
438
|
-
} else if (t instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && t instanceof SharedArrayBuffer) {
|
|
439
|
-
let f = t, p = 0, d = t.byteLength;
|
|
440
|
-
if (typeof r == "object" && r !== null) s = r;
|
|
441
|
-
else if (typeof r == "number") {
|
|
442
|
-
if (p = r, !Number.isSafeInteger(p)) throw new RangeError("'byteOffset' must be an integer.");
|
|
443
|
-
if (p < 0 || p >= f.byteLength) throw new RangeError(`'byteOffset' is out of range [0, ${f.byteLength}).`);
|
|
444
|
-
if (d = t.byteLength - p, typeof a == "number") {
|
|
445
|
-
if (d = a, !Number.isSafeInteger(d)) throw new RangeError("'byteLength' must be an integer.");
|
|
446
|
-
if (d <= 0 || p + d > f.byteLength) throw new RangeError(`'byteLength' is out of range (0, ${f.byteLength - p}].`);
|
|
447
|
-
if (typeof n == "object" && n !== null) s = n;
|
|
448
|
-
else if (typeof n < "u") throw new TypeError("'options' must be an object.");
|
|
449
|
-
} else if (typeof a < "u") throw new TypeError("'byteLength' must be a number.");
|
|
450
|
-
} else if (typeof r < "u") throw new TypeError("'options' must be an object.");
|
|
451
|
-
o = new Uint8Array(f, p, d);
|
|
452
|
-
} else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
|
|
453
|
-
let [i, u] = await nr(s), l = await i.createInferenceSessionHandler(o, u);
|
|
454
|
-
return ae("InferenceSession.create"), pe(), new vr(l);
|
|
455
|
-
}
|
|
456
|
-
startProfiling() {
|
|
457
|
-
this.handler.startProfiling();
|
|
458
|
-
}
|
|
459
|
-
endProfiling() {
|
|
460
|
-
this.handler.endProfiling();
|
|
461
|
-
}
|
|
462
|
-
get inputNames() {
|
|
463
|
-
return this.handler.inputNames;
|
|
464
|
-
}
|
|
465
|
-
get outputNames() {
|
|
466
|
-
return this.handler.outputNames;
|
|
467
|
-
}
|
|
468
|
-
get inputMetadata() {
|
|
469
|
-
return this.handler.inputMetadata;
|
|
470
|
-
}
|
|
471
|
-
get outputMetadata() {
|
|
472
|
-
return this.handler.outputMetadata;
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
}), Tr, ln = b(() => {
|
|
476
|
-
un(), Tr = br;
|
|
477
|
-
}), fn = b(() => {
|
|
478
|
-
}), pn = b(() => {
|
|
479
|
-
}), dn = b(() => {
|
|
480
|
-
}), hn = b(() => {
|
|
481
|
-
}), Er = {};
|
|
482
|
-
Be(Er, { InferenceSession: () => Tr, TRACE: () => st, TRACE_EVENT_BEGIN: () => ne, TRACE_EVENT_END: () => ae, TRACE_FUNC_BEGIN: () => fe, TRACE_FUNC_END: () => pe, Tensor: () => z, env: () => U, registerBackend: () => le });
|
|
483
|
-
var oe = b(() => {
|
|
484
|
-
en(), rn(), ln(), mr(), fn(), pn(), gr(), dn(), hn();
|
|
485
|
-
}), ct = b(() => {
|
|
486
|
-
}), Ir = {};
|
|
487
|
-
Be(Ir, { default: () => Cr });
|
|
488
|
-
var Fe, je, Cr, cn = b(() => {
|
|
489
|
-
kr(), se(), yt(), Fe = "ort-wasm-proxy-worker", je = globalThis.self?.name === Fe, je && (self.onmessage = (e) => {
|
|
490
|
-
let { type: t, in: r } = e.data;
|
|
491
|
-
try {
|
|
492
|
-
switch (t) {
|
|
493
|
-
case "init-wasm":
|
|
494
|
-
mt(r.wasm).then(() => {
|
|
495
|
-
Et(r).then(() => {
|
|
496
|
-
postMessage({ type: t });
|
|
497
|
-
}, (a) => {
|
|
498
|
-
postMessage({ type: t, err: a });
|
|
499
|
-
});
|
|
500
|
-
}, (a) => {
|
|
501
|
-
postMessage({ type: t, err: a });
|
|
502
|
-
});
|
|
503
|
-
break;
|
|
504
|
-
case "init-ep": {
|
|
505
|
-
let { epName: a, env: n } = r;
|
|
506
|
-
It(n, a).then(() => {
|
|
507
|
-
postMessage({ type: t });
|
|
508
|
-
}, (o) => {
|
|
509
|
-
postMessage({ type: t, err: o });
|
|
510
|
-
});
|
|
511
|
-
break;
|
|
512
|
-
}
|
|
513
|
-
case "copy-from": {
|
|
514
|
-
let { buffer: a } = r, n = Ne(a);
|
|
515
|
-
postMessage({ type: t, out: n });
|
|
516
|
-
break;
|
|
517
|
-
}
|
|
518
|
-
case "create": {
|
|
519
|
-
let { model: a, options: n } = r;
|
|
520
|
-
Ct(a, n).then((o) => {
|
|
521
|
-
postMessage({ type: t, out: o });
|
|
522
|
-
}, (o) => {
|
|
523
|
-
postMessage({ type: t, err: o });
|
|
524
|
-
});
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
case "release":
|
|
528
|
-
At(r), postMessage({ type: t });
|
|
529
|
-
break;
|
|
530
|
-
case "run": {
|
|
531
|
-
let { sessionId: a, inputIndices: n, inputs: o, outputIndices: s, options: i } = r;
|
|
532
|
-
xt(a, n, o, s, new Array(s.length).fill(null), i).then((u) => {
|
|
533
|
-
u.some((l) => l[3] !== "cpu") ? postMessage({ type: t, err: "Proxy does not support non-cpu tensor location." }) : postMessage({ type: t, out: u }, Ot([...o, ...u]));
|
|
534
|
-
}, (u) => {
|
|
535
|
-
postMessage({ type: t, err: u });
|
|
536
|
-
});
|
|
537
|
-
break;
|
|
538
|
-
}
|
|
539
|
-
case "end-profiling":
|
|
540
|
-
Ut(r), postMessage({ type: t });
|
|
541
|
-
break;
|
|
542
|
-
default:
|
|
543
|
-
}
|
|
544
|
-
} catch (a) {
|
|
545
|
-
postMessage({ type: t, err: a });
|
|
546
|
-
}
|
|
547
|
-
}), Cr = je ? null : (e) => new Worker(e ?? _, { type: "module", name: Fe });
|
|
548
|
-
}), Ve, Mt, Nt, _, wt, Ce, Bt, St, ze, kt, He, Ar, qe, xr, yt = b(() => {
|
|
549
|
-
ct(), Ve = typeof location > "u" ? void 0 : location.origin, Mt = import.meta.url > "file:" && import.meta.url < "file;", Nt = () => {
|
|
550
|
-
{
|
|
551
|
-
if (Mt) {
|
|
552
|
-
let e = URL;
|
|
553
|
-
return new URL(new e("ort.webgpu.min.mjs", import.meta.url).href, Ve).href;
|
|
554
|
-
}
|
|
555
|
-
return import.meta.url;
|
|
556
|
-
}
|
|
557
|
-
}, _ = Nt(), wt = () => {
|
|
558
|
-
if (_ && !_.startsWith("blob:")) return _.substring(0, _.lastIndexOf("/") + 1);
|
|
559
|
-
}, Ce = (e, t) => {
|
|
560
|
-
try {
|
|
561
|
-
let r = t ?? _;
|
|
562
|
-
return (r ? new URL(e, r) : new URL(e)).origin === Ve;
|
|
563
|
-
} catch {
|
|
564
|
-
return !1;
|
|
565
|
-
}
|
|
566
|
-
}, Bt = (e, t) => {
|
|
567
|
-
let r = t ?? _;
|
|
568
|
-
try {
|
|
569
|
-
return (r ? new URL(e, r) : new URL(e)).href;
|
|
570
|
-
} catch {
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
}, St = (e, t) => `${t ?? "./"}${e}`, ze = async (e) => {
|
|
574
|
-
let t = await (await fetch(e, { credentials: "same-origin" })).blob();
|
|
575
|
-
return URL.createObjectURL(t);
|
|
576
|
-
}, kt = async (e) => (await import(
|
|
577
|
-
/*webpackIgnore:true*/
|
|
578
|
-
e
|
|
579
|
-
)).default, He = (cn(), dt(Ir)).default, Ar = async () => {
|
|
580
|
-
if (!_) throw new Error("Failed to load proxy worker: cannot determine the script source URL.");
|
|
581
|
-
if (Ce(_)) return [void 0, He()];
|
|
582
|
-
let e = await ze(_);
|
|
583
|
-
return [e, He(e)];
|
|
584
|
-
}, qe = void 0, xr = async (e, t, r, a) => {
|
|
585
|
-
let n = qe && !(e || t);
|
|
586
|
-
if (n) if (_) n = Ce(_);
|
|
587
|
-
else if (a && !r) n = !0;
|
|
588
|
-
else throw new Error("cannot determine the script source URL.");
|
|
589
|
-
if (n) return [void 0, qe];
|
|
590
|
-
{
|
|
591
|
-
let o = "ort-wasm-simd-threaded.asyncify.mjs", s = e ?? Bt(o, t), i = r && s && !Ce(s, t), u = i ? await ze(s) : s ?? St(o, t);
|
|
592
|
-
return [i ? u : void 0, await kt(u)];
|
|
593
|
-
}
|
|
594
|
-
};
|
|
595
|
-
}), Ze, Ae, ye, Xe, Pt, Dt, _t, mt, I, se = b(() => {
|
|
596
|
-
yt(), Ae = !1, ye = !1, Xe = !1, Pt = () => {
|
|
597
|
-
if (typeof SharedArrayBuffer > "u") return !1;
|
|
598
|
-
try {
|
|
599
|
-
return typeof MessageChannel < "u" && new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)), WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 5, 4, 1, 3, 1, 1, 10, 11, 1, 9, 0, 65, 0, 254, 16, 2, 0, 26, 11]));
|
|
600
|
-
} catch {
|
|
601
|
-
return !1;
|
|
602
|
-
}
|
|
603
|
-
}, Dt = () => {
|
|
604
|
-
try {
|
|
605
|
-
return WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10, 30, 1, 28, 0, 65, 0, 253, 15, 253, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253, 186, 1, 26, 11]));
|
|
606
|
-
} catch {
|
|
607
|
-
return !1;
|
|
608
|
-
}
|
|
609
|
-
}, _t = () => {
|
|
610
|
-
try {
|
|
611
|
-
return WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 19, 1, 17, 0, 65, 1, 253, 15, 65, 2, 253, 15, 65, 3, 253, 15, 253, 147, 2, 11]));
|
|
612
|
-
} catch {
|
|
613
|
-
return !1;
|
|
614
|
-
}
|
|
615
|
-
}, mt = async (e) => {
|
|
616
|
-
if (Ae) return Promise.resolve();
|
|
617
|
-
if (ye) throw new Error("multiple calls to 'initializeWebAssembly()' detected.");
|
|
618
|
-
if (Xe) throw new Error("previous call to 'initializeWebAssembly()' failed.");
|
|
619
|
-
ye = !0;
|
|
620
|
-
let t = e.initTimeout, r = e.numThreads;
|
|
621
|
-
if (e.simd !== !1) {
|
|
622
|
-
if (e.simd === "relaxed") {
|
|
623
|
-
if (!_t()) throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.");
|
|
624
|
-
} else if (!Dt()) throw new Error("WebAssembly SIMD is not supported in the current environment.");
|
|
625
|
-
}
|
|
626
|
-
let a = Pt();
|
|
627
|
-
r > 1 && !a && (typeof self < "u" && !self.crossOriginIsolated && console.warn("env.wasm.numThreads is set to " + r + ", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."), console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."), e.numThreads = r = 1);
|
|
628
|
-
let n = e.wasmPaths, o = typeof n == "string" ? n : void 0, s = n?.mjs, i = s?.href ?? s, u = n?.wasm, l = u?.href ?? u, f = e.wasmBinary, [p, d] = await xr(i, o, r > 1, !!f || !!l), h = !1, w = [];
|
|
629
|
-
if (t > 0 && w.push(new Promise((v) => {
|
|
630
|
-
setTimeout(() => {
|
|
631
|
-
h = !0, v();
|
|
632
|
-
}, t);
|
|
633
|
-
})), w.push(new Promise((v, C) => {
|
|
634
|
-
let y = { numThreads: r };
|
|
635
|
-
if (f) y.wasmBinary = f;
|
|
636
|
-
else if (l || o) y.locateFile = (c) => l ?? o + c;
|
|
637
|
-
else if (i && i.indexOf("blob:") !== 0) y.locateFile = (c) => new URL(c, i).href;
|
|
638
|
-
else if (p) {
|
|
639
|
-
let c = wt();
|
|
640
|
-
c && (y.locateFile = (A) => c + A);
|
|
641
|
-
}
|
|
642
|
-
d(y).then((c) => {
|
|
643
|
-
ye = !1, Ae = !0, Ze = c, v(), p && URL.revokeObjectURL(p);
|
|
644
|
-
}, (c) => {
|
|
645
|
-
ye = !1, Xe = !0, C(c);
|
|
646
|
-
});
|
|
647
|
-
})), await Promise.race(w), h) throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`);
|
|
648
|
-
}, I = () => {
|
|
649
|
-
if (Ae && Ze) return Ze;
|
|
650
|
-
throw new Error("WebAssembly is not initialized yet.");
|
|
651
|
-
};
|
|
652
|
-
}), G, Re, T, gt = b(() => {
|
|
653
|
-
se(), G = (e, t) => {
|
|
654
|
-
let r = I(), a = r.lengthBytesUTF8(e) + 1, n = r._malloc(a);
|
|
655
|
-
return r.stringToUTF8(e, n, a), t.push(n), n;
|
|
656
|
-
}, Re = (e, t, r, a) => {
|
|
657
|
-
if (typeof e == "object" && e !== null) {
|
|
658
|
-
if (r.has(e)) throw new Error("Circular reference in options");
|
|
659
|
-
r.add(e);
|
|
660
|
-
}
|
|
661
|
-
Object.entries(e).forEach(([n, o]) => {
|
|
662
|
-
let s = t ? t + n : n;
|
|
663
|
-
if (typeof o == "object") Re(o, s + ".", r, a);
|
|
664
|
-
else if (typeof o == "string" || typeof o == "number") a(s, o.toString());
|
|
665
|
-
else if (typeof o == "boolean") a(s, o ? "1" : "0");
|
|
666
|
-
else throw new Error(`Can't handle extra config type: ${typeof o}`);
|
|
667
|
-
});
|
|
668
|
-
}, T = (e) => {
|
|
669
|
-
let t = I(), r = t.stackSave();
|
|
670
|
-
try {
|
|
671
|
-
let a = t.PTR_SIZE, n = t.stackAlloc(2 * a);
|
|
672
|
-
t._OrtGetLastError(n, n + a);
|
|
673
|
-
let o = Number(t.getValue(n, a === 4 ? "i32" : "i64")), s = t.getValue(n + a, "*"), i = s ? t.UTF8ToString(s) : "";
|
|
674
|
-
throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${i}`);
|
|
675
|
-
} finally {
|
|
676
|
-
t.stackRestore(r);
|
|
677
|
-
}
|
|
678
|
-
};
|
|
679
|
-
}), Ur, wn = b(() => {
|
|
680
|
-
se(), gt(), Ur = (e) => {
|
|
681
|
-
let t = I(), r = 0, a = [], n = e || {};
|
|
682
|
-
try {
|
|
683
|
-
if (e?.logSeverityLevel === void 0) n.logSeverityLevel = 2;
|
|
684
|
-
else if (typeof e.logSeverityLevel != "number" || !Number.isInteger(e.logSeverityLevel) || e.logSeverityLevel < 0 || e.logSeverityLevel > 4) throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);
|
|
685
|
-
if (e?.logVerbosityLevel === void 0) n.logVerbosityLevel = 0;
|
|
686
|
-
else if (typeof e.logVerbosityLevel != "number" || !Number.isInteger(e.logVerbosityLevel)) throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);
|
|
687
|
-
e?.terminate === void 0 && (n.terminate = !1);
|
|
688
|
-
let o = 0;
|
|
689
|
-
return e?.tag !== void 0 && (o = G(e.tag, a)), r = t._OrtCreateRunOptions(n.logSeverityLevel, n.logVerbosityLevel, !!n.terminate, o), r === 0 && T("Can't create run options."), e?.extra !== void 0 && Re(e.extra, "", /* @__PURE__ */ new WeakSet(), (s, i) => {
|
|
690
|
-
let u = G(s, a), l = G(i, a);
|
|
691
|
-
t._OrtAddRunConfigEntry(r, u, l) !== 0 && T(`Can't set a run config entry: ${s} - ${i}.`);
|
|
692
|
-
}), [r, a];
|
|
693
|
-
} catch (o) {
|
|
694
|
-
throw r !== 0 && t._OrtReleaseRunOptions(r), a.forEach((s) => t._free(s)), o;
|
|
695
|
-
}
|
|
696
|
-
};
|
|
697
|
-
}), Gt, Wt, Ft, xe, Ue, jt, Or, yn = b(() => {
|
|
698
|
-
se(), gt(), Gt = (e) => {
|
|
699
|
-
switch (e) {
|
|
700
|
-
case "disabled":
|
|
701
|
-
return 0;
|
|
702
|
-
case "basic":
|
|
703
|
-
return 1;
|
|
704
|
-
case "extended":
|
|
705
|
-
return 2;
|
|
706
|
-
case "layout":
|
|
707
|
-
return 3;
|
|
708
|
-
case "all":
|
|
709
|
-
return 99;
|
|
710
|
-
default:
|
|
711
|
-
throw new Error(`unsupported graph optimization level: ${e}`);
|
|
712
|
-
}
|
|
713
|
-
}, Wt = (e) => {
|
|
714
|
-
switch (e) {
|
|
715
|
-
case "sequential":
|
|
716
|
-
return 0;
|
|
717
|
-
case "parallel":
|
|
718
|
-
return 1;
|
|
719
|
-
default:
|
|
720
|
-
throw new Error(`unsupported execution mode: ${e}`);
|
|
721
|
-
}
|
|
722
|
-
}, Ft = (e) => {
|
|
723
|
-
e.extra || (e.extra = {}), e.extra.session || (e.extra.session = {});
|
|
724
|
-
let t = e.extra.session;
|
|
725
|
-
t.use_ort_model_bytes_directly || (t.use_ort_model_bytes_directly = "1"), e.executionProviders && e.executionProviders.some((r) => (typeof r == "string" ? r : r.name) === "webgpu") && (e.enableMemPattern = !1);
|
|
726
|
-
}, xe = (e, t, r, a) => {
|
|
727
|
-
let n = G(t, a), o = G(r, a);
|
|
728
|
-
I()._OrtAddSessionConfigEntry(e, n, o) !== 0 && T(`Can't set a session config entry: ${t} - ${r}.`);
|
|
729
|
-
}, Ue = (e, t, r, a) => {
|
|
730
|
-
let n = G(t, a), o = G(r, a);
|
|
731
|
-
e.push([n, o]);
|
|
732
|
-
}, jt = async (e, t, r) => {
|
|
733
|
-
for (let a of t) {
|
|
734
|
-
let n = typeof a == "string" ? a : a.name, o = [];
|
|
735
|
-
switch (n) {
|
|
736
|
-
case "webnn":
|
|
737
|
-
if (n = "WEBNN", typeof a != "string") {
|
|
738
|
-
let f = a?.deviceType;
|
|
739
|
-
f && xe(e, "deviceType", f, r);
|
|
740
|
-
}
|
|
741
|
-
break;
|
|
742
|
-
case "webgpu":
|
|
743
|
-
{
|
|
744
|
-
n = "WebGPU";
|
|
745
|
-
let f;
|
|
746
|
-
if (typeof a != "string") {
|
|
747
|
-
let d = a;
|
|
748
|
-
if (d.device) if (typeof GPUDevice < "u" && d.device instanceof GPUDevice) f = d.device;
|
|
749
|
-
else throw new Error("Invalid GPU device set in WebGPU EP options.");
|
|
750
|
-
}
|
|
751
|
-
let p = I().webgpuRegisterDevice(f);
|
|
752
|
-
if (p) {
|
|
753
|
-
let [d, h, w] = p;
|
|
754
|
-
Ue(o, "deviceId", d.toString(), r), Ue(o, "webgpuInstance", h.toString(), r), Ue(o, "webgpuDevice", w.toString(), r);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
break;
|
|
758
|
-
case "wasm":
|
|
759
|
-
case "cpu":
|
|
760
|
-
continue;
|
|
761
|
-
default:
|
|
762
|
-
throw new Error(`not supported execution provider: ${n}`);
|
|
763
|
-
}
|
|
764
|
-
let s = G(n, r), i = o.length, u = 0, l = 0;
|
|
765
|
-
if (i > 0) {
|
|
766
|
-
u = I()._malloc(i * I().PTR_SIZE), r.push(u), l = I()._malloc(i * I().PTR_SIZE), r.push(l);
|
|
767
|
-
for (let f = 0; f < i; f++) I().setValue(u + f * I().PTR_SIZE, o[f][0], "*"), I().setValue(l + f * I().PTR_SIZE, o[f][1], "*");
|
|
768
|
-
}
|
|
769
|
-
await I()._OrtAppendExecutionProvider(e, s, u, l, i) !== 0 && T(`Can't append execution provider: ${n}.`);
|
|
770
|
-
}
|
|
771
|
-
}, Or = async (e) => {
|
|
772
|
-
let t = I(), r = 0, a = [], n = e || {};
|
|
773
|
-
Ft(n);
|
|
774
|
-
try {
|
|
775
|
-
let o = Gt(n.graphOptimizationLevel ?? "all"), s = Wt(n.executionMode ?? "sequential"), i = typeof n.logId == "string" ? G(n.logId, a) : 0, u = n.logSeverityLevel ?? 2;
|
|
776
|
-
if (!Number.isInteger(u) || u < 0 || u > 4) throw new Error(`log severity level is not valid: ${u}`);
|
|
777
|
-
let l = n.logVerbosityLevel ?? 0;
|
|
778
|
-
if (!Number.isInteger(l) || l < 0 || l > 4) throw new Error(`log verbosity level is not valid: ${l}`);
|
|
779
|
-
let f = typeof n.optimizedModelFilePath == "string" ? G(n.optimizedModelFilePath, a) : 0;
|
|
780
|
-
if (r = t._OrtCreateSessionOptions(o, !!n.enableCpuMemArena, !!n.enableMemPattern, s, !!n.enableProfiling, 0, i, u, l, f), r === 0 && T("Can't create session options."), n.executionProviders && await jt(r, n.executionProviders, a), n.enableGraphCapture !== void 0) {
|
|
781
|
-
if (typeof n.enableGraphCapture != "boolean") throw new Error(`enableGraphCapture must be a boolean value: ${n.enableGraphCapture}`);
|
|
782
|
-
xe(r, "enableGraphCapture", n.enableGraphCapture.toString(), a);
|
|
783
|
-
}
|
|
784
|
-
if (n.freeDimensionOverrides) for (let [p, d] of Object.entries(n.freeDimensionOverrides)) {
|
|
785
|
-
if (typeof p != "string") throw new Error(`free dimension override name must be a string: ${p}`);
|
|
786
|
-
if (typeof d != "number" || !Number.isInteger(d) || d < 0) throw new Error(`free dimension override value must be a non-negative integer: ${d}`);
|
|
787
|
-
let h = G(p, a);
|
|
788
|
-
t._OrtAddFreeDimensionOverride(r, h, d) !== 0 && T(`Can't set a free dimension override: ${p} - ${d}.`);
|
|
789
|
-
}
|
|
790
|
-
return n.extra !== void 0 && Re(n.extra, "", /* @__PURE__ */ new WeakSet(), (p, d) => {
|
|
791
|
-
xe(r, p, d, a);
|
|
792
|
-
}), [r, a];
|
|
793
|
-
} catch (o) {
|
|
794
|
-
throw r !== 0 && t._OrtReleaseSessionOptions(r) !== 0 && T("Can't release session options."), a.forEach((s) => t._free(s)), o;
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
}), re, $e, ue, Te, Me, bt, vt, it, de = b(() => {
|
|
798
|
-
re = (e) => {
|
|
799
|
-
switch (e) {
|
|
800
|
-
case "int8":
|
|
801
|
-
return 3;
|
|
802
|
-
case "uint8":
|
|
803
|
-
return 2;
|
|
804
|
-
case "bool":
|
|
805
|
-
return 9;
|
|
806
|
-
case "int16":
|
|
807
|
-
return 5;
|
|
808
|
-
case "uint16":
|
|
809
|
-
return 4;
|
|
810
|
-
case "int32":
|
|
811
|
-
return 6;
|
|
812
|
-
case "uint32":
|
|
813
|
-
return 12;
|
|
814
|
-
case "float16":
|
|
815
|
-
return 10;
|
|
816
|
-
case "float32":
|
|
817
|
-
return 1;
|
|
818
|
-
case "float64":
|
|
819
|
-
return 11;
|
|
820
|
-
case "string":
|
|
821
|
-
return 8;
|
|
822
|
-
case "int64":
|
|
823
|
-
return 7;
|
|
824
|
-
case "uint64":
|
|
825
|
-
return 13;
|
|
826
|
-
case "int4":
|
|
827
|
-
return 22;
|
|
828
|
-
case "uint4":
|
|
829
|
-
return 21;
|
|
830
|
-
default:
|
|
831
|
-
throw new Error(`unsupported data type: ${e}`);
|
|
832
|
-
}
|
|
833
|
-
}, $e = (e) => {
|
|
834
|
-
switch (e) {
|
|
835
|
-
case 3:
|
|
836
|
-
return "int8";
|
|
837
|
-
case 2:
|
|
838
|
-
return "uint8";
|
|
839
|
-
case 9:
|
|
840
|
-
return "bool";
|
|
841
|
-
case 5:
|
|
842
|
-
return "int16";
|
|
843
|
-
case 4:
|
|
844
|
-
return "uint16";
|
|
845
|
-
case 6:
|
|
846
|
-
return "int32";
|
|
847
|
-
case 12:
|
|
848
|
-
return "uint32";
|
|
849
|
-
case 10:
|
|
850
|
-
return "float16";
|
|
851
|
-
case 1:
|
|
852
|
-
return "float32";
|
|
853
|
-
case 11:
|
|
854
|
-
return "float64";
|
|
855
|
-
case 8:
|
|
856
|
-
return "string";
|
|
857
|
-
case 7:
|
|
858
|
-
return "int64";
|
|
859
|
-
case 13:
|
|
860
|
-
return "uint64";
|
|
861
|
-
case 22:
|
|
862
|
-
return "int4";
|
|
863
|
-
case 21:
|
|
864
|
-
return "uint4";
|
|
865
|
-
default:
|
|
866
|
-
throw new Error(`unsupported data type: ${e}`);
|
|
867
|
-
}
|
|
868
|
-
}, ue = (e, t) => {
|
|
869
|
-
let r = [-1, 4, 1, 1, 2, 2, 4, 8, -1, 1, 2, 8, 4, 8, -1, -1, -1, -1, -1, -1, -1, 0.5, 0.5][e], a = typeof t == "number" ? t : t.reduce((n, o) => n * o, 1);
|
|
870
|
-
return r > 0 ? Math.ceil(a * r) : void 0;
|
|
871
|
-
}, Te = (e) => {
|
|
872
|
-
switch (e) {
|
|
873
|
-
case "float16":
|
|
874
|
-
return typeof Float16Array < "u" && Float16Array.from ? Float16Array : Uint16Array;
|
|
875
|
-
case "float32":
|
|
876
|
-
return Float32Array;
|
|
877
|
-
case "uint8":
|
|
878
|
-
return Uint8Array;
|
|
879
|
-
case "int8":
|
|
880
|
-
return Int8Array;
|
|
881
|
-
case "uint16":
|
|
882
|
-
return Uint16Array;
|
|
883
|
-
case "int16":
|
|
884
|
-
return Int16Array;
|
|
885
|
-
case "int32":
|
|
886
|
-
return Int32Array;
|
|
887
|
-
case "bool":
|
|
888
|
-
return Uint8Array;
|
|
889
|
-
case "float64":
|
|
890
|
-
return Float64Array;
|
|
891
|
-
case "uint32":
|
|
892
|
-
return Uint32Array;
|
|
893
|
-
case "int64":
|
|
894
|
-
return BigInt64Array;
|
|
895
|
-
case "uint64":
|
|
896
|
-
return BigUint64Array;
|
|
897
|
-
default:
|
|
898
|
-
throw new Error(`unsupported type: ${e}`);
|
|
899
|
-
}
|
|
900
|
-
}, Me = (e) => {
|
|
901
|
-
switch (e) {
|
|
902
|
-
case "verbose":
|
|
903
|
-
return 0;
|
|
904
|
-
case "info":
|
|
905
|
-
return 1;
|
|
906
|
-
case "warning":
|
|
907
|
-
return 2;
|
|
908
|
-
case "error":
|
|
909
|
-
return 3;
|
|
910
|
-
case "fatal":
|
|
911
|
-
return 4;
|
|
912
|
-
default:
|
|
913
|
-
throw new Error(`unsupported logging level: ${e}`);
|
|
914
|
-
}
|
|
915
|
-
}, bt = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint8" || e === "bool" || e === "uint4" || e === "int4", vt = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint64" || e === "int8" || e === "uint8" || e === "bool" || e === "uint4" || e === "int4", it = (e) => {
|
|
916
|
-
switch (e) {
|
|
917
|
-
case "none":
|
|
918
|
-
return 0;
|
|
919
|
-
case "cpu":
|
|
920
|
-
return 1;
|
|
921
|
-
case "cpu-pinned":
|
|
922
|
-
return 2;
|
|
923
|
-
case "texture":
|
|
924
|
-
return 3;
|
|
925
|
-
case "gpu-buffer":
|
|
926
|
-
return 4;
|
|
927
|
-
case "ml-tensor":
|
|
928
|
-
return 5;
|
|
929
|
-
default:
|
|
930
|
-
throw new Error(`unsupported data location: ${e}`);
|
|
931
|
-
}
|
|
932
|
-
};
|
|
933
|
-
}), Tt, Lr = b(() => {
|
|
934
|
-
ct(), Tt = async (e) => {
|
|
935
|
-
if (typeof e == "string") {
|
|
936
|
-
let t = await fetch(e);
|
|
937
|
-
if (!t.ok) throw new Error(`failed to load external data file: ${e}`);
|
|
938
|
-
let r = t.headers.get("Content-Length"), a = r ? parseInt(r, 10) : 0;
|
|
939
|
-
if (a < 1073741824) return new Uint8Array(await t.arrayBuffer());
|
|
940
|
-
{
|
|
941
|
-
if (!t.body) throw new Error(`failed to load external data file: ${e}, no response body.`);
|
|
942
|
-
let n = t.body.getReader(), o;
|
|
943
|
-
try {
|
|
944
|
-
o = new ArrayBuffer(a);
|
|
945
|
-
} catch (i) {
|
|
946
|
-
if (i instanceof RangeError) {
|
|
947
|
-
let u = Math.ceil(a / 65536);
|
|
948
|
-
o = new WebAssembly.Memory({ initial: u, maximum: u }).buffer;
|
|
949
|
-
} else throw i;
|
|
950
|
-
}
|
|
951
|
-
let s = 0;
|
|
952
|
-
for (; ; ) {
|
|
953
|
-
let { done: i, value: u } = await n.read();
|
|
954
|
-
if (i) break;
|
|
955
|
-
let l = u.byteLength;
|
|
956
|
-
new Uint8Array(o, s, l).set(u), s += l;
|
|
957
|
-
}
|
|
958
|
-
return new Uint8Array(o, 0, a);
|
|
959
|
-
}
|
|
960
|
-
} else return e instanceof Blob ? new Uint8Array(await e.arrayBuffer()) : e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
961
|
-
};
|
|
962
|
-
}), $r, mn = b(() => {
|
|
963
|
-
de(), $r = (e, t) => new (Te(t))(e);
|
|
964
|
-
}), Vt, zt, Ht, qt, Rr, Zt, S, Mr = b(() => {
|
|
965
|
-
de(), Vt = ["V", "I", "W", "E", "F"], zt = (e, t) => {
|
|
966
|
-
console.log(`[${Vt[e]},${(/* @__PURE__ */ new Date()).toISOString()}]${t}`);
|
|
967
|
-
}, Rr = (e, t) => {
|
|
968
|
-
Ht = e, qt = t;
|
|
969
|
-
}, Zt = (e, t) => {
|
|
970
|
-
let r = Me(e), a = Me(Ht);
|
|
971
|
-
r >= a && zt(r, typeof t == "function" ? t() : t);
|
|
972
|
-
}, S = (...e) => {
|
|
973
|
-
qt && Zt(...e);
|
|
974
|
-
};
|
|
975
|
-
}), Ke, ut, Ye, Xt, Je, Kt, Qe, et, tt, Yt, Nr, gn = b(() => {
|
|
976
|
-
de(), Mr(), Ke = /* @__PURE__ */ new Map([["float32", 32], ["float16", 16], ["int32", 32], ["uint32", 32], ["int64", 64], ["uint64", 64], ["int8", 8], ["uint8", 8], ["int4", 4], ["uint4", 4]]), ut = (e, t) => {
|
|
977
|
-
if (t === "int32") return e;
|
|
978
|
-
let r = Ke.get(t);
|
|
979
|
-
if (!r) throw new Error(`WebNN backend does not support data type: ${t}`);
|
|
980
|
-
let a = r / 8;
|
|
981
|
-
if (e.byteLength % a !== 0) throw new Error(`Invalid Uint8Array length - must be a multiple of ${a}.`);
|
|
982
|
-
let n = e.byteLength / a, o = new (Te(t))(e.buffer, e.byteOffset, n);
|
|
983
|
-
switch (t) {
|
|
984
|
-
case "int64":
|
|
985
|
-
case "uint64": {
|
|
986
|
-
let s = new Int32Array(n);
|
|
987
|
-
for (let i = 0; i < n; i++) {
|
|
988
|
-
let u = o[i];
|
|
989
|
-
if (u > 2147483647n || u < -2147483648n) throw new Error("Can not convert int64 data to int32 - value out of range.");
|
|
990
|
-
s[i] = Number(u);
|
|
991
|
-
}
|
|
992
|
-
return new Uint8Array(s.buffer);
|
|
993
|
-
}
|
|
994
|
-
case "int8":
|
|
995
|
-
case "uint8":
|
|
996
|
-
case "uint32": {
|
|
997
|
-
if (t === "uint32" && o.some((i) => i > 2147483647)) throw new Error("Can not convert uint32 data to int32 - value out of range.");
|
|
998
|
-
let s = Int32Array.from(o, Number);
|
|
999
|
-
return new Uint8Array(s.buffer);
|
|
1000
|
-
}
|
|
1001
|
-
default:
|
|
1002
|
-
throw new Error(`Unsupported data conversion from ${t} to 'int32'`);
|
|
1003
|
-
}
|
|
1004
|
-
}, Ye = (e, t) => {
|
|
1005
|
-
if (t === "int32") return e;
|
|
1006
|
-
if (e.byteLength % 4 !== 0) throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");
|
|
1007
|
-
let r = e.byteLength / 4, a = new Int32Array(e.buffer, e.byteOffset, r);
|
|
1008
|
-
switch (t) {
|
|
1009
|
-
case "int64": {
|
|
1010
|
-
let n = BigInt64Array.from(a, BigInt);
|
|
1011
|
-
return new Uint8Array(n.buffer);
|
|
1012
|
-
}
|
|
1013
|
-
case "uint64": {
|
|
1014
|
-
if (a.some((o) => o < 0)) throw new Error("Can not convert int32 data to uin64 - negative value found.");
|
|
1015
|
-
let n = BigUint64Array.from(a, BigInt);
|
|
1016
|
-
return new Uint8Array(n.buffer);
|
|
1017
|
-
}
|
|
1018
|
-
case "int8": {
|
|
1019
|
-
if (a.some((o) => o < -128 || o > 127)) throw new Error("Can not convert int32 data to int8 - value out of range.");
|
|
1020
|
-
let n = Int8Array.from(a, Number);
|
|
1021
|
-
return new Uint8Array(n.buffer);
|
|
1022
|
-
}
|
|
1023
|
-
case "uint8": {
|
|
1024
|
-
if (a.some((n) => n < 0 || n > 255)) throw new Error("Can not convert int32 data to uint8 - value out of range.");
|
|
1025
|
-
return Uint8Array.from(a, Number);
|
|
1026
|
-
}
|
|
1027
|
-
case "uint32": {
|
|
1028
|
-
if (a.some((o) => o < 0)) throw new Error("Can not convert int32 data to uint32 - negative value found.");
|
|
1029
|
-
let n = Uint32Array.from(a, Number);
|
|
1030
|
-
return new Uint8Array(n.buffer);
|
|
1031
|
-
}
|
|
1032
|
-
default:
|
|
1033
|
-
throw new Error(`Unsupported data conversion from 'int32' to ${t}`);
|
|
1034
|
-
}
|
|
1035
|
-
}, Xt = 1, Je = () => Xt++, Kt = /* @__PURE__ */ new Map([["int8", "int32"], ["uint8", "int32"], ["uint32", "int32"], ["int64", "int32"]]), Qe = (e, t) => {
|
|
1036
|
-
let r = Ke.get(e);
|
|
1037
|
-
if (!r) throw new Error(`WebNN backend does not support data type: ${e}`);
|
|
1038
|
-
return t.length > 0 ? Math.ceil(t.reduce((a, n) => a * n) * r / 8) : 0;
|
|
1039
|
-
}, et = class {
|
|
1040
|
-
constructor(e) {
|
|
1041
|
-
this.isDataConverted = !1;
|
|
1042
|
-
let { sessionId: t, context: r, tensor: a, dataType: n, shape: o, fallbackDataType: s } = e;
|
|
1043
|
-
this.sessionId = t, this.mlContext = r, this.mlTensor = a, this.dataType = n, this.tensorShape = o, this.fallbackDataType = s;
|
|
1044
|
-
}
|
|
1045
|
-
get tensor() {
|
|
1046
|
-
return this.mlTensor;
|
|
1047
|
-
}
|
|
1048
|
-
get type() {
|
|
1049
|
-
return this.dataType;
|
|
1050
|
-
}
|
|
1051
|
-
get fallbackType() {
|
|
1052
|
-
return this.fallbackDataType;
|
|
1053
|
-
}
|
|
1054
|
-
get shape() {
|
|
1055
|
-
return this.tensorShape;
|
|
1056
|
-
}
|
|
1057
|
-
get byteLength() {
|
|
1058
|
-
return Qe(this.dataType, this.tensorShape);
|
|
1059
|
-
}
|
|
1060
|
-
destroy() {
|
|
1061
|
-
S("verbose", () => "[WebNN] TensorWrapper.destroy"), this.mlTensor.destroy();
|
|
1062
|
-
}
|
|
1063
|
-
write(e) {
|
|
1064
|
-
this.mlContext.writeTensor(this.mlTensor, e);
|
|
1065
|
-
}
|
|
1066
|
-
async read(e) {
|
|
1067
|
-
if (this.fallbackDataType) {
|
|
1068
|
-
let t = await this.mlContext.readTensor(this.mlTensor), r = Ye(new Uint8Array(t), this.dataType);
|
|
1069
|
-
if (e) {
|
|
1070
|
-
(e instanceof ArrayBuffer ? new Uint8Array(e) : new Uint8Array(e.buffer, e.byteOffset, e.byteLength)).set(r);
|
|
1071
|
-
return;
|
|
1072
|
-
} else return r.buffer;
|
|
1073
|
-
} else return e ? this.mlContext.readTensor(this.mlTensor, e) : this.mlContext.readTensor(this.mlTensor);
|
|
1074
|
-
}
|
|
1075
|
-
canReuseTensor(e, t, r) {
|
|
1076
|
-
return this.mlContext === e && this.dataType === t && this.tensorShape.length === r.length && this.tensorShape.every((a, n) => a === r[n]);
|
|
1077
|
-
}
|
|
1078
|
-
setIsDataConverted(e) {
|
|
1079
|
-
this.isDataConverted = e;
|
|
1080
|
-
}
|
|
1081
|
-
}, tt = class {
|
|
1082
|
-
constructor(e, t) {
|
|
1083
|
-
this.tensorManager = e, this.wrapper = t;
|
|
1084
|
-
}
|
|
1085
|
-
get tensorWrapper() {
|
|
1086
|
-
return this.wrapper;
|
|
1087
|
-
}
|
|
1088
|
-
releaseTensor() {
|
|
1089
|
-
this.tensorWrapper && (this.tensorManager.releaseTensor(this.tensorWrapper), this.wrapper = void 0);
|
|
1090
|
-
}
|
|
1091
|
-
async ensureTensor(e, t, r, a) {
|
|
1092
|
-
let n = this.tensorManager.getMLContext(e), o;
|
|
1093
|
-
if (!n.opSupportLimits().input.dataTypes.includes(t)) {
|
|
1094
|
-
if (o = Kt.get(t), !o || !n.opSupportLimits().input.dataTypes.includes(o)) throw new Error(`WebNN backend does not support data type: ${t}`);
|
|
1095
|
-
S("verbose", () => `[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${o}`);
|
|
1096
|
-
}
|
|
1097
|
-
if (this.wrapper) {
|
|
1098
|
-
if (this.wrapper.canReuseTensor(n, t, r)) return this.wrapper.tensor;
|
|
1099
|
-
if (a) {
|
|
1100
|
-
if (this.wrapper.byteLength !== Qe(t, r)) throw new Error("Unable to copy data to tensor with different size.");
|
|
1101
|
-
this.activeUpload = new Uint8Array(await this.wrapper.read());
|
|
1102
|
-
}
|
|
1103
|
-
this.tensorManager.releaseTensor(this.wrapper);
|
|
1104
|
-
}
|
|
1105
|
-
let s = typeof MLTensorUsage > "u" ? void 0 : MLTensorUsage.READ | MLTensorUsage.WRITE;
|
|
1106
|
-
return this.wrapper = await this.tensorManager.getCachedTensor(e, t, r, s, !0, !0, o), a && this.activeUpload && (this.wrapper.write(this.activeUpload), this.activeUpload = void 0), this.wrapper.tensor;
|
|
1107
|
-
}
|
|
1108
|
-
upload(e) {
|
|
1109
|
-
let t = e;
|
|
1110
|
-
if (this.wrapper) {
|
|
1111
|
-
if (this.wrapper.fallbackType) if (this.wrapper.fallbackType === "int32") t = ut(e, this.wrapper.type), this.wrapper.setIsDataConverted(!0);
|
|
1112
|
-
else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);
|
|
1113
|
-
if (e.byteLength === this.wrapper.byteLength) {
|
|
1114
|
-
this.wrapper.write(t);
|
|
1115
|
-
return;
|
|
1116
|
-
} else S("verbose", () => "Data size does not match tensor size. Releasing tensor."), this.releaseTensor();
|
|
1117
|
-
}
|
|
1118
|
-
this.activeUpload ? this.activeUpload.set(t) : this.activeUpload = new Uint8Array(t);
|
|
1119
|
-
}
|
|
1120
|
-
async download(e) {
|
|
1121
|
-
if (this.activeUpload) {
|
|
1122
|
-
let t = this.wrapper?.isDataConverted ? Ye(this.activeUpload, this.wrapper?.type) : this.activeUpload;
|
|
1123
|
-
if (e) {
|
|
1124
|
-
e instanceof ArrayBuffer ? new Uint8Array(e).set(t) : new Uint8Array(e.buffer, e.byteOffset, e.byteLength).set(t);
|
|
1125
|
-
return;
|
|
1126
|
-
} else return t.buffer;
|
|
1127
|
-
}
|
|
1128
|
-
if (!this.wrapper) throw new Error("Tensor has not been created.");
|
|
1129
|
-
return e ? this.wrapper.read(e) : this.wrapper.read();
|
|
1130
|
-
}
|
|
1131
|
-
}, Yt = class {
|
|
1132
|
-
constructor(e) {
|
|
1133
|
-
this.backend = e, this.tensorTrackersById = /* @__PURE__ */ new Map(), this.freeTensors = [], this.externalTensors = /* @__PURE__ */ new Set();
|
|
1134
|
-
}
|
|
1135
|
-
getMLContext(e) {
|
|
1136
|
-
let t = this.backend.getMLContext(e);
|
|
1137
|
-
if (!t) throw new Error("MLContext not found for session.");
|
|
1138
|
-
return t;
|
|
1139
|
-
}
|
|
1140
|
-
reserveTensorId() {
|
|
1141
|
-
let e = Je();
|
|
1142
|
-
return this.tensorTrackersById.set(e, new tt(this)), e;
|
|
1143
|
-
}
|
|
1144
|
-
releaseTensorId(e) {
|
|
1145
|
-
let t = this.tensorTrackersById.get(e);
|
|
1146
|
-
t && (this.tensorTrackersById.delete(e), t.tensorWrapper && this.releaseTensor(t.tensorWrapper));
|
|
1147
|
-
}
|
|
1148
|
-
async ensureTensor(e, t, r, a, n) {
|
|
1149
|
-
S("verbose", () => `[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${a}, copyOld: ${n}}`);
|
|
1150
|
-
let o = this.tensorTrackersById.get(t);
|
|
1151
|
-
if (!o) throw new Error("Tensor not found.");
|
|
1152
|
-
return o.ensureTensor(e, r, a, n);
|
|
1153
|
-
}
|
|
1154
|
-
upload(e, t) {
|
|
1155
|
-
let r = this.tensorTrackersById.get(e);
|
|
1156
|
-
if (!r) throw new Error("Tensor not found.");
|
|
1157
|
-
r.upload(t);
|
|
1158
|
-
}
|
|
1159
|
-
async download(e, t) {
|
|
1160
|
-
S("verbose", () => `[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);
|
|
1161
|
-
let r = this.tensorTrackersById.get(e);
|
|
1162
|
-
if (!r) throw new Error("Tensor not found.");
|
|
1163
|
-
return r.download(t);
|
|
1164
|
-
}
|
|
1165
|
-
releaseTensorsForSession(e) {
|
|
1166
|
-
for (let t of this.freeTensors) t.sessionId === e && t.destroy();
|
|
1167
|
-
this.freeTensors = this.freeTensors.filter((t) => t.sessionId !== e);
|
|
1168
|
-
}
|
|
1169
|
-
registerTensor(e, t, r, a) {
|
|
1170
|
-
let n = this.getMLContext(e), o = Je(), s = new et({ sessionId: e, context: n, tensor: t, dataType: r, shape: a });
|
|
1171
|
-
return this.tensorTrackersById.set(o, new tt(this, s)), this.externalTensors.add(s), o;
|
|
1172
|
-
}
|
|
1173
|
-
async getCachedTensor(e, t, r, a, n, o, s) {
|
|
1174
|
-
let i = this.getMLContext(e);
|
|
1175
|
-
for (let [l, f] of this.freeTensors.entries()) if (f.canReuseTensor(i, t, r)) {
|
|
1176
|
-
S("verbose", () => `[WebNN] Reusing tensor {dataType: ${t}, ${s ? `fallbackDataType: ${s},` : ""} shape: ${r}`);
|
|
1177
|
-
let p = this.freeTensors.splice(l, 1)[0];
|
|
1178
|
-
return p.sessionId = e, p;
|
|
1179
|
-
}
|
|
1180
|
-
S("verbose", () => `[WebNN] MLContext.createTensor {dataType: ${t}, ${s ? `fallbackDataType: ${s},` : ""} shape: ${r}}`);
|
|
1181
|
-
let u = await i.createTensor({ dataType: s ?? t, shape: r, dimensions: r, usage: a, writable: n, readable: o });
|
|
1182
|
-
return new et({ sessionId: e, context: i, tensor: u, dataType: t, shape: r, fallbackDataType: s });
|
|
1183
|
-
}
|
|
1184
|
-
releaseTensor(e) {
|
|
1185
|
-
this.externalTensors.has(e) && this.externalTensors.delete(e), this.freeTensors.push(e);
|
|
1186
|
-
}
|
|
1187
|
-
}, Nr = (...e) => new Yt(...e);
|
|
1188
|
-
}), Br = {};
|
|
1189
|
-
Be(Br, { WebNNBackend: () => Sr });
|
|
1190
|
-
var me, Jt, Sr, bn = b(() => {
|
|
1191
|
-
de(), se(), mn(), gn(), Mr(), me = /* @__PURE__ */ new Map([[1, "float32"], [10, "float16"], [6, "int32"], [12, "uint32"], [7, "int64"], [13, "uint64"], [22, "int4"], [21, "uint4"], [3, "int8"], [2, "uint8"], [9, "uint8"]]), Jt = (e, t) => {
|
|
1192
|
-
if (e === t) return !0;
|
|
1193
|
-
if (e === void 0 || t === void 0) return !1;
|
|
1194
|
-
let r = Object.keys(e).sort(), a = Object.keys(t).sort();
|
|
1195
|
-
return r.length === a.length && r.every((n, o) => n === a[o] && e[n] === t[n]);
|
|
1196
|
-
}, Sr = class {
|
|
1197
|
-
constructor(e) {
|
|
1198
|
-
this.tensorManager = Nr(this), this.mlContextBySessionId = /* @__PURE__ */ new Map(), this.sessionIdsByMLContext = /* @__PURE__ */ new Map(), this.mlContextCache = [], this.sessionGraphInputs = /* @__PURE__ */ new Map(), this.sessionGraphOutputs = /* @__PURE__ */ new Map(), this.temporaryGraphInputs = [], this.temporaryGraphOutputs = [], this.temporarySessionTensorIds = /* @__PURE__ */ new Map(), Rr(e.logLevel, !!e.debug);
|
|
1199
|
-
}
|
|
1200
|
-
get currentSessionId() {
|
|
1201
|
-
if (this.activeSessionId === void 0) throw new Error("No active session");
|
|
1202
|
-
return this.activeSessionId;
|
|
1203
|
-
}
|
|
1204
|
-
onRunStart(e) {
|
|
1205
|
-
S("verbose", () => `[WebNN] onRunStart {sessionId: ${e}}`), this.activeSessionId = e;
|
|
1206
|
-
}
|
|
1207
|
-
onRunEnd(e) {
|
|
1208
|
-
S("verbose", () => `[WebNN] onRunEnd {sessionId: ${e}}`);
|
|
1209
|
-
let t = this.temporarySessionTensorIds.get(e);
|
|
1210
|
-
if (t) {
|
|
1211
|
-
for (let r of t) S("verbose", () => `[WebNN] releasing temporary tensor {tensorId: ${r}}`), this.tensorManager.releaseTensorId(r);
|
|
1212
|
-
this.temporarySessionTensorIds.delete(e), this.activeSessionId = void 0;
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
async createMLContext(e) {
|
|
1216
|
-
if (e instanceof GPUDevice) {
|
|
1217
|
-
let r = this.mlContextCache.findIndex((a) => a.gpuDevice === e);
|
|
1218
|
-
if (r !== -1) return this.mlContextCache[r].mlContext;
|
|
1219
|
-
{
|
|
1220
|
-
let a = await navigator.ml.createContext(e);
|
|
1221
|
-
return this.mlContextCache.push({ gpuDevice: e, mlContext: a }), a;
|
|
1222
|
-
}
|
|
1223
|
-
} else if (e === void 0) {
|
|
1224
|
-
let r = this.mlContextCache.findIndex((a) => a.options === void 0 && a.gpuDevice === void 0);
|
|
1225
|
-
if (r !== -1) return this.mlContextCache[r].mlContext;
|
|
1226
|
-
{
|
|
1227
|
-
let a = await navigator.ml.createContext();
|
|
1228
|
-
return this.mlContextCache.push({ mlContext: a }), a;
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
let t = this.mlContextCache.findIndex((r) => Jt(r.options, e));
|
|
1232
|
-
if (t !== -1) return this.mlContextCache[t].mlContext;
|
|
1233
|
-
{
|
|
1234
|
-
let r = await navigator.ml.createContext(e);
|
|
1235
|
-
return this.mlContextCache.push({ options: e, mlContext: r }), r;
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
registerMLContext(e, t) {
|
|
1239
|
-
this.mlContextBySessionId.set(e, t);
|
|
1240
|
-
let r = this.sessionIdsByMLContext.get(t);
|
|
1241
|
-
r || (r = /* @__PURE__ */ new Set(), this.sessionIdsByMLContext.set(t, r)), r.add(e), this.temporaryGraphInputs.length > 0 && (this.sessionGraphInputs.set(e, this.temporaryGraphInputs), this.temporaryGraphInputs = []), this.temporaryGraphOutputs.length > 0 && (this.sessionGraphOutputs.set(e, this.temporaryGraphOutputs), this.temporaryGraphOutputs = []);
|
|
1242
|
-
}
|
|
1243
|
-
onReleaseSession(e) {
|
|
1244
|
-
this.sessionGraphInputs.delete(e), this.sessionGraphOutputs.delete(e);
|
|
1245
|
-
let t = this.mlContextBySessionId.get(e);
|
|
1246
|
-
if (!t) return;
|
|
1247
|
-
this.tensorManager.releaseTensorsForSession(e), this.mlContextBySessionId.delete(e);
|
|
1248
|
-
let r = this.sessionIdsByMLContext.get(t);
|
|
1249
|
-
if (r.delete(e), r.size === 0) {
|
|
1250
|
-
this.sessionIdsByMLContext.delete(t);
|
|
1251
|
-
let a = this.mlContextCache.findIndex((n) => n.mlContext === t);
|
|
1252
|
-
a !== -1 && this.mlContextCache.splice(a, 1);
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
getMLContext(e) {
|
|
1256
|
-
return this.mlContextBySessionId.get(e);
|
|
1257
|
-
}
|
|
1258
|
-
reserveTensorId() {
|
|
1259
|
-
return this.tensorManager.reserveTensorId();
|
|
1260
|
-
}
|
|
1261
|
-
releaseTensorId(e) {
|
|
1262
|
-
S("verbose", () => `[WebNN] releaseTensorId {tensorId: ${e}}`), this.tensorManager.releaseTensorId(e);
|
|
1263
|
-
}
|
|
1264
|
-
async ensureTensor(e, t, r, a, n) {
|
|
1265
|
-
let o = me.get(r);
|
|
1266
|
-
if (!o) throw new Error(`Unsupported ONNX data type: ${r}`);
|
|
1267
|
-
return this.tensorManager.ensureTensor(e ?? this.currentSessionId, t, o, a, n);
|
|
1268
|
-
}
|
|
1269
|
-
async createTemporaryTensor(e, t, r) {
|
|
1270
|
-
S("verbose", () => `[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${r}}`);
|
|
1271
|
-
let a = me.get(t);
|
|
1272
|
-
if (!a) throw new Error(`Unsupported ONNX data type: ${t}`);
|
|
1273
|
-
let n = this.tensorManager.reserveTensorId();
|
|
1274
|
-
await this.tensorManager.ensureTensor(e, n, a, r, !1);
|
|
1275
|
-
let o = this.temporarySessionTensorIds.get(e);
|
|
1276
|
-
return o ? o.push(n) : this.temporarySessionTensorIds.set(e, [n]), n;
|
|
1277
|
-
}
|
|
1278
|
-
uploadTensor(e, t) {
|
|
1279
|
-
if (!I().shouldTransferToMLTensor) throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");
|
|
1280
|
-
S("verbose", () => `[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`), this.tensorManager.upload(e, t);
|
|
1281
|
-
}
|
|
1282
|
-
async downloadTensor(e, t) {
|
|
1283
|
-
return this.tensorManager.download(e, t);
|
|
1284
|
-
}
|
|
1285
|
-
createMLTensorDownloader(e, t) {
|
|
1286
|
-
return async () => {
|
|
1287
|
-
let r = await this.tensorManager.download(e);
|
|
1288
|
-
return $r(r, t);
|
|
1289
|
-
};
|
|
1290
|
-
}
|
|
1291
|
-
registerMLTensor(e, t, r, a) {
|
|
1292
|
-
let n = me.get(r);
|
|
1293
|
-
if (!n) throw new Error(`Unsupported ONNX data type: ${r}`);
|
|
1294
|
-
let o = this.tensorManager.registerTensor(e, t, n, a);
|
|
1295
|
-
return S("verbose", () => `[WebNN] registerMLTensor {tensor: ${t}, dataType: ${n}, dimensions: ${a}} -> {tensorId: ${o}}`), o;
|
|
1296
|
-
}
|
|
1297
|
-
registerMLConstant(e, t, r, a, n, o, s = !1) {
|
|
1298
|
-
if (!o) throw new Error("External mounted files are not available.");
|
|
1299
|
-
let i = e;
|
|
1300
|
-
e.startsWith("./") && (i = e.substring(2));
|
|
1301
|
-
let u = o.get(i);
|
|
1302
|
-
if (!u) throw new Error(`File with name ${i} not found in preloaded files.`);
|
|
1303
|
-
if (t + r > u.byteLength) throw new Error("Out of bounds: data offset and length exceed the external file data size.");
|
|
1304
|
-
let l = u.slice(t, t + r).buffer, f;
|
|
1305
|
-
switch (n.dataType) {
|
|
1306
|
-
case "float32":
|
|
1307
|
-
f = new Float32Array(l);
|
|
1308
|
-
break;
|
|
1309
|
-
case "float16":
|
|
1310
|
-
f = typeof Float16Array < "u" && Float16Array.from ? new Float16Array(l) : new Uint16Array(l);
|
|
1311
|
-
break;
|
|
1312
|
-
case "int32":
|
|
1313
|
-
f = new Int32Array(l);
|
|
1314
|
-
break;
|
|
1315
|
-
case "uint32":
|
|
1316
|
-
f = new Uint32Array(l);
|
|
1317
|
-
break;
|
|
1318
|
-
case "int64":
|
|
1319
|
-
if (s) {
|
|
1320
|
-
let p = ut(new Uint8Array(l), "int64");
|
|
1321
|
-
f = new Int32Array(p.buffer), n.dataType = "int32";
|
|
1322
|
-
} else f = new BigInt64Array(l);
|
|
1323
|
-
break;
|
|
1324
|
-
case "uint64":
|
|
1325
|
-
f = new BigUint64Array(l);
|
|
1326
|
-
break;
|
|
1327
|
-
case "int8":
|
|
1328
|
-
f = new Int8Array(l);
|
|
1329
|
-
break;
|
|
1330
|
-
case "int4":
|
|
1331
|
-
case "uint4":
|
|
1332
|
-
case "uint8":
|
|
1333
|
-
f = new Uint8Array(l);
|
|
1334
|
-
break;
|
|
1335
|
-
default:
|
|
1336
|
-
throw new Error(`Unsupported data type: ${n.dataType} in creating WebNN Constant from external data.`);
|
|
1337
|
-
}
|
|
1338
|
-
return S("verbose", () => `[WebNN] registerMLConstant {dataType: ${n.dataType}, shape: ${n.shape}}} ${s ? "(Note: it was int64 data type and registered to int32 as workaround)" : ""}`), a.constant(n, f);
|
|
1339
|
-
}
|
|
1340
|
-
registerGraphInput(e) {
|
|
1341
|
-
this.temporaryGraphInputs.push(e);
|
|
1342
|
-
}
|
|
1343
|
-
registerGraphOutput(e) {
|
|
1344
|
-
this.temporaryGraphOutputs.push(e);
|
|
1345
|
-
}
|
|
1346
|
-
isGraphInput(e, t) {
|
|
1347
|
-
let r = this.sessionGraphInputs.get(e);
|
|
1348
|
-
return r ? r.includes(t) : !1;
|
|
1349
|
-
}
|
|
1350
|
-
isGraphOutput(e, t) {
|
|
1351
|
-
let r = this.sessionGraphOutputs.get(e);
|
|
1352
|
-
return r ? r.includes(t) : !1;
|
|
1353
|
-
}
|
|
1354
|
-
isGraphInputOutputTypeSupported(e, t, r = !0) {
|
|
1355
|
-
let a = this.mlContextBySessionId.get(e), n = me.get(re(t));
|
|
1356
|
-
return typeof n > "u" ? !1 : r ? !!a?.opSupportLimits().input.dataTypes.includes(n) : !!a?.opSupportLimits().output.dataTypes.includes(n);
|
|
1357
|
-
}
|
|
1358
|
-
flush() {
|
|
1359
|
-
}
|
|
1360
|
-
};
|
|
1361
|
-
}), Qt, Et, It, Y, er, rt, Ne, Ct, At, nt, xt, Ut, Ot, kr = b(() => {
|
|
1362
|
-
oe(), wn(), yn(), de(), se(), gt(), Lr(), Qt = (e, t) => {
|
|
1363
|
-
I()._OrtInit(e, t) !== 0 && T("Can't initialize onnxruntime.");
|
|
1364
|
-
}, Et = async (e) => {
|
|
1365
|
-
Qt(e.wasm.numThreads, Me(e.logLevel));
|
|
1366
|
-
}, It = async (e, t) => {
|
|
1367
|
-
I().asyncInit?.();
|
|
1368
|
-
let r = e.webgpu.adapter;
|
|
1369
|
-
if (t === "webgpu") {
|
|
1370
|
-
if (typeof navigator > "u" || !navigator.gpu) throw new Error("WebGPU is not supported in current environment");
|
|
1371
|
-
if (r) {
|
|
1372
|
-
if (typeof r.limits != "object" || typeof r.features != "object" || typeof r.requestDevice != "function") throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.");
|
|
1373
|
-
} else {
|
|
1374
|
-
let a = e.webgpu.powerPreference;
|
|
1375
|
-
if (a !== void 0 && a !== "low-power" && a !== "high-performance") throw new Error(`Invalid powerPreference setting: "${a}"`);
|
|
1376
|
-
let n = e.webgpu.forceFallbackAdapter;
|
|
1377
|
-
if (n !== void 0 && typeof n != "boolean") throw new Error(`Invalid forceFallbackAdapter setting: "${n}"`);
|
|
1378
|
-
if (r = await navigator.gpu.requestAdapter({ powerPreference: a, forceFallbackAdapter: n }), !r) throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.');
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
if (t === "webnn" && (typeof navigator > "u" || !navigator.ml)) throw new Error("WebNN is not supported in current environment");
|
|
1382
|
-
if (t === "webgpu" && I().webgpuInit((a) => {
|
|
1383
|
-
e.webgpu.device = a;
|
|
1384
|
-
}), t === "webnn") {
|
|
1385
|
-
let a = new (bn(), dt(Br)).WebNNBackend(e);
|
|
1386
|
-
I().webnnInit([a, () => a.reserveTensorId(), (n) => a.releaseTensorId(n), async (n, o, s, i, u) => a.ensureTensor(n, o, s, i, u), (n, o) => {
|
|
1387
|
-
a.uploadTensor(n, o);
|
|
1388
|
-
}, async (n, o) => a.downloadTensor(n, o), (n, o) => a.registerMLContext(n, o), !!e.trace]);
|
|
1389
|
-
}
|
|
1390
|
-
}, Y = /* @__PURE__ */ new Map(), er = (e) => {
|
|
1391
|
-
let t = I(), r = t.stackSave();
|
|
1392
|
-
try {
|
|
1393
|
-
let a = t.PTR_SIZE, n = t.stackAlloc(2 * a);
|
|
1394
|
-
t._OrtGetInputOutputCount(e, n, n + a) !== 0 && T("Can't get session input/output count.");
|
|
1395
|
-
let o = a === 4 ? "i32" : "i64";
|
|
1396
|
-
return [Number(t.getValue(n, o)), Number(t.getValue(n + a, o))];
|
|
1397
|
-
} finally {
|
|
1398
|
-
t.stackRestore(r);
|
|
1399
|
-
}
|
|
1400
|
-
}, rt = (e, t) => {
|
|
1401
|
-
let r = I(), a = r.stackSave(), n = 0;
|
|
1402
|
-
try {
|
|
1403
|
-
let o = r.PTR_SIZE, s = r.stackAlloc(2 * o);
|
|
1404
|
-
r._OrtGetInputOutputMetadata(e, t, s, s + o) !== 0 && T("Can't get session input/output metadata.");
|
|
1405
|
-
let i = Number(r.getValue(s, "*"));
|
|
1406
|
-
n = Number(r.getValue(s + o, "*"));
|
|
1407
|
-
let u = r.HEAP32[n / 4];
|
|
1408
|
-
if (u === 0) return [i, 0];
|
|
1409
|
-
let l = r.HEAPU32[n / 4 + 1], f = [];
|
|
1410
|
-
for (let p = 0; p < l; p++) {
|
|
1411
|
-
let d = Number(r.getValue(n + 8 + p * o, "*"));
|
|
1412
|
-
f.push(d !== 0 ? r.UTF8ToString(d) : Number(r.getValue(n + 8 + (p + l) * o, "*")));
|
|
1413
|
-
}
|
|
1414
|
-
return [i, u, f];
|
|
1415
|
-
} finally {
|
|
1416
|
-
r.stackRestore(a), n !== 0 && r._OrtFree(n);
|
|
1417
|
-
}
|
|
1418
|
-
}, Ne = (e) => {
|
|
1419
|
-
let t = I(), r = t._malloc(e.byteLength);
|
|
1420
|
-
if (r === 0) throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);
|
|
1421
|
-
return t.HEAPU8.set(e, r), [r, e.byteLength];
|
|
1422
|
-
}, Ct = async (e, t) => {
|
|
1423
|
-
let r, a, n = I();
|
|
1424
|
-
Array.isArray(e) ? [r, a] = e : e.buffer === n.HEAPU8.buffer ? [r, a] = [e.byteOffset, e.byteLength] : [r, a] = Ne(e);
|
|
1425
|
-
let o = 0, s = 0, i = 0, u = [], l = [], f = [];
|
|
1426
|
-
try {
|
|
1427
|
-
if ([s, u] = await Or(t), t?.externalData && n.mountExternalData) {
|
|
1428
|
-
let m = [];
|
|
1429
|
-
for (let E of t.externalData) {
|
|
1430
|
-
let k = typeof E == "string" ? E : E.path;
|
|
1431
|
-
m.push(Tt(typeof E == "string" ? E : E.data).then((P) => {
|
|
1432
|
-
n.mountExternalData(k, P);
|
|
1433
|
-
}));
|
|
1434
|
-
}
|
|
1435
|
-
await Promise.all(m);
|
|
1436
|
-
}
|
|
1437
|
-
for (let m of t?.executionProviders ?? []) if ((typeof m == "string" ? m : m.name) === "webnn") {
|
|
1438
|
-
if (n.shouldTransferToMLTensor = !1, typeof m != "string") {
|
|
1439
|
-
let E = m, k = E?.context, P = E?.gpuDevice, N = E?.deviceType, B = E?.powerPreference;
|
|
1440
|
-
k ? n.currentContext = k : P ? n.currentContext = await n.webnnCreateMLContext(P) : n.currentContext = await n.webnnCreateMLContext({ deviceType: N, powerPreference: B });
|
|
1441
|
-
} else n.currentContext = await n.webnnCreateMLContext();
|
|
1442
|
-
break;
|
|
1443
|
-
}
|
|
1444
|
-
o = await n._OrtCreateSession(r, a, s), n.webgpuOnCreateSession?.(o), o === 0 && T("Can't create a session."), n.jsepOnCreateSession?.(), n.currentContext && (n.webnnRegisterMLContext(o, n.currentContext), n.currentContext = void 0, n.shouldTransferToMLTensor = !0);
|
|
1445
|
-
let [p, d] = er(o), h = !!t?.enableGraphCapture, w = [], v = [], C = [], y = [], c = [];
|
|
1446
|
-
for (let m = 0; m < p; m++) {
|
|
1447
|
-
let [E, k, P] = rt(o, m);
|
|
1448
|
-
E === 0 && T("Can't get an input name."), l.push(E);
|
|
1449
|
-
let N = n.UTF8ToString(E);
|
|
1450
|
-
w.push(N), C.push(k === 0 ? { name: N, isTensor: !1 } : { name: N, isTensor: !0, type: $e(k), shape: P });
|
|
1451
|
-
}
|
|
1452
|
-
for (let m = 0; m < d; m++) {
|
|
1453
|
-
let [E, k, P] = rt(o, m + p);
|
|
1454
|
-
E === 0 && T("Can't get an output name."), f.push(E);
|
|
1455
|
-
let N = n.UTF8ToString(E);
|
|
1456
|
-
v.push(N), y.push(k === 0 ? { name: N, isTensor: !1 } : { name: N, isTensor: !0, type: $e(k), shape: P });
|
|
1457
|
-
{
|
|
1458
|
-
if (h && t?.preferredOutputLocation === void 0) {
|
|
1459
|
-
c.push("gpu-buffer");
|
|
1460
|
-
continue;
|
|
1461
|
-
}
|
|
1462
|
-
let B = typeof t?.preferredOutputLocation == "string" ? t.preferredOutputLocation : t?.preferredOutputLocation?.[N] ?? "cpu", he = n.webnnIsGraphOutput;
|
|
1463
|
-
if (B === "cpu" && he && he(o, N)) {
|
|
1464
|
-
c.push("ml-tensor-cpu-output");
|
|
1465
|
-
continue;
|
|
1466
|
-
}
|
|
1467
|
-
if (B !== "cpu" && B !== "cpu-pinned" && B !== "gpu-buffer" && B !== "ml-tensor") throw new Error(`Not supported preferred output location: ${B}.`);
|
|
1468
|
-
if (h && B !== "gpu-buffer") throw new Error(`Not supported preferred output location: ${B}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);
|
|
1469
|
-
c.push(B);
|
|
1470
|
-
}
|
|
1471
|
-
}
|
|
1472
|
-
let A = null;
|
|
1473
|
-
return c.some((m) => m === "gpu-buffer" || m === "ml-tensor" || m === "ml-tensor-cpu-output") && (i = n._OrtCreateBinding(o), i === 0 && T("Can't create IO binding."), A = { handle: i, outputPreferredLocations: c, outputPreferredLocationsEncoded: c.map((m) => m === "ml-tensor-cpu-output" ? "ml-tensor" : m).map((m) => it(m)) }), Y.set(o, [o, l, f, A, h, !1]), [o, w, v, C, y];
|
|
1474
|
-
} catch (p) {
|
|
1475
|
-
throw l.forEach((d) => n._OrtFree(d)), f.forEach((d) => n._OrtFree(d)), i !== 0 && n._OrtReleaseBinding(i) !== 0 && T("Can't release IO binding."), o !== 0 && n._OrtReleaseSession(o) !== 0 && T("Can't release session."), p;
|
|
1476
|
-
} finally {
|
|
1477
|
-
n._free(r), s !== 0 && n._OrtReleaseSessionOptions(s) !== 0 && T("Can't release session options."), u.forEach((p) => n._free(p)), n.unmountExternalData?.();
|
|
1478
|
-
}
|
|
1479
|
-
}, At = (e) => {
|
|
1480
|
-
let t = I(), r = Y.get(e);
|
|
1481
|
-
if (!r) throw new Error(`cannot release session. invalid session id: ${e}`);
|
|
1482
|
-
let [a, n, o, s, i] = r;
|
|
1483
|
-
s && (i && t._OrtClearBoundOutputs(s.handle) !== 0 && T("Can't clear bound outputs."), t._OrtReleaseBinding(s.handle) !== 0 && T("Can't release IO binding.")), t.jsepOnReleaseSession?.(e), t.webnnOnReleaseSession?.(e), t.webgpuOnReleaseSession?.(e), n.forEach((u) => t._OrtFree(u)), o.forEach((u) => t._OrtFree(u)), t._OrtReleaseSession(a) !== 0 && T("Can't release session."), Y.delete(e);
|
|
1484
|
-
}, nt = async (e, t, r, a, n, o, s = !1) => {
|
|
1485
|
-
if (!e) {
|
|
1486
|
-
t.push(0);
|
|
1487
|
-
return;
|
|
1488
|
-
}
|
|
1489
|
-
let i = I(), u = i.PTR_SIZE, l = e[0], f = e[1], p = e[3], d = p, h, w;
|
|
1490
|
-
if (l === "string" && (p === "gpu-buffer" || p === "ml-tensor")) throw new Error("String tensor is not supported on GPU.");
|
|
1491
|
-
if (s && p !== "gpu-buffer") throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);
|
|
1492
|
-
if (p === "gpu-buffer") {
|
|
1493
|
-
let y = e[2].gpuBuffer;
|
|
1494
|
-
w = ue(re(l), f);
|
|
1495
|
-
{
|
|
1496
|
-
let c = i.webgpuRegisterBuffer;
|
|
1497
|
-
if (!c) throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');
|
|
1498
|
-
h = c(y, a);
|
|
1499
|
-
}
|
|
1500
|
-
} else if (p === "ml-tensor") {
|
|
1501
|
-
let y = e[2].mlTensor;
|
|
1502
|
-
w = ue(re(l), f);
|
|
1503
|
-
let c = i.webnnRegisterMLTensor;
|
|
1504
|
-
if (!c) throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');
|
|
1505
|
-
h = c(a, y, re(l), f);
|
|
1506
|
-
} else {
|
|
1507
|
-
let y = e[2];
|
|
1508
|
-
if (Array.isArray(y)) {
|
|
1509
|
-
w = u * y.length, h = i._malloc(w), r.push(h);
|
|
1510
|
-
for (let c = 0; c < y.length; c++) {
|
|
1511
|
-
if (typeof y[c] != "string") throw new TypeError(`tensor data at index ${c} is not a string`);
|
|
1512
|
-
i.setValue(h + c * u, G(y[c], r), "*");
|
|
1513
|
-
}
|
|
1514
|
-
} else {
|
|
1515
|
-
let c = i.webnnIsGraphInput, A = i.webnnIsGraphOutput;
|
|
1516
|
-
if (l !== "string" && c && A) {
|
|
1517
|
-
let m = i.UTF8ToString(n);
|
|
1518
|
-
if (c(a, m) || A(a, m)) {
|
|
1519
|
-
let E = re(l);
|
|
1520
|
-
w = ue(E, f), d = "ml-tensor";
|
|
1521
|
-
let k = i.webnnCreateTemporaryTensor, P = i.webnnUploadTensor;
|
|
1522
|
-
if (!k || !P) throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');
|
|
1523
|
-
let N = await k(a, E, f);
|
|
1524
|
-
P(N, new Uint8Array(y.buffer, y.byteOffset, y.byteLength)), h = N;
|
|
1525
|
-
} else w = y.byteLength, h = i._malloc(w), r.push(h), i.HEAPU8.set(new Uint8Array(y.buffer, y.byteOffset, w), h);
|
|
1526
|
-
} else w = y.byteLength, h = i._malloc(w), r.push(h), i.HEAPU8.set(new Uint8Array(y.buffer, y.byteOffset, w), h);
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
let v = i.stackSave(), C = i.stackAlloc(4 * f.length);
|
|
1530
|
-
try {
|
|
1531
|
-
f.forEach((c, A) => i.setValue(C + A * u, c, u === 4 ? "i32" : "i64"));
|
|
1532
|
-
let y = i._OrtCreateTensor(re(l), h, w, C, f.length, it(d));
|
|
1533
|
-
y === 0 && T(`Can't create tensor for input/output. session=${a}, index=${o}.`), t.push(y);
|
|
1534
|
-
} finally {
|
|
1535
|
-
i.stackRestore(v);
|
|
1536
|
-
}
|
|
1537
|
-
}, xt = async (e, t, r, a, n, o) => {
|
|
1538
|
-
let s = I(), i = s.PTR_SIZE, u = Y.get(e);
|
|
1539
|
-
if (!u) throw new Error(`cannot run inference. invalid session id: ${e}`);
|
|
1540
|
-
let l = u[0], f = u[1], p = u[2], d = u[3], h = u[4], w = u[5], v = t.length, C = a.length, y = 0, c = [], A = [], m = [], E = [], k = s.stackSave(), P = s.stackAlloc(v * i), N = s.stackAlloc(v * i), B = s.stackAlloc(C * i), he = s.stackAlloc(C * i);
|
|
1541
|
-
try {
|
|
1542
|
-
[y, c] = Ur(o), ne("wasm prepareInputOutputTensor");
|
|
1543
|
-
for (let g = 0; g < v; g++) await nt(r[g], A, E, e, f[t[g]], t[g], h);
|
|
1544
|
-
for (let g = 0; g < C; g++) await nt(n[g], m, E, e, p[a[g]], v + a[g], h);
|
|
1545
|
-
ae("wasm prepareInputOutputTensor");
|
|
1546
|
-
for (let g = 0; g < v; g++) s.setValue(P + g * i, A[g], "*"), s.setValue(N + g * i, f[t[g]], "*");
|
|
1547
|
-
for (let g = 0; g < C; g++) s.setValue(B + g * i, m[g], "*"), s.setValue(he + g * i, p[a[g]], "*");
|
|
1548
|
-
if (d && !w) {
|
|
1549
|
-
let { handle: g, outputPreferredLocations: j, outputPreferredLocationsEncoded: Se } = d;
|
|
1550
|
-
if (f.length !== v) throw new Error(`input count from feeds (${v}) is expected to be always equal to model's input count (${f.length}).`);
|
|
1551
|
-
ne("wasm bindInputsOutputs");
|
|
1552
|
-
for (let x = 0; x < v; x++) {
|
|
1553
|
-
let V = t[x];
|
|
1554
|
-
await s._OrtBindInput(g, f[V], A[x]) !== 0 && T(`Can't bind input[${x}] for session=${e}.`);
|
|
1555
|
-
}
|
|
1556
|
-
for (let x = 0; x < C; x++) {
|
|
1557
|
-
let V = a[x];
|
|
1558
|
-
n[x]?.[3] ? s._OrtBindOutput(g, p[V], m[x], 0) !== 0 && T(`Can't bind pre-allocated output[${x}] for session=${e}.`) : s._OrtBindOutput(g, p[V], 0, Se[V]) !== 0 && T(`Can't bind output[${x}] to ${j[x]} for session=${e}.`);
|
|
1559
|
-
}
|
|
1560
|
-
ae("wasm bindInputsOutputs"), Y.set(e, [l, f, p, d, h, !0]);
|
|
1561
|
-
}
|
|
1562
|
-
s.jsepOnRunStart?.(l), s.webnnOnRunStart?.(l);
|
|
1563
|
-
let L;
|
|
1564
|
-
d ? L = await s._OrtRunWithBinding(l, d.handle, C, B, y) : L = await s._OrtRun(l, N, P, v, he, C, B, y), L !== 0 && T("failed to call OrtRun().");
|
|
1565
|
-
let H = [], Lt = [];
|
|
1566
|
-
ne("wasm ProcessOutputTensor");
|
|
1567
|
-
for (let g = 0; g < C; g++) {
|
|
1568
|
-
let j = Number(s.getValue(B + g * i, "*"));
|
|
1569
|
-
if (j === m[g]) {
|
|
1570
|
-
H.push(n[g]);
|
|
1571
|
-
continue;
|
|
1572
|
-
}
|
|
1573
|
-
let Se = s.stackSave(), x = s.stackAlloc(4 * i), V = !1, $, D = 0;
|
|
1574
|
-
try {
|
|
1575
|
-
s._OrtGetTensorData(j, x, x + i, x + 2 * i, x + 3 * i) !== 0 && T(`Can't access output tensor data on index ${g}.`);
|
|
1576
|
-
let ke = i === 4 ? "i32" : "i64", Ee = Number(s.getValue(x, ke));
|
|
1577
|
-
D = s.getValue(x + i, "*");
|
|
1578
|
-
let $t = s.getValue(x + i * 2, "*"), Zr = Number(s.getValue(x + i * 3, ke)), Z = [];
|
|
1579
|
-
for (let R = 0; R < Zr; R++) Z.push(Number(s.getValue($t + R * i, ke)));
|
|
1580
|
-
s._OrtFree($t) !== 0 && T("Can't free memory for tensor dims.");
|
|
1581
|
-
let X = Z.reduce((R, O) => R * O, 1);
|
|
1582
|
-
$ = $e(Ee);
|
|
1583
|
-
let ce = d?.outputPreferredLocations[a[g]];
|
|
1584
|
-
if ($ === "string") {
|
|
1585
|
-
if (ce === "gpu-buffer" || ce === "ml-tensor") throw new Error("String tensor is not supported on GPU.");
|
|
1586
|
-
let R = [];
|
|
1587
|
-
for (let O = 0; O < X; O++) {
|
|
1588
|
-
let q = s.getValue(D + O * i, "*"), Pe = s.getValue(D + (O + 1) * i, "*"), De = O === X - 1 ? void 0 : Pe - q;
|
|
1589
|
-
R.push(s.UTF8ToString(q, De));
|
|
1590
|
-
}
|
|
1591
|
-
H.push([$, Z, R, "cpu"]);
|
|
1592
|
-
} else if (ce === "gpu-buffer" && X > 0) {
|
|
1593
|
-
let R = s.webgpuGetBuffer;
|
|
1594
|
-
if (!R) throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');
|
|
1595
|
-
let O = R(D), q = ue(Ee, X);
|
|
1596
|
-
if (q === void 0 || !bt($)) throw new Error(`Unsupported data type: ${$}`);
|
|
1597
|
-
V = !0;
|
|
1598
|
-
{
|
|
1599
|
-
s.webgpuRegisterBuffer(O, e, D);
|
|
1600
|
-
let Pe = s.webgpuCreateDownloader(O, q, e);
|
|
1601
|
-
H.push([$, Z, { gpuBuffer: O, download: async () => {
|
|
1602
|
-
let De = await Pe();
|
|
1603
|
-
return new (Te($))(De);
|
|
1604
|
-
}, dispose: () => {
|
|
1605
|
-
s._OrtReleaseTensor(j) !== 0 && T("Can't release tensor.");
|
|
1606
|
-
} }, "gpu-buffer"]);
|
|
1607
|
-
}
|
|
1608
|
-
} else if (ce === "ml-tensor" && X > 0) {
|
|
1609
|
-
let R = s.webnnEnsureTensor, O = s.webnnIsGraphInputOutputTypeSupported;
|
|
1610
|
-
if (!R || !O) throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');
|
|
1611
|
-
if (ue(Ee, X) === void 0 || !vt($)) throw new Error(`Unsupported data type: ${$}`);
|
|
1612
|
-
if (!O(e, $, !1)) throw new Error(`preferredLocation "ml-tensor" for ${$} output is not supported by current WebNN Context.`);
|
|
1613
|
-
let q = await R(e, D, Ee, Z, !1);
|
|
1614
|
-
V = !0, H.push([$, Z, { mlTensor: q, download: s.webnnCreateMLTensorDownloader(D, $), dispose: () => {
|
|
1615
|
-
s.webnnReleaseTensorId(D), s._OrtReleaseTensor(j);
|
|
1616
|
-
} }, "ml-tensor"]);
|
|
1617
|
-
} else if (ce === "ml-tensor-cpu-output" && X > 0) {
|
|
1618
|
-
let R = s.webnnCreateMLTensorDownloader(D, $)(), O = H.length;
|
|
1619
|
-
V = !0, Lt.push((async () => {
|
|
1620
|
-
let q = [O, await R];
|
|
1621
|
-
return s.webnnReleaseTensorId(D), s._OrtReleaseTensor(j), q;
|
|
1622
|
-
})()), H.push([$, Z, [], "cpu"]);
|
|
1623
|
-
} else {
|
|
1624
|
-
let R = Te($), O = new R(X);
|
|
1625
|
-
new Uint8Array(O.buffer, O.byteOffset, O.byteLength).set(s.HEAPU8.subarray(D, D + O.byteLength)), H.push([$, Z, O, "cpu"]);
|
|
1626
|
-
}
|
|
1627
|
-
} finally {
|
|
1628
|
-
s.stackRestore(Se), $ === "string" && D && s._free(D), V || s._OrtReleaseTensor(j);
|
|
1629
|
-
}
|
|
1630
|
-
}
|
|
1631
|
-
d && !h && (s._OrtClearBoundOutputs(d.handle) !== 0 && T("Can't clear bound outputs."), Y.set(e, [l, f, p, d, h, !1]));
|
|
1632
|
-
for (let [g, j] of await Promise.all(Lt)) H[g][2] = j;
|
|
1633
|
-
return ae("wasm ProcessOutputTensor"), H;
|
|
1634
|
-
} finally {
|
|
1635
|
-
s.webnnOnRunEnd?.(l), s.stackRestore(k), r.forEach((L) => {
|
|
1636
|
-
L && L[3] === "gpu-buffer" && s.webgpuUnregisterBuffer(L[2].gpuBuffer);
|
|
1637
|
-
}), n.forEach((L) => {
|
|
1638
|
-
L && L[3] === "gpu-buffer" && s.webgpuUnregisterBuffer(L[2].gpuBuffer);
|
|
1639
|
-
}), A.forEach((L) => s._OrtReleaseTensor(L)), m.forEach((L) => s._OrtReleaseTensor(L)), E.forEach((L) => s._free(L)), y !== 0 && s._OrtReleaseRunOptions(y), c.forEach((L) => s._free(L));
|
|
1640
|
-
}
|
|
1641
|
-
}, Ut = (e) => {
|
|
1642
|
-
let t = I(), r = Y.get(e);
|
|
1643
|
-
if (!r) throw new Error("invalid session id");
|
|
1644
|
-
let a = r[0], n = t._OrtEndProfiling(a);
|
|
1645
|
-
n === 0 && T("Can't get an profile file name."), t._OrtFree(n);
|
|
1646
|
-
}, Ot = (e) => {
|
|
1647
|
-
let t = [];
|
|
1648
|
-
for (let r of e) {
|
|
1649
|
-
let a = r[2];
|
|
1650
|
-
!Array.isArray(a) && "buffer" in a && t.push(a.buffer);
|
|
1651
|
-
}
|
|
1652
|
-
return t;
|
|
1653
|
-
};
|
|
1654
|
-
}), J, F, ie, ge, be, Oe, at, Le, Q, ee, tr, Pr, Dr, _r, Gr, Wr, Fr, jr, Vr = b(() => {
|
|
1655
|
-
oe(), kr(), se(), yt(), J = () => !!U.wasm.proxy && typeof document < "u", ie = !1, ge = !1, be = !1, Le = /* @__PURE__ */ new Map(), Q = (e, t) => {
|
|
1656
|
-
let r = Le.get(e);
|
|
1657
|
-
r ? r.push(t) : Le.set(e, [t]);
|
|
1658
|
-
}, ee = () => {
|
|
1659
|
-
if (ie || !ge || be || !F) throw new Error("worker not ready");
|
|
1660
|
-
}, tr = (e) => {
|
|
1661
|
-
switch (e.data.type) {
|
|
1662
|
-
case "init-wasm":
|
|
1663
|
-
ie = !1, e.data.err ? (be = !0, at[1](e.data.err)) : (ge = !0, at[0]()), Oe && (URL.revokeObjectURL(Oe), Oe = void 0);
|
|
1664
|
-
break;
|
|
1665
|
-
case "init-ep":
|
|
1666
|
-
case "copy-from":
|
|
1667
|
-
case "create":
|
|
1668
|
-
case "release":
|
|
1669
|
-
case "run":
|
|
1670
|
-
case "end-profiling": {
|
|
1671
|
-
let t = Le.get(e.data.type);
|
|
1672
|
-
e.data.err ? t.shift()[1](e.data.err) : t.shift()[0](e.data.out);
|
|
1673
|
-
break;
|
|
1674
|
-
}
|
|
1675
|
-
}
|
|
1676
|
-
}, Pr = async () => {
|
|
1677
|
-
if (!ge) {
|
|
1678
|
-
if (ie) throw new Error("multiple calls to 'initWasm()' detected.");
|
|
1679
|
-
if (be) throw new Error("previous call to 'initWasm()' failed.");
|
|
1680
|
-
if (ie = !0, J()) return new Promise((e, t) => {
|
|
1681
|
-
F?.terminate(), Ar().then(([r, a]) => {
|
|
1682
|
-
try {
|
|
1683
|
-
F = a, F.onerror = (o) => t(o), F.onmessage = tr, at = [e, t];
|
|
1684
|
-
let n = { type: "init-wasm", in: U };
|
|
1685
|
-
if (!n.in.wasm.wasmPaths && r) {
|
|
1686
|
-
let o = wt();
|
|
1687
|
-
o && (n.in.wasm.wasmPaths = o);
|
|
1688
|
-
}
|
|
1689
|
-
F.postMessage(n), Oe = r;
|
|
1690
|
-
} catch (n) {
|
|
1691
|
-
t(n);
|
|
1692
|
-
}
|
|
1693
|
-
}, t);
|
|
1694
|
-
});
|
|
1695
|
-
try {
|
|
1696
|
-
await mt(U.wasm), await Et(U), ge = !0;
|
|
1697
|
-
} catch (e) {
|
|
1698
|
-
throw be = !0, e;
|
|
1699
|
-
} finally {
|
|
1700
|
-
ie = !1;
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
}, Dr = async (e) => {
|
|
1704
|
-
if (J()) return ee(), new Promise((t, r) => {
|
|
1705
|
-
Q("init-ep", [t, r]);
|
|
1706
|
-
let a = { type: "init-ep", in: { epName: e, env: U } };
|
|
1707
|
-
F.postMessage(a);
|
|
1708
|
-
});
|
|
1709
|
-
await It(U, e);
|
|
1710
|
-
}, _r = async (e) => J() ? (ee(), new Promise((t, r) => {
|
|
1711
|
-
Q("copy-from", [t, r]);
|
|
1712
|
-
let a = { type: "copy-from", in: { buffer: e } };
|
|
1713
|
-
F.postMessage(a, [e.buffer]);
|
|
1714
|
-
})) : Ne(e), Gr = async (e, t) => {
|
|
1715
|
-
if (J()) {
|
|
1716
|
-
if (t?.preferredOutputLocation) throw new Error('session option "preferredOutputLocation" is not supported for proxy.');
|
|
1717
|
-
return ee(), new Promise((r, a) => {
|
|
1718
|
-
Q("create", [r, a]);
|
|
1719
|
-
let n = { type: "create", in: { model: e, options: { ...t } } }, o = [];
|
|
1720
|
-
e instanceof Uint8Array && o.push(e.buffer), F.postMessage(n, o);
|
|
1721
|
-
});
|
|
1722
|
-
} else return Ct(e, t);
|
|
1723
|
-
}, Wr = async (e) => {
|
|
1724
|
-
if (J()) return ee(), new Promise((t, r) => {
|
|
1725
|
-
Q("release", [t, r]);
|
|
1726
|
-
let a = { type: "release", in: e };
|
|
1727
|
-
F.postMessage(a);
|
|
1728
|
-
});
|
|
1729
|
-
At(e);
|
|
1730
|
-
}, Fr = async (e, t, r, a, n, o) => {
|
|
1731
|
-
if (J()) {
|
|
1732
|
-
if (r.some((s) => s[3] !== "cpu")) throw new Error("input tensor on GPU is not supported for proxy.");
|
|
1733
|
-
if (n.some((s) => s)) throw new Error("pre-allocated output tensor is not supported for proxy.");
|
|
1734
|
-
return ee(), new Promise((s, i) => {
|
|
1735
|
-
Q("run", [s, i]);
|
|
1736
|
-
let u = r, l = { type: "run", in: { sessionId: e, inputIndices: t, inputs: u, outputIndices: a, options: o } };
|
|
1737
|
-
F.postMessage(l, Ot(u));
|
|
1738
|
-
});
|
|
1739
|
-
} else return xt(e, t, r, a, n, o);
|
|
1740
|
-
}, jr = async (e) => {
|
|
1741
|
-
if (J()) return ee(), new Promise((t, r) => {
|
|
1742
|
-
Q("end-profiling", [t, r]);
|
|
1743
|
-
let a = { type: "end-profiling", in: e };
|
|
1744
|
-
F.postMessage(a);
|
|
1745
|
-
});
|
|
1746
|
-
Ut(e);
|
|
1747
|
-
};
|
|
1748
|
-
}), ot, rr, zr, vn = b(() => {
|
|
1749
|
-
oe(), Vr(), de(), ct(), Lr(), ot = (e, t) => {
|
|
1750
|
-
switch (e.location) {
|
|
1751
|
-
case "cpu":
|
|
1752
|
-
return [e.type, e.dims, e.data, "cpu"];
|
|
1753
|
-
case "gpu-buffer":
|
|
1754
|
-
return [e.type, e.dims, { gpuBuffer: e.gpuBuffer }, "gpu-buffer"];
|
|
1755
|
-
case "ml-tensor":
|
|
1756
|
-
return [e.type, e.dims, { mlTensor: e.mlTensor }, "ml-tensor"];
|
|
1757
|
-
default:
|
|
1758
|
-
throw new Error(`invalid data location: ${e.location} for ${t()}`);
|
|
1759
|
-
}
|
|
1760
|
-
}, rr = (e) => {
|
|
1761
|
-
switch (e[3]) {
|
|
1762
|
-
case "cpu":
|
|
1763
|
-
return new z(e[0], e[2], e[1]);
|
|
1764
|
-
case "gpu-buffer": {
|
|
1765
|
-
let t = e[0];
|
|
1766
|
-
if (!bt(t)) throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);
|
|
1767
|
-
let { gpuBuffer: r, download: a, dispose: n } = e[2];
|
|
1768
|
-
return z.fromGpuBuffer(r, { dataType: t, dims: e[1], download: a, dispose: n });
|
|
1769
|
-
}
|
|
1770
|
-
case "ml-tensor": {
|
|
1771
|
-
let t = e[0];
|
|
1772
|
-
if (!vt(t)) throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);
|
|
1773
|
-
let { mlTensor: r, download: a, dispose: n } = e[2];
|
|
1774
|
-
return z.fromMLTensor(r, { dataType: t, dims: e[1], download: a, dispose: n });
|
|
1775
|
-
}
|
|
1776
|
-
default:
|
|
1777
|
-
throw new Error(`invalid data location: ${e[3]}`);
|
|
1778
|
-
}
|
|
1779
|
-
}, zr = class {
|
|
1780
|
-
async fetchModelAndCopyToWasmMemory(e) {
|
|
1781
|
-
return _r(await Tt(e));
|
|
1782
|
-
}
|
|
1783
|
-
async loadModel(e, t) {
|
|
1784
|
-
fe();
|
|
1785
|
-
let r;
|
|
1786
|
-
typeof e == "string" ? r = await this.fetchModelAndCopyToWasmMemory(e) : r = e, [this.sessionId, this.inputNames, this.outputNames, this.inputMetadata, this.outputMetadata] = await Gr(r, t), pe();
|
|
1787
|
-
}
|
|
1788
|
-
async dispose() {
|
|
1789
|
-
return Wr(this.sessionId);
|
|
1790
|
-
}
|
|
1791
|
-
async run(e, t, r) {
|
|
1792
|
-
fe();
|
|
1793
|
-
let a = [], n = [];
|
|
1794
|
-
Object.entries(e).forEach((p) => {
|
|
1795
|
-
let d = p[0], h = p[1], w = this.inputNames.indexOf(d);
|
|
1796
|
-
if (w === -1) throw new Error(`invalid input '${d}'`);
|
|
1797
|
-
a.push(h), n.push(w);
|
|
1798
|
-
});
|
|
1799
|
-
let o = [], s = [];
|
|
1800
|
-
Object.entries(t).forEach((p) => {
|
|
1801
|
-
let d = p[0], h = p[1], w = this.outputNames.indexOf(d);
|
|
1802
|
-
if (w === -1) throw new Error(`invalid output '${d}'`);
|
|
1803
|
-
o.push(h), s.push(w);
|
|
1804
|
-
});
|
|
1805
|
-
let i = a.map((p, d) => ot(p, () => `input "${this.inputNames[n[d]]}"`)), u = o.map((p, d) => p ? ot(p, () => `output "${this.outputNames[s[d]]}"`) : null), l = await Fr(this.sessionId, n, i, s, u, r), f = {};
|
|
1806
|
-
for (let p = 0; p < l.length; p++) f[this.outputNames[s[p]]] = o[p] ?? rr(l[p]);
|
|
1807
|
-
return pe(), f;
|
|
1808
|
-
}
|
|
1809
|
-
startProfiling() {
|
|
1810
|
-
}
|
|
1811
|
-
endProfiling() {
|
|
1812
|
-
jr(this.sessionId);
|
|
1813
|
-
}
|
|
1814
|
-
};
|
|
1815
|
-
}), Hr = {};
|
|
1816
|
-
Be(Hr, { OnnxruntimeWebAssemblyBackend: () => ft, initializeFlags: () => lt, wasmBackend: () => qr });
|
|
1817
|
-
var lt, ft, qr, Tn = b(() => {
|
|
1818
|
-
oe(), Vr(), vn(), lt = () => {
|
|
1819
|
-
(typeof U.wasm.initTimeout != "number" || U.wasm.initTimeout < 0) && (U.wasm.initTimeout = 0);
|
|
1820
|
-
let e = U.wasm.simd;
|
|
1821
|
-
if (typeof e != "boolean" && e !== void 0 && e !== "fixed" && e !== "relaxed" && (console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`), U.wasm.simd = !1), typeof U.wasm.proxy != "boolean" && (U.wasm.proxy = !1), typeof U.wasm.trace != "boolean" && (U.wasm.trace = !1), typeof U.wasm.numThreads != "number" || !Number.isInteger(U.wasm.numThreads) || U.wasm.numThreads <= 0) if (typeof self < "u" && !self.crossOriginIsolated) U.wasm.numThreads = 1;
|
|
1822
|
-
else {
|
|
1823
|
-
let t = typeof navigator > "u" ? Jr("node:os").cpus().length : navigator.hardwareConcurrency;
|
|
1824
|
-
U.wasm.numThreads = Math.min(4, Math.ceil((t || 1) / 2));
|
|
1825
|
-
}
|
|
1826
|
-
}, ft = class {
|
|
1827
|
-
async init(e) {
|
|
1828
|
-
lt(), await Pr(), await Dr(e);
|
|
1829
|
-
}
|
|
1830
|
-
async createInferenceSessionHandler(e, t) {
|
|
1831
|
-
let r = new zr();
|
|
1832
|
-
return await r.loadModel(e, t), r;
|
|
1833
|
-
}
|
|
1834
|
-
}, qr = new ft();
|
|
1835
|
-
});
|
|
1836
|
-
oe();
|
|
1837
|
-
oe();
|
|
1838
|
-
oe();
|
|
1839
|
-
var En = "1.23.2", In = Er;
|
|
1840
|
-
{
|
|
1841
|
-
let e = (Tn(), dt(Hr)).wasmBackend;
|
|
1842
|
-
le("webgpu", e, 5), le("webnn", e, 5), le("cpu", e, 10), le("wasm", e, 10);
|
|
1843
|
-
}
|
|
1844
|
-
Object.defineProperty(U.versions, "web", { value: En, enumerable: !0 });
|
|
1845
|
-
export {
|
|
1846
|
-
Tr as InferenceSession,
|
|
1847
|
-
st as TRACE,
|
|
1848
|
-
ne as TRACE_EVENT_BEGIN,
|
|
1849
|
-
ae as TRACE_EVENT_END,
|
|
1850
|
-
fe as TRACE_FUNC_BEGIN,
|
|
1851
|
-
pe as TRACE_FUNC_END,
|
|
1852
|
-
z as Tensor,
|
|
1853
|
-
In as default,
|
|
1854
|
-
U as env,
|
|
1855
|
-
le as registerBackend
|
|
1856
|
-
};
|