A torch.dtype is an object that represents the data type of a torch.Tensor. PyTorch has twelve different data types:
Data type | dtype | Legacy Constructors |
---|---|---|
32-bit floating point | torch.float32 or torch.float | torch.*.FloatTensor |
64-bit floating point | torch.float64 or torch.double | torch.*.DoubleTensor |
64-bit complex | torch.complex64 or torch.cfloat | |
128-bit complex | torch.complex128 or torch.cdouble | |
16-bit floating point 1 | torch.float16 or torch.half | torch.*.HalfTensor |
16-bit floating point 2 | torch.bfloat16 | torch.*.BFloat16Tensor |
8-bit integer (unsigned) | torch.uint8 | torch.*.ByteTensor |
8-bit integer (signed) | torch.int8 | torch.*.CharTensor |
16-bit integer (signed) | torch.int16 or torch.short | torch.*.ShortTensor |
32-bit integer (signed) | torch.int32 or torch.int | torch.*.IntTensor |
64-bit integer (signed) | torch.int64 or torch.long | torch.*.LongTensor |
Boolean | torch.bool | torch.*.BoolTensor |
1
Sometimes referred to as binary16: uses 1 sign, 5 exponent, and 10 significand bits. Useful when precision is important.
2
Sometimes referred to as Brain Floating Point: use 1 sign, 8 exponent and 7 significand bits. Useful when range is important, since it has the same number of exponent bits as float32
**To find out if a **[torch.dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.torch.dtype)** is a floating point data type, the property **[is_floating_point](https://pytorch.org/docs/stable/generated/torch.is_floating_point.html#torch.is_floating_point)** can be used, which returns True if the data type is a floating point data type.**<br />**To find out if a **[torch.dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.torch.dtype)** is a complex data type, the property **[is_complex](https://pytorch.org/docs/stable/generated/torch.is_complex.html#torch.is_complex)** can be used, which returns True if the data type is a complex data type.**<br />**When the dtypes of inputs to an arithmetic operation (add, sub, div, mul) differ, we promote by finding the minimum dtype that satisfies the following rules:**
- If the type of a scalar operand is of a higher category than tensor operands (where complex > floating > integral > boolean), we promote to a type with sufficient size to hold all scalar operands of that category.
- If a zero-dimension tensor operand has a higher category than dimensioned operands, we promote to a type with sufficient size and category to hold all zero-dim tensor operands of that category.
- If there are no higher-category zero-dim operands, we promote to a type with sufficient size and category to hold all dimensioned operands.
A floating point scalar operand has dtype torch.get_default_dtype() and an integral non-boolean scalar operand has dtype torch.int64. Unlike numpy, we do not inspect values when determining the minimum dtypes of an operand. Quantized and complex types are not yet supported.
Promotion Examples: