liveBook · Manning

Constexpr

As explained in GPU Programming with Triton

constexpr denotes a value available at compile time rather than only at runtime.

In a Triton kernel, a constexpr parameter supplies a value at compile time. Marking the block size as tl.constexpr lets the compiler specialize and optimize the generated GPU code for that size. section 2.4 section 2.2.10

Specialization affects compilation and caching. Triton caches compiled binaries using constexpr values and coarse properties of other arguments. Changing BLOCK_SIZE triggers a fresh compile; subsequent calls with the same specialization can reuse the binary. section 2.2.10

Vector length versus block size

For vector addition, n_elements is a runtime integer, while BLOCK_SIZE is a compile-time constant. Keeping the length at runtime allows a binary to be reused across input sizes; marking it constexpr would require a separate binary for each distinct length. A block-size sweep instead triggers compilation for new block sizes. section 2.2.10

When benchmarking steady-state performance, discard the first call and usually several early calls as warmup. The first call includes CPU-side compilation, which can otherwise dominate the measurement. Repeat warmup when testing a new specialization. section 2.2.10

How GPU Programming with Triton covers it

How deep it goes
Explains specialization and binary caching through a vector-addition kernel, including their consequences for benchmarking.
Examples
Contrasts runtime vector length with compile-time block size in a vector-addition kernel.
What you can do after
Operate/configure a Triton kernel by choosing which parameters specialize compiled binaries and warming up before timing.
How it connects
Connects compile-time constants to JIT compilation, runtime arguments, binary caching, and benchmark warmup.

Also explained in

Compare how 4 books explain constexpr →

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.