Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer[Value] is not working #2

Open
lorenzoliuzzo opened this issue Jan 29, 2024 · 1 comment
Open

Pointer[Value] is not working #2

lorenzoliuzzo opened this issue Jan 29, 2024 · 1 comment

Comments

@lorenzoliuzzo
Copy link

error in mojograd/utils.mojo:9:45: error: 'DynamicVector' parameter #0 has 'CollectionElement' type, but value has type 'Pointer[Value, 0]'

@staticmethodPointer
fn build_topo(inout ptr_v: Pointer[Value], inout visited: DynamicVector[Pointer[Value]], inout topo: DynamicVector[Pointer[Value]]):
    if ptr_v == Pointer[Value].get_null():
        return
    var is_visited: Bool = False
    let size: Int = len(visited)
    for i in range(size):
        if ptr_v == visited[i]:
            is_visited = True
    if not is_visited:
        visited.push_back(ptr_v)
        # Make sure we don't try to access null pointers (e.g. on pow
        # where we don't have the right child)
        if ptr_v.load().l != Pointer[Int].get_null():
            var ptr_l: Pointer[Value] = ptr_v.load().l.bitcast[Value]()
            if ptr_l != Pointer[Value].get_null():
                Value.build_topo(ptr_l, visited, topo)
        if ptr_v.load().r != Pointer[Int].get_null():
            var ptr_r: Pointer[Value] = ptr_v.load().r.bitcast[Value]()
            if ptr_r != Pointer[Value].get_null():
                Value.build_topo(ptr_r, visited, topo)
        topo.push_back(ptr_v)

@always_inline
fn backward(inout self):
    var visited: DynamicVector[Pointer[Value]] = DynamicVector[Pointer[Value]]()
    var topo: DynamicVector[Pointer[Value]] = DynamicVector[Pointer[Value]]()
    var ptr_self: Pointer[Value] = Pointer[Value].alloc(1)
    ptr_self.store(self)
    Value.build_topo(ptr_self, visited, topo)
    self.grad.store(1.0)
    var reversed: DynamicVector[Pointer[Value]] = reverse(topo)
    for i in range(len(reversed)):
        Value._backward(reversed[i])
    visited.clear()
    topo.clear()
    reversed.clear()
    ptr_self.free()
@automata
Copy link
Owner

automata commented Apr 5, 2024

Thanks for reporting. I'm finally getting back to development of mojograd. I hope to get it updated to latest Mojo and will check those issues while doing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants