Skip to content

Commit fc58db8

Browse files
authored
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.
1 parent ccee5a9 commit fc58db8

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/webgpu/util/math.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert } from '../../common/util/util.js';
33
import {
44
Float16Array,
55
getFloat16,
6+
hfround,
67
setFloat16,
78
} from '../../external/petamoriken/float16/float16.js';
89

@@ -2021,13 +2022,9 @@ export function quantizeToF32(num: number): number {
20212022
return quantizeToF32Data[0];
20222023
}
20232024

2024-
/** Statically allocate working data, so it doesn't need per-call creation */
2025-
const quantizeToF16Data = new Float16Array(new ArrayBuffer(2));
2026-
20272025
/** @returns the closest 16-bit floating point value to the input */
20282026
export function quantizeToF16(num: number): number {
2029-
quantizeToF16Data[0] = num;
2030-
return quantizeToF16Data[0];
2027+
return hfround(num);
20312028
}
20322029

20332030
/** Statically allocate working data, so it doesn't need per-call creation */

0 commit comments

Comments
 (0)