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
#![allow(clippy::redundant_closure)]
use std::collections::{BTreeSet, BTreeMap};

use crate::collection::generational_index::{GenIndex, NetIndex};
use crate::collection::graph::Graph;
use tensor_rs::tensor::Tensor;
use crate::op::*;
use crate::var::*;

/// The computation network.
/// Connection has duplication.
pub struct Net {
    data: GenIndex<Tensor>,
    ops: GenIndex<Op>,
    funcs: BTreeMap<NetIndex, Vec<NetIndex>>, // for func composition
    set_mark: BTreeSet<NetIndex>,
    graph: Graph<NetIndex, NetIndex>,
    data_grad: BTreeMap<NetIndex, Tensor>,
}

impl Net {
    pub fn new() -> Net {
        Net {
            data: GenIndex::new(),
            ops: GenIndex::new(),
            funcs: BTreeMap::new(),
            set_mark: BTreeSet::new(),
            graph: Graph::new(),
            data_grad: BTreeMap::new(),
        }
    }

    pub fn get_data(&self) -> &GenIndex<Tensor> {
        &self.data
    }

    pub fn get_data_mut(&mut self) -> &mut GenIndex<Tensor> {
        &mut self.data
    }

    pub fn get_op(&self, func: &Func) -> Option<&Op> {
        self.ops.get(func.get_id())
    }

    pub fn is_dangling_var(&self, var: &Var) -> Result<bool, ()> {
        if !self.data.contains(var.get_id()) {
            Err(())
        } else if self.graph.iter_op_given_input(var.get_id()).expect("").count() == 0 &&
            self.graph.iter_op_given_output(var.get_id()).expect("").count() == 0{
                Ok(true)
            } else {
                Ok(false)
            }

    }

    pub fn get_grad(&self)  -> &BTreeMap<NetIndex, Tensor> {
        &self.data_grad
    }

    ///
    /// Return one output variable id if there is one.
    ///
    pub fn get_func_output(&self, func: &Func) -> Option<NetIndex> {
        for i in self.graph.iter_output_given_op(func.get_id()).ok()? {
            return Some(*i)
        }
        None
    }

    /// Insert an empty var into the network.
    pub fn init_var(&mut self) -> NetIndex {
        let id = self.data.insert(Tensor::new());
        self.graph.add_data(&id).expect("");
        id
    }

    pub fn drop_var(&mut self, var: &Var) {
        self.data.remove(var.get_id()).expect("");
        self.graph.drop_data(var.get_id()).expect("");
    }

    /// Insert operator into the network.
    pub fn init_op(&mut self, op: Op) -> NetIndex {
        let id = self.ops.insert(op);
        self.graph.add_op(&id).expect("");
        self.funcs.insert(id, Vec::new());
        id
    }

    ///
    /// For Module::func, insert a new composed func.
    ///
    pub fn init_func(&mut self, funcs: &[NetIndex]) -> NetIndex {
        let id = self.ops.insert(Op::nop());
        self.graph.add_op(&id).expect("");
        self.funcs.insert(id, funcs.to_vec());
        id
    }

    ///
    /// Remove a concrete op or composed func from the graph.
    ///
    pub fn del_func_or_op(&mut self, func: &Func) {
        let _ = self.ops.remove(func.get_id());
        let _ = self.graph.drop_op(func.get_id());

        // ignore the result as to allow duplicate delete

        //
        // The following dosen't work 
        // because if the composed goes out of scope doesn't mean
        //     its member ops go out of scope.
        //
        // Check to see the func type.
        // If it is a op, delete it
        // If it is a func, find all the underlying op
        //     and var in between and remove them.
        //

    }

    ///
    /// Disconnect the variable and the function the variable is the input.
    /// Delete the variable if it becomes the dangling variable.
    ///
    pub fn decouple_input(&mut self, func: &Func) -> Vec<NetIndex> {
        let mut decoupled_inputs = Vec::new();
        let inputs: Vec<NetIndex> = self.graph.iter_input_given_op(func.get_id())
            .expect("").map(|x| x.clone()).collect();
        for i in inputs {
            self.graph.decouple_data_func(&i, func.get_id()).expect("");
            decoupled_inputs.push(i);
        }
        decoupled_inputs
    }

    ///
    /// Return a vec of sub ops for the given op.
    /// Empty should be returned if the input is a concrete op.
    ///
    pub fn get_sub_func(&self, func: NetIndex) -> Vec<NetIndex> {
        self.funcs.get(&func).expect("").to_vec()
    }

    ///
    /// Check the func is concrete or not.
    ///
    pub fn is_composed(&self, func: &Func) -> Result<bool, ()> {
        if self.ops.contains(func.get_id()) {
            if !self.funcs.get(func.get_id()).expect("").is_empty() {
                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            Err(())
        }
    }

    ///
    /// Build input-operator-output relation, with given components.
    ///
    pub fn connect(&mut self, input: &[NetIndex], op: Op, output: &[NetIndex]) {
        println!("Deprecated! Graph::connect");
        let opid = self.init_op(op);
        self.graph.connect(input, output, &opid).expect("");
    }

    pub fn connect2(&mut self, input: &[&Var], func: &Func, output: &[&Var]) {
        // connect if there is not connection.
        // do nothing if there is already a connection.
        let mut input_ids = Vec::with_capacity(input.len());
        for i in input {
            input_ids.push(*i.get_id());
        }
        let mut output_ids = Vec::with_capacity(output.len());
        for i in output {
            output_ids.push(*i.get_id());
        }
        
        self.graph.connect(&input_ids, &output_ids, func.get_id()).expect("");
    }

    /// set the set_mark, set_mark is used to label var with input value with it.
    pub fn set_mark(&mut self, did: &NetIndex) {
        self.set_mark.insert(*did);
    }
    pub fn unset_mark(&mut self, did: &NetIndex) {
        self.set_mark.remove(did);
    }

    /// Merge two computation graph
    //fn merge(&self, o: &Net) -> Net {
    //    Net::new()
    //}

    /// Forward evaluate the computaiton graph.
    pub fn eval(&mut self) -> Result<(), BTreeSet<NetIndex>> {
        println!("Deprecated! no more whole network forward pass.");
        let mut all_input = Vec::new();
        for i in &self.set_mark {
            all_input.push(*i);
        }
        
        self.graph
            .walk(
                &all_input[..],
                true,
                |input, output, op| {
                    //println!("op: {}", self.ops.get(op).expect("").get_name());
                    
                    let mut inputs: Vec<&Tensor> = Vec::new();
                    for input_id in input {
                        let a = self.data.get(input_id).expect("");
                        inputs.push(a);
                    }

                    let mut outputs: Vec<&Tensor> = Vec::new();
                    for output_id in output {
                        let a = self.data.get(output_id).expect("");
                        outputs.push(a);
                    }

                    self.ops
                        .get(op)
                        .expect("")
                        .apply(&inputs, &outputs);
                    
                    //println!("var.rs: {:?}", outputs[0].size());
                    
                }
            )?;

        Ok(())
    }

    pub fn eval_op(&self, input: &[&Var], func: &Func, output: &[&Var]) {
        let mut inputs: Vec<&Tensor> = Vec::new();
        for input_var in input {
            let a = self.data.get(input_var.get_id()).expect("");
            inputs.push(a);
        }

        let mut outputs: Vec<&Tensor> = Vec::new();
        for output_var in output {
            let a = self.data.get(output_var.get_id()).expect("");
            outputs.push(a);
        }

        self.ops
            .get(func.get_id())
            .expect("")
            .apply(&inputs, &outputs);
    }

    pub fn bptt_scale(&mut self, r: f32) {
        let output = self.graph.get_output_edge_data();
        let mut output_grad = BTreeMap::new();
        for i in &output {
            output_grad.insert(*i,
                               Tensor::fill(&self.data.get(i).expect("").size(),
                                            r));
        }
        self.bptt(&output_grad);
    }

    pub fn bptt(&mut self, output_grad: &BTreeMap<NetIndex, Tensor>) {
        let mut output = Vec::new();
        self.data_grad.clear();
        for (k, v) in output_grad {
            output.push(*k);
            self.data_grad.insert(*k, v.clone());
        }

        for i in self.graph.iter_data() {
            self.data_grad.entry(*i).or_insert(Tensor::new());
        }
        
        self.graph
            .walk(
                &output[..],
                false,
                |output_grads, input_grads, op| {
                    //println!("op, bptt: {}", self.ops.get(op).expect("").get_name());

                    // collect input tensor.
                    let mut inputs: Vec<&Tensor> = Vec::new();
                    for input_id in input_grads {
                        //println!("bptt {:?}", input_id);
                        let a = self.data.get(input_id).expect("");
                        inputs.push(a);
                    }
                    //println!("input: size {:?}", inputs.len());

                    // collect the output tensor gradient (forward view).
                    let mut output_grad: Vec<&Tensor> = Vec::new();
                    for output_id in output_grads {
                        //println!("bptt 2 {:?}", output_id);
                        let a = self.data_grad.get(output_id).expect("");
                        output_grad.push(a);
                    }
                    //println!("output grad: size {:?}", output_grad.len());
                    
                    // collect the input tensor gradient (forward view).
                    let mut input_grad: Vec<&Tensor> = Vec::new();
                    for input_id in input_grads {
                        //println!("bptt 3 {:?}", input_id);
                        let a = self.data_grad.get(input_id).expect("");
                        input_grad.push(a);
                    }
                    //println!("input grad: size {:?}", input_grad.len());

                    self.ops
                        .get(op)
                        .expect("")
                        .grad(&inputs, &output_grad, &input_grad);
                    
                    //println!("var.rs: {:?}", 1);
                    
                }
            ).expect("");
    }

    /// Iterate over all ops, no order guarantee
    pub fn visit_op<F>(&mut self, closure: F,
                       allow: Option<Vec<NetIndex>>,
                       skip: Option<Vec<NetIndex>>)
    where F: Fn(&Op) {
        let allow_list = if let Some(val) = allow { val } else {Vec::new()};
        let skip_list = if let Some(val) = skip {val} else {Vec::new()};
        
        for i in self.graph.iter_op() {
            if (allow_list.is_empty() && skip_list.is_empty()) ||
                (!allow_list.is_empty() && allow_list.contains(i)) ||
                (!skip_list.is_empty() && !skip_list.contains(i) ) {
                    closure(self.ops.get(i).expect(""));
            }
        }
    }

    pub fn visit_data<F>(&mut self, closure: F)
    where F: Fn(NetIndex, &Tensor) {
        for i in self.graph.iter_data() {
            closure(*i, self.data.get(i).expect(""));
        }
    }

    pub fn append(&mut self, other: &mut Self) {
        self.data.append(&mut other.data);
    }
}