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

[Relay][VM]Create closure object for GlobalVar #3411

Merged
merged 1 commit into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/tvm/relay/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ def load_prelude(self):
with open(prelude_file) as prelude:
prelude = fromtext(prelude.read())
self.mod.update(prelude)
self.id = self.mod["id"]
self.compose = self.mod["compose"]
self.id = self.mod.get_global_var("id")
self.compose = self.mod.get_global_var("compose")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aah... why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mod["id"] returns Function. But the function name symbols need to be GlobalVar, you can check other prelude functions. Otherwise it would cause errors like https://discuss.tvm.ai/t/relay-to-a-normal-form-fails-on-a-prelude-function/3031.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.



def __init__(self, mod):
Expand Down
8 changes: 6 additions & 2 deletions src/relay/backend/vm/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ struct VMCompiler : ExprFunctor<void(const Expr& expr)> {
}

void VisitExpr_(const GlobalVarNode* gvar) {
// TODO(wweic): Support Load GlobalVar into a register
LOG(FATAL) << "Loading GlobalVar into register is not yet supported";
auto var = GetRef<GlobalVar>(gvar);
auto func = this->context->module->Lookup(var);
auto it = this->context->global_map.find(var);
CHECK(it != this->context->global_map.end());
// Allocate closure with zero free vars
Emit(Instruction::AllocClosure(it->second, 0, {}, NewRegister()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should merge for now, but eta expand these in the future then optimize/lift.

}

void VisitExpr_(const IfNode* if_node) {
Expand Down
41 changes: 41 additions & 0 deletions tests/python/relay/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,47 @@ def test_let_scalar():
result = veval(f, x_data)
tvm.testing.assert_allclose(result.asnumpy(), x_data + 42.0)

def test_compose():
mod = relay.Module()
p = Prelude(mod)

compose = p.compose

# remove all functions to not have pattern match to pass vm compilation
# TODO(wweic): remove the hack and implement pattern match
for v, _ in mod.functions.items():
if v.name_hint == 'compose':
continue
mod[v] = relay.const(0)

# add_one = fun x -> x + 1
sb = relay.ScopeBuilder()
x = relay.var('x', 'float32')
x1 = sb.let('x1', x)
xplusone = x1 + relay.const(1.0, 'float32')
sb.ret(xplusone)
body = sb.get()
add_one = relay.GlobalVar("add_one")
add_one_func = relay.Function([x], body)

# add_two = compose(add_one, add_one)
sb = relay.ScopeBuilder()
y = relay.var('y', 'float32')
add_two_func = sb.let('add_two', compose(add_one_func, add_one_func))
add_two_res = add_two_func(y)
sb.ret(add_two_res)
add_two_body = sb.get()

mod[add_one] = add_one_func

f = relay.Function([y], add_two_body)
mod[mod.entry_func] = f

x_data = np.array(np.random.rand()).astype('float32')
result = veval(mod)(x_data)

tvm.testing.assert_allclose(result.asnumpy(), x_data + 2.0)

def test_closure():
x = relay.var('x', shape=())
y = relay.var('y', shape=())
Expand Down