DType

torch.DType
See theDType companion object
sealed abstract class DType

A torch.DType is an object that represents the data type of a torch.Tensor. PyTorch has twelve different data types:

Data type dtype
32-bit floating point torch.float32 or torch.float
64-bit floating point torch.float64 or torch.double
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
16-bit floating point[2] torch.bfloat16
8-bit integer (unsigned) torch.uint8
8-bit integer (signed) torch.int8
16-bit integer (signed) torch.int16 or torch.short
32-bit integer (signed) torch.int32 or torch.int
64-bit integer (signed) torch.int64 or torch.long
Boolean torch.bool

To find out if a torch.dtype is a floating point data type, the property is_floating_point can be used, which returns True if the data type is a floating point data type.

To find out if a torch.dtype is a complex data type, the property is_complex can be used, which returns True if the data type is a complex data type.

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.

[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

Attributes

Companion
object
Source
DType.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class BFloat16
object bfloat16.type
class Bits16
object bits16.type
class Bits1x8
object bits1x8.type
class Bits2x4
object bits2x4.type
class Bits4x2
object bits4x2.type
class Bits8
object bits8.type
class Bool
object bool.type
class Complex128
object complex128.type
class Complex32
object complex32.type
class Complex64
object complex64.type
class Float16
object float16.type
class Float32
object float32.type
class Float64
object float64.type
object float8_e4m3fn.type
class Float8_e5m2
object float8_e5m2.type
class Int16
object int16.type
class Int32
object int32.type
class Int64
object int64.type
class Int8
object int8.type
class NumOptions
object numoptions.type
class QInt32
object qint32.type
class QInt8
object qint8.type
class QUInt2x4
object quint2x4.type
class QUInt4x2
object quint4x2.type
class QUInt8
object quint8.type
class UInt8
object uint8.type
class Undefined
object undefined.type
Show all
In this article