/
test_timer.py
184 lines (145 loc) · 5.59 KB
/
test_timer.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
184
import time
import unittest
import numpy
import six
import chainer
from chainer import cuda
from chainer import function_hooks
from chainer import functions
from chainer.functions.math import basic_math
from chainer import testing
from chainer.testing import attr
def check_history(self, t, function_type, return_type):
func = getattr(t[0], 'function', t[0])
self.assertIsInstance(func, function_type)
self.assertIsInstance(t[1], return_type)
class SimpleLink(chainer.Link):
def __init__(self):
super(SimpleLink, self).__init__()
with self.init_scope():
init_w = numpy.random.uniform(-1, 1, (3, 5)).astype(
numpy.float32)
self.w = chainer.Parameter(init_w)
def __call__(self, x):
return self.w * x
class TestTimerHookToLink(unittest.TestCase):
def setUp(self):
self.h = function_hooks.TimerHook()
self.l = SimpleLink()
self.x = numpy.random.uniform(-0.1, 0.1, (3, 5)).astype(numpy.float32)
self.gy = numpy.random.uniform(-0.1, 0.1, (3, 5)).astype(numpy.float32)
def test_name(self):
self.assertEqual(self.h.name, 'TimerHook')
def check_forward(self, x):
with self.h:
self.l(chainer.Variable(x))
self.assertEqual(1, len(self.h.call_history))
check_history(self, self.h.call_history[0], basic_math.Mul, float)
def test_forward_cpu(self):
self.check_forward(self.x)
@attr.gpu
def test_forward_gpu(self):
self.l.to_gpu()
self.check_forward(cuda.to_gpu(self.x))
def check_backward(self, x, gy):
x = chainer.Variable(x)
y = self.l(x)
y.grad = gy
with self.h:
y.backward()
# It includes forward of + that accumulates gradients to W and b
self.assertEqual(3, len(self.h.call_history), self.h.call_history)
for entry in self.h.call_history:
if entry[0].label == '_ + _':
continue
check_history(self, entry, basic_math.Mul, float)
def test_backward_cpu(self):
self.check_backward(self.x, self.gy)
@attr.gpu
def test_backward_gpu(self):
self.l.to_gpu()
self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
class TestTimerHookToFunction(unittest.TestCase):
def setUp(self):
self.h = function_hooks.TimerHook()
self.f = functions.Exp()
self.f.add_hook(self.h)
self.x = numpy.random.uniform(-0.1, 0.1, (3, 5)).astype(numpy.float32)
self.gy = numpy.random.uniform(-0.1, 0.1, (3, 5)).astype(numpy.float32)
def check_forward(self, x):
self.f(chainer.Variable(x))
self.assertEqual(1, len(self.h.call_history))
check_history(self, self.h.call_history[0], functions.Exp, float)
def test_forward_cpu(self):
self.check_forward(self.x)
@attr.gpu
def test_forward_gpu(self):
self.check_forward(cuda.to_gpu(self.x))
def check_backward(self, x, gy):
x = chainer.Variable(x)
y = self.f(x)
y.grad = gy
y.backward()
self.assertEqual(2, len(self.h.call_history))
check_history(self, self.h.call_history[1], functions.Exp, float)
def test_backward_cpu(self):
self.check_backward(self.x, self.gy)
@attr.gpu
def test_backward_gpu(self):
self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
def test_reentrant(self):
# In/grad data are random; these do not simulate the actually possible
# cases.
g = functions.Identity() # any function other than Exp is ok
self.h.backward_preprocess(self.f, (self.x,), (self.gy,))
t1 = time.time()
time.sleep(0.001) # longer than each hook call
self.h.forward_preprocess(g, (self.x,))
self.h.forward_postprocess(g, (self.x,))
t2 = time.time()
self.h.backward_postprocess(self.f, (self.x,), (self.gy,))
history = dict(self.h.call_history)
self.assertEqual(len(history), 2)
self.assertIn(self.f, history)
self.assertIn(g, history)
f_time = history[self.f]
g_time = history[g]
self.assertLessEqual(g_time, t2 - t1)
self.assertGreaterEqual(f_time, t2 - t1)
def test_reentrant_total_time(self):
g = functions.Identity()
t0 = time.time()
self.h.backward_preprocess(self.f, (self.x,), (self.gy,))
t1 = time.time()
self.h.forward_preprocess(g, (self.x,))
time.sleep(0.001)
self.h.forward_postprocess(g, (self.x,))
t2 = time.time()
self.h.backward_postprocess(self.f, (self.x,), (self.gy,))
t3 = time.time()
self.assertLessEqual(self.h.total_time(), t3 - t0)
self.assertGreaterEqual(self.h.total_time(), t2 - t1)
class TestTimerPrintReport(unittest.TestCase):
def setUp(self):
self.h = function_hooks.TimerHook()
self.f = functions.Exp()
self.f.add_hook(self.h)
self.x = numpy.random.uniform(-0.1, 0.1, (3, 5)).astype(numpy.float32)
def test_summary(self):
x = self.x
self.f(chainer.Variable(x))
self.f(chainer.Variable(x))
self.assertEqual(2, len(self.h.call_history))
self.assertEqual(1, len(self.h.summary()))
def test_print_report(self):
x = self.x
self.f(chainer.Variable(x))
self.f(chainer.Variable(x))
io = six.StringIO()
self.h.print_report(file=io)
expect = r'''\AFunctionName ElapsedTime Occurrence
+Exp +[0-9.\-e]+.s +[0-9]+$
'''
actual = io.getvalue()
six.assertRegex(self, actual, expect)
testing.run_module(__name__, __file__)