# The SM100 Memory Hierarchy, from HBM to Tensor Memory *Every memory space of a datacenter Blackwell GPU, measured on a B300, and where FlashInfer's NVFP4 GEMM keeps each piece of its data, global scale included* If you have ever interviewed for a job that involves GPU programming, there is a good chance that someone asked you to explain the CUDA memory hierarchy. The textbook answer goes something like this: registers are private to a thread and fastest, shared memory is shared by a thread block and almost as fast, global memory is big and slow, and there are some caches in between. That answer isn't wrong, but on a Blackwell datacenter GPU it leaves out a lot. There is now a separate memory just for the tensor cores, thread blocks can read each other's shared memory, the L2 cache and the global memory are split across two dies, and the fastest matrix multiplication kernels don't keep their results in registers anymore. In this article, I want to give the long version of that answer for SM100, the architecture of NVIDIA's B200 and B300 GPUs. We will start at global memory and work our way down to the registers, one memory space at a time. For each one, I'll say what it is for, how big it is, how long it takes to get data out of it, and how a kernel uses it. I measured the numbers myself on a B300, and I'll show the code. Then, in the second half, we will take a real NVFP4 (4-bit floating point) GEMM, the one FlashInfer runs on SM100, and follow every piece of its data through the hierarchy, including the per-tensor global scale: which bytes sit in global memory, which in shared memory, which in tensor memory and which in registers, and which instruction moves them from one to the next. This is a follow-up to my article on [TMA load, prefetch and multicast in CuTe DSL](https://sleepy-capybara.vercel.app/tma-cute-dsl/), and like that one, all the code here is CuTe DSL (`nvidia-cutlass-dsl` 4.8.0). I'll introduce the GPU terms as we go, but I'll assume that you know what a warp is (32 threads that execute together), a CTA (a thread block, which runs on one SM) and a grid. The scope is SM100 only: I won't cover the consumer Blackwell GPUs (SM120), which have a different memory system and no tensor memory, and I'll only mention Hopper where it explains why something on SM100 is the way it is. ## 1. The Machine We Are Looking At Let's start with the hardware. The B300 I used reports compute capability 10.3. It belongs to the SM100 family, together with the B200 (10.0), and as far as the memory hierarchy goes, the two look the same to a kernel. (FlashInfer's SM100 NVFP4 GEMM, which we'll use in section 4, sizes its shared memory for `sm_100` and ran unchanged on the B300.) You can ask the CUDA driver for most of the numbers that matter. Here is a short script that does that with the `cuda-python` bindings: ```python from cuda.bindings import runtime as rt def attr(name): err, value = rt.cudaDeviceGetAttribute( getattr(rt.cudaDeviceAttr, "cudaDevAttr" + name), 0) return value print("SMs:", attr("MultiProcessorCount")) print("registers per SM:", attr("MaxRegistersPerMultiprocessor")) print("SMEM per SM:", attr("MaxSharedMemoryPerMultiprocessor")) print("SMEM per CTA:", attr("MaxSharedMemoryPerBlockOptin")) print("L2 bytes:", attr("L2CacheSize")) print("constant memory:", attr("TotalConstantMemory")) bus, clock = attr("GlobalMemoryBusWidth"), attr("MemoryClockRate") print("peak HBM TB/s:", bus / 8 * clock * 1e3 * 2 / 1e12) ``` On the B300, this prints 148 SMs, 65,536 registers per SM, 233,472 bytes (228 KB) of shared memory per SM, of which one CTA can use 232,448 bytes (227 KB), 132,644,864 bytes (126.5 MiB) of L2 cache, 64 KB of constant memory, and a peak memory bandwidth of 7.67 TB/s, which comes from the 7,680-bit memory bus running at 3,996 MHz with two transfers per clock. Figure 1 below puts all of these memories in one picture. I'll go through them from the bottom (far from the threads) to the top (close to the threads), which is the order in which data usually travels. ![The memory spaces of an SM100 GPU](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig01_overview.png) *Figure 1: The memory hierarchy of a B300. Two dies with 74 SMs each, each with its part of the L2 cache and of the HBM. Inside every SM: registers, tensor memory, the combined L1 cache and shared memory, a constant cache and a TMA unit.* One thing in Figure 1 that the classic interview answer never mentions is that the GPU is made of two chips. NVIDIA builds Blackwell from two reticle-sized dies, connected by a link called NV-HBI that carries 10 TB/s, and presents them to software as a single GPU. The full chip has 160 SMs, of which this B300 has 148 enabled. We will see in section 2 that you can measure where one die ends and the other begins. Before we go through the memories, let me explain how I measured their latency, since we'll use the same measurement for almost every level. ### 1.1 Measuring Latency with a Pointer Chase The classic way to measure the latency of a memory is a pointer chase: we fill a buffer with a chain of indices, where each element holds the index of the next element to read, and then a single thread follows the chain. Since every load needs the result of the previous one to know its address, the loads cannot overlap, and the time per step is the latency of one load. By changing the size of the buffer, we decide where the data lives: a buffer of 64 KB fits into the L1 cache, a buffer of 16 MB fits into L2 but not L1, and a buffer of 1 GB fits nowhere and has to come from HBM. Here is the kernel, slightly cut down from the one I used for the measurements: ```python import torch import cutlass import cutlass.cute as cute from cutlass.cute.runtime import from_dlpack HOPS = 4096 @cute.kernel def chase(a: cute.Tensor, res: cute.Tensor, lap: cutlass.Int32): idx = cutlass.Int32(0) for _ in cutlass.range(lap, unroll=1): # one lap to warm the caches idx = a[idx] t0 = cute.arch.clock64() for _ in cutlass.range(HOPS, unroll=1): idx = a[idx] # each load needs the previous one t1 = cute.arch.clock64() res[0] = cutlass.Int64(idx) res[1] = t1 - t0 @cute.jit def run(a: cute.Tensor, res: cute.Tensor, lap: cutlass.Int32): chase(a, res, lap).launch(grid=(1, 1, 1), block=(1, 1, 1)) ``` And here is how we build the chain. It visits one element in every 128-byte line of the buffer (128 bytes is the size of a cache line), in random order, so that two consecutive loads never hit the same line: ```python def make_chain(nbytes): lines = nbytes // 128 order = torch.randperm(lines) a = torch.zeros(lines * 32, dtype=torch.int32) a[order * 32] = (torch.roll(order, -1) * 32).to(torch.int32) return a.cuda(), lines res = torch.zeros(2, dtype=torch.int64, device="cuda") for nbytes in (64 << 10, 16 << 20): a, lines = make_chain(nbytes) run(from_dlpack(a), from_dlpack(res), lines) torch.cuda.synchronize() print(nbytes >> 10, "KB:", res[1].item() / HOPS, "cycles per load") ``` `clock64()` reads the SM's cycle counter, so all latencies in this article are in SM clock cycles. (The B300's SM clock goes up to 2,032 MHz, so at full speed, 100 cycles are about 50 nanoseconds.) For shared memory, I used the same chase on a copy of the chain in shared memory. Figure 2 shows what came out, for buffers from 8 KB to 1 GB. ![Latency of one load as a function of the buffer size](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig02_latency.png) *Figure 2: Cycles per dependent load on a B300, from a single thread, as the buffer the chase walks through grows from 8 KB to 1 GB. The dashed lines are the same chase in the CTA's own shared memory and in another CTA's shared memory.* As we can see in Figure 2, there are three clear plateaus. Up to 128 KB, every load hits the L1 cache and takes 38 cycles. From 512 KB to 32 MB, every load misses L1, hits L2 and takes about 307 cycles. From 256 MB on, every load goes out to HBM and takes about 853 cycles on average. The shared memory line sits at 29 cycles, a bit below an L1 hit, and loading from another CTA's shared memory takes 219 cycles. We will come back to each of these numbers in the sections below. (The point at 256 KB, 195 cycles, is a mix of L1 hits and misses, since the L1 cache and shared memory share 256 KB of storage, so the L1 can't hold all of it.) ## 2. Global Memory and HBM **What it is.** Global memory is the GPU's main memory. Physically, it's HBM3e (high-bandwidth memory), stacks of DRAM chips that sit right next to the GPU dies in the same package. On the B300, it holds 288 GB (the driver reports 267.7 GiB), and every thread of every kernel can read and write all of it. When you allocate a PyTorch tensor on the GPU, this is where it lives. **How fast it is.** We already saw that a dependent load from HBM takes about 853 cycles, which is the latency side. For bandwidth, I timed a plain device-to-device copy (`dst.copy_(src)`) of 8 GiB, which moves 6.66 TB/s (counting both the reads and the writes), or 87% of the 7.67 TB/s peak we computed from the bus width and the clock. Those two numbers together are why GPU kernels are written the way they are: 853 cycles is a long time to wait, but if enough loads are in flight at the same time, the memory system can deliver 6.66 TB/s. (Little's law again, which I used in the TMA article: to keep 6.66 TB/s flowing with a latency of about 420 ns (853 cycles at the maximum clock), about 2.8 MB have to be in flight at any moment, spread over all the SMs.) **The two dies.** The 853 cycles are an average, and that average hides something interesting. I changed the pointer chase so that it times every single load on its own, and plotted how often each latency occurs. Figure 3 shows the result for a 16 MB buffer, where all loads hit L2, and for a 1 GB buffer, where all loads go to HBM. ![Histogram of single-load latencies for L2 hits and for HBM loads](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig03_two_dies.png) *Figure 3: Every load of the pointer chase timed separately. L2 hits all take about the same time. Loads from HBM fall into two groups, around 600 and around 1,000 cycles.* As shown in Figure 3, the L2 hits form a single narrow peak, but the HBM loads form two peaks, one around 600 cycles and one around 1,000 cycles, with roughly half of the loads in each. My first guess was that the slow half were TLB misses (the TLB caches the translation from virtual to physical addresses). To check, I ran the exact same chase, with the same addresses in the same order, from one SM at a time, for all 148 SMs. If the slow loads were TLB misses, the same addresses would be slow no matter which SM runs the chase. Instead, the SMs split into two groups of exactly 74, and an address that is slow for one group is fast for the other: two SMs from different groups agree on fewer than 10% of the loads. That matches the two dies. Each die has its own HBM stacks, and a load from memory that is attached to the other die has to cross the NV-HBI link, which costs about 400 extra cycles. (The SM numbers don't tell you the die in an obvious way. SMs 0 and 1 are on one die, 2 to 7 on the other, 8 and 9 on the first again, and so on, always in pairs.) For most kernels, this doesn't matter much, since a large tensor is spread evenly over both dies, so every SM gets half fast and half slow loads. But it's good to know when you look at latency numbers, and it's a nice example of something the textbook hierarchy doesn't show. **How a kernel uses it.** In CuTe DSL, a tensor in global memory is what you get from `from_dlpack(torch_tensor)`, and a kernel can index it directly, as the pointer chase does. Each such access compiles to an ordinary load (`ld.global`) or store (`st.global`). For moving whole tiles into shared memory, the fast path on SM100 is the TMA, which we will meet again in section 4. ## 3. From L2 Down to the Registers ### 3.1 The L2 Cache **What it is.** The L2 cache sits between the SMs and HBM, and every load and store from any SM to global memory goes through it. On the B300 it's 126.5 MiB in total, which is large enough to hold a surprising amount of a model's working data. Unlike the L1, the L2 is shared by all SMs, which is what makes it useful as a place where different CTAs meet: when 32 CTAs all need the same tile of a matrix, only the first one has to bring it in from HBM. **How fast it is.** An L2 hit took about 307 cycles in the pointer chase, compared with 853 for HBM. For bandwidth, a copy of 32 MiB (64 MiB of reads and writes together, small enough to stay in L2) reached 10.0 TB/s with PyTorch's ordinary copy kernel, against 6.66 TB/s from HBM. I'd treat that 10 TB/s as a lower bound, since the copy kernel isn't written to push the L2 as hard as it can. **Where the 126.5 MiB go.** Here is something that I can't fully explain. In Figure 2, the latency starts to rise already at 64 MB, which is only half of the L2. Timing every load at 64 MB, about 60% are L2 hits at around 270 cycles, and the other 40% take about 620 cycles, but none take 1,000 cycles, which is what a load from the other die's HBM costs. Since the L2 is physically split between the two dies, my guess is that a single SM can keep only about half of the L2 close to itself, and the rest of the lines it needs are served from the other die's L2, which costs about as much as a load from its own die's HBM. But that is a guess from the measurements, and I haven't found it documented. **How a kernel uses it.** Mostly, you don't manage the L2 directly. You influence it in three ways. The order in which CTAs run decides how much they reuse each other's data while it is still in L2 (this is what tile schedulers and "rasterization" in GEMMs are about). Loads and TMA copies can carry cache hints that say how long a line should stay. And a CUDA stream can mark a window of memory as "persisting", which reserves part of the L2 for it (up to 79 MiB on the B300, according to the driver). The TMA prefetch from my previous article is another tool: it brings a tile into L2 without bringing it into the SM. ### 3.2 Constant Memory and Kernel Parameters **What it is.** Constant memory is a small (64 KB) region of global memory that kernels can only read, and that is cached in a small constant cache in every SM. Its special trick is broadcasting: when all 32 threads of a warp read the same constant address, the value is delivered to all of them at once. If the threads read different addresses, the reads are serialized. **Why it matters in practice.** Even if you never declare a constant yourself, every kernel uses constant memory, because that is where the kernel parameters live. When you launch a kernel, its arguments are copied into constant bank 0, next to some values that the driver puts there, and the kernel reads them from there. In the SASS (the GPU's machine code) of the NVFP4 GEMM we'll look at later, the first instructions include these: ```text LDC R1, c[0x0][0x37c] LDC.64 R2, c[0x0][0x788] LDCU.64 UR4, c[0x0][0x358] ``` `LDC` and `LDCU` load from constant memory, `c[0x0][...]` is an offset into constant bank 0, and `cuobjdump -res-usage` reports that this GEMM uses 1,936 bytes of that bank. The second line, for example, loads a 64-bit pointer that the kernel uses a few instructions later to load the GEMM's global scale from global memory (we'll meet it in section 4.6). Among the parameters are also five TMA descriptors, each of which is a 128-byte object that the host builds and passes in. That is also why a TMA descriptor cannot be changed by the kernel: it sits in read-only constant memory. ### 3.3 The L1 Cache **What it is.** Every SM has 256 KB of on-chip storage that is shared between the L1 data cache (which also serves as the texture cache) and shared memory. The split, which NVIDIA calls the carveout, can be chosen per kernel: on SM100, shared memory can take 0, 8, 16, 32, 64, 100, 132, 164, 196 or 228 KB of it, and the rest is L1. A kernel that uses a lot of shared memory, like the GEMM in section 4, therefore leaves only a small L1. **How fast it is.** An L1 hit took 38 cycles in the pointer chase, about 8 times faster than an L2 hit. The pointer chase used ordinary loads, and they hit in L1, so global loads are cached in L1 by default. **How a kernel uses it.** Like the L2, the L1 is managed by the hardware. It helps most when the threads of an SM touch the same data more than once in an irregular way, where it would be awkward to stage the data in shared memory by hand. For the regular, tile-shaped data of a GEMM, the TMA copies data from L2 straight into shared memory, without going through the L1 at all. ### 3.4 Local Memory **What it is.** Local memory is the most misleading name in the whole hierarchy. It's private to one thread, like a register, but it lives in global memory, out in HBM, and it is cached in L1 and L2 like any other global data. The compiler uses it for things that don't fit into registers: when a kernel needs more registers than it is allowed to use (register spilling), and for arrays that the thread indexes with a value that is only known at run time, since registers can't be indexed dynamically. The second case is easy to run into in CuTe DSL. A "register tensor" made with `cute.make_rmem_tensor` really lives in registers only as long as every index into it is a compile-time constant. Here are two versions of a kernel that keeps 64 floats per thread in such a tensor and then sums them in a scrambled order: ```python N = 64 @cute.kernel def regs(x: cute.Tensor, y: cute.Tensor, dynamic: cutlass.Constexpr): tidx, _, _ = cute.arch.thread_idx() r = cute.make_rmem_tensor(cute.make_layout(N), cutlass.Float32) for i in cutlass.range_constexpr(N): r[i] = x[tidx * N + i] acc = cutlass.Float32(0) if cutlass.const_expr(dynamic): for i in cutlass.range(N, unroll=1): # index known at run time acc += r[(i * 7 + tidx) % N] else: for i in cutlass.range_constexpr(N): # index known when compiling acc += r[(i * 7) % N] y[tidx] = acc @cute.jit def run_regs(x: cute.Tensor, y: cute.Tensor, dynamic: cutlass.Constexpr): regs(x, y, dynamic).launch(grid=(1, 1, 1), block=(32, 1, 1)) ``` Both versions compute the same sums. But when I kept the compiled binaries (with `CUTE_DSL_KEEP=cubin`) and looked at them with `cuobjdump`, the first version uses no local memory at all, and the second has a 272-byte stack frame in local memory and 17 `LDL`/`STL` instructions (local loads and stores), because the compiler had to put the array somewhere it can index at run time. So, when a kernel is unexpectedly slow, it is worth checking the resource usage for `STACK` or `LOCAL`, and the SASS for `LDL` and `STL`. ### 3.5 Shared Memory **What it is.** Shared memory (SMEM) is the part of the SM's 256 KB that the program manages itself. Each CTA gets its own piece of it, up to 227 KB on SM100 (CUDA keeps 1 KB per CTA for itself), which all threads of the CTA can read and write, and which is gone when the CTA finishes. Shared memory is divided into 32 banks of 4 bytes each, and a warp's access is fast when its 32 threads hit 32 different banks. When several threads hit different addresses in the same bank, the accesses are serialized, which is called a bank conflict. This is why the shared memory layouts of a GEMM are "swizzled": the addresses are permuted so that the reads of a warp spread over all banks. **How fast it is.** 29 cycles per dependent load in the pointer chase, a little faster than an L1 hit, presumably because no cache tags have to be checked. **What it holds on SM100.** On older GPUs, shared memory was mostly a scratchpad that threads wrote to and read from. On SM100, it has become the place where the asynchronous units meet. The TMA writes tiles into shared memory. The tensor cores read their B operand (and usually their A operand) directly from shared memory, through a 64-bit "matrix descriptor" that describes where the tile is and how it's laid out, so the data never passes through registers. And the mbarriers, the synchronization objects that the TMA and the tensor cores use to say "this tile has arrived" or "I'm done with this tile", are 8-byte objects in shared memory too. We will see all of this in the GEMM. ### 3.6 Distributed Shared Memory **What it is.** CTAs can be grouped into clusters (up to 8 CTAs portably, 16 on B200 if the kernel opts in), and the hardware guarantees that all CTAs of a cluster run at the same time, on nearby SMs. Because of that, a CTA can read and write the shared memory of the other CTAs in its cluster, which is called distributed shared memory (DSMEM). In PTX, a CTA gets the address of the same offset in another CTA's shared memory with `mapa`, and then uses ordinary `ld.shared::cluster` and `st.shared::cluster` instructions. (CuTe DSL has `cute.arch.map_dsmem_ptr` for the first part, but in version 4.8 it won't load through the resulting pointer, so for the measurement below I wrote the two PTX instructions with inline assembly.) **How fast it is.** I ran the pointer chase in a cluster of two CTAs, once through the CTA's own shared memory and once through the other CTA's shared memory, both with `ld.shared::cluster`. The own shared memory took 32 cycles per load and the other CTA's 219 cycles, which is slower than local shared memory but still faster than an L2 hit (307). **How a kernel uses it.** In GEMMs, DSMEM is mostly used indirectly. A TMA multicast load writes one tile into the shared memory of several CTAs of a cluster at once, and the 2-CTA tensor core instructions of SM100 read their operands from the shared memory of two CTAs. We'll see both in section 4.3. ### 3.7 Tensor Memory **What it is.** Tensor memory (TMEM) is new in SM100, and it's the part of the hierarchy that the classic interview answer misses completely. It is a dedicated on-chip memory for the tensor cores, with 128 rows, which PTX calls lanes, and 512 columns of 32-bit cells per CTA, so 256 KB in total, the same size as the register file. Figure 4 below shows its shape. ![The layout of tensor memory and how the NVFP4 GEMM uses it](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig04_tmem.png) *Figure 4: Tensor memory has 128 lanes and 512 columns of 32-bit cells. Each warp of a warpgroup can only access its own 32 lanes. The colored part is what the NVFP4 GEMM of section 4 uses.* **Why it exists.** On Hopper, the tensor core instructions (`wgmma`) read one operand from shared memory and kept the accumulator, the running sum of the matrix product, in the registers of the threads. A 128 x 256 fp32 accumulator is 128 KB, which is half of the register file of an SM, so the registers that the epilogue and everything else needed were always tight. SM100 moves the accumulator out of the registers and into TMEM. The tensor core instruction `tcgen05.mma` writes its result D into TMEM, reads A from TMEM or shared memory and B from shared memory, and for block-scaled formats like NVFP4 it also reads the scale factors from TMEM. And unlike `wgmma`, which a whole warpgroup issues together, `tcgen05.mma` is issued by a single thread. **How a kernel uses it.** TMEM isn't allocated automatically. One warp allocates columns with `tcgen05.alloc`, in units of 32 columns and always a power of 2, and the allocation has to be freed with `tcgen05.dealloc` before the kernel exits. Data moves in and out with three instructions: `tcgen05.cp` copies from shared memory into TMEM, `tcgen05.ld` loads from TMEM into registers, and `tcgen05.st` stores from registers into TMEM. There is one access rule that shapes every SM100 epilogue: a warp can only load and store its own quarter of the lanes. Warp 0 of a warpgroup (a group of 4 consecutive warps) gets lanes 0 to 31, warp 1 gets lanes 32 to 63, and so on, while every warp can reach all 512 columns. Since the accumulator's rows are the lanes, the four warps of the epilogue naturally split the output tile into four bands of 32 rows. ### 3.8 Registers **What it is.** Registers are the fastest storage and private to each thread. An SM has 65,536 32-bit registers (256 KB), and a thread can use at most 255 of them. The registers are divided among all threads that run on an SM at the same time, so a kernel that uses many registers per thread can run fewer threads at once, which is the classic occupancy trade-off. On SM100, the SASS also has a second kind of register, the uniform registers (`UR0`, `UR1`, ...), which hold one value for the whole warp. We'll see them in the GEMM, where they hold things like TMEM addresses and shared memory descriptors, which are the same for every thread. **What they hold on SM100.** With the accumulator in TMEM and the operands in shared memory, the registers of a GEMM's main loop hold surprisingly little: addresses, loop counters and barrier phases. They fill up in the epilogue, where the accumulator comes out of TMEM so that the threads can convert it and store it. ## 4. Where an NVFP4 GEMM Keeps Its Data Now let's put the whole hierarchy to work on a real kernel. The one we'll follow is FlashInfer's NVFP4 GEMM for SM100, `Sm100BlockScaledPersistentDenseGemmKernel` in [`flashinfer/gemm/kernels/dense_blockscaled_gemm_sm100.py`](https://github.com/flashinfer-ai/flashinfer/blob/c0e62fa7b18cbf051f729214984c235eb521a551/flashinfer/gemm/kernels/dense_blockscaled_gemm_sm100.py). It's written in CuTe DSL, and it's what `flashinfer.mm_fp4(..., backend="cute-dsl")` runs on SM100, so it's a kernel that serving engines actually call. It uses every memory space from sections 2 and 3, including the global scale of the NVFP4 format, which simpler examples tend to leave out. I built the kernel directly, with the same compile helper that `mm_fp4` uses, and ran it on the B300 with M = N = K = 8,192 and a bf16 output. (I bypassed `mm_fp4`'s autotuner so that I could pin one configuration and print its memory budget while it compiles. At this shape, every option the autotuner considers on the B300 is this same kernel with different tile, cluster and prefetch settings.) The configuration is the one that came out fastest for this shape: an MMA tile of 256 x 256 computed by a pair of CTAs, a cluster of 2 x 1, and the L2 hint that `mm_fp4` picks for this shape, which I'll get to in section 4.2. Its output was bit-identical to FlashInfer's CUTLASS backend on the same inputs, and with a cold L2 it took 157.7 microseconds, or 6.97 PFLOP/s (2 x 8,192^3 floating-point operations in that time). ### 4.1 What It Computes, Including the Global Scale NVFP4 is a 4-bit floating-point format with two levels of scaling. Every element is a 4-bit `e2m1` number (1 sign bit, 2 exponent bits, 1 mantissa bit), whose largest value is 6. Every block of 16 consecutive elements along K shares one 8-bit `e4m3` scale factor, whose largest value is 448. And on top of that, the whole tensor has one fp32 global scale g. FlashInfer's tests compute it as g = 448 x 6 / amax, where amax is the largest absolute value in the tensor, so that the tensor is stretched to the range that an e4m3 scale times an e2m1 value can represent. Each value x is then stored as a 4-bit q with its block's scale s, so that q x s is approximately x x g. So, if we multiply two quantized matrices with their block scales, we get the product of the stretched matrices, (A gA)(B gB)^T. To get back to A B^T, we have to divide by gA x gB at the end. FlashInfer calls that number alpha: alpha = 1 / (gA x gB), C = alpha x sum over k of (qA x sA)(qB x sB). The block scales go into the tensor cores, which apply them inside the MMA instruction. The global scale can't go there. The block-scaled `tcgen05.mma` takes e4m3 scale factors from TMEM, one per 16 elements, and has no operand for a per-tensor fp32 number. And we can't fold g into the block scales either, since g is exactly what keeps the block scales inside e4m3's range. So, alpha is applied in the epilogue, after the accumulator is finished, and we'll see exactly where in section 4.6. The kernel has 6 warps per CTA, and each has one job. Warp 5 issues all TMA loads, warp 4 issues the tensor core instructions, and warps 0 to 3 run the epilogue. It's also persistent: instead of one CTA per tile, it launches as many clusters as fit on the GPU at once (74 clusters of 2 CTAs, one CTA per SM on the B300), and each cluster loops over output tiles. ### 4.2 Global Memory, L2 and Constant Memory For M = N = K = 8,192, the kernel's data in global memory is: - A: 8,192 x 8,192 fp4 values, two per byte, so 32 MiB. - B: the same, 32 MiB. - SFA and SFB: one e4m3 byte per 16 values, so 4 MiB each, stored in a tiled layout (FlashInfer's 128 x 4 scale-factor layout) so that the bytes one tile needs are contiguous. - C: 8,192 x 8,192 bf16 values, 128 MiB. - alpha: a single fp32 number, 4 bytes, in a one-element tensor. That is 200 MiB that have to cross the HBM boundary at least once, which takes about 31 microseconds at the 6.66 TB/s we measured, while the kernel takes 158 microseconds. So, this GEMM is limited by the tensor cores and by how fast data can be fed to them, not by HBM bandwidth. Each 256-row tile of A is needed by all 32 output tiles in its row, and each 256-column tile of B by all 32 tiles in its column, so A and B are read many times, but from L2 rather than from HBM. FlashInfer helps the L2 keep them with a cache hint. For this shape, `mm_fp4` loads both A and B with an `evict_last` policy, which tells the L2 to evict these lines after other data. Without the hint, the same kernel took 159.7 instead of 157.7 microseconds, so the hint buys about 1.3%. The kernel parameters live in constant bank 0, as described in section 3.2. `cuobjdump` reports 1,936 bytes of it for this kernel, and among the parameters are five TMA descriptors, one each for A, B, SFA, SFB and C. The first thing warp 5 does is prefetch all five of them (`cpasync.prefetch_descriptor`, which shows up as five `UTMACCTL.PF` instructions in the SASS), so that the first TMA copies don't have to wait for their descriptors. ### 4.3 Shared Memory: Five Stages and an Output Buffer Here is the kernel's shared memory, cut down (`# ...` marks what I left out): ```python # excerpt from dense_blockscaled_gemm_sm100.py @cute.struct class SharedStorage: ab_full_mbar_ptr: cute.struct.MemRange[cutlass.Int64, self.num_ab_stage] ab_empty_mbar_ptr: cute.struct.MemRange[cutlass.Int64, self.num_ab_stage] acc_full_mbar_ptr: cute.struct.MemRange[cutlass.Int64, self.num_acc_stage] acc_empty_mbar_ptr: cute.struct.MemRange[cutlass.Int64, self.num_acc_stage] tmem_dealloc_mbar_ptr: cutlass.Int64 tmem_holding_buf: cutlass.Int32 # (EPI_TILE_M, EPI_TILE_N, STAGE) sC: cute.struct.Align[ cute.struct.MemRange[ self.c_dtype, cute.cosize(self.c_smem_layout_staged.outer), ], self.buffer_align_bytes, ] # (MMA, MMA_M, MMA_K, STAGE) sA: cute.struct.Align[ cute.struct.MemRange[ self.a_dtype, cute.cosize(self.a_smem_layout_staged.outer) ], self.buffer_align_bytes, ] # ... the same for sB, sSFA and sSFB ``` The first six fields are the small things. There is a "full" and an "empty" mbarrier for every stage of the operand ring buffer: the TMA completes "full" when all bytes of a stage have arrived, and the tensor cores complete "empty" when they're done reading it. There is a pair of mbarriers for the accumulator, which the MMA warp and the epilogue warps use to hand it back and forth, and one more that the two CTAs of a pair use to agree on when TMEM may be freed. And there is `tmem_holding_buf`, a 32-bit word where the TMEM allocation instruction writes the address of the TMEM it allocated, since that instruction returns its result through shared memory. Then come the big buffers. When I printed the sizes that the kernel computed while compiling, each stage of the operand ring buffer was 16 KB of A, 16 KB of B, 2 KB of SFA and 4 KB of SFB, 38 KB in total. The kernel computes how many stages fit (in `_compute_stages`): it reserves 1 KB for the barriers and two output buffers, divides the rest by 38 KB and gets 5 stages, and then gives the remaining space to the output buffer, which ends up with 4 stages of 8 KB. Figure 5 shows the result. ![Shared memory budget of FlashInfer's NVFP4 GEMM](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig05_smem_budget.png) *Figure 5: The shared memory of one CTA of FlashInfer's NVFP4 GEMM: the mbarriers, four 8 KB output buffers for C, and five stages of A, B, SFA and SFB, 222 KB out of the 227 KB that one CTA can have.* Why only 16 KB of B per stage, when a 256 x 256 tile of B with K = 256 is 32 KB? Because the tile is computed by two CTAs together, with the 2-CTA version of the MMA instruction (`cta_group::2`). Each CTA of the pair keeps its own 128 rows of A and its own 128 columns of B, and the MMA, issued by the first CTA of the pair (the leader), reads B from the shared memory of both CTAs, which is the distributed shared memory from section 3.6. Figure 7 shows what each CTA holds. ![What each CTA of the pair holds](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig07_two_cta.png) *Figure 7: One 256 x 256 output tile is computed by two CTAs. Each holds half of A and half of B, both hold all scale factors of B, and each keeps its own 128 rows of the accumulator in its TMEM.* With a single CTA and a 128 x 256 tile, each stage would need all 32 KB of B, 54 KB per stage, and only three stages would fit next to the output buffer. I ran that configuration too, and it was 27% slower (200.7 instead of 157.7 microseconds), which is a good illustration of how much the shared memory budget matters. Warp 5 fills the stages with four TMA copies per K step of 256, all of which complete on the stage's "full" mbarrier: ```python # excerpt from dense_blockscaled_gemm_sm100.py cute.copy( tma_atom_a, tAgA_slice[(None, ab_producer_state.count)], tAsA[(None, ab_producer_state.index)], tma_bar_ptr=ab_pipeline.producer_get_barrier(ab_producer_state), mcast_mask=a_full_mcast_mask, cache_policy=a_cache_policy, ) # ... the same for B, SFA and SFB ``` Note the two extra arguments compared with the TMA load in my previous article: `mcast_mask` for multicast and `cache_policy` for the L2 hint from section 4.2. In the SASS, the four copies are `UTMALDG.2D.2CTA` (twice), `UTMALDG.3D.2CTA` and `UTMALDG.3D.MULTICAST.2CTA`. The `.2CTA` forms are the pair versions: the copies of both CTAs complete on the leader CTA's barrier, since only the leader waits on it before issuing the MMA. The one multicast copy is SFB: both CTAs need the scale factors of all 256 columns of B, so one copy delivers them into the shared memory of both. ### 4.4 Tensor Memory: Two Accumulators That Overlap At the start, the first epilogue warp allocates all 512 columns of TMEM (`cute.arch.alloc_tmem`, with `is_two_cta=True`, so the pair allocates together). How the kernel uses those 512 columns is where it gets clever. A 128 x 256 fp32 accumulator takes 256 columns, and the scale factors of one K step take 16 columns for SFA and 32 for SFB. That leaves 208 columns, not enough for a second, separate accumulator. But a second accumulator is what lets the tensor cores start on the next tile while the epilogue warps are still reading out the current one. So the kernel makes the two accumulators overlap: ```python # excerpt from dense_blockscaled_gemm_sm100.py if cutlass.const_expr(self.overlapping_accum): num_acc_stage_overlapped = 2 tCtAcc_fake = tiled_mma.make_fragment_C( cute.append(acc_shape, num_acc_stage_overlapped) ) # (MMA, MMA_M, MMA_N, STAGE) tCtAcc_fake = cute.make_tensor( tCtAcc_fake.iterator, cute.make_layout( tCtAcc_fake.shape, stride=( tCtAcc_fake.stride[0], tCtAcc_fake.stride[1], tCtAcc_fake.stride[2], (256 - self.num_sf_tmem_cols) * tCtAcc_fake.stride[0][1], ), ), ) ``` The second accumulator starts 256 - 48 = 208 columns after the first, instead of 256. So the accumulator of one tile sits in columns 0 to 255, the accumulator of the next tile in columns 208 to 463, and the 48 columns from 208 to 255 belong to both. The scale factors take the last 48 columns, 464 to 511, which matches the SASS, where the scale-factor copies write to TMEM columns `0x1d0` (464) to `0x1fc` (508). Figure 4 shows the whole layout. ![The tensor memory layout of FlashInfer's NVFP4 GEMM](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig04_tmem.png) *Figure 4: Tensor memory in FlashInfer's NVFP4 GEMM. Two 256-column accumulators overlap in 48 columns, and the scale factors take the last 48 columns. Each warp of the epilogue can only reach its own 32 lanes, which are its 32 rows of the output tile.* The overlap works because of the order in which the epilogue reads the accumulator. It reads 32 columns at a time, and it starts with the shared columns: the accumulator in columns 0 to 255 is read backwards (from the high columns), the one in columns 208 to 463 forwards (from the low columns). After the first two 32-column pieces, which cover the 48 shared columns, the epilogue tells the MMA warp that it may start writing the next accumulator, and the tensor cores start on the next tile while the epilogue reads out the remaining 192 columns. ### 4.5 The Tensor Cores: Reading SMEM and TMEM, Writing TMEM The MMA warp waits for a stage to be full, copies its scale factors from shared memory into TMEM, and issues the matrix multiplications. All of this happens on the leader CTA only: ```python # excerpt from dense_blockscaled_gemm_sm100.py if is_leader_cta: # Conditionally wait for AB buffer full ab_pipeline.consumer_wait( ab_consumer_state, peek_ab_full_status ) # Copy SFA/SFB from smem to tmem # ... cute.copy( tiled_copy_s2t_sfa, tCsSFA_compact_s2t_staged, tCtSFA_compact_s2t, ) # ... the same for SFB # tCtAcc += tCrA * tCrSFA * tCrB * tCrSFB num_kphases = cute.size(tCrA, mode=[2]) for kphase_idx in cutlass.range(num_kphases, unroll_full=True): # ... cute.gemm( tiled_mma, tCtAcc, tCrA[kphase_coord], tCrB[kphase_coord], tCtAcc, ) # ... # Async arrive AB buffer empty ab_pipeline.consumer_release(ab_consumer_state) ``` The copy from shared memory into TMEM is `tcgen05.cp` (`UTCCP.T.S.2CTA` in the SASS). It's needed because the TMA can only write to shared memory, but the tensor cores want the scale factors in TMEM. A K step of 256 is then four MMA instructions of K = 64, each of which becomes one SASS instruction: ```text UTCOMMA.2CTA.BLOCK16 gdesc[UR34], gdesc[UR36], tmem[UR23], tmem[UR4], idesc[UR5], tmem[UR24], UP0 ``` Its operands are a shared memory descriptor for A (`gdesc`), one for B, three TMEM addresses (the accumulator and the two scale-factor tensors) and an instruction descriptor (`idesc`) for the types and shape. In one instruction, issued by one thread, the tensor cores of both CTAs read A and B from shared memory and the scale factors from TMEM, and add into the accumulators in both CTAs' TMEM. Despite their names, `tCrA` and `tCrB` are not in registers: they are views of the shared memory tiles. No register holds any matrix data in the main loop, which is why the whole kernel gets by with 51 registers per thread. `consumer_release` at the end becomes a `tcgen05.commit` (`UTCBAR.2CTA.MULTICAST`), which tells the tensor cores to arrive on the stage's "empty" mbarrier in both CTAs once the MMAs issued so far have finished reading. The thread can't simply mark the stage free when it has issued the MMAs, since they run asynchronously and might still be reading. After the last K step of a tile, the MMA warp does the same for the accumulator's "full" mbarrier, which is the signal for the epilogue. ### 4.6 The Epilogue: Where the Global Scale Is Applied Now we come to the question of where alpha is applied. It happens in registers, in fp32, between reading the accumulator out of TMEM and writing the bf16 result into shared memory. Let's follow it. Alpha is read once, at the very top of the kernel: ```python # excerpt from dense_blockscaled_gemm_sm100.py if cutlass.const_expr(self.per_token_alpha is not None): alpha_value = cutlass.Float32(1.0) else: alpha_value = alpha[0].to(cutlass.Float32) ``` In the SASS, this is one of the very first instructions of the kernel, `LDG.E R41, desc[UR4][R2.64]`, which uses the pointer loaded from constant memory in section 3.2: an ordinary global load of the one fp32 number into register `R41`, where it stays for the whole kernel. (The `per_token_alpha` branch is for a different mode, where alpha is a vector with one scale per row or column and is multiplied in element by element in the same place. We use the scalar mode.) Then the four epilogue warps wait on the accumulator's "full" mbarrier and walk through the 128 x 256 tile in subtiles of 128 x 32. Here is one subtile, cut down: ```python # excerpt from dense_blockscaled_gemm_sm100.py for subtile_idx in cutlass.range(subtile_cnt): # ... tTR_tAcc_mn = tTR_tAcc[(None, None, None, real_subtile_idx)] cute.copy(tiled_copy_t2r, tTR_tAcc_mn, tTR_rAcc) # ... acc_vec = tiled_copy_r2s.retile(tTR_rAcc).load() # Multiply alpha in FP32 before converting to c_dtype to # avoid overflow when c_dtype is FP16 and acc values are large. acc_vec = epilogue_op((alpha_value * acc_vec).to(self.c_dtype)) tRS_rC.store(acc_vec) # # Store C to shared memory # c_buffer = (num_prev_subtiles + subtile_idx) % self.num_c_stage cute.copy( tiled_copy_r2s, tRS_rC, tRS_sC[(None, None, None, c_buffer)], ) # Fence and barrier to make sure shared memory store is visible to TMA store cute.arch.fence_proxy( "async.shared", space="cta", ) # ... if warp_idx == self.epilog_warp_id[0]: cute.copy( tma_atom_c, bSG_sC[(None, c_buffer)], bSG_gC[(None, real_subtile_idx)], ) ``` And here is what one subtile turns into in the SASS, in order: 1. **TMEM to registers.** `LDTM.x32 R4, tmem[UR8]`: each thread loads 32 fp32 values of its own lane, so the 128 threads hold the 128 x 32 subtile, one row segment each. 2. **Multiply by alpha.** 16 `FMUL2` instructions like `FMUL2 R12, R41.F32, R12.F32x2.HI_LO`, each of which multiplies two fp32 values by alpha in `R41`. This is the global scale. 3. **Convert to bf16.** 16 `F2FP.BF16.F32.PACK_AB` instructions, each of which packs two fp32 values into two bf16 values in one register. The comment in the code explains why the multiplication comes first: if alpha is small and the accumulator large, converting first could overflow the output type. 4. **Registers to shared memory.** Four `STS.128` instructions write the 64 bytes of bf16 into one of the four 8 KB output buffers. 5. **Make the data visible to the TMA.** `MEMBAR`, then `FENCE.VIEW.ASYNC.S` (the `fence_proxy` call), then a barrier for the 128 epilogue threads (`BAR.SYNC 0x1, 0x80`). The fence is needed because the TMA reads shared memory through a different path (the "async proxy") than ordinary stores, and without the fence it might not see them. 6. **Shared memory to global memory.** One elected thread of warp 0 issues the TMA store, `UTMASTG.2D`, followed by `UTMACMDFLUSH`. Then `DEPBAR.LE SB0, 0x3` waits until at most three TMA stores are still in flight, so that the output buffer about to be reused has been written out. So, the order is TMEM, registers (multiply by alpha in fp32, convert to bf16), shared memory, and global memory with a TMA store, and it repeats for each of the 8 subtiles of a tile. Figure 6 puts the whole kernel together, one memory per column. ![The data flow of FlashInfer's NVFP4 GEMM through the memory hierarchy](https://sleepy-capybara.vercel.app/sm100-memory-hierarchy/figures/fig06_gemm_flow.png) *Figure 6: Where FlashInfer's NVFP4 GEMM keeps its data and which instruction moves it: TMA from global into shared memory, tcgen05.cp into TMEM for the scale factors, tcgen05.mma from shared memory and TMEM into the TMEM accumulator, and in the epilogue tcgen05.ld into registers, the multiplication by alpha, a store into shared memory and a TMA store to global memory.* Since the epilogue only ever holds a 32-column subtile, the registers stay small: 32 fp32 values per thread at a time. Together with the main loop, which holds no matrix data at all, that's how the kernel fits into 51 registers per thread, with no local memory. ### 4.7 Other Configurations The kernel supports other tile shapes, and two of them show nicely how the memory budget decides the speed. Here are all four runs, with the same inputs and a cold L2: - **256 x 256 tile, 2 CTAs, evict_last hint:** 157.7 microseconds, 6.97 PFLOP/s. Five 38 KB stages, overlapping accumulators. - **The same without the L2 hint:** 159.7 microseconds, 6.88 PFLOP/s. - **256 x 128 tile, 2 CTAs:** 192.7 microseconds, 5.71 PFLOP/s. The stages are smaller (28 KB), so 7 of them fit, and two separate 128-column accumulators fit into TMEM without overlapping, but each tile does half as much math per byte loaded. - **128 x 256 tile, 1 CTA:** 200.7 microseconds, 5.48 PFLOP/s. Each CTA holds all of B, so a stage is 54 KB and only 3 stages fit. All four produced the same output, bit for bit. ## 5. Summary So, if someone asks you to explain the memory hierarchy of an SM100 GPU, here is the long answer, from far to near, with the numbers from the B300: 1. **Global memory (HBM3e).** 288 GB shared by everything, 7.67 TB/s peak and 6.66 TB/s for a plain copy, about 850 cycles per load on average. Split between two dies, so a load from the near die takes about 600 cycles and one from the far die about 1,000. 2. **L2 cache.** 126.5 MiB shared by all SMs, about 307 cycles per load, at least 10 TB/s. It's where CTAs that need the same tiles meet. 3. **Constant memory.** 64 KB, read-only and broadcast to a warp. Kernel parameters, including TMA descriptors, live here. 4. **L1 cache.** Part of each SM's 256 KB of on-chip storage, 38 cycles per load, managed by the hardware. 5. **Local memory.** Private to a thread, but stored in global memory. It's where spills and dynamically indexed arrays go, and it shows up as `STACK` and `LDL`/`STL` in the binary. 6. **Shared memory.** Up to 227 KB per CTA from the same 256 KB, 29 cycles per load, organized in 32 banks. On SM100, it's where the TMA and the tensor cores exchange tiles and where their mbarriers live. 7. **Distributed shared memory.** Another CTA's shared memory in the same cluster, 219 cycles per load, used by multicast and by the 2-CTA MMA. 8. **Tensor memory.** 128 lanes x 512 columns of 32 bits per CTA, new in SM100. It holds the accumulator and the scale factors of the tensor cores, is allocated by a warp, and each warp can only access its own 32 lanes. 9. **Registers.** 64K 32-bit registers per SM, at most 255 per thread. In an SM100 GEMM, they mainly hold the output on its way from TMEM to memory. And in FlashInfer's NVFP4 GEMM, the data travels like this: A, B and the block scale factors go from HBM through L2 into shared memory with the TMA (with an `evict_last` hint and, for SFB, a multicast to both CTAs of a pair); the scale factors continue into TMEM with `tcgen05.cp`; the tensor cores read A and B from shared memory and the scale factors from TMEM, and accumulate in TMEM, in two accumulators that overlap so the next tile can start early; and in the epilogue, each 32-column piece of the result comes out of TMEM into registers, is multiplied by the global scale alpha = 1 / (gA x gB) in fp32, converted to bf16, written to shared memory, and stored to global memory with a TMA store. ## Sources - [FlashInfer's SM100 NVFP4 GEMM (dense_blockscaled_gemm_sm100.py)](https://github.com/flashinfer-ai/flashinfer/blob/c0e62fa7b18cbf051f729214984c235eb521a551/flashinfer/gemm/kernels/dense_blockscaled_gemm_sm100.py) - [FlashInfer's shared block-scaled GEMM helpers (dense_blockscaled_gemm_sm100_common.py)](https://github.com/flashinfer-ai/flashinfer/blob/c0e62fa7b18cbf051f729214984c235eb521a551/flashinfer/gemm/kernels/dense_blockscaled_gemm_sm100_common.py) - [PTX ISA: Tensor Memory and the tcgen05 instructions](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#tensor-memory) - [NVIDIA Blackwell Tuning Guide](https://docs.nvidia.com/cuda/blackwell-tuning-guide/index.html) - [CUDA Programming Guide](https://docs.nvidia.com/cuda/cuda-programming-guide/index.html) - [Inside NVIDIA Blackwell Ultra (NVIDIA Technical Blog)](https://developer.nvidia.com/blog/inside-nvidia-blackwell-ultra-the-chip-powering-the-ai-factory-era/) - [Using TMA Load, Prefetch, and Multicast Load in CuTe DSL](https://sleepy-capybara.vercel.app/tma-cute-dsl/)