Skip to content

Commit

Permalink
Address Chris comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdadh committed May 12, 2022
1 parent 0c6d8fd commit 8cfb2f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
16 changes: 10 additions & 6 deletions python/tvm/testing/usmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import tvm


def check_for_no_tvm_backendallocworkspace_calls(mod: tvm.runtime.module):
"""This checker checks whether any c-source produced has TVMBackendAllocWorkspace calls.
If USMP is invoked, none of them should have TVMBAW calls"""
def is_tvm_backendallocworkspace_calls(mod: tvm.runtime.module) -> bool:
"""TVMBackendAllocWorkspace call check.
This checker checks whether any c-source produced has TVMBackendAllocWorkspace calls.
If USMP is invoked, none of them should have TVMBAW calls
"""
dso_modules = mod._collect_dso_modules()
for dso_mod in dso_modules:
if dso_mod.type_key not in ["c", "llvm"]:
Expand All @@ -30,6 +33,7 @@ def check_for_no_tvm_backendallocworkspace_calls(mod: tvm.runtime.module):
), 'Current AoT codegen flow should only produce type "c" or "llvm" runtime modules'

source = dso_mod.get_source()
assert (
source.count("TVMBackendAllocWorkspace") == 0
), "This is failing because USMP was unable to plan for every tir.allocate node"
if source.count("TVMBackendAllocWorkspace") != 0:
return False

return True
10 changes: 6 additions & 4 deletions tests/python/contrib/test_hexagon/test_usmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
from tvm import relay
from tvm.relay.backend import Executor, Runtime
from tvm.contrib.hexagon.session import Session
from tvm.testing.usmp import check_for_no_tvm_backendallocworkspace_calls
from tvm.testing.usmp import is_tvm_backendallocworkspace_calls

from .conftest import requires_hexagon_toolchain

usmp_enabled = tvm.testing.parameter(False, True)


@requires_hexagon_toolchain
def test_conv2d(hexagon_session: Session, aot_host_target, aot_target):
def test_conv2d(hexagon_session: Session, aot_host_target, aot_target, usmp_enabled):
dtype = "float32"
input_shape = (1, 8, 8, 3)
w1_shape = (5, 5, 3, 1)
Expand Down Expand Up @@ -73,7 +75,7 @@ def test_conv2d(hexagon_session: Session, aot_host_target, aot_target):
params = {"weight1": weight1_data, "weight2": weight2_data}
inputs = {"data": input_data}

with tvm.transform.PassContext(opt_level=3, config={"tir.usmp.enable": True}):
with tvm.transform.PassContext(opt_level=3, config={"tir.usmp.enable": usmp_enabled}):
lowered = tvm.relay.build(
relay_mod,
params=params,
Expand All @@ -82,7 +84,7 @@ def test_conv2d(hexagon_session: Session, aot_host_target, aot_target):
executor=Executor("aot", {"unpacked-api": False, "interface-api": "packed"}),
)

check_for_no_tvm_backendallocworkspace_calls(lowered.lib)
assert is_tvm_backendallocworkspace_calls(lowered.lib) == usmp_enabled

aot_mod = hexagon_session.get_executor_from_factory(lowered)
aot_mod.set_input(**inputs)
Expand Down
24 changes: 15 additions & 9 deletions tests/python/relay/aot/test_crt_aot_usmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@
run_and_check,
create_relay_module_and_inputs_from_tflite_file,
)
from tvm.testing.usmp import check_for_no_tvm_backendallocworkspace_calls
from tvm.testing.usmp import is_tvm_backendallocworkspace_calls


def _check_for_no_tvm_backendallocworkspace_calls(mod: tvm.runtime.module):
assert is_tvm_backendallocworkspace_calls(
mod
), "This is failing because USMP was unable to plan for every tir.allocate node."


@pytest.mark.parametrize(
Expand Down Expand Up @@ -125,7 +131,7 @@ def test_conv2d(interface_api, use_unpacked_api, test_runner, groups, weight_sha
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -184,7 +190,7 @@ def test_byoc_microtvm(merge_compiler_regions):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -238,7 +244,7 @@ def test_tflite_model_u1_usecase(model_url, usmp_algo, workspace_size):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

# Checking the workspace size reported in model library format
mlf_memory_map = mlf._build_function_memory_map(
Expand Down Expand Up @@ -317,7 +323,7 @@ def test_tflite_model_u3_usecase_single_external_pool(model_url, usmp_algo):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -377,7 +383,7 @@ def test_tflite_model_u3_usecase_two_external_pools(model_url, usmp_algo):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -445,7 +451,7 @@ def test_tflite_model_u2_usecase_two_models_with_a_single_external_pool(model_ur
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -513,7 +519,7 @@ def test_tflite_model_u4_usecase_single_external_pool(model_url, usmp_algo):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down Expand Up @@ -589,7 +595,7 @@ def test_tflite_model_u4_usecase_two_external_pools(model_url, usmp_algo):
)

for compiled_model in compiled_test_mods:
check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)
_check_for_no_tvm_backendallocworkspace_calls(compiled_model.executor_factory.lib)

run_and_check(
models=compiled_test_mods,
Expand Down

0 comments on commit 8cfb2f9

Please sign in to comment.