bitgpu 0.19.0 → 0.19.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 +1 -1
- package/dist/chat.d.ts +7 -1
- package/dist/chat.js +37 -7
- package/dist/chat.js.map +1 -1
- package/dist/index.js +236 -140
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1229,6 +1229,7 @@ async function createEngineInner(options, holder) {
|
|
|
1229
1229
|
if (loadOom) throw new GpuOutOfMemoryError(`GPU allocation failed while loading weights (~${Math.round(weightBytes / 1048576)} MB VRAM needed): ${loadOom.message}`);
|
|
1230
1230
|
if (loadVal) throw new Error(`bitgpu: WebGPU validation error while loading weights: ${loadVal.message}`);
|
|
1231
1231
|
async function ensureKvCapacity(needed) {
|
|
1232
|
+
needed = Math.min(needed, maxSeqLen);
|
|
1232
1233
|
if (needed <= kvCapacity) return;
|
|
1233
1234
|
const newCap = Math.min(maxSeqLen, Math.max(needed, kvCapacity * 2));
|
|
1234
1235
|
const copyBytes = kvCapacity * KV * Dh * KVB;
|
|
@@ -2162,7 +2163,7 @@ async function createEngineInner(options, holder) {
|
|
|
2162
2163
|
const vocab = W.lm_head.N;
|
|
2163
2164
|
const embed = new Float32Array(S * Hd), layer0 = new Float32Array(S * Hd), finalnorm = new Float32Array(S * Hd), logits = new Float32Array(S * vocab);
|
|
2164
2165
|
transients = [];
|
|
2165
|
-
const prefillSeg = (globalThis.__SEG ?? 0) || PREFILL_SEG_HY;
|
|
2166
|
+
const prefillSeg = Math.min((globalThis.__SEG ?? 0) || PREFILL_SEG_HY, 32);
|
|
2166
2167
|
try {
|
|
2167
2168
|
for (let off = 0; off < S; off += prefillSeg) {
|
|
2168
2169
|
const seg = ids.slice(off, off + prefillSeg);
|
|
@@ -2434,14 +2435,22 @@ async function createEngineInner(options, holder) {
|
|
|
2434
2435
|
usage: S_ | CS
|
|
2435
2436
|
});
|
|
2436
2437
|
const sigOff = KT * 8 + (lpN ? 4 : 0);
|
|
2437
|
-
|
|
2438
|
+
let affBuf = device.createBuffer({
|
|
2438
2439
|
size: maxSeqLen * 4,
|
|
2439
2440
|
usage: S_ | CD
|
|
2440
2441
|
});
|
|
2441
|
-
|
|
2442
|
+
let banBuf = device.createBuffer({
|
|
2442
2443
|
size: maxSeqLen * 4,
|
|
2443
2444
|
usage: S_ | CD
|
|
2444
2445
|
});
|
|
2446
|
+
const grow = (b, bytes) => {
|
|
2447
|
+
if (bytes <= b.size) return b;
|
|
2448
|
+
b.destroy();
|
|
2449
|
+
return device.createBuffer({
|
|
2450
|
+
size: 1 << 32 - Math.clz32(bytes - 1),
|
|
2451
|
+
usage: S_ | CD
|
|
2452
|
+
});
|
|
2453
|
+
};
|
|
2445
2454
|
const rbBuf = device.createBuffer({
|
|
2446
2455
|
size: sigOff + (topNS > 0 ? 12 : 0),
|
|
2447
2456
|
usage: GPUBufferUsage.MAP_READ | CD
|
|
@@ -2452,9 +2461,9 @@ async function createEngineInner(options, holder) {
|
|
|
2452
2461
|
});
|
|
2453
2462
|
const writeAffBan = (history) => {
|
|
2454
2463
|
const aff = penalty !== 1 || presence !== 0 ? affectedIds(history) : /* @__PURE__ */ new Uint32Array(0);
|
|
2455
|
-
if (aff.length) device.queue.writeBuffer(affBuf, 0, aff);
|
|
2464
|
+
if (aff.length) device.queue.writeBuffer(affBuf = grow(affBuf, aff.byteLength), 0, aff);
|
|
2456
2465
|
const ban = ngramN > 0 ? ngramBans(history, ngramN) : [];
|
|
2457
|
-
if (ban.length) device.queue.writeBuffer(banBuf, 0, Uint32Array.from(ban));
|
|
2466
|
+
if (ban.length) device.queue.writeBuffer(banBuf = grow(banBuf, ban.length * 4), 0, Uint32Array.from(ban));
|
|
2458
2467
|
return {
|
|
2459
2468
|
affLen: aff.length,
|
|
2460
2469
|
banLen: ban.length
|
|
@@ -2569,13 +2578,14 @@ async function createEngineInner(options, holder) {
|
|
|
2569
2578
|
const ci = ciAll.subarray(0, K);
|
|
2570
2579
|
const cv = cvAll.subarray(0, K);
|
|
2571
2580
|
const perm = new Set(filter(ci, cv));
|
|
2572
|
-
|
|
2581
|
+
{
|
|
2573
2582
|
const pIds = [];
|
|
2574
2583
|
const pVals = [];
|
|
2575
2584
|
for (let i = 0; i < ci.length; i++) if (perm.has(ci[i])) {
|
|
2576
2585
|
pIds.push(ci[i]);
|
|
2577
2586
|
pVals.push(cv[i]);
|
|
2578
2587
|
}
|
|
2588
|
+
if (pIds.length === 0) return chooseTokenSlow(ciAll, cvAll);
|
|
2579
2589
|
let ids = pIds, vals = pVals;
|
|
2580
2590
|
const st = sigTrim(pIds, pVals);
|
|
2581
2591
|
if (st) {
|
|
@@ -2591,6 +2601,8 @@ async function createEngineInner(options, holder) {
|
|
|
2591
2601
|
chosenLogit = pVals[pIds.indexOf(tk)];
|
|
2592
2602
|
return tk;
|
|
2593
2603
|
}
|
|
2604
|
+
};
|
|
2605
|
+
const chooseTokenSlow = async (ciAll, cvAll) => {
|
|
2594
2606
|
const all = await readback(lg, vocab);
|
|
2595
2607
|
for (let i = 0; i < ciAll.length; i++) all[ciAll[i]] = cvAll[i];
|
|
2596
2608
|
const order = Array.from(all.keys()).sort((a, b) => all[b] - all[a] || a - b);
|
|
@@ -2598,14 +2610,19 @@ async function createEngineInner(options, holder) {
|
|
|
2598
2610
|
const pVals = [];
|
|
2599
2611
|
const B = 512;
|
|
2600
2612
|
for (let i = 0; i < order.length && pIds.length < K; i += B) {
|
|
2601
|
-
if (all[order[i]] === -Infinity) break;
|
|
2602
|
-
const batch = order.slice(i, i + B)
|
|
2613
|
+
if (all[order[i]] === -Infinity && pIds.length > 0) break;
|
|
2614
|
+
const batch = order.slice(i, i + B);
|
|
2603
2615
|
const ok = new Set(filter(Uint32Array.from(batch), Float32Array.from(batch.map((id) => all[id]))));
|
|
2604
2616
|
for (const id of batch) if (ok.has(id)) {
|
|
2605
2617
|
pIds.push(id);
|
|
2606
2618
|
pVals.push(all[id]);
|
|
2607
2619
|
if (pIds.length >= K) break;
|
|
2608
2620
|
}
|
|
2621
|
+
if (pVals[0] === -Infinity) {
|
|
2622
|
+
pIds.length = 1;
|
|
2623
|
+
pVals.length = 1;
|
|
2624
|
+
break;
|
|
2625
|
+
}
|
|
2609
2626
|
}
|
|
2610
2627
|
if (pIds.length === 0) throw new Error("bitgpu: candidateFilter permitted no token in the entire vocabulary");
|
|
2611
2628
|
let ids = pIds, vals = pVals;
|
|
@@ -2762,7 +2779,7 @@ async function createEngineInner(options, holder) {
|
|
|
2762
2779
|
const ngramN = genOpts.noRepeatNgramSize ?? 0;
|
|
2763
2780
|
const pl = typeof genOpts.promptLookup === "object" && genOpts.promptLookup !== null ? genOpts.promptLookup : {};
|
|
2764
2781
|
const ngramSize = Math.max(2, pl.ngramSize ?? 3);
|
|
2765
|
-
const maxDraft = Math.max(1, pl.maxDraft ?? 8);
|
|
2782
|
+
const maxDraft = Math.max(1, Math.min(pl.maxDraft ?? 8, 31));
|
|
2766
2783
|
const stopSet = genOpts.stopTokens ? new Set(genOpts.stopTokens) : null;
|
|
2767
2784
|
const onToken = genOpts.onToken;
|
|
2768
2785
|
const signal = genOpts.signal;
|
|
@@ -2788,14 +2805,22 @@ async function createEngineInner(options, holder) {
|
|
|
2788
2805
|
size: K * 4,
|
|
2789
2806
|
usage: S_ | CS
|
|
2790
2807
|
});
|
|
2791
|
-
|
|
2808
|
+
let affBuf = device.createBuffer({
|
|
2792
2809
|
size: (maxSeqLen + maxDraft + 1) * 4,
|
|
2793
2810
|
usage: S_ | CD
|
|
2794
2811
|
});
|
|
2795
|
-
|
|
2812
|
+
let banBuf = device.createBuffer({
|
|
2796
2813
|
size: (maxSeqLen + maxDraft + 1) * 4,
|
|
2797
2814
|
usage: S_ | CD
|
|
2798
2815
|
});
|
|
2816
|
+
const grow = (b, bytes) => {
|
|
2817
|
+
if (bytes <= b.size) return b;
|
|
2818
|
+
b.destroy();
|
|
2819
|
+
return device.createBuffer({
|
|
2820
|
+
size: 1 << 32 - Math.clz32(bytes - 1),
|
|
2821
|
+
usage: S_ | CD
|
|
2822
|
+
});
|
|
2823
|
+
};
|
|
2799
2824
|
const rbAll = device.createBuffer({
|
|
2800
2825
|
size: (maxDraft + 1) * K * 8,
|
|
2801
2826
|
usage: GPUBufferUsage.MAP_READ | CD
|
|
@@ -2810,9 +2835,9 @@ async function createEngineInner(options, holder) {
|
|
|
2810
2835
|
});
|
|
2811
2836
|
const writeAffBan = (h) => {
|
|
2812
2837
|
const aff = penalty !== 1 || presence !== 0 ? affectedIds(h) : /* @__PURE__ */ new Uint32Array(0);
|
|
2813
|
-
if (aff.length) device.queue.writeBuffer(affBuf, 0, aff);
|
|
2838
|
+
if (aff.length) device.queue.writeBuffer(affBuf = grow(affBuf, aff.byteLength), 0, aff);
|
|
2814
2839
|
const ban = ngramN > 0 ? ngramBans(h, ngramN) : [];
|
|
2815
|
-
if (ban.length) device.queue.writeBuffer(banBuf, 0, Uint32Array.from(ban));
|
|
2840
|
+
if (ban.length) device.queue.writeBuffer(banBuf = grow(banBuf, ban.length * 4), 0, Uint32Array.from(ban));
|
|
2816
2841
|
return {
|
|
2817
2842
|
affLen: aff.length,
|
|
2818
2843
|
banLen: ban.length
|
|
@@ -3046,7 +3071,10 @@ async function createEngineInner(options, holder) {
|
|
|
3046
3071
|
}
|
|
3047
3072
|
}
|
|
3048
3073
|
async function debugDecode(prefillIds) {
|
|
3074
|
+
fullHistory = [];
|
|
3075
|
+
cacheLen = 0;
|
|
3049
3076
|
await ensureKvCapacity(prefillIds.length + 1);
|
|
3077
|
+
transients = [];
|
|
3050
3078
|
const encP = device.createCommandEncoder();
|
|
3051
3079
|
stack(encP, embedBatch(encP, prefillIds), prefillIds.length, 0);
|
|
3052
3080
|
device.queue.submit([encP.finish()]);
|
|
@@ -3061,6 +3089,7 @@ async function createEngineInner(options, holder) {
|
|
|
3061
3089
|
size: W.lm_head.N * 4,
|
|
3062
3090
|
usage: S_ | CS
|
|
3063
3091
|
});
|
|
3092
|
+
transients?.push(lg);
|
|
3064
3093
|
const pass = enc.beginComputePass();
|
|
3065
3094
|
lmHead(pass, r.fn, 1, lg);
|
|
3066
3095
|
pass.end();
|
|
@@ -3080,12 +3109,22 @@ async function createEngineInner(options, holder) {
|
|
|
3080
3109
|
DBG0 = null;
|
|
3081
3110
|
return ck;
|
|
3082
3111
|
};
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3112
|
+
try {
|
|
3113
|
+
return {
|
|
3114
|
+
fast: await runStep(false),
|
|
3115
|
+
slow: await runStep(true)
|
|
3116
|
+
};
|
|
3117
|
+
} finally {
|
|
3118
|
+
FORCE_SLOW = false;
|
|
3119
|
+
DBG0 = null;
|
|
3120
|
+
flushTransients();
|
|
3121
|
+
transients = null;
|
|
3122
|
+
}
|
|
3087
3123
|
}
|
|
3088
3124
|
async function debugSampler(ids, genOpts) {
|
|
3125
|
+
fullHistory = [];
|
|
3126
|
+
cacheLen = 0;
|
|
3127
|
+
transients = [];
|
|
3089
3128
|
const vocab = W.lm_head.N;
|
|
3090
3129
|
const K = Math.max(1, Math.min(genOpts.topK ?? 20, vocab));
|
|
3091
3130
|
const penalty = genOpts.repetitionPenalty ?? 1;
|
|
@@ -3103,6 +3142,7 @@ async function createEngineInner(options, holder) {
|
|
|
3103
3142
|
size: K * 4,
|
|
3104
3143
|
usage: S_ | CS
|
|
3105
3144
|
});
|
|
3145
|
+
transients?.push(lg, candIds, candVals);
|
|
3106
3146
|
const aff = penalty !== 1 || presence !== 0 ? affectedIds(ids) : /* @__PURE__ */ new Uint32Array(0);
|
|
3107
3147
|
const ban = ngramN > 0 ? ngramBans(ids, ngramN) : [];
|
|
3108
3148
|
const affBuf = upload(aff.length ? aff : /* @__PURE__ */ new Uint32Array(1), S_ | CD);
|
|
@@ -3113,6 +3153,7 @@ async function createEngineInner(options, holder) {
|
|
|
3113
3153
|
size: Hd * 4,
|
|
3114
3154
|
usage: S_ | CS | CD
|
|
3115
3155
|
});
|
|
3156
|
+
transients?.push(lastP);
|
|
3116
3157
|
enc1.copyBufferToBuffer(fn, (ids.length - 1) * Hd * 4, lastP, 0, Hd * 4);
|
|
3117
3158
|
let pass = enc1.beginComputePass();
|
|
3118
3159
|
lmHead(pass, lastP, 1, lg);
|
|
@@ -3142,12 +3183,17 @@ async function createEngineInner(options, holder) {
|
|
|
3142
3183
|
pass.end();
|
|
3143
3184
|
device.queue.submit([enc2.finish()]);
|
|
3144
3185
|
await device.queue.onSubmittedWorkDone();
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3186
|
+
try {
|
|
3187
|
+
return {
|
|
3188
|
+
base,
|
|
3189
|
+
penalized: await readback(lg, vocab),
|
|
3190
|
+
candIds: await readbackU32(candIds, K),
|
|
3191
|
+
candVals: await readback(candVals, K)
|
|
3192
|
+
};
|
|
3193
|
+
} finally {
|
|
3194
|
+
flushTransients();
|
|
3195
|
+
transients = null;
|
|
3196
|
+
}
|
|
3151
3197
|
}
|
|
3152
3198
|
const capabilities = {
|
|
3153
3199
|
useSubgroups: useSG,
|
|
@@ -3172,6 +3218,7 @@ async function createEngineInner(options, holder) {
|
|
|
3172
3218
|
const sampled = genOpts.temperature != null && genOpts.temperature > 0 && genOpts.temperature !== 1;
|
|
3173
3219
|
const hasProcessors = (genOpts.repetitionPenalty ?? 1) !== 1 || (genOpts.noRepeatNgramSize ?? 0) > 0 || (genOpts.presencePenalty ?? 0) !== 0 || (genOpts.dryMultiplier ?? 0) > 0;
|
|
3174
3220
|
if (((genOpts.dryMultiplier ?? 0) > 0 || (genOpts.topNSigma ?? 0) > 0) && genOpts.promptLookup && genOpts.promptLookup !== "auto") throw new Error("bitgpu: dryMultiplier/topNSigma are not supported with promptLookup (they need per-position statistics; auto simply disables lookup)");
|
|
3221
|
+
if (A.hybrid && genOpts.promptLookup && genOpts.promptLookup !== "auto") throw new Error("bitgpu: promptLookup is not supported on the qwen3_5 hybrid backbone (rejected drafts would corrupt the linear-attention recurrent state); use promptLookup: 'auto' or omit it");
|
|
3175
3222
|
const reuse = (genOpts.reuseCache ?? false) && fullHistory.length > 0;
|
|
3176
3223
|
if (genOpts.signal?.aborted) return {
|
|
3177
3224
|
tokens: [],
|
|
@@ -3186,6 +3233,7 @@ async function createEngineInner(options, holder) {
|
|
|
3186
3233
|
};
|
|
3187
3234
|
let posBase = reuse ? roll ? cacheLen : fullHistory.length - 1 : 0;
|
|
3188
3235
|
const prefillTokens = reuse ? [fullHistory[fullHistory.length - 1], ...promptTokenIds] : promptTokenIds;
|
|
3236
|
+
const hist = reuse ? fullHistory : [...promptTokenIds];
|
|
3189
3237
|
if (prefillTokens.length === 0) throw new Error("generate: no tokens to process");
|
|
3190
3238
|
if (roll && posBase + prefillTokens.length + 1 > maxSeqLen) {
|
|
3191
3239
|
if (SINKS + prefillTokens.length + 1 > maxSeqLen) throw new Error(`generate: prompt length ${prefillTokens.length} exceeds the rolling window (maxSeqLen ${maxSeqLen} minus ${SINKS} sinks); trim the prompt`);
|
|
@@ -3196,110 +3244,120 @@ async function createEngineInner(options, holder) {
|
|
|
3196
3244
|
const room = maxSeqLen - posBase - prefillTokens.length;
|
|
3197
3245
|
if (room < 1) throw new Error(`generate: prompt length ${posBase + prefillTokens.length} exceeds maxSeqLen ${maxSeqLen}; trim history or raise maxSeqLen`);
|
|
3198
3246
|
const maxTokens = roll ? genOpts.maxTokens ?? 256 : Math.min(genOpts.maxTokens ?? 256, room);
|
|
3199
|
-
if (reuse)
|
|
3200
|
-
else fullHistory =
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
tokens: [],
|
|
3211
|
-
prefillMs: performance.now() - t0,
|
|
3212
|
-
decodeMs: 0,
|
|
3213
|
-
tokensPerSecond: 0,
|
|
3214
|
-
timing: {
|
|
3215
|
-
recordMs: 0,
|
|
3216
|
-
gpuMs: 0,
|
|
3217
|
-
readbackMs: 0
|
|
3247
|
+
if (reuse) hist.push(...promptTokenIds);
|
|
3248
|
+
else fullHistory = hist;
|
|
3249
|
+
try {
|
|
3250
|
+
if (maxTokens < 1) {
|
|
3251
|
+
await ensureKvCapacity(posBase + prefillTokens.length);
|
|
3252
|
+
transients = [];
|
|
3253
|
+
try {
|
|
3254
|
+
const t0 = performance.now();
|
|
3255
|
+
if (prefillTokens.length > 1) {
|
|
3256
|
+
await runPrefill(prefillTokens.slice(0, -1), posBase, genOpts.signal);
|
|
3257
|
+
await device.queue.onSubmittedWorkDone();
|
|
3218
3258
|
}
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3259
|
+
cacheLen = posBase + prefillTokens.length - 1;
|
|
3260
|
+
return {
|
|
3261
|
+
tokens: [],
|
|
3262
|
+
prefillMs: performance.now() - t0,
|
|
3263
|
+
decodeMs: 0,
|
|
3264
|
+
tokensPerSecond: 0,
|
|
3265
|
+
timing: {
|
|
3266
|
+
recordMs: 0,
|
|
3267
|
+
gpuMs: 0,
|
|
3268
|
+
readbackMs: 0
|
|
3269
|
+
}
|
|
3270
|
+
};
|
|
3271
|
+
} finally {
|
|
3272
|
+
flushTransients();
|
|
3273
|
+
transients = null;
|
|
3274
|
+
}
|
|
3223
3275
|
}
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
if (E < 24) r = r1;
|
|
3231
|
-
else {
|
|
3232
|
-
const keep = pldWorthIt(E, r1.spec?.steps ?? 0, sampled || hasProcessors);
|
|
3233
|
-
const ids2 = [r1.tokens[E - 1]];
|
|
3234
|
-
const pos2 = posBase + prefillTokens.length + E - 1;
|
|
3235
|
-
const n2 = maxTokens - E;
|
|
3236
|
-
let r2;
|
|
3237
|
-
if (keep) r2 = await generatePldImpl(ids2, pos2, n2, genOpts, fullHistory, r1.rng);
|
|
3238
|
-
else if (sampled || hasProcessors) r2 = await generateSampledImpl(ids2, pos2, n2, genOpts, fullHistory, r1.rng);
|
|
3276
|
+
const hasFilter = !!genOpts.candidateFilter || (genOpts.logprobs ?? 0) > 0;
|
|
3277
|
+
let r;
|
|
3278
|
+
if (!hasFilter && !A.hybrid && (genOpts.dryMultiplier ?? 0) === 0 && (genOpts.topNSigma ?? 0) === 0 && genOpts.promptLookup === "auto" && maxTokens > 24) {
|
|
3279
|
+
const r1 = await generatePldImpl(prefillTokens, posBase, 24, genOpts, hist);
|
|
3280
|
+
const E = r1.tokens.length;
|
|
3281
|
+
if (E < 24) r = r1;
|
|
3239
3282
|
else {
|
|
3240
|
-
const
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
decodeMs,
|
|
3255
|
-
tokPerSec: Math.max(1, total - 1) / (decodeMs / 1e3),
|
|
3256
|
-
tokens: [...r1.tokens, ...r2.tokens],
|
|
3257
|
-
firstArgmax: r1.firstArgmax,
|
|
3258
|
-
recMs: (r1.recMs * w1 + r2.recMs * w2) / (w1 + w2),
|
|
3259
|
-
gpuMs: (r1.gpuMs * w1 + r2.gpuMs * w2) / (w1 + w2),
|
|
3260
|
-
rbMs: (r1.rbMs * w1 + r2.rbMs * w2) / (w1 + w2),
|
|
3261
|
-
spec: {
|
|
3262
|
-
steps: (r1.spec?.steps ?? 0) + (r2.spec?.steps ?? 0),
|
|
3263
|
-
drafted: (r1.spec?.drafted ?? 0) + (r2.spec?.drafted ?? 0),
|
|
3264
|
-
accepted: (r1.spec?.accepted ?? 0) + (r2.spec?.accepted ?? 0),
|
|
3265
|
-
bailed: !keep
|
|
3283
|
+
const keep = pldWorthIt(E, r1.spec?.steps ?? 0, sampled || hasProcessors);
|
|
3284
|
+
const ids2 = [r1.tokens[E - 1]];
|
|
3285
|
+
const pos2 = posBase + prefillTokens.length + E - 1;
|
|
3286
|
+
const n2 = maxTokens - E;
|
|
3287
|
+
let r2;
|
|
3288
|
+
if (keep) r2 = await generatePldImpl(ids2, pos2, n2, genOpts, hist, r1.rng);
|
|
3289
|
+
else if (sampled || hasProcessors) r2 = await generateSampledImpl(ids2, pos2, n2, genOpts, hist, r1.rng);
|
|
3290
|
+
else {
|
|
3291
|
+
r2 = await generateImpl(ids2, pos2, n2, null, SYNC_N, {
|
|
3292
|
+
stopTokens: genOpts.stopTokens,
|
|
3293
|
+
onToken: genOpts.onToken,
|
|
3294
|
+
signal: genOpts.signal
|
|
3295
|
+
});
|
|
3296
|
+
hist.push(...r2.tokens);
|
|
3266
3297
|
}
|
|
3267
|
-
|
|
3298
|
+
const total = E + r2.tokens.length;
|
|
3299
|
+
const decodeMs = r1.decodeMs + r2.prefillMs + r2.decodeMs;
|
|
3300
|
+
const w1 = Math.max(1, E - 1);
|
|
3301
|
+
const w2 = Math.max(0, r2.tokens.length);
|
|
3302
|
+
r = {
|
|
3303
|
+
prefillMs: r1.prefillMs,
|
|
3304
|
+
decodeMs,
|
|
3305
|
+
tokPerSec: Math.max(1, total - 1) / (decodeMs / 1e3),
|
|
3306
|
+
tokens: [...r1.tokens, ...r2.tokens],
|
|
3307
|
+
firstArgmax: r1.firstArgmax,
|
|
3308
|
+
recMs: (r1.recMs * w1 + r2.recMs * w2) / (w1 + w2),
|
|
3309
|
+
gpuMs: (r1.gpuMs * w1 + r2.gpuMs * w2) / (w1 + w2),
|
|
3310
|
+
rbMs: (r1.rbMs * w1 + r2.rbMs * w2) / (w1 + w2),
|
|
3311
|
+
spec: {
|
|
3312
|
+
steps: (r1.spec?.steps ?? 0) + (r2.spec?.steps ?? 0),
|
|
3313
|
+
drafted: (r1.spec?.drafted ?? 0) + (r2.spec?.drafted ?? 0),
|
|
3314
|
+
accepted: (r1.spec?.accepted ?? 0) + (r2.spec?.accepted ?? 0),
|
|
3315
|
+
bailed: !keep
|
|
3316
|
+
}
|
|
3317
|
+
};
|
|
3318
|
+
}
|
|
3319
|
+
} else if (!hasFilter && !A.hybrid && (genOpts.dryMultiplier ?? 0) === 0 && (genOpts.topNSigma ?? 0) === 0 && genOpts.promptLookup) r = await generatePldImpl(prefillTokens, posBase, maxTokens, genOpts, hist);
|
|
3320
|
+
else if (sampled || hasProcessors || hasFilter) r = await generateSampledImpl(prefillTokens, posBase, maxTokens, genOpts, hist);
|
|
3321
|
+
else {
|
|
3322
|
+
r = await generateImpl(prefillTokens, posBase, maxTokens, null, SYNC_N, {
|
|
3323
|
+
stopTokens: genOpts.stopTokens,
|
|
3324
|
+
onToken: genOpts.onToken,
|
|
3325
|
+
signal: genOpts.signal
|
|
3326
|
+
});
|
|
3327
|
+
hist.push(...r.tokens);
|
|
3268
3328
|
}
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3329
|
+
return {
|
|
3330
|
+
tokens: r.tokens,
|
|
3331
|
+
prefillMs: r.prefillMs,
|
|
3332
|
+
decodeMs: r.decodeMs,
|
|
3333
|
+
tokensPerSecond: r.tokPerSec,
|
|
3334
|
+
timing: {
|
|
3335
|
+
recordMs: r.recMs,
|
|
3336
|
+
gpuMs: r.gpuMs,
|
|
3337
|
+
readbackMs: r.rbMs
|
|
3338
|
+
},
|
|
3339
|
+
...r.spec ? { speculation: r.spec } : {},
|
|
3340
|
+
...r.lp ? { logprobs: r.lp } : {}
|
|
3341
|
+
};
|
|
3342
|
+
} catch (e) {
|
|
3343
|
+
fullHistory = [];
|
|
3344
|
+
cacheLen = 0;
|
|
3345
|
+
throw e;
|
|
3279
3346
|
}
|
|
3280
|
-
return {
|
|
3281
|
-
tokens: r.tokens,
|
|
3282
|
-
prefillMs: r.prefillMs,
|
|
3283
|
-
decodeMs: r.decodeMs,
|
|
3284
|
-
tokensPerSecond: r.tokPerSec,
|
|
3285
|
-
timing: {
|
|
3286
|
-
recordMs: r.recMs,
|
|
3287
|
-
gpuMs: r.gpuMs,
|
|
3288
|
-
readbackMs: r.rbMs
|
|
3289
|
-
},
|
|
3290
|
-
...r.spec ? { speculation: r.spec } : {},
|
|
3291
|
-
...r.lp ? { logprobs: r.lp } : {}
|
|
3292
|
-
};
|
|
3293
3347
|
}
|
|
3294
3348
|
async function prefill(ids) {
|
|
3295
3349
|
if (ids.length === 0) throw new Error("prefill: no tokens to process");
|
|
3296
3350
|
if (ids.length > maxSeqLen) throw new Error(`prefill: sequence length ${ids.length} exceeds maxSeqLen ${maxSeqLen}`);
|
|
3351
|
+
fullHistory = [];
|
|
3352
|
+
cacheLen = 0;
|
|
3297
3353
|
await ensureKvCapacity(ids.length);
|
|
3298
3354
|
transients = [];
|
|
3299
3355
|
try {
|
|
3300
3356
|
const t0 = performance.now();
|
|
3301
|
-
|
|
3302
|
-
|
|
3357
|
+
if (ids.length > 1) {
|
|
3358
|
+
await runPrefill(ids.slice(0, -1), 0);
|
|
3359
|
+
await device.queue.onSubmittedWorkDone();
|
|
3360
|
+
}
|
|
3303
3361
|
fullHistory = [...ids];
|
|
3304
3362
|
cacheLen = ids.length - 1;
|
|
3305
3363
|
return { prefillMs: performance.now() - t0 };
|
|
@@ -3321,34 +3379,70 @@ async function createEngineInner(options, holder) {
|
|
|
3321
3379
|
const bytes = snapshotBytes(count);
|
|
3322
3380
|
const data = new ArrayBuffer(bytes);
|
|
3323
3381
|
if (bytes > 0) {
|
|
3324
|
-
const
|
|
3325
|
-
|
|
3326
|
-
usage: GPUBufferUsage.MAP_READ | CD
|
|
3327
|
-
});
|
|
3328
|
-
const enc = device.createCommandEncoder();
|
|
3329
|
-
let off = 0;
|
|
3382
|
+
const cap = Math.max(4, Math.min((globalThis.__RBCAP ?? 0) || 128 << 20, device.limits.maxBufferSize) & -4);
|
|
3383
|
+
const pieces = [];
|
|
3330
3384
|
if (count > 0) for (const li of kvLayers) {
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3385
|
+
pieces.push({
|
|
3386
|
+
src: Kc[li],
|
|
3387
|
+
off: base * kvRowBytes,
|
|
3388
|
+
size: count * kvRowBytes
|
|
3389
|
+
});
|
|
3390
|
+
pieces.push({
|
|
3391
|
+
src: Vc[li],
|
|
3392
|
+
off: base * kvRowBytes,
|
|
3393
|
+
size: count * kvRowBytes
|
|
3394
|
+
});
|
|
3335
3395
|
if (kv8) {
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3396
|
+
pieces.push({
|
|
3397
|
+
src: Ksc[li],
|
|
3398
|
+
off: base * scRowBytes,
|
|
3399
|
+
size: count * scRowBytes
|
|
3400
|
+
});
|
|
3401
|
+
pieces.push({
|
|
3402
|
+
src: Vsc[li],
|
|
3403
|
+
off: base * scRowBytes,
|
|
3404
|
+
size: count * scRowBytes
|
|
3405
|
+
});
|
|
3340
3406
|
}
|
|
3341
3407
|
}
|
|
3342
3408
|
for (const li of linearLayers) {
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3409
|
+
pieces.push({
|
|
3410
|
+
src: hyRS[li][hyPar],
|
|
3411
|
+
off: 0,
|
|
3412
|
+
size: rsSz
|
|
3413
|
+
});
|
|
3414
|
+
pieces.push({
|
|
3415
|
+
src: hyCS[li][hyPar],
|
|
3416
|
+
off: 0,
|
|
3417
|
+
size: csSz
|
|
3418
|
+
});
|
|
3419
|
+
}
|
|
3420
|
+
const rb = device.createBuffer({
|
|
3421
|
+
size: Math.min(bytes, cap),
|
|
3422
|
+
usage: GPUBufferUsage.MAP_READ | CD
|
|
3423
|
+
});
|
|
3424
|
+
let pi = 0;
|
|
3425
|
+
let pOff = 0;
|
|
3426
|
+
for (let dst = 0; dst < bytes;) {
|
|
3427
|
+
const sz = Math.min(cap, bytes - dst);
|
|
3428
|
+
const enc = device.createCommandEncoder();
|
|
3429
|
+
for (let fill = 0; fill < sz;) {
|
|
3430
|
+
const p = pieces[pi];
|
|
3431
|
+
const take = Math.min(p.size - pOff, sz - fill);
|
|
3432
|
+
enc.copyBufferToBuffer(p.src, p.off + pOff, rb, fill, take);
|
|
3433
|
+
fill += take;
|
|
3434
|
+
pOff += take;
|
|
3435
|
+
if (pOff === p.size) {
|
|
3436
|
+
pi++;
|
|
3437
|
+
pOff = 0;
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
device.queue.submit([enc.finish()]);
|
|
3441
|
+
await rb.mapAsync(GPUMapMode.READ, 0, sz);
|
|
3442
|
+
new Uint8Array(data, dst, sz).set(new Uint8Array(rb.getMappedRange(0, sz)));
|
|
3443
|
+
rb.unmap();
|
|
3444
|
+
dst += sz;
|
|
3347
3445
|
}
|
|
3348
|
-
device.queue.submit([enc.finish()]);
|
|
3349
|
-
await rb.mapAsync(GPUMapMode.READ);
|
|
3350
|
-
new Uint8Array(data).set(new Uint8Array(rb.getMappedRange()));
|
|
3351
|
-
rb.unmap();
|
|
3352
3446
|
rb.destroy();
|
|
3353
3447
|
}
|
|
3354
3448
|
return {
|
|
@@ -3382,7 +3476,7 @@ async function createEngineInner(options, holder) {
|
|
|
3382
3476
|
const base = Math.max(0, Math.floor(snap.base ?? 0));
|
|
3383
3477
|
if (base > 0 && A.hybrid) throw new Error("restoreCache: delta snapshots are not supported for the qwen3_5 hybrid backbone");
|
|
3384
3478
|
const count = len - base;
|
|
3385
|
-
if (len + 1 > maxSeqLen) throw new Error(`restoreCache: snapshot needs ${len + 1} cache slots but maxSeqLen is ${maxSeqLen}`);
|
|
3479
|
+
if (len + (snap.version === 2 ? 0 : 1) > maxSeqLen) throw new Error(`restoreCache: snapshot needs ${len + (snap.version === 2 ? 0 : 1)} cache slots but maxSeqLen is ${maxSeqLen}`);
|
|
3386
3480
|
if (snap.data.byteLength !== snapshotBytes(count)) throw new Error(`restoreCache: snapshot data is ${snap.data.byteLength} bytes, expected ${snapshotBytes(count)}`);
|
|
3387
3481
|
if (base > 0) {
|
|
3388
3482
|
if (cacheLen !== base) throw new Error(`restoreCache: delta snapshot expects the cache at position ${base} (prewarm the shared prefix first); it is at ${cacheLen}`);
|
|
@@ -3432,16 +3526,18 @@ async function createEngineInner(options, holder) {
|
|
|
3432
3526
|
dispose: () => device.destroy(),
|
|
3433
3527
|
device,
|
|
3434
3528
|
adapter,
|
|
3435
|
-
profileDecode: async (ids, nTokens, full = null, syncN = SYNC_N) => {
|
|
3529
|
+
profileDecode: serialize(async (ids, nTokens, full = null, syncN = SYNC_N) => {
|
|
3530
|
+
fullHistory = [];
|
|
3531
|
+
cacheLen = 0;
|
|
3436
3532
|
TS_PROFILE = hasTS;
|
|
3437
3533
|
try {
|
|
3438
3534
|
return await generateImpl(ids, 0, nTokens, full, syncN);
|
|
3439
3535
|
} finally {
|
|
3440
3536
|
TS_PROFILE = false;
|
|
3441
3537
|
}
|
|
3442
|
-
},
|
|
3443
|
-
debugDecode,
|
|
3444
|
-
debugSampler
|
|
3538
|
+
}),
|
|
3539
|
+
debugDecode: serialize(debugDecode),
|
|
3540
|
+
debugSampler: serialize(debugSampler)
|
|
3445
3541
|
};
|
|
3446
3542
|
}
|
|
3447
3543
|
//#endregion
|