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
use std::{collections::HashMap, path::Path};

use crate::{backend::Cpu, Backend, GenericValue, ModuleValue, Tensor, ValueSpec};

pub trait Module {
    type Input;
    type Output;
    fn forward(&self, x: &Self::Input) -> Self::Output;

    fn gather_params(&self, params: &mut HashMap<usize, Tensor>);

    fn params(&self) -> HashMap<usize, Tensor> {
        let mut params = HashMap::new();
        self.gather_params(&mut params);
        params
    }

    fn update_params(&self, params: &mut HashMap<usize, Tensor>);

    fn gather_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>);

    fn named_params(&self, prefix: &str) -> HashMap<String, Tensor> {
        let mut params = HashMap::new();
        self.gather_named_params(prefix, &mut params);
        params
    }

    fn update_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>);

    fn to_safetensors<P: AsRef<Path>>(&self, filename: P)
    where
        Self: Sized,
    {
        let named_params = self.named_params("");
        // todo: add to_device ops, and move all tensors to cpu first
        // todo: eval once after call to_device
        Cpu.to_safetensors(named_params, filename.as_ref());
    }

    fn update_by_safetensors<P: AsRef<std::path::Path>>(&self, filenames: &[P]) {
        let mut st_tensors: HashMap<String, Tensor> = HashMap::new();
        for filename in filenames {
            let data = std::fs::read(filename).unwrap();
            let st = safetensors::SafeTensors::deserialize(&data).unwrap();
            for (name, view) in st.tensors() {
                let t = Tensor::from_safetensor(&view, &Cpu);
                st_tensors.insert(name, t);
            }
        }
        self.update_named_params("", &mut st_tensors);
    }

    fn chain<B>(self, b: B) -> Chain<Self, B>
    where
        Self: Sized,
    {
        Chain::new(self, b)
    }
}

impl<'a, T> Module for &'a T
where
    T: Module,
{
    type Input = T::Input;
    type Output = T::Output;

    #[inline]
    fn forward(&self, x: &Self::Input) -> Self::Output {
        (*self).forward(x)
    }

    #[inline]
    fn gather_params(&self, params: &mut HashMap<usize, Tensor>) {
        (*self).gather_params(params)
    }

    #[inline]
    fn update_params(&self, params: &mut HashMap<usize, Tensor>) {
        (*self).update_params(params)
    }

    fn gather_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>) {
        (*self).gather_named_params(prefix, params)
    }

    fn update_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>) {
        (*self).update_named_params(prefix, params)
    }
}

pub trait TrainableModule:
    Module
    + ValueSpec<
        Kind = ModuleValue,
        Tensors = HashMap<usize, Tensor>,
        Gradient = HashMap<usize, Tensor>,
    >
{
}

impl<'a, T> TrainableModule for &'a T where T: TrainableModule {}

impl<T> GenericValue<ModuleValue, HashMap<usize, Tensor>, HashMap<usize, Tensor>> for T
where
    T: TrainableModule<Tensors = HashMap<usize, Tensor>, Gradient = HashMap<usize, Tensor>>,
{
    #[inline]
    fn gv_tensors(&self) -> HashMap<usize, Tensor> {
        self.params()
    }

    #[inline]
    fn gv_grad(
        tensors: &HashMap<usize, Tensor>,
        grad_map: &HashMap<usize, Tensor>,
    ) -> HashMap<usize, Tensor> {
        tensors
            .keys()
            .map(|id| (*id, grad_map.get(id).unwrap().clone()))
            .collect()
    }

    #[inline]
    fn gv_grad_map(
        tensors: &HashMap<usize, Tensor>,
        grad: HashMap<usize, Tensor>,
        out: &mut HashMap<usize, Tensor>,
    ) {
        for id in tensors.keys() {
            out.insert(*id, grad.get(id).unwrap().clone());
        }
    }
}

pub trait NonTrainableModule:
    Module + ValueSpec<Kind = ModuleValue, Tensors = (), Gradient = ()>
{
}
impl<'a, T> NonTrainableModule for &'a T where T: NonTrainableModule {}

impl<T> GenericValue<ModuleValue, (), ()> for T
where
    T: NonTrainableModule<Tensors = (), Gradient = ()>,
{
    fn gv_tensors(&self) {}
    fn gv_grad(_: &(), _: &HashMap<usize, Tensor>) {}
    fn gv_grad_map(_: &(), _: (), _: &mut HashMap<usize, Tensor>) {}
}

pub struct Chain<A, B> {
    a: A,
    b: B,
}

impl<A, B> Chain<A, B> {
    pub fn new(a: A, b: B) -> Self {
        Self { a, b }
    }
}

impl<A, B, T> Module for Chain<A, B>
where
    A: Module<Input = T>,
    B: Module<Input = A::Output>,
{
    type Input = T;
    type Output = B::Output;

    fn forward(&self, x: &Self::Input) -> Self::Output {
        self.b.forward(&self.a.forward(x))
    }

    fn gather_params(&self, params: &mut HashMap<usize, Tensor>) {
        self.a.gather_params(params);
        self.b.gather_params(params);
    }

    fn update_params(&self, params: &mut HashMap<usize, Tensor>) {
        self.a.update_params(params);
        self.b.update_params(params);
    }

    fn gather_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>) {
        self.a.gather_named_params(prefix, params);
        self.b.gather_named_params(prefix, params);
    }

    fn update_named_params(&self, prefix: &str, params: &mut HashMap<String, Tensor>) {
        self.a.update_named_params(prefix, params);
        self.b.update_named_params(prefix, params);
    }
}