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/runtime/base.h>
30 #include <tvm/runtime/logging.h>
31 
32 #include <cstring>
33 #include <string>
34 #include <type_traits>
35 
36 namespace tvm {
37 namespace runtime {
38 
39 using tvm_index_t = ffi::Shape::index_type;
40 
47 class DataType {
48  public:
57  enum TypeCode {
58  kInt = kDLInt,
59  kUInt = kDLUInt,
60  kFloat = kDLFloat,
61  kHandle = kDLOpaqueHandle,
62  kBFloat = kDLBfloat,
63  kBool = kDLBool,
64  kFloat8_e3m4 = kDLFloat8_e3m4,
65  kFloat8_e4m3 = kDLFloat8_e4m3,
66  kFloat8_e4m3b11fnuz = kDLFloat8_e4m3b11fnuz,
67  kFloat8_e4m3fn = kDLFloat8_e4m3fn,
68  kFloat8_e4m3fnuz = kDLFloat8_e4m3fnuz,
69  kFloat8_e5m2 = kDLFloat8_e5m2,
70  kFloat8_e5m2fnuz = kDLFloat8_e5m2fnuz,
71  kFloat8_e8m0fnu = kDLFloat8_e8m0fnu,
72  kFloat6_e2m3fn = kDLFloat6_e2m3fn,
73  kFloat6_e3m2fn = kDLFloat6_e3m2fn,
74  kFloat4_e2m1fn = kDLFloat4_e2m1fn,
75  kCustomBegin = 129
76  };
78  DataType() { data_ = DataType::Void(); }
83  explicit DataType(DLDataType dtype) : data_(dtype) {}
91  DataType(int code, int bits, int lanes, bool is_scalable = false) {
92  data_.code = static_cast<uint8_t>(code);
93  data_.bits = static_cast<uint8_t>(bits);
94  if (is_scalable) {
95  ICHECK(lanes > 1) << "Invalid value for vscale factor" << lanes;
96  }
97  data_.lanes = is_scalable ? static_cast<uint16_t>(-lanes) : static_cast<uint16_t>(lanes);
98  if (code == kBFloat) {
99  ICHECK_EQ(bits, 16);
100  }
104  ICHECK_EQ(bits, 8);
105  }
106  if (code == kFloat6_e2m3fn || code == kFloat6_e3m2fn) {
107  ICHECK_EQ(bits, 6);
108  }
109  if (code == kFloat4_e2m1fn) {
110  ICHECK_EQ(bits, 4);
111  }
112  }
114  int code() const { return static_cast<int>(data_.code); }
116  int bits() const { return static_cast<int>(data_.bits); }
118  int bytes() const { return (bits() + 7) / 8; }
120  int lanes() const {
121  int lanes_as_int = static_cast<int16_t>(data_.lanes);
122  if (lanes_as_int < 0) {
123  LOG(FATAL) << "Can't fetch the lanes of a scalable vector at a compile time.";
124  }
125  return lanes_as_int;
126  }
128  int vscale_factor() const {
129  int lanes_as_int = static_cast<int16_t>(data_.lanes);
130  if (lanes_as_int >= -1) {
131  LOG(FATAL) << "A fixed length vector doesn't have a vscale factor.";
132  }
133  return -lanes_as_int;
134  }
137  return is_scalable_vector() ? vscale_factor() : lanes();
138  }
140  bool is_scalar() const { return !is_scalable_vector() && lanes() == 1; }
142  bool is_bool() const { return code() == DataType::kBool; }
144  bool is_predicate_dtype() const { return is_bool() || (is_uint() && bits() == 1); }
146  bool is_float() const { return code() == DataType::kFloat; }
148  bool is_bfloat() const { return code() == DataType::kBFloat; }
150  bool is_float8() const {
151  return bits() == 8 &&
156  }
158  bool is_float6() const {
159  return bits() == 6 &&
161  }
163  bool is_float4() const { return bits() == 4 && code() == DataType::kFloat4_e2m1fn; }
165  bool is_float8_e3m4() const { return bits() == 8 && code() == DataType::kFloat8_e3m4; }
167  bool is_float8_e4m3() const { return bits() == 8 && code() == DataType::kFloat8_e4m3; }
169  bool is_float8_e4m3b11fnuz() const {
170  return bits() == 8 && code() == DataType::kFloat8_e4m3b11fnuz;
171  }
173  bool is_float8_e4m3fn() const { return bits() == 8 && code() == DataType::kFloat8_e4m3fn; }
175  bool is_float8_e4m3fnuz() const { return bits() == 8 && code() == DataType::kFloat8_e4m3fnuz; }
177  bool is_float8_e5m2() const { return bits() == 8 && code() == DataType::kFloat8_e5m2; }
179  bool is_float8_e5m2fnuz() const { return bits() == 8 && code() == DataType::kFloat8_e5m2fnuz; }
181  bool is_float8_e8m0fnu() const { return bits() == 8 && code() == DataType::kFloat8_e8m0fnu; }
183  bool is_float6_e2m3fn() const { return bits() == 6 && code() == DataType::kFloat6_e2m3fn; }
185  bool is_float6_e3m2fn() const { return bits() == 6 && code() == DataType::kFloat6_e3m2fn; }
187  bool is_float4_e2m1fn() const { return bits() == 4 && code() == DataType::kFloat4_e2m1fn; }
189  bool is_float16() const { return is_float() && bits() == 16; }
191  bool is_bfloat16() const { return code() == DataType::kBFloat && bits() == 16; }
193  bool is_int() const { return code() == DataType::kInt; }
195  bool is_uint() const { return code() == DataType::kUInt; }
197  bool is_handle() const { return code() == DataType::kHandle && !is_void(); }
200  int encoded_lanes = static_cast<int16_t>(data_.lanes);
201  return (encoded_lanes < -1) || (1 < encoded_lanes);
202  }
204  bool is_fixed_length_vector() const { return static_cast<int16_t>(data_.lanes) > 1; }
206  bool is_scalable_vector() const { return static_cast<int16_t>(data_.lanes) < -1; }
208  bool is_vector() const { return lanes() > 1; }
212  bool is_void() const {
213  return code() == DataType::kHandle && bits() == 0 && static_cast<int16_t>(data_.lanes) == 0;
214  }
220  DataType with_lanes(int lanes) const { return DataType(data_.code, data_.bits, lanes); }
227  return DataType(data_.code, data_.bits, -vscale_factor);
228  }
234  DataType with_bits(int bits) const { return DataType(data_.code, bits, data_.lanes); }
239  DataType element_of() const { return with_lanes(1); }
243  DataType& operator=(const DataType& rhs) {
244  if (this == &rhs) {
245  return *this;
246  }
247  data_ = rhs.data_;
248  return *this;
249  }
255  bool operator==(const DataType& other) const {
256  return data_.code == other.data_.code && data_.bits == other.data_.bits &&
257  data_.lanes == other.data_.lanes;
258  }
264  bool operator!=(const DataType& other) const { return !operator==(other); }
269  operator DLDataType() const { return data_; }
270 
277  static DataType Int(int bits, int lanes = 1) { return DataType(kDLInt, bits, lanes); }
285  static DataType UInt(int bits, int lanes = 1, bool is_scalable = false) {
286  return DataType(kDLUInt, bits, lanes, is_scalable);
287  }
294  static DataType Float(int bits, int lanes = 1) { return DataType(kDLFloat, bits, lanes); }
301  static DataType BFloat(int bits, int lanes = 1) { return DataType(kDLBfloat, bits, lanes); }
307  static DataType Float8E3M4(int lanes = 1) { return DataType(kFloat8_e3m4, 8, lanes); }
308 
314  static DataType Float8E4M3(int lanes = 1) { return DataType(kFloat8_e4m3, 8, lanes); }
315 
321  static DataType Float8E4M3B11FNUZ(int lanes = 1) {
322  return DataType(kFloat8_e4m3b11fnuz, 8, lanes);
323  }
324 
330  static DataType Float8E4M3FN(int lanes = 1) { return DataType(kFloat8_e4m3fn, 8, lanes); }
331 
337  static DataType Float8E4M3FNUZ(int lanes = 1) { return DataType(kFloat8_e4m3fnuz, 8, lanes); }
338 
344  static DataType Float8E5M2(int lanes = 1) { return DataType(kFloat8_e5m2, 8, lanes); }
345 
351  static DataType Float8E5M2FNUZ(int lanes = 1) { return DataType(kFloat8_e5m2fnuz, 8, lanes); }
352 
358  static DataType Float8E8M0FNU(int lanes = 1) { return DataType(kFloat8_e8m0fnu, 8, lanes); }
359 
365  static DataType Float6E2M3FN(int lanes = 1) { return DataType(kFloat6_e2m3fn, 6, lanes); }
366 
372  static DataType Float6E3M2FN(int lanes = 1) { return DataType(kFloat6_e3m2fn, 6, lanes); }
373 
379  static DataType Float4E2M1FN(int lanes = 1) { return DataType(kFloat4_e2m1fn, 4, lanes); }
386  static DataType Bool(int lanes = 1, bool is_scalable = false) {
387  return DataType(kDLBool, 8, lanes, is_scalable);
388  }
395  static DataType Handle(int bits = 64, int lanes = 1) { return DataType(kHandle, bits, lanes); }
400  static DataType Void() { return DataType(kHandle, 0, 0); }
405  static DataType ShapeIndex() {
406  if (std::is_signed<tvm_index_t>::value) {
407  return DataType::Int(sizeof(tvm_index_t) * 8);
408  } else {
409  return DataType::UInt(sizeof(tvm_index_t) * 8);
410  }
411  }
412 
413  private:
414  DLDataType data_;
415 };
416 
422 inline int GetVectorBytes(DataType dtype) {
423  int data_bits = dtype.bits() * dtype.lanes();
424  // allow bool to exist
425  if (dtype == DataType::Bool() || dtype == DataType::Int(4) || dtype == DataType::UInt(4) ||
426  dtype == DataType::Int(1) || dtype == DataType::Float4E2M1FN() ||
427  dtype == DataType::Float6E2M3FN() || dtype == DataType::Float6E3M2FN()) {
428  return 1;
429  }
430  ICHECK_EQ(data_bits % 8, 0U) << "Need to load/store by multiple of bytes";
431  return data_bits / 8;
432 }
433 
441 inline bool TypeMatch(DLDataType t, int code, int bits, int lanes = 1) {
442  return t.code == code && t.bits == bits && t.lanes == lanes;
443 }
449 inline bool TypeEqual(DLDataType lhs, DLDataType rhs) {
450  return lhs.code == rhs.code && lhs.bits == rhs.bits && lhs.lanes == rhs.lanes;
451 }
452 
453 using ffi::DLDataTypeToString;
454 using ffi::StringToDLDataType;
455 
456 inline std::ostream& operator<<(std::ostream& os, const DataType& dtype) { // NOLINT(*)
457  return os << dtype.operator DLDataType();
458 }
459 } // namespace runtime
460 
462 
463 namespace ffi {
464 
465 // runtime::DataType
466 template <>
467 struct TypeTraits<runtime::DataType> : public TypeTraitsBase {
468  static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIDataType;
469 
470  TVM_FFI_INLINE static void CopyToAnyView(const runtime::DataType& src, TVMFFIAny* result) {
471  // clear padding part to ensure the equality check can always check the v_uint64 part
472  result->v_uint64 = 0;
473  result->zero_padding = 0;
474  result->type_index = TypeIndex::kTVMFFIDataType;
475  result->v_dtype = src;
476  }
477 
478  TVM_FFI_INLINE static void MoveToAny(runtime::DataType src, TVMFFIAny* result) {
479  // clear padding part to ensure the equality check can always check the v_uint64 part
480  result->v_uint64 = 0;
481  result->zero_padding = 0;
482  result->type_index = TypeIndex::kTVMFFIDataType;
483  result->v_dtype = src;
484  }
485 
486  TVM_FFI_INLINE static std::optional<runtime::DataType> TryCastFromAnyView(const TVMFFIAny* src) {
487  auto opt_dtype = TypeTraits<DLDataType>::TryCastFromAnyView(src);
488  if (opt_dtype) {
489  return runtime::DataType(opt_dtype.value());
490  }
491  return std::nullopt;
492  }
493 
494  TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
495  return TypeTraits<DLDataType>::CheckAnyStrict(src);
496  }
497 
498  TVM_FFI_INLINE static runtime::DataType CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
499  return runtime::DataType(TypeTraits<DLDataType>::CopyFromAnyViewAfterCheck(src));
500  }
501 
502  TVM_FFI_INLINE static std::string TypeStr() { return ffi::StaticTypeKey::kTVMFFIDataType; }
503 
504  TVM_FFI_INLINE static std::string TypeSchema() {
505  return R"({"type":")" + std::string(ffi::StaticTypeKey::kTVMFFIDataType) + R"("})";
506  }
507 };
508 
509 } // namespace ffi
510 } // namespace tvm
511 
512 namespace std {
513 template <>
514 struct hash<tvm::DataType> {
515  inline int cantor_pairing_function(int a, int b) const { return (a + b) * (a + b + 1) / 2 + b; }
516  std::size_t operator()(tvm::DataType const& dtype) const {
517  int a = dtype.code();
518  int b = dtype.bits();
519  int c = dtype.lanes();
520  int d = cantor_pairing_function(a, b);
521  return cantor_pairing_function(c, d);
522  }
523 };
524 } // namespace std
525 
526 #endif // TVM_RUNTIME_DATA_TYPE_H_
Runtime primitive data type.
Definition: data_type.h:47
static DataType ShapeIndex()
Get the corresponding type of TVMShapeIndex.
Definition: data_type.h:405
bool is_handle() const
Definition: data_type.h:197
static DataType Float8E4M3FNUZ(int lanes=1)
Construct float8 e4m3fnuz datatype.
Definition: data_type.h:337
bool is_uint() const
Definition: data_type.h:195
int get_lanes_or_vscale_factor() const
Definition: data_type.h:136
bool is_float8_e5m2() const
Definition: data_type.h:177
static DataType Float4E2M1FN(int lanes=1)
Construct float4 e2m1fn datatype.
Definition: data_type.h:379
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:294
bool is_float4_e2m1fn() const
Definition: data_type.h:187
bool is_float6() const
Definition: data_type.h:158
static DataType Float8E8M0FNU(int lanes=1)
Construct float8 e8m0fnu datatype.
Definition: data_type.h:358
DataType element_of() const
Get the scalar version of the type.
Definition: data_type.h:239
bool is_scalable_vector() const
Definition: data_type.h:206
bool is_float8_e4m3() const
Definition: data_type.h:167
DataType & operator=(const DataType &rhs)
Assignment operator.
Definition: data_type.h:243
static DataType Float8E4M3FN(int lanes=1)
Construct float8 e4m3fn datatype.
Definition: data_type.h:330
int bytes() const
Definition: data_type.h:118
TypeCode
Type code for the DataType.
Definition: data_type.h:57
@ kFloat8_e4m3b11fnuz
Definition: data_type.h:66
@ kBool
Definition: data_type.h:63
@ kHandle
Definition: data_type.h:61
@ kUInt
Definition: data_type.h:59
@ kFloat8_e4m3
Definition: data_type.h:65
@ kFloat6_e3m2fn
Definition: data_type.h:73
@ kFloat6_e2m3fn
Definition: data_type.h:72
@ kBFloat
Definition: data_type.h:62
@ kFloat
Definition: data_type.h:60
@ kFloat8_e4m3fn
Definition: data_type.h:67
@ kFloat8_e4m3fnuz
Definition: data_type.h:68
@ kCustomBegin
Definition: data_type.h:75
@ kFloat8_e8m0fnu
Definition: data_type.h:71
@ kFloat4_e2m1fn
Definition: data_type.h:74
@ kInt
Definition: data_type.h:58
@ kFloat8_e3m4
Definition: data_type.h:64
@ kFloat8_e5m2fnuz
Definition: data_type.h:70
@ kFloat8_e5m2
Definition: data_type.h:69
bool is_float6_e3m2fn() const
Definition: data_type.h:185
static DataType Float8E5M2FNUZ(int lanes=1)
Construct float8 e5m2fnuz datatype.
Definition: data_type.h:351
bool is_float8_e3m4() const
Definition: data_type.h:165
bool is_bool() const
Definition: data_type.h:142
static DataType Float8E4M3B11FNUZ(int lanes=1)
Construct float8 e4m3b11fnuz datatype.
Definition: data_type.h:321
bool is_int() const
Definition: data_type.h:193
DataType with_bits(int bits) const
Create a new data type by change bits to a specified value.
Definition: data_type.h:234
bool operator!=(const DataType &other) const
NotEqual comparator.
Definition: data_type.h:264
DataType()
default constructor
Definition: data_type.h:78
bool is_scalable_or_fixed_length_vector() const
Definition: data_type.h:199
int code() const
Definition: data_type.h:114
int lanes() const
Definition: data_type.h:120
static DataType Float8E5M2(int lanes=1)
Construct float8 e5m2 datatype.
Definition: data_type.h:344
DataType(int code, int bits, int lanes, bool is_scalable=false)
Constructor.
Definition: data_type.h:91
bool is_float8_e8m0fnu() const
Definition: data_type.h:181
static DataType Float6E2M3FN(int lanes=1)
Construct float6 e2m3fn datatype.
Definition: data_type.h:365
bool operator==(const DataType &other) const
Equal comparator.
Definition: data_type.h:255
bool is_float16() const
Definition: data_type.h:189
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:386
DataType with_lanes(int lanes) const
Create a new data type by change lanes to a specified value.
Definition: data_type.h:220
static DataType Float6E3M2FN(int lanes=1)
Construct float6 e3m2fn datatype.
Definition: data_type.h:372
int vscale_factor() const
Definition: data_type.h:128
DataType(DLDataType dtype)
Constructor.
Definition: data_type.h:83
bool is_float8_e4m3b11fnuz() const
Definition: data_type.h:169
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:226
bool is_float8_e4m3fnuz() const
Definition: data_type.h:175
bool is_fixed_length_vector() const
Definition: data_type.h:204
bool is_predicate_dtype() const
Definition: data_type.h:144
bool is_bfloat() const
Definition: data_type.h:148
static DataType BFloat(int bits, int lanes=1)
Construct an bfloat type.
Definition: data_type.h:301
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:277
static DataType Void()
Construct a Void type.
Definition: data_type.h:400
bool is_vector() const
Definition: data_type.h:208
bool is_float6_e2m3fn() const
Definition: data_type.h:183
bool is_scalar() const
Definition: data_type.h:140
int bits() const
Definition: data_type.h:116
bool is_float8() const
Definition: data_type.h:150
bool is_bfloat16() const
Definition: data_type.h:191
bool is_float8_e4m3fn() const
Definition: data_type.h:173
bool is_vector_bool() const
Definition: data_type.h:210
bool is_float4() const
Definition: data_type.h:163
static DataType Handle(int bits=64, int lanes=1)
Construct a handle type.
Definition: data_type.h:395
static DataType UInt(int bits, int lanes=1, bool is_scalable=false)
Construct an uint type.
Definition: data_type.h:285
static DataType Float8E4M3(int lanes=1)
Construct float8 e4m3 datatype.
Definition: data_type.h:314
static DataType Float8E3M4(int lanes=1)
Construct float8 e3m4 datatype.
Definition: data_type.h:307
bool is_void() const
Definition: data_type.h:212
bool is_float8_e5m2fnuz() const
Definition: data_type.h:179
bool is_float() const
Definition: data_type.h:146
std::ostream & operator<<(std::ostream &os, const DataType &dtype)
Definition: data_type.h:456
ffi::Shape::index_type tvm_index_t
Definition: data_type.h:39
int GetVectorBytes(DataType dtype)
Get the number of bytes needed in a vector.
Definition: data_type.h:422
bool TypeMatch(DLDataType t, int code, int bits, int lanes=1)
Check whether type matches the given spec.
Definition: data_type.h:441
bool TypeEqual(DLDataType lhs, DLDataType rhs)
Check whether two types are equal .
Definition: data_type.h:449
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
runtime::DataType DataType
Definition: data_type.h:461
static TVM_FFI_INLINE void MoveToAny(runtime::DataType src, TVMFFIAny *result)
Definition: data_type.h:478
static TVM_FFI_INLINE void CopyToAnyView(const runtime::DataType &src, TVMFFIAny *result)
Definition: data_type.h:470
static TVM_FFI_INLINE std::string TypeSchema()
Definition: data_type.h:504
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition: data_type.h:494
static TVM_FFI_INLINE runtime::DataType CopyFromAnyViewAfterCheck(const TVMFFIAny *src)
Definition: data_type.h:498
static TVM_FFI_INLINE std::string TypeStr()
Definition: data_type.h:502
static TVM_FFI_INLINE std::optional< runtime::DataType > TryCastFromAnyView(const TVMFFIAny *src)
Definition: data_type.h:486