==> include/mesh/ref/gpt2_scalar.h <==1/* @ajax/mesh-ref-gpt2-scalar: GPT-2's scalar functions built from + - * / and integer bit operations,
2 * every float op in the cluster float mode Z (mesh-ref gpt2.rs), so every executor computes the same
3 * bits. The constants as f32 bits (the Metal port writes the same words). Freestanding. */
4#ifndef MESH_REF_GPT2_SCALAR_H
5#define MESH_REF_GPT2_SCALAR_H
6
7#include <stdint.h>
8
9#define MESH_GPT2_ATTN_SCALE_BITS 0x3e000000u /* 0.125 = 1 / sqrt(head_dim) */
10#define MESH_GPT2_LN_EPS_BITS 0x3727c5acu /* 1e-5 */
11#define MESH_GPT2_GELU_C_BITS 0x3f4c422au /* sqrt(2 / pi) = 0.7978846 */
12#define MESH_GPT2_GELU_A_BITS 0x3d372713u /* 0.044715 */
13#define MESH_GPT2_LOG2E_BITS 0x3fb8aa3bu /* 1.442695 */
14#define MESH_GPT2_LN2_HI_BITS 0x3f317200u /* 0.69314575 */
15#define MESH_GPT2_LN2_LO_BITS 0x35bfbe8eu /* 1.4286068e-6 */
16/* exp's Horner coefficients 1/10!, 1/9!, .., 1/2!, 1/1!, 1/0!. */
17extern const uint32_t mesh_gpt2_exp_poly_coeffs[11];
18
19/* exp: k = trunc(x log2e +- 0.5), r = (x - k ln2_hi) - k ln2_lo, a degree-10 Horner polynomial, scaled
20 * by 2^k in two exact steps. 0 below -87, inf above 88. */
21float mesh_gpt2_exp_poly(float x);
22/* tanh = 1 - 2 / (exp(2x) + 1); +-1 beyond |x| > 10. */
23float mesh_gpt2_tanh_poly(float x);
24/* The tanh-form GELU ("gelu_new"): (0.5 x) (1 + tanh(c (x + ((a x) x) x))). */
25float mesh_gpt2_gelu_tanh(float x);
26/* sqrt of a > 0: a bit-shift first guess, then 4 Newton steps y = 0.5 (y + a / y). */
27float mesh_gpt2_sqrt_newton(float a);
28
29#endif