-
Notifications
You must be signed in to change notification settings - Fork 45
/
tests.lua
236 lines (191 loc) · 6.77 KB
/
tests.lua
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
233
234
235
236
local optnet = require 'optnet.env'
local models = require 'optnet.models'
local utils = require 'optnet.utils'
local countUsedMemory = optnet.countUsedMemory
local optest = torch.TestSuite()
local tester = torch.Tester()
local use_cudnn = false
local backward_tol = 1e-6
local function resizeAndConvert(input, type)
local res
local s = 64
if torch.isTensor(input) then
local iSize = torch.Tensor(input:size():totable())[{{2,-1}}]
res = torch.rand(s,table.unpack(iSize:totable())):type(type)
else
res = {}
for k, v in ipairs(input) do
res[k] = resizeAndConvert(v,type)
end
end
return res
end
local function cudnnSetDeterministic(net)
net:apply(function(m)
if m.setMode then m:setMode(1, 1, 1) end
end)
end
local function genericTestForward(model,opts)
local net, input = models[model](opts)
net:evaluate()
if use_cudnn then
cudnn.convert(net,cudnn);
net:cuda();
input = resizeAndConvert(input,'torch.CudaTensor')
end
local out_orig = utils.recursiveClone(net:forward(input))
local mems1 = optnet.countUsedMemory(net)
optnet.optimizeMemory(net, input)
local out = utils.recursiveClone(net:forward(input))
local mems2 = countUsedMemory(net)
tester:eq(out_orig, out, 'Outputs differ after optimization of '..model)
local mem1 = mems1.total_size
local mem2 = mems2.total_size
local omem1 = mems1.outputs
local omem2 = mems2.outputs
local bmem1 = mems1.buffers
local bmem2 = mems2.buffers
local pmem1 = mems1.params
local pmem2 = mems2.params
tester:assertle(mem2, mem1, 'Optimized model uses more memory! '..
'Before: '.. mem1..' bytes, After: '..mem2..' bytes')
print('Memory use')
print('Total', mem1/1024/1024, mem2/1024/1024, 1-mem2/mem1)
print('Outputs',omem1/1024/1024,omem2/1024/1024, 1-omem2/omem1)
print('Buffers',bmem1/1024/1024,bmem2/1024/1024, 1-bmem2/bmem1)
print('Params', pmem1/1024/1024,pmem2/1024/1024, 1-pmem2/pmem1)
end
-------------------------------------------------
-- Backward
-------------------------------------------------
local function genericTestBackward(model,opts)
local net, input = models[model](opts)
net:training()
if use_cudnn then
cudnn.convert(net,cudnn);
cudnnSetDeterministic(net)
net:cuda();
input = resizeAndConvert(input,'torch.CudaTensor')
end
local out_orig = utils.recursiveClone(net:forward(input))
local grad_orig = utils.recursiveClone(out_orig)
net:zeroGradParameters()
local gradInput_orig = utils.recursiveClone(net:backward(input, grad_orig))
local _, gradParams_orig = net:getParameters()
gradParams_orig = gradParams_orig:clone()
local mems1 = optnet.countUsedMemory(net)
optnet.optimizeMemory(net, input, {mode='training'})
local out = utils.recursiveClone(net:forward(input))
local grad = utils.recursiveClone(out)
net:zeroGradParameters()
local gradInput = utils.recursiveClone(net:backward(input, grad))
local _, gradParams = net:getParameters()
gradParams = gradParams:clone()
local mems2 = countUsedMemory(net)
tester:eq(out_orig, out, 'Outputs differ after optimization of '..model)
tester:eq(gradInput_orig, gradInput, backward_tol, 'GradInputs differ after optimization of '..model)
tester:eq(gradParams_orig, gradParams, backward_tol, 'GradParams differ after optimization of '..model)
local mem1 = mems1.total_size
local mem2 = mems2.total_size
local omem1 = mems1.outputs
local omem2 = mems2.outputs
local imem1 = mems1.gradInputs
local imem2 = mems2.gradInputs
local bmem1 = mems1.buffers
local bmem2 = mems2.buffers
local pmem1 = mems1.params
local pmem2 = mems2.params
tester:assertle(mem2, mem1, 'Optimized model uses more memory! '..
'Before: '.. mem1..' bytes, After: '..mem2..' bytes')
print('Memory use')
print('Total', mem1/1024/1024, mem2/1024/1024, 1-mem2/mem1)
print('Outputs',omem1/1024/1024,omem2/1024/1024, 1-omem2/omem1)
print('gradInputs',imem1/1024/1024,imem2/1024/1024, 1-imem2/imem1)
print('Buffers',bmem1/1024/1024,bmem2/1024/1024, 1-bmem2/bmem1)
print('Params', pmem1/1024/1024,pmem2/1024/1024, 1-pmem2/pmem1)
end
-------------------------------------------------
-- removing optimization
-------------------------------------------------
local function genericTestRemoveOptim(model,opts)
local net, input = models[model](opts)
net:training()
if use_cudnn then
cudnn.convert(net,cudnn);
cudnnSetDeterministic(net)
net:cuda();
input = resizeAndConvert(input,'torch.CudaTensor')
end
local out_orig = utils.recursiveClone(net:forward(input))
local grad_orig = utils.recursiveClone(out_orig)
net:zeroGradParameters()
local gradInput_orig = utils.recursiveClone(net:backward(input, grad_orig))
local _, gradParams_orig = net:getParameters()
gradParams_orig = gradParams_orig:clone()
optnet.optimizeMemory(net, input)
optnet.removeOptimization(net)
local out = utils.recursiveClone(net:forward(input))
local grad = utils.recursiveClone(out)
net:zeroGradParameters()
local gradInput = utils.recursiveClone(net:backward(input, grad))
local _, gradParams = net:getParameters()
gradParams = gradParams:clone()
tester:eq(out_orig, out, 'Outputs differ after optimization of '..model)
tester:eq(gradInput_orig, gradInput, backward_tol, 'GradInputs differ after optimization of '..model)
tester:eq(gradParams_orig, gradParams, backward_tol, 'GradParams differ after optimization of '..model)
end
for k, v in pairs(models) do
if k ~= 'resnet' and k ~= 'preresnet' then
optest[k] = function()
genericTestForward(k)
end
optest[k..'_backward'] = function()
genericTestBackward(k)
end
optest[k..'_remove'] = function()
genericTestRemoveOptim(k)
end
end
end
for _, v in ipairs({20,32,56,110}) do
for _, k in ipairs{'resnet', 'preresnet'} do
local opts = {dataset='cifar10',depth=v}
optest[k..v] = function()
genericTestForward(k, opts)
end
optest[k..v..'_backward'] = function()
genericTestBackward(k, opts)
end
optest[k..v..'_remove'] = function()
genericTestRemoveOptim(k, opts)
end
end
end
tester:add(optest)
function optnet.test(tests, opts)
opts = opts or {}
local tType = torch.getdefaulttensortype()
torch.setdefaulttensortype('torch.FloatTensor')
if opts.only_basic_tests then
local disable = {
'alexnet','vgg','googlenet',
'resnet20','resnet32','resnet56','resnet110',
'preresnet20','preresnet32','preresnet56','preresnet110'
}
local toDisable = {}
for _, v in ipairs(disable) do
table.insert(toDisable,v)
table.insert(toDisable,v..'_backward')
table.insert(toDisable,v..'_remove')
end
tester:disable(toDisable)
end
if opts.use_cudnn then
use_cudnn = true
require 'cudnn'
require 'cunn'
end
tester:run(tests)
torch.setdefaulttensortype(tType)
return tester
end