@flaxia/node 0.7.0 → 0.7.2
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/dist/assets/ai-inference.js +9 -9
- package/dist/assets/ai-model-registry.js +0 -5
- package/dist/assets/cc-extract.js +20 -20
- package/dist/assets/container.js +59 -43
- package/dist/assets/decision.js +43 -36
- package/dist/assets/embed-gemma.js +28 -20
- package/dist/assets/image-process.js +78 -70
- package/dist/assets/nudenet.js +2 -2
- package/dist/assets/{VectorStoreEngine.js → sharedEngine.js} +67 -41
- package/dist/assets/vector-embed.js +49 -36
- package/dist/assets/vector-query.js +4 -4
- package/dist/assets/vector-store.js +4 -4
- package/dist/assets/webgpuProbe.js +87 -47
- package/dist/client/SignalingClient.d.ts.map +1 -1
- package/dist/client/light-preloader.d.ts +16 -6
- package/dist/client/light-preloader.d.ts.map +1 -1
- package/dist/executor/WorkerPool.d.ts +6 -0
- package/dist/executor/WorkerPool.d.ts.map +1 -1
- package/dist/executor/__tests__/resultLimits.test.d.ts +2 -0
- package/dist/executor/__tests__/resultLimits.test.d.ts.map +1 -0
- package/dist/executor/container-executor.d.ts +15 -0
- package/dist/executor/container-executor.d.ts.map +1 -1
- package/dist/executor/memoryProbe.d.ts +25 -1
- package/dist/executor/memoryProbe.d.ts.map +1 -1
- package/dist/executor/resultLimits.d.ts +19 -0
- package/dist/executor/resultLimits.d.ts.map +1 -0
- package/dist/index.js +416 -396
- package/dist/vector-store/HNSWIndex.d.ts +4 -1
- package/dist/vector-store/HNSWIndex.d.ts.map +1 -1
- package/dist/vector-store/VectorStoreEngine.d.ts +7 -1
- package/dist/vector-store/VectorStoreEngine.d.ts.map +1 -1
- package/dist/vector-store/__tests__/sharedEngine.test.d.ts +2 -0
- package/dist/vector-store/__tests__/sharedEngine.test.d.ts.map +1 -0
- package/dist/vector-store/sharedEngine.d.ts +13 -0
- package/dist/vector-store/sharedEngine.d.ts.map +1 -0
- package/dist/worker.js +163 -160
- package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts +2 -0
- package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts.map +1 -0
- package/dist/workloads/ai-inference.d.ts +8 -7
- package/dist/workloads/ai-inference.d.ts.map +1 -1
- package/dist/workloads/ai-model-registry.d.ts.map +1 -1
- package/dist/workloads/decision.d.ts +2 -0
- package/dist/workloads/decision.d.ts.map +1 -1
- package/dist/workloads/embed-gemma.d.ts.map +1 -1
- package/dist/workloads/image-process.d.ts +8 -0
- package/dist/workloads/image-process.d.ts.map +1 -1
- package/dist/workloads/nudenet.d.ts.map +1 -1
- package/dist/workloads/vector-embed.d.ts +6 -0
- package/dist/workloads/vector-embed.d.ts.map +1 -1
- package/dist/workloads/vector-query.d.ts.map +1 -1
- package/dist/workloads/vector-store.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/worker.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $ as e, C as t, H as n, N as r, Q as i, S as a, W as o, Z as s, c, d as l, g as u, l as d, o as f, r as p, s as m, t as h, v as g, w as _, x as ee, y as te } from "./assets/webgpuProbe.js";
|
|
2
2
|
//#region src/executor/throttle.ts
|
|
3
3
|
function v(e) {
|
|
4
4
|
return new Promise((t) => setTimeout(t, e));
|
|
5
5
|
}
|
|
6
|
-
var y = 3e4,
|
|
6
|
+
var y = 3e4, ne = 500, b = class extends Error {
|
|
7
7
|
constructor(e, t, n) {
|
|
8
8
|
super(`device is over its CPU budget (load ${e.toFixed(2)} > max ${t.toFixed(2)}) after waiting ${Math.round(n)}ms; task not started`), this.load = e, this.maxLoad = t, this.waitedMs = n, this.retryable = !0, this.name = "ThrottleBusyError";
|
|
9
9
|
}
|
|
10
|
-
},
|
|
10
|
+
}, re = class {
|
|
11
11
|
constructor(e = .15) {
|
|
12
12
|
this.windowStart = performance.now(), this.windowBusyMs = 0, this.lastLoad = 0, this.dutyStartedAt = null, this.maxLoad = Math.max(.05, Math.min(.3, e));
|
|
13
13
|
}
|
|
@@ -40,7 +40,7 @@ var y = 3e4, ee = 500, b = class extends Error {
|
|
|
40
40
|
for (; await this.shouldPause();) {
|
|
41
41
|
let n = performance.now() - t;
|
|
42
42
|
if (n >= e) throw new b(this.lastLoad, this.maxLoad, n);
|
|
43
|
-
await v(Math.min(
|
|
43
|
+
await v(Math.min(ne, Math.max(1, e - n)));
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
async yieldIfOverloaded() {
|
|
@@ -58,12 +58,12 @@ var y = 3e4, ee = 500, b = class extends Error {
|
|
|
58
58
|
return this.maxLoad;
|
|
59
59
|
}
|
|
60
60
|
}, x = 4096;
|
|
61
|
-
function
|
|
61
|
+
function ie(e) {
|
|
62
62
|
let t = e.maxSeq;
|
|
63
63
|
return typeof t == "number" && Number.isInteger(t) && t > 0 ? t : x;
|
|
64
64
|
}
|
|
65
65
|
function S(e, t, n) {
|
|
66
|
-
let { pos: r, tokens: i } = e.header, a =
|
|
66
|
+
let { pos: r, tokens: i } = e.header, a = ie(t);
|
|
67
67
|
if (!Number.isInteger(i) || i < 1) return {
|
|
68
68
|
kind: "corrupt",
|
|
69
69
|
reason: `carried ${i} token columns`
|
|
@@ -95,78 +95,78 @@ function S(e, t, n) {
|
|
|
95
95
|
hidden: o
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
|
-
function
|
|
98
|
+
function ae(e, t, n, r) {
|
|
99
99
|
let a = i(e);
|
|
100
100
|
return a ? a.header.pos !== t || a.header.requestId !== t ? { kind: "stale" } : S(a, n, r) : {
|
|
101
101
|
kind: "corrupt",
|
|
102
102
|
reason: "malformed frame header"
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
|
-
async function
|
|
106
|
-
let { engine:
|
|
107
|
-
if (
|
|
108
|
-
|
|
105
|
+
async function oe(t) {
|
|
106
|
+
let { engine: n, link: i, semantics: a, promptTokens: o, chainLength: c, maxNewTokens: l } = t;
|
|
107
|
+
if (o.length === 0) throw Error("swarm host requires at least one prompt token");
|
|
108
|
+
n.reset();
|
|
109
109
|
let u = null;
|
|
110
|
-
|
|
111
|
-
if (e
|
|
112
|
-
let
|
|
113
|
-
if (!
|
|
114
|
-
let
|
|
115
|
-
if (
|
|
116
|
-
if (u = null,
|
|
117
|
-
|
|
110
|
+
i.onFrame((e) => {
|
|
111
|
+
if (r(e)) return;
|
|
112
|
+
let t = u;
|
|
113
|
+
if (!t) return;
|
|
114
|
+
let i = ae(e, t.pos, n, a);
|
|
115
|
+
if (i.kind !== "stale") {
|
|
116
|
+
if (u = null, i.kind === "corrupt") {
|
|
117
|
+
t.reject(/* @__PURE__ */ Error(`swarm hop failed: ${i.reason}`));
|
|
118
118
|
return;
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
t.resolve(i.hidden);
|
|
121
121
|
}
|
|
122
122
|
});
|
|
123
|
-
let d = (e,
|
|
123
|
+
let d = (e, t) => c <= 1 ? Promise.resolve(e) : new Promise((n, r) => {
|
|
124
124
|
u = {
|
|
125
|
-
pos:
|
|
126
|
-
resolve:
|
|
127
|
-
reject:
|
|
128
|
-
},
|
|
129
|
-
requestId:
|
|
130
|
-
pos:
|
|
125
|
+
pos: t,
|
|
126
|
+
resolve: n,
|
|
127
|
+
reject: r
|
|
128
|
+
}, i.send(s({
|
|
129
|
+
requestId: t,
|
|
130
|
+
pos: t,
|
|
131
131
|
tokens: 1
|
|
132
|
-
},
|
|
133
|
-
}), f = async (e, t) => d(await
|
|
134
|
-
for (let e = 0; e <
|
|
135
|
-
let t = await f(
|
|
136
|
-
e ===
|
|
132
|
+
}, a.packHidden(e)));
|
|
133
|
+
}), f = async (e, t) => d(await n.embedRun(e, t), t), p = null;
|
|
134
|
+
for (let e = 0; e < o.length; e++) {
|
|
135
|
+
let t = await f(o[e], e);
|
|
136
|
+
e === o.length - 1 && (p = await n.headFromHidden(t));
|
|
137
137
|
}
|
|
138
138
|
if (!p) throw Error("swarm host produced no logits for the prompt");
|
|
139
|
-
let m = [], h =
|
|
140
|
-
for (; m.length < l && !(
|
|
139
|
+
let m = [], h = o.length, g = a.argmax(p);
|
|
140
|
+
for (; m.length < l && !(t.eosIds?.has(g) || (m.push(g), await t.onToken?.(g), m.length >= l));) {
|
|
141
141
|
let e = await f(g, h++);
|
|
142
|
-
p = await
|
|
142
|
+
p = await n.headFromHidden(e), g = a.argmax(p);
|
|
143
143
|
}
|
|
144
|
-
return c > 1 &&
|
|
144
|
+
return c > 1 && i.send(e()), { tokens: m };
|
|
145
145
|
}
|
|
146
|
-
function
|
|
147
|
-
let { engine:
|
|
148
|
-
return
|
|
146
|
+
function se(e) {
|
|
147
|
+
let { engine: t, link: n, semantics: a } = e;
|
|
148
|
+
return t.reset(), new Promise((e, o) => {
|
|
149
149
|
let c = Promise.resolve(), l = !1;
|
|
150
|
-
|
|
150
|
+
n.onFrame((u) => {
|
|
151
151
|
if (!l) {
|
|
152
|
-
if (
|
|
153
|
-
l = !0,
|
|
152
|
+
if (r(u)) {
|
|
153
|
+
l = !0, n.send(u), c = c.then(() => e());
|
|
154
154
|
return;
|
|
155
155
|
}
|
|
156
156
|
c = c.then(async () => {
|
|
157
157
|
if (l) return;
|
|
158
158
|
let e = i(u);
|
|
159
159
|
if (!e) throw Error("swarm frame has a malformed frame header");
|
|
160
|
-
let
|
|
161
|
-
if (
|
|
162
|
-
let
|
|
163
|
-
|
|
160
|
+
let r = S(e, t, a);
|
|
161
|
+
if (r.kind === "corrupt") throw Error(`swarm frame is unusable: ${r.reason}`);
|
|
162
|
+
let o = await t.runHidden(r.hidden, e.header.pos);
|
|
163
|
+
n.send(s({
|
|
164
164
|
requestId: e.header.requestId,
|
|
165
165
|
pos: e.header.pos,
|
|
166
166
|
tokens: e.header.tokens
|
|
167
|
-
},
|
|
167
|
+
}, a.packHidden(o)));
|
|
168
168
|
}).catch((e) => {
|
|
169
|
-
l = !0,
|
|
169
|
+
l = !0, o(e instanceof Error ? e : Error(String(e)));
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
172
|
});
|
|
@@ -174,7 +174,7 @@ function ie(n) {
|
|
|
174
174
|
}
|
|
175
175
|
//#endregion
|
|
176
176
|
//#region src/swarm/controller.ts
|
|
177
|
-
var
|
|
177
|
+
var ce = class {
|
|
178
178
|
constructor(e) {
|
|
179
179
|
this.options = e, this.frameHandler = null, this.engine = null, this.semantics = null, this.sessionId = "", this.model = "", this.chainLength = 1, this.chain = [], this.slice = null, this.hostWarm = void 0, this.prompt = "", this.maxNewTokens = 128, this.sliceLoaded = !1, this.generationStarted = !1, this.settled = !1, this.flowPaused = !1, this.flowWaiters = /* @__PURE__ */ new Set();
|
|
180
180
|
}
|
|
@@ -223,7 +223,7 @@ var ae = class {
|
|
|
223
223
|
if (!Number.isInteger(e.chainLength) || e.chainLength < 1 || e.chainLength > 16) throw Error(`swarm slice has an invalid chain length: ${JSON.stringify(e.chainLength)}`);
|
|
224
224
|
if (!Number.isInteger(e.index) || e.index < 0 || e.index >= e.chainLength) throw Error(`swarm slice has an invalid index: ${JSON.stringify(e.index)}`);
|
|
225
225
|
if (e.role !== (e.index === 0 ? "host" : "worker")) throw Error(`swarm slice role ${e.role} does not match index ${e.index}`);
|
|
226
|
-
if (!
|
|
226
|
+
if (!l(e.slice)) throw Error(`swarm slice is out of bounds: ${JSON.stringify(e.slice)}`);
|
|
227
227
|
let t = e.index === 0;
|
|
228
228
|
if (e.slice.hasEmbed !== t || e.slice.hasHead !== t) throw Error("swarm slice embed/head ownership does not match the host position");
|
|
229
229
|
}
|
|
@@ -238,11 +238,11 @@ var ae = class {
|
|
|
238
238
|
this.engine = t, this.semantics = n, this.hostWarm = r, this.sliceLoaded = !0, this.options.sendControl({
|
|
239
239
|
type: "swarm-ready",
|
|
240
240
|
sessionId: e.sessionId
|
|
241
|
-
}), e.role === "worker" && (this.generationStarted = !0,
|
|
241
|
+
}), e.role === "worker" && (this.generationStarted = !0, se({
|
|
242
242
|
engine: t,
|
|
243
243
|
link: this.link(),
|
|
244
244
|
semantics: n
|
|
245
|
-
}).then(() => this.finish(
|
|
245
|
+
}).then(() => this.finish(le())).catch((e) => this.fail(e)));
|
|
246
246
|
}
|
|
247
247
|
async startHost() {
|
|
248
248
|
if (this.generationStarted) return;
|
|
@@ -254,7 +254,7 @@ var ae = class {
|
|
|
254
254
|
let r = t.encodePrompt(this.prompt), i = e.maxSeq ?? 4096;
|
|
255
255
|
if (r.length + n > i) throw Error(`swarm request needs ${r.length + n} tokens but the model context holds ${i}`);
|
|
256
256
|
this.generationStarted = !0;
|
|
257
|
-
let a = performance.now(), { tokens: o } = await
|
|
257
|
+
let a = performance.now(), { tokens: o } = await oe({
|
|
258
258
|
chainLength: this.chainLength,
|
|
259
259
|
promptTokens: r,
|
|
260
260
|
maxNewTokens: n,
|
|
@@ -310,7 +310,7 @@ var ae = class {
|
|
|
310
310
|
this.flowWaiters.clear();
|
|
311
311
|
}
|
|
312
312
|
};
|
|
313
|
-
function
|
|
313
|
+
function le() {
|
|
314
314
|
return {
|
|
315
315
|
output: "",
|
|
316
316
|
tokens: [],
|
|
@@ -320,13 +320,13 @@ function oe() {
|
|
|
320
320
|
}
|
|
321
321
|
//#endregion
|
|
322
322
|
//#region src/swarm/adapter.ts
|
|
323
|
-
var
|
|
323
|
+
var C = class extends Error {
|
|
324
324
|
constructor(e) {
|
|
325
325
|
super(e), this.name = "SwarmEngineUnavailableError";
|
|
326
326
|
}
|
|
327
327
|
};
|
|
328
|
-
async function
|
|
329
|
-
if (!(await
|
|
328
|
+
async function ue(e, t) {
|
|
329
|
+
if (!(await h()).webgpu) throw new C("WebGPU is not available in this context");
|
|
330
330
|
let n = t ?? ((e) => import(
|
|
331
331
|
/* @vite-ignore */
|
|
332
332
|
e
|
|
@@ -334,13 +334,13 @@ async function T(e, t) {
|
|
|
334
334
|
try {
|
|
335
335
|
r = await n(e);
|
|
336
336
|
} catch (e) {
|
|
337
|
-
throw new
|
|
337
|
+
throw new C(`failed to load swarm engine module: ${e instanceof Error ? e.message : String(e)}`);
|
|
338
338
|
}
|
|
339
339
|
let i = r?.Qwen35Engine;
|
|
340
|
-
if (!i || typeof i.create != "function") throw new
|
|
340
|
+
if (!i || typeof i.create != "function") throw new C(`module at ${e} does not export Qwen35Engine.create`);
|
|
341
341
|
return r;
|
|
342
342
|
}
|
|
343
|
-
function
|
|
343
|
+
function de(e, t, n, r) {
|
|
344
344
|
let i = !1, a = async (e) => {
|
|
345
345
|
r?.();
|
|
346
346
|
let t = await e();
|
|
@@ -383,19 +383,19 @@ function se(e, t, n, r) {
|
|
|
383
383
|
}
|
|
384
384
|
//#endregion
|
|
385
385
|
//#region src/swarm/runtime.ts
|
|
386
|
-
var
|
|
386
|
+
var w = {
|
|
387
387
|
"qwen3.5-2b": "https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/Qwen3.5-2B-Q4_0.gguf",
|
|
388
388
|
"qwen3.8-27b": "https://huggingface.co/unsloth/Qwen3.8-27B-GGUF/resolve/main/Qwen3.8-27B-Q4_0.gguf",
|
|
389
389
|
"qwen3.6-35b-moe": "https://huggingface.co/bartowski/Qwen_Qwen3.6-35B-A3B-GGUF/resolve/main/Qwen_Qwen3.6-35B-A3B-Q4_0.gguf"
|
|
390
|
-
},
|
|
391
|
-
function
|
|
392
|
-
|
|
390
|
+
}, fe = "flaxia-swarm-weights-v1", pe = 12 * 1024 * 1024, T = 256 * 1024 * 1024, E = /* @__PURE__ */ new Map();
|
|
391
|
+
function D(e, t) {
|
|
392
|
+
E.set(e, {
|
|
393
393
|
status: t,
|
|
394
394
|
failedExpiresAt: t.state === "failed" ? Date.now() + 3e4 : void 0
|
|
395
395
|
});
|
|
396
396
|
}
|
|
397
|
-
function
|
|
398
|
-
let i =
|
|
397
|
+
function O(e, t, n, r) {
|
|
398
|
+
let i = A(e);
|
|
399
399
|
return {
|
|
400
400
|
modelId: e,
|
|
401
401
|
revision: /\/resolve\/([^/]+)\//.exec(new URL(i).pathname)?.[1] ?? "unknown",
|
|
@@ -406,11 +406,11 @@ function A(e, t, n, r) {
|
|
|
406
406
|
referenceCount: r
|
|
407
407
|
};
|
|
408
408
|
}
|
|
409
|
-
function
|
|
409
|
+
function k(e = Date.now()) {
|
|
410
410
|
let t = [];
|
|
411
|
-
for (let [n, r] of
|
|
411
|
+
for (let [n, r] of E) {
|
|
412
412
|
if (r.failedExpiresAt !== void 0 && r.failedExpiresAt <= e) {
|
|
413
|
-
|
|
413
|
+
E.delete(n);
|
|
414
414
|
continue;
|
|
415
415
|
}
|
|
416
416
|
t.push(r.status);
|
|
@@ -418,40 +418,40 @@ function j(e = Date.now()) {
|
|
|
418
418
|
return {
|
|
419
419
|
version: 1,
|
|
420
420
|
sampledAt: e,
|
|
421
|
-
expiresAt: e +
|
|
421
|
+
expiresAt: e + a,
|
|
422
422
|
entries: t.slice(0, 8)
|
|
423
423
|
};
|
|
424
424
|
}
|
|
425
|
-
function
|
|
426
|
-
let t = Object.prototype.hasOwnProperty.call(
|
|
425
|
+
function A(e) {
|
|
426
|
+
let t = Object.prototype.hasOwnProperty.call(w, e) ? w[e] : void 0;
|
|
427
427
|
if (t) return t;
|
|
428
|
-
throw new
|
|
428
|
+
throw new C(`unknown swarm model: ${e}`);
|
|
429
429
|
}
|
|
430
|
-
var
|
|
431
|
-
async function
|
|
432
|
-
if (
|
|
430
|
+
var j = null, M = 0, N = 0;
|
|
431
|
+
async function me() {
|
|
432
|
+
if (j !== null) return j;
|
|
433
433
|
try {
|
|
434
|
-
|
|
434
|
+
j = typeof caches < "u" ? await caches.open(fe) : !1;
|
|
435
435
|
} catch {
|
|
436
|
-
|
|
436
|
+
j = !1;
|
|
437
437
|
}
|
|
438
|
-
return
|
|
438
|
+
return j;
|
|
439
439
|
}
|
|
440
|
-
function
|
|
440
|
+
function he(e, t, n) {
|
|
441
441
|
return `https://flaxia-weights.local/${encodeURIComponent(e)}/${t}-${n}`;
|
|
442
442
|
}
|
|
443
|
-
async function
|
|
444
|
-
let r = await
|
|
443
|
+
async function P(e, t, n) {
|
|
444
|
+
let r = await F(e, t, n), i = new Uint8Array(await r.arrayBuffer()), a = n - t + 1;
|
|
445
445
|
if (i.byteLength !== a) throw Error(`short range ${i.byteLength}/${a}`);
|
|
446
446
|
return i;
|
|
447
447
|
}
|
|
448
|
-
async function
|
|
449
|
-
let r = n - t + 1, i = await
|
|
448
|
+
async function F(e, t, n) {
|
|
449
|
+
let r = n - t + 1, i = await me(), a = he(e, t, n);
|
|
450
450
|
if (i) try {
|
|
451
451
|
let e = await i.match(a);
|
|
452
452
|
if (e) {
|
|
453
453
|
let t = new Uint8Array(await e.arrayBuffer());
|
|
454
|
-
if (t.byteLength === r) return
|
|
454
|
+
if (t.byteLength === r) return M++, new Response(t, { status: 200 });
|
|
455
455
|
await i.delete(a).catch(() => {});
|
|
456
456
|
}
|
|
457
457
|
} catch {}
|
|
@@ -460,7 +460,7 @@ async function L(e, t, n) {
|
|
|
460
460
|
try {
|
|
461
461
|
let s = await fetch(e, { headers: { Range: `bytes=${t}-${n}` } });
|
|
462
462
|
if (s.status !== 206) o = /* @__PURE__ */ Error(`model host refused range requests (${s.status})`);
|
|
463
|
-
else return
|
|
463
|
+
else return N++, i && s.clone().arrayBuffer().then((e) => {
|
|
464
464
|
if (e.byteLength === r) return i.put(a, new Response(e, {
|
|
465
465
|
status: 200,
|
|
466
466
|
headers: { "content-type": "application/octet-stream" }
|
|
@@ -473,23 +473,23 @@ async function L(e, t, n) {
|
|
|
473
473
|
}
|
|
474
474
|
throw Error(`range fetch ${t}-${n} failed: ${o instanceof Error ? o.message : String(o)}`);
|
|
475
475
|
}
|
|
476
|
-
async function
|
|
477
|
-
let n =
|
|
476
|
+
async function I(e, t) {
|
|
477
|
+
let n = pe;
|
|
478
478
|
for (;;) {
|
|
479
|
-
let r = await
|
|
479
|
+
let r = await P(t, 0, n - 1), i = r.buffer.slice(r.byteOffset, r.byteOffset + r.byteLength);
|
|
480
480
|
try {
|
|
481
481
|
return e.parseGGUFHeader(i);
|
|
482
482
|
} catch (e) {
|
|
483
|
-
if (n >=
|
|
484
|
-
n = Math.min(n * 2,
|
|
483
|
+
if (n >= T) throw e;
|
|
484
|
+
n = Math.min(n * 2, T);
|
|
485
485
|
}
|
|
486
486
|
}
|
|
487
487
|
}
|
|
488
|
-
async function
|
|
488
|
+
async function L() {
|
|
489
489
|
let e = navigator.gpu;
|
|
490
|
-
if (!e) throw new
|
|
490
|
+
if (!e) throw new C("WebGPU is not available in this context");
|
|
491
491
|
let t = await e.requestAdapter({ powerPreference: "high-performance" });
|
|
492
|
-
if (!t) throw new
|
|
492
|
+
if (!t) throw new C("no WebGPU adapter");
|
|
493
493
|
return t.requestDevice({ requiredLimits: {
|
|
494
494
|
maxBufferSize: t.limits.maxBufferSize,
|
|
495
495
|
maxStorageBufferBindingSize: t.limits.maxStorageBufferBindingSize
|
|
@@ -525,36 +525,36 @@ function R(e, t) {
|
|
|
525
525
|
eosIds: new Set(c)
|
|
526
526
|
};
|
|
527
527
|
}
|
|
528
|
-
function
|
|
529
|
-
let t = null,
|
|
528
|
+
function ge(e) {
|
|
529
|
+
let t = null, n = null, r = () => {
|
|
530
530
|
if (!t) {
|
|
531
531
|
let n = new URL("./swarm-engine.js", "" + import.meta.url).href;
|
|
532
|
-
t = e ? e() :
|
|
532
|
+
t = e ? e() : ue(n).then((e) => e);
|
|
533
533
|
}
|
|
534
534
|
return t;
|
|
535
|
-
},
|
|
536
|
-
let t = await
|
|
537
|
-
(!
|
|
538
|
-
url:
|
|
539
|
-
gguf: await
|
|
535
|
+
}, i = async (e) => {
|
|
536
|
+
let t = await r(), i = A(e);
|
|
537
|
+
(!n || n.url !== i) && (n = {
|
|
538
|
+
url: i,
|
|
539
|
+
gguf: await I(t, i)
|
|
540
540
|
});
|
|
541
|
-
let a = Number(
|
|
542
|
-
if (!Number.isInteger(a) || a < 1) throw new
|
|
541
|
+
let a = Number(n.gguf.meta["qwen35.block_count"]) - Number(n.gguf.meta["qwen35.nextn_predict_layers"] || 0);
|
|
542
|
+
if (!Number.isInteger(a) || a < 1) throw new C(`model ${e} has no qwen35 trunk layers`);
|
|
543
543
|
return {
|
|
544
544
|
mod: t,
|
|
545
|
-
url:
|
|
546
|
-
gguf:
|
|
545
|
+
url: i,
|
|
546
|
+
gguf: n.gguf,
|
|
547
547
|
layers: a
|
|
548
548
|
};
|
|
549
549
|
};
|
|
550
550
|
return {
|
|
551
551
|
async plan(e, t) {
|
|
552
|
-
let { layers:
|
|
553
|
-
return
|
|
552
|
+
let { layers: n } = await i(t);
|
|
553
|
+
return o({
|
|
554
554
|
sessionId: "",
|
|
555
555
|
taskId: "",
|
|
556
556
|
model: t,
|
|
557
|
-
layers:
|
|
557
|
+
layers: n,
|
|
558
558
|
nodes: e.map((e) => ({
|
|
559
559
|
nodeId: e.nodeId,
|
|
560
560
|
capacity: e.capacity
|
|
@@ -562,17 +562,17 @@ function z(e) {
|
|
|
562
562
|
}).chain;
|
|
563
563
|
},
|
|
564
564
|
async load(e, t) {
|
|
565
|
-
|
|
565
|
+
M = 0, N = 0;
|
|
566
566
|
let n = JSON.stringify([
|
|
567
567
|
t,
|
|
568
568
|
e.start,
|
|
569
569
|
e.end
|
|
570
570
|
]);
|
|
571
|
-
|
|
572
|
-
let r,
|
|
571
|
+
D(n, O(t, e, "loading", 1));
|
|
572
|
+
let r, a = !1;
|
|
573
573
|
try {
|
|
574
|
-
let { mod: o, url: s, gguf: c } = await
|
|
575
|
-
r = await
|
|
574
|
+
let { mod: o, url: s, gguf: c } = await i(t);
|
|
575
|
+
r = await L();
|
|
576
576
|
let l = null, u = r;
|
|
577
577
|
u.addEventListener?.("uncapturederror", () => {
|
|
578
578
|
l = "swarm GPU validation failed";
|
|
@@ -580,9 +580,9 @@ function z(e) {
|
|
|
580
580
|
l = "swarm GPU device lost";
|
|
581
581
|
});
|
|
582
582
|
let d = () => {
|
|
583
|
-
if (l) throw new
|
|
584
|
-
}, f = R(o, o.makeTokenizer(o.tokenizerFromGGUF(c.meta))), p = (e) =>
|
|
585
|
-
c.streamEntry = (e) => o.streamEntryToGPU(r, e, (e) =>
|
|
583
|
+
if (l) throw new C(l);
|
|
584
|
+
}, f = R(o, o.makeTokenizer(o.tokenizerFromGGUF(c.meta))), p = (e) => P(s, e.byteOffset, e.byteOffset + e.byteLength - 1);
|
|
585
|
+
c.streamEntry = (e) => o.streamEntryToGPU(r, e, (e) => F(s, e.byteOffset, e.byteOffset + e.byteLength - 1), { staging: 4 * 1024 * 1024 });
|
|
586
586
|
let m = await o.qwen35Weights(c, p, {
|
|
587
587
|
lo: e.start,
|
|
588
588
|
hi: e.end,
|
|
@@ -601,36 +601,36 @@ function z(e) {
|
|
|
601
601
|
weights: m
|
|
602
602
|
});
|
|
603
603
|
d();
|
|
604
|
-
let _ =
|
|
605
|
-
|
|
604
|
+
let _ = de(g, r, () => {
|
|
605
|
+
D(n, O(t, e, "evicting", 0)), queueMicrotask(() => E.delete(n));
|
|
606
606
|
}, d);
|
|
607
|
-
return
|
|
607
|
+
return a = !0, D(n, O(t, e, "ready", 1)), {
|
|
608
608
|
engine: _,
|
|
609
609
|
semantics: f,
|
|
610
|
-
warm:
|
|
610
|
+
warm: M > 0 && N === 0
|
|
611
611
|
};
|
|
612
|
-
} catch (
|
|
613
|
-
if (
|
|
612
|
+
} catch (i) {
|
|
613
|
+
if (D(n, O(t, e, "failed", 0)), !a) try {
|
|
614
614
|
r?.destroy?.();
|
|
615
615
|
} catch {}
|
|
616
|
-
throw
|
|
616
|
+
throw i;
|
|
617
617
|
}
|
|
618
618
|
}
|
|
619
619
|
};
|
|
620
620
|
}
|
|
621
621
|
//#endregion
|
|
622
622
|
//#region src/swarm/messages.ts
|
|
623
|
-
function
|
|
624
|
-
return
|
|
623
|
+
function _e(e) {
|
|
624
|
+
return n(e) || ee(e) || t(e) || _(e) || u(e) || te(e);
|
|
625
625
|
}
|
|
626
|
-
function
|
|
626
|
+
function z(e) {
|
|
627
627
|
if (!e || typeof e != "object") return !1;
|
|
628
628
|
let t = e;
|
|
629
|
-
return t.type === "swarm-control" && typeof t.id == "string" &&
|
|
629
|
+
return t.type === "swarm-control" && typeof t.id == "string" && _e(t.message);
|
|
630
630
|
}
|
|
631
631
|
//#endregion
|
|
632
632
|
//#region src/worker/main.worker.ts
|
|
633
|
-
var B = null,
|
|
633
|
+
var B = null, ve = new Set([...g, "ai-preload"]), ye = y, be = 1, V = 15e3, H = /* @__PURE__ */ new Map(), U = /* @__PURE__ */ new Set(), W = /* @__PURE__ */ new Map();
|
|
634
634
|
function G(e) {
|
|
635
635
|
U.delete(e);
|
|
636
636
|
let t = W.get(e);
|
|
@@ -639,24 +639,24 @@ function G(e) {
|
|
|
639
639
|
for (let e of t) e();
|
|
640
640
|
}
|
|
641
641
|
}
|
|
642
|
-
function
|
|
642
|
+
function xe(e) {
|
|
643
643
|
return U.has(e) ? new Promise((t) => {
|
|
644
644
|
let n = W.get(e) ?? /* @__PURE__ */ new Set();
|
|
645
645
|
n.add(t), W.set(e, n);
|
|
646
646
|
}) : Promise.resolve();
|
|
647
647
|
}
|
|
648
|
-
var K = null, q = null, J = null,
|
|
649
|
-
async function
|
|
648
|
+
var K = null, q = null, J = null, Se = 6e4;
|
|
649
|
+
async function Ce() {
|
|
650
650
|
for (let e = 0;; e++) try {
|
|
651
|
-
await B.waitForSlot(
|
|
651
|
+
await B.waitForSlot(ye);
|
|
652
652
|
return;
|
|
653
653
|
} catch (t) {
|
|
654
654
|
if (!(t instanceof b)) throw t;
|
|
655
|
-
if (e >=
|
|
655
|
+
if (e >= be) throw Error(`device CPU busy: ${t.message}; task not started, retry on an idle node (retryable)`);
|
|
656
656
|
console.warn(`[flaxia-node:worker] ${t.message}; cooling down ${V}ms before retrying`), await new Promise((e) => setTimeout(e, V));
|
|
657
657
|
}
|
|
658
658
|
}
|
|
659
|
-
function
|
|
659
|
+
function we(e) {
|
|
660
660
|
let t = (t) => {
|
|
661
661
|
let n = H.get(e);
|
|
662
662
|
if (n !== void 0) throw Error(n);
|
|
@@ -667,22 +667,22 @@ function xe(e) {
|
|
|
667
667
|
});
|
|
668
668
|
};
|
|
669
669
|
return async (n) => {
|
|
670
|
-
await
|
|
670
|
+
await xe(e), t(n), await B?.yieldIfOverloaded();
|
|
671
671
|
};
|
|
672
672
|
}
|
|
673
|
-
var Y =
|
|
674
|
-
console.log(`[flaxia-node:worker] startup memory probe wasmMemoryBytes=${Y} capable=${
|
|
675
|
-
function
|
|
676
|
-
return
|
|
673
|
+
var Y = m(), Te = f();
|
|
674
|
+
console.log(`[flaxia-node:worker] startup memory probe wasmMemoryBytes=${Y} capable=${Te}`);
|
|
675
|
+
function Ee() {
|
|
676
|
+
return f();
|
|
677
677
|
}
|
|
678
678
|
var X = /* @__PURE__ */ new Map(), Z = /* @__PURE__ */ new Map();
|
|
679
|
-
Z.set("swarm-inference",
|
|
679
|
+
Z.set("swarm-inference", k);
|
|
680
680
|
var Q = null;
|
|
681
|
-
function
|
|
681
|
+
function De() {
|
|
682
682
|
for (let e of X.values()) try {
|
|
683
683
|
e();
|
|
684
684
|
} catch {}
|
|
685
|
-
X.clear(), Z.clear(), Z.set("swarm-inference",
|
|
685
|
+
X.clear(), Z.clear(), Z.set("swarm-inference", k);
|
|
686
686
|
}
|
|
687
687
|
function $() {
|
|
688
688
|
let e = Date.now(), t = [...Z.values()].flatMap((e) => {
|
|
@@ -699,18 +699,20 @@ function $() {
|
|
|
699
699
|
entries: t
|
|
700
700
|
};
|
|
701
701
|
}
|
|
702
|
-
function
|
|
702
|
+
function Oe() {
|
|
703
703
|
Q && clearTimeout(Q), Q = setTimeout(() => {
|
|
704
|
-
Q = null,
|
|
705
|
-
},
|
|
704
|
+
Q = null, De();
|
|
705
|
+
}, Se);
|
|
706
706
|
}
|
|
707
|
-
async function
|
|
707
|
+
async function ke(e, t, n, r, i) {
|
|
708
708
|
switch (e) {
|
|
709
709
|
case "ai-inference": {
|
|
710
710
|
let r = await import("./assets/ai-inference.js").then((e) => e.s);
|
|
711
|
-
X.set(e, r.releaseCache), Z.set(e, r.getModelCacheSnapshot)
|
|
712
|
-
|
|
713
|
-
|
|
711
|
+
if (X.set(e, r.releaseCache), Z.set(e, r.getModelCacheSnapshot), t && typeof t == "object" && t.decision) {
|
|
712
|
+
let e = await import("./assets/decision.js");
|
|
713
|
+
X.set("decision", e.releaseCache);
|
|
714
|
+
}
|
|
715
|
+
return await r.handleAiInference(t, n);
|
|
714
716
|
}
|
|
715
717
|
case "ai-preload": {
|
|
716
718
|
let e = await import("./assets/ai-inference.js").then((e) => e.s);
|
|
@@ -739,11 +741,11 @@ async function Te(e, t, n, r, i) {
|
|
|
739
741
|
default: throw Error(`Unknown workload type: ${e}`);
|
|
740
742
|
}
|
|
741
743
|
}
|
|
742
|
-
function
|
|
744
|
+
function Ae(e, t, n) {
|
|
743
745
|
return new Promise((r, i) => {
|
|
744
|
-
let a = new
|
|
746
|
+
let a = new ce({
|
|
745
747
|
initial: t,
|
|
746
|
-
runtime:
|
|
748
|
+
runtime: ge(),
|
|
747
749
|
sendControl: (t) => self.postMessage({
|
|
748
750
|
id: e,
|
|
749
751
|
type: "swarm-message",
|
|
@@ -773,7 +775,7 @@ self.onmessage = async (e) => {
|
|
|
773
775
|
return;
|
|
774
776
|
}
|
|
775
777
|
if (t?.type === "swarm-control") {
|
|
776
|
-
if (!
|
|
778
|
+
if (!z(t)) {
|
|
777
779
|
console.warn("[flaxia-node:worker] dropped malformed swarm-control message");
|
|
778
780
|
return;
|
|
779
781
|
}
|
|
@@ -792,8 +794,8 @@ self.onmessage = async (e) => {
|
|
|
792
794
|
let { id: n, workload: r, payload: i, config: a } = t, o = performance.now();
|
|
793
795
|
J = n, G(n);
|
|
794
796
|
try {
|
|
795
|
-
if (B ||= new
|
|
796
|
-
await
|
|
797
|
+
if (B ||= new re(a?.maxCpuLoad), console.log(`[flaxia-node:worker] task start id=${n} workload=${r}`), ve.has(r) && !Ee()) throw Error(`${r}: node incapable: insufficient WASM memory (${Y} bytes < ${p})`);
|
|
798
|
+
await Ce(), B.beginWork();
|
|
797
799
|
let e = setInterval(() => {
|
|
798
800
|
self.postMessage({
|
|
799
801
|
id: n,
|
|
@@ -803,21 +805,22 @@ self.onmessage = async (e) => {
|
|
|
803
805
|
});
|
|
804
806
|
}, 1e4), t = performance.now();
|
|
805
807
|
try {
|
|
806
|
-
let e =
|
|
808
|
+
let e = we(n), t = Array.isArray(a?.fileSourceOrigins) ? a.fileSourceOrigins.filter((e) => typeof e == "string") : [], s = Array.isArray(a?.containerImageOrigins) ? a.containerImageOrigins.filter((e) => typeof e == "string") : [], l = Array.isArray(a?.ccRangeOrigins) ? a.ccRangeOrigins.filter((e) => typeof e == "string") : [], u = r === "swarm-inference" ? await Ae(n, i, e) : await ke(r, i, e, {
|
|
807
809
|
fileSourceOrigins: t,
|
|
808
810
|
containerImageOrigins: s,
|
|
809
|
-
ccRangeOrigins:
|
|
811
|
+
ccRangeOrigins: l
|
|
810
812
|
}, async () => {
|
|
811
813
|
await B?.yieldIfOverloaded();
|
|
812
814
|
});
|
|
815
|
+
if (!d(u)) throw Error(`${r}: result exceeds the ${c} byte limit; refused before posting to the main thread`);
|
|
813
816
|
self.postMessage({
|
|
814
817
|
id: n,
|
|
815
818
|
type: "done",
|
|
816
|
-
result:
|
|
819
|
+
result: u,
|
|
817
820
|
modelCache: $()
|
|
818
821
|
}), console.log(`[flaxia-node:worker] task done id=${n} workload=${r} durationMs=${Math.round(performance.now() - o)}`);
|
|
819
822
|
} finally {
|
|
820
|
-
J = null, H.delete(n), G(n), clearInterval(e), B.markTaskComplete(performance.now() - t),
|
|
823
|
+
J = null, H.delete(n), G(n), clearInterval(e), B.markTaskComplete(performance.now() - t), Oe();
|
|
821
824
|
}
|
|
822
825
|
} catch (e) {
|
|
823
826
|
let t = e instanceof Error ? e.message : String(e), i = e, a = typeof i?.code == "string" ? i.code : void 0, o = typeof i?.status == "number" ? i.status : void 0, s = typeof i?.detail == "string" ? i.detail : void 0;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nudenet-session-cache.test.d.ts","sourceRoot":"","sources":["../../../src/workloads/__tests__/nudenet-session-cache.test.ts"],"names":[],"mappings":""}
|