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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Structs that implement trait [Module](crate::nn::Module) for anything that it makes sense to implement this trait.
//! These include zyx::ops, as well as layers, such as [Linear].
//!
//! This module is expected to get most stuff added.
//! It will contain functors, layers, models, cells, simply anything that can have .forward(input) function.
//!

mod module;
pub mod parameters;

pub use module::{Module, ApplyModule};

use crate::{ops::{self, HasShape, Pow, Zero, One, MatMul, HasDType, IntoVariable, ZerosLike}, tensor::Variable, device::{BufferInit, cpu::Buffer}, shape::{Shape, Sh2, Axes}};
use core::{ops::{Neg, Add, Sub, Div}, marker::PhantomData};

/// ReLU operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ReLU;

impl<Input> Module<'_, Input> for ReLU
where
    Input: ops::ReLU,
{
    type Output = Input::Output;
    fn forward(&self, x: Input) -> Self::Output { x.relu() }
}

impl<'p> HasParameters<'p> for ReLU {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Exp operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Exp;

impl<Input> Module<'_, Input> for Exp
where
    Input: ops::Exp,
{
    type Output = Input::Output;
    fn forward(&self, x: Input) -> Self::Output { x.exp() }
}

impl<'p> HasParameters<'p> for Exp {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Ln operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ln;

impl<Input> Module<'_, Input> for Ln
where
    Input: ops::Ln,
{
    type Output = Input::Output;
    fn forward(&self, x: Input) -> Self::Output { x.ln() }
}

impl<'p> HasParameters<'p> for Ln {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Tanh operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tanh;

impl<Input> Module<'_, Input> for Tanh
where
    Input: ops::Tanh,
{
    type Output = Input::Output;
    fn forward(&self, x: Input) -> Self::Output {
        x.tanh()
    }
}

impl<'p> HasParameters<'p> for Tanh {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Sigmoid operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sigmoid;

impl<Input> Module<'_, Input> for Sigmoid
where
    Input: Neg + HasDType,
    Input::T: ops::One,
    <Input as Neg>::Output: ops::Exp,
    <<Input as Neg>::Output as ops::Exp>::Output: Add<Input::T>,
    <<<Input as Neg>::Output as ops::Exp>::Output as Add<Input::T>>::Output: Pow<i32>,
{
    type Output = <<<<Input as Neg>::Output as ops::Exp>::Output as Add<Input::T>>::Output as Pow<i32>>::Output;
    fn forward(&self, x: Input) -> Self::Output {
        use ops::Exp;
        ((-x).exp() + Input::T::one()).pow(-1)
    }
}

impl<'p> HasParameters<'p> for Sigmoid {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/*
/// Softmax operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SoftMax<Dims>
where
    Dims: Axes,
{
    /// Dimension to calculate softmax across
    pub dims: Dims
}

impl<Input, D> Module<'_, Input> for SoftMax<D>
where
    D: Clone + Axes,
    Input: Clone + ops::Maximizable<D> + Sub<<Input as ops::Maximizable<D>>::Output>,
    <Input as Sub<<Input as ops::Maximizable<D>>::Output>>::Output: ops::Exp,
    <<Input as Sub<<Input as ops::Maximizable<D>>::Output>>::Output as ops::Exp>::Output: Clone + ops::Summable<D>,
    <<Input as Sub<<Input as ops::Maximizable<D>>::Output>>::Output as ops::Exp>::Output: Div<<<<Input as Sub<<Input as ops::Maximizable<D>>::Output>>::Output as ops::Exp>::Output as ops::Summable<D>>::Output>,
{
    type Output = <<<Input as Sub<<Input as ops::Max<D>>::Output>>::Output as ops::Exp>::Output as Div<<<<Input as Sub<<Input as ops::Max<D>>::Output>>::Output as ops::Exp>::Output as ops::Sum<D>>::Output>>::Output;
    type Params = ();

    fn forward(&self, x: Input) -> Self::Output {
        use crate::ops::{Exp, Sum};
        // TODO: Check if cloning Tensors (that is also cloning their grad_fn)
        // has any effect on correctness of this function's gradient calculation
        let temp = (x.clone() - x.max()).exp();
        temp.clone() / temp.sum()
    }

    fn parameters(&mut self) -> Self::Params {}
}*/

#[test]
fn softmax_test() {
    //use crate::prelude::*;
    //use crate::device::cpu::Buffer;

    //let x = Buffer::<f32, _>::cfrom([[3., 2., 4.], [4., 2., 5.]]).with_grad();

    /*let dim = -1;
    let e_x = ((&x).data().clone() - (&x).max(())).exp();
    println!("\n{}", e_x);
    let y = e_x.clone() / e_x.sum(dim);
    println!("\n{}", y);
    y.backward();
    println!("\n{}", x.grad());*/

    /*let sm = SoftMax { dims: -1 };
    let y = sm.forward(&x);
    //println!("\n{}", y);
    y.backward();
    //println!("\n{}", x);
    //panic!();*/
}

/// Sum operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sum<Dims>
where
    Dims: Axes,
{
    dims: PhantomData<Dims>,
}

impl<Input, Dims> Module<'_, Input> for Sum<Dims>
where
    Input: ops::Summable<Dims>,
    Dims: Axes,
{
    type Output = Input::Output;
    fn forward(&self, x: Input) -> Self::Output {
        x._sum()
    }
}

impl<'p, Dims> HasParameters<'p> for Sum<Dims>
where
    Dims: Axes,
{
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Max operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Max<Dims>
where
    Dims: Axes,
{
    /// [Dimensions](crate::shape::Shape) to max
    pub dims: Dims,
}

impl<Input, Dims> Module<'_, Input> for Max<Dims>
where
    Input: ops::Maximizable<Dims>,
    Dims: Axes,
{
    type Output = (Input::Values, Input::Indices);
    fn forward(&self, x: Input) -> Self::Output { x._max() }
}

impl<'p, Dims> HasParameters<'p> for Max<Dims>
where
    Dims: Axes,
{
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// Minimizable operation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Min<Dims>
where
    Dims: Axes,
{
    /// [Dimensions](crate::shape::Shape) to min
    pub dims: Dims,
}

impl<Input, Dims> Module<'_, Input> for Min<Dims>
where
    Input: ops::Minimizable<Dims>,
    Dims: Axes,
{
    type Output = (Input::Values, Input::Indices);
    fn forward(&self, x: Input) -> Self::Output { x._min() }
}

impl<'p, Dims> HasParameters<'p> for Min<Dims>
where
    Dims: Axes,
{
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Mean operation
pub struct Mean<Dims>
where
    Dims: Axes,
{
    /// [Dimensions](crate::shape::Shape) for mean
    pub dims: Dims
}

impl<Input, Dims> Module<'_, Input> for Mean<Dims>
where
    Dims: Axes,
    Input: HasShape + ops::Summable<Dims>,
    <Input as ops::Summable<Dims>>::Output: Div<i32>,
{
    type Output = <<Input as ops::Summable<Dims>>::Output as Div<i32>>::Output;
    fn forward(&self, x: Input) -> Self::Output {
        x._sum()/(Input::Sh::NUMEL as i32)
    }
}

impl<'p, Dims> HasParameters<'p> for Mean<Dims>
where
    Dims: Axes,
{
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

/// MSE loss
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MSELoss;

impl<Y, YP> Module<'_, (Y, YP)> for MSELoss
where
    Y: Sub<YP>,
    <Y as Sub<YP>>::Output: Pow<i32>,
{
    type Output = <<Y as Sub<YP>>::Output as Pow<i32>>::Output;
    fn forward(&self, x: (Y, YP)) -> Self::Output {
        (x.0 - x.1).pow(2)
    }
}

impl<'p> HasParameters<'p> for MSELoss {
    type Params = ();
    fn parameters(&'p mut self) -> Self::Params {}
}

//pub struct STD {}

/*#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct NormLayer {}

impl<Input> Module<Input> for NormLayer {
    type Output = Self;
    fn forward(&self, x: Input) -> Self::Output {
        (x - x.apply(&Mean))/x.apply(&STD)
    }
}*/

use crate::device::BufferFromSlice;

use self::parameters::HasParameters;
/// Linear layer
#[derive(Debug)]
pub struct Linear<'d, const IN_FEATURES: usize, const OUT_FEATURES: usize, W = Buffer<'d, Sh2<IN_FEATURES, OUT_FEATURES>>, B = Buffer<'d, Sh2<1, OUT_FEATURES>>>
where
    W: HasShape + HasDType,
    B: HasShape + HasDType,
{
    dev: PhantomData<&'d W>,
    w: Variable<W>,
    b: Variable<B>,
}

impl<'d, W, B, const IN_FEATURES: usize, const OUT_FEATURES: usize> Linear<'d, IN_FEATURES, OUT_FEATURES, W, B>
where
    W: HasShape + HasDType,
    W::T: Zero + One,
    B: HasShape + HasDType,
    B::T: Zero + One,
{
    /// Create new [Linear layer](Linear) with given in_features and out_features dimensions
    /// with parameters stored on given device.
    pub fn new<Dev>(device: &'d Dev) -> Self
    where
        Dev: BufferFromSlice<'d, W> + BufferFromSlice<'d, B>,
        W: 'd + IntoVariable,
        W::T: rand::distributions::uniform::SampleUniform,
        B: 'd + IntoVariable,
        B::T: rand::distributions::uniform::SampleUniform,
    {
        Self {
            dev: PhantomData,
            w: <Dev as BufferInit<'d, W>>::uniform(device, W::T::zero(), W::T::one()).with_grad(),
            b: <Dev as BufferInit<'d, B>>::uniform(device, B::T::zero(), B::T::one()).with_grad(),
        }
    }
}

impl<'p, Input, W, B, const IN_FEATURES: usize, const OUT_FEATURES: usize> Module<'p, Input> for Linear<'_, IN_FEATURES, OUT_FEATURES, W, B>
where
    W: 'p + HasShape + HasDType,
    W::T: Zero + One,
    B: 'p + HasShape + HasDType,
    B::T: Zero + One,
    Input: MatMul<&'p Variable<W>>,
    <Input as MatMul<&'p Variable<W>>>::Output: Add<&'p Variable<B>>,
{
    type Output = <<Input as MatMul<&'p Variable<W>>>::Output as Add<&'p Variable<B>>>::Output;
    fn forward(&'p self, x: Input) -> Self::Output {
        x.matmul(&self.w) + &self.b
    }
}

impl<'p, W, B, const IN_FEATURES: usize, const OUT_FEATURES: usize> HasParameters<'p> for Linear<'_, IN_FEATURES, OUT_FEATURES, W, B>
where
    W: 'p + HasShape + HasDType + ZerosLike,
    B: 'p + HasShape + HasDType + ZerosLike,
{
    type Params = (&'p mut Variable<W>, &'p mut Variable<B>);
    fn parameters(&'p mut self) -> Self::Params { (&mut self.w, &mut self.b) }
}

// RNNCell
// TODO: Should we rewrite this as two linear layers?
/*#[derive(Debug, Clone)]
pub struct RNNCell<WI, BI, WH, BH> {
    wih: Variable<WI>,
    bih: Variable<BI>,
    whh: Variable<WH>,
    bhh: Variable<BH>,
}

impl<WI, BI, WH, BH> RNNCell<WI, BI, WH, BH> {
    /// Create new [RNNCell] with given input_size and hidden_size dimensions
    pub fn new<T>(input_size: usize, hidden_size: usize) -> Self
    where
        T: Zeros<Sh = Ax0> + Ones<Sh = Ax0>,
        WI: UniformInit<T = T, Sh = (usize, usize)>,
        BI: UniformInit<T = T, Sh = (usize, usize)>,
        WH: UniformInit<T = T, Sh = (usize, usize)>,
        BH: UniformInit<T = T, Sh = (usize, usize)>,
    {
        Self {
            wih: WI::uniform((input_size, hidden_size), T::zeros(), T::ones()).with_grad(),
            bih: BI::uniform((1, hidden_size), T::zeros(), T::ones()).with_grad(),
            whh: WH::uniform((hidden_size, hidden_size), T::zeros(), T::ones()).with_grad(),
            bhh: BH::uniform((1, hidden_size), T::zeros(), T::ones()).with_grad(),
        }
    }
}

impl<'a, WI, BI, WH, BH, I, H> Module<'a, (I, H)> for RNNCell<WI, BI, WH, BH>
where
    WI: 'a + Clone + Sub<<WI as Mul<f64>>::Output, Output = WI> + Mul<f64>,
    BI: 'a + Clone + Sub<<BI as Mul<f64>>::Output, Output = BI> + Mul<f64>,
    WH: 'a + Clone + Sub<<WH as Mul<f64>>::Output, Output = WH> + Mul<f64>,
    BH: 'a + Clone + Sub<<BH as Mul<f64>>::Output, Output = BH> + Mul<f64>,

    I: MatMul<&'a Variable<WI>>,
    <I as MatMul<&'a Variable<WI>>>::Output: Add<&'a Variable<BI>>,
    H: MatMul<&'a Variable<WH>>,
    <H as MatMul<&'a Variable<WH>>>::Output: Add<&'a Variable<BH>>,
    <<I as MatMul<&'a Variable<WI>>>::Output as Add<&'a Variable<BI>>>::Output: Add<<<H as MatMul<&'a Variable<WH>>>::Output as Add<&'a Variable<BH>>>::Output>,
{
    type Output = <<<I as MatMul<&'a Variable<WI>>>::Output as Add<&'a Variable<BI>>>::Output as Add<<<H as MatMul<&'a Variable<WH>>>::Output as Add<&'a Variable<BH>>>::Output>>::Output;
    type Params = (&'a mut Variable<WI>, &'a mut Variable<BI>, &'a mut Variable<WH>, &'a mut Variable<BH>);

    fn forward(&'a self, x: (I, H)) -> Self::Output {
        (x.0.matmul(&self.wih) + &self.bih) + (x.1.matmul(&self.whh) + &self.bhh)
    }

    fn parameters(&'a mut self) -> Self::Params {
        (&mut self.wih, &mut self.bih, &mut self.whh, &mut self.bhh)
    }
}*/

/*#[derive(Debug, Clone)]
struct Attention<W, WG, F, FG, FB, FBG> {
    num_heads: usize,
    scale: f64,
    qkv: Variable<W, WG>,
    fc_out: Linear<F, FG, FB, FBG>,
}

impl<'p, W, WG, F, FG, FB, FBG, QKV> Attention<'p, QKV> for Attention<W, WG, F, WG, FB, FBG>
where
    W: 'p + Clone + Sub<<WG as Mul<f64>>::Output, Output = W>,
    WG: 'a + Clone + Mul<f64>,
    F: 'a + Clone + Sub<<FG as Mul<f64>>::Output, Output = F>,
    FG: 'a + Clone + Mul<f64>,
    FB: 'a + Clone + Sub<<FBG as Mul<f64>>::Output, Output = FB>,
    FBG: 'a + Clone + Mul<f64>,

{
    type Output = ();
    type Params = (&'p mut Variable<W, WG>, (&'p mut Variable<F, FG>, &'p mut Variable<FB, FBG>));

    fn forward(&'a self, x: QKV) -> Self::Output {
        // All that is needed for attention mechanism
        let s = x.shape();
        let (B, N, C) = (s[0], s[1], s[2]);
        qkv = x.matmul(self.qkv).reshape(B, B, 3, self.num_heads, C/self.num_heads).permute(2, 0, 3, 1, 4);
        let (q, k, v) = qkv.unbind(0); // we don't have unbind yet, so we need to implement it and then we will have working attention

        attn = (q.matmul(k.transpose())) * self.scale;
        attn = SoftMax { dim = -1 }.forward(attn);
        x = (attn.matmul(v)).permute(1, 2).reshape(B, N, C);

        self.fc_out.forward(x)
    }

    fn parameters(&'a mut self) -> Self::Params {
        (&mut self.qkv, self.fc_out.parameters())
    }
}*/