-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.lua
226 lines (190 loc) · 7.74 KB
/
test.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
local nnpack = require 'nnpack'
local nnpacktest = torch.TestSuite()
local precision_forward = 1e-4
local precision_backward = 1e-2
local precision_jac = 1e-3
local precision_io = 1e-5
local nloop = 1
local times = {}
local mytester
local jac = nn.Jacobian
function nnpacktest.SpatialConvolution_forward_single()
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local input = torch.randn(1,from,inj,ini):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float()
local gconv = nnpack.SpatialConvolution(from,to,ki,kj,si,sj):float()
gconv.weight:copy(sconv.weight)
gconv.bias:copy(sconv.bias)
local function test(sconv, gconv)
local groundtruth = sconv:forward(input)
local resfloat = gconv:forward(input)
mytester:asserteq(resfloat:dim(), 4, 'error in dimension')
local error = resfloat:float() - groundtruth:float()
mytester:assertlt(error:abs():max(), precision_forward,
'error on state (forward) ')
-- IO
local ferr,berr = jac.testIO(gconv, input)
mytester:assertlt(ferr, precision_io, torch.typename(gconv) .. ' - i/o forward err ')
mytester:assertlt(berr, precision_io, torch.typename(gconv) .. ' - i/o backward err ')
end
test(sconv, gconv)
local gconv = nnpack.convert(sconv, nnpack)
mytester:asserteq(torch.typename(gconv), 'nnpack.SpatialConvolution', 'conversion type check')
test(sconv, gconv)
end
function nnpacktest.SpatialConvolution_forward_batch()
local bs = math.random(1,32)
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
-- local si = math.random(1,ki)
-- local sj = math.random(1,kj)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local input = torch.randn(bs,from,inj,ini):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float()
local gconv = nnpack.SpatialConvolution(from,to,ki,kj,si,sj):float()
gconv.weight:copy(sconv.weight)
gconv.bias:copy(sconv.bias)
local function test(sconv, gconv)
local groundtruth = sconv:forward(input)
local rescuda = gconv:forward(input)
local error = rescuda:float() - groundtruth:float()
mytester:assertlt(error:abs():max(), precision_forward, 'error on state (forward) ')
-- IO
local ferr,berr = jac.testIO(gconv, input)
mytester:assertlt(ferr, precision_io, torch.typename(gconv) .. ' - i/o forward err ')
mytester:assertlt(berr, precision_io, torch.typename(gconv) .. ' - i/o backward err ')
end
test(sconv, gconv)
local gconv = nnpack.convert(sconv, nnpack)
mytester:asserteq(torch.typename(gconv), 'nnpack.SpatialConvolution', 'conversion type check')
test(sconv, gconv)
end
function nnpacktest.SpatialConvolution_backward_batch()
local bs = math.random(1,32)
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
-- local si = math.random(1,ki)
-- local sj = math.random(1,kj)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local scale = 1
local input = torch.randn(bs,from,inj,ini):float()
local gradOutput = torch.randn(bs,to,outj,outi):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float()
sconv:forward(input)
sconv:zeroGradParameters()
local groundgrad = sconv:backward(input, gradOutput, scale)
local groundweight = sconv.gradWeight
local groundbias = sconv.gradBias
local gconv = nnpack.SpatialConvolution(from,to,ki,kj,si,sj):float()
gconv.weight:copy(sconv.weight)
gconv.bias:copy(sconv.bias)
gconv:forward(input)
local function test(sconv, gconv)
gconv:forward(input)
gconv:zeroGradParameters()
local rescuda = gconv:backward(input, gradOutput, scale)
local weightcuda = gconv.gradWeight
local biascuda = gconv.gradBias
local error = rescuda - groundgrad
local werror = weightcuda - groundweight
local berror = biascuda - groundbias
mytester:assertlt(error:abs():max(), precision_backward, 'error on state (backward) ')
mytester:assertlt(werror:abs():max(), precision_backward, 'error on weight (backward) ')
mytester:assertlt(berror:abs():max(), precision_backward, 'error on bias (backward) ')
end
test(sconv, gconv)
local gconv = nnpack.convert(sconv, cudnn)
mytester:asserteq(torch.typename(gconv), 'nnpack.SpatialConvolution', 'conversion type check')
test(sconv, gconv)
end
function nnpacktest.functional_conv2d_forward_single()
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local input = torch.randn(1,from,inj,ini):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float():noBias()
local groundtruth = sconv:forward(input)
local resfloat = nnpack.conv2d(input, sconv.weight, si, sj)
mytester:asserteq(resfloat:dim(), 4, 'error in dimension')
local error = resfloat:float() - groundtruth:float()
mytester:assertlt(error:abs():max(), precision_forward, 'error on state (forward)')
end
function nnpacktest.functional_conv2d_forward_batch()
local bs = math.random(1,32)
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
-- local si = math.random(1,ki)
-- local sj = math.random(1,kj)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local input = torch.randn(bs,from,inj,ini):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float():noBias()
local groundtruth = sconv:forward(input)
local rescuda = nnpack.conv2d(input, sconv.weight, si, sj)
local error = rescuda:float() - groundtruth:float()
mytester:assertlt(error:abs():max(), precision_forward, 'error on state (forward) ')
end
function nnpacktest.functional_conv2d_backward()
local bs = math.random(1,32)
local from = math.random(1,32)
local to = math.random(1,64)
local ki = math.random(1,15)
local kj = math.random(1,15)
-- local si = math.random(1,ki)
-- local sj = math.random(1,kj)
local si,sj = 1,1
local outi = math.random(1,64)
local outj = math.random(1,64)
local ini = (outi-1)*si+ki
local inj = (outj-1)*sj+kj
local scale = 1
local input = torch.randn(bs,from,inj,ini):float()
local gradOutput = torch.randn(bs,to,outj,outi):float()
local sconv = nn.SpatialConvolution(from,to,ki,kj,si,sj):float():noBias()
sconv:forward(input)
sconv:zeroGradParameters()
local groundgrad = sconv:backward(input, gradOutput, scale)
local groundweight = sconv.gradWeight
local rescuda = nnpack.conv2d_updateGradInput(input, sconv.weight, gradOutput, si, sj)
local weightcuda = nnpack.conv2d_accGradParameters(input, sconv.weight, gradOutput, si, sj)
local error = rescuda - groundgrad
local werror = weightcuda - groundweight
mytester:assertlt(error:abs():max(), precision_backward, 'error on state (backward) ')
mytester:assertlt(werror:abs():max(), precision_backward, 'error on weight (backward) ')
end
torch.setdefaulttensortype('torch.FloatTensor')
math.randomseed(os.time())
mytester = torch.Tester()
mytester:add(nnpacktest)
mytester:run()