tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
101 class Registry {
102  public:
107  TVM_DLL Registry& set_body(PackedFunc f); // NOLINT(*)
112  template <typename TCallable,
113  typename = typename std::enable_if_t<
114  std::is_convertible<TCallable, std::function<void(TVMArgs, TVMRetValue*)>>::value &&
115  !std::is_base_of<PackedFunc, TCallable>::value>>
116  Registry& set_body(TCallable f) { // NOLINT(*)
117  return set_body(PackedFunc(f));
118  }
142  template <typename FLambda>
143  Registry& set_body_typed(FLambda f) {
144  using FType = typename detail::function_signature<FLambda>::FType;
145  return set_body(TypedPackedFunc<FType>(std::move(f), name_).packed());
146  }
168  template <typename T, typename R, typename... Args>
169  Registry& set_body_method(R (T::*f)(Args...)) {
170  using R_ = typename std::remove_reference<R>::type;
171  auto fwrap = [f](T target, Args... params) -> R_ {
172  // call method pointer
173  return (target.*f)(params...);
174  };
175  return set_body(TypedPackedFunc<R_(T, Args...)>(fwrap, name_));
176  }
177 
199  template <typename T, typename R, typename... Args>
200  Registry& set_body_method(R (T::*f)(Args...) const) {
201  auto fwrap = [f](const T target, Args... params) -> R {
202  // call method pointer
203  return (target.*f)(params...);
204  };
205  return set_body(TypedPackedFunc<R(const T, Args...)>(fwrap, name_));
206  }
207 
239  template <typename TObjectRef, typename TNode, typename R, typename... Args,
240  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
241  Registry& set_body_method(R (TNode::*f)(Args...)) {
242  auto fwrap = [f](TObjectRef ref, Args... params) {
243  TNode* target = ref.operator->();
244  // call method pointer
245  return (target->*f)(params...);
246  };
247  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
248  }
249 
281  template <typename TObjectRef, typename TNode, typename R, typename... Args,
282  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
283  Registry& set_body_method(R (TNode::*f)(Args...) const) {
284  auto fwrap = [f](TObjectRef ref, Args... params) {
285  const TNode* target = ref.operator->();
286  // call method pointer
287  return (target->*f)(params...);
288  };
289  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
290  }
291 
298  TVM_DLL static Registry& Register(const String& name, bool override = false); // NOLINT(*)
304  TVM_DLL static bool Remove(const String& name);
311  TVM_DLL static const PackedFunc* Get(const String& name); // NOLINT(*)
316  TVM_DLL static std::vector<String> ListNames();
317 
318  // Internal class.
319  struct Manager;
320 
321  protected:
326  friend struct Manager;
327 };
328 
329 #define TVM_FUNC_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED ::tvm::runtime::Registry& __mk_##TVM
330 
339 #define TVM_REGISTER_GLOBAL(OpName) \
340  TVM_STR_CONCAT(TVM_FUNC_REG_VAR_DEF, __COUNTER__) = ::tvm::runtime::Registry::Register(OpName)
341 
342 #define TVM_STRINGIZE_DETAIL(x) #x
343 #define TVM_STRINGIZE(x) TVM_STRINGIZE_DETAIL(x)
344 #define TVM_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" TVM_STRINGIZE(__LINE__))
348 #define TVM_ADD_FILELINE "\n\nDefined in " __FILE__ ":L" TVM_STRINGIZE(__LINE__)
349 
350 } // namespace runtime
351 } // namespace tvm
352 #endif // TVM_RUNTIME_REGISTRY_H_
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
Registry for global function.
Definition: registry.h:101
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:283
friend struct Manager
Definition: registry.h:326
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:116
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:143
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:325
String name_
name of the function
Definition: registry.h:319
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:200
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:169
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:241
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:391
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
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.