tvm
expr.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_EXPR_H_
25 #define TVM_IR_EXPR_H_
26 
27 #include <tvm/ir/span.h>
28 #include <tvm/ir/type.h>
29 #include <tvm/node/node.h>
31 #include <tvm/runtime/object.h>
32 
33 #include <algorithm>
34 #include <limits>
35 #include <string>
36 #include <type_traits>
37 
38 namespace tvm {
39 
41 
42 // Forward-declare VirtualDevice to avoid circular imports.
43 class VirtualDevice;
44 
49 class BaseExprNode : public Object {
50  public:
55  mutable Span span;
56 
57  static constexpr const char* _type_key = "BaseExpr";
58  static constexpr const bool _type_has_method_sequal_reduce = true;
59  static constexpr const bool _type_has_method_shash_reduce = true;
60  static constexpr const uint32_t _type_child_slots = 62;
62 };
63 
68 class BaseExpr : public ObjectRef {
69  public:
71 };
72 
85 class PrimExprNode : public BaseExprNode {
86  public:
102 
103  static constexpr const char* _type_key = "PrimExpr";
104  static constexpr const uint32_t _type_child_slots = 38;
106 };
107 
112 class PrimExpr : public BaseExpr {
113  public:
118  TVM_DLL PrimExpr(int32_t value); // NOLINT(*)
123  TVM_DLL PrimExpr(float value); // NOLINT(*)
124 
126  DataType dtype() const { return static_cast<const PrimExprNode*>(get())->dtype; }
127 
129 
130  private:
131  // Internal function for conversion.
132  friend struct runtime::PackedFuncValueConverter<PrimExpr>;
133  TVM_DLL static PrimExpr FromObject_(ObjectRef ref);
134 };
135 
145 class RelayExprNode : public BaseExprNode {
146  public:
153  mutable Type checked_type_ = Type(nullptr);
157  inline const Type& checked_type() const;
168  template <typename TTypeNode>
169  inline const TTypeNode* type_as() const;
170 
194 
206  VirtualDevice virtual_device() const;
207 
208  static constexpr const char* _type_key = "RelayExpr";
209  static constexpr const uint32_t _type_child_slots = 22;
211 };
212 
217 class RelayExpr : public BaseExpr {
218  public:
220 };
221 
222 class GlobalVar;
231 class GlobalVarNode : public RelayExprNode {
232  public:
235 
237  v->Visit("name_hint", &name_hint);
238  v->Visit("virtual_device_", &virtual_device_);
239  v->Visit("span", &span);
240  v->Visit("_checked_type_", &checked_type_);
241  }
242 
243  bool SEqualReduce(const GlobalVarNode* other, SEqualReducer equal) const {
244  // name matters for global var.
245  return equal(name_hint, other->name_hint) && equal.FreeVarEqualImpl(this, other);
246  }
247 
248  void SHashReduce(SHashReducer hash_reduce) const {
249  hash_reduce(name_hint);
250  hash_reduce.FreeVarHashImpl(this);
251  }
252 
253  static constexpr const char* _type_key = "GlobalVar";
255 };
256 
261 class GlobalVar : public RelayExpr {
262  public:
263  TVM_DLL explicit GlobalVar(String name_hint, Type type = {}, Span span = {});
264 
267 };
268 
269 // PrimExprs that are useful as runtime containers.
270 //
275 class IntImmNode : public PrimExprNode {
276  public:
278  int64_t value;
279 
281  v->Visit("dtype", &dtype);
282  v->Visit("value", &value);
283  v->Visit("span", &span);
284  }
285 
286  bool SEqualReduce(const IntImmNode* other, SEqualReducer equal) const {
287  return equal(dtype, other->dtype) && equal(value, other->value);
288  }
289 
290  void SHashReduce(SHashReducer hash_reduce) const {
291  hash_reduce(dtype);
292  hash_reduce(value);
293  }
294 
295  static constexpr const char* _type_key = "IntImm";
297 };
298 
304 class IntImm : public PrimExpr {
305  public:
312  TVM_DLL IntImm(DataType dtype, int64_t value, Span span = Span());
313 
315 };
316 
321 class FloatImmNode : public PrimExprNode {
322  public:
324  double value;
325 
327  v->Visit("dtype", &dtype);
328  v->Visit("value", &value);
329  v->Visit("span", &span);
330  }
331 
332  bool SEqualReduce(const FloatImmNode* other, SEqualReducer equal) const {
333  return equal(dtype, other->dtype) && equal(value, other->value);
334  }
335 
336  void SHashReduce(SHashReducer hash_reduce) const {
337  hash_reduce(dtype);
338  hash_reduce(value);
339  }
340 
341  static constexpr const char* _type_key = "FloatImm";
343 };
344 
350 class FloatImm : public PrimExpr {
351  public:
358  TVM_DLL FloatImm(DataType dtype, double value, Span span = Span());
359 
361 };
362 
369 class Bool : public IntImm {
370  public:
371  explicit Bool(bool value, Span span = Span()) : IntImm(DataType::Bool(), value, span) {}
372  Bool operator!() const { return Bool((*this)->value == 0); }
373  operator bool() const { return (*this)->value != 0; }
374 
376 };
377 
378 // Overload operators to make sure we have the most fine grained types.
379 inline Bool operator||(const Bool& a, bool b) { return Bool(a.operator bool() || b); }
380 inline Bool operator||(bool a, const Bool& b) { return Bool(a || b.operator bool()); }
381 inline Bool operator||(const Bool& a, const Bool& b) {
382  return Bool(a.operator bool() || b.operator bool());
383 }
384 inline Bool operator&&(const Bool& a, bool b) { return Bool(a.operator bool() && b); }
385 inline Bool operator&&(bool a, const Bool& b) { return Bool(a && b.operator bool()); }
386 inline Bool operator&&(const Bool& a, const Bool& b) {
387  return Bool(a.operator bool() && b.operator bool());
388 }
389 
390 inline bool operator==(const Bool& a, bool b) { return a.operator bool() == b; }
391 inline bool operator==(bool a, const Bool& b) { return a == b.operator bool(); }
392 inline bool operator==(const Bool& a, const Bool& b) {
393  return a.operator bool() == b.operator bool();
394 }
395 
404 class Integer : public IntImm {
405  public:
406  Integer() {}
410  explicit Integer(ObjectPtr<Object> node) : IntImm(node) {}
414  Integer(int value, Span span = Span()) : IntImm(DataType::Int(32), value, span) {} // NOLINT(*)
419  Integer(IntImm other) : IntImm(std::move(other)) {} // NOLINT(*)
425  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
426  explicit Integer(Enum value) : Integer(static_cast<int>(value)) {
427  static_assert(std::is_same<int, typename std::underlying_type<Enum>::type>::value,
428  "declare enum to be enum int to use visitor");
429  }
434  Integer& operator=(const IntImm& other) {
435  data_ = ObjectRef::GetDataPtr<Object>(other);
436  return *this;
437  }
441  operator int64_t() const {
442  ICHECK(data_ != nullptr) << " Trying to reference a null Integer";
443  return (*this)->value;
444  }
445  // comparators
446  Bool operator==(int other) const {
447  if (data_ == nullptr) return Bool(false);
448  return Bool((*this)->value == other);
449  }
450  Bool operator!=(int other) const { return !(*this == other); }
451  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
452  Bool operator==(Enum other) const {
453  return *this == static_cast<int>(other);
454  }
455  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
456  Bool operator!=(Enum other) const {
457  return *this != static_cast<int>(other);
458  }
459 };
460 
462 class RangeNode : public Object {
463  public:
469  mutable Span span;
472  RangeNode(PrimExpr min, PrimExpr extent, Span span = Span())
473  : min(min), extent(extent), span(span) {}
474 
476  v->Visit("min", &min);
477  v->Visit("extent", &extent);
478  v->Visit("span", &span);
479  }
480 
481  bool SEqualReduce(const RangeNode* other, SEqualReducer equal) const {
482  return equal(min, other->min) && equal(extent, other->extent);
483  }
484 
485  void SHashReduce(SHashReducer hash_reduce) const {
486  hash_reduce(min);
487  hash_reduce(extent);
488  }
489 
490  static constexpr const char* _type_key = "Range";
491  static constexpr const bool _type_has_method_sequal_reduce = true;
492  static constexpr const bool _type_has_method_shash_reduce = true;
494 };
495 
497 class Range : public ObjectRef {
498  public:
505  TVM_DLL Range(PrimExpr begin, PrimExpr end, Span span = Span());
516  static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span = Span());
517  // declare range.
519 };
520 
521 // implementataions
522 inline const Type& RelayExprNode::checked_type() const {
523  ICHECK(checked_type_.defined()) << "internal error: the type checker has "
524  << "not populated the checked_type "
525  << "field for " << GetRef<RelayExpr>(this);
526  return this->checked_type_;
527 }
528 
529 template <typename TTypeNode>
530 inline const TTypeNode* RelayExprNode::type_as() const {
531  static_assert(std::is_base_of<TypeNode, TTypeNode>::value,
532  "TType must be a special case of type");
533  ICHECK(checked_type_.defined())
534  << "Type inference for this Expr has not completed. Try to call infer_type pass.";
535  const TTypeNode* node = checked_type_.as<TTypeNode>();
536  ICHECK(node != nullptr) << "Expected type to be " << TTypeNode::_type_key << ", but get "
537  << checked_type_->GetTypeKey();
538  return node;
539 }
540 
541 } // namespace tvm
542 
543 namespace tvm {
544 namespace runtime {
545 // common rule for RetValue and ArgValue
546 template <>
548  static PrimExpr From(const TVMPODValue_& val) {
549  if (val.type_code() == kTVMNullptr) {
550  return PrimExpr(ObjectPtr<Object>(nullptr));
551  }
552  if (val.type_code() == kDLInt) {
553  return PrimExpr(val.operator int());
554  }
555  if (val.type_code() == kDLFloat) {
556  return PrimExpr(static_cast<float>(val.operator double()));
557  }
558 
559  return PrimExpr::FromObject_(val.AsObjectRef<ObjectRef>());
560  }
561 };
562 
563 template <>
565  static tvm::Integer From(const TVMPODValue_& val) {
566  if (val.type_code() == kTVMNullptr) {
567  return Integer(ObjectPtr<Object>(nullptr));
568  }
569  if (val.type_code() == kTVMArgInt) {
570  return Integer(val.operator int());
571  }
572  return val.AsObjectRef<tvm::Integer>();
573  }
574 };
575 
576 template <>
578  static tvm::Bool From(const TVMPODValue_& val) {
579  if (val.type_code() == kTVMNullptr) {
580  return Bool(ObjectPtr<Object>(nullptr));
581  }
582  if (val.type_code() == kTVMArgInt) {
583  int v = val.operator int();
584  ICHECK(v == 0 || v == 1) << "ValueError: boolean value can only be 0 or 1, but get " << v;
585  return Bool(static_cast<bool>(v));
586  }
587  return val.AsObjectRef<tvm::Bool>();
588  }
589 };
590 
591 } // namespace runtime
592 } // namespace tvm
593 #endif // TVM_IR_EXPR_H_
Integer(Enum value)
Constructor from enum.
Definition: expr.h:426
tvm::Span Span
Definition: base.h:65
static constexpr const char * _type_key
Definition: expr.h:57
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:185
double value
The constant value content.
Definition: expr.h:324
PrimExpr min
beginning of the node
Definition: expr.h:465
const Type & checked_type() const
Definition: expr.h:522
Bool operator &&(const Bool &a, bool b)
Definition: expr.h:384
A custom smart pointer for Object.
Definition: object.h:358
Boolean constant.
Definition: expr.h:369
Definitions and helper macros for IR/AST nodes.
Bool operator||(const Bool &a, bool b)
Definition: expr.h:379
Runtime String container types.
Internal base class to handle conversion to POD values.
Definition: packed_func.h:541
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:485
bool FreeVarEqualImpl(const runtime::Object *lhs, const runtime::Object *rhs) const
Implementation for equality rule of var type objects(e.g. TypeVar, tir::Var).
Definition: structural_equal.h:190
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
static constexpr const bool _type_has_method_shash_reduce
Definition: expr.h:59
Integer()
Definition: expr.h:406
String name_hint
The name of the variable, this only acts as a hint.
Definition: expr.h:234
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Span span
the location of this range in the source
Definition: expr.h:469
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:102
Definition: c_runtime_api.h:111
Bool operator==(int other) const
Definition: expr.h:446
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Constant floating point literals in the program.
Definition: expr.h:321
Definition: loop_state.h:456
bool SEqualReduce(const IntImmNode *other, SEqualReducer equal) const
Definition: expr.h:286
DataType dtype() const
Definition: expr.h:126
Integer(int value, Span span=Span())
Construct integer from int value.
Definition: expr.h:414
base class of all object containers.
Definition: object.h:167
Integer & operator=(const IntImm &other)
Assign an expression to integer.
Definition: expr.h:434
Integer(IntImm other)
Construct integer from int imm.
Definition: expr.h:419
Managed reference to BaseExprNode.
Definition: expr.h:68
Constant integer literals in the program.
Definition: expr.h:275
PrimExpr extent
the extend of range
Definition: expr.h:467
Span information for debugging purposes.
Integer(ObjectPtr< Object > node)
constructor from node.
Definition: expr.h:410
Managed reference class to FloatImmNode.
Definition: expr.h:350
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
static tvm::Bool From(const TVMPODValue_ &val)
Definition: expr.h:578
bool SEqualReduce(const FloatImmNode *other, SEqualReducer equal) const
Definition: expr.h:332
Managed reference class to VirtualDeviceNode.
Definition: virtual_device.h:261
Range constainer.
Definition: expr.h:497
Definition: span.h:115
Span span
Span that points to the original source code. Reserved debug information.
Definition: expr.h:55
static constexpr const uint32_t _type_child_slots
Definition: expr.h:60
IR/AST nodes for the unified type system in TVM.
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:248
bool SEqualReduce(const RangeNode *other, SEqualReducer equal) const
Definition: expr.h:481
Runtime primitive data type.
Definition: data_type.h:41
Base type of all the expressions.
Definition: expr.h:49
tvm::GlobalVar GlobalVar
Definition: expr.h:58
static PrimExpr From(const TVMPODValue_ &val)
Definition: expr.h:548
static constexpr const bool _type_has_method_sequal_reduce
Definition: expr.h:58
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:326
Bool operator!() const
Definition: expr.h:372
Managed reference class to IntImmNode.
Definition: expr.h:304
Managed reference to GlobalVarNode.
Definition: expr.h:261
RangeNode(PrimExpr min, PrimExpr extent, Span span=Span())
Definition: expr.h:472
ObjectRef virtual_device_
The virtual device (VirtualDevice) for this node (the result of device planning). For first-order exp...
Definition: expr.h:193
TObjectRef AsObjectRef() const
Definition: packed_func.h:1823
int64_t value
the Internal value.
Definition: expr.h:278
Reference to string objects.
Definition: string.h:124
Managed reference to RelayExprNode.
Definition: expr.h:217
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:280
bool SEqualReduce(const GlobalVarNode *other, SEqualReducer equal) const
Definition: expr.h:243
Bool operator!=(Enum other) const
Definition: expr.h:456
tvm::Type Type
Definition: type.h:47
bool operator==(const Bool &a, bool b)
Definition: expr.h:390
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:290
Bool operator!=(int other) const
Definition: expr.h:450
Base class of all object reference.
Definition: object.h:511
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:785
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:336
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
RangeNode()
constructor
Definition: expr.h:471
static tvm::Integer From(const TVMPODValue_ &val)
Definition: expr.h:565
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:475
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:236
const TTypeNode * type_as() const
Check if the inferred(checked) type of the Expr is backed by a TTypeNode and return it...
Definition: expr.h:530
DataType dtype
The runtime data type of the primitive expression.
Definition: expr.h:101
Definition: c_runtime_api.h:114
int type_code() const
Definition: packed_func.h:610
Bool(bool value, Span span=Span())
Definition: expr.h:371
Managed reference to TypeNode.
Definition: type.h:93
TVM_DECLARE_BASE_OBJECT_INFO(BaseExprNode, Object)
Reference to PrimExprNode.
Definition: expr.h:112
Global variable that lives in the top-level module.
Definition: expr.h:231
Base node of all non-primitive expressions.
Definition: expr.h:145
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
Bool operator==(Enum other) const
Definition: expr.h:452
Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue.
Definition: packed_func.h:1096
Base node of all primitive expressions.
Definition: expr.h:85
Container of constant int that adds more constructors.
Definition: expr.h:404
range over one dimension
Definition: expr.h:462