aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-infer / src / math.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Scalar f32 math without libm (no_std). |
| 17 | //! |
| 18 | //! `exp_approx`, `sqrt_approx` and `sin_cos` are adapted from the retired |
| 19 | //! kernel crate's `crates/aienos-kernel/src/infer/{gemm,rope}.rs` (same |
| 20 | //! authors, Apache-2.0); `exp` got one more series term and `ln` is new. |
| 21 | |
| 22 | use core::f32::consts::{FRAC_PI_2, LN_2, LOG2_E, PI, TAU}; |
| 23 | |
| 24 | /// Cody-Waite split of ln 2: the high part is exact in f32 (355/512), so the |
| 25 | /// reduction `x - n*LN_2_HI` is exact for large |n|; the low part restores precision. |
| 26 | #[allow(clippy::excessive_precision)] // exact f32 (355/512), written in full on purpose |
| 27 | const LN_2_HI: f32 = 0.693_359_375; |
| 28 | const LN_2_LO: f32 = -2.121_944_4e-4; |
| 29 | |
| 30 | /// Newton square root from a bit-trick seed (error ~1 ulp). |
| 31 | pub fn sqrt(x: f32) -> f32 { |
| 32 | if x <= 0.0 { |
| 33 | return 0.0; |
| 34 | } |
| 35 | let mut y = f32::from_bits((x.to_bits() >> 1) + 0x1fc0_0000); |
| 36 | for _ in 0..5 { |
| 37 | y = 0.5 * (y + x / y); |
| 38 | } |
| 39 | y |
| 40 | } |
| 41 | |
| 42 | /// `e^x` by range reduction to `|r| <= ln2/2` and a degree-6 series |
| 43 | /// (relative error below 2e-7 on [-87, 88]). |
| 44 | pub fn exp(x: f32) -> f32 { |
| 45 | let x = x.clamp(-87.0, 88.0); |
| 46 | let scaled = x * LOG2_E; |
| 47 | let n = if scaled >= 0.0 { |
| 48 | (scaled + 0.5) as i32 |
| 49 | } else { |
| 50 | (scaled - 0.5) as i32 |
| 51 | }; |
| 52 | // Cody-Waite two-part ln2 keeps the reduction exact for large |n|. |
| 53 | let r = (x - n as f32 * LN_2_HI) - n as f32 * LN_2_LO; |
| 54 | let p = 1.0 |
| 55 | + r * (1.0 |
| 56 | + r * (0.5 + r * (1.0 / 6.0 + r * (1.0 / 24.0 + r * (1.0 / 120.0 + r / 720.0))))); |
| 57 | p * f32::from_bits(((n + 127) as u32) << 23) |
| 58 | } |
| 59 | |
| 60 | /// Natural log for positive normal `x` (frexp then atanh series on |
| 61 | /// the mantissa folded to [sqrt(1/2), sqrt(2)]; error below 1e-7). |
| 62 | pub fn ln(x: f32) -> f32 { |
| 63 | let bits = x.to_bits(); |
| 64 | let mut e = ((bits >> 23) & 0xff) as i32 - 127; |
| 65 | let mut m = f32::from_bits((bits & 0x7f_ffff) | (127 << 23)); |
| 66 | if m > core::f32::consts::SQRT_2 { |
| 67 | m *= 0.5; |
| 68 | e += 1; |
| 69 | } |
| 70 | let y = (m - 1.0) / (m + 1.0); |
| 71 | let y2 = y * y; |
| 72 | let s = y |
| 73 | * (1.0 |
| 74 | + y2 * (1.0 / 3.0 |
| 75 | + y2 * (1.0 / 5.0 + y2 * (1.0 / 7.0 + y2 * (1.0 / 9.0 + y2 / 11.0))))); |
| 76 | 2.0 * s + e as f32 * LN_2 |
| 77 | } |
| 78 | |
| 79 | /// `base^e` for positive `base`. |
| 80 | pub fn powf(base: f32, e: f32) -> f32 { |
| 81 | exp(e * ln(base)) |
| 82 | } |
| 83 | |
| 84 | /// `(sin, cos)`. Reduce to [-pi, pi], fold to [-pi/2, pi/2], Taylor series |
| 85 | /// (about 1e-6 absolute). |
| 86 | pub fn sin_cos(angle: f32) -> (f32, f32) { |
| 87 | let mut r = (angle + PI) % TAU; |
| 88 | if r < 0.0 { |
| 89 | r += TAU; |
| 90 | } |
| 91 | let mut x = r - PI; |
| 92 | let mut cos_sign = 1.0; |
| 93 | if x > FRAC_PI_2 { |
| 94 | x = PI - x; |
| 95 | cos_sign = -1.0; |
| 96 | } else if x < -FRAC_PI_2 { |
| 97 | x = -PI - x; |
| 98 | cos_sign = -1.0; |
| 99 | } |
| 100 | let x2 = x * x; |
| 101 | let sin = x |
| 102 | * (1.0 |
| 103 | + x2 * (-1.0 / 6.0 |
| 104 | + x2 * (1.0 / 120.0 + x2 * (-1.0 / 5040.0 + x2 * (1.0 / 362_880.0))))); |
| 105 | let cos = 1.0 |
| 106 | + x2 * (-1.0 / 2.0 |
| 107 | + x2 * (1.0 / 24.0 + x2 * (-1.0 / 720.0 + x2 * (1.0 / 40_320.0 - x2 / 3_628_800.0)))); |
| 108 | (sin, cos_sign * cos) |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod tests { |
| 113 | use super::*; |
| 114 | |
| 115 | fn rel(a: f32, b: f32) -> f32 { |
| 116 | (a - b).abs() / b.abs().max(1e-30) |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn exp_matches_std() { |
| 121 | let mut x = -80.0f32; |
| 122 | while x < 80.0 { |
| 123 | assert!(rel(exp(x), x.exp()) < 5e-7, "exp({x})"); |
| 124 | x += 0.173; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn ln_matches_std() { |
| 130 | let mut x = 1e-6f32; |
| 131 | while x < 1e6 { |
| 132 | let (a, b) = (ln(x), x.ln()); |
| 133 | assert!((a - b).abs() < 2e-6 * b.abs().max(1.0), "ln({x}) {a} {b}"); |
| 134 | x *= 1.37; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn powf_rope_theta_scale() { |
| 140 | // llama.cpp: theta_scale = powf(freq_base, -2/n_rot) |
| 141 | let (a, b) = (powf(500000.0, -2.0 / 64.0), 500000f32.powf(-2.0 / 64.0)); |
| 142 | assert!(rel(a, b) < 1e-6, "{a} {b}"); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn sqrt_matches_std() { |
| 147 | for &x in &[1e-8f32, 0.25, 1.0, 2.0, 1234.5, 1e9] { |
| 148 | assert!(rel(sqrt(x), x.sqrt()) < 2e-7, "sqrt({x})"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn sin_cos_matches_std_and_unit_norm() { |
| 154 | let mut a = -50.0f32; |
| 155 | while a < 600.0 { |
| 156 | let (s, c) = sin_cos(a); |
| 157 | assert!((s - a.sin()).abs() < 3e-5, "sin({a})"); |
| 158 | assert!((c - a.cos()).abs() < 3e-5, "cos({a})"); |
| 159 | assert!((s * s + c * c - 1.0).abs() < 1e-5); |
| 160 | a += 0.37; |
| 161 | } |
| 162 | } |
| 163 | } |