Background#

Coding agents built the compiler of tnzr.agents, which generates code for tensor contractions on GPUs and CPUs. This chapter introduces tensor contractions, schedules, the roofline model and coding agents.

Tensor contractions#

A tensor contraction generalizes matrix multiplication to tensors with any number of axes. We write contractions in einsum notation, which lists the axes of each input and, after an arrow, the axes of the output. For example, km,nk→nm multiplies two matrices.

Each axis has one of four roles:

  • A batch axis appears in all three tensors.

  • An M axis appears in the first input and the output.

  • An N axis appears in the second input and the output.

  • A K axis appears only in the two inputs. The contraction sums the products of the inputs’ elements over this axis.

A general matrix multiplication (GEMM) has one axis of each role M, N and K. A batched GEMM adds one batch axis. A general contraction has several axes of at least one role. For example, ce,deba→dcba has the M axis c, the K axis e and the three N axes d, b and a. Each tensor lies in memory in row-major order of its axes, so the order of the axes gives the tensor’s layout and the last axis has unit stride. A contraction either overwrites its output, C = A·B, or adds to it, C += A·B.

Libraries compute a contraction through a GEMM or directly on the tensors.

Transpose-transpose-GEMM-transpose (TTGT) transposes the inputs into matrices, multiplies the matrices with the GEMM of a tuned library and transposes the product into the output. TTGT can use a tensor as a matrix without a transposition only if the axes of each role lie next to each other and in the same order in all tensors. In ce,deba→dcba, the K axis e lies between N axes of the second input, and the M axis c lies between N axes of the output. TTGT thus transposes the second input before the GEMM and the product after it.

Direct contraction, which GETT [1] and TBLIS [2] implement, avoids the extra time and memory of these transpositions. It follows the design of fast GEMMs [3], which copy blocks of their input matrices into buffers that fit the caches. Direct contraction copies these blocks from the input tensors and transposes them during the copy. It writes each block of the product into its place in the output tensor.

Schedules#

A schedule describes how a device executes a computation. It sets the tiling of the axes, the order of the loops, the parallelization and the memory that holds each tile. Compilers such as Halide [4] keep the schedule apart from the computation and generate code from both.

Roofline model#

The roofline model [5] limits the performance of a computation by a device’s peak throughput and its memory bandwidth. The roofline time of a contraction is the longer of two lower bounds on its time. One bound is the time of the contraction’s floating-point operations at the device’s peak throughput. For C = A·B, the other bound is the time to read the inputs and write the output once at the device’s memory bandwidth. For C += A·B, this bound adds the time to read the output. A program’s percentage of the roofline is the roofline time as a percentage of the program’s time.

Coding agents#

A coding agent is a language model that develops software by calling tools in a loop. The tools read and edit files and run commands, such as tests. The model chooses its next step from the result of each call. Codex and Claude Code are two such agents.

References#

  1. [1]Paul Springer and Paolo Bientinesi. Design of a High-Performance GEMM-like Tensor–Tensor Multiplication. ACM Transactions on Mathematical Software 44(3):1–29, 2018. ↩
  2. [2]Devin A. Matthews. High-Performance Tensor Contraction without Transposition. SIAM Journal on Scientific Computing 40(1):C1–C24, 2018. ↩
  3. [3]Kazushige Goto and Robert A. van de Geijn. Anatomy of High-Performance Matrix Multiplication. ACM Transactions on Mathematical Software 34(3):1–25, 2008. ↩
  4. [4]Jonathan Ragan-Kelley, Connelly Barnes, Andrew Adams, Sylvain Paris, Frédo Durand and Saman Amarasinghe. Halide: A Language and Compiler for Optimizing Parallelism, Locality, and Recomputation in Image Processing Pipelines. ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 519–530, 2013. ↩
  5. [5]Samuel Webb Williams, Andrew Waterman and David A. Patterson. Roofline: An Insightful Visual Performance Model for Floating-Point Programs and Multicore Architectures. Technical Report UCB/EECS-2008-134, EECS Department, University of California, Berkeley, 2008. ↩