Download source/demo/web/src/local/float16.ts from jackboyla/hendrix: direct link, hf CLI and curl.
- Browser
- Download file 787 Bytes
-
https://huggingface.co/spaces/jackboyla/hendrix/resolve/main/source/demo/web/src/local/float16.ts
- Command line
-
hf download hf://spaces/jackboyla/hendrix/source/demo/web/src/local/float16.ts
-
curl -L -o float16.ts https://huggingface.co/spaces/jackboyla/hendrix/resolve/main/source/demo/web/src/local/float16.ts
787 Bytes
| /** | |
| * Read one float16 value returned by ONNX Runtime Web. | |
| * | |
| * Browsers without Float16Array expose raw IEEE-754 bits in a Uint16Array. | |
| * Safari exposes numeric values through Float16Array instead. Treating those | |
| * numeric values as bit patterns corrupts decoder argmax results. | |
| */ | |
| export function readFloat16(data: ArrayLike<number>, index: number): number { | |
| const value = data[index]; | |
| if (!(data instanceof Uint16Array)) return Number(value); | |
| const sign = value & 0x8000 ? -1 : 1; | |
| const exponent = (value >> 10) & 0x1f; | |
| const fraction = value & 0x3ff; | |
| if (exponent === 0) return sign * 2 ** -14 * (fraction / 1024); | |
| if (exponent === 31) return fraction ? Number.NaN : sign * Number.POSITIVE_INFINITY; | |
| return sign * 2 ** (exponent - 15) * (1 + fraction / 1024); | |
| } | |