tvm
env_func.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 
24 #ifndef TVM_IR_ENV_FUNC_H_
25 #define TVM_IR_ENV_FUNC_H_
26 
27 #include <tvm/node/reflection.h>
28 
29 #include <string>
30 #include <utility>
31 
32 namespace tvm {
41 class EnvFuncNode : public Object {
42  public:
49 
50  void VisitAttrs(AttrVisitor* v) { v->Visit("name", &name); }
51 
52  bool SEqualReduce(const EnvFuncNode* other, SEqualReducer equal) const {
53  // name uniquely identifies the env function.
54  return name == other->name;
55  }
56 
57  void SHashReduce(SHashReducer hash_reduce) const {
58  // Name uniquely identifies the env function.
59  hash_reduce(name);
60  }
61 
62  static constexpr const char* _type_key = "EnvFunc";
63  static constexpr bool _type_has_method_sequal_reduce = true;
64  static constexpr bool _type_has_method_shash_reduce = true;
66 };
67 
72 class EnvFunc : public ObjectRef {
73  public:
74  EnvFunc() {}
75  explicit EnvFunc(ObjectPtr<Object> n) : ObjectRef(n) {}
77  const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
83  template <typename... Args>
84  runtime::TVMRetValue operator()(Args&&... args) const {
85  const EnvFuncNode* n = operator->();
86  ICHECK(n != nullptr);
87  return n->func(std::forward<Args>(args)...);
88  }
95  TVM_DLL static EnvFunc Get(const String& name);
98 };
99 
103 template <typename FType>
105 
115 template <typename R, typename... Args>
116 class TypedEnvFunc<R(Args...)> : public ObjectRef {
117  public:
119  using TSelf = TypedEnvFunc<R(Args...)>;
127  TSelf& operator=(const EnvFunc& other) {
128  ObjectRef::operator=(other);
129  return *this;
130  }
132  const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
138  R operator()(Args... args) const {
139  const EnvFuncNode* n = operator->();
140  ICHECK(n != nullptr);
141  return runtime::detail::typed_packed_call_dispatcher<R>::run(n->func,
142  std::forward<Args>(args)...);
143  }
146 };
147 
148 } // namespace tvm
149 #endif // TVM_IR_ENV_FUNC_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A serializable function backed by TVM's global environment.
Definition: env_func.h:41
String name
Unique name of the global function.
Definition: env_func.h:44
static constexpr const char * _type_key
Definition: env_func.h:62
TVM_DECLARE_FINAL_OBJECT_INFO(EnvFuncNode, Object)
EnvFuncNode()
constructor
Definition: env_func.h:48
runtime::PackedFunc func
The internal packed function.
Definition: env_func.h:46
static constexpr bool _type_has_method_sequal_reduce
Definition: env_func.h:63
bool SEqualReduce(const EnvFuncNode *other, SEqualReducer equal) const
Definition: env_func.h:52
void SHashReduce(SHashReducer hash_reduce) const
Definition: env_func.h:57
void VisitAttrs(AttrVisitor *v)
Definition: env_func.h:50
static constexpr bool _type_has_method_shash_reduce
Definition: env_func.h:64
Managed reference to EnvFuncNode.
Definition: env_func.h:72
EnvFunc(ObjectPtr< Object > n)
Definition: env_func.h:75
const EnvFuncNode * operator->() const
Definition: env_func.h:77
runtime::TVMRetValue operator()(Args &&... args) const
Invoke the function.
Definition: env_func.h:84
EnvFunc()
Definition: env_func.h:74
static EnvFunc Get(const String &name)
Get a global function based on the name.
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
A typed version of EnvFunc. It is backed by a GlobalFuncNode internally.
Definition: env_func.h:116
TypedEnvFunc(ObjectPtr< Object > n)
Definition: env_func.h:121
TypedEnvFunc()
Definition: env_func.h:120
TSelf & operator=(const EnvFunc &other)
Assign global function to a TypedEnvFunc.
Definition: env_func.h:127
const EnvFuncNode * operator->() const
Definition: env_func.h:132
R operator()(Args... args) const
Invoke the function.
Definition: env_func.h:138
Please refer to TypedEnvFunc<R(Args..)>.
Definition: env_func.h:104
A custom smart pointer for Object.
Definition: object.h:362
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
base class of all object containers.
Definition: object.h:171
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
Reference to string objects.
Definition: string.h:98
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Reflection and serialization of compiler IR/AST nodes.