demo · v135
Image tensor downsample — fp32 vs fp16
The TC39 proposal calls out machine-learning and graphics pipelines as the primary motivation for Float16Array. Here a 256×256 image is loaded as a normalized tensor (RGB / 255). The middle panel keeps it as Float32Array; the right panel rounds through Float16Array. Compare bytes used and reconstructed pixels.
Float16Array: ?
source (uint8 rgb)
tensor → Float32 → pixels
tensor → Float16 → pixels
fp32 tensor
—
bytes (rgb only)
fp16 tensor
—
bytes (rgb only)
peak abs error
—
per-channel (0..1)
the code
// Real ML preprocessing: HWC uint8 → normalized fp tensor.
const px = ctx.getImageData(0, 0, W, H).data;
const n = W * H * 3;
const fp32 = new Float32Array(n);
const fp16 = new Float16Array(n); // Chrome 135+
for (let i = 0, j = 0; i < px.length; i += 4, j += 3) {
fp32[j ] = px[i ] / 255;
fp32[j + 1] = px[i + 1] / 255;
fp32[j + 2] = px[i + 2] / 255;
}
fp16.set(fp32); // rounds to binary16 on write
console.log(fp32.byteLength, fp16.byteLength); // 49152 vs 24576
see also
- Float16Array — feature index
- Float16Array round-trip — single-value bit-level demo
- ChromeStatus entry
- TC39 proposal — motivation