1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file library_module.h
22 * \brief Module that builds from a libary of symbols.
23 */
24#ifndef TVM_RUNTIME_LIBRARY_MODULE_H_
25#define TVM_RUNTIME_LIBRARY_MODULE_H_
26
27#include <tvm/runtime/c_backend_api.h>
28#include <tvm/runtime/c_runtime_api.h>
29#include <tvm/runtime/module.h>
30
31#include <functional>
32#include <string>
33
34namespace tvm {
35namespace runtime {
36
37/*! \brief Load a module with the given type key directly from the stream.
38 * This function wraps the registry mechanism used to store type based deserializers
39 * for each runtime::Module sub-class.
40 *
41 * \param type_key The type key of the serialized module.
42 * \param stream A pointer to the stream containing the serialized module.
43 * \return module The deserialized module.
44 */
45Module LoadModuleFromBinary(const std::string& type_key, dmlc::Stream* stream);
46
47/*!
48 * \brief Library is the common interface
49 * for storing data in the form of shared libaries.
50 *
51 * \sa dso_library.cc
52 * \sa system_library.cc
53 */
54class Library : public Object {
55 public:
56 // destructor.
57 virtual ~Library() {}
58 /*!
59 * \brief Get the symbol address for a given name.
60 * \param name The name of the symbol.
61 * \return The symbol.
62 */
63 virtual void* GetSymbol(const char* name) = 0;
64 // NOTE: we do not explicitly create an type index and type_key here for libary.
65 // This is because we do not need dynamic type downcasting.
66};
67
68/*!
69 * \brief Wrap a TVMBackendPackedCFunc to packed function.
70 * \param faddr The function address
71 * \param mptr The module pointer node.
72 */
73PackedFunc WrapPackedFunc(TVMBackendPackedCFunc faddr, const ObjectPtr<Object>& mptr);
74
75/*!
76 * \brief Utility to initialize conext function symbols during startup
77 * \param fgetsymbol A symbol lookup function.
78 */
79void InitContextFunctions(std::function<void*(const char*)> fgetsymbol);
80
81/*!
82 * \brief Type alias for function to wrap a TVMBackendPackedCFunc.
83 * \param The function address imported from a module.
84 * \param mptr The module pointer node.
85 * \return Packed function that wraps the invocation of the function at faddr.
86 */
87using PackedFuncWrapper =
88 std::function<PackedFunc(TVMBackendPackedCFunc faddr, const ObjectPtr<Object>& mptr)>;
89
90/*! \brief Return a library object interface over dynamic shared
91 * libraries in Windows and Linux providing support for
92 * loading/unloading and symbol lookup.
93 * \param Full path to shared library.
94 * \return Returns pointer to the Library providing symbol lookup.
95 */
96ObjectPtr<Library> CreateDSOLibraryObject(std::string library_path);
97
98/*!
99 * \brief Create a module from a library.
100 *
101 * \param lib The library.
102 * \param wrapper Optional function used to wrap a TVMBackendPackedCFunc,
103 * by default WrapPackedFunc is used.
104 * \return The corresponding loaded module.
105 *
106 * \note This function can create multiple linked modules
107 * by parsing the binary blob section of the library.
108 */
109Module CreateModuleFromLibrary(ObjectPtr<Library> lib, PackedFuncWrapper wrapper = WrapPackedFunc);
110} // namespace runtime
111} // namespace tvm
112#endif // TVM_RUNTIME_LIBRARY_MODULE_H_
113