GPT-2's scalar functions from + - * / in the cluster float mode: exp by a degree-10 polynomial, tanh, the tanh GELU, Newton sqrt
$ sexc add @ajax/mesh-ref-gpt2-scalar- total 3
- drwxr-xr-x-../
- drwxr-xr-x-Oct 27 01:59v/# 1 version
- drwxr-xr-x-Oct 27 01:59files/# the files of 0.1.0
- latest
- 0.1.0, published Oct 27 01:59 by ajax
- owners
- ajax
- license
- MIT
- dependencies
- @ajax/mesh-ftz ^0.1.0
- dependents
- @ajax/mesh-prog-gpt2x, @ajax/mesh-ref-gpt2-attend, @ajax/mesh-ref-gpt2-linear, @ajax/mesh-ref-gpt2-norm
- keywords
- alphaos mesh reference gpt2 determinism
- homepage
- https://github.com/thomasdavis/alpha-c-mesh/tree/main/packages/mesh-ref-gpt2-scalar
- repository
- https://github.com/thomasdavis/alpha-c-mesh
1# @ajax/mesh-ref-gpt2-scalar 2 3GPT-2's scalar functions built only from `+ - * /` and integer bit operations, every float operation in 4the cluster float mode, so every executor (CPU, CUDA, Metal, wasm) computes the same bits: `exp` (range 5reduction by k·ln 2 in two parts, a degree-10 Horner polynomial, 2^k in two exact steps), `tanh` from it, 6the tanh-form GELU, and a Newton square root. The constants are f32 bit patterns. 7 8**Ported from** `crates/mesh-ref/src/gpt2.rs` (`exp_poly`, `tanh_poly`, `gelu_tanh`, `sqrt_newton`, the 9constants). Freestanding. 10 11**Tested** by gpt2.rs's `constants_are_their_values` and `scalar_functions_are_close` (the double 12references computed without libm), and a golden digest from the Rust over 300 000 inputs of each 13function (random bits, tiny exponents, ±150). 14 15## Differences 16 17- `exp_poly` of a NaN: Rust's `as i32` turns NaN into 0, where a C cast is undefined, so it is tested 18 first and mapped to 0 as well.