~/bend-docscommunity

math_fp32.bend checks

raw source on the hub · import stelliferous@0.0.4.0/math_fp32.bend as Math_fp32

Ordered FP32 arithmetic on the shared sequence model. No separate container.

2 imports
import Base
import ./math_choice.bend as Choice

Types

type Argument source · line 107 · raw

Data

w=2 sqrt(2/pi) c (1+0.044715 c^2). Twelve-bit input/product heads, a nine-bit cube head and short coefficient heads keep the head sum exact for 0.5<=|c|<=16: the linear/cubic products need at most 18/16 bits, and their exponent gap is at most 7/4 bits. Smaller inputs have a small absolute head error, measured separately from the ordered-map theorem. Carry coefficient and cubic tails through c_low^2, omitting c_low^3.

type DerivativeArgument source · line 296 · raw

Data

type GeluPrepared source · line 335 · raw

Data

type GeluReduced source · line 352 · raw

Data

type Gaussian source · line 414 · raw

Data

GELU(x)=x Phi(x). Phi(-a)=exp(-a^2/2) m(a), where m is the Mills ratio over sqrt(2 pi). Twelve-bit heads make h^2/2 exact, the tail of a^2/2 goes into the reduced argument r, and exp(-a^2/2)=2^n (1+e) with e=r+remainder(r).

Definitions

def multiply_add source · line 5 · raw

@accumulator:F32 -> @input:F32 -> @weight:F32 -> F32

def exponential_polynomial source · line 9 · raw

@+r:F32 -> F32

The Cephes expf polynomial after its constant and linear terms.

def from_bits source · line 13 · raw

@value:U32 -> F32

The 32-bit word of a U32, read as an F32, and back.

def to_bits source · line 17 · raw

@value:F32 -> U32

def exponential_remainder source · line 23 · raw

@+r:F32 -> F32

exp(r)-1-r near |r| <= ln(2)/2. Chebyshev coefficients, rounded to F32; paired evaluation shortens the dependency chain without contracting products.

def logistic_denominator source · line 30 · raw

@q:F32 -> @+base:F32 -> @r:F32 -> @correction:F32 -> F32

b=RN(1+q). Recover the small term lost in that sum before adding the reduced exponential. Writing (1-b)+q keeps the eight lanes together under the default vectorizer; q-(b-1) makes it mix unrelated addition trees.

def selected source · line 34 · raw

@+mask:U32 -> @yes:F32 -> @+no:F32 -> F32

The bits of yes where mask is set, the bits of no elsewhere.

def silu_finish source · line 41 · raw

@+x:F32 -> @value:F32 -> F32

Above 18 SiLU rounds to x. Pass x through, including positive infinity; the negative tail uses the clamped input before the final underflow.

def logistic_scaled source · line 47 · raw

@silu:Bool -> @x:F32 -> @value:F32 -> @power:F32 -> @denominator:F32 -> F32

def sigmoid_reduced source · line 59 · raw

@silu:Bool -> @x:F32 -> @+value:F32 -> @+biased:F32 -> F32

u=-value, n approximates u/ln(2) to the nearest integer, r=u-n*ln(2). The exponent word makes H=2^(64-n), so sigmoid(x)=H/(exp(r)+2^-n)*2^-64. H remains normal; only the last multiplication underflows. The tiny denominator addend q may be floored to the least normal F32 without changing its rounded sum.

def sigmoid_input source · line 66 · raw

@silu:Bool -> @x:F32 -> @+value:F32 -> F32

def sigmoid source · line 72 · raw

@+x:F32 -> F32

At x>=18 sigmoid rounds to 1. Below -110 both sigmoid and SiLU round to zero; multiplying the clamped x before the final scaling preserves -0 for SiLU, including at -infinity. NaNs pass the clamp and propagate.

def activate source · line 75 · raw

@+x:F32 -> @enabled:Bool -> F32

def split_high source · line 84 · raw

@+a:F32 -> F32

Twelve-bit heads have exact normal head products; the residual carries the remaining twelve fraction bits.

def gelu_exponential_remainder source · line 90 · raw

@+r:F32 -> F32

Chebyshev interpolation of (exp(r)-1-r)/r^2 on [-0.375,1.0], rounded to F32. The compensated head needs a wider interval than ln(2)/2. Paired evaluation shortens the dependency chain without contracting products.

def gelu_reduced source · line 94 · raw

@x:F32 -> @+value:F32 -> @low:F32 -> @+biased:F32 -> F32

def gelu_argument source · line 110 · raw

@+x:F32 -> @+c:F32 -> Argument

def gelu_negative_input source · line 130 · raw

@+x:F32 -> F32

Positive results already round to x at magnitude >=6. Cap their negative-tail evaluation there to avoid an unnecessary final underflow; negative results keep the full range through magnitude 16.

def gelu_finish source · line 135 · raw

@arg:Argument -> F32

def gelu_prepared source · line 145 · raw

@+x:F32 -> Argument

GELU(x)=x+GELU(-x). Evaluate a negative tail on both sides, then restore the positive result. Clamping before arithmetic keeps infinities out of the products. Negative magnitudes >=16 round to -0. The original word selects the sign, including -0; NaNs propagate.

def gelu_tanh source · line 148 · raw

@x:F32 -> F32

def tanh_finish source · line 153 · raw

@+x:F32 -> @+value:F32 -> F32

Sign restoration and the tiny interval are word selections. For |x|<2^-12, |x-tanh(x)|<|x|^3/3 is below half an F32 ulp, so return x, including -0.

def tanh_magnitude source · line 161 · raw

@+x:F32 -> F32

At |x|>=10, 1-tanh(|x|)<2*exp(-20)<2^-25. Clamping before arithmetic also keeps infinities away from intermediate products; NaNs propagate.

def tanh_small source · line 166 · raw

@+t:F32 -> @+z:F32 -> F32

Chebyshev interpolation of (tanh(sqrt(z))/sqrt(z)-1)/z, |t|<=0.75. The coefficients are F32 values.

def tanh_remainder source · line 170 · raw

@+r:F32 -> F32

One additional term for the expm1 correction in the tanh quotient.

def tanh_exponential source · line 173 · raw

@+a:F32 -> @+biased:F32 -> @x:F32 -> F32

def tanh_input source · line 183 · raw

@+a:F32 -> @x:F32 -> F32

def tanh source · line 186 · raw

@+x:F32 -> F32

def exponential_scale source · line 192 · raw

@biased:F32 -> @x:F32 -> @p:F32 -> F32

Split 2^n at +64 for nonnegative inputs and -64 for negative inputs. The first product is always normal and exact; only the last product can underflow. The exponent words come directly from the biased integer n.

def exponential_precise source · line 198 · raw

@+r:F32 -> F32

FastTwoSum retains the low bits of 1+r before adding the quadratic tail.

def exponential_reduced source · line 203 · raw

@+x:F32 -> @+biased:F32 -> F32

def exponential_input source · line 210 · raw

@+x:F32 -> F32

def exp source · line 213 · raw

@x:F32 -> F32

def logarithm_series source · line 221 · raw

@+r:F32 -> @+n:F32 -> F32

For s near r/(2+r), d = r-(2+r)*s gives the exact real identity log(1+r) = 2*atanh(s) + log(1+d/(1+s)). The odd series ends at s^11. Split r and s into 12-bit heads so their head product is exact; retain the low products in d. (1-s)*(1+s^2+s^4) approximates 1/(1+s). FastTwoSum retains the low bits when adding the exponent's ln(2) part.

def logarithm_normalized source · line 240 · raw

@x:F32 -> @adjustment:F32 -> F32

The exponent carry at mantissa word 0x3504f4 selects the same reduced interval as halving m above sqrt(2), without a floating-point selection.

def logarithm_finish source · line 248 · raw

@+x:F32 -> @value:F32 -> F32

Scaling a subnormal by 2^23 is exact. Zero, negatives, infinities and NaNs are selected explicitly, after normalizing the finite positive input.

def logarithm_input source · line 256 · raw

@+x:F32 -> F32

def log source · line 260 · raw

@x:F32 -> F32

def sqrt source · line 264 · raw

@x:F32 -> F32

The square root rounded to nearest, as IEEE 754 requires of every backend.

def sigmoid_derivative source · line 268 · raw

@x:F32 -> F32

Cancellation-free even derivatives with one exponential reduction.

def tanh_derivative source · line 272 · raw

@x:F32 -> F32

def silu_derivative_root source · line 278 · raw

@+value:F32 -> F32

The derivative divided by the distance from its zero, a degree-eight polynomial on [-2,-0.5] in the exact difference from the head of the zero.

def silu_derivative_tail source · line 288 · raw

@+value:F32 -> @+scaled:F32 -> F32

def silu_derivative_prepared source · line 299 · raw

@+x:F32 -> @+value:F32 -> DerivativeArgument

def silu_derivative_prepare source · line 307 · raw

@+x:F32 -> DerivativeArgument

def silu_derivative_finish source · line 310 · raw

@arg:DerivativeArgument -> F32

def silu_derivative source · line 317 · raw

@x:F32 -> F32

def gelu_derivative_root source · line 322 · raw

@+c:F32 -> F32

Root quotient on [-1.25,-0.5]. Split the linear head exactly and retain the low parts of the first two rounded quotient coefficients.

def gelu_derivative_prepared source · line 338 · raw

@+c:F32 -> @arg:Argument -> GeluPrepared

def gelu_derivative_prepare source · line 348 · raw

@+x:F32 -> GeluPrepared

def gelu_derivative_exponential source · line 355 · raw

@arg:GeluPrepared -> GeluReduced

def gelu_derivative_evaluated source · line 373 · raw

@arg:GeluReduced -> DerivativeArgument

Correct the quotient before the final power-of-two scaling.

def gelu_derivative_small source · line 387 · raw

@+x:F32 -> F32

Split the linear term before adding the odd correction to one half.

def gelu_derivative_finish source · line 397 · raw

@arg:DerivativeArgument -> F32

def gelu_tanh_derivative source · line 408 · raw

@x:F32 -> F32

def gaussian_reduced source · line 417 · raw

@+half:F32 -> @+tail:F32 -> @+biased:F32 -> Gaussian

def gaussian source · line 422 · raw

@+a:F32 -> Gaussian

def gaussian_scaled source · line 429 · raw

@+scale:F32 -> @+quotient:F32 -> @arg:Gaussian -> F32

scale * quotient * exp(-a^2/2), with only the last multiplication able to underflow.

def gelu_center source · line 434 · raw

@+z:F32 -> F32

(Phi(x)-1/2)/x in z=x^2 on |x|<5/8. Chebyshev interpolation, rounded to F32.

def gelu_ratio source · line 439 · raw

@+t:F32 -> F32

a m(a) on [5/8,15] in t=(a-7/4)/(a+7/4), after its constant term. Chebyshev interpolation, rounded to F32.

def gelu_scaled source · line 443 · raw

@+head:F32 -> @+low:F32 -> @arg:Gaussian -> F32

-(head + low) exp(-a^2/2), with only the last multiplication able to underflow.

def gelu_tail source · line 450 · raw

@+a:F32 -> F32

GELU(-a)=-a m(a) exp(-a^2/2). The product a m(a) is one polynomial in t, so neither a nor a division by a+7/4 adds a rounding, and the constant term keeps the low part of its addition.

def gelu source · line 459 · raw

@x:F32 -> F32

PyTorch's gelu: x (1 + erf(x / sqrt 2)) / 2. Below 5/8 in magnitude x (1/2 + x center(x^2)) needs no exponential. Otherwise GELU(x)=x+GELU(-x) restores the positive side from its negative tail, which is capped as in gelu_tanh; the original word selects the sign and NaNs propagate.

def gelu_slope source · line 468 · raw

@+t:F32 -> F32

(m(a) - a / sqrt(2 pi)) / (a0 - a) on [0,15] in t=(a-5/2)/(a+5/2), with a0 the zero of the derivative, 0.7517915...

def gelu_derivative_tail source · line 471 · raw

@+a:F32 -> F32

def gelu_derivative source · line 476 · raw

@x:F32 -> F32

GELU'(-a)=exp(-a^2/2) (a0 - a) slope(t) and GELU'(a)=1-GELU'(-a).