velt/half
API referenceArrays of the 16-bit storage floats f16 and bf16 in bulk: loading them from the bytes of a model file, widening them to f32[] and narrowing them back, and the dot product of two of them, at the speed of the CPU's conversion instructions. For a walkthrough, read the guide.
Exports
count little-endian 16-bit patterns in bytes from byte at on, as a packed f16[]: one copy of 2 * count bytes, every pattern kept (NaN payloads too).bf16FromBytesAs f16FromBytes, for bf16 (the top 16 bits of an f32): the count little-endian patterns from byte at on, one copy.widenF16Each element of ws as an f32: exact, NaN payloads kept (their quiet bit set).widenF16IntoWrites each element of ws as an f32 to xs[at..at + ws.length), allocating nothing.widenBf16Each element of ws as an f32: exact (a bf16 is an f32's top half).widenBf16IntoAs widenF16Into, for bf16.narrowF16Each element of xs rounded to the nearest f16 (ties to even; overflow to ±Infinity), NaN payloads kept as far as they fit.narrowF16IntoWrites each element of xs, rounded to f16, to ws[at..at + xs.length), allocating nothing.narrowBf16Each element of xs rounded to the nearest bf16 (ties to even), NaN payloads kept.narrowBf16IntoAs narrowF16Into, for bf16.dotF16The dot product of a and b: a[0] * b[0] + a[1] * b[1] + …, each product exact in f32, accumulated in f32 lanes by f16x8.dotAdd and summed in number at the end, the same number on every CPU.dotBf16The dot product of a and b, as dotF16: accumulated in f32 lanes by bf16x8.dotAdd, the same number on every CPU, with no use of a CPU's bf16 dot product, whose rounding differs (a loop over bf16x8.dotAddFast has it).Re-exported from velt/half/bytes
function f16FromBytes(
bytes: u8[],
at: usize = 0,
count: usize | null = null,
): f16[]The count little-endian 16-bit patterns in bytes from byte at on, as a packed f16[]:
one copy of 2 * count bytes, every pattern kept (NaN payloads too). count defaults to
all the whole patterns after at, so an odd byte left over is not read. Panics with a
RangeError when at + 2 * count passes the end of bytes, as u8x16.load does. For
big-endian data, swap each pair first (u16x8.reverseBytes, or by hand).
Re-exported from velt/half/bytes
function bf16FromBytes(
bytes: u8[],
at: usize = 0,
count: usize | null = null,
): bf16[]As f16FromBytes, for bf16 (the top 16 bits of an f32): the count little-endian
patterns from byte at on, one copy.
Re-exported from velt/half/widen
function widenF16(ws: f16[]): f32[]Each element of ws as an f32: exact, NaN payloads kept (their quiet bit set).
Re-exported from velt/half/widen
function widenF16Into(ws: f16[], xs: f32[], at: usize = 0)Writes each element of ws as an f32 to xs[at..at + ws.length), allocating nothing.
Panics with a RangeError when that range passes the end of xs.
Re-exported from velt/half/widen
function widenBf16(ws: bf16[]): f32[]Each element of ws as an f32: exact (a bf16 is an f32's top half).
Re-exported from velt/half/widen
function widenBf16Into(ws: bf16[], xs: f32[], at: usize = 0)As widenF16Into, for bf16.
Re-exported from velt/half/narrow
function narrowF16(xs: f32[]): f16[]Each element of xs rounded to the nearest f16 (ties to even; overflow to ±Infinity), NaN
payloads kept as far as they fit.
Re-exported from velt/half/narrow
function narrowF16Into(xs: f32[], ws: f16[], at: usize = 0)Writes each element of xs, rounded to f16, to ws[at..at + xs.length), allocating
nothing. Panics with a RangeError when that range passes the end of ws.
Re-exported from velt/half/narrow
function narrowBf16(xs: f32[]): bf16[]Each element of xs rounded to the nearest bf16 (ties to even), NaN payloads kept.
Re-exported from velt/half/narrow
function narrowBf16Into(xs: f32[], ws: bf16[], at: usize = 0)As narrowF16Into, for bf16.
Re-exported from velt/half/dot
function dotF16(a: f16[], b: f16[]): numberThe dot product of a and b: a[0] * b[0] + a[1] * b[1] + …, each product exact in f32,
accumulated in f32 lanes by f16x8.dotAdd and summed in number at the end, the same number
on every CPU. Panics with a RangeError when the lengths differ.
Re-exported from velt/half/dot
function dotBf16(a: bf16[], b: bf16[]): numberThe dot product of a and b, as dotF16: accumulated in f32 lanes by bf16x8.dotAdd, the
same number on every CPU, with no use of a CPU's bf16 dot product, whose rounding differs (a loop
over bf16x8.dotAddFast has it). Panics with a RangeError when the lengths differ.