Spaces:
Running
Running
File size: 4,803 Bytes
48441eb | 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 | // 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));
}
|