// Embedding-space operations on precomputed conditionings, ported from pixel-art-training // scripts/embed_sheets.py (prompt sweeps, word arithmetic) and scripts/embed_arith_avg.py (averaged directions). // A conditioning is { tokens: ndarray [L, D], len, pool: ndarray [D] } (embeddings.js). import ndarray from 'https://cdn.jsdelivr.net/npm/ndarray@1.0.19/+esm'; import ops from 'https://cdn.jsdelivr.net/npm/ndarray-ops@1.2.2/+esm'; function zeros(shape) { return ndarray(new Float32Array(shape.reduce((a, b) => a * b, 1)), shape); } function clone(a) { return ndarray(Float32Array.from(a.data.subarray(a.offset, a.offset + a.size)), a.shape.slice()); } function row(T, r) { return T.pick(r, null); } function dot(a, b) { const tmp = zeros(a.shape); ops.mul(tmp, a, b); return ops.sum(tmp); } function meanRowNorm(T, n) { let s = 0; for (let r = 0; r < n; r++) s += ops.norm2(row(T, r)); return n > 0 ? s / n : 0; } // Spherical interpolation of two vectors, with the norm interpolated linearly (embed_sheets.slerp). export function slerp(u, v, t, out = zeros(u.shape)) { const nu = Math.max(ops.norm2(u), 1e-6); const nv = Math.max(ops.norm2(v), 1e-6); const c = Math.min(1 - 1e-6, Math.max(-1 + 1e-6, dot(u, v) / (nu * nv))); const om = Math.acos(c); const so = Math.sin(om); const norm = (1 - t) * nu + t * nv; const ku = (Math.sin((1 - t) * om) / so / nu) * norm; const kv = (Math.sin(t * om) / so / nv) * norm; ops.muls(out, u, ku); const tmp = zeros(v.shape); ops.muls(tmp, v, kv); ops.addeq(out, tmp); return out; } // Rows [0, n) rescaled to the norm `ref`, rows past n zeroed. function rescaleRows(T, n, ref) { const L = T.shape[0]; for (let r = 0; r < L; r++) { const x = row(T, r); if (r < n) ops.mulseq(x, ref / Math.max(ops.norm2(x), 1e-6)); else ops.assigns(x, 0); } return T; } function scaledPool(P, refNorm) { ops.mulseq(P, refNorm / Math.max(ops.norm2(P), 1e-6)); return P; } // Prompt sweep: A at t = 0, B at t = 1. Token states are slerped per position over the positions both prompts // have; the longer prompt's extra positions fade in or out linearly; the pool is slerped (embed_sheets.mix). export function mix(A, B, t) { const [L, D] = A.tokens.shape; const T = zeros([L, D]); const both = Math.min(A.len, B.len); for (let r = 0; r < both; r++) slerp(row(A.tokens, r), row(B.tokens, r), t, row(T, r)); for (let r = both; r < A.len; r++) ops.muls(row(T, r), row(A.tokens, r), 1 - t); for (let r = both; r < B.len; r++) ops.muls(row(T, r), row(B.tokens, r), t); const len = Math.max(both, t < 0.5 ? A.len : B.len); return { tokens: T, len, pool: slerp(A.pool, B.pool, t) }; } // Word arithmetic A - B + C (embed_sheets.arith), generalised to A + s * (C - B). Token rows are rescaled to // C's mean token norm at s = 1 (A's at s = 0); the pool to |C| (|A| at s = 0). export function wordArith(A, B, C, s = 1) { const n = Math.max(A.len, B.len, C.len); const T = clone(A.tokens); const d = zeros(T.shape); ops.sub(d, C.tokens, B.tokens); ops.mulseq(d, s); ops.addeq(T, d); const w = Math.min(Math.max(s, 0), 1); const ref = (1 - w) * meanRowNorm(A.tokens, A.len) + w * meanRowNorm(C.tokens, C.len); rescaleRows(T, n, ref); const P = clone(A.pool); const dp = zeros(P.shape); ops.sub(dp, C.pool, B.pool); ops.mulseq(dp, s); ops.addeq(P, dp); scaledPool(P, (1 - w) * ops.norm2(A.pool) + w * ops.norm2(C.pool)); return { tokens: T, len: n, pool: P }; } // Averaged direction (embed_arith_avg.avg_arith): dir = mean_k(E[C obj_k] - E[B obj_k]) per token position and // for the pool, then A + s * dir, token rows rescaled to A's mean token norm, the pool to |A|. export function averagedDirection(pairs) { const [L, D] = pairs[0][0].tokens.shape; const dTok = zeros([L, D]); const dPool = zeros([D]); const tmp = zeros([L, D]); const tmpP = zeros([D]); let n = 0; for (const [B, C] of pairs) { ops.sub(tmp, C.tokens, B.tokens); ops.addeq(dTok, tmp); ops.sub(tmpP, C.pool, B.pool); ops.addeq(dPool, tmpP); n = Math.max(n, B.len, C.len); } ops.mulseq(dTok, 1 / pairs.length); ops.mulseq(dPool, 1 / pairs.length); return { tokens: dTok, pool: dPool, len: n }; } export function applyDirection(A, dir, s = 1) { const n = Math.max(A.len, dir.len); const T = clone(A.tokens); const d = clone(dir.tokens); ops.mulseq(d, s); ops.addeq(T, d); rescaleRows(T, n, meanRowNorm(A.tokens, A.len)); const P = clone(A.pool); const dp = clone(dir.pool); ops.mulseq(dp, s); ops.addeq(P, dp); scaledPool(P, ops.norm2(A.pool)); return { tokens: T, len: n, pool: P }; } export function linspace(a, b, n) { if (n <= 1) return [a]; return Array.from({ length: n }, (_, i) => a + ((b - a) * i) / (n - 1)); }