tvm
registry.h
Go to the documentation of this file.
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 
43 #ifndef TVM_RUNTIME_REGISTRY_H_
44 #define TVM_RUNTIME_REGISTRY_H_
45 
48 
49 #include <type_traits>
50 #include <utility>
51 #include <vector>
52 
53 namespace tvm {
54 namespace runtime {
55 
98 TVM_DLL void EnvCheckSignals();
99 
107  public:
109  WrappedPythonObject() : python_obj_(nullptr) {}
110 
112  explicit WrappedPythonObject(std::nullptr_t) : python_obj_(nullptr) {}
113 
122  explicit WrappedPythonObject(void* python_obj);
123 
129 
132 
135  WrappedPythonObject& operator=(std::nullptr_t);
136 
137  operator bool() { return python_obj_; }
138 
139  void* raw_pointer() { return python_obj_; }
140 
141  private:
142  void* python_obj_ = nullptr;
143 };
144 
146 class Registry {
147  public:
152  TVM_DLL Registry& set_body(PackedFunc f); // NOLINT(*)
157  template <typename TCallable,
158  typename = typename std::enable_if_t<
159  std::is_convertible<TCallable, std::function<void(TVMArgs, TVMRetValue*)>>::value &&
160  !std::is_base_of<PackedFunc, TCallable>::value>>
161  Registry& set_body(TCallable f) { // NOLINT(*)
162  return set_body(PackedFunc(f));
163  }
187  template <typename FLambda>
188  Registry& set_body_typed(FLambda f) {
189  using FType = typename detail::function_signature<FLambda>::FType;
190  return set_body(TypedPackedFunc<FType>(std::move(f), name_).packed());
191  }
213  template <typename T, typename R, typename... Args>
214  Registry& set_body_method(R (T::*f)(Args...)) {
215  using R_ = typename std::remove_reference<R>::type;
216  auto fwrap = [f](T target, Args... params) -> R_ {
217  // call method pointer
218  return (target.*f)(params...);
219  };
220  return set_body(TypedPackedFunc<R_(T, Args...)>(fwrap, name_));
221  }
222 
244  template <typename T, typename R, typename... Args>
245  Registry& set_body_method(R (T::*f)(Args...) const) {
246  auto fwrap = [f](const T target, Args... params) -> R {
247  // call method pointer
248  return (target.*f)(params...);
249  };
250  return set_body(TypedPackedFunc<R(const T, Args...)>(fwrap, name_));
251  }
252 
284  template <typename TObjectRef, typename TNode, typename R, typename... Args,
285  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
286  Registry& set_body_method(R (TNode::*f)(Args...)) {
287  auto fwrap = [f](TObjectRef ref, Args... params) {
288  TNode* target = ref.operator->();
289  // call method pointer
290  return (target->*f)(params...);
291  };
292  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
293  }
294 
326  template <typename TObjectRef, typename TNode, typename R, typename... Args,
327  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
328  Registry& set_body_method(R (TNode::*f)(Args...) const) {
329  auto fwrap = [f](TObjectRef ref, Args... params) {
330  const TNode* target = ref.operator->();
331  // call method pointer
332  return (target->*f)(params...);
333  };
334  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
335  }
336 
343  TVM_DLL static Registry& Register(const String& name, bool override = false); // NOLINT(*)
349  TVM_DLL static bool Remove(const String& name);
356  TVM_DLL static const PackedFunc* Get(const String& name); // NOLINT(*)
361  TVM_DLL static std::vector<String> ListNames();
362 
363  // Internal class.
364  struct Manager;
365 
366  protected:
371  friend struct Manager;
372 };
373 
374 #define TVM_FUNC_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED ::tvm::runtime::Registry& __mk_##TVM
375 
384 #define TVM_REGISTER_GLOBAL(OpName) \
385  TVM_STR_CONCAT(TVM_FUNC_REG_VAR_DEF, __COUNTER__) = ::tvm::runtime::Registry::Register(OpName)
386 
387 #define TVM_STRINGIZE_DETAIL(x) #x
388 #define TVM_STRINGIZE(x) TVM_STRINGIZE_DETAIL(x)
389 #define TVM_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" TVM_STRINGIZE(__LINE__))
393 #define TVM_ADD_FILELINE "\n\nDefined in " __FILE__ ":L" TVM_STRINGIZE(__LINE__)
394 
395 } // namespace runtime
396 } // namespace tvm
397 #endif // TVM_RUNTIME_REGISTRY_H_
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:139
Registry for global function.
Definition: registry.h:146
Registry & set_body_method(R(TNode::*f)(Args...) const)
set the body of the function to be the passed method pointer. Used when calling a method on a Node su...
Definition: registry.h:328
friend struct Manager
Definition: registry.h:371
static bool Remove(const String &name)
Erase global function from registry, if exist.
Registry & set_body(TCallable f)
set the body of the function to be f
Definition: registry.h:161
Registry & set_body_typed(FLambda f)
set the body of the function to the given function. Note that this will ignore default arg values and...
Definition: registry.h:188
static std::vector< String > ListNames()
Get the names of currently registered global function.
static Registry & Register(const String &name, bool override=false)
Register a function with given name.
PackedFunc func_
internal packed function
Definition: registry.h:370
String name_
name of the function
Definition: registry.h:364
static const PackedFunc * Get(const String &name)
Get the global function by name.
Registry & set_body_method(R(T::*f)(Args...) const)
set the body of the function to be the passed method pointer. Note that this will ignore default arg ...
Definition: registry.h:245
Registry & set_body_method(R(T::*f)(Args...))
set the body of the function to be the passed method pointer. Note that this will ignore default arg ...
Definition: registry.h:214
Registry & set_body_method(R(TNode::*f)(Args...))
set the body of the function to be the passed method pointer. Used when calling a method on a Node su...
Definition: registry.h:286
Registry & set_body(PackedFunc f)
set the body of the function to be f
Reference to string objects.
Definition: string.h:98
Arguments into TVM functions.
Definition: packed_func.h:392
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:807
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:61
A class that wraps a Python object and preserves its ownership.
Definition: registry.h:106
void * raw_pointer()
Definition: registry.h:139
~WrappedPythonObject()
Drop ownership of a python object.
WrappedPythonObject & operator=(const WrappedPythonObject &)
WrappedPythonObject(WrappedPythonObject &&)
WrappedPythonObject(void *python_obj)
Take ownership of a python object.
WrappedPythonObject()
Construct a wrapper that doesn't own anything.
Definition: registry.h:109
WrappedPythonObject & operator=(WrappedPythonObject &&)
WrappedPythonObject(const WrappedPythonObject &)
WrappedPythonObject & operator=(std::nullptr_t)
WrappedPythonObject(std::nullptr_t)
Conversion constructor from nullptr.
Definition: registry.h:112
void EnvCheckSignals()
Check if signals have been sent to the process and if so invoke the registered signal handler in the ...
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Type-erased function used across TVM API.
Runtime String container types.