Skip to main content

LLM inference kernels

FP16 Without Tensor Cores? Using WMMA in CUDA

Published 7 min 35 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

Half-precision inputs alone do not make a custom CUDA kernel use Tensor Cores. This walkthrough corrects the launch grid, verifies the scalar baseline, and replaces scalar dot products with warp-wide WMMA load, mma_sync and store operations. On the matching N=256 launch, kernel duration goes from 61.70 to 5.22 microseconds, and Tensor activity from 0.0% to 6.7%. The full unprofiled test run does not get faster: 634 ms before, 642 ms after.

Read along

Transcript

Select a time to play the video from that chapter.

0:00Learn Tensor Cores in CUDA C++

Today, we'll optimize matrix multiplication in CUDA. NVIDIA GPUs have specialized hardware for this work, called Tensor Cores. Our starting kernel isn't using them. Let's open GPUFlight, see why, and change the code. Then we'll check correctness, kernel time, and Tensor Core activity.

0:24What the problem asks

Here, C equals A times B. Each output value is a dot product of one row from A and one column from B. The inputs use half precision, also called FP16. That means each input value is stored as a 16-bit floating-point number. The output uses 32-bit float. We test square matrices up to 256 × 256. Every size is a multiple of sixteen, and the harness provides aligned memory. Those constraints let us focus on Tensor Cores without adding partial edge tiles to this lesson.

1:04First, repair and verify the baseline

Before comparing performance, fix the launch grid. The original grid has only one row, so larger tests do not cover the full output. Set both grid dimensions to the number of 16 × 16 tiles. Keep 32 threads per block. For the largest matrix, that gives 16 × 16 blocks. Run this corrected scalar version first. It passes the output checks. The lab still says not solved because Tensor Core activity is zero. Correctness and the performance goal are separate checks.

1:40FP16 storage does not select Tensor Cores

Look at the inner loop. Each lane loads two half-precision values, converts them to float, multiplies them, and adds to a scalar sum. That is ordinary floating-point arithmetic. The storage type alone does not request a Tensor Core operation. Tensor Cores are specialized hardware for matrix multiply-and-accumulate. We will use the CUDA WMMA interface to express that work. A warp is a group of 32 threads. In this version, one whole warp cooperates on a 16 × 16 output tile.

2:19Inspect the baseline kernel

Open the baseline session, choose Kernels, and expand the individual launches. There are five launches for five matrix sizes. We want the largest case, with a 16 × 16 grid and 32 threads per block. Its duration is 61.70 µs. A microsecond is one millionth of a second. Expand that launch and find Tensor active under hardware counters. It reads zero percent. This is the kernel-level evidence behind the lab result.

2:55Edit 1: use warp-wide fragments

Replace the per-lane scalar loop with three matrix fragments: one for A, one for B, and one accumulator. A fragment holds each thread's share of a matrix tile in registers. Together, the 32 threads hold the tile. This drawing is conceptual: WMMA does not specify the exact element-to-thread mapping. The input fragments use half precision and row-major layout. In this drawing, read across the first row, then continue with the second. Values from the same row sit next to each other in memory. The accumulator uses float, and we initialize it to zero. All 32 threads execute these operations together.

3:42Edit 2: load tiles and accumulate

Advance through the shared matrix dimension sixteen elements at a time. Load the A tile from this output tile's row, and the B tile from this output tile's column. The final N argument is the row stride in elements. Then call mma_sync. It multiplies the two tiles and adds their result into the accumulator. Each step contributes to all 256 output values in the tile. Continue until the full dot products are complete. One WMMA call can become multiple hardware instructions. It is a cooperative matrix operation, not one scalar multiplication.

4:26Edit 3: store the tile and keep the full grid

Store the accumulated tile back to C in row-major order. Keep the full two-dimensional grid and one warp per block. Both measured versions now cover the same output. The lab supplies the CUDA headers and surrounding test runner. In a standalone project, you would also include the WMMA header, mma.h. All lanes must participate in the fragment loads, multiply, and store. Putting these calls inside a lane-zero-only branch would be incorrect.

5:02Verify the optimized result

Run the WMMA version on the same RTX 3090. The output checks pass, and Tensor Core utilization reaches 6.7%, above this lab's three percent target. This computes the same matrix operation within the test tolerance. Different accumulation orders can produce small floating-point differences. Now open the candidate session and inspect the same largest launch.

5:32Compare the matching kernel

The grid is still 16 × 16, with 32 threads per block. The duration is now 5.22 µs. The hardware counters show 6.7% Tensor activity, compared with zero in the baseline. Back in the lab, open Compare. Kernel time falls from 61.70 to 5.22 µs: about 91.5% less kernel time. That is about 11.8× faster for this kernel launch in this measured pair. It is not a claim about every matrix size or every application.

6:15A faster kernel is not the whole application

Look at the unprofiled runtime row too. The complete test runner takes about 634 ms before and 642 ms after. That broader time includes work outside this small kernel. This pair does not demonstrate an end-to-end improvement. Also, 6.7% Tensor activity does not mean the hardware is saturated. This is a small teaching kernel with one warp per block. Further tuning could improve data reuse and how much work is in flight. Production matrix multiplication needs more than replacing one arithmetic instruction. These are single-run observations on the same device. Repeat measurements before relying on a precise speedup.

7:05Choose the operation. Verify the hardware.

Half-precision data did not activate Tensor Cores. Expressing the matrix operation through WMMA did. We checked correct output, matched the launch sizes, and inspected both kernel time and hardware counters. Try this Tensor Core lab in GPUFlight Performance Lab. Change the code and see what the hardware actually does. Thanks for watching!

Measure it yourself

Run the same experiment on a real GPU

Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.

Run the Tensor Core lab