tvm
adt.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 
27 #ifndef TVM_IR_ADT_H_
28 #define TVM_IR_ADT_H_
29 
30 #include <tvm/ir/expr.h>
31 #include <tvm/ir/type.h>
32 #include <tvm/node/node.h>
36 #include <tvm/runtime/object.h>
37 
38 #include <string>
39 
40 namespace tvm {
41 
48  public:
56  mutable int32_t tag = -1;
57 
59 
61  v->Visit("name_hint", &name_hint);
62  v->Visit("inputs", &inputs);
63  v->Visit("belong_to", &belong_to);
64  v->Visit("tag", &tag);
65  v->Visit("span", &span);
66  v->Visit("_checked_type_", &checked_type_);
67  }
68 
69  bool SEqualReduce(const ConstructorNode* other, SEqualReducer equal) const {
70  // Use namehint for now to be consistent with the legacy relay impl
71  // TODO(tvm-team) revisit, need to check the type var.
72  return equal(name_hint, other->name_hint) && equal(inputs, other->inputs);
73  }
74 
75  void SHashReduce(SHashReducer hash_reduce) const {
76  hash_reduce(name_hint);
77  hash_reduce(inputs);
78  }
79 
80  static constexpr const char* _type_key = "relay.Constructor";
82 };
83 
88 class Constructor : public RelayExpr {
89  public:
96  TVM_DLL Constructor(String name_hint, Array<Type> inputs, GlobalTypeVar belong_to);
97 
99 };
100 
102 class TypeDataNode : public TypeNode {
103  public:
115 
117  v->Visit("header", &header);
118  v->Visit("type_vars", &type_vars);
119  v->Visit("constructors", &constructors);
120  v->Visit("span", &span);
121  }
122 
123  bool SEqualReduce(const TypeDataNode* other, SEqualReducer equal) const {
124  return equal.DefEqual(header, other->header) && equal.DefEqual(type_vars, other->type_vars) &&
126  }
127 
128  void SHashReduce(SHashReducer hash_reduce) const {
129  hash_reduce.DefHash(header);
130  hash_reduce.DefHash(type_vars);
131  hash_reduce(constructors);
132  }
133 
134  static constexpr const char* _type_key = "relay.TypeData";
136 };
137 
149 class TypeData : public Type {
150  public:
157  TVM_DLL TypeData(GlobalTypeVar header, Array<TypeVar> type_vars, Array<Constructor> constructors);
158 
160 };
161 
162 } // namespace tvm
163 #endif // TVM_IR_ADT_H_
Runtime Array container types.
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
Span span
Span that points to the original source code. Reserved debug information.
Definition: expr.h:55
ADT constructor. Constructors compare by pointer equality.
Definition: adt.h:47
bool SEqualReduce(const ConstructorNode *other, SEqualReducer equal) const
Definition: adt.h:69
int32_t tag
Index in the table of constructors (set when the type is registered).
Definition: adt.h:56
static constexpr const char * _type_key
Definition: adt.h:80
Array< Type > inputs
Input to the constructor.
Definition: adt.h:52
void SHashReduce(SHashReducer hash_reduce) const
Definition: adt.h:75
GlobalTypeVar belong_to
The datatype the constructor will construct.
Definition: adt.h:54
void VisitAttrs(AttrVisitor *v)
Definition: adt.h:60
ConstructorNode()
Definition: adt.h:58
String name_hint
The name (only a hint)
Definition: adt.h:50
TVM_DECLARE_FINAL_OBJECT_INFO(ConstructorNode, RelayExprNode)
Managed reference to ConstructorNode.
Definition: adt.h:88
Constructor(String name_hint, Array< Type > inputs, GlobalTypeVar belong_to)
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(Constructor, RelayExpr, ConstructorNode)
Managed reference to GlobalTypeVarNode.
Definition: type.h:333
Base node of all non-primitive expressions.
Definition: expr.h:361
Type checked_type_
Stores the result of type inference(type checking).
Definition: expr.h:369
Managed reference to RelayExprNode.
Definition: expr.h:441
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:126
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:110
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:187
TypeData container node.
Definition: adt.h:102
GlobalTypeVar header
The header is simply the name of the ADT. We adopt nominal typing for ADT definitions; that is,...
Definition: adt.h:110
Array< TypeVar > type_vars
The type variables (to allow for polymorphism).
Definition: adt.h:112
void SHashReduce(SHashReducer hash_reduce) const
Definition: adt.h:128
Array< Constructor > constructors
The constructors.
Definition: adt.h:114
bool SEqualReduce(const TypeDataNode *other, SEqualReducer equal) const
Definition: adt.h:123
static constexpr const char * _type_key
Definition: adt.h:134
void VisitAttrs(AttrVisitor *v)
Definition: adt.h:116
TVM_DECLARE_FINAL_OBJECT_INFO(TypeDataNode, TypeNode)
Stores all data for an Algebraic Data Type (ADT).
Definition: adt.h:149
TypeData(GlobalTypeVar header, Array< TypeVar > type_vars, Array< Constructor > constructors)
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(TypeData, Type, TypeDataNode)
Type is the base type of all types.
Definition: type.h:74
Span span
Span that points to the original source code. Reserved debug information.
Definition: type.h:80
Managed reference to TypeNode.
Definition: type.h:93
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Reference to string objects.
Definition: string.h:98
Base expr nodes in TVM.
IR/AST nodes for the unified type system in TVM.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.
Runtime ADT container types.
Runtime String container types.