This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
/
nnwrapper.lua
322 lines (292 loc) · 7.86 KB
/
nnwrapper.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
local util = require 'autograd.util'
local nodeApply
local function directApply(fun, gradFun, ...)
return fun.fn(...)
end
local function setApplyFn(fn)
nodeApply = fn or directApply
end
setApplyFn()
local function hasParams(nnObject)
local hasParamFn, params = pcall(nnObject.parameters, nnObject)
params = params or {}
if not hasParamFn or #params == 0 then
return false
else
return true
end
end
local function isCriterion(nnObject)
local isCriterion = false
local mt = getmetatable(nnObject)
if mt then
local mmt = getmetatable(mt)
if mmt then
if mmt.__typename == 'nn.Criterion' then
isCriterion = true
end
end
end
return isCriterion
end
local function isModule(nnObject)
local isModule = false
local mt = getmetatable(nnObject)
if mt then
local mmt = getmetatable(mt)
if mmt then
local t
local mmmt = getmetatable(mmt)
if mmmt then
t = mmmt.__typename
else
t = mmt.__typename
end
if t == "nn.Module" or t == "nn.Sequential" or t == "nn.Container" or t == "nn.Threshold" then
isModule = true
end
end
end
return isModule
end
local function getInputType(x)
local dataType = nil
if torch.isTensor(x) then
dataType = torch.type(x)
elseif type(x) == "table" then
if x[1] then
dataType = torch.type(x[1])
end
end
return dataType
end
local function updateType(nnObject, lastType, newType)
if not newType then
error("Input is neither a tensor or a table of tensors. Type is " .. type(newType))
end
if lastType ~= newType then
lastType = newType
nnObject:type(newType)
end
return nnObject, lastType
end
local function wrapCriterion(nnObject)
local lastType = ""
local mod = {}
local function forward(x, y)
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
return nnObject:forward(x, y)
end
local function backward(g, x, y)
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
return nnObject:backward(x, y)
end
local fn = function(x, y)
local backFnDesc = {
object = mod,
raw = nnObject,
method = "backward",
name = "criterion",
fn = backward,
capture = true,
}
local gradFn = {
function(g,ans,x,y)
return nodeApply(backFnDesc, nil, g, x, y)
end,
function(g,ans,x,y)
-- NOTE: should we throw error as uniplemented here?
return util.fillSameSizeAs(y, 0)
end,
}
local fnDesc = {
object = mod,
raw = nnObject,
method = "forward",
name = "criterion",
fn = forward,
capture = true,
}
return nodeApply(fnDesc, gradFn, x, y)
end
mod.entry = fn
mod.forward = forward
mod.backward = backward
mod.module = nnObject
-- Shortcut:
setmetatable(mod, {
__call = function(self, ...)
return self.entry(...)
end
})
return mod
end
local function wrapModuleWithoutParams(nnObject)
local lastType = ""
local mod = {}
local function forward(x)
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
return nnObject:forward(x)
end
local function backward(g,x)
-- NOTE: Is this necessary if it's done forward?
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
nnObject:zeroGradParameters()
local gradInput = nnObject:backward(x, g)
return gradInput
end
local fn = function(x)
local grads = nil
local backFnDesc = {
object = mod,
raw = nnObject,
method = "backward",
name = "model",
fn = backward,
capture = true,
}
local gradFn = {
function(g,ans,x)
return nodeApply(backFnDesc, nil, g, x)
end
}
local fnDesc = {
object = mod,
raw = nnObject,
method = "forward",
name = "model",
fn = forward,
capture = true,
}
return nodeApply(fnDesc, gradFn, x)
end
mod.entry = fn
mod.forward = forward
mod.backward = backward
mod.module = nnObject
-- Shortcut:
setmetatable(mod, {
__call = function(self, ...)
return self.entry(...)
end
})
return mod
end
local function wrapModuleWithParams(nnObject)
local lastType = ""
local mod = {}
local params = nnObject:parameters()
local function forward(params,x)
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
local modelParams, modelGradParams = nnObject:parameters()
for i,p in ipairs(modelParams) do
if p ~= params[i] then
-- NOTE: need a better error message
-- if there's a type mismatch
p:view(params[i], params[i]:size())
end
end
return nnObject:forward(x)
end
local function backward(g,params,x)
-- NOTE: Is this necessary if it's done forward?
nnObject, lastType = updateType(nnObject, lastType, getInputType(x))
local modelParams, modelGradParams = nnObject:parameters()
for i,p in ipairs(modelParams) do
if p ~= params[i] then
p:view(params[i], params[i]:size())
end
end
nnObject:zeroGradParameters()
local gradInput = nnObject:backward(x, g)
return {modelGradParams, gradInput}
end
local fn = function(params, x)
local grads = nil
local backFnDesc = {
object = mod,
raw = nnObject,
method = "backward",
name = "model",
fn = backward,
capture = true,
}
local gradFn = {
function(g,ans,params,x)
if grads == nil then
grads = nodeApply(backFnDesc, nil, g, params, x)
end
return grads[1]
end,
function(g,ans,params,x)
if grads == nil then
grads = nodeApply(backFnDesc, nil, g, params, x)
end
return grads[2]
end,
}
local fnDesc = {
object = mod,
raw = nnObject,
method = "forward",
name = "model",
fn = forward,
capture = true,
}
return nodeApply(fnDesc, gradFn, params, x)
end
mod.entry = fn
mod.forward = forward
mod.backward = backward
mod.module = nnObject
-- Shortcut:
setmetatable(mod, {
__call = function(self, ...)
return self.entry(...)
end
})
return mod, params
end
-- Take in an nn module and functionalize it
local functionalize, functionalizePackage
functionalize = function(nnObject)
if type(nnObject) == "string" then
return functionalizePackage(nnObject)
end
if isModule(nnObject) then
if hasParams(nnObject) then
return wrapModuleWithParams(nnObject)
else
return wrapModuleWithoutParams(nnObject)
end
elseif isCriterion(nnObject) then
return wrapCriterion(nnObject)
else
error("Input is not a package name or nn object")
end
end
functionalizePackage = function(packageName)
assert(type(packageName) == 'string')
local loaded, mod = pcall(require, packageName)
if not loaded then
error("Could not load package '" .. packageName .. "'")
else
-- Iterate through every module in the package,
-- and functionalize it
local map = {}
for modName, nnClass in pairs(mod) do
if isModule(nnClass) or isCriterion(nnClass) then
map[modName] = function(...)
local out = {functionalize(nnClass(...))}
return table.unpack(out)
end
end
end
return map
end
end
return {
functionalize = functionalize,
functionalizePackage = functionalizePackage,
setApplyFn = setApplyFn
}