Skip to content

Commit c5911a6

Browse files
authored
[ETHOSN] Add support for experimental compiler option (#13410)
* [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.
1 parent 06be0b3 commit c5911a6

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/relay/backend/contrib/ethosn/codegen.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,17 @@ runtime::ethosn::OrderedCompiledNetwork EthosnCompiler::CompileEthosnFunc(const
713713
auto network_with_ids = ConstructNetwork(mod, gvar, func);
714714
// Now set the required build flags
715715
sl::CompilationOptions options = CreateOptions();
716-
// Finally compile the network
716+
// Set the experimental compiler if enabled, for now this is not part of the
717+
// support library compilation options.
718+
bool experimental_compiler = GetCompilerAttrs()->experimental_compiler;
719+
if (experimental_compiler) {
720+
setenv("FORCE_EXPERIMENTAL_COMPILER", "1", 1);
721+
}
717722
std::vector<std::unique_ptr<sl::CompiledNetwork>> compiled_networks =
718723
sl::Compile(*network_with_ids.network, options);
724+
if (experimental_compiler) {
725+
unsetenv("FORCE_EXPERIMENTAL_COMPILER");
726+
}
719727
ICHECK_GE(compiled_networks.size(), 1) << "Ethos-N compiler failed to compile network";
720728
auto compiled_network = std::move(compiled_networks[0]);
721729
// Determine the order that the inputs/outputs are in and how that corresponds to the

src/relay/backend/contrib/ethosn/codegen_ethosn.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ struct EthosnCompilerConfigNode : public tvm::AttrsNode<EthosnCompilerConfigNode
252252
bool disable_winograd;
253253
String debug_dir;
254254
bool inline_non_compute_intensive_partitions;
255+
bool experimental_compiler;
255256

256257
TVM_DECLARE_ATTRS(EthosnCompilerConfigNode, "ext.attrs.EthosnCompilerConfigNode") {
257258
TVM_ATTR_FIELD(variant).describe("See Ethos-N documentation.").set_default("n78");
@@ -285,6 +286,9 @@ struct EthosnCompilerConfigNode : public tvm::AttrsNode<EthosnCompilerConfigNode
285286
"Ethos(TM)-N that are deemed 'non-compute-intensive'. The inlined functions will "
286287
"continue through TVM's standard compilation flow.")
287288
.set_default(true);
289+
TVM_ATTR_FIELD(experimental_compiler)
290+
.describe("An exprimental cascading compiler for Arm(R) Ethos(TM)-N.")
291+
.set_default(false);
288292
}
289293
};
290294

tests/python/contrib/test_ethosn/test_codegen.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,57 @@ def test_compile_with_unsupported_variant():
5050

5151
with pytest.raises(tvm.TVMError, match=r"Unknown NPU type"):
5252
tei.build_and_run(mod, inputs, 1, {}, True, additional_config_args=additional_config_args)
53+
54+
55+
@requires_ethosn
56+
def test_experimental_compiler(capfd):
57+
"""Test compilation with the experimental compiler."""
58+
dtype = "int8"
59+
input_shape = (1, 2, 2, 2)
60+
61+
x = relay.var("x", shape=input_shape, dtype=dtype)
62+
y = relay.reshape(x, newshape=(1, 1, 1, 8))
63+
mod = tei.make_ethosn_partition(y)
64+
65+
additional_config_args = {
66+
"variant": "n78",
67+
"experimental_compiler": True,
68+
"inline_non_compute_intensive_partitions": False,
69+
}
70+
71+
tei.build(mod, {}, True, additional_config_args=additional_config_args)
72+
73+
# Check for hints that the experimental compiler was activated.
74+
# The support library logs a warning to say the the experimental
75+
# compiler is in use. Check that this warning was logged.
76+
captured = capfd.readouterr()
77+
assert (
78+
"WARNING: Experimental Compiler in use." in captured.err
79+
), "Experimental compiler was not activated."
80+
81+
82+
@requires_ethosn
83+
def test_without_experimental_compiler(capfd):
84+
"""Test compilation when the experimental compiler is not enabled."""
85+
dtype = "int8"
86+
input_shape = (1, 2, 2, 2)
87+
88+
x = relay.var("x", shape=input_shape, dtype=dtype)
89+
y = relay.reshape(x, newshape=(1, 1, 1, 8))
90+
mod = tei.make_ethosn_partition(y)
91+
92+
additional_config_args = {
93+
"variant": "n78",
94+
"experimental_compiler": False,
95+
"inline_non_compute_intensive_partitions": False,
96+
}
97+
98+
tei.build(mod, {}, True, additional_config_args=additional_config_args)
99+
100+
# Check for hints that the experimental compiler was activated.
101+
# The support library logs a warning to say the the experimental
102+
# compiler is in use. Check that this warning was logged.
103+
captured = capfd.readouterr()
104+
assert (
105+
"WARNING: Experimental Compiler in use." not in captured.err
106+
), "Experimental compiler was enabled when it is not expected to be."

0 commit comments

Comments
 (0)