TZIR#
TZIR is our intermediate representation (IR) for scheduling tensor programs. In our workflow, TZIR is the interface between the parts of the compiler that agents build in separate tasks. A TZIR program nests statements in loops and in placements on compute units. Each statement performs one tile operation, such as the product of two tiles. The program fixes every scheduling decision, such as the tiling, the order of the loops, the parallelization and the memory that holds each tile. A backend compiles the program for one microarchitecture and executes it as written. The backend decides only what the program leaves open, such as the instructions inside each tile operation. TZIR consists of a language for programs and of transforms, which derive programs from an initial program.
Programs#
A program consists of a target description, the extents of its axes, its buffers and its schedule.
The target description declares compute levels and memory spaces. A compute level is a set of interchangeable compute units, such as the cooperative thread arrays (CTAs) of a GPU or the cores of a CPU. A memory space is memory in which software allocates buffers, such as dynamic random-access memory (DRAM), shared memory or registers that hold a tile across tile operations. Hardware-managed caches lie outside TZIR, because the hardware decides which data they hold.
Each buffer has a layout, a memory space and an element type. The inputs and the output of a contraction lie in the target’s main memory, such as DRAM.
The schedule nests three kinds of nodes:
A loop runs its body once for each index of its axis, in order.
A placement runs one instance of its body for each combination of its axes’ indices, each instance on a different unit of a compute level.
A statement performs one tile operation, such as a contraction, a reduction, an elementwise operation, a copy, a type conversion or a fill.
A tile operation computes over the axes of its statement that no enclosing loop or placement binds.
Transforms#
Transforms derive a program from the contraction’s initial program, which consists of one statement on whole tensors. A trace records each applied transform with its arguments. Replaying the trace reproduces the program. The transforms split axes into tiles, move axes into loops and placements, and move data into buffers in other memory spaces.
Example#
Figure 2 shows the compiler’s program for the contraction ce,deba→dcba on the GB10 GPU.
The compiler derives the program from the initial program with nine transforms.
The program reaches 1.19 times the performance of a general matrix multiplication of the same size in NVIDIA’s library.
It also outperforms every baseline on this workload.
levels ctas(2147483647)space dram [127598768KiB align 256B] rootspace shared [48KiB align 16B] per ctasspace registers [128KiB] per ctasaxes |a|=96, |b|=84, |c|=84, |d|=84, |e|=96, |x0|=3, |x1|=32B0: A[c,e] @dram bf16 externB1: B[d,e,b,a] @dram bf16 externB2: C[d,c,b,a] @dram bf16 externB3: acc0[d,b|c,a] @registers f32B4: shared0[d,b|c,(8+x1)] @shared bf16B5: shared1[d,b|x1,(8+a)] @shared bf16P0: [d,b] over ctas S2: acc0[d,b|c,a]@registers = 0 L2: x0 S3: shared0[d,b|c,(8+{x1})]@shared = A[c,x0,x1]@dram S4: shared1[d,b|x1,(8+{a})]@shared = B[d,x0,x1,b,a]@dram S0: acc0[d,b|c,a]@registers +=(sum f32) shared0[d,b|c,(8+{x1})]@shared, shared1[d,b|x1,(8+{a})]@shared S1: C[d,c,b,a]@dram = cast(acc0[d,b|c,a]@registers)
Figure 2 The compiler’s program for ce,deba→dcba on the GB10 GPU.
The upper part gives the program’s text, and the lower part draws its schedule.
The drawing has one row for each memory space.
Program order runs from left to right.
Each box is a statement in the row of the memory space that it writes.
Arrows lead to each box from the data that its statement reads.
Dashed frames mark the placement over the CTAs and the loop over the tiles of the K axis e.#
The program runs one CTA for each pair of indices of the N axes d and b (P0), 7,056 CTAs in all.
Each CTA holds an accumulator for its tile of the output in registers, with 32-bit floating-point (FP32) elements.
The CTA sets the accumulator to zero (S2) and loops over the three tiles of the K axis e (L2).
In each iteration, the CTA copies the current tiles of both inputs from DRAM into shared memory (S3 and S4).
The contraction then adds the product of these tiles to the accumulator (S0).
After the loop, a type conversion writes the accumulator into the output in DRAM (S1).
The layouts record further decisions.
Each on-chip buffer has one instance for each CTA.
In acc0[d,b|c,a], for example, the axes d and b before the bar select the CTA’s instance.
The axes c and a after the bar index its elements.
The shared buffers also pad each row by 8 elements, as 8+x1 in the layout of shared0 shows.
Figure 3 steps through the nine transforms that derive this program from the initial program.
Figure 3 The transforms that derive the program of Figure 2 from the initial program. Each step applies one transform of the trace to the program before it. Lines that a transform removes appear struck through. Lines that it adds or changes are marked.#