Skip to content

Commit

Permalink
[ETHOSN] Match config for is-supported with compilation target (#9160)
Browse files Browse the repository at this point in the history
The Ethos-N variant configuration for the is-supported functionality is now
the same as the variant configuration for the actual compilation
  • Loading branch information
leo-blonk authored Oct 22, 2021
1 parent d11bdcd commit edda830
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
73 changes: 51 additions & 22 deletions src/relay/backend/contrib/ethosn/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,25 +606,37 @@ std::pair<std::vector<uint32_t>, std::vector<uint32_t>> EthosnCompiler::GetInput
return std::make_pair(input_order, output_order);
}

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(sl::GetFwAndHwCapabilities(
sl::EthosNVariantFromString(cfg.value()->variant.c_str()), cfg.value()->sram_size_bytes));
std::unique_ptr<sl::SupportQueries> EthosnCompiler::m_Queries;

EthosnError EthosnCompiler::SupportedSetup() {
if (m_Queries == nullptr) {
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>();
m_Queries = std::make_unique<sl::SupportQueries>(sl::GetFwAndHwCapabilities(
sl::EthosNVariantFromString(cfg.value()->variant.c_str()), cfg.value()->sram_size_bytes));
if (m_Queries == nullptr) {
return EthosnError("Could not initialise Ethos-N compiler isSupported");
}
}
return EthosnError();
}

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);
err += EthosnCompiler::SupportedSetup();
if (params.is_depthwise) {
*rv = !err &&
m_Queries.IsDepthwiseConvolutionSupported(params.bias_info, params.weights_info,
params.conv_info, params.activation_info);
EthosnCompiler::GetSupported()->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);
*rv = !err &&
EthosnCompiler::GetSupported()->IsConvolutionSupported(
params.bias_info, params.weights_info, params.conv_info, params.activation_info);
}
});

Expand All @@ -633,81 +645,98 @@ TVM_REGISTER_GLOBAL("relay.ethos-n.support.fc")
Call call = args[0];
FullyConnectedParams params;
auto err = EthosnAPI::QnnFullyConnected(call, &params);
*rv = !err && m_Queries.IsFullyConnectedSupported(params.bias_info, params.weights_info,
params.fc_info, params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err && EthosnCompiler::GetSupported()->IsFullyConnectedSupported(
params.bias_info, params.weights_info, params.fc_info, params.input_info);
});

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);
*rv = !err && m_Queries.IsPoolingSupported(params.pool_info, params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err &&
EthosnCompiler::GetSupported()->IsPoolingSupported(params.pool_info, params.input_info);
});

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);
*rv = !err && m_Queries.IsPoolingSupported(params.pool_info, params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err &&
EthosnCompiler::GetSupported()->IsPoolingSupported(params.pool_info, params.input_info);
});

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);
*rv = !err && m_Queries.IsReshapeSupported(params.new_shape, params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err &&
EthosnCompiler::GetSupported()->IsReshapeSupported(params.new_shape, params.input_info);
});

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);
*rv = !err && m_Queries.IsAdditionSupported(params.lhs_info, params.rhs_info,
params.output_quantization_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err && EthosnCompiler::GetSupported()->IsAdditionSupported(
params.lhs_info, params.rhs_info, params.output_quantization_info);
});

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);
*rv = !err && m_Queries.IsSigmoidSupported(params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err && EthosnCompiler::GetSupported()->IsSigmoidSupported(params.input_info);
});

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);
*rv = !err && m_Queries.IsConcatenationSupported(params.input_infos, params.concat_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err && EthosnCompiler::GetSupported()->IsConcatenationSupported(params.input_infos,
params.concat_info);
});

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);
*rv = !err && m_Queries.IsSplitSupported(params.input_info, params.split_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err &&
EthosnCompiler::GetSupported()->IsSplitSupported(params.input_info, params.split_info);
});

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);
*rv = !err && m_Queries.IsDepthToSpaceSupported(params.input_info, params.depth_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err && EthosnCompiler::GetSupported()->IsDepthToSpaceSupported(params.input_info,
params.depth_info);
});

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);
*rv = !err && m_Queries.IsReluSupported(params.relu_info, params.input_info);
err += EthosnCompiler::SupportedSetup();
*rv = !err &&
EthosnCompiler::GetSupported()->IsReluSupported(params.relu_info, params.input_info);
});

TVM_REGISTER_GLOBAL("relay.ethos-n.query").set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
Expand Down
18 changes: 18 additions & 0 deletions src/relay/backend/contrib/ethosn/codegen_ethosn.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ class EthosnCompiler {
*/
static runtime::Module CreateRuntimeModule(const ObjectRef& ref);

/*!
* \brief Initialise the is-supported functionality of the Ethos-N support library
* with the target variant.
* \return Error object
*/
static EthosnError SupportedSetup();

/*!
* \brief Return the is-supported API of the Support Library
* \return A reference to the API.
*/
static std::unique_ptr<sl::SupportQueries>& GetSupported() {
ICHECK(m_Queries != nullptr);
return m_Queries;
}

private:
/*!
* \brief Compile a single Relay Ethos-N function into an ordered compiled network.
Expand Down Expand Up @@ -322,6 +338,8 @@ class EthosnCompiler {
*/
static std::pair<std::vector<uint32_t>, std::vector<uint32_t>> GetInputOutputOrder(
NetworkWithIDs network, const std::unique_ptr<sl::CompiledNetwork>& compiled_network);

static std::unique_ptr<sl::SupportQueries> m_Queries;
};

runtime::Module CompileEthosn(const ObjectRef& ref) {
Expand Down

0 comments on commit edda830

Please sign in to comment.