liveBook · Manning

Tl.make_block_ptr

As explained in GPU Programming with Triton

tl.make_block_ptr is a Triton API function that creates a descriptor for a block of memory from its base address, shape, strides, offset, block shape, and memory order.

tl.make_block_ptr creates a descriptor for an N-dimensional block within a parent tensor. It records the base pointer, shape, strides, starting offsets, block shape, and memory order; creating the descriptor does not access memory. Use tl.load and tl.store to perform the actual read and write. section 5.4.3 section 5.6.2

For each block, provide:

  • The parent tensor’s base pointer, shape, and strides. section 5.4.3
  • The block’s starting offsets, block shape, and dimension order. section 5.4.3

Matrix tiles

A transpose kernel describes an input tile and a transposed destination tile with separate block pointers, then loads, transposes, and stores them. A matrix multiplication kernel similarly describes operand tiles for loading into tl.dot. section 5.4.3 section 5.6.2

Listing 5.5 Tiled matrix transpose using make_block_ptr
@triton.jit
def transpose_tiled_kernel(
    x_ptr, y_ptr,
    M, N,
    stride_xm, stride_xn,
    stride_ym, stride_yn,
    BLOCK_M: tl.constexpr,
    BLOCK_N: tl.constexpr,
):
    pid_m = tl.program_id(axis=0)
    pid_n = tl.program_id(axis=1)

    x_block = tl.make_block_ptr(
        x_ptr,
        shape=(M, N),
        strides=(stride_xm, stride_xn),
        offsets=(pid_m * BLOCK_M, pid_n * BLOCK_N),
        block_shape=(BLOCK_M, BLOCK_N),
        order=(1, 0),
    )
    tile = tl.load(x_block, boundary_check=(0, 1))

    y_block = tl.make_block_ptr(
        y_ptr,
        shape=(N, M),
        strides=(stride_ym, stride_yn),
        offsets=(pid_n * BLOCK_N, pid_m * BLOCK_M),
        block_shape=(BLOCK_N, BLOCK_M),
        order=(1, 0),
    )
    tl.store(y_block, tl.trans(tile), boundary_check=(0, 1))

See the complete tiled transpose kernel, including block-pointer arguments, boundary checks, and the load-transpose-store sequence. section 5.4.3

How GPU Programming with Triton covers it

How deep it goes
Defines descriptor fields and distinguishes descriptor creation from memory access.
Examples
Uses input and output tiles in matrix transpose and operand tiles in matrix multiplication.
What you can do after
Apply tl.make_block_ptr to describe tensor tiles before loading or storing them.
How it connects
Connects tensor shapes and strides with offsets, block tiling, tl.load, and tl.store.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of GPU Programming with Triton. The section links point to the parts of the book it draws on.