From ed2298adfd4a117526a3a3af0e23f6d88f869a50 Mon Sep 17 00:00:00 2001 From: Luke Hutton Date: Tue, 15 Nov 2022 09:34:16 +0000 Subject: [PATCH 1/2] [ETHOSN] Add support for experimental compiler option The support library currently supports enabling the experimental cascading compiler option via an environment variable `FORCE_EXPERIMENTAL_COMPILER`. This commit exposes the ability to enable this option through TVMC. Change-Id: Ie5667a300f35f99bc8f92d780a56894ef9bbe3ad --- python/tvm/relay/op/contrib/ethosn.py | 12 +++++++++ src/relay/backend/contrib/ethosn/codegen.cc | 10 ++++++- .../backend/contrib/ethosn/codegen_ethosn.h | 4 +++ .../contrib/test_ethosn/test_codegen.py | 27 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/op/contrib/ethosn.py b/python/tvm/relay/op/contrib/ethosn.py index e28eea9d224f..795525a0da55 100644 --- a/python/tvm/relay/op/contrib/ethosn.py +++ b/python/tvm/relay/op/contrib/ethosn.py @@ -101,6 +101,18 @@ def is_inline_non_compute_intensive_partitions_enabled() -> bool: return compiler_attrs.inline_non_compute_intensive_partitions +def is_experimental_compiler_enabled() -> bool: + """ + Determine whether the experimental compiler option is enabled. + + Returns + ------- + True if the experimental compiler is enabled, False if not. + """ + compiler_attrs = tvm.get_global_func("relay.ext.ethos-n.get_compiler_attrs")() + return compiler_attrs.experimental_compiler + + def partition_for_ethosn(mod, params=None, **opts): """Partition the graph greedily offloading supported operators to Arm Ethos-N NPU. diff --git a/src/relay/backend/contrib/ethosn/codegen.cc b/src/relay/backend/contrib/ethosn/codegen.cc index edf7caca820d..0fed73d2a35e 100644 --- a/src/relay/backend/contrib/ethosn/codegen.cc +++ b/src/relay/backend/contrib/ethosn/codegen.cc @@ -713,9 +713,17 @@ runtime::ethosn::OrderedCompiledNetwork EthosnCompiler::CompileEthosnFunc(const auto network_with_ids = ConstructNetwork(mod, gvar, func); // Now set the required build flags sl::CompilationOptions options = CreateOptions(); - // Finally compile the network + // Set the experimental compiler if enabled, for now this is not part of the + // support library compilation options. + bool experimental_compiler = GetCompilerAttrs()->experimental_compiler; + if (experimental_compiler) { + setenv("FORCE_EXPERIMENTAL_COMPILER", "1", 1); + } std::vector> compiled_networks = sl::Compile(*network_with_ids.network, options); + if (experimental_compiler) { + unsetenv("FORCE_EXPERIMENTAL_COMPILER"); + } ICHECK_GE(compiled_networks.size(), 1) << "Ethos-N compiler failed to compile network"; auto compiled_network = std::move(compiled_networks[0]); // Determine the order that the inputs/outputs are in and how that corresponds to the diff --git a/src/relay/backend/contrib/ethosn/codegen_ethosn.h b/src/relay/backend/contrib/ethosn/codegen_ethosn.h index 7c52da713c5c..118292b45f84 100644 --- a/src/relay/backend/contrib/ethosn/codegen_ethosn.h +++ b/src/relay/backend/contrib/ethosn/codegen_ethosn.h @@ -252,6 +252,7 @@ struct EthosnCompilerConfigNode : public tvm::AttrsNode Date: Tue, 22 Nov 2022 17:40:22 +0000 Subject: [PATCH 2/2] Address comments * Remove unused code * Add negative test case Change-Id: I5e12d070554954e320b4d15c02d5e2ada179fce5 --- python/tvm/relay/op/contrib/ethosn.py | 12 --------- .../contrib/test_ethosn/test_codegen.py | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/python/tvm/relay/op/contrib/ethosn.py b/python/tvm/relay/op/contrib/ethosn.py index 795525a0da55..e28eea9d224f 100644 --- a/python/tvm/relay/op/contrib/ethosn.py +++ b/python/tvm/relay/op/contrib/ethosn.py @@ -101,18 +101,6 @@ def is_inline_non_compute_intensive_partitions_enabled() -> bool: return compiler_attrs.inline_non_compute_intensive_partitions -def is_experimental_compiler_enabled() -> bool: - """ - Determine whether the experimental compiler option is enabled. - - Returns - ------- - True if the experimental compiler is enabled, False if not. - """ - compiler_attrs = tvm.get_global_func("relay.ext.ethos-n.get_compiler_attrs")() - return compiler_attrs.experimental_compiler - - def partition_for_ethosn(mod, params=None, **opts): """Partition the graph greedily offloading supported operators to Arm Ethos-N NPU. diff --git a/tests/python/contrib/test_ethosn/test_codegen.py b/tests/python/contrib/test_ethosn/test_codegen.py index a43fde7f49a6..4a40d062af5c 100644 --- a/tests/python/contrib/test_ethosn/test_codegen.py +++ b/tests/python/contrib/test_ethosn/test_codegen.py @@ -77,3 +77,30 @@ def test_experimental_compiler(capfd): assert ( "WARNING: Experimental Compiler in use." in captured.err ), "Experimental compiler was not activated." + + +@requires_ethosn +def test_without_experimental_compiler(capfd): + """Test compilation when the experimental compiler is not enabled.""" + dtype = "int8" + input_shape = (1, 2, 2, 2) + + x = relay.var("x", shape=input_shape, dtype=dtype) + y = relay.reshape(x, newshape=(1, 1, 1, 8)) + mod = tei.make_ethosn_partition(y) + + additional_config_args = { + "variant": "n78", + "experimental_compiler": False, + "inline_non_compute_intensive_partitions": False, + } + + tei.build(mod, {}, True, additional_config_args=additional_config_args) + + # Check for hints that the experimental compiler was activated. + # The support library logs a warning to say the the experimental + # compiler is in use. Check that this warning was logged. + captured = capfd.readouterr() + assert ( + "WARNING: Experimental Compiler in use." not in captured.err + ), "Experimental compiler was enabled when it is not expected to be."