tvm
functor.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  */
23 #ifndef TVM_NODE_FUNCTOR_H_
24 #define TVM_NODE_FUNCTOR_H_
25 
26 #include <dmlc/logging.h>
27 #include <tvm/runtime/object.h>
28 
29 #include <type_traits>
30 #include <utility>
31 #include <vector>
32 
33 namespace tvm {
34 
35 using runtime::ObjectRef;
36 
63 template <typename FType>
65 
66 template <typename R, typename... Args>
67 class NodeFunctor<R(const ObjectRef& n, Args...)> {
68  private:
70  typedef R (*FPointer)(const ObjectRef& n, Args...);
72  using TSelf = NodeFunctor<R(const ObjectRef& n, Args...)>;
74  std::vector<FPointer> func_;
75 
76  public:
78  using result_type = R;
84  bool can_dispatch(const ObjectRef& n) const {
85  uint32_t type_index = n->type_index();
86  return type_index < func_.size() && func_[type_index] != nullptr;
87  }
94  R operator()(const ObjectRef& n, Args... args) const {
95  ICHECK(can_dispatch(n)) << "NodeFunctor calls un-registered function on type "
96  << n->GetTypeKey();
97  return (*func_[n->type_index()])(n, std::forward<Args>(args)...);
98  }
105  template <typename TNode>
106  TSelf& set_dispatch(FPointer f) { // NOLINT(*)
107  uint32_t tindex = TNode::RuntimeTypeIndex();
108  if (func_.size() <= tindex) {
109  func_.resize(tindex + 1, nullptr);
110  }
111  ICHECK(func_[tindex] == nullptr) << "Dispatch for " << TNode::_type_key << " is already set";
112  func_[tindex] = f;
113  return *this;
114  }
121  template <typename TNode>
122  TSelf& clear_dispatch() { // NOLINT(*)
123  uint32_t tindex = TNode::RuntimeTypeIndex();
124  ICHECK_LT(tindex, func_.size()) << "clear_dispatch: index out of range";
125  func_[tindex] = nullptr;
126  return *this;
127  }
128 };
129 
130 #define TVM_REG_FUNC_VAR_DEF(ClsName) static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName
131 
173 #define TVM_STATIC_IR_FUNCTOR(ClsName, FField) \
174  TVM_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
175 } // namespace tvm
176 #endif // TVM_NODE_FUNCTOR_H_
TSelf & set_dispatch(FPointer f)
set the dispatcher for type TNode
Definition: functor.h:106
TSelf & clear_dispatch()
unset the dispatcher for type TNode
Definition: functor.h:122
R operator()(const ObjectRef &n, Args... args) const
invoke the functor, dispatch on type of n
Definition: functor.h:94
bool can_dispatch(const ObjectRef &n) const
Whether the functor can dispatch the corresponding Node.
Definition: functor.h:84
R result_type
the result type of this functor
Definition: functor.h:78
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Base class of all object reference.
Definition: object.h:519
uint32_t type_index() const
Definition: object.h:179
std::string GetTypeKey() const
Definition: object.h:184
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A managed object in the TVM runtime.