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 
47 
48 #include <string>
49 #include <utility>
50 #include <vector>
51 
52 namespace tvm {
53 namespace runtime {
54 
97 TVM_DLL void EnvCheckSignals();
98 
100 class Registry {
101  public:
106  TVM_DLL Registry& set_body(PackedFunc f); // NOLINT(*)
112  return set_body(PackedFunc(f));
113  }
137  template <typename FLambda>
138  Registry& set_body_typed(FLambda f) {
139  using FType = typename detail::function_signature<FLambda>::FType;
140  return set_body(TypedPackedFunc<FType>(std::move(f), name_).packed());
141  }
163  template <typename T, typename R, typename... Args>
164  Registry& set_body_method(R (T::*f)(Args...)) {
165  auto fwrap = [f](T target, Args... params) -> R {
166  // call method pointer
167  return (target.*f)(params...);
168  };
169  return set_body(TypedPackedFunc<R(T, Args...)>(fwrap, name_));
170  }
171 
193  template <typename T, typename R, typename... Args>
194  Registry& set_body_method(R (T::*f)(Args...) const) {
195  auto fwrap = [f](const T target, Args... params) -> R {
196  // call method pointer
197  return (target.*f)(params...);
198  };
199  return set_body(TypedPackedFunc<R(const T, Args...)>(fwrap, name_));
200  }
201 
233  template <typename TObjectRef, typename TNode, typename R, typename... Args,
234  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
235  Registry& set_body_method(R (TNode::*f)(Args...)) {
236  auto fwrap = [f](TObjectRef ref, Args... params) {
237  TNode* target = ref.operator->();
238  // call method pointer
239  return (target->*f)(params...);
240  };
241  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
242  }
243 
275  template <typename TObjectRef, typename TNode, typename R, typename... Args,
276  typename = typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
277  Registry& set_body_method(R (TNode::*f)(Args...) const) {
278  auto fwrap = [f](TObjectRef ref, Args... params) {
279  const TNode* target = ref.operator->();
280  // call method pointer
281  return (target->*f)(params...);
282  };
283  return set_body(TypedPackedFunc<R(TObjectRef, Args...)>(fwrap, name_));
284  }
285 
292  TVM_DLL static Registry& Register(const std::string& name, bool override = false); // NOLINT(*)
298  TVM_DLL static bool Remove(const std::string& name);
305  TVM_DLL static const PackedFunc* Get(const std::string& name); // NOLINT(*)
310  TVM_DLL static std::vector<std::string> ListNames();
311 
312  // Internal class.
313  struct Manager;
314 
315  protected:
317  std::string name_;
320  friend struct Manager;
321 };
322 
323 #define TVM_FUNC_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED ::tvm::runtime::Registry& __mk_##TVM
324 
333 #define TVM_REGISTER_GLOBAL(OpName) \
334  TVM_STR_CONCAT(TVM_FUNC_REG_VAR_DEF, __COUNTER__) = ::tvm::runtime::Registry::Register(OpName)
335 
336 #define TVM_STRINGIZE_DETAIL(x) #x
337 #define TVM_STRINGIZE(x) TVM_STRINGIZE_DETAIL(x)
338 #define TVM_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" TVM_STRINGIZE(__LINE__))
339 
342 #define TVM_ADD_FILELINE "\n\nDefined in " __FILE__ ":L" TVM_STRINGIZE(__LINE__)
343 
344 } // namespace runtime
345 } // namespace tvm
346 #endif // TVM_RUNTIME_REGISTRY_H_
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:235
static std::vector< std::string > ListNames()
Get the names of currently registered global function.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
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:138
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:277
PackedFunc func_
internal packed function
Definition: registry.h:319
static const PackedFunc * Get(const std::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:194
void EnvCheckSignals()
Check if signals have been sent to the process and if so invoke the registered signal handler in the ...
Registry & set_body(PackedFunc::FType f)
set the body of the function to be f
Definition: registry.h:111
std::function< void(TVMArgs args, TVMRetValue *rv)> FType
The internal std::function.
Definition: packed_func.h:88
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:136
static bool Remove(const std::string &name)
Erase global function from registry, if exist.
Registry & set_body(PackedFunc f)
set the body of the function to be f
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:68
Registry for global function.
Definition: registry.h:100
std::string name_
name of the function
Definition: registry.h:313
friend struct Manager
Definition: registry.h:320
static Registry & Register(const std::string &name, bool override=false)
Register a function with given name.
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:164
Type-erased function used across TVM API.