aboutsummaryrefslogtreecommitdiff
path: root/nerv/nn/layer_dag.lua
blob: 689687803fe5c7b635f16f26663c81af503779ae (plain) (blame)
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
local DAGLayer = nerv.class("nerv.DAGLayer", "nerv.Layer")

local function parse_id(str)
    local id, port, _
    _, _, id, port = string.find(str, "([a-zA-Z0-9_.]+)%[([0-9]+)%]")
    if id == nil or port == nil then
        _, _, id, port = string.find(str, "(.+)%[([0-9]+)%]")
        if not (id == "<input>" or id == "<output>") then
            nerv.error("wrong format of connection id")
        end
    end
    port = tonumber(port)
    return id, port
end

local function discover(id, layers, layer_repo)
    local ref = layers[id]
    if id == "<input>" or id == "<output>" then
        return nil
    end
    if ref == nil then
        local layer = layer_repo:get_layer(id)
        local dim_in, dim_out = layer:get_dim()
        ref = {
            layer = layer,
            inputs = {},
            outputs = {},
            err_inputs = {},
            err_outputs = {},
            next_layers = {},
            input_len = #dim_in,
            output_len = #dim_out,
            in_deg = 0,
            visited = false
        }
        layers[id] = ref
    end
    return ref
end

local function touch_list_by_idx(list, idx)
    if list[idx] == nil then
        list[idx] = {}
    end
end

function DAGLayer:__init(id, global_conf, layer_conf)
    local layers = {}
    local inputs = {}
    local outputs = {}
    local dim_in = layer_conf.dim_in
    local dim_out = layer_conf.dim_out
    local parsed_conn = {}
    for from, to in pairs(layer_conf.connections) do
        local id_from, port_from = parse_id(from)
        local id_to, port_to = parse_id(to)
        local ref_from = discover(id_from, layers, layer_conf.sub_layers)
        local ref_to = discover(id_to, layers, layer_conf.sub_layers)
        local input_dim, output_dim, _
        if ref_from then
            touch_list_by_idx(ref_from.outputs, 1)
            if ref_from.outputs[1][port_from] ~= nil then
                nerv.error("%s has already been attached", from)
            end
        end
        if ref_to then
            touch_list_by_idx(ref_to.inputs, 1)
            if ref_to.inputs[1][port_to] ~= nil then
                nerv.error("%s has already been attached", to)
            end
        end
        if id_from == "<input>" then
            input_dim, _ = ref_to.layer:get_dim()
            if dim_in[port_from] ~= input_dim[port_to] then
                nerv.error("mismatching data dimension between %s and %s", from, to)
            end
            inputs[port_from] = {ref_to, port_to}
            ref_to.inputs[1][port_to] = inputs -- just a place holder
        elseif id_to == "<output>" then
            _, output_dim = ref_from.layer:get_dim()
            if output_dim[port_from] ~= dim_out[port_to] then
                nerv.error("mismatching data dimension between %s and %s", from, to)
            end
            outputs[port_to] = {ref_from, port_from}
            ref_from.outputs[1][port_from] = outputs -- just a place holder
        else
            _, output_dim = ref_from.layer:get_dim()
            input_dim, _ = ref_to.layer:get_dim()
            if output_dim[port_from] ~= input_dim[port_to] then
                nerv.error("mismatching data dimension between %s and %s", from, to)
            end

            table.insert(parsed_conn,
            {{ref_from, port_from}, {ref_to, port_to}})
            table.insert(ref_from.next_layers, ref_to) -- add edge
            ref_to.in_deg = ref_to.in_deg + 1          -- increase the in-degree of the target layer
        end
    end

    -- topology sort
    local queue = {}
    local l = 1
    local r = 1
    for id, ref in pairs(layers) do
        if ref.in_deg == 0 then
            table.insert(queue, ref)
            nerv.info("adding source layer: %s", id)
            r = r + 1
        end
    end
    if l == r then
        nerv.error("loop detected")
    end
    while l < r do
        local cur = queue[l]
        cur.visited = true
        l = l + 1
        for _, nl in pairs(cur.next_layers) do
            nl.in_deg = nl.in_deg - 1
            if nl.in_deg == 0 then
                table.insert(queue, nl)
                r = r + 1
            end
        end
    end
    for i = 1, #queue do
        nerv.info("enqueued layer: %s %s", queue[i].layer, queue[i].layer.id)
    end

    for id, ref in pairs(layers) do
        -- check wether the graph is connected
        if ref.visited == false then
            nerv.warning("layer %s is ignored", id)
        end
    end

    self.layers = layers
    self.inputs = inputs
    self.outputs = outputs
    self.id = id
    self.dim_in = dim_in
    self.dim_out = dim_out
    self.parsed_conn = parsed_conn
    self.queue = queue
    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
end

function DAGLayer:init(batch_size, chunk_size)
    if chunk_size == nil then
        chunk_size = 1
    end
    for i, conn in ipairs(self.parsed_conn) do
        local _, output_dim
        local ref_from, port_from, ref_to, port_to
        ref_from, port_from = unpack(conn[1])
        ref_to, port_to = unpack(conn[2])
        _, output_dim = ref_from.layer:get_dim()
        local dim = 1
        if output_dim[port_from] > 0 then
            dim = output_dim[port_from]
        end

        for t = 1, chunk_size do
            local mid = self.mat_type(batch_size, dim)
            local err_mid = mid:create()
            touch_list_by_idx(ref_to.inputs, t)
            touch_list_by_idx(ref_from.outputs, t)
            touch_list_by_idx(ref_from.err_inputs, t)
            touch_list_by_idx(ref_to.err_outputs, t)

            ref_from.outputs[t][port_from] = mid
            ref_to.inputs[t][port_to] = mid

            ref_from.err_inputs[t][port_from] = err_mid
            ref_to.err_outputs[t][port_to] = err_mid
        end
    end
    for id, ref in pairs(self.layers) do
        for i = 1, ref.input_len do
            if ref.inputs[1][i] == nil then
                nerv.error("dangling input port %d of layer %s", i, id)
            end
        end
        for i = 1, ref.output_len do
            if ref.outputs[1][i] == nil then
                nerv.error("dangling output port %d of layer %s", i, id)
            end
        end
        -- initialize sub layers
        ref.layer:init(batch_size, chunk_size)
    end
    for i = 1, #self.dim_in do
        if self.inputs[i] == nil then
            nerv.error("dangling port %d of layer <input>", i)
        end
    end
    for i = 1, #self.dim_out do
        if self.outputs[i] == nil then
            nerv.error("dangling port %d of layer <output>", i)
        end
    end
end

function DAGLayer:batch_resize(batch_size, chunk_size)
    if chunk_size == nil then
        chunk_size = 1
    end

    for i, conn in ipairs(self.parsed_conn) do
        local _, output_dim
        local ref_from, port_from, ref_to, port_to
        ref_from, port_from = unpack(conn[1])
        ref_to, port_to = unpack(conn[2])
        _, output_dim = ref_from.layer:get_dim()

        if ref_from.outputs[1][port_from]:nrow() ~= batch_size
            and output_dim[port_from] > 0 then
            for t = 1, chunk_size do
                local mid = self.mat_type(batch_size, output_dim[port_from])
                local err_mid = mid:create()

                ref_from.outputs[t][port_from] = mid
                ref_to.inputs[t][port_to] = mid

                ref_from.err_inputs[t][port_from] = err_mid
                ref_to.err_outputs[t][port_to] = err_mid
            end
        end
    end
    for id, ref in pairs(self.layers) do
        ref.layer:batch_resize(batch_size, chunk_size)
    end
    collectgarbage("collect")
end

function DAGLayer:set_inputs(input, t)
    for i = 1, #self.dim_in do
        if input[i] == nil then
            nerv.error("some input is not provided");
        end
        local layer = self.inputs[i][1]
        local port = self.inputs[i][2]
        touch_list_by_idx(layer.inputs, t)
        layer.inputs[t][port] = input[i]
    end
end

function DAGLayer:set_outputs(output, t)
    for i = 1, #self.dim_out do
        if output[i] == nil then
            nerv.error("some output is not provided");
        end
        local layer = self.outputs[i][1]
        local port = self.outputs[i][2]
        touch_list_by_idx(layer.outputs, t)
        layer.outputs[t][port] = output[i]
    end
end

function DAGLayer:set_err_inputs(bp_err, t)
    for i = 1, #self.dim_out do
        local layer = self.outputs[i][1]
        local port = self.outputs[i][2]
        touch_list_by_idx(layer.err_inputs, t)
        layer.err_inputs[t][port] = bp_err[i]
    end
end

function DAGLayer:set_err_outputs(next_bp_err, t)
    for i = 1, #self.dim_in do
        local layer = self.inputs[i][1]
        local port = self.inputs[i][2]
        touch_list_by_idx(layer.err_outputs, t)
        layer.err_outputs[t][port] = next_bp_err[i]
    end
end

function DAGLayer:update(bp_err, <