Post

Beyond CUDA Cores: Inside the Tensor Core Execution Model

Picking up where the CUDA GEMM ladder left off: a visual walkthrough of warp-wide Tensor Core math (mma.sync), register fragments, ldmatrix, zero-overhead XOR swizzling, and how WMMA maps to the hardware on an H100.

Beyond CUDA Cores: Inside the Tensor Core Execution Model

Kernel Engineering Series — Part 4: Builds on Part 1: The Great AI Compromise, Part 2: Why Faster GPUs Leave Lazy Kernels Starving, and Part 3: Every Fix Moves the Bottleneck: Climbing the CUDA GEMM Ladder.

In Part 3, we pushed a hand-written FP32 CUDA kernel from 35.16 ms down to 1.71 ms on an H100, with cuBLAS at 0.371 ms and the physical CUDA-core Speed-of-Light sitting at 0.26 ms (257 µs at 66.9 TFLOP/s).

That is a hard wall for standard 32-bit CUDA cores—no amount of tiling can cross 66.9 TFLOP/s on those lanes. Yet right next to them on the exact same H100 die sit the Tensor Cores, rated at 989.4 TFLOP/s in 16-bit precision—14.8× faster, dropping the theoretical floor for that same 2048³ matmul from 257 µs down to 17 µs.

Our one-number-at-a-time a * b loop from Part 3 can’t touch a single Tensor Core. Before we put all of these kernel-engineering ideas to work on a real LLM workload, let’s build a visual mental model of what actually changes under the hood—starting from the 2D tiling we already know, walking through the four building blocks (mma.sync, fragments, ldmatrix, and XOR swizzling), and seeing which lines of CUDA code actually trigger each piece.


1. Starting From 2D Tiling: Scalar Lanes vs. Matrix Units

In Part 3’s 2D tiled kernel, each thread owned a small 4 × 4 output patch of C. At every step along k, each thread pulled two tiny vectors from A and B into its own registers and looped over scalar fused multiply-adds (FFMA, computing c = a * b + c one number at a time). Even across a full 32-thread warp, one FFMA instruction only does 32 multiply-adds.

A Tensor Core changes the unit of work from a scalar to a mini-matrix. Instead of each lane multiplying two single numbers, the entire 32-thread warp issues one mma.sync instruction together (Matrix Multiply-Accumulate) on a 16 × 8 tile of A and an 8 × 8 tile of B—computing 1,024 multiply-adds in a single instruction:

One thing that tripped me up at first: I initially pictured each thread running its own mini-matrix multiply. That’s not how it works. No single thread runs mma.sync alone—all 32 threads fire it in unison, and the 16 × 8 output tile is their collective result.

Why is doing a whole matrix tile at once so much faster than 32 scalar lanes? Two reasons:

  1. Reuse amortizes overhead: On silicon, fetching an instruction and pulling operands out of the register file costs roughly 20× more energy than the multiply-add itself. Click “Show A[3][0] Reuse” above—each value from A is reused across all 8 columns of B, and each value from B meets all 16 rows of A, so you pay the fetch overhead once for a whole grid of math.
  2. Narrow inputs: Tensor Cores take 16-bit inputs (FP16 or BF16, 2 bytes each) while keeping the running sums in 32-bit FP32 so accuracy holds over long dot products.

2. Where Tensor Cores Live on the H100

Good news: the memory hierarchy doesn’t change at all.

Inside each of the H100’s 132 active Streaming Multiprocessors (SMs), execution is split into 4 quadrants (the 4 sub-partitions from Part 2). Every quadrant has 1 Tensor Core sitting right beside its 32 CUDA lanes and its 64 KB register file—giving 4 Tensor Cores per SM and 528 across the chip.

Data still travels the exact same path: HBM3 → L2 Cache → Shared Memory (up to 228 KB/SM) → Register Wall. Through mma.sync, a Tensor Core reads only registers—every number it multiplies has to climb down that funnel first.

DimensionCUDA Cores (FFMA, FP32)Tensor Cores (mma.sync, FP16/BF16)
Work per warp instruction32 multiply-adds (1 / lane)1,024 multiply-adds (m16n8k8)
Who issues itEach thread independentlyAll 32 threads of a warp together
Operand types32-bit scalars16-bit inputs, 32-bit accumulators
H100 rated peak66.9 TFLOP/s989.4 TFLOP/s (14.8×)

3. Fragments & ldmatrix: How a Warp Feeds a Tensor Core

Just like in Part 3, work still nests into Blocks → Warps → Threads: a block stages chunks from HBM into Shared Memory, each warp owns a warp tile (say 64 × 64), and the warp steps through that tile using 16 × 8 × 8 mma.sync instructions.

What changes is who owns which registers. In CUDA 2D tiling, each thread owned its own self-contained 4 × 4 box of C. For mma.sync, the A, B, and C tiles are sliced across all 32 threads of the warp. Each thread holds a tiny piece called a fragment (for m16n8k8, just 4 values of A, 2 of B, and 4 running sums of C—7 registers total). You don’t need to memorize which thread holds which cell; the key mental-model shift is simple: a CUDA lane can only reach into its own thread’s registers, whereas a Tensor Core reaches across all 32 threads’ registers in the warp at once.

Loading that exact interleaved pattern one scalar at a time would take 24 Shared Memory loads per thread for a single warp slice. Instead, NVIDIA provides ldmatrix—a warp-cooperative load where just 8 threads pass row pointers for an 8 × 8 tile in Shared Memory, and hardware deals 2 numbers directly into each of the 32 threads’ registers:

ldmatrix also supports loading multiple tiles at once (.x2 for two tiles, .x4 for four) and a built-in hardware transpose (.trans, toggle it above to see how it turns B on its side so each thread gets column elements). That cuts a warp slice’s loads from 24 per thread down to just 4 warp-wide instructions.


4. Clearing Bank Conflicts Without Padding: XOR Swizzling

Remember Shared Memory’s 32 parallel 4-byte banks (the 32 checkout lanes from Part 3)? When ldmatrix reads an 8 × 8 FP16 tile (16 bytes = 4 banks per row) from a chunk whose rows are 128 bytes wide:

  • 128 bytes wraps the 32 banks with zero remainder, so every row starts at Bank 0.
  • All 8 rows pile onto Banks 0–3, leaving Banks 4–31 idle and serializing one load into 8 slow passes.

The classic fix is padding (+16 unused bytes at the end of every row) so each row shifts by 4 banks. That gets you back to 1 pass, but wastes precious Shared Memory—around 15 KB across a 3-stage pipeline out of H100’s 228 KB budget.

XOR swizzling gets the exact same 1-pass read with zero extra bytes. Cut each 128-byte row into 8 pieces of 16 bytes (p = 0..7) and store piece p of row r at slot p ⊕ r (bitwise XOR). Toggle between Plain, Padded, and XOR Swizzled below—and click Piece p = 0 ↻ to verify that every piece across rows 0–7 fans across all 32 banks in 1 pass:

You apply the same rule twice—once when writing from HBM into Shared Memory (row_base + (p ^ r) * 16) and once when passing row pointers to ldmatrix. The entire cost is one XOR instruction per address and zero extra Shared Memory.


5. Mapping the Hardware to Real Code (WMMA)

With the four hardware pieces in mind (mma.sync, fragments, ldmatrix, and swizzling), the natural question is: which lines of code actually hand work off to the Tensor Core?

In CUDA C++, the most approachable entry point is WMMA (Warp Matrix Multiply-Accumulate, nvcuda::wmma). You don’t need to write assembly or memorize register layouts—just look at which line triggers which hardware step:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <mma.h>
using namespace nvcuda;

// One warp: C (16 x 16) += A (16 x 16) * B (16 x 16)
__device__ void tile_wmma(const half *A, int lda,
                          const half *B, int ldb,
                          float *C, int ldc) {
  // 1. Reserve each thread's register slice (fragment) for A, B, and C
  wmma::fragment<wmma::matrix_a, 16, 16, 16, half, wmma::row_major> a;
  wmma::fragment<wmma::matrix_b, 16, 16, 16, half, wmma::row_major> b;
  wmma::fragment<wmma::accumulator, 16, 16, 16, float> c;

  // 2. Cooperatively load tiles into warp registers (compiler lowers to ldmatrix)
  wmma::load_matrix_sync(c, C, ldc, wmma::mem_row_major);
  wmma::load_matrix_sync(a, A, lda);
  wmma::load_matrix_sync(b, B, ldb);

  // 3. Hand execution from the warp's CUDA lanes to the Tensor Core (mma.sync)
  wmma::mma_sync(c, a, b, c);

  // 4. Write the finished 16 x 16 output tile back out
  wmma::store_matrix_sync(C, c, ldc, wmma::mem_row_major);
}

Four lines tell the whole story:

  • wmma::fragment<...> reserves each thread’s slice of the warp’s registers—without forcing you to track which thread holds which index.
  • wmma::load_matrix_sync(...) is where data moves from Shared Memory into those register fragments (under the hood, the compiler lowers this into cooperative loads like ldmatrix).
  • wmma::mma_sync(c, a, b, c) is the exact handoff from the warp’s CUDA lanes to the Tensor Core, firing the matrix multiply-accumulate across all 32 threads in unison.
  • wmma::store_matrix_sync(...) writes the accumulated tile back to memory.

Where does XOR swizzling happen? Because wmma::load_matrix_sync hides the Shared Memory address math, you don’t write swizzling inside WMMA directly. When kernels need custom swizzled layouts—or Hopper’s 4-warp wgmma (warp-group matrix multiply)—frameworks like Triton and CUTLASS / CuTe (or lower-level PTX, NVIDIA’s GPU assembly) handle the swizzled address math and emit the ldmatrix and mma.sync instructions under the hood.


What’s Next: Bringing It All Together in Attention & FlashAttention

We now have all the core kernel-engineering pieces on the table: the GPU memory hierarchy and Roofline ridge (Part 2), tiling, coalescing, and keeping intermediates on-chip (Part 3), and how Tensor Cores multiply mini-matrices in unison (Part 4). Climbing a full Tensor Core GEMM ladder—layering 16-byte global loads, XOR-swizzled Shared Memory, and asynchronous HBM double buffering (cp.async)—is a deep dive I plan to come back to in a future post.

First, I want to connect everything we’ve learned so far to the operation at the heart of every modern LLM: Attention. Next up, we’ll start with standard Attention, see why materializing the attention matrix starves our fast Tensor Cores on HBM traffic, and build up to FlashAttention—where tiling and online softmax combine every concept from this series into one kernel.


Acknowledgments & References

This post is licensed under CC BY 4.0 by the author.