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
use crate::{transforms::Func, Tensor, WithTensors};
use std::collections::HashMap;

pub trait Module {
    fn forward(&self, input: &Tensor) -> Tensor;
    fn gather_parameters(&self, out: &mut Vec<Tensor>);
    fn parameters(&self) -> Vec<Tensor> {
        let mut params = Vec::new();
        self.gather_parameters(&mut params);
        params
    }
    fn update(&self, _params: &mut HashMap<usize, Tensor>) {}
}

impl<T> Func<Tensor, Tensor> for T
where
    T: Module,
{
    type Tangent = HashMap<usize, Tensor>;
    type Cotangent = HashMap<usize, Tensor>;

    fn call(&self, input: Tensor) -> Tensor {
        self.forward(&input)
    }

    fn self_captured_tensors(&self, tensors: &mut Vec<Tensor>) {
        self.gather_parameters(tensors)
    }

    fn extract_input_tensors(&self, _input: &Tensor, _tensors: &mut Vec<Tensor>) {}
}

pub struct Aux<T>(pub T);

impl<T> WithTensors for (Tensor, Aux<T>) {
    fn tensors(&self) -> Vec<Tensor> {
        vec![self.0.clone()]
    }
}

// for loss fn (module, input) -> loss
impl<'m, 'i, M, F> Func<(&'m M, &'i Tensor), Tensor> for F
where
    M: Module,
    F: Fn(&'m M, &'i Tensor) -> Tensor,
{
    type Tangent = HashMap<usize, Tensor>;
    type Cotangent = HashMap<usize, Tensor>;
    fn call(&self, input: (&'m M, &'i Tensor)) -> Tensor {
        self(input.0, input.1)
    }

    fn extract_input_tensors(&self, input: &(&'m M, &'i Tensor), inputs: &mut Vec<Tensor>) {
        inputs.extend(input.0.parameters())
    }
}

// for loss fn (module, input, label) -> loss
impl<'m, 'i, 'l, M, F> Func<(&'m M, &'i Tensor, &'l Tensor), Tensor> for F
where
    M: Module,
    F: Fn(&'m M, &'i Tensor, &'l Tensor) -> Tensor,
{
    type Tangent = HashMap<usize, Tensor>;
    type Cotangent = HashMap<usize, Tensor>;
    fn call(&self, input: (&'m M, &'i Tensor, &'l Tensor)) -> Tensor {
        self(input.0, input.1, input.2)
    }

    fn extract_input_tensors(
        &self,
        input: &(&'m M, &'i Tensor, &'l Tensor),
        inputs: &mut Vec<Tensor>,
    ) {
        inputs.extend(input.0.parameters())
    }
}

// for loss fn  (module, input) -> (loss, Aux<T>)
impl<'m, 'i, M, F, T> Func<(&'m M, &'i Tensor), (Tensor, Aux<T>)> for F
where
    M: Module,
    F: Fn(&'m M, &'i Tensor) -> (Tensor, Aux<T>),
{
    type Tangent = HashMap<usize, Tensor>;
    type Cotangent = HashMap<usize, Tensor>;
    fn call(&self, input: (&'m M, &'i Tensor)) -> (Tensor, Aux<T>) {
        self(input.0, input.1)
    }

    fn extract_input_tensors(&self, input: &(&'m M, &'i Tensor), inputs: &mut Vec<Tensor>) {
        inputs.extend(input.0.parameters())
    }
}

// for loss fn (module, input, label) -> (loss, Aux<T>)
impl<'m, 'i, 'l, M, F, T> Func<(&'m M, &'i Tensor, &'l Tensor), (Tensor, Aux<T>)> for F
where
    M: Module,
    F: Fn(&'m M, &'i Tensor, &'l Tensor) -> (Tensor, Aux<T>),
{
    type Tangent = HashMap<usize, Tensor>;
    type Cotangent = HashMap<usize, Tensor>;
    fn call(&self, input: (&'m M, &'i Tensor, &'l Tensor)) -> (Tensor, Aux<T>) {
        self(input.0, input.1, input.2)
    }

    fn extract_input_tensors(
        &self,
        input: &(&'m M, &'i Tensor, &'l Tensor),
        inputs: &mut Vec<Tensor>,
    ) {
        inputs.extend(input.0.parameters())
    }
}