forked from BachiLi/loma_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_diff.py
445 lines (379 loc) · 17.1 KB
/
forward_diff.py
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
import ir
ir.generate_asdl_file()
import _asdl.loma as loma_ir
import irmutator
import autodiff
from itertools import chain
def forward_diff(
diff_func_id: str,
structs: dict[str, loma_ir.Struct],
funcs: dict[str, loma_ir.func],
diff_structs: dict[str, loma_ir.Struct],
func: loma_ir.FunctionDef,
func_to_fwd: dict[str, str],
) -> loma_ir.FunctionDef:
"""Given a primal loma function func, apply forward differentiation
and return a function that computes the total derivative of func.
For example, given the following function:
def square(x : In[float]) -> float:
return x * x
and let diff_func_id = 'd_square', forward_diff() should return
def d_square(x : In[_dfloat]) -> _dfloat:
return make__dfloat(x.val * x.val, x.val * x.dval + x.dval * x.val)
where the class _dfloat is
class _dfloat:
val : float
dval : float
and the function make__dfloat is
def make__dfloat(val : In[float], dval : In[float]) -> _dfloat:
ret : _dfloat
ret.val = val
ret.dval = dval
return ret
Parameters:
diff_func_id - the ID of the returned function
structs - a dictionary that maps the ID of a Struct to
the corresponding Struct
funcs - a dictionary that maps the ID of a function to
the corresponding func
diff_structs - a dictionary that maps the ID of the primal
Struct to the corresponding differential Struct
e.g., diff_structs['float'] returns _dfloat
func - the function to be differentiated
func_to_fwd - mapping from primal function ID to its forward differentiation
"""
# HW1 happens here. Modify the following IR mutators to perform
# forward differentiation.
# Apply the differentiation.
class FwdDiffMutator(irmutator.IRMutator):
def mutate_function_def(self, node):
# Arguments
dargs = []
for arg in node.args:
dstructs_t = autodiff.type_to_diff_type(diff_structs, arg.t)
darg = loma_ir.Arg(id=arg.id, t=dstructs_t, i=arg.i)
dargs.append(darg)
# Return
return_dstruct = autodiff.type_to_diff_type(diff_structs, node.ret_type)
# Body
new_body = [self.mutate_stmt(stmt) for stmt in node.body]
# Important: mutate_stmt can return a list of statements. We need to flatten the list.
new_body = irmutator.flatten(new_body)
return loma_ir.FunctionDef(
diff_func_id,
dargs,
new_body,
node.is_simd,
node.is_openMpi,
return_dstruct,
lineno=node.lineno,
)
# return super().mutate_function_def(node)
def mutate_return(self, node):
mutated_node = self.mutate_expr(node.val)
if node.val.t == loma_ir.Int():
return loma_ir.Return(mutated_node[0], lineno=node.lineno)
elif isinstance(node.val.t, loma_ir.Struct):
return node
else:
return loma_ir.Return(
loma_ir.Call("make__dfloat", [mutated_node[0], mutated_node[1]]),
lineno=node.lineno,
)
def mutate_declare(self, node):
mutated_expression = None
if node.val:
mutated_expression = self.mutate_expr(node.val)
if isinstance(mutated_expression, list) and (mutated_expression[0], loma_ir.Call):
mutated_expression = mutated_expression[0]
elif isinstance(node.t, loma_ir.Int):
mutated_expression = mutated_expression[0]
elif isinstance(node.t, loma_ir.Float):
mutated_expression = loma_ir.Call(
"make__dfloat", [mutated_expression[0], mutated_expression[1]]
)
declare_dstruct = autodiff.type_to_diff_type(diff_structs, node.t)
return loma_ir.Declare(
node.target, declare_dstruct, mutated_expression, lineno=node.lineno
)
def mutate_assign(self, node):
mutated_expression = None
if node.val:
mutated_expression = self.mutate_expr(node.val)
if isinstance(node.val.t, loma_ir.Int):
mutated_expression = mutated_expression[0]
elif isinstance(node.val.t, loma_ir.Float):
mutated_expression = loma_ir.Call(
"make__dfloat", [mutated_expression[0], mutated_expression[1]]
)
# Parse expressions in target and get the val
if isinstance(node.target, loma_ir.StructAccess):
mutated_target = loma_ir.StructAccess(
node.target.struct, node.target.member_id
)
elif isinstance(node.target.t, loma_ir.Struct):
mutated_target = self.mutate_expr(node.target)
else:
temp = self.mutate_expr(node.target)
if isinstance(temp[0], loma_ir.StructAccess):
mutated_target = temp[0].struct
else:
mutated_target = temp[0]
return loma_ir.Assign(
mutated_target, mutated_expression, lineno=node.lineno
)
def mutate_ifelse(self, node):
new_cond, _ = self.mutate_expr(node.cond)
new_then_stmts = [self.mutate_stmt(stmt) for stmt in node.then_stmts]
new_else_stmts = [self.mutate_stmt(stmt) for stmt in node.else_stmts]
# Important: mutate_stmt can return a list of statements. We need to flatten the lists.
new_then_stmts = irmutator.flatten(new_then_stmts)
new_else_stmts = irmutator.flatten(new_else_stmts)
return loma_ir.IfElse(\
new_cond,
new_then_stmts,
new_else_stmts,
lineno = node.lineno)
def mutate_while(self, node):
new_cond, _ = self.mutate_expr(node.cond)
new_body = [self.mutate_stmt(stmt) for stmt in node.body]
# Important: mutate_stmt can return a list of statements. We need to flatten the list.
new_body = irmutator.flatten(new_body)
return loma_ir.While(\
new_cond,
node.max_iter,
new_body,
lineno = node.lineno)
def mutate_const_float(self, node):
return (node, loma_ir.ConstFloat(0.0))
def mutate_const_int(self, node):
return (node, loma_ir.ConstInt(0))
# return super().mutate_const_int(node)
def mutate_var(self, node):
if node.t == loma_ir.Int() and isinstance(node.t, loma_ir.Int):
return (node, loma_ir.ConstInt(0))
elif isinstance(node.t, loma_ir.Array) or isinstance(
node.t, loma_ir.Struct
):
return node
return (
loma_ir.StructAccess(node, "val"),
loma_ir.StructAccess(node, "dval"),
)
def mutate_array_access(self, node):
arr = self.mutate_expr(node.array)
idx = self.mutate_expr(node.index)
if isinstance(idx, tuple):
idx = idx[0]
if isinstance(arr, tuple):
arr = arr[0]
return_dstruct = autodiff.type_to_diff_type(diff_structs, node.t)
if arr.t and arr.t.t == loma_ir.Int() and isinstance(arr.t.t, loma_ir.Int):
val = loma_ir.ArrayAccess(
arr, idx, lineno=node.lineno, t=return_dstruct
)
d_val = loma_ir.ConstInt(0)
else:
val = loma_ir.StructAccess(
loma_ir.ArrayAccess(arr, idx, lineno=node.lineno, t=return_dstruct),
"val",
)
d_val = loma_ir.StructAccess(
loma_ir.ArrayAccess(arr, idx, lineno=node.lineno, t=return_dstruct),
"dval",
)
return (val, d_val)
def mutate_struct_access(self, node):
if node.t == loma_ir.Int() and isinstance(node.t, loma_ir.Int):
return (node, loma_ir.ConstInt(0))
elif isinstance(node.t, loma_ir.Array):
return (node, None)
val = loma_ir.StructAccess(node, "val")
dval = loma_ir.StructAccess(node, "dval")
return (val, dval)
def mutate_add(self, node):
left_exp = self.mutate_expr(node.left)
right_exp = self.mutate_expr(node.right)
val_add = loma_ir.BinaryOp(loma_ir.Add(), left_exp[0], right_exp[0])
dval_add = loma_ir.BinaryOp(loma_ir.Add(), left_exp[1], right_exp[1])
return (val_add, dval_add)
def mutate_sub(self, node):
left_exp = self.mutate_expr(node.left)
right_exp = self.mutate_expr(node.right)
val_add = loma_ir.BinaryOp(loma_ir.Sub(), left_exp[0], right_exp[0])
dval_add = loma_ir.BinaryOp(loma_ir.Sub(), left_exp[1], right_exp[1])
return (val_add, dval_add)
def mutate_mul(self, node):
left_exp = self.mutate_expr(node.left)
right_exp = self.mutate_expr(node.right)
val_mul = loma_ir.BinaryOp(loma_ir.Mul(), left_exp[0], right_exp[0])
dval_mul_1 = loma_ir.BinaryOp(loma_ir.Mul(), left_exp[0], right_exp[1])
dval_mul_2 = loma_ir.BinaryOp(loma_ir.Mul(), left_exp[1], right_exp[0])
dval_mul_final = loma_ir.BinaryOp(loma_ir.Add(), dval_mul_1, dval_mul_2)
return (val_mul, dval_mul_final)
def mutate_div(self, node):
left_exp = self.mutate_expr(node.left)
right_exp = self.mutate_expr(node.right)
val_mul = loma_ir.BinaryOp(loma_ir.Div(), left_exp[0], right_exp[0])
dval_mul_1 = loma_ir.BinaryOp(loma_ir.Mul(), left_exp[1], right_exp[0])
dval_mul_2 = loma_ir.BinaryOp(loma_ir.Mul(), left_exp[0], right_exp[1])
dval_numerator = loma_ir.BinaryOp(loma_ir.Sub(), dval_mul_1, dval_mul_2)
dval_denominator = loma_ir.BinaryOp(
loma_ir.Mul(), right_exp[0], right_exp[0]
)
dval_final = loma_ir.BinaryOp(
loma_ir.Div(), dval_numerator, dval_denominator
)
return (val_mul, dval_final)
def mutate_less(self, node):
left, _ = self.mutate_expr(node.left)
right, _ = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(\
loma_ir.Less(),
left,
right,
lineno = node.lineno,
t = node.t), None]
def mutate_greater(self, node):
left, _ = self.mutate_expr(node.left)
right, _ = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(\
loma_ir.Greater(),
left,
right,
lineno = node.lineno,
t = node.t), None]
def mutate_call(self, node):
call_dstruct = autodiff.type_to_diff_type(diff_structs, node.t)
mutated_expression = [self.mutate_expr(arg) for arg in node.args]
mutated_args = list(chain.from_iterable(mutated_expression))
val = None
dval = None
x_val = mutated_args[0]
x_dval = mutated_args[1]
match node.id:
case "sin":
val = loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
)
dval = loma_ir.BinaryOp(
loma_ir.Mul(),
loma_ir.Call(
"cos", [x_val], lineno=node.lineno, t=call_dstruct
),
x_dval,
)
case "cos":
val = loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
)
dval_mul = loma_ir.BinaryOp(
loma_ir.Mul(),
loma_ir.Call(
"sin", [x_val], lineno=node.lineno, t=call_dstruct
),
x_dval,
)
dval = loma_ir.BinaryOp(
loma_ir.Mul(), dval_mul, loma_ir.ConstFloat(-1.0)
)
case "sqrt":
val = loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
)
dval_mul = loma_ir.BinaryOp(
loma_ir.Mul(), loma_ir.ConstFloat(0.5), x_dval
)
dval = loma_ir.BinaryOp(
loma_ir.Div(),
dval_mul,
loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
),
)
case "pow":
y_val, y_dval = mutated_args[2], mutated_args[3]
val = loma_ir.Call(
node.id, [x_val, y_val], lineno=node.lineno, t=call_dstruct
)
dval_mul_1 = loma_ir.BinaryOp(
loma_ir.Mul(), x_dval, y_val
) # dx * y
y_val_less_1 = loma_ir.BinaryOp(
loma_ir.Sub(), y_val, loma_ir.ConstFloat(1.0)
)
dval_mul_2 = loma_ir.BinaryOp(
loma_ir.Mul(),
dval_mul_1,
loma_ir.Call(
node.id,
[x_val, y_val_less_1],
lineno=node.lineno,
t=call_dstruct,
),
) # dx * y * x^y-1
dval_mul_3 = loma_ir.BinaryOp(
loma_ir.Mul(), y_dval, val
) # dy * x^y
dval_mul_4 = loma_ir.BinaryOp(
loma_ir.Mul(),
dval_mul_3,
loma_ir.Call(
"log", [x_val], lineno=node.lineno, t=call_dstruct
),
) # dy * x^y * log(x)
# dx * y * x^y-1 + dy * x^y * log(x)
dval = loma_ir.BinaryOp(loma_ir.Add(), dval_mul_2, dval_mul_4)
case "exp":
val = loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
)
dval = loma_ir.BinaryOp(
loma_ir.Mul(),
x_dval,
loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
),
)
case "log":
val = loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
)
dval = loma_ir.BinaryOp(loma_ir.Div(), x_dval, x_val)
case "int2float":
return (
loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
),
loma_ir.ConstFloat(0.0),
)
case "float2int":
return (
loma_ir.Call(
node.id, [x_val], lineno=node.lineno, t=call_dstruct
),
loma_ir.ConstInt(0),
)
case _ :
dfloat_new_args = []
fwd_func_id = func_to_fwd[node.id]
## Check if a param is of type Out, add original param in that case
for i, arg in enumerate(mutated_expression):
called_func_def = funcs[node.id]
if called_func_def.args[i].i ==loma_ir.Out():
dfloat_new_args.append(node.args[i])
else:
dfloat_new_args.append(loma_ir.Call('make__dfloat', [arg[0], arg[1]]))
if node.t == loma_ir.Float():
val = loma_ir.StructAccess(loma_ir.Call(fwd_func_id, dfloat_new_args), 'val')
dval = loma_ir.StructAccess(loma_ir.Call(fwd_func_id, dfloat_new_args), 'dval')
return val, dval
else:
return loma_ir.Call(fwd_func_id, dfloat_new_args), None
return (val, dval)
def mutate_call_stmt(self, node):
mutated_call = self.mutate_expr(node.call)
return loma_ir.CallStmt(\
mutated_call[0],
lineno = node.lineno)
return FwdDiffMutator().mutate_function_def(func)