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
197
198
199
200
201
202
203
204
205
206
207

#[macro_export]
macro_rules! sequential_type {
    (input_shape: ($($shape:tt)*), layers: { $($layers:tt)* }) => {
        $crate::sequential_type_impl!( $($layers)* )
    };
}

#[macro_export]
macro_rules! sequential_type_impl {
    ($t:ty {$($tt:tt)*}) => {
        $crate::layer::LayerImpl<N, B, O, $t>
    };
    ($t:ty {$($xx:tt)*}, $($tt:tt)*) => {
        $crate::layers::Chain<N, B, O, 
            $crate::layer::LayerImpl<N, B, O, $t>,
            $crate::sequential_type_impl!($($tt)*)
        >
    };
    ($t:ty) => {
        $crate::layer::LayerImpl<N, B, O, $t>
    };
    ($t:ty, $($tt:tt)*) => {
        $crate::layers::Chain<N, B, O, 
            $crate::layer::LayerImpl<N, B, O, $t>,
            $crate::sequential_type_impl!($($tt)*)
        >
    };
}

#[macro_export]
macro_rules! sequential {
    (input_shape: ($($shape:tt)*), layers: { $($layers:tt)* }) => {{
        let initial_shape = $crate::tensor::TensorShape::from(($($shape)*,));

        $crate::sequential_impl!( initial_shape, $($layers)* )
    }};
}

#[macro_export]
macro_rules! sequential_impl {   
    ($p:expr, $t:ty { $($name:ident : $val:expr),* }) => {{
        #[allow(unused_imports)]
        use core::convert::TryInto;

        #[allow(unused_mut)]
        let mut params = <$t as $crate::layer::Layer<_, _>>::Config::default();
        $(
            params.$name = ($val).try_into().unwrap_or($val);
        )*

        $crate::layer::LayerImpl::new(<$t as $crate::layer::Layer<_, _>>::create(
            $p, params
        ))
    }};

    ($p:expr, $t:ty { $($name:ident : $val:expr),* }, $($tt:tt)*) => {{
        #[allow(unused_imports)]
        use core::convert::TryInto;

        #[allow(unused_mut)]
        let mut params = <$t as $crate::layer::Layer<_, _>>::Config::default();
        $(
            params.$name = ($val).try_into().unwrap_or($val);;
        )*

        let layer = $crate::layer::LayerImpl::new(<$t as $crate::layer::Layer<_, _>>::create(
            $p, params
        ));

        let prev_shape = layer.layer.output_shape();
        
        $crate::layers::Chain::new(
            layer, $crate::sequential_impl! { prev_shape, $($tt)* },
        )
    }};

    ($p:expr, $t:ty) => {
        $crate::sequential_impl!{ $p, $t {}}
    };

    ($p:expr, $t:ty, $($tt:tt)*) => {
        $crate::sequential_impl!{ $p, $t {}, $($tt)* }
    };
}

#[macro_export]
macro_rules! sequential_type_ctx {
    (input_shape: ($($shape:tt)*), layers: { $($layers:tt)* }) => {
        $crate::sequential_type_ctx_impl!( $($layers)* )
    };
}

#[macro_export]
macro_rules! sequential_type_ctx_impl {
    ($t:ty {$($xx:tt)*}) => {
        $crate::layer::CommonLayerContext<N, B>
    };

    ($t:ty {$($xx:tt)*}, $($tt:tt)*) => {
        $crate::layers::ChainContext<N, B,
            $crate::layer::CommonLayerContext<N, B>,
            $crate::sequential_type_ctx_impl!($($tt)*)
        >
    };

    ($t:ty) => {
        $crate::sequential_type_ctx_impl!( $t {} )
    };

    ($t:ty, $($tt:tt)*) => {
        $crate::sequential_type_ctx_impl!( $t {}, $($tt)* )
    };
}

#[macro_export]
macro_rules! model_impl {
    ($name:ident <$trait:path> ($($init:tt)*) { $($tt:tt)* }) => {
        mod ctx {
            #[allow(unused_imports)]
            use super::*;
            pub type $name<N, B> = $crate::sequential_type_ctx!($($tt)*);
        }

        pub struct $name <N, B, O>
            where B: $crate::backend::Backend<N> + $trait,
                  O: $crate::optimizer::Optimizer<N, B>
        {
            inner: $crate::sequential_type!($($tt)*),
            _m: core::marker::PhantomData<fn(N, B, O)>,
        }

        impl<N, B, O> $name<N, B, O> 
            where B: $crate::backend::Backend<N> + $trait,
                  O: $crate::optimizer::Optimizer<N, B>
        {
            #[allow(dead_code)]
            pub fn new($($init)*) -> Self {
                #[allow(unused_imports)]
                use $crate::backend::PaddingKind::*;

                #[allow(unused_imports)]
                use $crate::backend::PoolingKind::*;
                
                Self {
                    inner: $crate::sequential!($($tt)*),
                    _m: Default::default(),
                }
            }
        }

        impl<N, B, O> core::fmt::Display for $name<N, B, O> 
            where B: $crate::backend::Backend<N> + $trait,
                  O: $crate::optimizer::Optimizer<N, B>
        {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                writeln!(f, "{} {{", stringify!($name))?;
                write!(f, "{}", self.inner)?;
                writeln!(f, "}}")?;

                Ok(())
            }
        }

        impl<N, B, O> $crate::layer::AbstractLayer<N, B, O> for $name<N, B, O> 
            where B: $crate::backend::Backend<N> + $trait,
                  O: $crate::optimizer::Optimizer<N, B>
        {
            type Context = ctx::$name<N, B>;

            #[inline]
            fn forward(&mut self, backend: &B, inputs: &B::Tensor, ctx: &mut Self::Context) {
                $crate::layer::AbstractLayer::forward(&mut self.inner, backend, inputs, ctx)
            }

            #[inline]
            fn backward(&mut self, backend: &B, deltas: &B::Tensor, inputs: &B::Tensor, ctx: &mut Self::Context) {
                $crate::layer::AbstractLayer::backward(&mut self.inner, backend, deltas, inputs, ctx);
            }

            #[inline]
            fn update(&mut self, backend: &B, optimizer: &O, inputs: &B::Tensor, deltas: &B::Tensor, ctx: &mut Self::Context) {
                $crate::layer::AbstractLayer::update(&mut self.inner, backend, optimizer, inputs, deltas, ctx);
            }
        }
    };
}

#[macro_export]
macro_rules! model {
    ($name:ident ($($init:tt)*) { $($tt:tt)* }) => {
        mod tmp {
            pub trait BackendDefault<N> = $crate::backend::BackendReLu<N> 
                  + $crate::backend::BackendBias<N>
                  + $crate::backend::BackendSigmoid<N>
                  + $crate::backend::BackendSoftmax<N>
                  + $crate::backend::BackendGemm<N>
                  + $crate::backend::BackendConv2d<N>
                  + $crate::backend::BackendMaxPool2d<N>
                  + $crate::backend::BackendAvgPool2d<N>;
        }
        $crate::model_impl!($name <tmp::BackendDefault<N>> ($($init)*) { $($tt)* });
    };
    ($name:ident <$trait:path> ($($init:tt)*) { $($tt:tt)* }) => {
        $crate::model_impl!($name <$trait> ($($init)*) { $($tt)* });
    };
}