From cb5b33c7d391b2beb1a14105ffe765c4930660b1 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 31 Oct 2023 16:55:38 -0400 Subject: [PATCH] wgsl: Convert `quantizeToF32` to used `Math.fround` (#3119) Instead of passing the input through a F32Array, use the builtin Math.fround. This leads to a ~5% improvement benchmarking locally. This is less than the equivalent f16 change, because F32Array is provided by the runtime, whereas F16Array is being polyfilled, so is probably more efficient to begin with. --- src/webgpu/util/math.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/webgpu/util/math.ts b/src/webgpu/util/math.ts index 018d350a984d..9b901bfa6da4 100644 --- a/src/webgpu/util/math.ts +++ b/src/webgpu/util/math.ts @@ -2013,13 +2013,9 @@ export interface QuantizeFunc { (num: number): number; } -/** Statically allocate working data, so it doesn't need per-call creation */ -const quantizeToF32Data = new Float32Array(new ArrayBuffer(4)); - /** @returns the closest 32-bit floating point value to the input */ export function quantizeToF32(num: number): number { - quantizeToF32Data[0] = num; - return quantizeToF32Data[0]; + return Math.fround(num); } /** @returns the closest 16-bit floating point value to the input */