soiferj commented on a change in pull request #4258: [WIP][TVM] Bring Your Own Codegen to TVM URL: https://github.com/apache/incubator-tvm/pull/4258#discussion_r343206515
########## File path: src/runtime/contrib/extern_common.h ########## @@ -0,0 +1,179 @@ +/* + * 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. + */ + +/*! + * \file src/runtime/contrib/extern_common.h + * \brief The definition of the base class for the external runtime. + */ + +#ifndef TVM_RUNTIME_CONTRIB_EXTERN_COMMON_H_ +#define TVM_RUNTIME_CONTRIB_EXTERN_COMMON_H_ + +#include <stdlib.h> +#include <dlpack/dlpack.h> +#include <tvm/runtime/module.h> +#include <tvm/runtime/ndarray.h> +#include <tvm/runtime/util.h> + +#include <string> +#include <vector> + +#if defined(_WIN32) +#include <windows.h> +#else +#include <dlfcn.h> +#endif + +namespace tvm { +namespace runtime { +namespace contrib { + +/*! + * \brief Split the encoded function name to tokens. + * + * \param the function name string. + * + * \return a vector of tokenized function name splitted by "_". + */ +static inline std::string GetSubgraphID(const std::string& name) { + std::string temp = name; + std::vector<std::string> tokens; + std::string delimiter = "_"; + size_t pos = 0; + std::string token; + while ((pos = temp.find(delimiter)) != std::string::npos) { + token = temp.substr(0, pos); + tokens.push_back(token); + temp.erase(0, pos + delimiter.length()); + } + tokens.push_back(temp); + + CHECK(tokens.size() >= 2) << "Invalid subgraph name: " << name; + CHECK(tokens[0] == "subgraph") + << "Function name does not start with \"subgraph\": " << name; + return tokens[1]; +} + +class ExternModuleBase : public runtime:: ModuleNode { + public: + ExternModuleBase() = default; + + ~ExternModuleBase() { + Close(); + } + + /*! + * \brief Get a PackedFunc from module, which is a function ptr can be invoked + * for execution given some parameters. + * + * \param name the name of the external function. + * \param sptr_to_self The shared_ptr that points to this module node. + * + * \return PackedFunc(nullptr) when it is not available. + */ + runtime::PackedFunc GetFunction( + const std::string& name, + const std::shared_ptr<ModuleNode>& sptr_to_self) override = 0; + + const char* type_key() const override { + return "ExternModuleBase"; + } + + protected: + // Platform dependent handlers for opening system lib. +#if defined(_WIN32) + // The handle. + HMODULE handle_{nullptr}; + + // Check if the handle_ is open. + bool IsOpen() const { + return handle_ != nullptr; + } + + // Open the library. + virtual void Open(const std::string& name) { Review comment: For runtimes that don't generate a compiled library (such as TensorFlow runtime), we can override this function to load in a custom way? ---------------------------------------------------------------- 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] With regards, Apache Git Services
