Spaces:
Running
Running
File size: 3,139 Bytes
8f9eb25 f916d7c | 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 | // Small numeric helpers: fp16 <-> fp32, seeded Gaussian noise.
const f32 = new Float32Array(1), u32 = new Uint32Array(f32.buffer);
export function toHalf(src) { // Float32Array -> Uint16Array (round to nearest even)
const out = new Uint16Array(src.length);
for (let i = 0; i < src.length; i++) {
f32[0] = src[i]; const x = u32[0];
const sign = (x >>> 16) & 0x8000; let exp = ((x >>> 23) & 0xff) - 112; let mant = x & 0x7fffff;
if (exp <= 0) { // subnormal / zero
if (exp < -10) { out[i] = sign; continue; }
mant |= 0x800000; const shift = 14 - exp; let h = mant >> shift;
const rem = mant & ((1 << shift) - 1), half = 1 << (shift - 1);
if (rem > half || (rem === half && (h & 1))) h++;
out[i] = sign | h; continue;
}
if (exp >= 31) { out[i] = sign | (((x >>> 23) & 0xff) === 0xff && mant ? 0x7e00 : 0x7c00); continue; }
let h = sign | (exp << 10) | (mant >> 13);
const rem = mant & 0x1fff;
if (rem > 0x1000 || (rem === 0x1000 && (h & 1))) h++; // round half to even (carry may reach inf, as it should)
out[i] = h;
}
return out;
}
export function fromHalf(src) { // Uint16Array -> Float32Array
const out = new Float32Array(src.length);
for (let i = 0; i < src.length; i++) {
const h = src[i], s = h & 0x8000 ? -1 : 1, e = (h >> 10) & 0x1f, m = h & 0x3ff;
out[i] = e === 0 ? s * m * 2 ** -24 : e === 31 ? (m ? NaN : s * Infinity) : s * (1 + m / 1024) * 2 ** (e - 15);
}
return out;
}
/** Deterministic standard-normal noise (mulberry32 + Box-Muller). Not bit-identical to torch.randn. */
export function randn(n, seed = 0) {
let a = seed >>> 0;
const rand = () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return (((t ^ (t >>> 14)) >>> 0) + 1) / 4294967297; };
const out = new Float32Array(n);
for (let i = 0; i < n; i += 2) {
const r = Math.sqrt(-2 * Math.log(rand())), th = 2 * Math.PI * rand();
out[i] = r * Math.cos(th); if (i + 1 < n) out[i + 1] = r * Math.sin(th);
}
return out;
}
/** Round float32 values to bfloat16 precision (round to nearest even), as `x.to(torch.bfloat16).float()`. */
export function bf16Round(src, out = new Float32Array(src.length)) {
const f = new Float32Array(1), u = new Uint32Array(f.buffer);
for (let i = 0; i < src.length; i++) {
f[0] = src[i]; const x = u[0];
if ((x & 0x7f800000) === 0x7f800000) { out[i] = src[i]; continue; } // inf / nan unchanged
u[0] = (x + 0x7fff + ((x >>> 16) & 1)) & 0xffff0000; out[i] = f[0];
}
return out;
}
/** torch.linspace(start, end, steps) in float32 (torch's two-sided formula), as a Float32Array. */
export function linspaceF32(start, end, steps) {
const f = Math.fround, out = new Float32Array(steps);
if (steps === 1) { out[0] = start; return out; }
const step = f((end - start) / (steps - 1)), half = Math.floor(steps / 2);
for (let i = 0; i < steps; i++) out[i] = i < half ? f(start + f(step * i)) : f(end - f(step * (steps - 1 - i)));
return out;
}
|