warp.experimental.cutile.export_func#

warp.experimental.cutile.export_func(func, num_worker_warps=4, scope='thread', device=None)[source]#

Package a Warp @warp.func as a cuTile foreign function (a .tilelib) callable inside @ct.kernel bodies.

Experimental

The cuTile interop API is experimental and subject to change without a formal deprecation cycle; it builds on cuTile preview interfaces that may change between CUDA Toolkit and cuTile Python releases.

Compilation and packaging happen when this function is called, on the current machine, and the result is cached in the Warp kernel cache. Requires the CUDA Toolkit 13.5 tileiras, found in the nvidia-cuda-tileiras wheel that warp-lang[cutile] installs, else on PATH.

Parameters:
  • func (warp.Function) – A concrete (non-generic) @warp.func with scalar, vector, matrix, quaternion, transform, struct, or contiguous warp.array parameters (float16, bfloat16, and bool excepted) and exactly one return value.

  • num_worker_warps (int) – Worker-warp count of the calling kernel, as in @ct.kernel(num_worker_warps=...); a power of two between 1 and 32. It can normally be left at its default: a thread-scope declaration is built for every power-of-two tile length from 32 to 1024 and call sites select the length through their tiles. It matters in three cases. For scope="block" the declaration is built for the single tile length num_worker_warps * 32, every call must use it, and it must equal the calling kernel’s num_worker_warps, or tileiras rejects the kernel when it compiles it. For a call whose arguments are all shared values (no tile carries a length) it is the number of invocations the call runs. For functions using mesh AABB/sphere or BVH iterator queries it sets the block the traversal-stack scratch covers: max(8, num_worker_warps) warps at thread scope, exactly num_worker_warps at block scope. A calling kernel whose compiled block is larger stops with a device trap, a sticky CUDA error, so declare at least the kernel’s num_worker_warps.

  • scope (Literal['thread', 'block']) – Execution scope: "thread" (one independent invocation per tile element; the function must not synchronize across threads) or "block" (one invocation with all worker threads participating together, enabling block-cooperative Warp built-in functions that use barriers; declared to Tile IR as cta synchronization).

  • device (DeviceLike) – CUDA device whose architecture the declaration is built for. Defaults to the current Warp device.

Returns:

A callable for use inside @ct.kernel bodies with one argument per Warp parameter, positional or by name. Each argument is either per-invocation (a (TILE_SIZE,) or (TILE_SIZE * width,) tile) or shared by every invocation (a plain Python number or rank-0 tile for a scalar, a (width,) tile for a vector, matrix, or struct, a tuple of plain values for an array); the call tells the two apart by shape and expands shared values to the tile length of the per-invocation ones. It returns one tile (see the cuTile interoperability guide for the full calling convention). Its repr shows the call signature in Warp terms and the tile lengths it serves.

Return type:

TilelibForeignFunction

Raises:
  • ImportError – If the cuTile packages are missing or older than 1.8.1.

  • TypeError – If func is not a user-defined @warp.func, num_worker_warps is not an int, a parameter or return type cannot cross the boundary, or the function returns nothing.

  • ValueError – If num_worker_warps or scope is invalid, the function or a parameter has a name the generated code cannot use (not a plain ASCII identifier, the reserved _wp_ prefix, or a generated operand name such as ret0), the function has default parameter values, it uses block-cooperative built-in functions or tiled queries at thread scope, its scratch exceeds the device’s per-block shared memory, or device is not a CUDA device.

  • NotImplementedError – If the function has several overloads or return values.

  • RuntimeError – If tileiras cannot be found or is not the CUDA Toolkit 13.5 release, or NVRTC or tileiras fails to build the function.

Example

Export once on the host, then call the result inside a @ct.kernel with one argument per Warp parameter:

import cuda.tile as ct
import numpy as np

import warp as wp
import warp.experimental.cutile as wpct

TILE_SIZE = 128


@warp.func
def scale_add(x: float, y: float) -> float:
    return 2.0 * x + y


wp_scale_add = wpct.export_func(scale_add)


@ct.kernel
def scale_add_cutile_kernel(xs, ys, results, results_shared_y):
    x = ct.load(xs, (0,), shape=(TILE_SIZE,))
    y = ct.load(ys, (0,), shape=(TILE_SIZE,))
    ct.store(results, (0,), wp_scale_add(x, y))  # one call per tile element
    ct.store(results_shared_y, (0,), wp_scale_add(x, 1.0))  # y shared by every invocation


xs = warp.array(np.arange(TILE_SIZE), dtype=warp.float32)
ys = warp.ones(TILE_SIZE, dtype=warp.float32)
results = warp.empty(TILE_SIZE, dtype=warp.float32)
results_shared_y = warp.empty(TILE_SIZE, dtype=warp.float32)
ct.launch(
    warp.get_stream().cuda_stream, (1, 1, 1), scale_add_cutile_kernel, (xs, ys, results, results_shared_y)
)