atlas_core/kimi_k3/
situ.rs1#[inline]
17pub fn sigmoid(x: f32) -> f32 {
18 1.0 / (1.0 + (-x).exp())
19}
20
21#[inline]
23pub fn silu(x: f32) -> f32 {
24 x * sigmoid(x)
25}
26
27#[inline]
29pub fn softcap(x: f32, beta: f32) -> f32 {
30 beta * (x / beta).tanh()
31}
32
33#[inline]
35pub fn situ_gate(x: f32, beta: f32) -> f32 {
36 softcap(x, beta) * sigmoid(x)
37}
38
39#[inline]
41pub fn situ_up(x: f32, beta_lin: f32) -> f32 {
42 softcap(x, beta_lin)
43}
44
45#[inline]
47pub fn situ_glu(gate: f32, up: f32, beta: f32, beta_lin: f32) -> f32 {
48 situ_gate(gate, beta) * situ_up(up, beta_lin)
49}
50
51pub fn situ_glu_vec(gate: &[f32], up: &[f32], beta: f32, beta_lin: f32) -> Vec<f32> {
53 assert_eq!(gate.len(), up.len(), "SiTU-GLU gate/up length mismatch");
54 gate.iter()
55 .zip(up)
56 .map(|(g, u)| situ_glu(*g, *u, beta, beta_lin))
57 .collect()
58}
59
60#[inline]
62pub fn swiglu_mutant(gate: f32, up: f32) -> f32 {
63 gate * sigmoid(gate) * up
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 const B1: f32 = 4.0;
71 const B2: f32 = 25.0;
72 const TOL: f32 = 1e-6;
73
74 #[test]
75 fn situ_glu_matches_closed_form_vector() {
76 let tanh1 = 1.0f32.tanh();
78 let want_gate = B1 * tanh1 * sigmoid(4.0);
79 let want_up = B2 * tanh1;
80 let want = want_gate * want_up;
81 let got = situ_glu(4.0, 25.0, B1, B2);
82 assert!((got - want).abs() < TOL, "closed form {want} vs impl {got}");
83
84 let g = [0.0, 4.0, -2.0, 1.5];
85 let u = [0.0, 25.0, 3.0, -8.0];
86 let out = situ_glu_vec(&g, &u, B1, B2);
87 for i in 0..g.len() {
88 let closed = (B1 * (g[i] / B1).tanh() * sigmoid(g[i])) * (B2 * (u[i] / B2).tanh());
89 assert!(
90 (out[i] - closed).abs() < TOL,
91 "idx {i}: {} vs {closed}",
92 out[i]
93 );
94 }
95 assert_eq!(out[0], 0.0);
97 assert!(out.iter().all(|z| z.abs() < 100.0));
99 }
100
101 #[test]
102 fn situ_beta_zero_mutant_diverges_from_situ() {
103 let g = 4.0f32;
106 let u = 25.0f32;
107 let situ = situ_glu(g, u, B1, B2);
108 let mutant = swiglu_mutant(g, u);
109 assert!(
110 (situ - mutant).abs() > 1.0,
111 "SiTU {situ} must diverge from SwiGLU mutant {mutant}"
112 );
113 assert_ne!(B1, 0.0);
115 assert_ne!(B2, 0.0);
116 }
117}