19 #ifndef TVM_SCRIPT_PRINTER_IR_DOCSIFIER_FUNCTOR_H_ 20 #define TVM_SCRIPT_PRINTER_IR_DOCSIFIER_FUNCTOR_H_ 23 #include <tvm/runtime/logging.h> 27 #include <type_traits> 28 #include <unordered_map> 41 template <
typename R,
typename... Args>
46 template <
class TObjectRef,
class TCallable>
47 using IsDispatchFunction =
48 typename std::is_convertible<TCallable, std::function<R(TObjectRef, Args...)>>;
62 template <
class TObjectRef>
64 uint32_t type_index = obj.defined() ? obj->type_index() : 0;
66 if ((pf = LookupDispatchTable(token, type_index)) !=
nullptr) {
67 return (*pf)(obj, args...);
69 if ((pf = LookupDispatchTable(
"", type_index)) !=
nullptr) {
70 return (*pf)(obj, args...);
72 LOG(WARNING) <<
"ObjectFunctor calls un-registered function on type: " 74 <<
". ObjectType: " << obj->GetTypeKey() <<
". Object: " << obj;
75 ICHECK(
false) <<
"ObjectFunctor calls un-registered function on type: " 77 <<
". ObjectType: " << obj->GetTypeKey() <<
". Object: " << obj;
90 std::vector<runtime::PackedFunc>* table = &dispatch_table_[token];
91 if (table->size() <= type_index) {
92 table->resize(type_index + 1,
nullptr);
95 if (slot !=
nullptr) {
96 ICHECK(
false) <<
"Dispatch for type is already registered: " 108 template <
typename TObjectRef,
typename TCallable,
109 typename = std::enable_if_t<IsDispatchFunction<TObjectRef, TCallable>::value>>
111 return set_dispatch(token, TObjectRef::ContainerType::RuntimeTypeIndex(),
124 std::vector<runtime::PackedFunc>* table = &dispatch_table_[token];
125 if (table->size() <= type_index) {
128 (*table)[type_index] =
nullptr;
139 auto it = dispatch_table_.find(token);
140 if (it == dispatch_table_.end()) {
143 const std::vector<runtime::PackedFunc>& tab = it->second;
144 if (type_index >= tab.size()) {
158 using DispatchTable = std::unordered_map<std::string, std::vector<runtime::PackedFunc>>;
160 DispatchTable dispatch_table_;
166 #endif // TVM_SCRIPT_PRINTER_IR_DOCSIFIER_FUNCTOR_H_ Definitions and helper macros for IR/AST nodes.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Dynamic dispatch functor based on ObjectPath.
Definition: ir_docsifier_functor.h:42
R operator()(const String &token, TObjectRef obj, Args... args) const
Call the dispatch function.
Definition: ir_docsifier_functor.h:63
TSelf & set_dispatch(String token, TCallable f)
Set the dispatch function.
Definition: ir_docsifier_functor.h:110
bool defined() const
Definition: object.h:544
void remove_dispatch(String token, uint32_t type_index)
Remove dispatch function.
Definition: ir_docsifier_functor.h:123
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
TSelf & set_dispatch(String token, uint32_t type_index, runtime::PackedFunc f)
Set the dispatch function.
Definition: ir_docsifier_functor.h:89
static std::string TypeIndex2Key(uint32_t tindex)
Get the type key of the corresponding index from runtime.
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
Type-erased function used across TVM API.