-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_diff.py
More file actions
544 lines (499 loc) · 21.2 KB
/
forward_diff.py
File metadata and controls
544 lines (499 loc) · 21.2 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import ir
ir.generate_asdl_file()
import _asdl.loma as loma_ir
import irmutator
import autodiff
def loma_to_diff_type(t : loma_ir.type,
diff_structs : dict[str, loma_ir.Struct]) -> loma_ir.Struct:
""" Given a loma type, maps to the corresponding differential Struct by looking up diff_structs
"""
match t:
case loma_ir.Int():
return diff_structs['int']
case loma_ir.Float():
return diff_structs['float']
case loma_ir.Array():
return loma_ir.Array(loma_to_diff_type(t.t, diff_structs), t.static_size)
case loma_ir.Struct():
return diff_structs[t.id]
case None:
return None
case _:
assert False
def terminal_func_calls(func_id):
def Dsin(input):
input_x = input[0]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
val = loma_ir.Call('sin', [x])
dval = loma_ir.BinaryOp(loma_ir.Mul(), dx, loma_ir.Call('cos', [x]))
# return loma_ir.Call('make__dfloat', [val, dval])
return [val, dval]
def Dcos(input):
input_x = input[0]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
val = loma_ir.Call('cos', [x])
dval = loma_ir.BinaryOp(loma_ir.Mul(), dx, loma_ir.BinaryOp(loma_ir.Sub(), loma_ir.ConstFloat(0.0), loma_ir.Call('sin', [x])))
return [val, dval]
def Dpow(input):
input_x = input[0]
input_y = input[1]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
y = loma_ir.StructAccess(input_y, 'val')
dy = loma_ir.StructAccess(input_y, 'dval')
val = loma_ir.Call('pow', [x, y])
partial_x = loma_ir.BinaryOp(loma_ir.Mul(), dx, (loma_ir.BinaryOp(loma_ir.Mul(), y, loma_ir.Call('pow', [x, loma_ir.BinaryOp(loma_ir.Sub(), y, loma_ir.ConstInt(1))]))))
logx = loma_ir.Call('log', [x])
partial_y = loma_ir.BinaryOp(loma_ir.Mul(), dy, loma_ir.BinaryOp(loma_ir.Mul(), logx, val))
dval = loma_ir.BinaryOp(loma_ir.Add(), partial_x, partial_y)
return [val, dval]
def Dlog(input):
input_x = input[0]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
val = loma_ir.Call('log', [x])
dval = loma_ir.BinaryOp(loma_ir.Div(), dx, x)
return [val, dval]
def Dexp(input):
input_x = input[0]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
val = loma_ir.Call('exp', [x])
dval = loma_ir.BinaryOp(loma_ir.Mul(), dx, val)
return [val, dval]
def Dsqrt(input):
input_x = input[0]
x = loma_ir.StructAccess(input_x, 'val')
dx = loma_ir.StructAccess(input_x, 'dval')
val = loma_ir.Call('sqrt', [x])
dval = loma_ir.BinaryOp(loma_ir.Div(), dx, loma_ir.BinaryOp(loma_ir.Mul(), loma_ir.ConstFloat(2.0), val))
return [val, dval]
def Dfloat2int(input):
x = input
val = loma_ir.Call('float2int',[x], t=loma_ir.Int())
dval = loma_ir.ConstFloat(0.0)
return [val, dval]
def Dint2float(input):
x = input
val = loma_ir.Call('int2float',[x], t=loma_ir.Float())
dval = loma_ir.ConstFloat(0.0)
return [val, dval]
dfunc_dict = {}
dfunc_dict['sin'] = Dsin
dfunc_dict['cos'] = Dcos
dfunc_dict['pow'] = Dpow
dfunc_dict['log'] = Dlog
dfunc_dict['exp'] = Dexp
dfunc_dict['sqrt'] = Dsqrt
dfunc_dict['float2int'] = Dfloat2int
dfunc_dict['int2float'] = Dint2float
if func_id not in dfunc_dict.keys():
return None
return dfunc_dict[func_id]
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):
# HW1:
# FunctionDef(string id, arg* args, stmt* body, bool is_simd, type? ret_type)
# The mutated function:
# id: diff_func_id
# args: Diff[args]
# body: recursively call mutate_stmt()
# is_simd: remain unchanged
# ret_type: _dfloat
# lineno: the lineno of Forward/Backward func
# Diff[args], see tool function:
new_args = []
for arg in node.args:
diff_arg = loma_ir.Arg(id=arg.id, t=loma_to_diff_type(arg.t, diff_structs), i=arg.i)
new_args.append(diff_arg)
new_args = tuple(new_args)
# Recursively mutate stmt
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)
#
new_ret_type = loma_to_diff_type(node.ret_type, diff_structs)
fwd_func_id = func_to_fwd[node.id]
lineno = funcs[fwd_func_id].lineno
return loma_ir.FunctionDef( \
diff_func_id,
new_args,
new_body,
node.is_simd,
new_ret_type,
lineno=lineno)
def mutate_return(self, node):
# HW1:
# return expr
# ret type int:
# ret type float: assemble the results of expr by building a loma_ir.Call to make__dfloat, with the primal and differential expressions being the two arguments.
match func.ret_type:
case loma_ir.Int():
val, dval = self.mutate_expr(node.val)
return loma_ir.Return(val, lineno=node.lineno)
case loma_ir.Float():
val, dval = self.mutate_expr(node.val)
ret = loma_ir.Call("make__dfloat", [val, dval])
return loma_ir.Return(ret, lineno=node.lineno)
case _:
# ret type Structures
ret = self.mutate_expr(node.val)[0]
return loma_ir.Return(ret, lineno=node.lineno)
def mutate_declare(self, node):
# HW1:
# Declare (string target, type t, expr* val)
target = node.target
diff_t = loma_to_diff_type(node.t, diff_structs)
match node.t:
case loma_ir.Int():
val, dval = self.mutate_expr(node.val)
return loma_ir.Declare(target, diff_t, val, lineno=node.lineno)
case loma_ir.Float():
if node.val is None:
return loma_ir.Declare(target, diff_t, None, lineno=node.lineno)
else:
val, dval = self.mutate_expr(node.val)
ret_val = loma_ir.Call("make__dfloat", [val, dval])
return loma_ir.Declare( \
target,
diff_t,
ret_val,
lineno=node.lineno)
case _:
# Structures
if node.val is None:
return loma_ir.Declare(target, diff_t, None, lineno=node.lineno)
return loma_ir.Declare( \
target,
diff_t,
val=self.mutate_expr(node.val)[0],
lineno=node.lineno)
def mutate_assign(self, node):
# HW1:
# expr target = expr val
match node.target:
# FIXME ugly, any better resolution?
case loma_ir.ArrayAccess():
target = loma_ir.ArrayAccess( \
self.mutate_expr(node.target.array)[0],
self.mutate_expr(node.target.index)[0],
lineno=node.lineno,
t=loma_to_diff_type(node.target.t, diff_structs))
case _:
target = node.target
val, dval = self.mutate_expr(node.val)
if node.target.t == loma_ir.Int():
return loma_ir.Assign(target, val, lineno=node.lineno)
elif node.target.t == loma_ir.Float():
ret_val = loma_ir.Call("make__dfloat", [val, dval])
else:
ret_val = val
return loma_ir.Assign(target, ret_val, lineno=node.lineno)
def mutate_ifelse(self, node):
# HW3:
new_cond = self.mutate_expr(node.cond)[0]
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):
# HW3:
new_cond = self.mutate_expr(node.cond)[0]
new_body = [self.mutate_stmt(stmt) for stmt in node.body]
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):
# HW1:
# (val, 0)
return [node, loma_ir.ConstFloat(0.0)]
def mutate_const_int(self, node):
# HW1:
# (val, 0)
return [node, loma_ir.ConstFloat(0.0)]
def mutate_var(self, node):
# HW1:
# Mutate a Variable, and return the tuple (val, dval), we need to use loma_ir.StructAccess to get the member variable(val & dval)
match node.t:
# for integers, return (int, 0)
case loma_ir.Int():
return [node, loma_ir.ConstFloat(0.0)]
# for arrays, no nothing
case loma_ir.Array():
return [node, None]
case loma_ir.Float():
val = loma_ir.StructAccess(node, 'val', lineno=node.lineno)
dval = loma_ir.StructAccess(node, 'dval', lineno=node.lineno)
return [val, dval]
case _:
return [node, None]
def mutate_array_access(self, node):
# HW1:
match node.t:
case loma_ir.Int():
return [loma_ir.ArrayAccess(self.mutate_expr(node.array)[0], self.mutate_expr(node.index)[0],
lineno=node.lineno, t=node.t),
loma_ir.ConstFloat(0.0)]
case loma_ir.Float():
item = loma_ir.ArrayAccess( \
self.mutate_expr(node.array)[0],
self.mutate_expr(node.index)[0],
lineno=node.lineno,
t=loma_to_diff_type(node.t, diff_structs))
val = loma_ir.StructAccess(item, 'val', lineno=node.lineno)
dval = loma_ir.StructAccess(item, 'dval', lineno=node.lineno)
return [val, dval]
case _:
return [node, None]
def mutate_struct_access(self, node):
# HW1:
item = loma_ir.StructAccess( \
self.mutate_expr(node.struct)[0],
node.member_id,
lineno=node.lineno,
t=loma_to_diff_type(node.t, diff_structs))
match item.t:
case loma_ir.Int():
return (item, loma_ir.ConstFloat(0.0))
case loma_ir.Struct():
if item.t.id == '_dfloat':
val = loma_ir.StructAccess(item, 'val', lineno=node.lineno)
dval = loma_ir.StructAccess(item, 'dval', lineno=node.lineno)
return [val, dval]
else:
return [item, None]
case _:
return [item, None]
def mutate_add(self, node):
# HW1:
# (x.val + y.val, x.dval + y.dval)
xval, xdval = self.mutate_expr(node.left)
yval, ydval = self.mutate_expr(node.right)
val = loma_ir.BinaryOp( \
loma_ir.Add(),
xval,
yval,
lineno=node.lineno,
)
dval = loma_ir.BinaryOp( \
loma_ir.Add(),
xdval,
ydval,
lineno=node.lineno,
)
return [val, dval]
def mutate_sub(self, node):
# HW1:
# (x.val - y.val, x.dval - y.dval)
xval, xdval = self.mutate_expr(node.left)
yval, ydval = self.mutate_expr(node.right)
val = loma_ir.BinaryOp( \
loma_ir.Sub(),
xval,
yval,
lineno=node.lineno,
)
dval = loma_ir.BinaryOp( \
loma_ir.Sub(),
xdval,
ydval,
lineno=node.lineno,
)
return [val, dval]
def mutate_call_stmt(self, node):
return loma_ir.CallStmt( \
self.mutate_expr(node.call)[0],
lineno=node.lineno)
def mutate_mul(self, node):
# HW1:
# (x.val * y.val, x.dval * y.val + x.val * y.dval)
xval, xdval = self.mutate_expr(node.left)
yval, ydval = self.mutate_expr(node.right)
val = loma_ir.BinaryOp( \
loma_ir.Mul(),
xval,
yval,
lineno=node.lineno,
)
xdy = loma_ir.BinaryOp( \
loma_ir.Mul(),
xval,
ydval,
lineno=node.lineno,
)
ydx = loma_ir.BinaryOp( \
loma_ir.Mul(),
xdval,
yval,
lineno=node.lineno,
)
dval = loma_ir.BinaryOp( \
loma_ir.Add(),
xdy,
ydx,
lineno=node.lineno,
)
return [val, dval]
def mutate_div(self, node):
# HW1:
# (x.val / y.val, (ydx - xdy) / y^2)
xval, xdval = self.mutate_expr(node.left)
yval, ydval = self.mutate_expr(node.right)
val = loma_ir.BinaryOp( \
loma_ir.Div(),
xval,
yval,
lineno=node.lineno,
)
xdy = loma_ir.BinaryOp( \
loma_ir.Mul(),
xval,
ydval,
lineno=node.lineno,
)
ydx = loma_ir.BinaryOp( \
loma_ir.Mul(),
xdval,
yval,
lineno=node.lineno,
)
y_sqr = loma_ir.BinaryOp( \
loma_ir.Mul(),
yval,
yval,
lineno=node.lineno,
)
dval = loma_ir.BinaryOp( \
loma_ir.Div(),
loma_ir.BinaryOp(loma_ir.Sub(), ydx, xdy, lineno=node.lineno),
y_sqr,
lineno=node.lineno,
)
return [val, dval]
def mutate_call(self, node):
# HW1:
# before: y = f(x)
# after: (y.val, y.dval) = Df(x.val, x.dval);
func_id = node.id
def mutate_arg(arg):
match arg.t:
case loma_ir.Float():
val, dval = self.mutate_expr(arg)
return loma_ir.Call("make__dfloat", [val, dval])
case _:
val, _ = self.mutate_expr(arg)
return val
if terminal_func_calls(func_id) is not None:
# Terminal functions
diff_func = terminal_func_calls(func_id)
match func_id:
case "int2float":
val, dval = self.mutate_expr(node.args[0])
return diff_func(val)
case "float2int":
val, dval = self.mutate_expr(node.args[0])
return diff_func(val)
case _:
diff_args = [mutate_arg(arg) for arg in node.args]
return diff_func(diff_args)
else:
# Get arg types IN or OUT
primary_func_def = funcs[func_id]
assert isinstance(primary_func_def, loma_ir.FunctionDef)
diff_args = []
for i in range(len(node.args)):
arg = node.args[i]
arg_def = primary_func_def.args[i]
if arg_def.i == loma_ir.In():
diff_args.append(mutate_arg(arg))
else:
diff_args.append(arg)
# Call the differential function
diff_func_id = f"_d_fwd_{func_id}"
ret_type = primary_func_def.ret_type
match ret_type:
case loma_ir.Float():
dfloat_ret = loma_ir.Call(diff_func_id, args=diff_args, lineno=node.lineno)
return [loma_ir.StructAccess(struct=dfloat_ret, member_id='val'), loma_ir.StructAccess(struct=dfloat_ret, member_id='dval')]
case None:
return [loma_ir.Call(diff_func_id, args=diff_args, lineno=node.lineno),]
case loma_ir.Struct():
return [loma_ir.Call(diff_func_id, args=diff_args, lineno=node.lineno), None]
case _:
raise NotImplementedError
def mutate_less(self, node):
lval, ldval = self.mutate_expr(node.left)
rval, rdval = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(loma_ir.Less(), lval, rval, lineno=node.lineno), ]
def mutate_less_equal(self, node):
lval, ldval = self.mutate_expr(node.left)
rval, rdval = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(loma_ir.LessEqual(), lval, rval, lineno=node.lineno), ]
def mutate_greater(self, node):
lval, ldval = self.mutate_expr(node.left)
rval, rdval = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(loma_ir.Greater(), lval, rval, lineno=node.lineno), ]
def mutate_greater_equal(self, node):
lval, ldval = self.mutate_expr(node.left)
rval, rdval = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(loma_ir.GreaterEqual(), lval, rval, lineno=node.lineno), ]
def mutate_equal(self, node):
lval, ldval = self.mutate_expr(node.left)
rval, rdval = self.mutate_expr(node.right)
return [loma_ir.BinaryOp(loma_ir.Equal(), lval, rval, lineno=node.lineno), ]
def mutate_and(self, node):
return super().mutate_and(node)
def mutate_or(self, node):
return super().mutate_or(node)
return FwdDiffMutator().mutate_function_def(func)