math_fp32.bend checks
raw source on the hub · import stelliferous@0.0.3.0/math_fp32.bend as Math_fp32
Ordered FP32 arithmetic on the shared sequence model. No separate container.
2 imports
import Base import ./math_sequence.bend as Sequence
Types
type Argument source · line 110 · 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.
Argument@x:F32 -> @head:F32 -> @low:F32 -> Argument
type DerivativeArgument source · line 299 · raw
Data
DerivativeArgument@x:F32 -> @value:F32 -> @scaled:F32 -> DerivativeArgument
type GeluPrepared source · line 338 · raw
Data
GeluPrepared@x:F32 -> @head:F32 -> @low:F32 -> @slope:F32 -> @slope_error:F32 -> GeluPrepared
type GeluReduced source · line 355 · raw
Data
GeluReduced@x:F32 -> @power:F32 -> @denominator:F32 -> @error:F32 -> @slope:F32 -> @slope_error:F32 -> GeluReduced
Definitions
def multiply_add source · line 5 · raw
@accumulator:F32 -> @input:F32 -> @weight:F32 -> F32
def weighted_add source · line 8 · raw
@weight:F32 -> @accumulator:F32 -> @input:F32 -> F32
def zip_mac source · line 11 · raw
@length:Nat -> @accumulator:0xd6aaab858a4fbd1461bd0fee27b97184/math_sequence.Values(F32, length) -> @input:0xd6aaab858a4fbd1461bd0fee27b97184/math_sequence.Values(F32, length) -> @weight:F32 -> 0xd6aaab858a4fbd1461bd0fee27b97184/math_sequence.Values(F32, length)
def exponential_polynomial source · line 15 · raw
@+r:F32 -> F32
The Cephes expf polynomial after its constant and linear terms.
def from_bits source · line 19 · raw
@value:U32 -> F32
The 32-bit word of a U32, read as an F32, and back.
def to_bits source · line 23 · raw
@value:F32 -> U32
def exponential_remainder source · line 29 · 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 36 · 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 39 · raw
@+mask:U32 -> @yes:F32 -> @+no:F32 -> F32
def silu_finish source · line 44 · 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 50 · raw
@silu:Bool -> @x:F32 -> @value:F32 -> @power:F32 -> @denominator:F32 -> F32
def sigmoid_reduced source · line 62 · 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 69 · raw
@silu:Bool -> @x:F32 -> @+value:F32 -> F32
def sigmoid source · line 75 · 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 78 · raw
@+x:F32 -> @enabled:Bool -> F32
def split_high source · line 87 · 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 93 · 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 97 · raw
@x:F32 -> @+value:F32 -> @low:F32 -> @+biased:F32 -> F32
def gelu_argument source · line 113 · raw
@+x:F32 -> @+c:F32 -> Argument
def gelu_negative_input source · line 133 · 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 138 · raw
@arg:Argument -> F32
def gelu_prepared source · line 148 · 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 151 · raw
@x:F32 -> F32
def tanh_finish source · line 156 · 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 164 · 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 169 · 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 173 · raw
@+r:F32 -> F32
One additional term for the expm1 correction in the tanh quotient.
def tanh_exponential source · line 176 · raw
@+a:F32 -> @+biased:F32 -> @x:F32 -> F32
def tanh_input source · line 186 · raw
@+a:F32 -> @x:F32 -> F32
def tanh source · line 189 · raw
@+x:F32 -> F32
def exponential_scale source · line 195 · 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 201 · raw
@+r:F32 -> F32
FastTwoSum retains the low bits of 1+r before adding the quadratic tail.
def exponential_reduced source · line 206 · raw
@+x:F32 -> @+biased:F32 -> F32
def exponential_input source · line 213 · raw
@+x:F32 -> F32
def exp source · line 216 · raw
@x:F32 -> F32
def logarithm_series source · line 224 · 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 243 · 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 251 · 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 259 · raw
@+x:F32 -> F32
def log source · line 263 · raw
@x:F32 -> F32
def sqrt source · line 267 · raw
@x:F32 -> F32
The square root rounded to nearest, as IEEE 754 requires of every backend.
def sigmoid_derivative source · line 271 · raw
@x:F32 -> F32
Cancellation-free even derivatives with one exponential reduction.
def tanh_derivative source · line 275 · raw
@x:F32 -> F32
def silu_derivative_root source · line 281 · 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 291 · raw
@+value:F32 -> @+scaled:F32 -> F32
def silu_derivative_prepared source · line 302 · raw
@+x:F32 -> @+value:F32 -> DerivativeArgument
def silu_derivative_prepare source · line 310 · raw
@+x:F32 -> DerivativeArgument
def silu_derivative_finish source · line 313 · raw
@arg:DerivativeArgument -> F32
def silu_derivative source · line 320 · raw
@x:F32 -> F32
def gelu_derivative_root source · line 325 · 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 341 · raw
@+c:F32 -> @arg:Argument -> GeluPrepared
def gelu_derivative_prepare source · line 351 · raw
@+x:F32 -> GeluPrepared
def gelu_derivative_exponential source · line 358 · raw
@arg:GeluPrepared -> GeluReduced
def gelu_derivative_evaluated source · line 376 · raw
@arg:GeluReduced -> DerivativeArgument
Correct the quotient before the final power-of-two scaling.
def gelu_derivative_small source · line 390 · raw
@+x:F32 -> F32
Split the linear term before adding the odd correction to one half.
def gelu_derivative_finish source · line 400 · raw
@arg:DerivativeArgument -> F32
def gelu_tanh_derivative source · line 411 · raw
@x:F32 -> F32