5 local DAG, parent = torch.class('nn.DAG', 'nn.Container')
9 -- Nodes are indexed by the module they encompass
13 function DAG:createNode(n)
14 if not self.node[n] then
15 self:add(n) -- Add it to the object as a Container
17 self.node[n].succ = {}
18 self.node[n].pred = {}
22 function DAG:addEdge(a, b)
26 table.insert(self.node[b].pred, a)
27 table.insert(self.node[a].succ, b)
30 -- Apply f on t recursively; use the corresponding a1 and a2 elements
31 -- (i.e. same keys) as second and third parameters to f when
32 -- available; return the results from f, organized in a similarly
34 function DAG:nestApply(f, t, a1, a2)
35 if torch.type(t) == 'table' then
37 for k, s in pairs(t) do
38 result[k] = self:nestApply(f, s, a1 and a1[k], a2 and a2[k])
46 function DAG:setInput(i)
51 if #self.node[m].succ == 0 then
52 error('Input modules must have outgoing edges.')
54 if #self.node[m].pred > 0 then
55 error('Input modules cannog have incoming edges.')
62 function DAG:setOutput(o)
64 self.outputModules = o
67 if #self.node[m].pred == 0 then
68 error('Output module must have incoming edges.')
70 if #self.node[m].succ > 0 then
71 error('Output module cannot have outgoing edges.')
78 function DAG:putInOrder()
83 -- First, we sort the nodes according to the DAG order
87 self:nestApply(function(m) distance[m] = 1 end, self.inputModules)
93 for i, node in pairs(self.node) do
94 for _, j in pairs(node.succ) do
95 if distance[i] and (not distance[j] or distance[j] < distance[i] + 1) then
96 distance[j] = distance[i] + 1
104 for n, d in pairs(distance) do
105 table.insert(self.sorted, { distance = d, node = n })
108 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
110 for i, a in ipairs(self.sorted) do self.sorted[i] = a.node end
116 for i, d in ipairs(self.sorted) do
117 print('#' .. i .. ' -> ' .. torch.type(d))
121 function DAG:updateOutput(input)
124 self:nestApply(function(m, i) m:updateOutput(i) end, self.inputModules, input)
126 for _, m in ipairs(self.sorted) do
127 if #self.node[m].pred > 0 then
129 if #self.node[m].pred == 1 then
130 i = self.node[m].pred[1].output
131 elseif #self.node[m].pred > 1 then
133 for k = 1, #self.node[m].pred do
134 i[k] = self.node[m].pred[k].output
137 self.node[m].input = i
142 self.output = self:nestApply(function(m) return m.output end, self.outputModules)
147 function DAG:updateGradInput(input, gradOutput)
151 function(m, go) m:updateGradInput(self.node[m].input, go) end,
152 self.outputModules, gradOutput
155 for _, node in pairs(self.node) do
156 node.gradInputSucc = {}
159 for k = #self.sorted, 1, -1 do
160 local m = self.sorted[k]
161 local node = self.node[m]
162 local pred, succ, gradInputSucc = node.pred, node.succ, node.gradInputSucc
164 -- We update m:gradInput
165 if #gradInputSucc == 1 then
166 m:updateGradInput(node.input, gradInputSucc[1])
167 elseif #gradInputSucc > 1 then
171 sum:add(succ[k].gradInput)
173 sum = succ[k].gradInput
176 m:updateGradInput(node.input, sum)
179 -- We fill the gradInputSucc of our predecessors
181 table.insert(self.node[pred[1]].gradInputSucc, node.gradInput)
182 elseif #pred > 1 then
184 table.insert(self.node[node.pred[n]].gradInputSucc, m.gradInput[n])
189 self.gradInput = self:nestApply(function(m) return m.gradInput end, self.inputModules)
191 return self.gradInput