mbaret commented on a change in pull request #6222: URL: https://github.com/apache/incubator-tvm/pull/6222#discussion_r467886421
########## File path: src/relay/backend/contrib/ethosn/ethosn_api.cc ########## @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "ethosn_api.h" + +#include <tvm/relay/attrs/nn.h> +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> +#include <tvm/tir/analysis.h> + +#include <fstream> +#include <map> +#include <memory> +#include <string> +#include <utility> +#include <vector> + +#include "capabilities.h" +#include "ethosn_support_library/Support.hpp" +#include "ethosn_support_library/SupportQueries.hpp" + +namespace tvm { +namespace relay { +namespace contrib { +namespace ethosn { + +std::unique_ptr<sl::CompiledNetwork> EthosnAPI::Compile(std::shared_ptr<sl::Network> network, + const sl::CompilationOptions& options) { + std::vector<std::unique_ptr<sl::CompiledNetwork>> compiled_network = + sl::Compile(*network, options); + CHECK_GE(compiled_network.size(), 1) << "Ethos-N compiler failed to compile network"; + + return std::move(compiled_network[0]); +} + +struct EthosnCompilerConfigNode : public tvm::AttrsNode<EthosnCompilerConfigNode> { + int variant; + bool strategy0; + bool strategy1; + bool strategy3; + bool strategy4; + bool strategy6; + bool strategy7; + bool dump_ram; + bool initial_sram_dump; + bool block_config_16x16; + bool block_config_32x8; + bool block_config_8x32; + bool block_config_8x8; + bool enable_intermediate_compression; + bool disable_winograd; + bool dump_debug_files; + String debug_dir; + bool enable_cascading; + + TVM_DECLARE_ATTRS(EthosnCompilerConfigNode, "ext.attrs.EthosnCompilerConfigNode") { + TVM_ATTR_FIELD(variant) + .describe("0 for Ethos-N77, 1 for Ethos-N57, 2 for Ethos-N37. See Ethos-N documentation.") + .set_default(0); + TVM_ATTR_FIELD(strategy0).set_default(true); + TVM_ATTR_FIELD(strategy1).set_default(true); + TVM_ATTR_FIELD(strategy3).set_default(true); + TVM_ATTR_FIELD(strategy4).set_default(true); + TVM_ATTR_FIELD(strategy6).set_default(true); + TVM_ATTR_FIELD(strategy7).set_default(true); + TVM_ATTR_FIELD(dump_ram).set_default(false); + TVM_ATTR_FIELD(initial_sram_dump).set_default(false); + TVM_ATTR_FIELD(block_config_16x16).set_default(true); + TVM_ATTR_FIELD(block_config_32x8).set_default(true); + 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); + TVM_ATTR_FIELD(dump_debug_files).set_default(false); + TVM_ATTR_FIELD(debug_dir).set_default("."); + TVM_ATTR_FIELD(enable_cascading).set_default(false); + } +}; + +class EthosnCompilerConfig : public Attrs { + public: + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(EthosnCompilerConfig, Attrs, EthosnCompilerConfigNode); +}; + +TVM_REGISTER_NODE_TYPE(EthosnCompilerConfigNode); +TVM_REGISTER_PASS_CONFIG_OPTION("relay.ext.ethos-n.options", EthosnCompilerConfig); + +sl::CompilationOptions EthosnAPI::CreateOptions() { + auto ctx = transform::PassContext::Current(); + auto cfg = ctx->GetConfig<EthosnCompilerConfig>("relay.ext.ethos-n.options"); + if (!cfg.defined()) { + cfg = AttrsWithDefaultValues<EthosnCompilerConfig>(); + } + + sl::CompilationOptions options(variants[cfg.value()->variant]); + options.m_Strategy0 = cfg.value()->strategy0; + options.m_Strategy1 = cfg.value()->strategy1; + options.m_Strategy3 = cfg.value()->strategy3; + options.m_Strategy4 = cfg.value()->strategy4; + options.m_Strategy6 = cfg.value()->strategy6; + options.m_Strategy7 = cfg.value()->strategy7; + options.m_DebugInfo.m_DumpRam = cfg.value()->dump_ram; + options.m_DebugInfo.m_InitialSramDump = cfg.value()->initial_sram_dump; + options.m_BlockConfig16x16 = cfg.value()->block_config_16x16; + options.m_BlockConfig32x8 = cfg.value()->block_config_32x8; + 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; + options.m_DebugInfo.m_DumpDebugFiles = cfg.value()->dump_debug_files; + options.m_DebugInfo.m_DebugDir = cfg.value()->debug_dir; + options.m_EnableCascading = cfg.value()->enable_cascading; + return options; +} + +bool EthosnAPI::IsEthosFunc(const Call& call, const std::string& op_name) { + if (call->op->IsInstance<FunctionNode>()) { + Function func = Downcast<Function>(call->op); + CHECK(func.defined()); + auto name_node = func->GetAttr<String>(attr::kComposite); + return name_node.value() == op_name; + } + return false; +} + +bool EthosnAPI::IsEthosOp(const Call& call, const std::string& op_name) { Review comment: I've moved this out of EthosnAPI into the codegen now because it has no interactions with Support Library APIs. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
