Skip to main content

GPU concepts explained

Why Your GPU and CPU Disagree: FMA Rounding

Published 3 min 21 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

The same line of code prints 0 on the CPU and 1.49011612e-08 on the GPU, and neither is broken. The stored value of 0.1 is slightly more than 0.1. In two steps that extra is rounded away before the subtraction, while a fused multiply-add keeps it. The video reads the compiler's PTX listing to see which form each build used. Forcing two steps on the GPU also prints 0, and kernel time does not change.

Read along

Transcript

Select a time to play the video from that chapter.

0:00The mystery

Here is a mystery. One line of code: a times x, plus y. Same inputs on both machines. The CPU says zero. The GPU says a tiny number that is not zero. So which one is wrong?

0:16Stored numbers are rounded

First, a surprise. The numbers in the machine are not the numbers you typed. A float is a card with room for only so many digits. And it writes them in binary. In binary, 0.1 never ends, just like one third never ends in decimal. So the card holds the closest value it can: 0.1 and a hair more.

0:39What 0.1 times 10 really is

So what is 0.1 times 10, really? Not one. It's one, plus a tiny extra. And that extra is too small to fit on a card next to the one.

0:50Two steps: round twice

Now watch the first way to compute it. One student multiplies, and must write the product on a card. The tiny extra doesn't fit, so the card says one. A second student takes that card and subtracts one. The answer is zero. Two steps, two cards, two roundings.

1:09One step: round once

Here is the second way. One student does the multiply and the subtract together on scratch paper, keeping every digit. Only the final answer goes on a card. This time the tiny extra survives. One step, one card, one rounding.

1:27What did the compiler write?

Which way did each machine go? We can look. When CUDA code is compiled, the compiler writes a list of instructions for the GPU. That list is called PTX.

1:39Read the listing

In the lab's starter, the two steps are forced. The listing shows a multiply, then an add. Now write the line the natural way and compile again. The listing shows one instruction: a fused multiply-add. The compiler merged the two steps by itself. Here's the rule: each instruction that ends in .rn rounds once. Count them.

2:03The numbers agree

And the numbers agree. On an RTX 3090, the two-step kernel printed zero. The natural kernel printed 1.49 times ten to the minus eight. The CPU, running the same line, printed zero.

2:19Who is right?

So who is right? Both. Zero is right for the numbers you typed. The tiny number is right for the numbers that were actually stored. Each machine followed the rules for what it was asked to do.

2:33What to do about it

What should you do about it? Compare results with a small tolerance, never with equals. If you truly need a bit-for-bit match, force two steps. And watch for it when you subtract two numbers that are nearly equal. That's where one rounding shows.

2:50And the speed?

And the speed? In this lab, nothing changed: 239 microseconds either way. This kernel spends its time waiting on memory, not on arithmetic.

3:01Remember this

So remember this. Same code, same inputs, different rounding. When a GPU and a CPU disagree in the last digits, look at how many times each one rounded. Try this lab in GPUFlight Performance Lab. Class dismissed.

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 FMA contraction lab