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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
local network = nerv.class('nerv.Network')
function network:__init(id, global_conf, network_conf)
self.id = id
self.dim_in = network_conf.network.dim_in
self.dim_out = network_conf.network.dim_out
self.gconf = global_conf
if self.gconf.use_cpu then
self.mat_type = self.gconf.mmat_type
else
self.mat_type = self.gconf.cumat_type
end
self.clip = network_conf.clip
self.nn_act_default = network_conf.nn_act_default
if self.nn_act_default == nil then
self.nn_act_default = 0
end
self.layers = {}
self.input_conn = {}
self.output_conn = {}
self.socket = self:compile(network_conf.network)
for i = 1, #self.dim_in do
local edge = self.socket.inputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if self.input_conn[id][port] ~= nil then
nerv.error('duplicate edge')
end
self.input_conn[id][port] = {0, i, time}
end
for i = 1, #self.dim_out do
local edge = self.socket.outputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if self.output_conn[id][port] ~= nil then
nerv.error('duplicate edge')
end
self.output_conn[id][port] = {0, i, time}
end
self.delay = 0
for i = 1, #self.layers do
local dim_in, _ = self.layers[i]:get_dim()
for j = 1, #dim_in do
local time = self.input_conn[i][j][3]
if math.abs(time) > self.delay then
self.delay = math.abs(time)
end
end
end
end
function network:compile(layer)
local socket = {inputs = {}, outputs = {}}
if not nerv.is_type(layer, 'nerv.GraphLayer') then
table.insert(self.layers, layer)
local id = #self.layers
self.input_conn[id] = {}
self.output_conn[id] = {}
local dim_in, dim_out = layer:get_dim()
for i = 1, #dim_in do
socket.inputs[i] = {id, i, 0}
end
for i = 1, #dim_out do
socket.outputs[i] = {id, i, 0}
end
else
local sublayer_socket = {}
for id, sublayer in pairs(layer.layers) do
if id ~= '<input>' then
sublayer_socket[sublayer.id] = self:compile(sublayer.layer)
end
end
for _, edge in pairs(layer.connections) do
-- id = 0 means <input> or <output>
local id_from, port_from = edge[1], edge[2]
local id_to, port_to = edge[3], edge[4]
local time = edge[5]
if id_from == 0 then
if socket.inputs[port_from] ~= nil then
nerv.error('duplicate input socket')
end
local input = sublayer_socket[id_to].inputs[port_to]
local id, port, t = input[1], input[2], input[3] + time
socket.inputs[port_from] = {id, port, t}
else
local output = sublayer_socket[id_from].outputs[port_from]
local id, port, t = output[1], output[2], output[3] + time
if id_to == 0 then
if socket.outputs[port_to] ~= nil then
nerv.error('duplicate output socket')
end
socket.outputs[port_to] = {id, port, t}
else
local input = sublayer_socket[id_to].inputs[port_to]
local id1, port1, t1 = input[1], input[2], input[3]
if self.input_conn[id1][port1] ~= nil or self.output_conn[id][port] ~= nil then
nerv.error('duplicate edge')
end
self.input_conn[id1][port1] = {id, port, t + t1}
self.output_conn[id][port] = {id1, port1, t + t1}
end
end
end
end
return socket
end
function network:init(batch_size, chunk_size)
self.batch_size = batch_size
self.chunk_size = chunk_size
self:topsort()
self:make_initial_store()
collectgarbage('collect')
for i = 1, #self.layers do
self.layers[i]:init(batch_size, chunk_size)
end
end
function network:topsort()
nerv.info('Network topology sort')
local degree = {}
for t = 1, self.chunk_size do
degree[t] = {}
for i = 1, #self.layers do
degree[t][i] = 0
end
end
for t = 1, self.chunk_size do
for i = 1, #self.layers do
local _, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_out do
if self.output_conn[i][j] ~= nil then
local edge = self.output_conn[i][j]
local id, _, time = edge[1], edge[2], edge[3] + t
if time >= 1 and time <= self.chunk_size and id ~= 0 then
degree[time][id] = degree[time][id] + 1
end
end
end
end
end
self.queue = {}
local l = 1
local r = 0
for t = 1, self.chunk_size do
for i = 1, #self.layers do
if degree[t][i] == 0 then
r = r + 1
self.queue[r] = {chunk = t, id = i}
end
end
end
while l<=r do
local t, i = self.queue[l].chunk, self.queue[l].id
l = l + 1
local _, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_out do
if self.output_conn[i][j] ~= nil then
local edge = self.output_conn[i][j]
local id, _, time = edge[1], edge[2], edge[3] + t
if time >= 1 and time <= self.chunk_size and id ~= 0 then
degree[time][id] = degree[time][id] - 1
if degree[time][id] == 0 then
r = r + 1
self.queue[r] = {chunk = time, id = id}
end
end
end
end
end
if r ~= self.chunk_size * #self.layers then
nerv.error('loop detected')
end
end
function network:make_initial_store()
nerv.info('Network initing storage')
-- allocate memory
local memory = {}
local err_memory = {}
for t = 1 - self.delay, self.chunk_size + self.delay do
memory[t] = {}
err_memory[t] = {}
for i = 1, #self.layers do
memory[t][i] = {}
err_memory[t][i] = {}
local dim_in, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_in do
err_memory[t][i][j] = self.mat_type(self.batch_size, dim_in[j])
err_memory[t][i][j]:fill(0)
end
for j = 1, #dim_out do
memory[t][i][j] = self.mat_type(self.batch_size, dim_out[j])
memory[t][i][j]:fill(self.nn_act_default)
end
end
-- memory[t][0] stores network input
memory[t][0] = {}
for j = 1, #self.dim_in do
memory[t][0][j] = self.mat_type(self.batch_size, self.dim_in[j])
memory[t][0][j]:fill(self.nn_act_default)
end
-- err_memory[t][0] stores network err_input
err_memory[t][0] = {}
for j = 1, #self.dim_out do
err_memory[t][0][j] = self.mat_type(self.batch_size, self.dim_out[j])
err_memory[t][0][j]:fill(0)
end
end
-- connect memory and reference
self.input = {}
self.output = {}
self.err_input = {}
self.err_output = {}
for t = 1, self.chunk_size do
self.input[t] = {}
self.output[t] = {}
self.err_input[t] = {}
self.err_output[t] = {}
for i = 1, #self.layers do
self.input[t][i] = {}
self.output[t][i] = {}
self.err_input[t][i] = {}
self.err_output[t][i] = {}
local dim_in, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_in do
local edge = self.input_conn[i][j]
local id, port, time = edge[1], edge[2], edge[3]
if id ~= 0 or t - time < 1 or t - time > self.chunk_size then
self.input[t][i][j] = memory[t - time][id][port]
end
if id ~= 0 then
self.err_output[t][i][j] = err_memory[t][i][j]
end
end
for j = 1, #dim_out do
local edge = self.output_conn[i][j]
local id, port, time = edge[1], edge[2], edge[3]
if id ~= 0 then
self.output[t][i][j] = memory[t][i][j]
end
if id ~= 0 or t + time < 1 or t + time > self.chunk_size then
self.err_input[t][i][j] = err_memory[t + time][id][port]
end
end
end
end
-- check dangling reference
for t = 1, self.chunk_size do
for i = 1, #self.dim_in do
local edge = self.socket.inputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t + time >= 1 and t + time <= self.chunk_size then
if self.input[t + time][id][port] ~= nil then
nerv.error('input reference not nil')
end
self.input[t + time][id][port] = true -- just a place holder
if self.err_output[t + time][id][port] ~= nil then
nerv.error('err_output reference not nil')
end
self.err_output[t + time][id][port] = true -- just a place holder
end
end
for i = 1, #self.dim_out do
local edge = self.socket.outputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t - time >= 1 and t - time <= self.chunk_size then
if self.output[t - time][id][port] ~= nil then
nerv.error('output reference not nil')
end
self.output[t - time][id][port] = true -- just a place holder
if self.err_input[t - time][id][port] ~= nil then
nerv.error('err_output reference not nil')
end
self.err_input[t - time][id][port] = true -- just a place holder
end
end
end
for t = 1, self.chunk_size do
for i = 1, #self.layers do
local dim_in, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_in do
if self.input[t][i][j] == nil then
nerv.error('input reference dangling')
end
if self.err_output[t][i][j] == nil then
nerv.error('err_output reference dangling')
end
end
for j = 1, #dim_out do
if self.output[t][i][j] == nil then
nerv.error('output reference dangling')
end
if self.err_input[t][i][j] == nil then
nerv.error('err_input reference dangling')
end
end
end
end
-- allocate reference for legacy of previous mini-batch
self.legacy = {}
for t = 1 - self.delay, 0 do
self.legacy[t] = {}
for i = 1, #self.layers do
self.legacy[t][i] = {}
local _, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_out do
self.legacy[t][i][j] = memory[t][i][j]
end
end
end
end
function network:set_input(input)
for t = 1, #self.chunk_size do
for i = 1, #self.dim_in do
local edge = self.socket.inputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t + time >= 1 and t + time <= self.chunk_size then
self.input[t + time][id][port] = input[t][i]
end
end
end
end
function network:set_output(output)
for t = 1, #self.chunk_size do
for i = 1, #self.dim_out do
local edge = self.socket.outputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t - time >= 1 and t - time <= self.chunk_size then
self.output[t - time][id][port] = output[t][i]
end
end
end
end
function network:set_err_input(err_input)
for t = 1, #self.chunk_size do
for i = 1, #self.dim_out do
local edge = self.socket.outputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t - time >= 1 and t - time <= self.chunk_size then
self.err_input[t - time][id][port] = err_input[t][i]
end
end
end
end
function network:set_err_output(err_output)
for t = 1, self.chunk_size do
for i = 1, #self.dim_in do
local edge = self.socket.inputs[i]
local id, port, time = edge[1], edge[2], edge[3]
if t + time >= 1 and t + time <= self.chunk_size then
self.err_output[t + time][id][port] = err_output[t][i]
end
end
end
end
function network:mini_batch_init(information)
self.info = information
self.max_length = 0
self.border = {}
for i = 1, self.chunk_size do
self.border[i] = {}
end
for i = 1, self.batch_size do
if self.info.seq_length[i] > self.max_length then
self.max_length = self.info.seq_length[i]
end
for t = 1, self.delay do
local chunk = self.info.seq_length[i] + t
if chunk > self.chunk_size then
break
end
table.insert(self.border[chunk], i)
end
end
for t = 1 - self.delay, 0 do
for i = 1, #self.layers do
local _, dim_out = self.layers[i]:get_dim()
for j = 1, #dim_out do
self.legacy[t][i][j]:copy_from(self.output[t + self.chunk_size][i][j])
for k = 1, #self.info.new_seq do
local batch = self.info.new_seq[k]
self.legacy[t][i][j][batch - 1]:fill(self.nn_act_default)
end
end
end
end
for t = self.max_length + 1, self.max_length + self.delay do
if t > self.chunk_size then
break
end
for i = 1, #self.layers do
local dim_in, _ = self.layers[i]:get_dim()
for j = 1, #dim_in do
self.err_output[t][i][j]:fill(0)
end
end
end
end
function network:propagate(input, output)
network:set_input(input)
network:set_output(output)
for i = 1, #self.queue do
local t, id = self.queue[i].chunk, self.queue[i].id
if t <= self.max_length then
self.layers[id]:propagate(self.input[t][id], self.output[t][id], t)
end
for j = 1, #self.border[t] do
local batch = self.border[t][j]
local _, dim_out = self.layers[id]:get_dim()
for k = 1, #dim_out do
self.output[t][id][k][batch - 1]:fill(self.nn_act_default)
end
end
end
end
function network:back_propagate(bp_err, next_bp_err, input, output)
network:set_input(input)
network:set_output(output)
network:set_err_input(bp_err)
network:set_err_output(next_bp_err)
for i = #self.queue, 1, -1 do
local t, id = self.queue[i].chunk, self.queue[i].id
if t <= self.max_length then
for j = 1, #self.border[t] do
local batch = self.border[t][j]
local dim_in, _ = self.layers[id]:get_dim()
for k = 1, #dim_in do
self.err_input[t][id][k][batch - 1]:fill(0)
end
end
self.layers[id]:back_propagate(self.err_input[t][id], self.err_output[t][id], self.input[t][id], self.output[t][id], t)
end
end
end
function network:update(bp_err, input, output)
network:set_input(input)
network:set_output(output)
network:set_err_input(bp_err)
for i = 1, #self.queue do
local t, id = self.queue[i].chunk, self.queue[i].id
if t <= self.max_length then
self.layers[id]:update(self.err_input[t][id], self.input[t][id], self.output[t][id], t)
end
end
end
|