-
Notifications
You must be signed in to change notification settings - Fork 55
/
irnn.py
183 lines (137 loc) · 5.92 KB
/
irnn.py
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
import torch
import math
import cupy
from torch import nn
import torch.nn.functional as F
class Stream:
ptr = torch.cuda.current_stream().cuda_stream
IRNNForward = open('./IRNN_Forward_cuda.cu','r').read()
IRNNBackward = open('./IRNN_Backward_cuda.cu','r').read()
@cupy.util.memoize(for_each_device=True)
def cunnex(strFunction):
return cupy.cuda.compile_with_cache(globals()[strFunction]).get_function(strFunction)
# end
class irnn(torch.autograd.Function):
def __init__(self):
super(irnn, self).__init__()
def forward(self, input_feature, weight_up, weight_right, weight_down, weight_left, bias_up, bias_right, bias_down, bias_left):
assert(input_feature.is_contiguous() == True)
assert(weight_left.is_contiguous() == True)
assert(weight_right.is_contiguous() == True)
assert(weight_down.is_contiguous() == True)
assert(weight_up.is_contiguous() == True)
assert(bias_left.is_contiguous() ==True)
assert(bias_right.is_contiguous() == True)
assert(bias_up.is_contiguous() == True)
assert(bias_down.is_contiguous() == True)
output_left = input_feature.clone()
output_right = input_feature.clone()
output_up = input_feature.clone()
output_down = input_feature.clone()
if input_feature.is_cuda == True:
n = input_feature.nelement()
cuda_num_threads = 1024
cunnex('IRNNForward')(
grid=tuple([ int((n + cuda_num_threads - 1) / cuda_num_threads ), 1, 1 ]),
block=tuple([ cuda_num_threads , 1, 1 ]),
args=[
input_feature.data_ptr(),
weight_up.data_ptr(),
weight_right.data_ptr(),
weight_down.data_ptr(),
weight_left.data_ptr(),
bias_up.data_ptr(),
bias_right.data_ptr(),
bias_down.data_ptr(),
bias_left.data_ptr(),
output_up.data_ptr(),
output_right.data_ptr(),
output_down.data_ptr(),
output_left.data_ptr(),
input_feature.size(1),
input_feature.size(2),
input_feature.size(3),
n],
stream=Stream
)
elif input_feature.is_cuda == False:
raise NotImplementedError()
self.save_for_backward(input_feature,weight_up,weight_right,weight_down,weight_left,output_up,output_right,output_down,output_left)
return output_up,output_right,output_down,output_left
# end
def backward(self, grad_output_up,grad_output_right,grad_output_down,grad_output_left):
input_feature,weight_up,weight_right,weight_down,weight_left,output_up,output_right,output_down,output_left = self.saved_tensors
# print(weight_left)
if grad_output_up.is_contiguous() != True:
grad_output_up = grad_output_up.contiguous()
if grad_output_right.is_contiguous() != True:
grad_output_right = grad_output_right.contiguous()
if grad_output_down.is_contiguous() != True:
grad_output_down = grad_output_down.contiguous()
if grad_output_left.is_contiguous() != True:
grad_output_left = grad_output_left.contiguous()
# init gradient of input_feature
grad_input = torch.zeros_like(input_feature)
# init gradient map of weights
grad_weight_up_map = torch.zeros_like(input_feature)
grad_weight_right_map = torch.zeros_like(input_feature)
grad_weight_down_map = torch.zeros_like(input_feature)
grad_weight_left_map = torch.zeros_like(input_feature)
# init gradient of weights
grad_weight_left = torch.zeros_like(weight_left)
grad_weight_right = torch.zeros_like(weight_left)
grad_weight_up = torch.zeros_like(weight_left)
grad_weight_down = torch.zeros_like(weight_left)
grad_bias_up_map = torch.zeros_like(input_feature)
grad_bias_right_map = torch.zeros_like(input_feature)
grad_bias_down_map = torch.zeros_like(input_feature)
grad_bias_left_map = torch.zeros_like(input_feature)
if input_feature.is_cuda == True:
n = grad_input.nelement()
cuda_num_threads = 1024
cunnex('IRNNBackward')(
grid=tuple([ int((n + cuda_num_threads - 1) / cuda_num_threads), 1, 1 ]),
block=tuple([ cuda_num_threads , 1, 1 ]),
args=[
grad_input.data_ptr(),
grad_weight_up_map.data_ptr(),
grad_weight_right_map.data_ptr(),
grad_weight_down_map.data_ptr(),
grad_weight_left_map.data_ptr(),
grad_bias_up_map.data_ptr(),
grad_bias_right_map.data_ptr(),
grad_bias_down_map.data_ptr(),
grad_bias_left_map.data_ptr(),
weight_up.data_ptr(),
weight_right.data_ptr(),
weight_down.data_ptr(),
weight_left.data_ptr(),
grad_output_up.data_ptr(),
grad_output_right.data_ptr(),
grad_output_down.data_ptr(),
grad_output_left.data_ptr(),
output_up.data_ptr(),
output_right.data_ptr(),
output_down.data_ptr(),
output_left.data_ptr(),
input_feature.size(1),
input_feature.size(2),
input_feature.size(3),
n],
stream=Stream
)
grad_bias_up = torch.zeros_like(weight_left).reshape(weight_left.size(0))
grad_bias_right = torch.zeros_like(weight_left).reshape(weight_left.size(0))
grad_bias_down = torch.zeros_like(weight_left).reshape(weight_left.size(0))
grad_bias_left = torch.zeros_like(weight_left).reshape(weight_left.size(0))
grad_weight_left = grad_weight_left_map.sum(2).sum(2).sum(0).resize_as_(grad_weight_left)
grad_weight_right = grad_weight_right_map.sum(2).sum(2).sum(0).resize_as_(grad_weight_left)
grad_weight_up = grad_weight_up_map.sum(2).sum(2).sum(0).resize_as_(grad_weight_left)
grad_weight_down = grad_weight_down_map.sum(2).sum(2).sum(0).resize_as_(grad_weight_left)
grad_bias_up = grad_bias_up_map.sum(2).sum(2).sum(0).resize_as_(grad_bias_up)
grad_bias_right = grad_bias_right_map.sum(2).sum(2).sum(0).resize_as_(grad_bias_up)
grad_bias_down = grad_bias_down_map.sum(2).sum(2).sum(0).resize_as_(grad_bias_up)
grad_bias_left = grad_bias_left_map.sum(2).sum(2).sum(0).resize_as_(grad_bias_up)
elif input_feature.is_cuda == False:
raise NotImplementedError()
return grad_input, grad_weight_up,grad_weight_right,grad_weight_down,grad_weight_left,grad_bias_up, grad_bias_right, grad_bias_down, grad_bias_left