rai_nn/
activations.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use rai_core::Tensor;
use rai_derive::Module;
use serde::Deserialize;

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Default, Module)]
#[module(crate = rai_core)]
#[serde(rename_all = "lowercase")]
pub enum Activation {
    #[default]
    Gelu,
    #[serde(alias = "gelu_new")]
    NewGelu,
    Relu,
    Relu2,
    Relu6,
    Silu,
}

impl Activation {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        match self {
            Activation::Gelu => x.gelu(),
            Activation::NewGelu => x.new_gelu(),
            Activation::Relu => x.relu(),
            Activation::Relu2 => x.relu2(),
            Activation::Relu6 => x.relu6(),
            Activation::Silu => x.silu(),
        }
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct Relu;
impl Relu {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.relu()
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct Relu2;
impl Relu2 {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.relu2()
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct Relu6;
impl Relu6 {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.relu6()
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct Gelu;
impl Gelu {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.gelu()
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct NewGelu;
impl NewGelu {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.new_gelu()
    }
}

#[derive(Clone, Debug, Copy, Module)]
#[module(crate = rai_core)]
pub struct Silu;
impl Silu {
    pub fn fwd(&self, x: &Tensor) -> Tensor {
        x.silu()
    }
}