@flaxia/node 0.3.7 → 0.5.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 +43 -19
- package/dist/__tests__/consent-policy.test.d.ts +2 -0
- package/dist/__tests__/consent-policy.test.d.ts.map +1 -0
- package/dist/assets/VectorStoreEngine.js +140 -85
- package/dist/assets/ai-inference.js +336 -40
- package/dist/assets/container.js +368 -260
- package/dist/assets/egress-guard.js +174 -0
- package/dist/assets/image-process.js +184 -19
- package/dist/assets/nudenet.js +2 -2
- package/dist/assets/webgpuProbe.js +357 -0
- package/dist/client/SignalingClient.d.ts +6 -0
- package/dist/client/SignalingClient.d.ts.map +1 -1
- package/dist/client/__tests__/mobile-lifecycle.test.d.ts +2 -0
- package/dist/client/__tests__/mobile-lifecycle.test.d.ts.map +1 -0
- package/dist/client/mobile-lifecycle.d.ts +20 -0
- package/dist/client/mobile-lifecycle.d.ts.map +1 -0
- package/dist/consent/ConsentUI.d.ts +44 -5
- package/dist/consent/ConsentUI.d.ts.map +1 -1
- package/dist/consent/notice.d.ts +29 -0
- package/dist/consent/notice.d.ts.map +1 -0
- package/dist/consent/storage.d.ts +91 -7
- package/dist/consent/storage.d.ts.map +1 -1
- package/dist/executor/WorkerPool.d.ts +42 -2
- package/dist/executor/WorkerPool.d.ts.map +1 -1
- package/dist/executor/__tests__/WorkerPool.swarm.test.d.ts +13 -0
- package/dist/executor/__tests__/WorkerPool.swarm.test.d.ts.map +1 -0
- package/dist/executor/__tests__/container-executor.test.d.ts +2 -0
- package/dist/executor/__tests__/container-executor.test.d.ts.map +1 -0
- package/dist/executor/__tests__/egress-guard.test.d.ts +2 -0
- package/dist/executor/__tests__/egress-guard.test.d.ts.map +1 -0
- package/dist/executor/__tests__/resourceReporter.test.d.ts +2 -0
- package/dist/executor/__tests__/resourceReporter.test.d.ts.map +1 -0
- package/dist/executor/__tests__/webgpuProbe.test.d.ts +2 -0
- package/dist/executor/__tests__/webgpuProbe.test.d.ts.map +1 -0
- package/dist/executor/container-executor.d.ts +49 -1
- package/dist/executor/container-executor.d.ts.map +1 -1
- package/dist/executor/egress-guard.d.ts +105 -0
- package/dist/executor/egress-guard.d.ts.map +1 -0
- package/dist/executor/memoryProbe.d.ts +30 -0
- package/dist/executor/memoryProbe.d.ts.map +1 -1
- package/dist/executor/resourceReporter.d.ts +15 -0
- package/dist/executor/resourceReporter.d.ts.map +1 -0
- package/dist/executor/throttle.d.ts +62 -4
- package/dist/executor/throttle.d.ts.map +1 -1
- package/dist/executor/webgpuProbe.d.ts +20 -0
- package/dist/executor/webgpuProbe.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1423 -235
- package/dist/swarm/__tests__/adapter.test.d.ts +2 -0
- package/dist/swarm/__tests__/adapter.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/controller.test.d.ts +2 -0
- package/dist/swarm/__tests__/controller.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/defects.test.d.ts +16 -0
- package/dist/swarm/__tests__/defects.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/hardening.test.d.ts +2 -0
- package/dist/swarm/__tests__/hardening.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/peer-quality.test.d.ts +2 -0
- package/dist/swarm/__tests__/peer-quality.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/peer-transport.test.d.ts +2 -0
- package/dist/swarm/__tests__/peer-transport.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/runtime.test.d.ts +2 -0
- package/dist/swarm/__tests__/runtime.test.d.ts.map +1 -0
- package/dist/swarm/__tests__/session.test.d.ts +2 -0
- package/dist/swarm/__tests__/session.test.d.ts.map +1 -0
- package/dist/swarm/adapter.d.ts +115 -0
- package/dist/swarm/adapter.d.ts.map +1 -0
- package/dist/swarm/controller.d.ts +91 -0
- package/dist/swarm/controller.d.ts.map +1 -0
- package/dist/swarm/messages.d.ts +41 -0
- package/dist/swarm/messages.d.ts.map +1 -0
- package/dist/swarm/peer-quality.d.ts +14 -0
- package/dist/swarm/peer-quality.d.ts.map +1 -0
- package/dist/swarm/peer-transport.d.ts +81 -0
- package/dist/swarm/peer-transport.d.ts.map +1 -0
- package/dist/swarm/runtime.d.ts +20 -0
- package/dist/swarm/runtime.d.ts.map +1 -0
- package/dist/swarm/session.d.ts +73 -0
- package/dist/swarm/session.d.ts.map +1 -0
- package/dist/swarm-engine.js +7496 -0
- package/dist/vector-store/HNSWIndex.d.ts +30 -0
- package/dist/vector-store/HNSWIndex.d.ts.map +1 -1
- package/dist/vector-store/VectorStoreEngine.d.ts +43 -0
- package/dist/vector-store/VectorStoreEngine.d.ts.map +1 -1
- package/dist/vector-store/__tests__/HNSWIndex.test.d.ts +2 -0
- package/dist/vector-store/__tests__/HNSWIndex.test.d.ts.map +1 -0
- package/dist/vector-store/__tests__/VectorStoreEngine.test.d.ts +2 -0
- package/dist/vector-store/__tests__/VectorStoreEngine.test.d.ts.map +1 -0
- package/dist/worker/__tests__/main.worker.abort.test.d.ts +2 -0
- package/dist/worker/__tests__/main.worker.abort.test.d.ts.map +1 -0
- package/dist/worker/__tests__/main.worker.control.test.d.ts +2 -0
- package/dist/worker/__tests__/main.worker.control.test.d.ts.map +1 -0
- package/dist/worker.js +766 -67
- package/dist/workloads/__tests__/model-pipeline-cache.test.d.ts +2 -0
- package/dist/workloads/__tests__/model-pipeline-cache.test.d.ts.map +1 -0
- package/dist/workloads/__tests__/nudenet.test.d.ts +2 -0
- package/dist/workloads/__tests__/nudenet.test.d.ts.map +1 -0
- package/dist/workloads/ai-inference.d.ts +8 -1
- package/dist/workloads/ai-inference.d.ts.map +1 -1
- package/dist/workloads/ai-model-registry.d.ts +72 -0
- package/dist/workloads/ai-model-registry.d.ts.map +1 -0
- package/dist/workloads/container.d.ts +1 -1
- package/dist/workloads/container.d.ts.map +1 -1
- package/dist/workloads/image-process.d.ts +27 -0
- package/dist/workloads/image-process.d.ts.map +1 -1
- package/dist/workloads/model-pipeline-cache.d.ts +48 -0
- package/dist/workloads/model-pipeline-cache.d.ts.map +1 -0
- package/dist/workloads/nudenet.d.ts +8 -0
- package/dist/workloads/nudenet.d.ts.map +1 -1
- package/package.json +3 -2
- package/dist/assets/memoryProbe.js +0 -28
- package/dist/client/WebRTCPeer.d.ts +0 -8
- package/dist/client/WebRTCPeer.d.ts.map +0 -1
|
@@ -1,6 +1,238 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { j as e } from "./webgpuProbe.js";
|
|
2
|
+
import { TextStreamer as t, env as n, pipeline as r } from "./transformers.web.js";
|
|
3
|
+
//#region src/workloads/ai-model-registry.ts
|
|
4
|
+
var i = 512 * 1024 * 1024, a = 4 * 1024 ** 3, o = new Map(Object.entries({
|
|
5
|
+
"Xenova/bert-base-multilingual-uncased-sentiment": {
|
|
6
|
+
repo: "Xenova/bert-base-multilingual-uncased-sentiment",
|
|
7
|
+
revision: "main",
|
|
8
|
+
maxBytes: i
|
|
9
|
+
},
|
|
10
|
+
"onnx-community/Qwen3-Embedding-0.6B-ONNX": {
|
|
11
|
+
repo: "onnx-community/Qwen3-Embedding-0.6B-ONNX",
|
|
12
|
+
revision: "main",
|
|
13
|
+
maxBytes: i
|
|
14
|
+
}
|
|
15
|
+
}).map(([e, t]) => [e, { ...t }])), s = [
|
|
16
|
+
"wasm",
|
|
17
|
+
"cpu",
|
|
18
|
+
"webgpu"
|
|
19
|
+
], c = [
|
|
20
|
+
"q4",
|
|
21
|
+
"q4f16",
|
|
22
|
+
"q8",
|
|
23
|
+
"int8",
|
|
24
|
+
"uint8"
|
|
25
|
+
], l = {
|
|
26
|
+
max_new_tokens: {
|
|
27
|
+
min: 1,
|
|
28
|
+
max: 1024
|
|
29
|
+
},
|
|
30
|
+
temperature: {
|
|
31
|
+
min: 0,
|
|
32
|
+
max: 4
|
|
33
|
+
},
|
|
34
|
+
top_p: {
|
|
35
|
+
min: 0,
|
|
36
|
+
max: 1
|
|
37
|
+
},
|
|
38
|
+
top_k: {
|
|
39
|
+
min: 0,
|
|
40
|
+
max: 1e3
|
|
41
|
+
},
|
|
42
|
+
repetition_penalty: {
|
|
43
|
+
min: 0,
|
|
44
|
+
max: 4
|
|
45
|
+
}
|
|
46
|
+
}, u = 1e5;
|
|
47
|
+
function d() {
|
|
48
|
+
return [...o.keys()];
|
|
49
|
+
}
|
|
50
|
+
function f(e) {
|
|
51
|
+
if (typeof e != "string" || !e) throw Error(`ai-inference: model must be a non-empty string, got ${JSON.stringify(e)}`);
|
|
52
|
+
let t = o.get(e);
|
|
53
|
+
if (!t) throw Error(`ai-inference: model not allowed: ${e} (allowed: ${d().join(", ") || "none"})`);
|
|
54
|
+
return t;
|
|
55
|
+
}
|
|
56
|
+
function p(e) {
|
|
57
|
+
if (e != null) {
|
|
58
|
+
if (typeof e != "string" || !s.includes(e)) throw Error(`ai-inference: device not allowed: ${JSON.stringify(e)} (allowed: ${s.join(", ")})`);
|
|
59
|
+
return e;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function m(e) {
|
|
63
|
+
if (e != null) {
|
|
64
|
+
if (typeof e != "string" || !c.includes(e)) throw Error(`ai-inference: dtype not allowed: ${JSON.stringify(e)} (allowed: ${c.join(", ")})`);
|
|
65
|
+
return e;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function h(e) {
|
|
69
|
+
if (typeof e == "string") {
|
|
70
|
+
if (e.length > 1e5) throw Error(`ai-inference: input exceeds ${u} characters`);
|
|
71
|
+
return e;
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(e)) {
|
|
74
|
+
if (e.length === 0 || e.length > 64) throw Error("ai-inference: input array must hold 1..64 items");
|
|
75
|
+
for (let t of e) {
|
|
76
|
+
if (typeof t != "string") throw Error("ai-inference: input array items must be strings");
|
|
77
|
+
if (t.length > 1e5) throw Error(`ai-inference: input exceeds ${u} characters`);
|
|
78
|
+
}
|
|
79
|
+
return e;
|
|
80
|
+
}
|
|
81
|
+
throw Error("ai-inference: input must be a string or an array of strings");
|
|
82
|
+
}
|
|
83
|
+
function g(e, t) {
|
|
84
|
+
for (let [n, r] of Object.entries(l)) {
|
|
85
|
+
let i = t[n];
|
|
86
|
+
if (i != null) {
|
|
87
|
+
if (typeof i != "number" || !Number.isFinite(i) || i < r.min || i > r.max) throw Error(`ai-inference: option ${n} must be a number within ${r.min}..${r.max}, got ${JSON.stringify(i)}`);
|
|
88
|
+
e[n] = i;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (t.do_sample !== void 0) {
|
|
92
|
+
if (typeof t.do_sample != "boolean") throw Error("ai-inference: option do_sample must be a boolean");
|
|
93
|
+
e.do_sample = t.do_sample;
|
|
94
|
+
}
|
|
95
|
+
for (let n of ["src_lang", "tgt_lang"]) {
|
|
96
|
+
let r = t[n];
|
|
97
|
+
if (r != null) {
|
|
98
|
+
if (typeof r != "string" || !/^[a-z]{2,3}(_[A-Za-z0-9]{2,8})?$/.test(r)) throw Error(`ai-inference: option ${n} must be a language code like 'en_XX'`);
|
|
99
|
+
e[n] = r;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
var _ = class {
|
|
104
|
+
constructor(e = {}) {
|
|
105
|
+
this.options = e, this.entries = /* @__PURE__ */ new Map();
|
|
106
|
+
}
|
|
107
|
+
async acquire(t, n, r, i) {
|
|
108
|
+
let a = this.entries.get(t);
|
|
109
|
+
if (a?.state === "ready" && a.references === 0 && a.expiresAt <= this.now() && (await this.disposeEntry(a), a = this.entries.get(t)), a?.state === "failed" && (this.entries.delete(t), a = void 0), a || (await this.evictExpired(), a = this.entries.get(t), a)) return this.acquireEntry(a);
|
|
110
|
+
let o = Math.max(1, Math.floor(this.options.capacity ?? 2));
|
|
111
|
+
if (this.entries.size >= o && await this.makeRoom(), a = this.entries.get(t), a) return this.acquireEntry(a);
|
|
112
|
+
let s = this.now(), c = {
|
|
113
|
+
key: t,
|
|
114
|
+
descriptor: { ...n },
|
|
115
|
+
state: "loading",
|
|
116
|
+
references: 1,
|
|
117
|
+
updatedAt: s,
|
|
118
|
+
lastUsedAt: s,
|
|
119
|
+
expiresAt: s + this.ttlMs(),
|
|
120
|
+
promise: Promise.resolve(void 0),
|
|
121
|
+
dispose: i
|
|
122
|
+
};
|
|
123
|
+
this.entries.set(t, c), c.promise = (async () => {
|
|
124
|
+
try {
|
|
125
|
+
let e = await r();
|
|
126
|
+
return c.value = e.value, c.descriptor = {
|
|
127
|
+
...c.descriptor,
|
|
128
|
+
...e.descriptor
|
|
129
|
+
}, c.state === "loading" && (c.state = "ready"), c.updatedAt = this.now(), c.expiresAt = this.now() + this.ttlMs(), e.value;
|
|
130
|
+
} catch (t) {
|
|
131
|
+
throw c.state = "failed", c.updatedAt = this.now(), c.expiresAt = this.now() + Math.min(this.ttlMs(), e), t;
|
|
132
|
+
}
|
|
133
|
+
})();
|
|
134
|
+
try {
|
|
135
|
+
let e = await c.promise;
|
|
136
|
+
return this.createLease(c, e);
|
|
137
|
+
} catch (e) {
|
|
138
|
+
throw c.references = Math.max(0, c.references - 1), e;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async acquireEntry(e) {
|
|
142
|
+
if (e.state === "failed") throw this.entries.delete(e.key), Error("model cache entry failed before it could be acquired");
|
|
143
|
+
if (e.state === "evicting") throw Error("model cache entry is being evicted");
|
|
144
|
+
e.references++, e.lastUsedAt = this.now(), e.expiresAt = this.now() + this.ttlMs();
|
|
145
|
+
try {
|
|
146
|
+
let t = await e.promise;
|
|
147
|
+
return this.createLease(e, t);
|
|
148
|
+
} catch (t) {
|
|
149
|
+
throw e.references = Math.max(0, e.references - 1), t;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
getSnapshot(t = this.now()) {
|
|
153
|
+
return {
|
|
154
|
+
version: 1,
|
|
155
|
+
sampledAt: t,
|
|
156
|
+
expiresAt: t + e,
|
|
157
|
+
entries: [...this.entries.values()].slice(0, 8).map((e) => ({
|
|
158
|
+
...e.descriptor,
|
|
159
|
+
state: e.state,
|
|
160
|
+
referenceCount: e.references
|
|
161
|
+
}))
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
async releaseAll() {
|
|
165
|
+
let e = [];
|
|
166
|
+
for (let t of this.entries.values()) {
|
|
167
|
+
if (t.state === "failed") {
|
|
168
|
+
this.entries.delete(t.key);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
t.state = "evicting", t.updatedAt = this.now(), t.references === 0 && t.value !== void 0 && e.push(this.disposeEntry(t));
|
|
172
|
+
}
|
|
173
|
+
await Promise.all(e);
|
|
174
|
+
}
|
|
175
|
+
createLease(e, t) {
|
|
176
|
+
let n = !1;
|
|
177
|
+
return {
|
|
178
|
+
value: t,
|
|
179
|
+
updateDescriptor: (t) => {
|
|
180
|
+
n || (e.descriptor = {
|
|
181
|
+
...e.descriptor,
|
|
182
|
+
...t
|
|
183
|
+
}, e.updatedAt = this.now());
|
|
184
|
+
},
|
|
185
|
+
invalidate: () => {
|
|
186
|
+
n || (e.state = "evicting", e.updatedAt = this.now());
|
|
187
|
+
},
|
|
188
|
+
release: async () => {
|
|
189
|
+
n || (n = !0, e.references = Math.max(0, e.references - 1), e.lastUsedAt = this.now(), e.updatedAt = this.now(), e.references === 0 && e.state === "evicting" ? await this.disposeEntry(e) : e.references === 0 && e.state === "ready" && (e.expiresAt = this.now() + this.ttlMs()));
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
async evictExpired() {
|
|
194
|
+
let e = this.now();
|
|
195
|
+
for (let t of [...this.entries.values()]) t.state === "failed" && t.expiresAt <= e ? this.entries.delete(t.key) : t.state === "ready" && t.references === 0 && t.expiresAt <= e && await this.disposeEntry(t);
|
|
196
|
+
}
|
|
197
|
+
async makeRoom() {
|
|
198
|
+
let e = Math.max(1, Math.floor(this.options.capacity ?? 2));
|
|
199
|
+
for (; this.entries.size >= e;) {
|
|
200
|
+
let e = [...this.entries.values()].filter((e) => e.references === 0 && (e.state === "failed" || e.state === "ready")).sort((e, t) => Number(t.state === "failed") - Number(e.state === "failed") || e.lastUsedAt - t.lastUsedAt)[0];
|
|
201
|
+
if (!e) throw Error("model cache capacity is occupied by active entries");
|
|
202
|
+
e.state === "failed" ? this.entries.delete(e.key) : await this.disposeEntry(e);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async disposeEntry(e) {
|
|
206
|
+
if (e.references > 0 || e.state === "loading") return;
|
|
207
|
+
e.state = "evicting", e.updatedAt = this.now();
|
|
208
|
+
let t = e.value;
|
|
209
|
+
e.value = void 0;
|
|
210
|
+
try {
|
|
211
|
+
t !== void 0 && await e.dispose(t);
|
|
212
|
+
} finally {
|
|
213
|
+
this.entries.get(e.key) === e && this.entries.delete(e.key);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
now() {
|
|
217
|
+
return this.options.now?.() ?? Date.now();
|
|
218
|
+
}
|
|
219
|
+
ttlMs() {
|
|
220
|
+
return Math.max(1, this.options.ttlMs ?? 18e5);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function v(e) {
|
|
224
|
+
return JSON.stringify([
|
|
225
|
+
e.task,
|
|
226
|
+
e.modelId,
|
|
227
|
+
e.revision,
|
|
228
|
+
e.dtype,
|
|
229
|
+
e.backend,
|
|
230
|
+
e.layerRange ?? null
|
|
231
|
+
]);
|
|
232
|
+
}
|
|
233
|
+
//#endregion
|
|
2
234
|
//#region src/workloads/ai-inference.ts
|
|
3
|
-
var
|
|
235
|
+
var y = [
|
|
4
236
|
"text-classification",
|
|
5
237
|
"token-classification",
|
|
6
238
|
"question-answering",
|
|
@@ -26,51 +258,115 @@ var r = [
|
|
|
26
258
|
"depth-estimation",
|
|
27
259
|
"feature-extraction",
|
|
28
260
|
"image-feature-extraction"
|
|
29
|
-
],
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
261
|
+
], b = new _(), x = () => {
|
|
262
|
+
b.releaseAll();
|
|
263
|
+
}, S = () => b.getSnapshot(), C = (e, t) => {
|
|
264
|
+
let n = "", r = null, i = Promise.resolve(), a = 0, o = 0, s, c = !1, l = !1, u = (t) => {
|
|
265
|
+
if (a >= 64 || o + t.length > 262144) throw c = !0, s = /* @__PURE__ */ Error("ai-inference token consumer is too slow; generation stopped at the bounded stream buffer"), s;
|
|
266
|
+
a++, o += t.length, i = i.then(() => {
|
|
267
|
+
if (!c) return e(t);
|
|
268
|
+
}).then(() => void 0, (e) => {
|
|
269
|
+
c = !0, s = e;
|
|
270
|
+
}).finally(() => {
|
|
271
|
+
a--, o -= t.length;
|
|
272
|
+
});
|
|
273
|
+
}, d = () => {
|
|
274
|
+
r !== null && clearTimeout(r), r = null;
|
|
275
|
+
let e = n;
|
|
276
|
+
n = "", e && !c && u(e);
|
|
277
|
+
}, f = (e) => {
|
|
278
|
+
if (!l) {
|
|
279
|
+
if (c) throw s;
|
|
280
|
+
if (!t.tokenBuffer) {
|
|
281
|
+
u(e);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (n.length + e.length > 262144) throw c = !0, s = /* @__PURE__ */ Error("ai-inference token consumer is too slow; generation stopped at the bounded stream buffer"), s;
|
|
285
|
+
n += e, r === null && (r = setTimeout(() => {
|
|
286
|
+
try {
|
|
287
|
+
d();
|
|
288
|
+
} catch (e) {
|
|
289
|
+
c = !0, s = e;
|
|
290
|
+
}
|
|
291
|
+
}, t.tokenBufferIntervalMs ?? 50));
|
|
292
|
+
}
|
|
293
|
+
}, p = async (e) => {
|
|
294
|
+
if (l = !0, r !== null && clearTimeout(r), r = null, e && !c ? d() : n = "", await i, c) throw s;
|
|
35
295
|
};
|
|
36
|
-
return
|
|
37
|
-
|
|
296
|
+
return {
|
|
297
|
+
callback: f,
|
|
298
|
+
finish: () => p(!0),
|
|
299
|
+
cancel: () => p(!1)
|
|
38
300
|
};
|
|
39
301
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
302
|
+
function w() {
|
|
303
|
+
let e = globalThis.fetch.bind(globalThis);
|
|
304
|
+
n.fetch = async (t, n) => {
|
|
305
|
+
let r = new URL(String(t));
|
|
306
|
+
if (!/(^|\.)huggingface\.co$/.test(r.hostname) && !/(^|\.)hf\.co$/.test(r.hostname)) throw Error(`ai-inference: model download blocked from ${r.hostname}`);
|
|
307
|
+
let i = await e(r, n), o = Number(i.headers.get("content-length"));
|
|
308
|
+
if (Number.isFinite(o) && o > a) throw Error(`ai-inference: model file too large (${o} bytes > ${a})`);
|
|
309
|
+
return i;
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
w(), n.backends?.onnx?.wasm && (n.backends.onnx.wasm.numThreads = 1, n.backends.onnx.wasm.proxy = !1);
|
|
313
|
+
var T = async (e, n) => {
|
|
314
|
+
let { task: i, model: a, input: o, options: s = {} } = e;
|
|
315
|
+
if (!y.includes(i)) throw Error(`Invalid or unsupported task: ${i}. Supported tasks are: ${y.join(", ")}`);
|
|
316
|
+
let c = f(a), l = p(s.device), u = m(s.dtype), d = h(o), _ = {};
|
|
317
|
+
g(_, s);
|
|
318
|
+
let x = u ?? "q4f16", S = l ?? "wasm", w = v({
|
|
319
|
+
task: i,
|
|
320
|
+
modelId: a,
|
|
321
|
+
revision: c.revision,
|
|
322
|
+
dtype: x,
|
|
323
|
+
backend: S
|
|
324
|
+
}), T = performance.now(), E = await b.acquire(w, {
|
|
325
|
+
modelId: a,
|
|
326
|
+
revision: c.revision,
|
|
327
|
+
dtype: x,
|
|
328
|
+
backend: S
|
|
329
|
+
}, async () => {
|
|
330
|
+
let e = {
|
|
331
|
+
dtype: x,
|
|
332
|
+
device: l,
|
|
333
|
+
revision: c.revision
|
|
334
|
+
};
|
|
335
|
+
console.log(`[flaxia-node] ai-inference: loading pipeline task=${i} model=${c.repo} revision=${c.revision} dtype=${e.dtype} device=${l ?? "wasm"}`);
|
|
336
|
+
let t;
|
|
337
|
+
try {
|
|
338
|
+
t = await r(i, c.repo, e);
|
|
339
|
+
} catch (n) {
|
|
340
|
+
if (l && l !== "wasm") console.warn(`[AiInference] ${l} failed, falling back to wasm:`, n), e.device = "wasm", t = await r(i, c.repo, e);
|
|
341
|
+
else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${i} model=${c.repo} error=${n instanceof Error ? n.message : String(n)}`), n;
|
|
49
342
|
}
|
|
343
|
+
return console.log(`[flaxia-node] ai-inference: pipeline ready task=${i} model=${c.repo} loadMs=${Math.round(performance.now() - T)}`), {
|
|
344
|
+
value: t,
|
|
345
|
+
descriptor: { backend: e.device ?? "wasm" }
|
|
346
|
+
};
|
|
347
|
+
}, (e) => e.dispose?.()), D = E.value;
|
|
348
|
+
try {
|
|
50
349
|
let e = {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
350
|
+
max_new_tokens: _.max_new_tokens ?? 128,
|
|
351
|
+
do_sample: _.do_sample ?? !1
|
|
352
|
+
};
|
|
353
|
+
for (let [t, n] of Object.entries(_)) t === "max_new_tokens" || t === "do_sample" || (e[t] = n);
|
|
354
|
+
let r = n && D.tokenizer ? C(n, s) : null;
|
|
355
|
+
r && (e.streamer = new t(D.tokenizer, {
|
|
356
|
+
skip_prompt: !0,
|
|
357
|
+
skip_special_tokens: !0,
|
|
358
|
+
callback_function: r.callback
|
|
359
|
+
}));
|
|
360
|
+
let a = performance.now(), o;
|
|
55
361
|
try {
|
|
56
|
-
|
|
57
|
-
} catch (
|
|
58
|
-
|
|
59
|
-
else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${c} model=${l} error=${t instanceof Error ? t.message : String(t)}`), t;
|
|
362
|
+
o = await D(d, e);
|
|
363
|
+
} catch (e) {
|
|
364
|
+
throw /out of memory|oom|allocation failed|could not allocate/i.test(e instanceof Error ? e.message : String(e)) && E.invalidate(), r && await r.cancel(), e;
|
|
60
365
|
}
|
|
61
|
-
console.log(`[flaxia-node] ai-inference:
|
|
366
|
+
return await r?.finish(), console.log(`[flaxia-node] ai-inference: done task=${i} model=${c.repo} execMs=${Math.round(performance.now() - a)} inputLen=${String(d).length}`), { output: o };
|
|
367
|
+
} finally {
|
|
368
|
+
await E.release();
|
|
62
369
|
}
|
|
63
|
-
let m = {
|
|
64
|
-
max_new_tokens: d.max_new_tokens ?? 128,
|
|
65
|
-
do_sample: d.do_sample ?? !1
|
|
66
|
-
};
|
|
67
|
-
d.temperature != null && (m.temperature = d.temperature), d.top_p != null && (m.top_p = d.top_p), d.top_k != null && (m.top_k = d.top_k), d.repetition_penalty != null && (m.repetition_penalty = d.repetition_penalty), d.src_lang != null && (m.src_lang = d.src_lang), d.tgt_lang != null && (m.tgt_lang = d.tgt_lang), o && p.tokenizer && (m.streamer = new e(p.tokenizer, {
|
|
68
|
-
skip_prompt: !0,
|
|
69
|
-
skip_special_tokens: !0,
|
|
70
|
-
callback_function: s(o, d)
|
|
71
|
-
}));
|
|
72
|
-
let h = performance.now(), g = await p(u, m);
|
|
73
|
-
return console.log(`[flaxia-node] ai-inference: done task=${c} model=${l} execMs=${Math.round(performance.now() - h)} inputLen=${String(u).length}`), { output: g };
|
|
74
370
|
};
|
|
75
371
|
//#endregion
|
|
76
|
-
export {
|
|
372
|
+
export { S as getModelCacheSnapshot, T as handleAiInference, x as releaseCache };
|