Skip to content

Commit

Permalink
[ETHOSN] Add support for 20.11 Ethos-N driver stack release (apache#7506
Browse files Browse the repository at this point in the history
)

- Updated ethosn relay backend to support 20.11 api changes.
 - Removed legacy support for 20.05.
 - Added a mechanism to specify the ethosn driver stack version.
  • Loading branch information
tristan-arm authored and trevor-m committed May 11, 2021
1 parent 7651b3b commit e0d30f3
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 148 deletions.
3 changes: 2 additions & 1 deletion cmake/utils/FindEthosN.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ macro(find_ethosn use_ethosn)
find_library(ETHOSN_COMPILER_LIBRARY NAMES EthosNSupport)

set(ETHOSN_PACKAGE_VERSION "0.1.1")
set(ETHOSN_DEFINITIONS -DETHOSN_API_VERSION=${USE_ETHOSN_API_VERSION})

if(${USE_ETHOSN_HW} MATCHES ${IS_TRUE_PATTERN})
# Runtime hardware support
Expand All @@ -70,7 +71,7 @@ macro(find_ethosn use_ethosn)
find_library(ETHOSN_RUNTIME_LIBRARY NAMES EthosNDriver
PATHS ${__ethosn_stack}/lib)
find_library(ETHOSN_RUNTIME_LIBRARY NAMES EthosNDriver)
set(ETHOSN_DEFINITIONS -DETHOSN_HW)
set(ETHOSN_DEFINITIONS -DETHOSN_HW -DETHOSN_API_VERSION=${USE_ETHOSN_API_VERSION})
endif ()

if(ETHOSN_COMPILER_LIBRARY)
Expand Down
28 changes: 19 additions & 9 deletions src/relay/backend/contrib/ethosn/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace ethosn {
* variant[2] - Ethos-N37
* variant[3] - Ethos-N78
*/
#if _ETHOSN_API_VERSION_ == 2008
#if _ETHOSN_API_VERSION_ == 2011
static std::vector<char> variants[4] = {
{
0x03, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -84,40 +84,50 @@ static std::vector<char> variants[4] = {
0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
}};
#else
static std::vector<char> variants[3] = {
static std::vector<char> variants[4] = {
{
0x02, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
{
0x02, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
{
0x02, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
{
0x03, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x02, 0x00,
0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
}};
#endif
} // namespace ethosn
Expand Down
192 changes: 187 additions & 5 deletions src/relay/backend/contrib/ethosn/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,19 @@ sl::TensorsAndId MakeOps(const sl::TensorAndId<sl::Operand>& op) {

NetworkWithIDs ConstructNetworkVisitor::Construct(const Function& func) {
// Initialise everything
#if _ETHOSN_API_VERSION_ == 2011
auto ctx = transform::PassContext::Current();
auto cfg = ctx->GetConfig<EthosnCompilerConfig>("relay.ext.ethos-n.options");
if (!cfg.defined()) {
cfg = AttrsWithDefaultValues<EthosnCompilerConfig>();
}
#endif
NetworkWithIDs network_with_ids;
#if _ETHOSN_API_VERSION_ == 2011
network_ = sl::CreateNetwork(variants[cfg.value()->variant]);
#else
network_ = sl::CreateNetwork();
#endif
network_with_ids.network = network_;
operand_table_.clear();

Expand Down Expand Up @@ -561,7 +572,11 @@ sl::CompilationOptions EthosnCompiler::CreateOptions() {
cfg = AttrsWithDefaultValues<EthosnCompilerConfig>();
}

#if _ETHOSN_API_VERSION_ == 2011
sl::CompilationOptions options;
#else
sl::CompilationOptions options(variants[cfg.value()->variant]);
#endif
options.m_Strategy0 = cfg.value()->strategy0;
options.m_Strategy1 = cfg.value()->strategy1;
options.m_Strategy3 = cfg.value()->strategy3;
Expand All @@ -575,15 +590,13 @@ sl::CompilationOptions EthosnCompiler::CreateOptions() {
options.m_BlockConfig8x32 = cfg.value()->block_config_8x32;
options.m_BlockConfig8x8 = cfg.value()->block_config_8x8;
options.m_EnableIntermediateCompression = cfg.value()->enable_intermediate_compression;
options.m_DisableWinograd = cfg.value()->disable_winograd;
#if _ETHOSN_API_VERSION_ == 2008
options.m_DebugInfo.m_DumpDebugFiles = cfg.value()->dump_debug_files;
#endif
options.m_DisableWinograd = cfg.value()->disable_winograd;
options.m_DebugInfo.m_DebugDir = cfg.value()->debug_dir;
#if _ETHOSN_API_VERSION_ == 2008
options.m_CompilerAlgorithm =
sl::EthosNCompilerAlgorithmFromString(cfg.value()->compiler_algorithm.c_str());
#else
options.m_EnableCascading = cfg.value()->enable_cascading;
#endif
return options;
}

Expand All @@ -606,6 +619,175 @@ std::pair<std::vector<uint32_t>, std::vector<uint32_t>> EthosnCompiler::GetInput
return std::make_pair(input_order, output_order);
}

#if _ETHOSN_API_VERSION_ == 2011
auto ctx = transform::PassContext::Current();
auto cfg = ctx -> GetConfig<EthosnCompilerConfig>("relay.ext.ethos-n.options").defined()
? ctx -> GetConfig<EthosnCompilerConfig>("relay.ext.ethos-n.options")
: AttrsWithDefaultValues<EthosnCompilerConfig>();
auto m_Queries = sl::SupportQueries(variants[cfg.value()->variant]);
#endif

TVM_REGISTER_GLOBAL("relay.ethos-n.support.conv2d")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
ConvolutionParams params;
auto err = EthosnAPI::QnnConv2d(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
if (params.is_depthwise) {
*rv = !err &&
m_Queries.IsDepthwiseConvolutionSupported(params.bias_info, params.weights_info,
params.conv_info, params.activation_info);
} else {
*rv = !err && m_Queries.IsConvolutionSupported(params.bias_info, params.weights_info,
params.conv_info, params.activation_info);
}
#else
if (params.is_depthwise) {
*rv = !err && sl::IsDepthwiseConvolutionSupported(params.bias_info, params.weights_info,
params.conv_info, params.activation_info);
} else {
*rv = !err && sl::IsConvolutionSupported(params.bias_info, params.weights_info,
params.conv_info, params.activation_info);
}
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.fc")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
FullyConnectedParams params;
auto err = EthosnAPI::QnnFullyConnected(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsFullyConnectedSupported(params.bias_info, params.weights_info,
params.fc_info, params.input_info);
#else
*rv = !err && sl::IsFullyConnectedSupported(params.bias_info, params.weights_info,
params.fc_info, params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.max_pool2d")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
MaxPool2DParams params;
auto err = EthosnAPI::MaxPool2D(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsPoolingSupported(params.pool_info, params.input_info);
#else
*rv = !err && sl::IsPoolingSupported(params.pool_info, params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.avg_pool2d")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
AvgPool2DParams params;
auto err = EthosnAPI::AvgPool2D(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsPoolingSupported(params.pool_info, params.input_info);
#else
*rv = !err && sl::IsPoolingSupported(params.pool_info, params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.reshape")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
ReshapeParams params;
auto err = EthosnAPI::Reshape(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsReshapeSupported(params.new_shape, params.input_info);
#else
*rv = !err && sl::IsReshapeSupported(params.new_shape, params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.addition")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
AdditionParams params;
auto err = EthosnAPI::Addition(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsAdditionSupported(params.lhs_info, params.rhs_info,
params.output_quantization_info);
#else
*rv = !err && sl::IsAdditionSupported(params.lhs_info, params.rhs_info,
params.output_quantization_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.sigmoid")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
SigmoidParams params;
auto err = EthosnAPI::Sigmoid(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsSigmoidSupported(params.input_info);
#else
*rv = !err && sl::IsSigmoidSupported(params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.concatenate")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
ConcatenateParams params;
auto err = EthosnAPI::Concatenate(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsConcatenationSupported(params.input_infos, params.concat_info);
#else
*rv = !err && sl::IsConcatenationSupported(params.input_infos, params.concat_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.split")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
SplitParams params;
auto err = EthosnAPI::Split(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsSplitSupported(params.input_info, params.split_info);
#else
*rv = !err && sl::IsSplitSupported(params.input_info, params.split_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.depth_to_space")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
DepthToSpaceParams params;
auto err = EthosnAPI::DepthToSpace(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsDepthToSpaceSupported(params.input_info, params.depth_info);
#else
*rv = !err && sl::IsDepthToSpaceSupported(params.input_info, params.depth_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.support.relu")
.set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Call call = args[0];
ReluParams params;
auto err = EthosnAPI::Relu(call, &params);
#if _ETHOSN_API_VERSION_ == 2011
*rv = !err && m_Queries.IsReluSupported(params.relu_info, params.input_info);
#else
*rv = !err && sl::IsReluSupported(params.relu_info, params.input_info);
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.query").set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
#if defined ETHOSN_HW
*rv = true;
#else
*rv = false;
#endif
});

TVM_REGISTER_GLOBAL("relay.ethos-n.api.version").set_body_typed([]() -> int {
return _ETHOSN_API_VERSION_;
});

} // namespace ethosn
} // namespace contrib
} // namespace relay
Expand Down
16 changes: 6 additions & 10 deletions src/relay/backend/contrib/ethosn/codegen_ethosn.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,12 @@ struct EthosnCompilerConfigNode : public tvm::AttrsNode<EthosnCompilerConfigNode
bool block_config_8x32;
bool block_config_8x8;
bool enable_intermediate_compression;
bool disable_winograd;
#if _ETHOSN_API_VERSION_ == 2008
bool dump_debug_files;
#endif
bool disable_winograd;
String debug_dir;
#if _ETHOSN_API_VERSION_ == 2008
String compiler_algorithm;
#else
bool enable_cascading;
#endif

TVM_DECLARE_ATTRS(EthosnCompilerConfigNode, "ext.attrs.EthosnCompilerConfigNode") {
TVM_ATTR_FIELD(variant)
Expand All @@ -268,14 +266,12 @@ struct EthosnCompilerConfigNode : public tvm::AttrsNode<EthosnCompilerConfigNode
TVM_ATTR_FIELD(block_config_8x32).set_default(true);
TVM_ATTR_FIELD(block_config_8x8).set_default(true);
TVM_ATTR_FIELD(enable_intermediate_compression).set_default(true);
TVM_ATTR_FIELD(disable_winograd).set_default(false);
#if _ETHOSN_API_VERSION_ == 2008
TVM_ATTR_FIELD(dump_debug_files).set_default(false);
#endif
TVM_ATTR_FIELD(disable_winograd).set_default(false);
TVM_ATTR_FIELD(debug_dir).set_default(".");
#if _ETHOSN_API_VERSION_ == 2008
TVM_ATTR_FIELD(compiler_algorithm).set_default("NonCascadingOnly");
#else
TVM_ATTR_FIELD(enable_cascading).set_default(false);
#endif
}
};

Expand Down
Loading

0 comments on commit e0d30f3

Please sign in to comment.