Spaces:
Running

File size: 6,703 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
// On-device embedder. No outside embedding model.
// A fixed ternary LUT, packed like the WGSL weight codes, then one discrete
// exterior step on the token path: coboundary, Laplacian smooth, mean.
// The LUT is stored as a BNZ2 module so new weights use the same codec.

import { decompressSafetensors, sha256 } from "./bnz.js";

export const DIM = 32;
export const BUCKETS = 256;

function mix(n) {
  n >>>= 0;
  n = Math.imul(n ^ (n >>> 16), 0x7feb352d) >>> 0;
  n = Math.imul(n ^ (n >>> 15), 0x846ca68b) >>> 0;
  return (n ^ (n >>> 16)) >>> 0;
}

export function buildLut() {
  const words = new Uint32Array(BUCKETS * (DIM / 16));
  for (let b = 0; b < BUCKETS; b++) {
    for (let d = 0; d < DIM; d++) {
      const code = mix(Math.imul(b, 131) + Math.imul(d, 17) + 0x9e3779b9) % 3;
      const idx = b * DIM + d;
      words[(idx / 16) | 0] |= code << ((idx % 16) * 2);
    }
  }
  return words;
}

export function codeAt(words, bucket, d) {
  const idx = bucket * DIM + d;
  const w = words[(idx / 16) | 0];
  return ((w >>> ((idx % 16) * 2)) & 3) - 1;
}

export function tokensOf(text) {
  const words = String(text || "").toLowerCase().match(/[a-z0-9]+/g);
  if (!words || !words.length) return [0];
  return words.slice(0, 64).map((word) => {
    let h = 0x811c9dc5;
    for (let i = 0; i < word.length; i++) {
      h ^= word.charCodeAt(i);
      h = Math.imul(h, 0x01000193);
    }
    return (h >>> 0) % BUCKETS;
  });
}

export function embedRaw(ids, lut) {
  const n = ids.length || 1;
  const acc = new Float32Array(DIM);
  for (let d = 0; d < DIM; d++) {
    let sum = 0;
    for (let i = 0; i < ids.length; i++) {
      const v = codeAt(lut, ids[i], d);
      let lv = 0;
      if (ids.length > 1) {
        if (i === 0) lv = v - codeAt(lut, ids[1], d);
        else if (i + 1 === ids.length) lv = v - codeAt(lut, ids[i - 1], d);
        else lv = 2 * v - codeAt(lut, ids[i - 1], d) - codeAt(lut, ids[i + 1], d);
      }
      sum += v - 0.5 * lv;
    }
    acc[d] = sum / n;
  }
  return acc;
}

export function normalize(acc) {
  let norm = 0;
  for (let i = 0; i < acc.length; i++) norm += acc[i] * acc[i];
  norm = Math.sqrt(norm) || 1;
  const out = new Float32Array(acc.length);
  for (let i = 0; i < acc.length; i++) out[i] = acc[i] / norm;
  return out;
}

export function embedTokens(ids, lut) {
  return normalize(embedRaw(ids, lut));
}

export function embedText(text, lut) {
  return embedTokens(tokensOf(text), lut);
}

export function cosine(a, b) {
  let dot = 0, na = 0, nb = 0;
  const n = Math.min(a.length, b.length);
  for (let i = 0; i < n; i++) {
    dot += a[i] * b[i];
    na += a[i] * a[i];
    nb += b[i] * b[i];
  }
  return dot / Math.sqrt(na * nb + 1e-12);
}

function safetensorU8(name, bytes, shape) {
  const header = {};
  header[name] = { dtype: "U8", shape, data_offsets: [0, bytes.length] };
  const json = new TextEncoder().encode(JSON.stringify(header));
  const out = new Uint8Array(8 + json.length + bytes.length);
  new DataView(out.buffer).setBigUint64(0, BigInt(json.length), true);
  out.set(json, 8);
  out.set(bytes, 8 + json.length);
  return out;
}

export function lutBytes(lut) {
  return new Uint8Array(lut.buffer, lut.byteOffset, lut.byteLength);
}

function packRawModule(raw) {
  const body = {
    orig_size: raw.length,
    sha256: sha256(raw),
    segments: [{
      name: "embed.lut",
      method: 0,
      raw_off: 0,
      raw_len: raw.length,
      off: 0,
      len: raw.length,
      shape: null,
      sha256: sha256(raw),
    }],
  };
  const ij = new TextEncoder().encode(JSON.stringify(body));
  const out = new Uint8Array(12 + ij.length + raw.length);
  out.set([0x42, 0x4e, 0x5a, 0x32], 0);
  new DataView(out.buffer).setBigUint64(4, BigInt(ij.length), true);
  out.set(ij, 12);
  out.set(raw, 12 + ij.length);
  return out;
}

export function packEmbedModule(lut) {
  const bytes = lutBytes(lut);
  const safe = safetensorU8("embed.lut", bytes, [BUCKETS, DIM / 4]);
  return packRawModule(safe);
}

export function unpackEmbedModule(bnz) {
  const safe = decompressSafetensors(bnz);
  const n = Number(new DataView(safe.buffer, safe.byteOffset, safe.byteLength).getBigUint64(0, true));
  const payload = safe.subarray(8 + n);
  const copy = payload.slice();
  return new Uint32Array(copy.buffer, copy.byteOffset, copy.byteLength / 4);
}

export async function embedGpu(ids, lut, shader) {
  if (!navigator.gpu) throw new Error("no gpu");
  const adapter = await navigator.gpu.requestAdapter();
  if (!adapter) throw new Error("no adapter");
  const device = await adapter.requestDevice();
  const module = device.createShaderModule({ code: shader });
  const info = await module.getCompilationInfo();
  const bad = info.messages.filter((m) => m.type === "error");
  if (bad.length) throw new Error(bad.map((m) => m.message).join("\n"));
  const tokens = new Uint32Array(ids);
  const tokenBuf = device.createBuffer({ size: Math.max(16, tokens.byteLength), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
  const lutBuf = device.createBuffer({ size: lut.byteLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
  const outBuf = device.createBuffer({ size: DIM * 4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC });
  const readBuf = device.createBuffer({ size: DIM * 4, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ });
  device.queue.writeBuffer(tokenBuf, 0, tokens);
  device.queue.writeBuffer(lutBuf, 0, lut);
  const bind = device.createBindGroupLayout({
    entries: [
      { binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "read-only-storage" } },
      { binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "read-only-storage" } },
      { binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
    ],
  });
  const layout = device.createPipelineLayout({ bindGroupLayouts: [bind] });
  const pipe = device.createComputePipeline({
    layout,
    compute: { module, entryPoint: "main", constants: { N: ids.length } },
  });
  const group = device.createBindGroup({
    layout: bind,
    entries: [
      { binding: 0, resource: { buffer: tokenBuf } },
      { binding: 1, resource: { buffer: lutBuf } },
      { binding: 2, resource: { buffer: outBuf } },
    ],
  });
  const enc = device.createCommandEncoder();
  const pass = enc.beginComputePass();
  pass.setPipeline(pipe);
  pass.setBindGroup(0, group);
  pass.dispatchWorkgroups(1);
  pass.end();
  enc.copyBufferToBuffer(outBuf, 0, readBuf, 0, DIM * 4);
  device.queue.submit([enc.finish()]);
  await readBuf.mapAsync(GPUMapMode.READ);
  const raw = new Float32Array(readBuf.getMappedRange()).slice();
  readBuf.unmap();
  return normalize(raw);
}