-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Relay][VM] Add support for references. #6798
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
Changes from all commits
6db7c48
b80b800
6d0f8e8
00e2715
ff1fe47
bb8d403
ab1305f
44d0403
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,8 @@ class Eliminator : private ExprMutator { | |
|
|
||
| Expr VisitExpr_(const LetNode* op) final { | ||
| Var v = op->var; | ||
| if (HasLet(v)) { | ||
| // TODO(@jroesch, @altanh, @M.K.): fix DCE with refs (#6803) | ||
| if (HasLet(v) || op->value.as<RefWriteNode>()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe this fully fixes DCE due to nesting ref_write deep inside lets that get DCEd
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should stop it from removing RefWrites anywhere and by extension keep all ref operations alive as the reference is now live too.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will give you a test case for this since I made this exact change when debugging and it wasn't enough to fix the problem. (In fact I remember segfaulting...)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, out of the scope of this PR, do we need to do some alias analysis to handle reference for DCE?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe so, for now we should probably require DCE to error when the incoming module contains references. @jroesch and I are planning on writing an alias analysis pass soon. |
||
| return Let(v, VisitExpr(op->value), VisitExpr(op->body)); | ||
| } else { | ||
| return VisitExpr(op->body); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,6 +262,12 @@ void VirtualMachine::InvokePacked(Index packed_index, const PackedFunc& func, In | |
| func.CallPacked(TVMArgs(values.data(), codes.data(), arity), &rv); | ||
| } | ||
|
|
||
| std::string DebugPrint(NDArray array) { | ||
| const PackedFunc* fprint = Registry::Get("relay._ndarray_repr"); | ||
| ICHECK(fprint) << "unable to find printing function for constants"; | ||
| return (*fprint)(array); | ||
| } | ||
|
|
||
|
Comment on lines
+265
to
+270
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this part of the ReprPrinter in ndarray.cc? I've been using something very similar in the VM, but I feel like we should make it more general.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree on this |
||
| void VirtualMachine::LoadExecutable(const Executable* exec) { | ||
| ICHECK(exec) << "The executable is not created yet."; | ||
| exec_ = exec; | ||
|
|
@@ -595,6 +601,29 @@ void VirtualMachine::RunLoop() { | |
| pc_++; | ||
| goto main_loop; | ||
| } | ||
| case Opcode::RefRead: { | ||
| ADT ref = Downcast<ADT>(ReadRegister(instr.ref_read.ref)); | ||
| WriteRegister(instr.dst, ref[0]); | ||
| pc_++; | ||
| goto main_loop; | ||
| } | ||
| case Opcode::RefWrite: { | ||
| ADT ref = Downcast<ADT>(ReadRegister(instr.ref_write.ref)); | ||
| ObjectRef value = ReadRegister(instr.ref_write.value); | ||
| // Not sure about this being best way to implement mutable thing. | ||
| ADTObj* mut_array = const_cast<ADTObj*>(ref.as<ADTObj>()); | ||
| ObjectRef& inner_value = (*mut_array)[0]; | ||
| inner_value = value; | ||
| pc_++; | ||
| goto main_loop; | ||
| } | ||
| case Opcode::RefCreate: { | ||
| auto value = {ReadRegister(instr.ref_create.initial_value)}; | ||
| auto ref = ADT::Tuple(value); | ||
| WriteRegister(instr.dst, ref); | ||
| pc_++; | ||
| goto main_loop; | ||
| } | ||
| default: | ||
| LOG(FATAL) << "Unknown instruction opcode: " << int(instr.op); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RefWrite doesn't have last register, right? Maybe set the
last_register_to -1?