Kernel Configuration#
Warp-NN kernels can be created and launched with configurable parameters, which are set through
KernelConfig and the kernel_config() context manager.
Tile shapes and block_dim#
Warp-NN kernels process most data in tiles: each CUDA thread block loads a tile of the input array, applies the
operation, and stores the result. Two settings of KernelConfig control this,
and they describe different things:
Setting |
Meaning |
|---|---|
|
How much data each block processes: the logical shape of the tile (for example |
|
How many threads cooperate on that tile (threads per block, at most 1024). |
The number of tile elements is the number of array values (scalars) in one tile, that is, the product of its
shape: (1024,) has 1024 elements, (8, 32) has 8 x 32 = 256, and (4, 8, 32) has 1024. It is the amount
of data one block reads and writes in a single pass. The tile shape does not have to match block_dim.
The tile’s elements are spread across the block’s threads, so each thread handles tile elements / block_dim
of them (the elements per thread).
block_dim = 256
tile of 64 elements -> 0.25 elements/thread (192 of 256 threads are idle)
tile of 256 elements -> 1 elements/thread (every thread has one element)
tile of 1024 elements -> 4 elements/thread (every thread loops over 4 elements)
Guidelines#
Do not use a tile smaller than
block_dim. Threads beyond the tile size have no work and are wasted.Use a
block_dimthat is a multiple of 32, the warp size. Here, a warp is a GPU hardware concept, unrelated to the NVIDIA Warp library: a group of 32 threads that the GPU runs together, executing the same instruction at the same time. A block always runs as whole warps, so ablock_dimof 100 still occupies 4 warps (128 threads), and 28 of them are idle. The default of 256 is a good general choice and matches the Warp library’s own default.Aim for a few elements per thread. The elementwise operations in Warp-NN are memory-bound, so it helps for each thread to move about 16 bytes per pass. As a rule of thumb, this is 2 elements for
float64, 4 forfloat32and 8 forfloat16. Withblock_dim=256andfloat32, that is a tile of about 1024 elements. Values between 1 and 8 elements per thread are usually close to each other in performance.Prefer a wide last dimension for 2D and higher tiles. The last dimension is contiguous in memory, while consecutive tile rows can be far apart. A wider row therefore means fewer, longer contiguous chunks per tile and more coalesced accesses. For example,
(8, 32)and(16, 16)both have 256 elements, but forfloat32the first reads 8 chunks of 128 bytes (one cache line each) and the second 16 chunks of 64 bytes, so(8, 32)is usually slightly better.Make the last dimension a multiple of 16 bytes. Warp can use faster 128-bit loads when
last_dim * sizeof(dtype)is a multiple of 16 bytes (for example, a multiple of 4 forfloat32). Both(8, 32)and(16, 16)satisfy this. The loads also need the array itself to be dense and 16-byte aligned, with rows of a multiple of 16 bytes; otherwise Warp silently falls back to scalar loads.Do not oversize tiles for small arrays. The grid is rounded up, so when an array dimension is smaller than the tile dimension, part of the tile is out of bounds and wasted work. A tile that is too large can also produce too few blocks to keep the GPU busy. Adaptive tiles (enabled by default) take care of this automatically.
Note
The best values depend on the GPU, the data type and the array sizes. The guidelines above are starting points:
if performance matters for your workload, benchmark a few tile shapes and block_dim values.
Default values#
The default configuration uses block_dim=256 and tiles of 1024 elements, that is, 4 elements per thread
for float32:
_BLOCK_DIM = 256
_TILE_1D = (1024,) # 1024 elements
_TILE_2D = (16, 64) # 1024 elements
_TILE_3D = (4, 8, 32) # 1024 elements
_TILE_4D = (2, 4, 8, 16) # 1024 elements
_ADAPTIVE = True # adapt the tiles (and block dimension) to the launched arrays
Example#
The configuration is read when a module is created, so create the module inside the context manager:
from warp_nn import nn
from warp_nn.utils import kernel_config
# 1D tile: 1024 elements / 256 threads = 4 elements per thread (16 bytes for float32).
# E.g., a 1D array of 1M elements is split into `ceil(1_000_000 / 1024) = 977` blocks
# (the last block is only partially filled).
with kernel_config(block_dim=256, tile_1d=(1024,)):
relu = nn.ReLU()
# Smaller blocks and tiles, e.g. for small arrays:
# - 1D tile: 256 elements / 128 threads = 2 elements per thread.
# - 2D tile: 8 x 16 = 128 elements / 128 threads = 1 element per thread.
# The last dimension of the 2D tile is 16 x 4 bytes = 64 bytes (a multiple of 16),
# so float32 can use 128-bit loads.
with kernel_config(block_dim=128, tile_1d=(256,), tile_2d=(8, 16)):
sigmoid = nn.Sigmoid()
Adaptive tiles#
The configured tiles suit large arrays, but waste work on smaller ones. For example, a (16, 64) tile over an
array of 8 columns computes 64 columns, so 7/8 of its work is out of bounds. And an array covered by only a few
tiles leaves most of the GPU’s SMs (streaming multiprocessors) idle.
When adaptive is true (the default), the modules adapt their tiles and block_dim to the shape of each array
they process. On CPU devices, which have no SMs to keep busy, the tiles are adapted without the shrink step (step 3
below) or the one thread per tile element that small grids get in step 4. Warp launches the tiled kernels on CPU
devices with one thread per block, so only the adapted tile matters there. The configured values act as upper bounds:
An adapted tile never has more elements than the configured tile, although a single dimension may grow beyond its configured size (e.g. more rows when there are fewer columns).
The adapted
block_dimis never larger than the configured one.
How a tile is adapted#
The tile is adapted to the launch shape (the array covered by the tiles) in four steps:
Narrow: each tile dimension steps down (see below) while the smaller size still covers the array dimension.
Grow: the tile dimensions that do not cover the array are doubled (last first), as long as the tile has no more elements than the configured tile.
Shrink: if the grid still has too few blocks to keep every SM busy (fewer than 2 blocks per SM for elementwise operations, 4 for matrix multiplications), the tile dimensions step down (leading ones first), down to a tile of 128 elements.
Block dimension: the block keeps the configured number of elements per thread, with its number of threads rounded up to a multiple of the GPU warp size (32). If the grid still has fewer than 2 blocks per SM, it gets one thread per tile element instead, up to the configured block dimension. Elementwise operations also do this when the tile shrinks in step 3. The number of threads is then raised (never lowered) to the next multiple of 32, up to the configured
block_dim, that divides the tile elements, if any, so that every thread gets the same number of elements.
A dimension steps down to the next smaller size of the form minimum size × power of two (the minimum size is 1, or 16 rows and 8 columns for matrix multiplications). For example, 64 steps down to 32, and 40 steps down to 32, 16 and then 8. The sizes reached by stepping down are therefore multiples of the minimum size, and there are few of them, so few kernel variants are compiled. Tiles whose dimensions are powers of two are simply halved.
Matrix multiplications (the Linear, RNN, GRU and LSTM layers and their cells) have a few more
constraints, given by their tile of (batch rows, output features):
The tile keeps at least 16 rows and 8 columns (the shape of the tensor-core instructions).
The rows plus columns, which set the shared memory used by the operand tiles, are limited to those of the configured tile.
When a Linear inference grid reaches the block target, the block dimension is capped at one warp per 32 tile columns.
The tile does not grow (step 2) for launches recorded on a
warp.Tapeby a module that requires gradients, since the backward kernels run on the same tile and were measured to be fastest with 16 rows.
Examples#
With the default configuration (tile_1d=(1024,), tile_2d=(16, 64) and block_dim=256), on a GPU with
76 SMs (the launch shape of a matrix multiplication is its output, (batch rows, output features)):
Operation |
Launch shape |
Adapted tile |
Adapted |
|---|---|---|---|
Elementwise |
|
|
256 |
Elementwise |
|
|
256 |
Elementwise |
|
|
128 |
Elementwise |
|
|
128 |
Linear inference |
|
|
64 |
Matrix multiplication |
|
|
128 |
Linear inference |
|
|
32 |
Matrix multiplication (recorded for differentiation) |
|
|
32 |
For example, the (50_000,) array would only get 49 blocks of the configured (1024,) tile, fewer than the
152 needed to give 2 blocks to each of the 76 SMs. The tile is halved twice to (256,) (196 blocks).
Since the tile shrank, the block uses one thread per element (256 threads). The (300,) array gets only 3 blocks
with the smallest tile of 128 elements, so it also gets one thread per element.
Compilation and caching#
Within a module, each adapted configuration gets its own kernels, which are compiled the first time an array needs
them. Arrays whose shapes are adapted to the same configuration share the same kernels, and the adapted configuration
of each shape is cached, so that later launches with that shape only do a lookup. If a workload uses many different
array shapes, this may compile more kernels; set adaptive=False to always use the configured tiles. Disabling it
is also useful to benchmark specific tile shapes, since they are then used as they are:
from warp_nn import nn
from warp_nn.utils import kernel_config
# use the configured tile and block dimension for every array, whatever its shape
with kernel_config(tile_1d=(512,), block_dim=128, adaptive=False):
relu = nn.ReLU()
Custom kernels#
Kernels written outside Warp-NN can be adapted the same way, with adapt().
It returns the configuration adapted to a launch shape on a device (or the configuration itself, if nothing changes),
under the adaptation policy of the operation (AdaptationPolicy). The adapted
configurations are cached per configuration instance, so keep the configuration rather than getting a new one for
each launch:
import warp as wp
from warp_nn.utils import AdaptationPolicy, get_kernel_config
config = get_kernel_config()
adapted = config.adapt((4096, 8), wp.get_device("cuda:0"), policy=AdaptationPolicy.MATMUL)
# compile (once per adapted configuration) and launch the kernel with adapted.tile_2d and adapted.block_dim
API#
- class warp_nn.utils.config.KernelConfig(
- *,
- block_dim: int,
- tile_1d: tuple[int],
- tile_2d: tuple[int, int],
- tile_3d: tuple[int, int, int],
- tile_4d: tuple[int, int, int, int],
- adaptive: bool = True,
Bases:
objectConfiguration for Warp kernels generation.
- adapt(
- shape: tuple[int, ...],
- device: Device,
- *,
- policy: AdaptationPolicy = AdaptationPolicy(min_sizes=(), limit_dims_sum=False, blocks_per_sm=2, thread_per_element_blocks_per_sm=2, thread_per_element_after_shrink=True, min_elements=128, grow_for_backward=True, limit_inference_threads_by_columns=False),
- requires_grad: bool = False,
Get the configuration adapted to a launch shape on a device.
The tile with the launch shape’s number of dimensions, and the block dimension, are sized for the launch shape (see how a tile is adapted). The resulting tile never has more elements than the configured tile, although single dimensions may grow beyond their configured size (e.g. more rows for fewer columns), and the resulting block dimension is at most the configured one.
On CPU devices, which have no SMs to keep busy, the tile is never shrunk for more blocks.
This configuration is returned unchanged if
adaptiveis false, if it has no tile with the launch shape’s number of dimensions, or if the sizing yields the configured values. The result is cached per configuration instance, so that adapting it to a known launch shape is a lookup: keep the configuration (e.g. read it once, as the modules do when they are created) rather than getting a new one for each launch (e.g. withget_kernel_config()).The backward kernels run on the resulting tile too if
requires_gradis true and awarp.Tapeis recording the launch, so that the forward-only launches of a module that requires gradients (e.g. inference) get the same tile as a module that does not. Policies that grow their tiles for the backward kernels too (seeAdaptationPolicy.grow_for_backward) ignore it.- Parameters:
shape – The launch shape (the array covered by the tiles), as a tuple.
device – The device on which the kernels are launched.
policy – The adaptation policy of the operation (e.g.
AdaptationPolicy.ELEMENTWISEorAdaptationPolicy.MATMUL).requires_grad – Whether the launched kernels require gradients (e.g. the module requires gradients).
- Returns:
The adapted configuration.
- Raises:
ValueError – If the configured tile with the launch shape’s number of dimensions has non-positive dimensions.
- adaptive: bool = True#
Whether to adapt the tiles (and block dimension) to the launched arrays.
If true, the modules that support it narrow their tiles to the arrays they process (e.g. to the output features of a narrow layer), and, on CUDA devices, use smaller tiles when the arrays are too small to keep every SM busy. An adapted tile has no more elements than the configured tile (although single dimensions may grow, e.g. more rows for fewer columns), and the adapted block dimension is at most the configured one. The modules create a kernel for each adapted tile, on the first launch that needs it. If false, the configured tiles and block dimension are used as they are.
- class warp_nn.utils.config.AdaptationPolicy(
- min_sizes: tuple[int, ...] = (),
- limit_dims_sum: bool = False,
- blocks_per_sm: int = 2,
- thread_per_element_blocks_per_sm: int = 2,
- thread_per_element_after_shrink: bool = False,
- min_elements: int = 128,
- grow_for_backward: bool = True,
- limit_inference_threads_by_columns: bool = False,
Bases:
objectRules under which the kernel configuration (tile and block dimension) of an operation is adapted to its launch shapes (see
KernelConfig.adapt()).Use the predefined policies (e.g.
AdaptationPolicy.MATMUL), or derive a policy from them (e.g. withdataclasses.replace()).- min_tile(ndim: int) list[int][source]#
Get the minimum size of each dimension of a tile.
The minimum sizes of the policy apply to the last dimensions of the tile (the leading ones, if the tile has fewer dimensions, are ignored), and the other dimensions have a minimum size of 1.
- Parameters:
ndim – The number of dimensions of the tile.
- Returns:
The minimum size of each dimension.
- ELEMENTWISE: ClassVar[AdaptationPolicy] = AdaptationPolicy(min_sizes=(), limit_dims_sum=False, blocks_per_sm=2, thread_per_element_blocks_per_sm=2, thread_per_element_after_shrink=True, min_elements=128, grow_for_backward=True, limit_inference_threads_by_columns=False)#
Element-wise operations: every dimension can be narrowed to the array (they load and store their tiles element by element, so a tile row narrower than a vector load costs nothing), and shrunk for occupancy (leading ones first). Tiles shrunk to supply more blocks get one thread per element, up to the configured block dimension.
- LINEAR: ClassVar[AdaptationPolicy] = AdaptationPolicy(min_sizes=(16, 8), limit_dims_sum=True, blocks_per_sm=4, thread_per_element_blocks_per_sm=2, thread_per_element_after_shrink=False, min_elements=128, grow_for_backward=False, limit_inference_threads_by_columns=True)#
Linear layers use the matrix multiplication tile (see
MATMUL), with one warp per warp-sized span of output columns when the inference grid meets the block target. This gives each warp more rows to process per block.
- MATMUL: ClassVar[AdaptationPolicy] = AdaptationPolicy(min_sizes=(16, 8), limit_dims_sum=True, blocks_per_sm=4, thread_per_element_blocks_per_sm=2, thread_per_element_after_shrink=False, min_elements=128, grow_for_backward=False, limit_inference_threads_by_columns=False)#
Matrix multiplications, whose tile is
(batch rows, output features): the tile keeps at least 16 rows and 8 columns (the tensor-core MMA shape), and the rows plus columns (which set the shared memory of the operand tiles) are limited to the configured tile’s. Small batches shrink the columns too once the rows are at their minimum: reloading the few input rows for each narrower column tile costs less than leaving SMs idle. The grid targets 4 blocks per SM, while one thread per tile element is kept for grids below 2 blocks per SM. The tile is not grown (its rows are kept at the configured 16 by default) for launches recorded for differentiation: their backward kernels were fastest with 16 rows (Linear, RNN, GRU and LSTM layers of up to 64 output features, batches of 64 to 32768, on an sm_89 GPU).
- blocks_per_sm: int = 2#
Number of blocks per SM that the grid should provide to keep the GPU busy (the target of the shrink step).
- grow_for_backward: bool = True#
Whether to grow the tile (see
KernelConfig.adapt()) for launches whose backward kernels run too.The backward kernels (the adjoints of the forward kernels) run on the tile of the forward kernels, and may favor a different tile than the forward pass alone.
- limit_dims_sum: bool = False#
Whether the sum of the tile dimensions is limited to the configured tile’s too (besides its elements).
- limit_inference_threads_by_columns: bool = False#
Whether to cap the block at one warp per warp-sized span of tile columns for a well-filled inference grid.
- min_sizes: tuple[int, ...] = ()#
Minimum sizes of the last tile dimensions. The other dimensions have a minimum size of 1.
- thread_per_element_after_shrink: bool = False#
Whether to give one thread per tile element after shrinking the tile to provide more blocks.
- thread_per_element_blocks_per_sm: int = 2#
Number of blocks per SM below which the block dimension gives one thread per tile element (at most
blocks_per_sm, since the shrink step stops at that target, so a larger value gives one thread per element to every grid below it).It is independent of
blocks_per_sm: a grid can be worth shrinking the tile for, while having enough blocks for the configured number of elements per thread.
- warp_nn.utils.config.kernel_config(
- *,
- block_dim: int | None = None,
- tile_1d: tuple[int] | None = None,
- tile_2d: tuple[int, int] | None = None,
- tile_3d: tuple[int, int, int] | None = None,
- tile_4d: tuple[int, int, int, int] | None = None,
- adaptive: bool | None = None,
Context manager that sets a thread-local configuration values.
Unspecified (
None) values are inherited from the enclosing context (or the default values).
- warp_nn.utils.config.get_kernel_config() KernelConfig[source]#
Get the current configuration.