tvm
data_type.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 /*
20  * \file tvm/runtime/data_type.h
21  * \brief Primitive runtime data type.
22  */
23 // Acknowledgement: DataType structure design originates from Halide.
24 #ifndef TVM_RUNTIME_DATA_TYPE_H_
25 #define TVM_RUNTIME_DATA_TYPE_H_
26 
27 #include <tvm/ffi/container/shape.h>
28 #include <tvm/ffi/dtype.h>
29 #include <tvm/ffi/error.h>
30 #include <tvm/runtime/base.h>
31 
32 #include <cstring>
33 #include <string>
34 #include <type_traits>
35 
36 namespace tvm {
37 namespace runtime {
38 
45 class DataType {
46  public:
55  enum TypeCode {
56  kInt = kDLInt,
57  kUInt = kDLUInt,
58  kFloat = kDLFloat,
59  kHandle = kDLOpaqueHandle,
60  kBFloat = kDLBfloat,
61  kBool = kDLBool,
62  kFloat8_e3m4 = kDLFloat8_e3m4,
63  kFloat8_e4m3 = kDLFloat8_e4m3,
64  kFloat8_e4m3b11fnuz = kDLFloat8_e4m3b11fnuz,
65  kFloat8_e4m3fn = kDLFloat8_e4m3fn,
66  kFloat8_e4m3fnuz = kDLFloat8_e4m3fnuz,
67  kFloat8_e5m2 = kDLFloat8_e5m2,
68  kFloat8_e5m2fnuz = kDLFloat8_e5m2fnuz,
69  kFloat8_e8m0fnu = kDLFloat8_e8m0fnu,
70  kFloat6_e2m3fn = kDLFloat6_e2m3fn,
71  kFloat6_e3m2fn = kDLFloat6_e3m2fn,
72  kFloat4_e2m1fn = kDLFloat4_e2m1fn,
73  kCustomBegin = 129
74  };
76  DataType() { data_ = DataType::Void(); }
81  explicit DataType(DLDataType dtype) : data_(dtype) {}
89  DataType(int code, int bits, int lanes, bool is_scalable = false) {
90  data_.code = static_cast<uint8_t>(code);
91  data_.bits = static_cast<uint8_t>(bits);
92  if (is_scalable) {
93  TVM_FFI_ICHECK(lanes > 1) << "Invalid value for vscale factor" << lanes;
94  }
95  data_.lanes = is_scalable ? static_cast<uint16_t>(-lanes) : static_cast<uint16_t>(lanes);
96  if (code == kBFloat) {
97  TVM_FFI_ICHECK_EQ(bits, 16);
98  }
102  TVM_FFI_ICHECK_EQ(bits, 8);
103  }
104  if (code == kFloat6_e2m3fn || code == kFloat6_e3m2fn) {
105  TVM_FFI_ICHECK_EQ(bits, 6);
106  }
107  if (code == kFloat4_e2m1fn) {
108  TVM_FFI_ICHECK_EQ(bits, 4);
109  }
110  }
112  int code() const { return static_cast<int>(data_.code); }
114  int bits() const { return static_cast<int>(data_.bits); }
116  int bytes() const { return (bits() + 7) / 8; }
118  int lanes() const {
119  int lanes_as_int = static_cast<int16_t>(data_.lanes);
120  if (lanes_as_int < 0) {
121  TVM_FFI_THROW(InternalError)
122  << "Can't fetch the lanes of a scalable vector at a compile time.";
123  }
124  return lanes_as_int;
125  }
127  int vscale_factor() const {
128  int lanes_as_int = static_cast<int16_t>(data_.lanes);
129  if (lanes_as_int >= -1) {
130  TVM_FFI_THROW(InternalError) << "A fixed length vector doesn't have a vscale factor.";
131  }
132  return -lanes_as_int;
133  }
136  return is_scalable_vector() ? vscale_factor() : lanes();
137  }
139  bool is_scalar() const { return !is_scalable_vector() && lanes() == 1; }
141  bool is_bool() const { return code() == DataType::kBool; }
143  bool is_predicate_dtype() const { return is_bool() || (is_uint() && bits() == 1); }
145  bool is_float() const { return code() == DataType::kFloat; }
147  bool is_bfloat() const { return code() == DataType::kBFloat; }
149  bool is_float8() const {
150  return bits() == 8 &&
155  }
157  bool is_float6() const {
158  return bits() == 6 &&
160  }
162  bool is_float4() const { return bits() == 4 && code() == DataType::kFloat4_e2m1fn; }
164  bool is_float8_e3m4() const { return bits() == 8 && code() == DataType::kFloat8_e3m4; }
166  bool is_float8_e4m3() const { return bits() == 8 && code() == DataType::kFloat8_e4m3; }
168  bool is_float8_e4m3b11fnuz() const {
169  return bits() == 8 && code() == DataType::kFloat8_e4m3b11fnuz;
170  }
172  bool is_float8_e4m3fn() const { return bits() == 8 && code() == DataType::kFloat8_e4m3fn; }
174  bool is_float8_e4m3fnuz() const { return bits() == 8 && code() == DataType::kFloat8_e4m3fnuz; }
176  bool is_float8_e5m2() const { return bits() == 8 && code() == DataType::kFloat8_e5m2; }
178  bool is_float8_e5m2fnuz() const { return bits() == 8 && code() == DataType::kFloat8_e5m2fnuz; }
180  bool is_float8_e8m0fnu() const { return bits() == 8 && code() == DataType::kFloat8_e8m0fnu; }
182  bool is_float6_e2m3fn() const { return bits() == 6 && code() == DataType::kFloat6_e2m3fn; }
184  bool is_float6_e3m2fn() const { return bits() == 6 && code() == DataType::kFloat6_e3m2fn; }
186  bool is_float4_e2m1fn() const { return bits() == 4 && code() == DataType::kFloat4_e2m1fn; }
188  bool is_float16() const { return is_float() && bits() == 16; }
190  bool is_bfloat16() const { return code() == DataType::kBFloat && bits() == 16; }
192  bool is_int() const { return code() == DataType::kInt; }
194  bool is_uint() const { return code() == DataType::kUInt; }
196  bool is_handle() const { return code() == DataType::kHandle && !is_void(); }
199  int encoded_lanes = static_cast<int16_t>(data_.lanes);
200  return (encoded_lanes < -1) || (1 < encoded_lanes);
201  }
203  bool is_fixed_length_vector() const { return static_cast<int16_t>(data_.lanes) > 1; }
205  bool is_scalable_vector() const { return static_cast<int16_t>(data_.lanes) < -1; }
207  bool is_vector() const { return lanes() > 1; }
211  bool is_void() const {
212  return code() == DataType::kHandle && bits() == 0 && static_cast<int16_t>(data_.lanes) == 0;
213  }
219  DataType with_lanes(int lanes) const { return DataType(data_.code, data_.bits, lanes); }
226  return DataType(data_.code, data_.bits, -vscale_factor);
227  }
233  DataType with_bits(int bits) const { return DataType(data_.code, bits, data_.lanes); }
238  DataType element_of() const { return with_lanes(1); }
242  DataType& operator=(const DataType& rhs) {
243  if (this == &rhs) {
244  return *this;
245  }
246  data_ = rhs.data_;
247  return *this;
248  }
254  bool operator==(const DataType& other) const {
255  return data_.code == other.data_.code && data_.bits == other.data_.bits &&
256  data_.lanes == other.data_.lanes;
257  }
263  bool operator!=(const DataType& other) const { return !operator==(other); }
268  operator DLDataType() const { return data_; }
269 
276  static DataType Int(int bits, int lanes = 1) { return DataType(kDLInt, bits, lanes); }
284  static DataType UInt(int bits, int lanes = 1, bool is_scalable = false) {
285  return DataType(kDLUInt, bits, lanes, is_scalable);
286  }
293  static DataType Float(int bits, int lanes = 1) { return DataType(kDLFloat, bits, lanes); }
300  static DataType BFloat(int bits, int lanes = 1) { return DataType(kDLBfloat, bits, lanes); }
306  static DataType Float8E3M4(int lanes = 1) { return DataType(kFloat8_e3m4, 8, lanes); }
307 
313  static DataType Float8E4M3(int lanes = 1) { return DataType(kFloat8_e4m3, 8, lanes); }
314 
320  static DataType Float8E4M3B11FNUZ(int lanes = 1) {
321  return DataType(kFloat8_e4m3b11fnuz, 8, lanes);
322  }
323 
329  static DataType Float8E4M3FN(int lanes = 1) { return DataType(kFloat8_e4m3fn, 8, lanes); }
330 
336  static DataType Float8E4M3FNUZ(int lanes = 1) { return DataType(kFloat8_e4m3fnuz, 8, lanes); }
337 
343  static DataType Float8E5M2(int lanes = 1) { return DataType(kFloat8_e5m2, 8, lanes); }
344 
350  static DataType Float8E5M2FNUZ(int lanes = 1) { return DataType(kFloat8_e5m2fnuz, 8, lanes); }
351 
357  static DataType Float8E8M0FNU(int lanes = 1) { return DataType(kFloat8_e8m0fnu, 8, lanes); }
358 
364  static DataType Float6E2M3FN(int lanes = 1) { return DataType(kFloat6_e2m3fn, 6, lanes); }
365 
371  static DataType Float6E3M2FN(int lanes = 1) { return DataType(kFloat6_e3m2fn, 6, lanes); }
372 
378  static DataType Float4E2M1FN(int lanes = 1) { return DataType(kFloat4_e2m1fn, 4, lanes); }
385  static DataType Bool(int lanes = 1, bool is_scalable = false) {
386  return DataType(kDLBool, 8, lanes, is_scalable);
387  }
394  static DataType Handle(int bits = 64, int lanes = 1) { return DataType(kHandle, bits, lanes); }
399  static DataType Void() { return DataType(kHandle, 0, 0); }
404  static DataType ShapeIndex() {
405  if (std::is_signed<ffi::Shape::index_type>::value) {
406  return DataType::Int(sizeof(ffi::Shape::index_type) * 8);
407  } else {
408  return DataType::UInt(sizeof(ffi::Shape::index_type) * 8);
409  }
410  }
411 
412  private:
413  DLDataType data_;
414 };
415 
421 inline int GetVectorBytes(DataType dtype) {
422  int data_bits = dtype.bits() * dtype.lanes();
423  // allow bool to exist
424  if (dtype == DataType::Bool() || dtype == DataType::Int(4) || dtype == DataType::UInt(4) ||
425  dtype == DataType::Int(1) || dtype == DataType::Float4E2M1FN() ||
426  dtype == DataType::Float6E2M3FN() || dtype == DataType::Float6E3M2FN()) {
427  return 1;
428  }
429  TVM_FFI_ICHECK_EQ(data_bits % 8, 0U) << "Need to load/store by multiple of bytes";
430  return data_bits / 8;
431 }
432 
440 inline bool TypeMatch(DLDataType t, int code, int bits, int lanes = 1) {
441  return t.code == code && t.bits == bits && t.lanes == lanes;
442 }
448 inline bool TypeEqual(DLDataType lhs, DLDataType rhs) {
449  return lhs.code == rhs.code && lhs.bits == rhs.bits && lhs.lanes == rhs.lanes;
450 }
451 
452 inline std::ostream& operator<<(std::ostream& os, const DataType& dtype) { // NOLINT(*)
453  return os << dtype.operator DLDataType();
454 }
455 } // namespace runtime
456 
458 
459 namespace ffi {
460 
461 // runtime::DataType
462 template <>
463 struct TypeTraits<runtime::DataType> : public TypeTraitsBase {
464  static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIDataType;
465 
466  TVM_FFI_INLINE static void CopyToAnyView(const runtime::DataType& src, TVMFFIAny* result) {
467  // clear padding part to ensure the equality check can always check the v_uint64 part
468  result->v_uint64 = 0;
469  result->zero_padding = 0;
470  result->type_index = TypeIndex::kTVMFFIDataType;
471  result->v_dtype = src;
472  }
473 
474  TVM_FFI_INLINE static void MoveToAny(runtime::DataType src, TVMFFIAny* result) {
475  // clear padding part to ensure the equality check can always check the v_uint64 part
476  result->v_uint64 = 0;
477  result->zero_padding = 0;
478  result->type_index = TypeIndex::kTVMFFIDataType;
479  result->v_dtype = src;
480  }
481 
482  TVM_FFI_INLINE static std::optional<runtime::DataType> TryCastFromAnyView(const TVMFFIAny* src) {
483  auto opt_dtype = TypeTraits<DLDataType>::TryCastFromAnyView(src);
484  if (opt_dtype) {
485  return runtime::DataType(opt_dtype.value());
486  }
487  return std::nullopt;
488  }
489 
490  TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
491  return TypeTraits<DLDataType>::CheckAnyStrict(src);
492  }
493 
494  TVM_FFI_INLINE static runtime::DataType CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
495  return runtime::DataType(TypeTraits<DLDataType>::CopyFromAnyViewAfterCheck(src));
496  }
497 
498  TVM_FFI_INLINE static std::string TypeStr() { return ffi::StaticTypeKey::kTVMFFIDataType; }
499 
500  TVM_FFI_INLINE static std::string TypeSchema() {
501  return R"({"type":")" + std::string(ffi::StaticTypeKey::kTVMFFIDataType) + R"("})";
502  }
503 };
504 
505 } // namespace ffi
506 } // namespace tvm
507 
508 namespace std {
509 template <>
510 struct hash<tvm::DataType> {
511  inline int cantor_pairing_function(int a, int b) const { return (a + b) * (a + b + 1) / 2 + b; }
512  std::size_t operator()(tvm::DataType const& dtype) const {
513  int a = dtype.code();
514  int b = dtype.bits();
515  int c = dtype.lanes();
516  int d = cantor_pairing_function(a, b);
517  return cantor_pairing_function(c, d);
518  }
519 };
520 } // namespace std
521 
522 #endif // TVM_RUNTIME_DATA_TYPE_H_
Runtime primitive data type.
Definition: data_type.h:45
static DataType ShapeIndex()
Get the corresponding type of TVMShapeIndex.
Definition: data_type.h:404
bool is_handle() const
Definition: data_type.h:196
static DataType Float8E4M3FNUZ(int lanes=1)
Construct float8 e4m3fnuz datatype.
Definition: data_type.h:336
bool is_uint() const
Definition: data_type.h:194
int get_lanes_or_vscale_factor() const
Definition: data_type.h:135
bool is_float8_e5m2() const
Definition: data_type.h:176
static DataType Float4E2M1FN(int lanes=1)
Construct float4 e2m1fn datatype.
Definition: data_type.h:378
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:293
bool is_float4_e2m1fn() const
Definition: data_type.h:186
bool is_float6() const
Definition: data_type.h:157
static DataType Float8E8M0FNU(int lanes=1)
Construct float8 e8m0fnu datatype.
Definition: data_type.h:357
DataType element_of() const
Get the scalar version of the type.
Definition: data_type.h:238
bool is_scalable_vector() const
Definition: data_type.h:205
bool is_float8_e4m3() const
Definition: data_type.h:166
DataType & operator=(const DataType &rhs)
Assignment operator.
Definition: data_type.h:242
static DataType Float8E4M3FN(int lanes=1)
Construct float8 e4m3fn datatype.
Definition: data_type.h:329
int bytes() const
Definition: data_type.h:116
TypeCode
Type code for the DataType.
Definition: data_type.h:55
@ kFloat8_e4m3b11fnuz
Definition: data_type.h:64
@ kBool
Definition: data_type.h:61
@ kHandle
Definition: data_type.h:59
@ kUInt
Definition: data_type.h:57
@ kFloat8_e4m3
Definition: data_type.h:63
@ kFloat6_e3m2fn
Definition: data_type.h:71
@ kFloat6_e2m3fn
Definition: data_type.h:70
@ kBFloat
Definition: data_type.h:60
@ kFloat
Definition: data_type.h:58
@ kFloat8_e4m3fn
Definition: data_type.h:65
@ kFloat8_e4m3fnuz
Definition: data_type.h:66
@ kCustomBegin
Definition: data_type.h:73
@ kFloat8_e8m0fnu
Definition: data_type.h:69
@ kFloat4_e2m1fn
Definition: data_type.h:72
@ kInt
Definition: data_type.h:56
@ kFloat8_e3m4
Definition: data_type.h:62
@ kFloat8_e5m2fnuz
Definition: data_type.h:68
@ kFloat8_e5m2
Definition: data_type.h:67
bool is_float6_e3m2fn() const
Definition: data_type.h:184
static DataType Float8E5M2FNUZ(int lanes=1)
Construct float8 e5m2fnuz datatype.
Definition: data_type.h:350
bool is_float8_e3m4() const
Definition: data_type.h:164
bool is_bool() const
Definition: data_type.h:141
static DataType Float8E4M3B11FNUZ(int lanes=1)
Construct float8 e4m3b11fnuz datatype.
Definition: data_type.h:320
bool is_int() const
Definition: data_type.h:192
DataType with_bits(int bits) const
Create a new data type by change bits to a specified value.
Definition: data_type.h:233
bool operator!=(const DataType &other) const
NotEqual comparator.
Definition: data_type.h:263
DataType()
default constructor
Definition: data_type.h:76
bool is_scalable_or_fixed_length_vector() const
Definition: data_type.h:198
int code() const
Definition: data_type.h:112
int lanes() const
Definition: data_type.h:118
static DataType Float8E5M2(int lanes=1)
Construct float8 e5m2 datatype.
Definition: data_type.h:343
DataType(int code, int bits, int lanes, bool is_scalable=false)
Constructor.
Definition: data_type.h:89
bool is_float8_e8m0fnu() const
Definition: data_type.h:180
static DataType Float6E2M3FN(int lanes=1)
Construct float6 e2m3fn datatype.
Definition: data_type.h:364
bool operator==(const DataType &other) const
Equal comparator.
Definition: data_type.h:254
bool is_float16() const
Definition: data_type.h:188
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:385
DataType with_lanes(int lanes) const
Create a new data type by change lanes to a specified value.
Definition: data_type.h:219
static DataType Float6E3M2FN(int lanes=1)
Construct float6 e3m2fn datatype.
Definition: data_type.h:371
int vscale_factor() const
Definition: data_type.h:127
DataType(DLDataType dtype)
Constructor.
Definition: data_type.h:81
bool is_float8_e4m3b11fnuz() const
Definition: data_type.h:168
DataType with_scalable_vscale_factor(int vscale_factor) const
Create a new scalable vector data type by changing the vscale multiplier to a specified value....
Definition: data_type.h:225
bool is_float8_e4m3fnuz() const
Definition: data_type.h:174
bool is_fixed_length_vector() const
Definition: data_type.h:203
bool is_predicate_dtype() const
Definition: data_type.h:143
bool is_bfloat() const
Definition: data_type.h:147
static DataType BFloat(int bits, int lanes=1)
Construct an bfloat type.
Definition: data_type.h:300
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:276
static DataType Void()
Construct a Void type.
Definition: data_type.h:399
bool is_vector() const
Definition: data_type.h:207
bool is_float6_e2m3fn() const
Definition: data_type.h:182
bool is_scalar() const
Definition: data_type.h:139
int bits() const
Definition: data_type.h:114
bool is_float8() const
Definition: data_type.h:149
bool is_bfloat16() const
Definition: data_type.h:190
bool is_float8_e4m3fn() const
Definition: data_type.h:172
bool is_vector_bool() const
Definition: data_type.h:209
bool is_float4() const
Definition: data_type.h:162
static DataType Handle(int bits=64, int lanes=1)
Construct a handle type.
Definition: data_type.h:394
static DataType UInt(int bits, int lanes=1, bool is_scalable=false)
Construct an uint type.
Definition: data_type.h:284
static DataType Float8E4M3(int lanes=1)
Construct float8 e4m3 datatype.
Definition: data_type.h:313
static DataType Float8E3M4(int lanes=1)
Construct float8 e3m4 datatype.
Definition: data_type.h:306
bool is_void() const
Definition: data_type.h:211
bool is_float8_e5m2fnuz() const
Definition: data_type.h:178
bool is_float() const
Definition: data_type.h:145
std::ostream & operator<<(std::ostream &os, const DataType &dtype)
Definition: data_type.h:452
int GetVectorBytes(DataType dtype)
Get the number of bytes needed in a vector.
Definition: data_type.h:421
bool TypeMatch(DLDataType t, int code, int bits, int lanes=1)
Check whether type matches the given spec.
Definition: data_type.h:440
bool TypeEqual(DLDataType lhs, DLDataType rhs)
Check whether two types are equal .
Definition: data_type.h:448
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
runtime::DataType DataType
Definition: data_type.h:457
static TVM_FFI_INLINE void MoveToAny(runtime::DataType src, TVMFFIAny *result)
Definition: data_type.h:474
static TVM_FFI_INLINE void CopyToAnyView(const runtime::DataType &src, TVMFFIAny *result)
Definition: data_type.h:466
static TVM_FFI_INLINE std::string TypeSchema()
Definition: data_type.h:500
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition: data_type.h:490
static TVM_FFI_INLINE runtime::DataType CopyFromAnyViewAfterCheck(const TVMFFIAny *src)
Definition: data_type.h:494
static TVM_FFI_INLINE std::string TypeStr()
Definition: data_type.h:498
static TVM_FFI_INLINE std::optional< runtime::DataType > TryCastFromAnyView(const TVMFFIAny *src)
Definition: data_type.h:482