Runtime#

Note

cutlass.cute.runtime is kept for compatibility. Runtime APIs are being moved to cutlass.runtime; prefer the new namespace when it is available.

API documentation#

cutlass.cute.runtime.from_dlpack(
tensor_dlpack: object,
assumed_align: int | None = None,
use_32bit_stride: bool = False,
*,
enable_tvm_ffi: bool = False,
force_tf32: bool = False,
) → cutlass.cute.typing.Tensor#

Convert from tensor object supporting __dlpack__() to a CuTe Tensor.

Parameters:
  • tensor_dlpack (object) – Tensor object that supports the DLPack protocol

  • assumed_align (int, optional) – Assumed alignment of the tensor (bytes), defaults to None, if None, will use the element size bytes as the assumed alignment.

  • use_32bit_stride (bool, optional) – Whether to use 32-bit stride, defaults to False. When True, the dynamic stride bitwidth will be set to 32 for small problem size (cosize(layout) <= Int32_max) for better performance. This is only applied when the dimension is dynamic.

  • enable_tvm_ffi (bool, optional) – Whether to enable TVM-FFI, defaults to False. When True, the tensor will be converted to a TVM-FFI function compatible tensor.

  • force_tf32 (bool, optional) – Whether to force the element type to TFloat32 if the element type is Float32.

Returns:

A CuTe Tensor object

Return type:

Tensor

For packed subbyte torch dtypes such as torch.float4_e2m1fn_x2, from_dlpack returns the logical element layout expected by CuTe instead of the packed storage layout. For example, a torch tensor with shape (128, 128) and dtype torch.float4_e2m1fn_x2 is exposed as a logical FP4 tensor with shape (128, 256).

Examples:

import torch
from cutlass.cute.runtime import from_dlpack
x = torch.randn(100, 100)
y = from_dlpack(x)
y.shape
# (100, 100)
type(y)
# <class 'cutlass.cute.Tensor'>
cutlass.cute.runtime.make_ptr(
dtype: Type[cutlass.cute.typing.Numeric],
value: int | _Pointer,
mem_space: cutlass.cute.typing.AddressSpace = cutlass.cute.typing.AddressSpace.generic,
assumed_align: int | None = None,
) → cutlass.cute.typing.Pointer#

Create a pointer from a memory address

Parameters:
  • dtype (Type[Numeric]) – Data type of the pointer elements

  • value (Union[int, ctypes._Pointer]) – Memory address as integer or ctypes pointer

  • mem_space (AddressSpace, optional) – Memory address space, defaults to AddressSpace.generic

  • align_bytes (int, optional) – Alignment in bytes, defaults to None

Returns:

A pointer object

Return type:

Pointer

import numpy as np
import ctypes

from cutlass import Float32
from cutlass.cute.runtime import make_ptr

# Create a numpy array
a = np.random.randn(16, 32).astype(np.float32)

# Get pointer address as integer
ptr_address = a.ctypes.data_as(ctypes.POINTER(ctypes.c_float))

# Create pointer from address
y = make_ptr(cutlass.Float32, ptr_address)

# Check properties
print(y.element_type)
print(type(y))  # <class 'cutlass.cute.Pointer'>
cutlass.cute.runtime.nullptr(
dtype: Type[cutlass.cute.typing.Numeric],
mem_space: cutlass.cute.typing.AddressSpace = cutlass.cute.typing.AddressSpace.generic,
assumed_align: int | None = None,
) → cutlass.cute.typing.Pointer#

Create a null pointer which is useful for compilation

Parameters:
  • dtype (Type[Numeric]) – Data type of the pointer elements

  • mem_space (AddressSpace, optional) – Memory address space, defaults to AddressSpace.generic

Returns:

A null pointer object

Return type:

Pointer

cutlass.cute.runtime.make_fake_tensor(
dtype: Type[cutlass.cute.typing.Numeric],
shape: tuple[int | cutlass.cute.typing.SymInt, ...],
stride: tuple[int | cutlass.cute.typing.SymInt, ...],
*,
memspace: cutlass.cute.typing.AddressSpace = cutlass.cute.typing.AddressSpace.gmem,
assumed_align: int | None = None,
) → _FakeTensor#

Create a fake tensor descriptor with an explicit layout.

Use this builder for cute.compile(...) when the logical tensor layout is not compact, when you already know the exact stride tuple, or when you want fake-tensor layout to match an external contract exactly. shape and stride are both expressed in elements, not bytes.

Parameters:
  • dtype (Type[Numeric]) – Data type of the tensor elements.

  • shape (tuple[int | SymInt, ...]) – Tensor extents in elements, one per mode. Each entry may be static (int) or dynamic (SymInt). Dynamic dimensions become runtime-bound scalar parameters on the compiled callable.

  • stride (tuple[int | SymInt, ...]) – Explicit stride tuple in elements. Must have the same rank as shape. Each entry may be static (int) or dynamic (SymInt).

  • memspace (AddressSpace, optional) – Memory space where the fake tensor resides. Defaults to AddressSpace.gmem.

  • assumed_align (int, optional) – Assumed byte alignment of the base pointer. If None, defaults to one element width in bytes (and at least 1).

Returns:

An instance of a fake tensor with the given properties.

Return type:

_FakeTensor

If the same runtime symbolic quantity appears in multiple positions, reuse the same SymInt object at every occurrence. Different SymInt objects are treated as distinct runtime parameters even if they share the same symbol string.

Use make_fake_compact_tensor() instead when the layout is compact and you want the stride tuple inferred from shape and a mode order.

Examples:

@cute.jit
def foo(x: cute.Tensor):
    ...

sym_m = cute.sym_int64(symbol="M")
sym_ld = cute.sym_int64(divisibility=16, symbol="LD")

# Row-major logical layout: contiguous K dimension, explicit leading dim.
x = make_fake_tensor(
    cutlass.Float16,
    shape=(sym_m, 128),
    stride=(sym_ld, 1),
)

compiled_foo = cute.compile(foo, x)
cutlass.cute.runtime.make_fake_compact_tensor(
dtype: Type[cutlass.cute.typing.Numeric],
shape: tuple[int | cutlass.cute.typing.SymInt, ...],
*,
stride_order: tuple[int, ...] | None = None,
memspace: cutlass.cute.typing.AddressSpace = cutlass.cute.typing.AddressSpace.gmem,
assumed_align: int | None = None,
use_32bit_stride: bool = False,
) → _FakeTensor#

Create a fake tensor descriptor with a compact layout derived from shape.

This is the usual builder for cute.compile(...) when the logical tensor is compact and you want the runtime stride tuple to be derived automatically from shape and stride_order. Each entry in shape may be a static Python int or a dynamic SymInt. Dynamic dimensions become runtime-bound scalar parameters on the compiled callable.

Parameters:
  • dtype (Type[Numeric]) – Data type of the tensor elements.

  • shape (tuple[int | SymInt, ...]) – Tensor extents in elements, one per mode. Each entry may be static (int) or dynamic (SymInt).

  • stride_order (tuple[int, ...], optional) – Permutation describing which mode is fastest-changing. 0 means the innermost / stride-1 mode, len(shape)-1 the slowest-changing mode. If omitted, the default is left-to-right order (0, 1, ..., n-1).

  • memspace (AddressSpace, optional) – Memory space where the fake tensor resides. Defaults to AddressSpace.gmem.

  • assumed_align (int, optional) – Assumed byte alignment of the base pointer. If None, defaults to one element width in bytes (and at least 1).

  • use_32bit_stride (bool, optional) – Use 32-bit symbolic strides instead of 64-bit ones for dynamic layouts. This only affects dynamically-derived stride entries and is useful when the compact layout provably fits in int32.

Returns:

An instance of a fake tensor with the given properties and compact layout.

Return type:

_FakeTensor

Use make_fake_tensor() instead when the logical layout is non-compact or when you need to spell the stride tuple explicitly.

Examples:

@cute.jit
def foo(x: cute.Tensor):
    ...

x = make_fake_compact_tensor(
    cutlass.Float32, (100, cute.sym_int32(divisibility=8)), stride_order=(1, 0)
)

# Compiled function will take a tensor with the type:
#   tensor<ptr<f32, generic> o (100,?{div=8}):(?{i32 div=8},1)>
compiled_foo = cute.compile(foo, x)

# Default stride order is left-to-right order (0, 1, ..., n-1)
y = make_fake_compact_tensor(cutlass.Float32, (8, 3, 2))
assert y.stride == (1, 8, 24)
cutlass.cute.runtime.make_fake_stream(
*,
use_tvm_ffi_env_stream: bool = False,
) → FakeStream#

Create a fake stream that can be used as a placeholder for a stream in compilation.

When use_tvm_ffi_env_stream is True and the function is compiled with TVM-FFI, the argument will be skipped from the function signature and we pass in this value through the environment stream obtained from caller context (e.g. torch.cuda.current_stream()). This can speedup the calling process since we no longer need to do stream query in python.

Parameters:

use_tvm_ffi_env_stream (bool) – Whether to skip this parameter use environment stream instead.

class cutlass.cute.runtime.FakeStream(*, use_tvm_ffi_env_stream: bool = False)#

Bases: object

A fake stream that can be used as a placeholder for a stream in compilation.

When use_tvm_ffi_env_stream is True and the function is compiled with TVM-FFI, the argument will be skipped from the function signature and we pass in this value through the environment stream obtained from caller context (e.g. torch.cuda.current_stream()).

__init__(*, use_tvm_ffi_env_stream: bool = False) → None#
use_tvm_ffi_env_stream: bool#
cutlass.cute.runtime.load_module(
file_path: str,
*,
enable_tvm_ffi: bool = False,
) → ExternalBinaryModule#

Load a module from a file path.

Parameters:
  • file_path (str) – The path to the module file

  • enable_tvm_ffi (bool, optional) – Whether to enable TVM-FFI, defaults to False. When True, the module will be loaded as a TVM-FFI module.

Returns:

A module object

Return type:

module

cutlass.cute.runtime.find_runtime_libraries(*, enable_tvm_ffi: bool = True) → list[str]#

Find the runtime libraries required to load a compiled CuTe module.

class cutlass.cute.runtime.TensorAdapter(arg: object)#

Bases: object

Convert a DLPack protocol supported tensor/array to a cute tensor.

__init__(arg: object) → None#