tvm
boxed_primitive.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_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_
25 #define TVM_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_
26 
27 #include <tvm/runtime/memory.h>
28 #include <tvm/runtime/object.h>
29 
30 namespace tvm {
31 namespace runtime {
32 
33 namespace detail {
34 /* \brief Provide the BoxNode<T> type traits in templated contexts
35  *
36  * The Box<T> class is used in many templated contexts, and is easier
37  * to have templated over the primitive type.
38  *
39  * However, much of the TVM type system depends on classes having a
40  * unique name. For example, the use of `Object::IsInstance` depends
41  * on `Object::GetOrAllocRuntimeTypeIndex`. Any duplicate names will
42  * result in duplicate indices, and invalid downcasting. Furthermore,
43  * the name must be specified in the Python FFI using
44  * `tvm._ffi.register_object`. This prevents use of
45  * `typeid(T)::name()` to build a unique name, as the name is not
46  * required to be human-readable or consistent across compilers.
47  *
48  * This utility struct should be specialized over the primitive type
49  * held by the box, to allow explicit listing of the `_type_key` and
50  * other similar tratis.
51  *
52  * Note: This should only contain traits that are required at runtime,
53  * and should *not* contain extensions for features that are only
54  * available at compile-time. For integration with compile-time-only
55  * functionality (e.g. StructuralHash, StructuralEqual), see
56  * `BoxNodeCompileTimeTraits` in `src/node/boxed_primitive.cc`.
57  */
58 template <typename Prim>
59 struct BoxNodeRuntimeTraits;
60 
61 } // namespace detail
62 
63 template <typename Prim>
64 class BoxNode : public Object {
65  public:
70  explicit BoxNode(Prim value) : value(value) {}
71 
73  Prim value;
74 
75  static constexpr const char* _type_key = detail::BoxNodeRuntimeTraits<Prim>::_type_key;
76  static constexpr bool _type_has_method_visit_attrs = false;
78 };
79 
80 template <typename Prim>
81 class Box : public ObjectRef {
82  public:
87  Box(Prim value) : ObjectRef(make_object<BoxNode<Prim>>(value)) {} // NOLINT(*)
88 
89  operator Prim() const { return (*this)->value; }
90 
92 };
93 
99 using Int = Box<int64_t>;
100 
108 
121 using Bool = Box<bool>;
122 
123 namespace detail {
124 template <>
125 struct BoxNodeRuntimeTraits<int64_t> {
126  static constexpr const char* _type_key = "runtime.BoxInt";
127 };
128 
129 template <>
130 struct BoxNodeRuntimeTraits<double> {
131  static constexpr const char* _type_key = "runtime.BoxFloat";
132 };
133 
134 template <>
135 struct BoxNodeRuntimeTraits<bool> {
136  static constexpr const char* _type_key = "runtime.BoxBool";
137 };
138 } // namespace detail
139 
140 } // namespace runtime
141 } // namespace tvm
142 
143 #endif // TVM_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_
Definition: boxed_primitive.h:64
static constexpr bool _type_has_method_visit_attrs
Definition: boxed_primitive.h:76
TVM_DECLARE_FINAL_OBJECT_INFO(BoxNode, Object)
BoxNode(Prim value)
Constructor.
Definition: boxed_primitive.h:70
static constexpr const char * _type_key
Definition: boxed_primitive.h:75
Prim value
The boxed value.
Definition: boxed_primitive.h:73
Definition: boxed_primitive.h:81
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Box, ObjectRef, BoxNode< Prim >)
Box(Prim value)
Constructor.
Definition: boxed_primitive.h:87
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
ObjectPtr< ArrayNode > make_object()
Definition: array.h:908
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A managed object in the TVM runtime.
Runtime memory management.