24 #ifndef TVM_RUNTIME_DATA_TYPE_H_
25 #define TVM_RUNTIME_DATA_TYPE_H_
28 #include <tvm/runtime/logging.h>
32 #include <type_traits>
69 explicit DataType(DLDataType dtype) : data_(dtype) {}
78 data_.code =
static_cast<uint8_t
>(
code);
79 data_.bits =
static_cast<uint8_t
>(
bits);
81 ICHECK(
lanes > 1) <<
"Invalid value for vscale factor" <<
lanes;
83 data_.lanes = is_scalable ?
static_cast<uint16_t
>(-
lanes) :
static_cast<uint16_t
>(
lanes);
92 int code()
const {
return static_cast<int>(data_.code); }
94 int bits()
const {
return static_cast<int>(data_.bits); }
99 int lanes_as_int =
static_cast<int16_t
>(data_.lanes);
100 if (lanes_as_int < 0) {
101 LOG(FATAL) <<
"Can't fetch the lanes of a scalable vector at a compile time.";
107 int lanes_as_int =
static_cast<int16_t
>(data_.lanes);
108 if (lanes_as_int >= -1) {
109 LOG(FATAL) <<
"A fixed length vector doesn't have a vscale factor.";
111 return -lanes_as_int;
144 int encoded_lanes =
static_cast<int16_t
>(data_.lanes);
145 return (encoded_lanes < -1) || (1 < encoded_lanes);
198 return data_.code == other.data_.code && data_.bits == other.data_.bits &&
199 data_.lanes == other.data_.lanes;
211 operator DLDataType()
const {
return data_; }
282 if (std::is_signed<tvm_index_t>::value) {
299 int data_bits = dtype.
bits() * dtype.
lanes();
305 ICHECK_EQ(data_bits % 8, 0U) <<
"Need to load/store by multiple of bytes";
306 return data_bits / 8;
316 inline bool TypeMatch(DLDataType t,
int code,
int bits,
int lanes = 1) {
317 return t.code == code && t.bits == bits && t.lanes == lanes;
325 return lhs.code == rhs.code && lhs.bits == rhs.bits && lhs.lanes == rhs.lanes;
373 switch (
static_cast<int>(type_code)) {
389 LOG(FATAL) <<
"unknown type_code=" <<
static_cast<int>(type_code);
394 inline std::ostream&
operator<<(std::ostream& os, DLDataType t) {
395 if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) {
403 os << DLDataTypeCode2Str(static_cast<DLDataTypeCode>(t.code));
408 int16_t lanes =
static_cast<int16_t
>(t.lanes);
409 os << static_cast<int>(t.bits);
412 }
else if (lanes < -1) {
413 os <<
"xvscalex" << -lanes;
419 return os << dtype.operator DLDataType();
423 if (t.bits == 0)
return "";
424 std::ostringstream os;
432 if (s.length() == 0 || s ==
"void") {
439 if (s.substr(0, 3) ==
"int") {
441 scan = s.c_str() + 3;
442 }
else if (s.substr(0, 4) ==
"uint") {
444 scan = s.c_str() + 4;
445 }
else if (s.substr(0, 5) ==
"float") {
447 scan = s.c_str() + 5;
448 }
else if (s.substr(0, 6) ==
"handle") {
451 scan = s.c_str() + 6;
452 }
else if (s ==
"bool") {
457 }
else if (s.substr(0, 6) ==
"bfloat") {
460 scan = s.c_str() + 6;
461 }
else if (s.substr(0, 10) ==
"e4m3_float") {
464 scan = s.c_str() + 10;
465 }
else if (s.substr(0, 10) ==
"e5m2_float") {
468 scan = s.c_str() + 10;
469 }
else if (s.substr(0, 6) ==
"custom") {
473 LOG(FATAL) <<
"unknown type " << s;
476 uint8_t bits =
static_cast<uint8_t
>(strtoul(
scan, &xdelim, 10));
477 if (bits != 0) t.bits = bits;
478 int scalable_multiplier = 1;
479 if (strncmp(xdelim,
"xvscale", 7) == 0) {
480 scalable_multiplier = -1;
483 char* endpt = xdelim;
484 if (*xdelim ==
'x') {
485 t.lanes =
static_cast<uint16_t
>(scalable_multiplier * strtoul(xdelim + 1, &endpt, 10));
487 ICHECK(endpt == s.c_str() + s.length()) <<
"unknown type " << s;
500 inline int cantor_pairing_function(
int a,
int b)
const {
return (a + b) * (a + b + 1) / 2 + b; }
502 int a = dtype.
code();
503 int b = dtype.
bits();
504 int c = dtype.
lanes();
505 int d = cantor_pairing_function(a, b);
506 return cantor_pairing_function(c, d);
@ kTVMOpaqueHandle
Definition: c_runtime_api.h:178
int64_t tvm_index_t
type of array index.
Definition: c_runtime_api.h:89
Runtime primitive data type.
Definition: data_type.h:43
static DataType ShapeIndex()
Get the corresponding type of TVMShapeIndex.
Definition: data_type.h:281
bool is_handle() const
Definition: data_type.h:141
bool is_uint() const
Definition: data_type.h:139
int get_lanes_or_vscale_factor() const
Definition: data_type.h:114
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:236
DataType element_of() const
Get the scalar version of the type.
Definition: data_type.h:181
bool is_scalable_vector() const
Definition: data_type.h:150
DataType & operator=(const DataType &rhs)
Assignment operator.
Definition: data_type.h:185
int bytes() const
Definition: data_type.h:96
TypeCode
Type code for the DataType.
Definition: data_type.h:53
@ kHandle
Definition: data_type.h:57
@ kUInt
Definition: data_type.h:55
@ kBFloat
Definition: data_type.h:58
@ kFloat
Definition: data_type.h:56
@ kE4M3Float
Definition: data_type.h:59
@ kCustomBegin
Definition: data_type.h:61
@ kE5M2Float
Definition: data_type.h:60
@ kInt
Definition: data_type.h:54
static DataType NVFloat8E4M3(int lanes=1)
Construct NV float8 e4m3 datatype.
Definition: data_type.h:249
bool is_bool() const
Definition: data_type.h:120
bool is_int() const
Definition: data_type.h:137
bool is_e4m3_float8() const
Definition: data_type.h:129
DataType with_bits(int bits) const
Create a new data type by change bits to a specified value.
Definition: data_type.h:176
static DataType NVFloat8E5M2(int lanes=1)
Construct NV float8 e5m2 datatype.
Definition: data_type.h:255
bool operator!=(const DataType &other) const
NotEqual comparator.
Definition: data_type.h:206
DataType()
default constructor
Definition: data_type.h:64
bool is_scalable_or_fixed_length_vector() const
Definition: data_type.h:143
int code() const
Definition: data_type.h:92
int lanes() const
Definition: data_type.h:98
DataType(int code, int bits, int lanes, bool is_scalable=false)
Constructor.
Definition: data_type.h:77
bool operator==(const DataType &other) const
Equal comparator.
Definition: data_type.h:197
bool is_float16() const
Definition: data_type.h:133
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:262
DataType with_lanes(int lanes) const
Create a new data type by change lanes to a specified value.
Definition: data_type.h:162
int vscale_factor() const
Definition: data_type.h:106
DataType(DLDataType dtype)
Constructor.
Definition: data_type.h:69
bool is_e5m2_float8() const
Definition: data_type.h:131
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:168
bool is_fixed_length_vector() const
Definition: data_type.h:148
static DataType BFloat(int bits, int lanes=1)
Construct an bfloat type.
Definition: data_type.h:243
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:219
static DataType Void()
Construct a Void type.
Definition: data_type.h:276
bool is_vector() const
Definition: data_type.h:152
bool is_scalar() const
Definition: data_type.h:118
int bits() const
Definition: data_type.h:94
bool is_float8() const
Definition: data_type.h:124
bool is_bfloat16() const
Definition: data_type.h:135
bool is_vector_bool() const
Definition: data_type.h:154
static DataType Handle(int bits=64, int lanes=1)
Construct a handle type.
Definition: data_type.h:271
static DataType UInt(int bits, int lanes=1, bool is_scalable=false)
Construct an uint type.
Definition: data_type.h:227
bool is_void() const
Definition: data_type.h:156
bool is_float() const
Definition: data_type.h:122
std::string GetCustomTypeName(uint8_t type_code)
Runtime utility for getting custom type name from code.
bool GetCustomTypeRegistered(uint8_t type_code)
Runtime utility for checking whether custom type is registered.
DLDataType String2DLDataType(std::string s)
convert a string to TVM type.
Definition: data_type.h:429
std::string DLDataType2String(DLDataType t)
convert a TVM type to string.
Definition: data_type.h:422
uint8_t ParseCustomDatatype(const std::string &s, const char **scan)
Runtime utility for parsing string of the form "custom[<typename>]".
int GetVectorBytes(DataType dtype)
Get the number of bytes needed in a vector.
Definition: data_type.h:298
bool TypeMatch(DLDataType t, int code, int bits, int lanes=1)
Check whether type matches the given spec.
Definition: data_type.h:316
bool TypeEqual(DLDataType lhs, DLDataType rhs)
Check whether two types are equal .
Definition: data_type.h:324
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
const char * DLDataTypeCode2Str(DLDataTypeCode type_code)
Convert type code to its name.
Definition: data_type.h:372
Array< Tensor > scan(Array< Tensor > init, Array< Tensor > update, Array< Tensor > state_placeholder, Array< Tensor > inputs=Array< Tensor >(), std::string name="scan", std::string tag="", Map< String, ObjectRef > attrs={})
Construct new tensors by scan.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
runtime::DataType DataType
Definition: data_type.h:493