Replies: 2 comments 7 replies
-
The solution with Pointer should work, but you'll need to be /extremely/ careful with them - in the absence of lifetimes, it is super easy to introduce dangling pointers etc. You'll also have to manually implement the copy/move ctors. btw, I'm fond of mojician 🪄 |
Beta Was this translation helpful? Give feedback.
-
I'm trying to port micrograd to Mojo while learning the language, and facing exactly the same problem. The following snippet crashes the kernel: from String import String
from BuiltinList import FloatLiteral
struct Value:
var data: Int
var grad: FloatLiteral
var _prev_a: Self
var _prev_b: Self
var _op: String
fn __init__(inout self, data: Int, _child_a: Self, _child_b: Self, _op: String):
self.data = data
self.grad = 0.0
self._prev_a = _child_a
self._prev_b = _child_b
self._op = _op
fn __copyinit__(inout self, other: Self):
self.data = other.data
self.grad = other.grad
self._prev_a = other._prev_a
self._prev_b = other._prev_b
self._op = other._op
fn __add__(self, owned other: Self) -> Self:
let out: Value = Value(self.data + other.data, self, other, '+')
return out
fn _backward_add(inout self, inout other: Self):
self._prev_a.grad += self.grad
self._prev_b.grad += self.grad I was trying to isolate the problem while following your suggestion to use var a: Pointer[Int]
a = Pointer[Int].alloc(1)
a.store(0, 42)
print(a.load(0)) I'm not able to do the same with: struct Node:
var left: Pointer[Self]
var right: Pointer[Self]
fn __init__(inout self):
self.left = Pointer[Self].alloc(1)
self.right = Pointer[Self].alloc(1) Getting the following error as result:
I'm pretty new to Mojo and MLIR, so there's a big chance that I'm using the pointers in the wrong way 😅 Any suggestion is really welcomed! |
Beta Was this translation helpful? Give feedback.
-
Hey fellow
MojoviansMojiciansI'm trying to implement backpopogation for a neural net and need to represent a computational graph somehow and the easiest way to do it is through some sort of tree.
Now I can't add a
@value
decorator because Node is a non-copyable and non-movable type. And if I create a construtor I wouldn't know what to setleft
orright
as because we don't have null values yet.And the kernel crashes when I run
I can't go with an adjacency matrix either because we don't have Dict types yet, it feels like I've hit a roadblock. Does anyone have ideas/workarounds for storing graph/tree like data structures?
The above snippet is a reduced version, this is what my
Tensor
struct looks likeWhen Tensors are initialised I only want shape and data to be defined but not the children,
and when I call
let t3 = t1.add(t2)
I would like theadd
function to sett3.child1
ast1
, andt3.child2
ast2
Beta Was this translation helpful? Give feedback.
All reactions