tvm_ffi.dtype#

class tvm_ffi.dtype(content)[source]#

Bases: str

Lightweight data type in TVM FFI.

It behaves like a Python str but also carries an internal FFI representation. You can construct it from strings, NumPy/ML dtypes, or via from_dlpack_data_type().

Parameters:
  • dtype_str – The string representation of the dtype.

  • content (Any)

Return type:

dtype

Examples

import tvm_ffi

# Create from string
f32 = tvm_ffi.dtype("float32")
assert f32.bits == 32
assert f32.itemsize == 4

# Adjust lanes to create vector types
v4f32 = f32.with_lanes(4)
assert v4f32 == "float32x4"

# Round-trip from a DLPack (code, bits, lanes) triple
f16 = tvm_ffi.dtype.from_dlpack_data_type((2, 16, 1))
assert f16 == "float16"

Note

This class subclasses str so it can be directly passed into other array api’s dtype arguments.

__init__()#

Methods

from_dlpack_data_type(dltype_data_type)

Create a dtype from a DLPack data type tuple.

is_dtype(arg)

Check if the given string is a valid dtype.

with_lanes(lanes)

Create a new dtype with the given number of lanes.

Attributes

bits

Number of bits of the scalar base type.

is_bool

Whether this dtype stores boolean values.

is_float

Whether this dtype stores floating-point values.

is_handle

Whether this dtype stores opaque handle values.

is_integer

Whether this dtype stores signed or unsigned integer values.

itemsize

Size of one element in bytes.

lanes

Number of lanes (for vector types).

type_code

Integer DLDataTypeCode of the scalar base type.

static from_dlpack_data_type(dltype_data_type)[source]#

Create a dtype from a DLPack data type tuple.

Parameters:

dltype_data_type (tuple[int, int, int]) – The DLPack data type tuple (type_code, bits, lanes).

Return type:

dtype

Returns:

dtype – The created dtype.

Examples

import tvm_ffi

# Create float16 and int8 directly from DLPack triples
f16 = tvm_ffi.dtype.from_dlpack_data_type((2, 16, 1))
i8 = tvm_ffi.dtype.from_dlpack_data_type((0, 8, 1))
assert f16 == "float16"
assert i8 == "int8"

See also

tvm_ffi.dtype

User-facing dtype wrapper.

tvm_ffi.dtype.with_lanes()

Create vector dtypes from a scalar base.

with_lanes(lanes)[source]#

Create a new dtype with the given number of lanes.

Parameters:

lanes (int) – The number of lanes for the resulting vector type.

Return type:

dtype

Returns:

dtype – The new dtype with the given number of lanes.

Examples

import tvm_ffi

f32 = tvm_ffi.dtype("float32")
v4f32 = f32.with_lanes(4)
assert v4f32 == "float32x4"
assert v4f32.bits == f32.bits and v4f32.lanes == 4

See also

tvm_ffi.dtype.from_dlpack_data_type()

Construct from a DLPack (code, bits, lanes) triple.

property itemsize: int#

Size of one element in bytes.

The size is computed as bits * lanes // 8. When the number of lanes is greater than 1, the itemsize represents the byte size of the vector element.

Examples

import tvm_ffi

assert tvm_ffi.dtype("float32").itemsize == 4
assert tvm_ffi.dtype("float32").with_lanes(4).itemsize == 16

See also

tvm_ffi.dtype.bits

Bit width of the scalar base type.

tvm_ffi.dtype.lanes

Number of lanes for vector types.

tvm_ffi.dtype.with_lanes()

Create a vector dtype from a scalar base.

property type_code: int#

Integer DLDataTypeCode of the scalar base type.

Examples

import tvm_ffi

f32 = tvm_ffi.dtype("float32")
# The type code is an integer following DLPack conventions
assert isinstance(f32.type_code, int)
# Consistent with constructing from an explicit (code, bits, lanes)
assert (
    f32.type_code == tvm_ffi.dtype.from_dlpack_data_type((2, 32, 1)).type_code
)

See also

tvm_ffi.dtype.from_dlpack_data_type()

Construct a dtype from a DLPack (code, bits, lanes) triple.

property bits: int#

Number of bits of the scalar base type.

Examples

import tvm_ffi

assert tvm_ffi.dtype("int8").bits == 8
v4f32 = tvm_ffi.dtype("float32").with_lanes(4)
assert v4f32.bits == 32  # per-lane bit width

See also

tvm_ffi.dtype.itemsize

Byte size accounting for lanes.

tvm_ffi.dtype.lanes

Number of lanes for vector types.

property lanes: int#

Number of lanes (for vector types).

Returns 1 for scalar dtypes and the lane count for vector dtypes created via tvm_ffi.dtype.with_lanes().

Examples

import tvm_ffi

assert tvm_ffi.dtype("float32").lanes == 1
assert tvm_ffi.dtype("float32").with_lanes(4).lanes == 4

See also

tvm_ffi.dtype.with_lanes()

Create a vector dtype from a scalar base.

tvm_ffi.dtype.itemsize

Byte size accounting for lanes.

property is_bool: bool#

Whether this dtype stores boolean values.

property is_integer: bool#

Whether this dtype stores signed or unsigned integer values.

property is_float: bool#

Whether this dtype stores floating-point values.

property is_handle: bool#

Whether this dtype stores opaque handle values.

static is_dtype(arg)[source]#

Check if the given string is a valid dtype.

Parameters:

arg (str | dtype) – The string to check.

Return type:

bool

Returns:

bool – Whether the input string is a valid dtype.

Examples

import tvm_ffi

assert tvm_ffi.dtype.is_dtype("float32")
assert not tvm_ffi.dtype.is_dtype("not_a_dtype")