-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompGraphs.jl
360 lines (284 loc) · 11.7 KB
/
CompGraphs.jl
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
#=
module CompGraphs
=================
The CompGraph type in this module is intended to hold the computational graph of a
single composite function with vector-valued inputs and outputs. This graph
expresses the composite function as a recipe of elemental operations. Computational
graphs are required for implementing certain numerical methods, such as:
- the standard reverse/adjoint mode of automatic differentiation (AD)
- the "branch-locking" method for efficient reverse-AD-like generalized
differentiation by Khan (2018)
https://doi.org/10.1080/10556788.2017.1341506
- the "cone-squashing" method for generalized differentiation
by Khan and Barton (2012, 2013)
https://doi.org/10.1145/2491491.2491493
- the "reverse McCormick" convex relaxations incorporating constraint propagation,
by Wechsung et al. (2015)
https://doi.org/10.1007/s10898-015-0303-6
"load_function!" in this module uses operator overloading to construct the
computational graph of a finite composition of supported operations. Each
node and the graph overall can also hold additional user-specified data,
intended for use in methods like the reverse AD mode.
Written by Kamil Khan on February 10, 2022
=#
##############################
# Last modified by Yulan Zhang on May 12, 2023
# Inlcude parameters into this graph computation
##############################
module CompGraphs
using Printf
export CompGraph, GraphNode, CompGraphList
export load_function!, is_function_loaded
# structs; T and P are for smuggling in any sort of application-dependent data,
# but could easily be Any or Nothing for simplicity.
struct GraphNode{P}
operation::Symbol
parentIndices::Vector{Int} # identify operands of "operation"
constValue::Union{Float64, Int} # only used when "operation" = :const or :^
data::P # hold extra node-specific data
end
struct CompGraph{T, P}
nodeList::Vector{GraphNode{P}}
domainDim::Int # domain dimension of graphed function
parmDim::Int # parameter dimension of graphed function
rangeDim::Int # range dimension of graphed function
index::Int # index of function
data::T # hold extra data not specific to any one node
end
struct CompGraphList{T, P}
GraphList::Vector{CompGraph{T,P}}
end
struct GraphBuilder{T,P}
index::Int
graph::CompGraph{T,P}
end
struct GraphBuilder_p{T,P}
index::Int
graph::CompGraph{T,P}
end
# constructors
GraphNode{P}(op::Symbol, i::Vector{Int}, p::P) where P = GraphNode{P}(op, i, 0.0, p)
# the following constructor requires a constructor T() with no arguments
function CompGraph{T, P}(n::Int, m::Int, k::Int, t::Int) where {T, P}
return CompGraph{T, P}(GraphNode{P}[], n, m, k, t, T())
end
function CompGraphList{T, P}() where {T, P}
return CompGraphList{T, P}(CompGraph{T,P}[])
end
# A GraphNode.operation can be any Symbol from the following lists
unaryOpList = [:-, :inv, :exp, :log, :sin, :cos, :abs]
binaryOpList = [:+, :-, :*, :/, :^, :max, :min, :hypot]
customOpList = [:input, :output, :parm, :const] # input, output, parameter and Float64 constant
# print graph or individual nodes
opStringDict = Dict(
:- => "neg",
:inv => "inv",
:exp => "exp",
:log => "log",
:sin => "sin",
:cos => "cos",
:abs => "abs",
:+ => " + ",
:- => " - ",
:* => " * ",
:/ => " / ",
:^ => " ^ ",
:max => "max",
:min => "min",
:hypot => "hyp",
:input => "inp",
:output => "out",
:parm => " p ",
:const => "con",
)
function Base.show(io::IO, node::GraphNode)
parents = node.parentIndices
nParents = length(parents)
if (node.operation == :^) && (length(parents) == 1)
opString = @sprintf " ^%1d" node.constValue
else
opString = opStringDict[node.operation]
end
if nParents <= 2
oneParent(i::Int) = (nParents < i) ? " " : @sprintf "%-3d" parents[i]
parentString = oneParent(1) * " " * oneParent(2)
else
parentString = string(parents)
end
return print(io, opString, " | ", parentString)
end
function Base.show(io::IO, graphList::CompGraphList)
return begin
for (j, graph) in enumerate(graphList.GraphList)
println(io, " Computational graph of function No." * string(j) * "\n")
println(" index | op | parents")
println(" ---------------------")
for (i, node) in enumerate(graph.nodeList)
@printf " %3d | " i
println(node)
end
println("\n")
end
end
end
# load in a function using operator overloading, and store its computational graph
function load_function!(
f::Function,
graph::CompGraph{T, P},
initP::P
) where {T, P}
empty!(graph.nodeList)
# push new nodes for function inputs
xGB = [GraphBuilder{T,P}(i,graph) for i=1:(graph.domainDim)]
for xComp in xGB
inputData = deepcopy(initP)
inputNode = GraphNode{P}(:input, Int[], 0.0, inputData)
push!(graph.nodeList, inputNode)
end
# push new nodes for function parameters
pGB = [GraphBuilder_p{T,P}(i,graph) for i=1:(graph.parmDim)]
for pComp in pGB
parmData = deepcopy(initP)
parmNode = GraphNode{P}(:parm, Int[], 0.0, parmData)
push!(graph.nodeList, parmNode)
end
# push new nodes for all intermediate operations, using operator overloading
yGB_i = graph.index
yGB = f(xGB, pGB, yGB_i)
if !(yGB isa Vector)
yGB = [yGB]
end
# push new nodes for function outputs
for yComp in yGB
outputData = deepcopy(initP)
outputNode = GraphNode{P}(:output, [yComp.index], 0.0, outputData)
push!(graph.nodeList, outputNode)
end
end
is_function_loaded(graphList::CompGraphList) = !isempty(graphList.GraphList)
## let GraphBuilder construct a nodeList by operator overloading
# overload unary operations in unaryOpList
macro define_GraphBuilder_unary_op_rule(op)
opEval = eval(op)
return quote
function Base.$opEval(u::GraphBuilder{T,P}) where {T,P}
parentGraph = u.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[u.index].data)
newNode = GraphNode{P}($op, [u.index], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.$opEval(u::GraphBuilder_p{T,P}) where {T,P}
parentGraph = u.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[u.index + parentGraph.domainDim].data)
newNode = GraphNode{P}($op, [u.index + parentGraph.domainDim], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
end
end
for i in 1:length(unaryOpList)
@eval @define_GraphBuilder_unary_op_rule $unaryOpList[$i]
end
# overload binary operations in binaryOpList.
# Uses nontrivial "promote" rules to push Float64 constants to the parent graph
# Constvalue
function Base.promote(uA::GraphBuilder{T,P}, uB::Float64) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}(:const, Int[], uB, newNodeData)
push!(prevNodes, newNode)
return (uA, GraphBuilder{T,P}(length(prevNodes), parentGraph))
end
function Base.promote(uA::Float64, uB::GraphBuilder{T,P}) where {T,P}
return reverse(promote(uB, uA))
end
function Base.promote(uA::GraphBuilder_p{T,P}, uB::Float64) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}(:const, Int[], uB, newNodeData)
push!(prevNodes, newNode)
return (uA, GraphBuilder{T,P}(length(prevNodes), parentGraph))
end
function Base.promote(uA::Float64, uB::GraphBuilder_p{T,P}) where {T,P}
return reverse(promote(uB, uA))
end
# Parameter
Base.promote(uA::GraphBuilder{T,P}, uB::GraphBuilder{T,P}) where {T,P} = (uA, uB)
Base.promote(uA::GraphBuilder{T,P}, uB::GraphBuilder_p{T,P}) where {T,P} = (uA, uB)
Base.promote(uA::GraphBuilder_p{T,P}, uB::GraphBuilder{T,P}) where {T,P} = (uA, uB)
Base.promote(uA::GraphBuilder_p{T,P}, uB::GraphBuilder_p{T,P}) where {T,P} = (uA, uB)
macro define_GraphBuilder_binary_op_rule(op)
opEval = eval(op)
return quote
function Base.$opEval(uA::GraphBuilder{T,P}, uB::GraphBuilder{T,P}) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}($op, [uA.index, uB.index], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.$opEval(uA::GraphBuilder{T,P}, uB::GraphBuilder_p{T,P}) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}($op, [uA.index, uB.index + parentGraph.domainDim], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.$opEval(uA::GraphBuilder_p{T,P}, uB::GraphBuilder{T,P}) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}($op, [uA.index + parentGraph.domainDim, uB.index], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.$opEval(uA::GraphBuilder_p{T,P}, uB::GraphBuilder_p{T,P}) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}($op, [uA.index + parentGraph.domainDim, uB.index + parentGraph.domainDim], 0.0, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.$opEval(uA::GraphBuilder{T,P}, uB::Float64) where {T,P}
return $opEval(promote(uA, uB)...)
end
function Base.$opEval(uA::Float64, uB::GraphBuilder{T,P}) where {T,P}
return $opEval(promote(uA, uB)...)
end
function Base.$opEval(uA::GraphBuilder_p{T,P}, uB::Float64) where {T,P}
return $opEval(promote(uA, uB)...)
end
function Base.$opEval(uA::Float64, uB::GraphBuilder_p{T,P}) where {T,P}
return $opEval(promote(uA, uB)...)
end
end
end
for i in 1:length(binaryOpList)
@eval @define_GraphBuilder_binary_op_rule $binaryOpList[$i]
end
# constant integer exponents are supported
function Base.:^(uA::GraphBuilder{T,P}, uB::Int) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}(:^, [uA.index], uB, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
function Base.:^(uA::GraphBuilder_p{T,P}, uB::Int) where {T,P}
parentGraph = uA.graph
prevNodes = parentGraph.nodeList
newNodeData = deepcopy(prevNodes[uA.index].data)
newNode = GraphNode{P}(:^, [uA.index + parentGraph.domainDim], uB, newNodeData)
push!(prevNodes, newNode)
return GraphBuilder{T,P}(length(prevNodes), parentGraph)
end
end # module