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
pub use self::activation::*;
pub use self::arithmetic::{Add, Mult};
pub use self::conv::Conv;
pub use self::conv::Padding;
pub use self::embedding::Embedding;
pub use self::global_pool::GlobalPool;
pub use self::matmul::MatMul;
use graph::Idx;
use ndarray::prelude::*;
use std::fmt::Debug;
mod activation;
mod arithmetic;
mod conv;
mod embedding;
mod global_pool;
mod matmul;
pub trait Operation: Debug {
fn eval(&self, inputs: &[ArrayViewD<f32>]) -> ArrayD<f32>;
fn grad(&self, inputs: &[ArrayViewD<f32>], loss: ArrayViewD<f32>) -> Vec<ArrayD<f32>>;
}
serialize_trait_object!(Operation);
#[derive(DebugStub, Serialize, Deserialize)]
pub enum Node {
Input {
#[serde(skip)]
#[debug_stub = "Option<Box<Iterator<Item=ArrayD<f32>>>>"]
it: Option<Box<Iterator<Item = ArrayD<f32>>>>,
},
Parameter(Box<[usize]>),
Conv { kernel: Idx, img: Idx, conv: Conv },
Add { xs: Vec<Idx> },
Mult { xs: Vec<Idx> },
MatMul { mat: Idx, v: Idx },
Activation { x: Idx, a: Activation },
Embedding { emb: Idx, code: Idx },
GlobalPool { pool: GlobalPool, x: Idx },
Operation {
inputs: Box<[Idx]>,
#[serde(skip_deserializing)]
operation: Box<Operation>,
},
Constant,
}
impl Node {
pub fn inputs(&self) -> Vec<Idx> {
match self {
Node::Conv { kernel, img, .. } => vec![*kernel, *img],
Node::Add { xs } => xs.to_vec(),
Node::Mult { xs } => xs.to_vec(),
Node::MatMul { mat, v } => vec![*mat, *v],
Node::Activation { x, .. } => vec![*x],
Node::Embedding { emb, code } => vec![*emb, *code],
Node::GlobalPool { x, .. } => vec![*x],
Node::Operation { inputs, .. } => inputs.to_vec(),
Node::Input { .. } | Node::Parameter(..) | Node::Constant => vec![],
}
}
pub fn forward(&mut self, inputs: &[ArrayViewD<f32>]) -> Option<ArrayD<f32>> {
match self {
Node::Conv { conv, .. } => Some(conv.eval(inputs)),
Node::Add { .. } => Some(Add().eval(inputs)),
Node::Mult { .. } => Some(Mult().eval(inputs)),
Node::MatMul { .. } => Some(MatMul().eval(inputs)),
Node::Activation { a, .. } => Some(a.eval(inputs)),
Node::Embedding { .. } => Some(Embedding().eval(inputs)),
Node::GlobalPool { pool, .. } => Some(pool.eval(inputs)),
Node::Operation { operation, .. } => Some(operation.eval(inputs)),
Node::Input { ref mut it } => it.as_mut().expect("Input node uninitialized.").next(),
Node::Parameter(..) | Node::Constant => None,
}
}
pub fn backward(&self, inputs: &[ArrayViewD<f32>], loss: &ArrayD<f32>) -> Vec<ArrayD<f32>> {
match self {
Node::Conv { conv, .. } => conv.grad(inputs, loss.view()),
Node::Add { .. } => Add().grad(inputs, loss.view()),
Node::Mult { .. } => Mult().grad(inputs, loss.view()),
Node::MatMul { .. } => MatMul().grad(inputs, loss.view()),
Node::Activation { a, .. } => a.grad(inputs, loss.view()),
Node::Embedding { .. } => Embedding().grad(inputs, loss.view()),
Node::GlobalPool { pool, .. } => pool.grad(inputs, loss.view()),
Node::Operation { operation, .. } => operation.grad(inputs, loss.view()),
Node::Input { .. } | Node::Constant | Node::Parameter(..) => vec![],
}
}
}
impl Default for Box<Operation> {
fn default() -> Self {
Box::new(arithmetic::Add())
}
}