tvm_ffi.dtype#
- class tvm_ffi.dtype(content)[source]#
Bases:
strLightweight data type in TVM FFI.
It behaves like a Python
strbut also carries an internal FFI representation. You can construct it from strings, NumPy/ML dtypes, or viafrom_dlpack_data_type().- Parameters:
dtype_str – The string representation of the dtype.
content (Any)
- Return type:
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.
with_lanes(lanes)Create a new dtype with the given number of lanes.
Attributes
Number of bits of the scalar base type.
Size of one element in bytes.
Number of lanes (for vector types).
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:
- 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.dtypeUser-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:
- 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, theitemsizerepresents 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.bitsBit width of the scalar base type.
tvm_ffi.dtype.lanesNumber 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.itemsizeByte size accounting for lanes.
tvm_ffi.dtype.lanesNumber of lanes for vector types.
- property lanes: int#
Number of lanes (for vector types).
Returns
1for scalar dtypes and the lane count for vector dtypes created viatvm_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.itemsizeByte size accounting for lanes.