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

[RFC][Relay] Reference(Mutation). #2483

Closed
MarisaKirisame opened this issue Jan 22, 2019 · 3 comments
Closed

[RFC][Relay] Reference(Mutation). #2483

MarisaKirisame opened this issue Jan 22, 2019 · 3 comments

Comments

@MarisaKirisame
Copy link
Contributor

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 of a.
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, where Ref<(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:

a : Ref<Int> = MakeRef 1
b : Ref<Int> = a # a and b will *always* be aligned. there are no way to make them point to different box.
assert (ReadRef(b) == 1)
WriteRef(a, 2)
assert (ReadRef(b) == 2)

In relay, we will provide special syntax like :=, ! to make using reference easier.

@ZihengJiang
Copy link
Contributor

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>)

Why the Ref<Int> in Ref<Ref<Int>> is mutable while "The value in the box is immutable"?

@MarisaKirisame
Copy link
Contributor Author

MarisaKirisame commented Feb 1, 2019

@ZihengJiang this is like a const pointer to a pointer to a const object.
the Ref is still immutable per se, but Ref<Ref> has the effect of mimicing a pointer to an int, in which both the pointer and the int is mutable. The pointer mutability is provided by calling WriteRef on Ref<Ref>, and the int mutability is provided by calling WriteRef on Ref

@tqchen tqchen closed this as completed Feb 18, 2019
@tqchen
Copy link
Member

tqchen commented Feb 18, 2019

closed by #2489

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

3 participants