-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.mojo
495 lines (357 loc) · 14.2 KB
/
engine.mojo
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
from memory.unsafe import Pointer
@register_passable("trivial")
struct Value:
"""Stores a single scalar value and its gradient."""
var data: Pointer[Float32] # pointer to float32
var grad: Pointer[Float32]
# # Commenting this as Pointer[Int] == Pointer[Value] throws error
# # Won't be able to perform visited check in the backward pass
# var l: Pointer[Int]
# var r: Pointer[Int]
var l: Pointer[Value]
var r: Pointer[Value]
var _op: StringRef
fn __init__(inout self, inp_data: Float32):
self.data = Pointer[Float32].alloc(1)
self.data.store(inp_data)
self.grad = Pointer[Float32].alloc(1)
self.grad.store(0.0)
self.l = Pointer[Value]()
self.r = Pointer[Value]()
self._op = ""
# --> ADD #
@always_inline
fn __add__(inout self, inout other: Pointer[Value]) -> Value:
"""Pointer + Pointer."""
var new_val: Value = Value(
self.data.load() + other.load().data.load()
)
var ptr_l: Pointer[Value] = Pointer[Value].alloc(1)
ptr_l.store(self)
# The bitcast method is used to create a new pointer, new_ptr, that points to the same memory location but treats the pointee as a Int.
new_val.l = ptr_l.bitcast[Value]()
new_val.r = other.bitcast[Value]()
new_val._op = "+"
return new_val
@always_inline
fn __add__(inout self, inout other: Float32) -> Value:
"""Pointer + Float32."""
var other_v: Value = Value(other)
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other_v)
return self + ptr_v
@always_inline
fn __add__(inout self, inout other: Value) -> Value:
"""Pointer + Value."""
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other)
# this add will trigger the pointer __add__
return self + ptr_v
# --> MUL #
@always_inline
fn __mul__(inout self, inout other: Pointer[Value]) -> Value:
"""Pointer * Pointer."""
var new_val: Value = Value(
self.data.load() * other.load().data.load()
)
var ptr_l: Pointer[Value] = Pointer[Value].alloc(1)
ptr_l.store(self)
# The bitcast method is used to create a new pointer, new_ptr, that points to the same memory location but treats the pointee as a Int.
new_val.l = ptr_l.bitcast[Value]()
new_val.r = other.bitcast[Value]()
new_val._op = "*"
return new_val
@always_inline
fn __mul__(inout self, inout other: Float32) -> Value:
"""Pointer * Float32."""
var other_v: Value = Value(other)
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other_v)
return self * ptr_v
@always_inline
fn __mul__(inout self, inout other: Value) -> Value:
"""Pointer * Value."""
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other)
# this add will trigger the pointer __add__
return self * ptr_v
# --> POW #
@always_inline
fn __pow__(inout self, inout other: Pointer[Value]) -> Value:
"""Pointer ** Pointer."""
var new_val: Value = Value(
self.data.load() ** other.load().data.load()
)
var ptr_l: Pointer[Value] = Pointer[Value].alloc(1)
ptr_l.store(self)
# The bitcast method is used to create a new pointer, new_ptr, that points to the same memory location but treats the pointee as a Int.
new_val.l = ptr_l.bitcast[Value]()
new_val.r = other.bitcast[Value]()
new_val._op = "**"
return new_val
@always_inline
fn __pow__(inout self, inout other: Float32) -> Value:
"""Pointer ** Float32."""
var other_v: Value = Value(other)
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other_v)
return self ** ptr_v
@always_inline
fn __pow__(inout self, inout other: Value) -> Value:
"""Pointer ** Value."""
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other)
# this add will trigger the pointer __add__
return self ** ptr_v
# --> RELU #
@always_inline
fn relu(inout self) -> Value:
"""Perform RELU."""
var new_val: Value = Value(
0 if self.data.load() < 0 else self.data.load()
)
var ptr_l: Pointer[Value] = Pointer[Value].alloc(1)
ptr_l.store(self)
# The bitcast method is used to create a new pointer, new_ptr, that points to the same memory location but treats the pointee as a Int.
new_val.l = ptr_l.bitcast[Value]()
# For ReLU, we dont need the right pointer, so it would be null
new_val._op = "ReLU"
return new_val
# --> Neg #
@always_inline
fn __neg__(inout self) -> Value:
"""Self * -1."""
var data: Float32 = -1
return self * data
# --> rADD #
@always_inline
fn __radd__(inout self, inout other: Float32) -> Value:
"""Other + Self."""
return self + other
@always_inline
fn __radd__(inout self, inout other: Pointer[Value]) -> Value:
"""Other + Self."""
return self + other
# --> sub #
@always_inline
fn __sub__(inout self, inout other: Pointer[Value]) -> Value:
"""Self - Other."""
var val: Value = other.load()
var data: Value = -val
return self + data
@always_inline
fn __sub__(inout self, inout other: Float32) -> Value:
"""Self - Other."""
var data: Value = Value(other)
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(data)
return self - ptr_v
@always_inline
fn __sub__(inout self, inout other: Value) -> Value:
"""Self - Other."""
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other)
return self - ptr_v
# --> rsub #
@always_inline
fn __rsub__(inout self, inout other: Pointer[Value]) -> Value:
"""Other - Self."""
var val: Value = other.load()
return val - self
@always_inline
fn __rsub__(inout self, inout other: Float32) -> Value:
"""Other - Self."""
var val: Value = Value(other)
return val - self
# --> rmul #
@always_inline
fn __rmul__(inout self, inout other: Pointer[Value]) -> Value:
"""Other * Self."""
return self * other
@always_inline
fn __rmul__(inout self, inout other: Float32) -> Value:
"""Other * Self."""
return self * other
# --> truediv #
@always_inline
fn __truediv__(inout self, inout other: Pointer[Value]) -> Value:
"""Self / Other."""
var val: Value = other.load()
var powie: Float32 = -1
var data: Value = val**powie
return self * data
@always_inline
fn __truediv__(inout self, inout other: Value) -> Value:
"""Self / Other."""
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(other)
return self / ptr_v
@always_inline
fn __truediv__(inout self, inout other: Float32) -> Value:
"""Self / Other."""
var data: Value = Value(other)
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(data)
return self / ptr_v
# --> rtruediv #
@always_inline
fn __rtruediv__(inout self, inout other: Float32) -> Value:
"""Other / Self."""
var val: Value = Value(other)
return val / self
@always_inline
fn __rtruediv__(inout self, inout other: Pointer[Value]) -> Value:
"""Other / Self."""
var val: Value = other.load()
return val / self
## BACKWARD PASS ##
# Moving this to another file might be better
# --> add #
@staticmethod
fn backward_add(inout ptr_v: Pointer[Value]):
"""Don't need to check for null pointer cases as it wont be null.
Still checking for safety.
z = x + y
l = 2*z
dl/dx = dl/dz * dz/dx == 2 * 1
"""
var val: Value = ptr_v.load() # this returns a copy of the value therefore update has to be done to the ptr_v
if val.l == Pointer[Value]():
print("Inside ptr null check")
return
var l_update: Float32 = val.l.load().grad.load() + val.grad.load()
ptr_v.load().l.load().grad.store(l_update)
if val.r == Pointer[Value]():
print("Inside ptr null check")
return
var r_update: Float32 = val.r.load().grad.load() + val.grad.load()
ptr_v.load().r.load().grad.store(r_update)
# --> mul #
@staticmethod
fn backward_mul(inout ptr_v: Pointer[Value]):
"""Don't need to check for null pointer cases as it wont be null.
Still checking for safety.
z = x * y
l = 2*z
dl/dx = dl/dz * dz/dx == 2 * y
"""
var val: Value = ptr_v.load()
if val.l == Pointer[Value]() or val.r == Pointer[Value]():
print("Inside ptr null check")
return
var l_update: Float32 = val.l.load().grad.load() + (val.grad.load() * val.r.load().data.load())
ptr_v.load().l.load().grad.store(l_update)
var r_update: Float32 = val.r.load().grad.load() + (val.grad.load() * val.l.load().data.load())
ptr_v.load().r.load().grad.store(r_update)
# --> pow #
@staticmethod
fn backward_pow(inout ptr_v: Pointer[Value]):
"""Don't need to check for null pointer cases as it wont be null.
Still checking for safety.
z = x ** y
l = 2*z
dl/dx = dl/dz * dz/dx == 2 * yx^(y-1)
Also, though we take y to be a pointer, y in this case will be treated
as a Float32. We won't update its grad.
"""
var val: Value = ptr_v.load()
if val.l == Pointer[Value]() or val.r == Pointer[Value]():
print("Inside ptr null check")
return
var l_update: Float32 = val.l.load().grad.load() + (
val.grad.load() * val.r.load().data.load() * (
val.l.load().data.load() ** (val.r.load().data.load() - 1)
)
)
ptr_v.load().l.load().grad.store(l_update)
# --> relu #
@staticmethod
fn backward_relu(inout ptr_v: Pointer[Value]):
"""Right child will be null over here.
z = x if x > 0
z = 0 if x < 0
l = 2*z
dl/dx = dl/dz * dz/dx == 2 * (1 if x > 0 else 0)
Also, though we take y to be a pointer, y in this case will be treated
as a Float32. We won't update its grad.
"""
var val: Value = ptr_v.load()
if val.l == Pointer[Value]():
print("Inside ptr null check")
return
var left: Value = val.l.bitcast[Value]().load()
left.grad.store(
left.grad.load() + (
val.grad.load() if left.data.load() > 0 else 0
)
)
var l_update: Float32 = val.l.load().grad.load() + (val.grad.load() if val.l.load().data.load() > 0 else 0)
ptr_v.load().l.load().grad.store(l_update)
@staticmethod
fn _backward(inout ptr: Pointer[Value]):
if ptr == Pointer[Value]():
print("Inside ptr null check")
return
var op: String = ptr.load()._op
if op == '':
return
elif op == '*':
Value.backward_mul(ptr)
elif op == '+':
Value.backward_add(ptr)
elif op == '**':
Value.backward_pow(ptr)
elif op == 'ReLU':
Value.backward_relu(ptr)
else:
print('op registered not supported - ', op)
@staticmethod
fn build_topo(
inout ptr_v: Pointer[Value],
inout visited: List[Pointer[Value]],
inout topo: List[Pointer[Value]]
):
if ptr_v == Pointer[Value]():
return
var is_visited: Bool = False
for item in visited:
if ptr_v == item[]:
is_visited = True
break
if not is_visited:
visited.append(ptr_v)
if ptr_v.load().l != Pointer[Value]():
# bitcast returns a new pointer pointing to the same location
# so ptr_l == ptr_v.load().l -> will be True
var ptr_l: Pointer[Value] = ptr_v.load().l.bitcast[Value]()
Value.build_topo(ptr_l, visited, topo)
if ptr_v.load().r != Pointer[Value]():
var ptr_r: Pointer[Value] = ptr_v.load().r.bitcast[Value]()
Value.build_topo(ptr_r, visited, topo)
topo.append(ptr_v)
fn backward(inout self):
"""List works but Set gives error.
List before mojo 24.2 was referred to as DynamicVector.
push_back() is now replaced with append().
"""
var visited: List[Pointer[Value]] = List[Pointer[Value]]()
var topo: List[Pointer[Value]] = List[Pointer[Value]]()
var ptr_v: Pointer[Value] = Pointer[Value].alloc(1)
ptr_v.store(self)
Value.build_topo(ptr_v, visited, topo)
self.grad.store(1.0)
for item in reversed(topo):
Value._backward(item[])
visited.clear() # Hmm, visited should be deleted by the compiler, clearing it shoulbn't be necessary
topo.clear()
ptr_v.free() # As this is a unsafe Pointer, user has to manage the deletion!
@always_inline
fn print(inout self):
print("<Value", "data: Pointer[Float32] -> ",self.data.load(), "grad: Pointer[Float32] -> ",self.grad.load(), "_op: StringRef -> ",self._op, ">")
@always_inline
fn __repr__(inout self):
print("<Value", "data: Pointer[Float32] -> ",self.data.load(), "grad: Pointer[Float32] -> ",self.grad.load(), "_op: StringRef -> ",self._op, ">")
# # TODO:: Not working, throwing error
# @always_inline
# fn __str__(self) -> String: # for print(Value) to work
# return "<Value" + "data: Pointer[Float32] -> " + str(self.data.load()) + "grad: Pointer[Float32] -> " + str(self.grad.load()) + "_op: StringRef -> " + self._op, ">"