Skip to main content

radiate_gp/ops/
mod.rs

1pub mod bool;
2pub mod math;
3pub mod mutator;
4pub mod operation;
5mod param;
6pub mod primitives;
7#[cfg(feature = "serde")]
8mod serde;
9
10use radiate_utils::Float;
11use std::cell::RefCell;
12
13pub use math::{activation_ops, all_ops, math_ops};
14pub use mutator::OperationMutator;
15pub use operation::*;
16pub use param::Param;
17
18pub trait OpFloat: Float + Send + Sync {
19    const MAX_VALUE: Self;
20    const LOG_EPS: Self;
21
22    fn with_loss_buffer<R>(f: impl FnOnce(&mut Vec<Self>) -> R) -> R;
23}
24
25macro_rules! impl_op_float {
26    ($t:ty, $max:expr, $log_eps:expr) => {
27        impl OpFloat for $t {
28            const MAX_VALUE: Self = $max;
29            const LOG_EPS: Self = $log_eps;
30
31            fn with_loss_buffer<R>(f: impl FnOnce(&mut Vec<Self>) -> R) -> R {
32                thread_local! {
33                    static BUFFER: RefCell<Vec<$t>> = const { RefCell::new(Vec::new()) };
34                }
35                BUFFER.with(|cell| f(&mut cell.borrow_mut()))
36            }
37        }
38    };
39}
40
41impl_op_float!(f32, 1e10_f32, 1e-7_f32);
42impl_op_float!(f64, 1e10_f64, 1e-7_f64);
43
44pub(crate) mod op_names {
45    /// Mathematical operation names
46    pub const ADD: &str = "add";
47    pub const SUB: &str = "sub";
48    pub const MUL: &str = "mul";
49    pub const DIV: &str = "div";
50    pub const SUM: &str = "sum";
51    pub const DIFF: &str = "diff";
52    pub const PROD: &str = "prod";
53    pub const NEG: &str = "neg";
54    pub const ABS: &str = "abs";
55    pub const SQRT: &str = "sqrt";
56    pub const POW: &str = "pow";
57    pub const MAX: &str = "max";
58    pub const MIN: &str = "min";
59    pub const SIN: &str = "sin";
60    pub const COS: &str = "cos";
61    pub const TAN: &str = "tan";
62    pub const EXP: &str = "exp";
63    pub const LOG: &str = "log";
64    pub const CEIL: &str = "ceil";
65    pub const FLOOR: &str = "floor";
66    pub const SOFTPLUS: &str = "softplus";
67    pub const MISH: &str = "mish";
68    pub const SWISH: &str = "swish";
69    pub const ELU: &str = "elu";
70    pub const LEAKY_RELU: &str = "l_relu";
71    pub const LINEAR: &str = "linear";
72    pub const RELU: &str = "relu";
73    pub const SIGMOID: &str = "sigmoid";
74    pub const TANH: &str = "tanh";
75    pub const IDENTITY: &str = "identity";
76    pub const LOGSUMEXP: &str = "logsumexp";
77    pub const TOOTH: &str = "tooth";
78    pub const SIGN: &str = "sign";
79    pub const RECIPROCAL: &str = "reciprocal";
80    pub const GAUSSIAN: &str = "gaussian";
81    pub const WEIGHT: &str = "w";
82    pub const WEIGHT2: &str = "w2";
83
84    /// Boolean operation names
85    pub const AND: &str = "and";
86    pub const OR: &str = "or";
87    pub const NOT: &str = "not";
88    pub const XOR: &str = "xor";
89    pub const EQ: &str = "eq";
90    pub const NE: &str = "ne";
91    pub const GT: &str = "gt";
92    pub const GE: &str = "ge";
93    pub const LT: &str = "lt";
94    pub const LE: &str = "le";
95    pub const IF_ELSE: &str = "if_else";
96    pub const AND_THEN: &str = "and_then";
97    pub const OR_ELSE: &str = "or_else";
98    pub const NAND: &str = "nand";
99    pub const NOR: &str = "nor";
100    pub const XNOR: &str = "xnor";
101    pub const IMPLIES: &str = "implies";
102    pub const IFF: &str = "iff";
103}