TensorFlow’s DataType enum, from types.proto, names the
element type of every tensor and the type attributes of ops (T,
dtype), such as the dtype of each Const node
in a frozen graph. Every
name has the prefix DT_. It is a different list from ONNX’s: DT_FLOAT is 1
in both, but DT_INT32 is 3 here and INT32 is 6 in ONNX.
The data types
| Value | Name | Bytes in tensor_content | Typed field |
|---|---|---|---|
| 0 | DT_INVALID | – | – (not allowed in a tensor) |
| 1 | DT_FLOAT | 4 | float_val |
| 2 | DT_DOUBLE | 8 | double_val |
| 3 | DT_INT32 | 4 | int_val |
| 4 | DT_UINT8 | 1 | int_val |
| 5 | DT_INT16 | 2 | int_val |
| 6 | DT_INT8 | 1 | int_val |
| 7 | DT_STRING | varint lengths, then bytes | string_val |
| 8 | DT_COMPLEX64 | 8 | scomplex_val |
| 9 | DT_INT64 | 8 | int64_val |
| 10 | DT_BOOL | 1 | bool_val |
| 11 | DT_QINT8 | 1 | quantised signed 8-bit |
| 12 | DT_QUINT8 | 1 | quantised unsigned 8-bit |
| 13 | DT_QINT32 | 4 | quantised signed 32-bit |
| 14 | DT_BFLOAT16 | 2 | half_val |
| 15 | DT_QINT16 | 2 | quantised signed 16-bit |
| 16 | DT_QUINT16 | 2 | quantised unsigned 16-bit |
| 17 | DT_UINT16 | 2 | int_val |
| 18 | DT_COMPLEX128 | 16 | dcomplex_val |
| 19 | DT_HALF | 2 | half_val |
| 20 | DT_RESOURCE | – | resource_handle_val |
| 21 | DT_VARIANT | varint lengths, then serialised VariantTensorDataProto messages | variant_val |
| 22 | DT_UINT32 | 4 | uint32_val |
| 23 | DT_UINT64 | 8 | uint64_val |
| 24 | DT_FLOAT8_E5M2 | 1 | float8_val |
| 25 | DT_FLOAT8_E4M3FN | 1 | float8_val |
| 26 | DT_FLOAT8_E4M3FNUZ | 1 | float8_val |
| 27 | DT_FLOAT8_E4M3B11FNUZ | 1 | float8_val |
| 28 | DT_FLOAT8_E5M2FNUZ | 1 | float8_val |
| 29 | DT_INT4 | 1 | int_val, one value each |
| 30 | DT_UINT4 | 1 | int_val, one value each |
| 31 | DT_INT2 | 1 | int_val, one value each |
| 32 | DT_UINT2 | 1 | int_val, one value each |
| 33 | DT_FLOAT4_E2M1FN | 1 | float8_val, one byte each |
| 34 | DT_FLOAT8_E8M0FNU | 1 | float8_val |
The typed field is the one tensor.proto’s comments name for that type. The comments
name no field for the quantised types: TensorFlow’s tensor.cc stores
DT_QINT8, DT_QUINT8, DT_QINT16, DT_QUINT16 and DT_QINT32 in int_val.
The same source stores the 4-bit and 2-bit integers one value to an
int_val entry, and DT_FLOAT4_E2M1FN one value to a byte of
float8_val, unpacked, not two to a byte as in ONNX. Whatever the type,
tensor_content may be used instead of the typed field, and the explorer
reads it with the sizes above; in memory these low-bit types take one byte each,
which is how tensor_content, a copy of the memory, holds them. The
explorer decodes all of them.
DT_STRING and DT_VARIANT have no fixed size. In
tensor_content they are written as a string list
(tensor_coding.cc, EncodeStringList): a varint32 length for
every element first, then all the elements’ bytes one after another. A variant
element is its value encoded as a VariantTensorDataProto
(variant.cc, EncodeVariantList). The explorer reads both: the
strings as text, and each variant as a VariantTensorDataProto.
The float8 types
DT_FLOAT8_E5M2, DT_FLOAT8_E4M3FN, DT_FLOAT8_E4M3FNUZ and DT_FLOAT8_E5M2FNUZ are the same 8-bit float formats as ONNX’s of those names: a sign bit, then 5 and 2 or 4 and 3 exponent and mantissa bits. DT_FLOAT8_E4M3B11FNUZ is the E4M3 layout with an exponent bias of 11 and no negative zero, as its name says. DT_FLOAT8_E8M0FNU is a power of two with no sign and no mantissa, the scale type of the OCP Microscaling formats.
Reference types: 101 to 134
Each type also has a reference form, its number plus 100, named with
_REF at the end: DT_FLOAT_REF is 101, DT_INT32_REF 103 and so on up to
134. types.proto marks them “Do not use!”: they belong to TensorFlow 1’s reference
variables, and appear in the type attributes of old graphs, not as the type of a
stored tensor. The explorer names them.