Spaces:
Running

File size: 24,761 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Chunk delivery for a BNZ2 weight file. The container is the bnz codec.
// Each WebRTC blob is that codec's bytes, gzipped again. The join side
// gunzips, concatenates, and hands the BNZ2 bytes to openBytes.
import { openBytes } from "./bnz.js";

export const RANGE_STEP = 2 * 1024 * 1024;

// HeapVM page table: one byte per wasm page over one memory block
// (machwasm MachPageLookupTable + HvAllocateMemoryBlock). The block is a
// WebAssembly.Memory, the Chrome heap, not a JS ArrayBuffer of the file.
// The process row matches chrome.processes: private memory is this block,
// and jsMemory stays 0 because the body is not on the V8 heap.
export const HEAP_PAGE = 65536;

export function createWeightHeap(size) {
  const pages = Math.ceil(size / HEAP_PAGE);
  const mem = new WebAssembly.Memory({ initial: pages, maximum: pages, shared: true });
  const table = new SharedArrayBuffer(pages);
  return {
    mem,
    table,
    size,
    pages,
    proc: { id: 1, image: "model.bnz", type: "gpu", tpt: 1, privateMemory: 0, jsMemoryAllocated: 0, jsMemoryUsed: 0 },
  };
}

// A phone cannot allocate a shared wasm memory the size of the decoded
// safetensors image (Hodge-3.5 is 2.18 GB). The guest reads that image
// front to back, twice (the header, then the upload), so a window a little
// larger than the biggest segment is enough. owners[slot] is the file step
// currently stored there, or -1. ctrl[0] is the first file step the guest
// may still read; earlier steps can be overwritten.
export function openWeightWindow(maxRaw, step) {
  if (step <= 0 || maxRaw <= 0) throw new Error("weight window");
  let bytes = 64 * 1024 * 1024;
  const need = maxRaw + step;
  while (bytes < need) bytes += 64 * 1024 * 1024;
  if (bytes > 256 * 1024 * 1024) throw new Error("a weight segment is too large for this device");
  const heap = createWeightHeap(bytes);
  const slots = bytes / step;
  const ctrlSab = new SharedArrayBuffer(4);
  const ownersSab = new SharedArrayBuffer(slots * 4);
  new Int32Array(ownersSab).fill(-1);
  return { heap, bytes, slots, ctrlSab, ownersSab };
}

export function notePages(table, off, n, size) {
  const pages = new Uint8Array(table);
  const end = Math.min(size, off + n);
  if (end <= off) return;
  const a = Math.floor(off / HEAP_PAGE);
  const b = Math.floor((end - 1) / HEAP_PAGE);
  for (let i = a; i <= b; i++) pages[i] = 1;
}

export function privateMemory(table, size) {
  const pages = new Uint8Array(table);
  let n = 0;
  for (let i = 0; i < pages.length; i++) {
    if (pages[i] !== 1) continue;
    const start = i * HEAP_PAGE;
    n += Math.min(size, start + HEAP_PAGE) - start;
  }
  return n;
}

export function weightProcess(table, size) {
  return {
    id: 1,
    image: "model.bnz",
    type: "gpu",
    tpt: 1,
    privateMemory: privateMemory(table, size),
    jsMemoryAllocated: 0,
    jsMemoryUsed: 0,
  };
}

export const MAX_BLOB = 16 * 1024;
const FLAG_BLOB = 0x01;
const FLAG_GZIP = 0x02;
const ICE = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };

export function encodeFrame(msg, blob, gzipped) {
  const js = new TextEncoder().encode(JSON.stringify(msg));
  const n = blob ? blob.length : 0;
  const out = new Uint8Array(5 + js.length + n);
  out[0] = (n ? FLAG_BLOB : 0) | (gzipped ? FLAG_GZIP : 0);
  new DataView(out.buffer).setUint32(1, js.length, false);
  out.set(js, 5);
  if (n) out.set(blob, 5 + js.length);
  return out;
}

export function decodeFrame(buf) {
  const u = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
  if (u.length < 5) throw new Error("bnz: short frame");
  const n = new DataView(u.buffer, u.byteOffset, u.byteLength).getUint32(1, false);
  const msg = JSON.parse(new TextDecoder().decode(u.subarray(5, 5 + n)));
  const blob = (u[0] & FLAG_BLOB) ? u.subarray(5 + n) : new Uint8Array(0);
  return { msg, blob, gzip: (u[0] & FLAG_GZIP) !== 0 };
}

export async function gzipBytes(bytes) {
  const stream = new Blob([bytes]).stream().pipeThrough(new CompressionStream("gzip"));
  return new Uint8Array(await new Response(stream).arrayBuffer());
}

export async function gunzipBytes(bytes) {
  const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"));
  return new Uint8Array(await new Response(stream).arrayBuffer());
}

function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); }

function waitGather(pc) {
  if (pc.iceGatheringState === "complete") return Promise.resolve();
  return new Promise((resolve) => {
    const t = setTimeout(resolve, 2500);
    pc.addEventListener("icegatheringstatechange", () => {
      if (pc.iceGatheringState === "complete") { clearTimeout(t); resolve(); }
    });
  });
}

async function pace(dc) {
  while (dc.bufferedAmount > 1024 * 1024) await sleep(5);
}

// Host: read model.bnz by HTTP Range and send gzip(bnz-slice) frames.
export function startHost({ url, room, onStatus }) {
  const base = roomBase(room);
  const pc = new RTCPeerConnection(ICE);
  const dc = pc.createDataChannel("bnz", { ordered: true });
  dc.binaryType = "arraybuffer";
  let size = 0;
  const say = (s) => { if (onStatus) onStatus(s); };

  dc.onopen = () => {
    dc.send(encodeFrame({ op: "hello", name: "bnz", proto: 1 }));
  };
  dc.onmessage = (ev) => {
    const frame = decodeFrame(ev.data);
    const m = frame.msg;
    if (m.op === "hello") {
      dc.send(encodeFrame({ op: "hello_ok", name: "bnz", proto: 1 }));
      return;
    }
    if (m.op === "stat") {
      dc.send(encodeFrame({ op: "stat_ok", id: m.id, path: m.path, size }));
      return;
    }
    if (m.op === "get") {
      sendRange(dc, url, m).catch((e) => {
        dc.send(encodeFrame({ op: "error", id: m.id, error: String(e.message || e) }));
      });
    }
  };

  (async () => {
    const head = await fetch(url, { method: "HEAD" });
    size = Number(head.headers.get("Content-Length"));
    if (!head.ok || !size) throw new Error("no weights");
    const offer = await pc.createOffer();
    await pc.setLocalDescription(offer);
    await waitGather(pc);
    await fetch(base + "/offer", { method: "POST", body: pc.localDescription.sdp });
    say("Waiting for a client.");
    for (;;) {
      const r = await fetch(base + "/answer");
      if (r.ok) {
        const sdp = await r.text();
        if (sdp.includes("v=0")) {
          await pc.setRemoteDescription({ type: "answer", sdp });
          say("Connected.");
          return;
        }
      }
      await sleep(200);
    }
  })().catch((e) => say(String(e.message || e)));

  return {
    close() { pc.close(); },
  };
}

async function readRange(url, off, n) {
  const r = await fetch(url, { headers: { Range: "bytes=" + off + "-" + (off + n - 1) } });
  if (r.status !== 206 && r.status !== 200) throw new Error("HTTP " + r.status);
  const b = new Uint8Array(await r.arrayBuffer());
  if (r.status === 206 || b.length === n) return b;
  return b.subarray(off, off + n);
}

async function sendRange(dc, url, m) {
  const end = m.off + m.size;
  dc.send(encodeFrame({ op: "get_ok", id: m.id, size: m.size }));
  for (let off = m.off; off < end; ) {
    const n = Math.min(MAX_BLOB, end - off);
    const raw = await readRange(url, off, n);
    const gz = await gzipBytes(raw);
    await pace(dc);
    dc.send(encodeFrame({ op: "chunk", id: m.id, off, last: off + n >= end }, gz, true));
    off += n;
  }
}

// Join: one offer check. No offer means the caller uses HTTP.
export async function pullWebRTC({ room, onProgress }) {
  const base = roomBase(room);
  const offerRes = await fetch(base + "/offer");
  if (!offerRes.ok) return null;
  const offer = await offerRes.text();
  if (!offer.includes("v=0")) return null;
  const pc = new RTCPeerConnection(ICE);
  const got = new Promise((resolve, reject) => {
    const timer = setTimeout(() => reject(new Error("weights timed out")), 15000);
    let seen = false;
    pc.ondatachannel = (ev) => {
      const dc = ev.channel;
      dc.binaryType = "arraybuffer";
      let out = null;
      let filled = 0;
      let chain = Promise.resolve();
      dc.onclose = () => {
        if (!out || filled < out.length) reject(new Error("weights closed"));
      };
      dc.onmessage = (msgEv) => {
        const frame = decodeFrame(msgEv.data);
        chain = chain.then(async () => {
          const m = frame.msg;
          if (m.op === "hello") {
            if (!seen) { seen = true; clearTimeout(timer); }
            dc.send(encodeFrame({ op: "hello_ok", name: "champ", proto: 1 }));
            dc.send(encodeFrame({ op: "stat", id: "s", path: "model.bnz" }));
            return;
          }
          if (m.op === "stat_ok") {
            out = new Uint8Array(m.size);
            dc.send(encodeFrame({ op: "get", id: "g", path: "model.bnz", off: 0, size: m.size }));
            return;
          }
          if (m.op === "error") throw new Error(m.error || "weights");
          if (m.op !== "chunk" || !out) return;
          const raw = frame.gzip ? await gunzipBytes(frame.blob) : frame.blob;
          out.set(raw, m.off);
          filled += raw.length;
          if (onProgress && out.length) onProgress(filled / out.length);
          if (m.last || filled >= out.length) {
            clearTimeout(timer);
            resolve(out);
          }
        }).catch((e) => { clearTimeout(timer); reject(e); });
      };
    };
  });
  await pc.setRemoteDescription({ type: "offer", sdp: offer });
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);
  await waitGather(pc);
  await fetch(base + "/answer", { method: "POST", body: pc.localDescription.sdp });
  try {
    return await got;
  } finally {
    pc.close();
  }
}

// The site serves the one model's packed bytes in gzip slices at a fixed endpoint: no path in
// the request, so it can only ever read that file.
const MODEL_PATH = "/models/champ/model.bnz";
const MODEL_BYTES = "/api/model/bytes";

// A WebRTC signaling room is a plain name, never a path.
function roomBase(room) {
  if (!/^[a-z0-9-]{1,32}$/.test(room || "")) throw new Error("weights");
  return "/api/bnz/" + room;
}

async function pullGzipApi(size, onProgress) {
  const probe = await fetch(MODEL_BYTES + "?off=0&n=1");
  if (!probe.ok) return null;
  const first = new Uint8Array(await probe.arrayBuffer());
  if (first.length !== 1) return null;
  const out = new Uint8Array(size);
  out.set(first, 0);
  let done = first.length;
  let next = 1;
  async function worker() {
    for (;;) {
      const off = next;
      next += MAX_BLOB;
      if (off >= size) return;
      const n = Math.min(MAX_BLOB, size - off);
      const r = await fetch(MODEL_BYTES + "?off=" + off + "&n=" + n);
      if (!r.ok) throw new Error("weights");
      const raw = new Uint8Array(await r.arrayBuffer());
      if (raw.length !== n) throw new Error("weights");
      out.set(raw, off);
      done += n;
      if (onProgress) onProgress(done / size);
    }
  }
  if (onProgress) onProgress(done / size);
  await Promise.all([worker(), worker(), worker(), worker()]);
  return out;
}

// One redirect, then ranges against the final URL. A 1 MiB range against
// the public link repeats the redirect for every piece.
const RANGE_WORKERS = 6;

function bodySize(res) {
  const cr = res.headers.get("Content-Range") || "";
  const total = Number((/\/(\d+)\s*$/.exec(cr) || [])[1]);
  const linked = Number(res.headers.get("X-Linked-Size"));
  const len = Number(res.headers.get("Content-Length"));
  return total || linked || len || 0;
}

async function pullRange(url, size, onProgress) {
  const out = new Uint8Array(size);
  let next = 0;
  let done = 0;
  async function worker() {
    for (;;) {
      const off = next;
      next += RANGE_STEP;
      if (off >= size) return;
      const n = Math.min(RANGE_STEP, size - off);
      const raw = await readRange(url, off, n);
      if (raw.length !== n) throw new Error("weights");
      out.set(raw, off);
      done += n;
      if (onProgress) onProgress(done / size);
    }
  }
  const jobs = [];
  for (let i = 0; i < RANGE_WORKERS; i++) jobs.push(worker());
  await Promise.all(jobs);
  return out;
}

export async function pullHttp(url, onProgress, knownSize) {
  const page = new URL(url, location.href);
  const same = page.origin === location.origin;
  let size = knownSize || 0;
  if (same && size && page.pathname === MODEL_PATH) {
    const gz = await pullGzipApi(size, onProgress);
    if (gz) return gz;
  }
  if (!size) {
    const probe = await fetch(url, { headers: { Range: "bytes=0-0" } });
    if (probe.status !== 206 && probe.status !== 200) throw new Error("weights");
    size = bodySize(probe);
    await probe.arrayBuffer();
    if (!size) throw new Error("weights");
    url = probe.url || url;
  }
  return pullRange(url, size, onProgress);
}

// Download into a SharedArrayBuffer the guest can read while bytes are
// still arriving. flags[i] is 0 missing, 1 ready, -1 failed.
const WEIGHT_CACHE = "champ-weights";

function chunkReq(etag, step, i) {
  return new Request("https://champ.invalid/bnz/" + etag + "/" + step + "/" + i);
}

async function openWeightCache(etag) {
  if (!etag || typeof caches === "undefined") return null;
  try { return await caches.open(WEIGHT_CACHE); }
  catch { return null; }
}

// Drop a previous Hugging Face file only after the new one is fully stored.
async function dropOtherWeights(cache, etag) {
  if (!cache || !etag) return;
  try {
    const keys = await cache.keys();
    await Promise.all(keys.map((req) => {
      if (!req.url.includes("/bnz/" + etag + "/")) return cache.delete(req);
    }));
  } catch { /* private mode or a full disk */ }
}

async function cachedChunk(cache, etag, step, i, n) {
  if (!cache || !etag) return null;
  try {
    const hit = await cache.match(chunkReq(etag, step, i));
    if (!hit) return null;
    const b = new Uint8Array(await hit.arrayBuffer());
    return b.length === n ? b : null;
  } catch {
    return null;
  }
}

function storeChunk(cache, etag, step, i, bytes) {
  if (!cache || !etag) return;
  const copy = bytes.slice();
  cache.put(chunkReq(etag, step, i), new Response(copy, {
    headers: { "content-length": String(copy.length) },
  })).catch(() => {});
}

function landChunk(data, table, flags, step, size, got, off, raw) {
  data.set(raw, off);
  notePages(table, off, raw.length, size);
  const i = Math.floor(off / step);
  got[i] += raw.length;
  const need = Math.min(step, size - i * step);
  if (got[i] >= need && Atomics.load(flags, i) !== 1) {
    Atomics.store(flags, i, 1);
    Atomics.notify(flags, i);
  }
}

async function streamWebRTC(spec, data, table, flags, step, onProgress) {
  const base = roomBase(spec.room);
  const offerRes = await fetch(base + "/offer");
  if (!offerRes.ok) return false;
  const offer = await offerRes.text();
  if (!offer.includes("v=0")) return false;
  const size = spec.size;
  const chunks = Math.ceil(size / step);
  const got = new Int32Array(chunks);
  const pc = new RTCPeerConnection(ICE);
  const done = new Promise((resolve, reject) => {
    const timer = setTimeout(() => reject(new Error("weights timed out")), 20000);
    let seen = false;
    pc.ondatachannel = (ev) => {
      const dc = ev.channel;
      dc.binaryType = "arraybuffer";
      let filled = 0;
      let chain = Promise.resolve();
      dc.onclose = () => {
        if (filled < size) reject(new Error("weights closed"));
      };
      dc.onmessage = (msgEv) => {
        const frame = decodeFrame(msgEv.data);
        chain = chain.then(async () => {
          const m = frame.msg;
          if (m.op === "hello") {
            if (!seen) { seen = true; clearTimeout(timer); }
            dc.send(encodeFrame({ op: "hello_ok", name: "champ", proto: 1 }));
            dc.send(encodeFrame({ op: "stat", id: "s", path: "model.bnz" }));
            return;
          }
          if (m.op === "stat_ok") {
            dc.send(encodeFrame({ op: "get", id: "g", path: "model.bnz", off: 0, size }));
            return;
          }
          if (m.op === "error") throw new Error(m.error || "weights");
          if (m.op !== "chunk") return;
          const raw = frame.gzip ? await gunzipBytes(frame.blob) : frame.blob;
          landChunk(data, table, flags, step, size, got, m.off, raw);
          filled += raw.length;
          if (onProgress && size) onProgress(filled / size);
          if (m.last || filled >= size) {
            clearTimeout(timer);
            resolve(true);
          }
        }).catch((e) => { clearTimeout(timer); reject(e); });
      };
    };
  });
  await pc.setRemoteDescription({ type: "offer", sdp: offer });
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);
  await waitGather(pc);
  await fetch(base + "/answer", { method: "POST", body: pc.localDescription.sdp });
  try {
    return await done;
  } finally {
    pc.close();
  }
}

async function streamRange(spec, data, table, flags, step, onProgress) {
  const size = spec.size;
  const chunks = Math.ceil(size / step);
  const got = new Int32Array(chunks);
  const cache = await openWeightCache(spec.etag || "");
  let next = 0;
  let done = 0;
  function fail(err) {
    for (let i = 0; i < chunks; i++) {
      if (Atomics.load(flags, i) === 0) {
        Atomics.store(flags, i, -1);
        Atomics.notify(flags, i);
      }
    }
    throw err;
  }
  async function worker() {
    for (;;) {
      const i = next;
      next += 1;
      if (i >= chunks) return;
      const off = i * step;
      const n = Math.min(step, size - off);
      let raw = await cachedChunk(cache, spec.etag, step, i, n);
      if (!raw) {
        try { raw = await readRange(spec.url, off, n); }
        catch (err) { fail(err); return; }
        if (raw.length !== n) { fail(new Error("weights")); return; }
        storeChunk(cache, spec.etag, step, i, raw);
      }
      landChunk(data, table, flags, step, size, got, off, raw);
      done += n;
      if (onProgress) onProgress(done / size);
    }
  }
  const jobs = [];
  for (let i = 0; i < RANGE_WORKERS; i++) jobs.push(worker());
  await Promise.all(jobs);
  await dropOtherWeights(cache, spec.etag || "");
}

// Chunks land in the wasm heap. A missing WebRTC host falls through to
// range reads of the same block. The JS side holds one chunk, not the file.
export async function fillHeap(spec, mem, table, flags, onProgress) {
  const data = new Uint8Array(mem.buffer, 0, spec.size);
  if (onProgress) onProgress(0);
  if (spec.room) {
    try {
      if (await streamWebRTC(spec, data, table, flags, spec.step, onProgress)) return;
    } catch { /* range fills the same heap */ }
  }
  await streamRange(spec, data, table, flags, spec.step, onProgress);
}

function flagView(flags) {
  return flags instanceof Int32Array ? flags : new Int32Array(flags);
}

export function openHeap(mem, flags, size, step) {
  const data = new Uint8Array(mem.buffer, 0, size);
  const words = flagView(flags);
  function ensure(off, len) {
    if (len <= 0) return;
    const last = Math.min(size, off + len);
    const a = Math.floor(off / step);
    const b = Math.floor((last - 1) / step);
    for (let i = a; i <= b; i++) {
      while (Atomics.load(words, i) === 0) {
        const woke = Atomics.wait(words, i, 0, 120000);
        if (woke === "timed-out" && Atomics.load(words, i) === 0) throw new Error("weights");
      }
      if (Atomics.load(words, i) !== 1) throw new Error("weights");
    }
  }
  return openBytes(data, ensure);
}

// A heap that holds the model's raw safetensors bytes (BNZ4: bnz4load.js lands each decoded,
// verified segment at its raw_off). readAt waits for the steps it touches, then copies them out.
// win, when set, is the phone window from openWeightWindow: the memory is only win.bytes long
// and file step i lives at slot i % win.slots. The guest reads twice from the start, so the
// first pass (the header) does not release steps; after the second read at 0, steps behind
// the cursor may be overwritten. A read larger than the window copies one step at a time.
export function openRaw(mem, flags, size, step, win, embed) {
  const words = flagView(flags);
  const span = win ? win.bytes : size;
  const data = new Uint8Array(mem.buffer, 0, span);
  let passes = 0;
  function release(stepIndex) {
    if (!win || passes < 2) return;
    if (Atomics.load(win.ctrl, 0) === stepIndex) return;
    Atomics.store(win.ctrl, 0, stepIndex);
    Atomics.notify(win.ctrl, 0);
  }
  function waitStep(i) {
    while (Atomics.load(words, i) !== 1) {
      if (Atomics.load(words, i) < 0) throw new Error("weights");
      const woke = Atomics.wait(words, i, 0, 120000);
      if (woke === "timed-out" && Atomics.load(words, i) !== 1) throw new Error("weights");
    }
  }
  // The phone path keeps the embedding tensor in a file. Bytes of that tensor
  // are copied there as the guest reads them. embed is {base, bytes, reuse, writeAt}.
  function tee(pos, bytes) {
    if (!embed || embed.reuse || !bytes || !bytes.length) return;
    const from = Math.max(pos, embed.base);
    const to = Math.min(pos + bytes.length, embed.base + embed.bytes);
    if (to <= from) return;
    if (!embed.writeAt(from - embed.base, bytes.subarray(from - pos, to - pos))) {
      throw new Error("embedding store write failed");
    }
  }
  return {
    size,
    readAt(pos, n) {
      if (pos < 0 || n <= 0) return new Uint8Array(0);
      const last = Math.min(size, pos + n);
      if (pos === 0) passes++;
      if (!win) {
        if (last > pos) {
          for (let i = Math.floor(pos / step); i <= Math.floor((last - 1) / step); i++) waitStep(i);
        }
        const out = data.slice(pos, last);
        tee(pos, out);
        return out;
      }
      const out = new Uint8Array(Math.max(0, last - pos));
      for (let at = pos; at < last;) {
        const stepIndex = Math.floor(at / step);
        release(stepIndex);
        waitStep(stepIndex);
        const slot = stepIndex % win.slots;
        if (Atomics.load(win.owners, slot) !== stepIndex) throw new Error("weights");
        const within = at - stepIndex * step;
        const ncopy = Math.min(last - at, step - within);
        out.set(data.subarray(slot * step + within, slot * step + within + ncopy), at - pos);
        at += ncopy;
      }
      tee(pos, out);
      return out;
    },
  };
}

export async function fillShared(url, sab, size, step, onProgress, etag) {
  const chunks = Math.ceil(size / step);
  const flags = new Int32Array(sab, 0, chunks);
  const data = new Uint8Array(sab, chunks * 4, size);
  const cache = await openWeightCache(etag || "");
  let next = 0;
  let done = 0;
  function fail(err) {
    for (let i = 0; i < chunks; i++) {
      if (Atomics.load(flags, i) === 0) {
        Atomics.store(flags, i, -1);
        Atomics.notify(flags, i);
      }
    }
    throw err;
  }
  async function worker() {
    for (;;) {
      const i = next;
      next += 1;
      if (i >= chunks) return;
      const off = i * step;
      const n = Math.min(step, size - off);
      let raw = await cachedChunk(cache, etag, step, i, n);
      if (!raw) {
        try { raw = await readRange(url, off, n); }
        catch (err) { fail(err); return; }
        if (raw.length !== n) { fail(new Error("weights")); return; }
        storeChunk(cache, etag, step, i, raw);
      }
      data.set(raw, off);
      Atomics.store(flags, i, 1);
      Atomics.notify(flags, i);
      done += n;
      if (onProgress) onProgress(done / size);
    }
  }
  const jobs = [];
  for (let i = 0; i < RANGE_WORKERS; i++) jobs.push(worker());
  await Promise.all(jobs);
  await dropOtherWeights(cache, etag || "");
}

export function openShared(sab, size, step) {
  const chunks = Math.ceil(size / step);
  const flags = new Int32Array(sab, 0, chunks);
  const data = new Uint8Array(sab, chunks * 4, size);
  function ensure(off, len) {
    if (len <= 0) return;
    const last = Math.min(size, off + len);
    const a = Math.floor(off / step);
    const b = Math.floor((last - 1) / step);
    for (let i = a; i <= b; i++) {
      while (Atomics.load(flags, i) === 0) {
        const woke = Atomics.wait(flags, i, 0, 120000);
        if (woke === "timed-out" && Atomics.load(flags, i) === 0) throw new Error("weights");
      }
      if (Atomics.load(flags, i) !== 1) throw new Error("weights");
    }
  }
  return openBytes(data, ensure);
}

// Bytes the guest can readAt. WebRTC (bnz + gzip) when a host is up,
// otherwise HTTP. The view decodes one BNZ segment at a time.
export async function openWeight(spec, onProgress) {
  let bytes = null;
  if (spec.room) {
    try { bytes = await pullWebRTC({ room: spec.room, onProgress }); }
    catch { bytes = null; }
  }
  if (!bytes) bytes = await pullHttp(spec.url, onProgress, spec.size || 0);
  return openBytes(bytes);
}