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

[VM] add removeUnusedFunctions pass in vm memoryopt #8040

Merged
merged 2 commits into from
May 20, 2021
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
3 changes: 3 additions & 0 deletions src/relay/backend/vm/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,9 @@ void VMCompiler::Lower(IRModule mod, const TargetsMap& targets, const tvm::Targe

transform::Sequential MemoryOpt(tvm::Target host_target, TargetsMap targets) {
Array<Pass> pass_seqs;
// Remove unused functions
Array<runtime::String> entry_functions{"main"};
pass_seqs.push_back(transform::RemoveUnusedFunctions(entry_functions));
// Manifest the allocations.
pass_seqs.push_back(transform::ManifestAlloc(host_target, targets));

Expand Down
26 changes: 26 additions & 0 deletions tests/python/relay/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from tvm.contrib import utils
from tvm import rpc
import tvm.testing
from tvm.relay.transform import InferType


def check_result(args, expected_result, mod=None):
Expand Down Expand Up @@ -186,6 +187,31 @@ def test_multiple_ifs():
assert res == [1, 0]


@tvm.testing.uses_gpu
def test_unused_function():
cond = relay.const(True)
mod = tvm.IRModule()
then_name = relay.GlobalVar("times_2")
# define unused function
else_name = relay.GlobalVar("times_3")
t1 = relay.TensorType((2, 2), dtype="float32")
x1 = relay.var("x1", t1, dtype="float32")
x2 = relay.var("x2", t1, dtype="float32")
f2 = relay.multiply(x1, relay.const(2.0))
f3 = relay.multiply(x2, relay.const(3.0))
mod[then_name] = relay.Function([x1], f2)
mod[else_name] = relay.Function([x2], f3)
mod = InferType()(mod)
x3 = relay.var("x3", t1, dtype="float32")
# put unused function in else branch
f = relay.If(cond, then_name(x3), else_name(x3))
mod["main"] = relay.Function([x3], f)
x_data = np.random.rand(2, 2).astype("float32")
y_data = x_data * 2

check_result([x_data], y_data, mod=mod)


@tvm.testing.uses_gpu
def test_simple_call():
mod = tvm.IRModule({})
Expand Down