solstice-80m-webgpu / js /embeddings.js
thalismind
Claude Opus 5.5
feat: Solstice 80M and Nyx 36M in the browser with prompt sweeps, embedding arithmetic and runtime LoRAs
48441eb
Raw History Blame Contribute Delete
3.04 kB
// Precomputed prompt embeddings (tools/build_embeddings.py): data/<model>.json index + data/<model>.bin.
// Per entry: int8 tokens [len, D], float16 scale [len], float16 pool [D]. Entry 0 is the unconditional prompt.
import ndarray from 'https://cdn.jsdelivr.net/npm/ndarray@1.0.19/+esm';
const F16 = new Float32Array(65536);
(function buildF16Table() {
for (let h = 0; h < 65536; h++) {
const s = h & 0x8000 ? -1 : 1;
const e = (h >> 10) & 0x1f;
const f = h & 0x3ff;
F16[h] = e === 0 ? s * 2 ** -14 * (f / 1024) : e === 31 ? (f ? NaN : s * Infinity) : s * 2 ** (e - 15) * (1 + f / 1024);
}
})();
function halfs(bytes, offset, n) {
const u16 = new Uint16Array(bytes.buffer, bytes.byteOffset + offset, n);
const out = new Float32Array(n);
for (let i = 0; i < n; i++) out[i] = F16[u16[i]];
return out;
}
// A conditioning: tokens [L, D] (zero past len), the number of attended positions, pool [D].
// `tokens` and `pool` are ndarrays over Float32Arrays.
export function makeCond(tokens, len, pool) {
return { tokens, len, pool };
}
export class EmbeddingStore {
constructor(meta, bytes) {
this.meta = meta;
this.bytes = bytes;
this.L = meta.tokens;
this.D = meta.dim;
this.allMask = meta.mask === 'all';
this.byKey = new Map(meta.entries.map((e, i) => [e.key, i]));
this.cache = new Map();
}
static async load(name, onProgress) {
const meta = await (await fetch(`data/${name}.json`)).json();
const res = await fetch(`data/${name}.bin`);
if (!res.ok) throw new Error(`HTTP ${res.status} fetching data/${name}.bin`);
const bytes = new Uint8Array(await res.arrayBuffer());
if (onProgress) onProgress(1);
return new EmbeddingStore(meta, bytes);
}
has(key) {
return this.byKey.has(key);
}
get keys() {
return this.meta.entries.slice(1).map((e) => e.key);
}
uncond() {
return this.getIndex(0);
}
get(key) {
const i = this.byKey.get(key);
if (i === undefined) throw new Error(`"${key}" is not in the precomputed prompt library`);
return this.getIndex(i);
}
getIndex(i) {
if (this.cache.has(i)) return this.cache.get(i);
const e = this.meta.entries[i];
const { L, D } = this;
const n = Math.max(1, e.len);
const q = new Int8Array(this.bytes.buffer, this.bytes.byteOffset + e.offset, n * D);
const scale = halfs(this.bytes, e.offset + n * D, n);
const pool = halfs(this.bytes, e.offset + n * D + 2 * n, D);
const tok = new Float32Array(L * D);
for (let r = 0; r < n; r++) {
const s = scale[r];
for (let c = 0; c < D; c++) tok[r * D + c] = q[r * D + c] * s;
}
const cond = makeCond(ndarray(tok, [L, D]), this.allMask ? L : e.len, ndarray(pool, [D]));
this.cache.set(i, cond);
return cond;
}
// A live-encoded conditioning (Nyx's SigLIP) in the same shape as the store's entries
fromArrays(tokens, pool) {
return makeCond(ndarray(Float32Array.from(tokens), [this.L, this.D]), this.L, ndarray(Float32Array.from(pool), [this.D]));
}
}