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
#![allow(clippy::too_many_arguments)]
use tensor_rs::tensor::{Tensor, PaddingMode};
use std::cell::{RefCell};
use std::rc::Rc;
use super::{OpTrait, OpCall, Op, OpHandle};
use crate::var::Var;
use crate::err::AutoDiffError;
pub struct Conv1d {
alpha: f32,
handle: OpHandle,
}
impl Conv1d {
pub fn new(alpha: f32) -> Conv1d {
Conv1d {
alpha,
handle: OpHandle::new(),
}
}
handle_method!();
}
impl OpTrait for Conv1d {
fn get_name(&self) -> String {
"Conv1d".to_string()
}
fn get_input_size(&self) -> usize {
2
}
fn get_output_size(&self) -> usize {
1
}
fn apply(&self, input: &[Tensor], output: &[Tensor]) {
unimplemented!();
}
fn grad(&self, input: &[Tensor],
output_grad: &[Tensor],
input_grad: &[Tensor]) {
unimplemented!();
}
fn get_values(&self) -> Vec<Tensor> {
Vec::new()
}
fn set_values(&self, v: &[Tensor]) {
}
fn get_grads(&self) -> Vec<Tensor> {
Vec::new()
}
}
pub struct Conv2d {
in_channels: usize,
out_channels: usize,
kernel_size: (usize, usize),
stride: (usize, usize),
padding: (usize, usize),
dilation: (usize, usize),
groups: usize,
bias_option: bool,
padding_mode: PaddingMode,
weight: Tensor,
bias: Tensor,
weight_grad: Tensor,
bias_grad: Tensor,
handle: OpHandle,
}
impl Conv2d {
pub fn new(in_channels: usize, out_channels: usize,
kernel_size: (usize, usize),
stride: (usize, usize),
padding: (usize, usize),
dilation: (usize, usize),
bias: bool,
padding_mode: PaddingMode
) -> Conv2d {
Conv2d {
in_channels,
out_channels,
kernel_size,
stride,
padding,
dilation,
groups: 1,
bias_option: bias,
padding_mode,
weight: Tensor::empty(&[out_channels, in_channels, kernel_size.0, kernel_size.1]),
bias: Tensor::empty(&[out_channels, ]),
weight_grad: Tensor::empty(&[out_channels, in_channels, kernel_size.0, kernel_size.1]),
bias_grad: Tensor::empty(&[out_channels, ]),
handle: OpHandle::new(),
}
}
pub fn weight(&self) -> &Tensor {
&self.weight
}
pub fn set_weight(&self, var: Var) {
self.weight.swap(&var.val());
}
pub fn bias(&self) -> &Tensor {
&self.bias
}
pub fn set_bias(&self, var: Var) {
self.bias.swap(&var.val());
}
handle_method!();
}
impl OpCall for Conv2d {
fn call(&mut self, inputs: &[&Var]) -> Result<Vec<Var>, AutoDiffError> {
let new_one = Conv2d {
in_channels: self.in_channels,
out_channels: self.out_channels,
kernel_size: self.kernel_size,
stride: self.stride,
padding: self.padding,
dilation: self.dilation,
groups: self.groups,
bias_option: self.bias_option,
padding_mode: self.padding_mode,
weight: self.weight.ref_copy(),
bias: self.bias.ref_copy(),
weight_grad: self.weight_grad.ref_copy(),
bias_grad: self.bias_grad.ref_copy(),
handle: OpHandle::new(),
};
let op = Op::new(Rc::new(RefCell::new(Box::new(new_one))));
inputs[0].called_with(op, &inputs[1..inputs.len()])
}
}
impl OpTrait for Conv2d {
fn get_name(&self) -> String {
"Conv2d".to_string()
}
fn get_input_size(&self) -> usize {
1
}
fn get_output_size(&self) -> usize {
1
}
fn apply(&self, input: &[Tensor], output: &[Tensor]) {
if self.groups > 1 {
unimplemented!();
}
if self.weight.size()[2] != self.kernel_size.0 || self.weight.size()[3] != self.kernel_size.1 {
panic!("this is conv2d");
}
let input_size = input[0].size();
if input_size[1] != self.in_channels {
panic!("conv2d expect the same input channel: input: {:?}, config: {:?}", input_size[1], self.in_channels);
}
let conv_output = input[0].conv2d(&self.weight, self.stride, self.padding, self.dilation, self.padding_mode);
if conv_output.size()[1] != self.out_channels {
panic!("conv2d expect the same input channel {:?}, {:?}", input_size[1], self.in_channels);
}
if self.bias_option {
let expanded_bias = self.bias
.unsqueeze(1)
.unsqueeze(2)
.repeat(&[1, conv_output.size()[2], conv_output.size()[3]]);
let ret = conv_output.add(&expanded_bias);
output[0].swap(&ret);
} else {
output[0].swap(&conv_output);
}
}
fn grad(&self, input: &[Tensor], output_grad: &[Tensor], input_grad: &[Tensor]) {
let (w_grad, d_grad) = input[0].conv2d_grad(&self.weight,
self.stride,
self.padding,
self.dilation,
self.padding_mode,
&output_grad[0]);
self.weight_grad.swap(&w_grad);
input_grad[0].swap(&d_grad);
if self.bias_option {
self.bias_grad.swap(&output_grad[0].mean(Some(&[0, 2, 3]), false));
}
}
fn get_values(&self) -> Vec<Tensor> {
vec![self.weight.ref_copy(), self.bias.ref_copy()]
}
fn set_values(&self, v: &[Tensor]) {
self.weight.data_copy(&v[0]);
self.bias.data_copy(&v[1]);
}
fn get_grads(&self) -> Vec<Tensor> {
vec![self.weight_grad.ref_copy(), self.bias_grad.ref_copy()]
}
}