-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathself_consistency_test.py
168 lines (123 loc) · 5.64 KB
/
self_consistency_test.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
# Copyright (c) 2023 Khaleelulla Khan Nazeer
# This file incorporates work covered by the following copyright:
# Copyright 2020 LMNT, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import unittest
from unittest import mock
import torch
import evnn_pytorch as evnn
import numpy as np
RNN_MAP = {
'egru': evnn.EGRU,
}
batch_size = 10
time_steps = 8
input_size = 4
hidden_size = 8
error_tol = 1e-3
@unittest.skipUnless(torch.cuda.is_available(), 'CUDA not available')
class EGRUCUDAForwardTest(unittest.TestCase):
def setUp(self):
rnn = RNN_MAP['egru']
self.seed = 5526
torch.manual_seed(self.seed)
torch.cuda.manual_seed(self.seed)
np.random.seed(self.seed)
x = torch.rand(batch_size, time_steps, input_size, dtype=torch.float32)
self.egru = rnn(input_size, hidden_size, zoneout=0.0, batch_first=True)
self.egru.cuda()
self.x_cuda = x.clone().cuda()
self.x_cuda_torch = self.x_cuda.detach().clone()
def test_forward_y(self):
with torch.no_grad():
y1, _ = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
y2, _ = self.egru.forward(self.x_cuda_torch)
assert torch.allclose(y1, y2, atol=error_tol)
def test_forward_h(self):
with torch.no_grad():
_, (h1, _, _) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (h2, _, _) = self.egru.forward(self.x_cuda_torch)
assert torch.allclose(h1, h2, atol=error_tol)
def test_forward_o(self):
with torch.no_grad():
_, (_, o1, _) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (_, o2, _) = self.egru.forward(self.x_cuda_torch)
assert torch.allclose(o1, o2)
def test_forward_trace(self):
with torch.no_grad():
_, (_, _, t1) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (_, _, t2) = self.egru.forward(self.x_cuda_torch)
assert torch.allclose(t1, t2, atol=error_tol)
@unittest.skipUnless(torch.cuda.is_available(), 'CUDA not available')
class EGRUCUDABackwardTest(unittest.TestCase):
def setUp(self):
rnn = RNN_MAP['egru']
self.seed = 5526
torch.manual_seed(self.seed)
torch.cuda.manual_seed(self.seed)
np.random.seed(self.seed)
x = torch.rand(batch_size, time_steps, input_size, dtype=torch.float32)
self.egru = rnn(input_size, hidden_size, zoneout=0.0, batch_first=True)
self.egru.cuda()
self.x_cuda = x.clone().cuda()
self.x_cuda_torch = self.x_cuda.detach().clone()
self.x_cuda.requires_grad_(True)
self.x_cuda_torch.requires_grad_(True)
def test_backward_y(self):
y1, _ = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
y2, _ = self.egru.forward(self.x_cuda_torch)
y1.backward(torch.ones_like(y1), retain_graph=True)
y2.backward(torch.ones_like(y2), retain_graph=True)
assert torch.allclose(self.x_cuda_torch.grad.data,
self.x_cuda.grad.data, atol=error_tol)
def test_backward_h(self):
_, (h1, _, _) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (h2, _, _) = self.egru.forward(self.x_cuda_torch)
h1.backward(torch.ones_like(h1), retain_graph=True)
h2.backward(torch.ones_like(h2), retain_graph=True)
assert torch.allclose(self.x_cuda_torch.grad.data,
self.x_cuda.grad.data, atol=error_tol)
def test_backward_o(self):
_, (_, o1, _) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (_, o2, _) = self.egru.forward(self.x_cuda_torch)
o1.backward(torch.ones_like(o1), retain_graph=True)
o2.backward(torch.ones_like(o2), retain_graph=True)
assert torch.allclose(self.x_cuda_torch.grad.data,
self.x_cuda.grad.data, atol=error_tol)
def test_backward_trace(self):
_, (_, _, t1) = self.egru.forward(self.x_cuda)
torch.manual_seed(self.seed)
with mock.patch.object(self.egru, "use_custom_cuda", False):
_, (_, _, t2) = self.egru.forward(self.x_cuda_torch)
t1.backward(torch.ones_like(t1), retain_graph=True)
t2.backward(torch.ones_like(t2), retain_graph=True)
assert torch.allclose(self.x_cuda_torch.grad.data,
self.x_cuda.grad.data, atol=error_tol)
if __name__ == '__main__':
unittest.main()