From fc58db8aad61b990ab734d8450d3500c6988bef7 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 31 Oct 2023 16:42:01 -0400 Subject: [PATCH] wgsl: Convert `quantizeToF16` to used `hfround` (#3118) Instead of passing the input through a F16Array, use the library provided function hfround. hfround is a fast look up table based rounding function for f16. Benchmarking locally this provides a ~20% improvement to fma interval calculations, which are particularly sensitive to quantization cost. Overall I was seeing more on the order of ~10% improvement. --- src/webgpu/util/math.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/webgpu/util/math.ts b/src/webgpu/util/math.ts index 380832e1b857..018d350a984d 100644 --- a/src/webgpu/util/math.ts +++ b/src/webgpu/util/math.ts @@ -3,6 +3,7 @@ import { assert } from '../../common/util/util.js'; import { Float16Array, getFloat16, + hfround, setFloat16, } from '../../external/petamoriken/float16/float16.js'; @@ -2021,13 +2022,9 @@ export function quantizeToF32(num: number): number { return quantizeToF32Data[0]; } -/** Statically allocate working data, so it doesn't need per-call creation */ -const quantizeToF16Data = new Float16Array(new ArrayBuffer(2)); - /** @returns the closest 16-bit floating point value to the input */ export function quantizeToF16(num: number): number { - quantizeToF16Data[0] = num; - return quantizeToF16Data[0]; + return hfround(num); } /** Statically allocate working data, so it doesn't need per-call creation */