-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[RFC][Relay] Reference(Mutation). #2483
Comments
Why the |
@ZihengJiang this is like a const pointer to a pointer to a const object. |
closed by #2489 |
This RFC aim to add ML style Reference into Relay, which is crucial for modeling traditional imperative algorithm, and for higher order reverse mode automatic differentiation (backpropagation require using reference to store the gradient).
Informally speaking, a
Ref(erence)<a>
point to a box, which hold a value ofa
.We can fetch value from the box, or update a new value into the box, forgetting the old one.
It is crucial to note that this 'update' is the only source of mutability in relay. The value in the box is immutable, and the reference itself is immutable (cannot point to new box). If you want the reference to be mutable, you need a reference of reference(
Ref<Ref<Int>>
). If you want the value to be mutable, you need to sprinkle reference inside the value((Ref<Int>, Ref<Int>)
for a tuple of reference, whereRef<(Int, Int)>
is a reference of tuple - the tuple itself is still immutable).More formally speaking, there are 3 function that come with reference:
MakeRef : a -> Ref<a>
, construct a new box, returning a reference pointing to it.ReadRef : Ref<a> -> a
, look at the input Reference, and return the value in the box.WriteRef : (Ref<a>, a) -> ()
, put the new a in the box pointed to by a reference.to make their semantic clearer, here are some example code:
In relay, we will provide special syntax like :=, ! to make using reference easier.
The text was updated successfully, but these errors were encountered: