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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::{
    prelude::{
        BuildModule, BuildOnDevice, Module, NonMutableModule, TensorCollection, TensorOptions,
    },
    shapes::*,
    tensor::*,
    tensor_ops::*,
};

pub mod builder {
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub struct PReLU;

    use core::marker::PhantomData;

    use crate::prelude::ConstDim;

    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub struct PReLU1D<C: ConstDim>(PhantomData<C>);
}

impl<E: Dtype, D: Device<E>> BuildOnDevice<D, E> for builder::PReLU
where
    PReLU<E, D>: BuildModule<D, E>,
{
    type Built = PReLU<E, D>;
    fn try_build_on_device(device: &D) -> Result<Self::Built, <D>::Err> {
        Self::Built::try_build(device)
    }
}

/// Calls [prelu()] with learnable value.
#[derive(Debug, Clone)]
pub struct PReLU<E: Dtype, D: Device<E>> {
    pub a: Tensor<(), E, D>,
}

impl<E: Dtype, D: Device<E>> NonMutableModule for PReLU<E, D> {}

impl<S: ConstShape, E: Dtype, D: Device<E>, T: Tape<E, D>> Module<Tensor<S, E, D, T>>
    for PReLU<E, D>
where
    Tensor<S, E, D, T>: TryPReLU<Tensor<S, E, D, NoneTape>>,
{
    type Output = Tensor<S, E, D, T>;
    type Error = <Tensor<S, E, D, T> as HasErr>::Err;

    fn try_forward(&self, input: Tensor<S, E, D, T>) -> Result<Self::Output, Self::Error> {
        input.try_prelu(self.a.retaped().broadcast())
    }
}

impl<E: Dtype, D: Device<E>> TensorCollection<E, D> for PReLU<E, D> {
    type To<E2: Dtype, D2: Device<E2>> = PReLU<E2, D2>;

    fn iter_tensors<V: crate::prelude::ModuleVisitor<Self, E, D>>(
        visitor: &mut V,
    ) -> Result<Option<Self::To<V::E2, V::D2>>, V::Err> {
        visitor.visit_fields(
            Self::tensor("a", |p| &p.a, |p| &mut p.a, TensorOptions::reset_to_zeros()),
            |a| PReLU { a },
        )
    }
}

/// Calls [prelu()] with learnable values along second dimension.
#[derive(Debug, Clone)]
pub struct PReLU1D<C: ConstDim, E: Dtype, D: Device<E>> {
    pub a: Tensor<(C,), E, D>,
}

impl<C: ConstDim, E: Dtype, D: Device<E>> BuildOnDevice<D, E> for builder::PReLU1D<C>
where
    PReLU1D<C, E, D>: BuildModule<D, E>,
{
    type Built = PReLU1D<C, E, D>;
    fn try_build_on_device(device: &D) -> Result<Self::Built, <D>::Err> {
        Self::Built::try_build(device)
    }
}

impl<C: ConstDim, E: Dtype, D: Device<E>> NonMutableModule for PReLU1D<C, E, D> {}

impl<C: ConstDim, E: Dtype, D: Device<E>, T: Tape<E, D>> Module<Tensor<(C,), E, D, T>>
    for PReLU1D<C, E, D>
where
    Tensor<(C,), E, D, T>: TryPReLU<Tensor<(C,), E, D, NoneTape>>,
{
    type Output = Tensor<(C,), E, D, T>;

    type Error = <Tensor<(C,), E, D, T> as HasErr>::Err;

    fn try_forward(&self, input: Tensor<(C,), E, D, T>) -> Result<Self::Output, Self::Error> {
        input.try_prelu(self.a.retaped())
    }
}

macro_rules! prelu1d {
    (($($InDims:tt),*), $Axes:ty) => {
        impl<E: Dtype, D: Device<E>, T: Tape<E, D> + Merge<NoneTape>, $($InDims: ConstDim),*> Module<Tensor<($($InDims),*), E, D, T>> for PReLU1D<C,E, D>
        where ($($InDims),*): ReduceShapeTo<(C,), $Axes>,
        Tensor<($($InDims),*), E, D, T>: TryPReLU<Tensor<($($InDims),*), E, D, NoneTape>>,
        {
            type Output = Tensor<($($InDims),*), E, D, T>;
            type Error = <Tensor<($($InDims),*), E, D, T> as HasErr>::Err;

            fn try_forward(&self, input: Tensor<($($InDims),*), E, D, T>) -> Result<Self::Output, Self::Error> {
                input.try_prelu(self.a.retaped().broadcast())
            }
        }
    };
}

prelu1d!((B, C), Axis<0>);
prelu1d!((B, C, M), Axes2<0, 2>);
prelu1d!((B, C, M, N), Axes3<0, 2, 3>);

impl<C: ConstDim, E: Dtype, D: Device<E>> TensorCollection<E, D> for PReLU1D<C, E, D> {
    type To<E2: Dtype, D2: Device<E2>> = PReLU1D<C, E2, D2>;

    fn iter_tensors<V: crate::prelude::ModuleVisitor<Self, E, D>>(
        visitor: &mut V,
    ) -> Result<Option<Self::To<V::E2, V::D2>>, V::Err> {
        visitor.visit_fields(
            Self::tensor("a", |p| &p.a, |p| &mut p.a, TensorOptions::reset_to_zeros()),
            |a| PReLU1D { a },
        )
    }
}

#[cfg(test)]
mod tests {
    use modules::Tanh;

    use crate::{nn::*, tests::*};

    use super::*;

    #[test]
    fn test_nn_activations_prelu() {
        let dev: TestDevice = Default::default();

        let t = dev.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]);
        let r1 = PReLU {
            a: dev.tensor(0.05),
        }
        .forward_mut(t.clone());
        let r2 = t.prelu(dev.tensor([0.05, 0.05, 0.05, 0.05, 0.05]));
        assert_eq!(r1.array(), r2.array());

        let t = dev.tensor([[-2.0, -1.0, 0.0], [1.0, 2.0, 3.0]]);
        let r1 = PReLU {
            a: dev.tensor(0.05),
        }
        .forward_mut(t.clone());
        let r2 = t.prelu(dev.tensor([[0.05, 0.05, 0.05], [0.05, 0.05, 0.05]]));
        assert_eq!(r1.array(), r2.array());

        let model = (
            Tanh,
            PReLU {
                a: dev.tensor(0.05),
            },
        );
        let t = dev.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]);
        let out = model.forward(t);
        assert_close_to_literal!(out, [-0.04820138, -0.03807970, 0.0, 0.76159415, 0.96402758])
    }

    #[test]
    fn test_nn_activations_prelu_1d() {
        let dev: TestDevice = Default::default();

        let t = dev.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]);
        let r1 = PReLU1D {
            a: dev.tensor([0.05, 0.06, 0.07, 0.08, 0.09]),
        }
        .forward_mut(t.clone());
        let r2 = t.prelu(dev.tensor([0.05, 0.06, 0.07, 0.08, 0.09]));
        assert_eq!(r1.array(), r2.array());

        let t = dev.tensor([[-2.0, -1.0, 0.0], [1.0, 2.0, 3.0]]);
        let r1 = PReLU1D {
            a: dev.tensor([0.05, 0.07, 0.09]),
        }
        .forward_mut(t.clone());
        let r2 = t.prelu(dev.tensor([[0.05, 0.07, 0.09], [0.05, 0.07, 0.09]]));
        assert_eq!(r1.array(), r2.array());

        let mut model = dev.build_module::<(Tanh, builder::PReLU1D<Const<5>>), f32>();
        let t = dev.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]);
        model.1.a = dev.tensor([0.05, 0.05, 0.05, 0.05, 0.05]);
        let out = model.forward(t);
        assert_close_to_literal!(out, [-0.04820138, -0.03807970, 0.0, 0.76159415, 0.96402758])
    }
}