Dithering and Stochastic Rounding

Round a quiet sine to three bits and the error becomes a comb of ugly tones. Add one step of random noise first and the tones melt into hiss, and the average is exact.

Pick a mode, then drag the bits. Watch the red spikes in the error spectrum under the wave. Then we build, piece by piece, why noise before rounding works, and how GPUs use it today.

You need what a probability is (a number from 0 to 1) and that rounding sends a number to its nearest level. We build the rest from zero.

Based on the Dithering and Stochastic Rounding lecture of Stanford EE269: Signal Processing and Quantization for Machine Learning, taught by Mert Pilanci. Lecture slides and all slides. Every idea is re-taught here in our own words and devices; it follows Lecture 4, Lloyd-Max.

Round a sine wave

3

A coherent sine: 5 whole cycles in 256 samples, peak 0.8 on a range of −1 to 1, so one step is Δ = 2 / 2bits. The error is y − x for every sample, and its spectrum is an exact 256-point transform computed in your browser. Heights are decibels above the average of bins 1 to 127. The noise is a seeded random draw; New noise reseeds it.

Chapter 0

Why Noise Before Rounding Helps

See why a smooth signal turns into stripes when you round it, and why a pinch of random noise makes the stripes disappear

You save a photo of an evening sky. The sky fades from deep blue at the top to pale gold at the horizon, with no edges anywhere. Then you shrink the file by keeping only a few shades of each colour. On the screen, the sky now has stripes: flat bands with hard edges, like a staircase seen from above.

Those stripes are called banding. Nobody drew them. The sky never had them. They come from one small act, repeated on every pixel: rounding.

What a quantizer does

A computer cannot keep a number with endless digits. It keeps one of a fixed list of values, such as 0, 16, 32, 48 and so on. A quantizer is the rule that picks which value. The simplest rule is to pick the nearest one. This is what you do when you round 2.7 to 3.

The gap between neighbouring values on the list is the step, written Δ (the Greek capital delta). With 4 bits you can name 24 = 16 values. Spread them over a range of 0 to 255 and the step is Δ = 16. With 1 bit you get two values and a giant step. Fewer bits mean a bigger step.

The quantization error is the output minus the input. If you always round to the nearest value, the error can never be larger than half a step, Δ/2. For the sky, that is a small error. So why do the stripes look so bad?

The error is small, but it is not random

Think about two neighbouring pixels in the sky. Their true brightness differs by a tiny amount, say 100.0 and 100.3. Both round to the same value, 96. Both have almost the same error, 4. The next pixel, 100.6, also gets error 4. A long run of pixels gets the same error. Then the input crosses the halfway point to the next value, and the output jumps by a whole step.

Plot the error against the input and you get a repeating ramp that drops back to start at each halfway point. It looks like the teeth of a saw, so people call it a sawtooth. The error is a fixed function of the input. It has no randomness at all.

That matters because your eye is very good at spotting edges and patterns. It is poor at spotting a faint, shapeless grain. A flat band with a hard edge is a pattern. Fine grain is not. The same error energy can look awful or look fine, depending on its shape.

The trick: add noise first, then round

Here is the idea of this lecture, in one sentence. Before you round, add a small random number to each input. This is dithering, and the random number is the dither.

It sounds like a bad idea, because you spoil the signal on purpose. But think about what the noise does at the halfway point. A pixel at 100.6 used to round the same way every time. Now a random push of up to half a step sometimes lifts it over the line and sometimes does not. The hard edge turns into a soft mix: more pixels at the new value as you go along the ramp, fewer at the old one.

Each pixel is still wrong by up to a step. But the wrongness has no pattern. Zoom out, and your eye averages the mix back to the right shade. The lecture states the trade directly: dithering swaps structured distortion for noise-like distortion, and a weak grain is often preferred to hard bands.

Try it. The strip below is a smooth brightness ramp from black to white. Pick how many levels the quantizer keeps. Then switch the noise on and off.

Stripes or grain

Top: the ramp after rounding, one grey square per sample. Bottom: the true ramp in blue, and in warm the average of each small window of the output. Lower the levels to 3 or 4, then toggle the noise.

4

Illustrative: a made-up ramp from 0 to 1 over 120 squares, 14 rows high. The levels are evenly spaced from 0 to 1, so the step is Δ = 1 / (levels − 1). The noise is uniform from −Δ/2 to Δ/2, drawn once with a fixed seed. Windows are 12 squares wide.

With plain rounding, the strip turns into a few flat bands, and the window averages climb in steps. With noise, the bands break up and the warm line hugs the blue one. Your eye reads the grainy strip as a smooth ramp.

Notice what the noise did not do. It did not make any single square more accurate. It changed how the errors are spread, from long runs of the same error to errors that cancel when you average.

What the lecture covers

The lecture asks three questions. Why does rounding at a finite number of bits make patterns when the textbook says it makes noise? What exactly does adding noise buy us? And where do computers use this idea today, from images and music to the low-bit number formats on GPUs? We answer them in that order. Along the way we prove one clean fact: with the right amount of noise, the average output equals the input exactly.

  1. When the noise model fails: banding, spurious tones and limit cycles.
  2. Add noise, then round: the two dither designs and the error formula.
  3. Linearization: the proof that the average output equals the input.
  4. The pixel at 100: a worked number, and why dither is a weighted coin.
  5. Stochastic rounding on GPUs: the NVFP4 format.
  6. How much dither: too little, too much, and just one step.
  7. Pictures: images at one bit.
  8. Sound: a sine wave and a fading note.
  9. Subtractive dither: exact, uniform error, if the receiver knows the noise.
  10. The whole lecture on one page: numbers, code and what comes next.
A smooth ramp is rounded to 4 levels. The error never exceeds half a step, yet the result shows flat bands. What is the cause?

Chapter 1

When the Noise Model Fails

Find out why rounding with few bits makes bands, tones and stuck outputs, when the textbook model promises plain noise

You switch off the music. The input to your digital reverb is now exactly zero. Yet a faint, steady whine comes out of the speaker, and it stays until you pull the plug. Nothing is playing. The whine is made by the arithmetic inside the filter.

This chapter explains where such artifacts come from. It starts from a promise that earlier lectures made, and shows exactly where that promise stops holding.

The promise: rounding is just noise

In the lecture on quantization noise, we modelled the quantizer as an adder. The output equals the input minus an error, and the error is a random number that is uniform between −Δ/2 and Δ/2. The error does not depend on the input. The lecture writes it like this.

X ↦ Q(X)
The input X goes into the quantizer Q. The arrow with a tail reads “is sent to”.
X
The true input, a random variable: a value we do not know in advance, with some spread.
ε
The error, written as a subtraction so that Q(X) = X − ε. Notice the sign: here the lecture defines it as input minus output.
U(−Δ/2, Δ/2)
A uniform distribution: every error between minus half a step and plus half a step is equally likely. Its power is Δ²/12.
ε ⊥ X
The error is independent of the input. Knowing X tells you nothing about ε.

This is the additive noise model. It is convenient. It gives the famous rule that every extra bit buys 6 dB of signal-to-noise ratio, and it lets engineers treat a quantizer as a noise source in their system sketches.

Why the promise only holds in the limit

Bennett’s theorem justifies the model, but only as the step Δ goes to zero. Three conditions hide inside that phrase. There must be many levels. The input must have a smooth probability density, a curve with no spikes. And there must be no overload, meaning the input never leaves the range the quantizer covers.

A theorem about a limit says nothing about a real converter with 3 bits. The lecture is blunt about this: at finite bit depth, the error ε(x) is deterministic and correlated. Deterministic means that the same input always gives the same error. Correlated means that the error moves in step with the input and with its own past values.

Check this with a number. Take Δ = 1 and the input 2.3. It rounds to 2, so the error is 0.3 every single time. Take 2.31 and you get 0.31. The error follows the input like a shadow.

Three symptoms

The lecture lists three symptoms of a quantizer that is deterministic and correlated.

Contouring. A smooth ramp becomes a staircase. In an image, this is the banding you saw in Chapter 0.

Spurious tones. Quantize a pure sine wave and you do not get white noise. A sine repeats every period, so its error repeats every period too. Anything that repeats is a sum of tones at the pitch of the signal and at whole-number multiples of it, which are the harmonics. Go back to the instrument at the top of the page: with plain rounding at 3 bits, the error spectrum has red spikes at odd harmonics, and nothing between them.

Limit cycles. This one happens in feedback systems, where the output goes back in as input. A limit cycle is a loop that repeats forever without any input to drive it. The next device builds one.

A filter that never goes quiet

Take the simplest feedback filter. Each new output is −0.9 times the previous one, rounded to a whole number: yn = Q(−0.9 · yn−1), with Δ = 1. With exact arithmetic, the output shrinks by 10 percent each step and swaps sign. Start at 5 and it goes −4.5, 4.05, −3.65, and slides towards zero.

Now round at each step. Start at 5: −0.9 × 5 = −4.5 rounds to −4. Then −0.9 × −4 = 3.6 rounds to 4. Then −3.6 rounds to −4. The output is stuck, swinging between 4 and −4 forever. The same filter in exact arithmetic would be silent.

Why 4? Rounding undoes the shrinking whenever 10 percent of the output is less than half a step. That holds for every |y| below 0.5 / (1 − 0.9) = 5. This region is the dead band. Any output that lands inside it can never leave. Move the start value and see where it locks.

Ring a filter with rounding

Blue stems: the exact filter, which decays to zero. Warm stems: the rounded filter. The red band is the dead band, where rounding stops any further shrinking. Move the start value, then switch to dither.

12

Illustrative toy: the filter yn = Q(−0.9 yn−1) with Δ = 1, 40 steps. Ties round up, so −4.5 gives −4 and 4.5 gives 5. The dither is a fresh uniform draw from −0.5 to 0.5 at every step, added before rounding. The dead band half-width 0.5 / (1 − 0.9) = 5 is exact for this filter.

With plain rounding, the output locks into a steady swing and never leaves. This is the whine from the start of the chapter, made of one number. With dither, the output is random on its way down, and in most runs it settles at zero, which is silence. It never locks into a tone. And as Chapter 3 proves, its average still decays at the exact filter’s rate, 0.9 per step.

That is the reason for the lecture. Rounding has bad habits at finite bit depth: bands, tones, stuck outputs. All of them need the error to repeat. Break the repetition and they go.

Bennett’s theorem says rounding error acts like independent uniform noise. When can you trust it?

Chapter 2

Add Noise, Then Round

Build the two dither designs from the rounding rule, and write down exactly what each one does to the error

Picture a marble and an egg carton. Each cup is one allowed level. You place the marble somewhere on the carton and let go. It rolls into the nearest cup. If you place it right on a ridge between two cups, a tiny nudge decides which cup wins.

Now shake the carton by about one cup width just before you let go. A marble near the bottom of a cup stays in that cup. A marble near a ridge falls either way. Over many marbles, the share that lands in the right-hand cup grows smoothly as you place them nearer to it. This shaking is dither.

The rounding rule, written down

The lecture uses a uniform rounding quantizer with step Δ. Its levels are all the whole multiples of Δ: …, −Δ, 0, Δ, 2Δ, …. It sends any input z to the nearest one.

QΔ(z)
The output level for input z. The subscript reminds us that the step is Δ.
round(z / Δ)
Count how many steps fit in z, then round that count to a whole number. Multiply by Δ to get back to the original units.
e(z)
The rounding error: output minus input. It always lies in a window one step wide, from −Δ/2 to Δ/2.

Take care with the sign. In Chapter 1, the lecture wrote ε as input minus output. From here on, it defines e(z) as output minus input. We follow the new convention. A positive error now means the quantizer rounded up.

Here is a worked case. Let Δ = 1 and z = 2.3. Then z / Δ = 2.3, which rounds to 2, so Q(z) = 2 and e = 2 − 2.3 = −0.3. Take z = 2.7: it rounds to 3, and e = +0.3. Slide z along the line and e is a sawtooth with period Δ. Without any randomness, the error at input x is a fixed periodic function of x. That is the structure we want to break.

Walk the sawtooth with Δ = 1. Input 0.2 rounds to 0, error −0.2. Input 0.4 rounds to 0, error −0.4. Input 0.5 is a tie and rounds up to 1, error +0.5. Input 0.6 gives +0.4, and input 0.8 gives +0.2. The error falls steadily, jumps at the halfway point, and falls again. It repeats with period 1.

Two ways to add the noise

The lecture shows two designs, drawn as block diagrams. In both, d is a random number we draw for each sample, independent of the signal x. We call it the dither.

Design (A), non-subtractive dither, is the one we study first: add d, round, and keep the result. The dither stays in the output. Design (B), subtractive dither, adds d, rounds, and then takes d away again. It only works if the receiver also knows d. Chapter 9 returns to it.

(A)
Add the dither, round, output. The output only ever sits on the grid of levels.
(B)
The same, then subtract the same dither. The output sits slightly off the grid, by exactly −d.

The error of design (A)

What is the output error y − x? Start from the rounding definition. Because e(z) = Q(z) − z, we know Q(z) = z + e(z). Put z = x + d and you get y = x + d + e(x + d). Move x to the left.

ε
The total reconstruction error: what comes out minus what went in.
d
The dither itself. It lands directly in the output, because we never remove it.
e(x + d)
The rounding error of the dithered input. It no longer depends on x alone, because d moves the input around the grid.

The lecture states the trade in two lines. The benefit is that the noise breaks the link between the signal and the staircase. The cost is that d appears directly in the output error. Design (B) removes that cost, because subtracting d leaves just e(x + d).

Check it with numbers. Let Δ = 1, x = 1.8 and d = −0.3. Then z = 1.5. It rounds to 2, so y = 2 and ε = 0.2. The formula agrees: d + e(z) = −0.3 + 0.5 = 0.2. Draw d = −0.4 instead: z = 1.4 rounds to 1, so ε = −0.8, and the formula gives −0.4 + (−0.4). In design (B), the same draw gives y = 1 − (−0.4) = 1.4 and ε = −0.4, just e(z).

Try it. Choose a design, place x, and draw dithers. Watch which part of the error comes from d and which from rounding.

Draw a dither, round the sum

Blue is the input x. Purple is the dither d, which pushes x to z. Warm is the output. The bars below count the errors you have drawn so far. Press Draw 100 to see the shape of the error.

1.80

Illustrative: Δ = 1, levels at 0 to 4, ties round up. The dither is uniform from −0.5 to 0.5, a fresh draw each time, from your browser’s random numbers. The error histogram has 20 bins of width 0.1 between −1 and 1 and restarts whenever you change x or the design.

With no dither, every draw gives the same error, one tall bar. With (A), the errors spread out and the average sits near zero, but the spread is wide. With (B), the errors fall in a flat block between −0.5 and 0.5. We show in Chapters 3 and 9 why those shapes are no accident.

With Δ = 1, x = 1.8 and the dither draw d = −0.4, design (A) outputs y = Q(1.4) = 1. What is the error y − x, and where does it come from?

Chapter 3

Linearization: The Average Is Exact

Prove that one step of uniform dither makes the average output equal the input, for every input at once

A shop owes you $2.30 in change, but the till holds only whole dollars. The cashier does not round. She flips a weighted coin: with probability 0.3 she hands you $3, otherwise $2. One customer gets $0.70 too little or $0.70 too much. But across a thousand customers she hands out $2,300 in total, to the dollar. Her rule is fair on average, and unfair to nobody in the long run.

Dithering does the same thing without a coin. The dither makes the quantizer act like that cashier. This chapter proves it.

The claim

Fix any input x and draw the dither d uniformly from one step: −Δ/2 to Δ/2. Then the output Q(x + d) can only be one of the two levels next to x, and its average is exactly x. We write the average as an expectation, E[·]: the mean over all the draws of d. The lecture calls the result linearization, because the average input-to-output curve of the quantizer is no longer a staircase. It is the straight line y = x.

The proof, one step at a time

Step 1: place x on the grid. Pick the level kΔ just below x. Write x = kΔ + r, where r is how far x sits above that level. So r is between 0 and Δ. (The slide writes r from −Δ/2 to Δ/2, which would put x on either side of kΔ. Slide 15 restates the proof with x between two neighbouring levels. We use that form.)

Step 2: see where x + d can land. Because d is uniform over a width of Δ, the sum x + d is uniform over an interval of width Δ, centred on x.

x + d ~ U( kΔ + r − Δ/2 , kΔ + r + Δ/2 )

Step 3: find the possible outputs. The rounding boundaries are halfway between levels, at kΔ − Δ/2, kΔ + Δ/2 and kΔ + 3Δ/2. The interval above starts no lower than kΔ − Δ/2 and ends below kΔ + 3Δ/2. It spans only one boundary, the one at kΔ + Δ/2. So Q(x + d) is either kΔ or (k + 1)Δ, and nothing else.

Step 4: find the chance of rounding up. The output is (k + 1)Δ exactly when x + d lands above the boundary kΔ + Δ/2. The interval is flat, so the chance equals the part of its width that lies above the boundary, divided by Δ. That part runs from kΔ + Δ/2 to kΔ + r + Δ/2, a width of r.

p
The probability that the output is the upper level. The other level gets 1 − p.
r / Δ
How far x sits between its two neighbours, as a fraction from 0 to 1. At the lower level it is 0, halfway it is 0.5, at the upper level it is almost 1.

Step 5: take the average. The output is a coin with two sides. Multiply each side by its chance and add.

E[ QΔ(x + d) | x ] = p (k+1)Δ + (1 − p) kΔ = kΔ + Δp = kΔ + r = x

That is the whole proof. The bar in E[· | x] reads “given x”: x is held fixed, and only the dither varies. The average comes out as x, whatever x was. The cashier’s coin sits inside the quantizer.

What the proof needed

Three things carried it. The dither must be uniform, so that chances are proportional to widths. It must be exactly one step wide, so that the interval touches just one boundary. And it must be independent of x, so that its draws do not know where x is. Slide 15 adds a fourth, a quiet one, no overload: x ± Δ/2 must lie inside the quantizer’s range, or the outer levels would clip the answer.

Now see it happen. The device fixes x between two levels and draws thousands of dithers. The purple band is the interval x + d can cover. The part of it past the dashed boundary is p.

Count the coin

Slide x between the two levels. Press Draw 1000 a few times. The bars count the outputs. Bottom right: the average output against x, with and without dither.

0.30

Illustrative: Δ = 1, lower level 0, upper level 1. The counts come from your browser’s random numbers, so they wobble around the theory by about 1.5 percentage points per 1,000 draws. The dashed curve is the exact average without dither. The straight line is the exact average with dither, which is what Steps 1 to 5 prove.

The bars settle at the heights the proof predicts: the upper level gets share r / Δ, the lower gets the rest. The average output lands on x, up to the small wobble of random counting. Without dither, the average curve is a staircase. With dither, it is a straight line.

Notice one more fact in the bars. Unless x sits exactly on a level, every single output is wrong. The output is exact only on average. How wrong, on average, depends on x: the spread is biggest when p = 0.5. We measure this in Chapter 9.

With Δ = 1 and a uniform dither one step wide, what does Q(3.2 + d) return, and how often?

Chapter 4

The Pixel at 100, and the Weighted Coin

Run the proof on real numbers, then see that dithering and flipping a weighted coin are the same act

A camera records a grey pixel with the value 100, on a scale from 0 (black) to 255 (white). You must show it on a cheap display that has only 4 bits per pixel. Which grey does the display get?

Plain rounding gives one answer, and it gives it to every pixel of that shade. Dithering gives a mix. This chapter works the numbers by hand, as the lecture does, then shows what the mix really is.

The lecture’s example, step by step

Four bits name 24 = 16 levels. Spread over 0 to 255, the step is Δ = 256 / 16 = 16. The levels sit at 0, 16, 32, …, 96, 112, 128 and on up. The pixel x = 100 lies between 96 and 112.

Write x as a level plus a remainder: 100 = 96 + 4. So kΔ = 96 and r = 4. The pixel sits 4 above the lower level, a quarter of the way to the next.

Draw the dither from one step, d ~ U(−8, 8). Then x + d is uniform on 92 to 108. The rounding boundary between 96 and 112 is halfway, at 104. Only the part of the interval from 104 to 108 rounds up.

P( output = 112 ) = r / Δ = 4 / 16 = 1/4,    P( output = 96 ) = 1 − 1/4 = 3/4

The average output is 112 × 1/4 + 96 × 3/4 = 28 + 72 = 100. It is exactly the pixel we started with. Plain rounding would give 96 every time, 4 too dark, on every pixel of this shade.

Now count the cost per pixel. A dithered pixel is 112 (too bright by 12) a quarter of the time, and 96 (too dark by 4) three quarters of the time. The mean squared error is 122 × 1/4 + 42 × 3/4 = 36 + 12 = 48. That is 3 times the 16 of plain rounding. The price of an unbiased average is a larger scatter.

Dithering is stochastic rounding

Look at what the dithered pixel really does. It picks one of the two neighbouring levels. It picks the far one with a chance equal to how far x has travelled along the gap. This is stochastic rounding: round up or down at random, so that the average equals the original number. Slide 15 states it in general. For x between t and t + Δ, let p = (x − t) / Δ, the share of the gap that x has covered.

QΔ(x + d)
The dithered output for the fixed input x.
t
The level just below x. It wins when the dither pushes x less far.
1 − p
The chance of the lower level: the share of the gap still ahead of x.
t + Δ
The level just above x.
p
The chance of the upper level. It equals the distance from x to t, over the distance from t to t + Δ.

The mean is (1 − p) t + p (t + Δ) = t + pΔ = x, the same line as in Chapter 3. So you can drop the noise and keep the coin. Compute p, draw a number u uniformly between 0 and 1, and round up when u is below p. Same chances, so the same average and the same spread. The two methods are two codings of one idea.

The coin version has one big advantage: it never needs the grid to be even. It needs only the two neighbours of x, and their distances. We use that in Chapter 5.

Here is the one-line trick programmers use. Add a uniform number from 0 up to 1, then round down: floor(x / Δ + u) · Δ. It is the same as adding a dither from −Δ/2 to Δ/2 and rounding to nearest, because shifting by one half turns round into floor. The code lives in the Field Guide.

Round a patch of pixels

Every square starts as the same grey. Choose the grey and the bits. Plain rounding paints one shade. Dither and the weighted coin each paint a mix. Compare their averages with the true value.

100
4

Illustrative: 96 squares per patch (12 by 8), each square an independent draw. The step is Δ = 256 / 2bits, levels at whole multiples of Δ. The grey on screen is the value divided by 255, clamped. At 4 bits and x = 100 this is the lecture’s example: levels 96 and 112, p = 1/4.

At the lecture’s setting, the plain patch is flat at 96. The other two have about a quarter of their squares at 112. Their averages sit near 100 and wobble by a point or two from draw to draw. Press Redraw the noise and watch the wobble. Raise the bits and the mix thins out, because the gap shrinks. At 8 bits the step is 1 and the mix is almost gone.

A 4-bit quantizer has Δ = 16. A pixel has value 108, between levels 96 and 112. With one-step dither, what are the output chances?

Chapter 5

Stochastic Rounding on GPUs: NVFP4

See why a 4-bit number format on a modern GPU rounds with a weighted coin, and what goes wrong when it does not

You train a large model. Every step nudges each weight by a tiny amount, say 0.01. The weight is stored in a format with only 15 possible values. The gap next to the weight is 0.5. A nudge of 0.01 is far smaller than half that gap. If you always round to the nearest value, the weight never moves. A hundred nudges, and not one of them counts.

This is why the lecture ends its tour of dithering on a GPU number format. Computer hardware has quietly adopted the weighted coin of Chapter 4.

What the slide says about NVFP4

The slide makes four statements about NVIDIA’s NVFP4 data format. First, the slide says NVIDIA announced it in March 2024 as a key feature of its Blackwell GPU architecture. (Blackwell was announced then; check NVIDIA’s pages for the exact date of the NVFP4 format itself.) Its representable values are not uniformly spaced. Each block of numbers is first scaled before quantization. And the format is popular in LLM inference and training. A data format is the agreed way of storing a number as bits.

The next point is the slide’s own definition. Stochastic rounding: instead of always rounding to the nearest value, round up or down at random so that the average equals the original number. After scaling, a real number x lies between two neighbouring NVFP4 values, vlow and vhigh. Round to vhigh with the chance below, otherwise to vlow.

P( round to vhigh )
The chance of going up. Otherwise the number goes down to vlow.
x − vlow
How far x has travelled from the lower neighbour.
vhigh − vlow
The width of the gap, whatever it is. The gap may differ from place to place on the grid.

It is the same rule as Chapter 4. The only change is that the gap is not Δ everywhere. That is why the lecture states the coin form. A uniform noise of one fixed width does not fit a grid with gaps of different sizes. The coin needs only the two neighbours.

The format itself, in numbers

The slides stop at the points above. These details come from NVIDIA’s public description of NVFP4, not from the lecture, so check NVIDIA’s specification before you rely on them. Each value is 4 bits: one sign bit, two exponent bits and one mantissa bit, a layout called E2M1. It can name only these sizes: 0, 0.5, 1, 1.5, 2, 3, 4 and 6, each with a plus or minus sign. That gives 15 distinct values, since zero has no sign.

Notice the spacing. It is 0.5 near zero, 1 between 2 and 4, and 2 between 4 and 6. Small numbers get a fine grid and large numbers a coarse one. That is the non-uniform spacing the slide mentions.

The values would be far too narrow on their own, since a weight might be 0.003 or 400. So the format scales them. A tensor is cut into blocks of 16 numbers. For each block, the hardware stores one scale s, chosen so that the largest number in the block, divided by s, fits within 6. Then it rounds x / s onto the 15-value grid. A weight matrix of shape (4096, 4096) becomes a stored array of 4-bit codes of shape (4096, 256, 16), with one 8-bit scale for every block. That costs 4 + 8/16 = 4.5 bits per number.

Take a scaled value x = 2.4. Its neighbours are 2 and 3, so p = (2.4 − 2) / (3 − 2) = 0.4. Stochastic rounding returns 3 with chance 0.4 and 2 with chance 0.6: average 1.2 + 1.2 = 2.4. Round-to-nearest always returns 2, a bias of −0.4. Another case, x = 0.7, between 0.5 and 1: p = 0.2 / 0.5 = 0.4. Try your own.

Flip the weighted coin on a 4-bit grid

The ticks are the eight non-negative values of the grid. Slide x between any two. Press Round 1000 times to see the shares and the mean. The hollow square shows what round-to-nearest returns.

2.40

Grid values (0, 0.5, 1, 1.5, 2, 3, 4, 6) are the E2M1 magnitudes from NVIDIA’s public description, not from the slides. The block scale is left out: x is already the scaled number. Counts come from your browser’s random numbers.

In the wide gaps, the chance moves more slowly as x moves, but the average still lands on x. Near 4.8 you get 4 or 6, and the chance of 6 is 0.4. The scatter is bigger there: a whole 2 apart.

Why stochastic rounding matters in training

Now the opening scene as a device. A weight starts at 1.0. Each step adds the same small update. Exact arithmetic gives a straight line. Round-to-nearest rounds every new value back to the grid. Stochastic rounding rounds with the coin. The device runs 200 independent copies of the stochastic version and shows their average.

Add a hundred tiny nudges

Blue is the exact sum. Warm is a weight stored with round-to-nearest. The thin purple paths are single runs with stochastic rounding. Green is the average of 200 runs. Make the nudge bigger and watch round-to-nearest escape.

0.010

Illustrative toy: one weight starts at 1.0, 100 steps, the same nudge each step, rounded onto the grid 0, 0.5, 1, 1.5, 2, 3, 4, 6 after every step. Block scales and real gradients are left out. The average is over 200 independent runs.

At the default nudge of 0.01, the sum should reach 2.0. Round-to-nearest stays at 1.0 the whole time, because 1.01 is nearer to 1 than to 1.5. The stochastic runs jump up now and then, and their average climbs along the exact line. Each single run is noisy and lands on the grid. The average is what the training process sees across many weights and many steps.

A scaled number x = 5.0 lies between the grid values 4 and 6. What does stochastic rounding return?

Chapter 6

How Much Dither Should We Add?

Turn the dither width up from zero and see exactly where the bias vanishes and what each extra bit of noise costs

Go back to the egg carton. Shake it too gently, and a marble in the middle of a cup never leaves. The shake only matters for marbles near a ridge. Shake it wildly, and every marble ends up in a random cup, nowhere near where you put it. There must be a sweet spot in between.

The lecture names it: one-LSB uniform dither. The LSB, the least significant bit, is the smallest change the number can show. In a rounding quantizer, that is one step, Δ. So the standard choice is a uniform dither exactly one step wide.

d ~ U( −Δ/2 , Δ/2 )

Two reasons justify it, and both are on the slide. It is wide enough to randomize the fractional part of x modulo Δ. That is, however far x sits between two levels, the dither spreads it over a full cell. And it is the minimal support that gives the exact result E[Q(x + d) | x] = x. A support is the range in which the dither can take values. Narrower fails. Wider works but wastes effort. The slide adds a warning: much smaller than Δ and the artifacts remain, much larger and you add noise you do not need.

Narrower than one step: a dead zone

Let the dither width be w steps, so d ranges over −wΔ/2 to wΔ/2. Take w less than 1, say w = 0.5. An input right in the middle of a cell, at a level, has the interval x ± 0.25Δ around it. That interval never reaches the rounding boundary 0.5Δ away. So the output is the level every time, whatever the dither does. The dither is switched off for that input.

In general, the dither matters only for inputs within wΔ/2 of a boundary. Inside the rest of the cell, a dead zone of width (1 − w)Δ, the output is the plain-rounded level and the error is still the sawtooth. So the quantizer’s average curve is a staircase whose risers have been tilted. The risers span a width wΔ. At w = 0 they are vertical, which is plain rounding. At w = 1 they span the entire cell and meet each other. The staircase has become a straight line.

The size of the worst miss follows from this picture. In the dead zone, the bias is minus the distance from the level, reaching (1 − w)Δ/2 at the edge. The sloped part brings it back to zero and swings the other way by the same amount. So the largest average miss is (1 − w)Δ/2. It shrinks in a straight line and reaches zero at w = 1.

Work one case with w = 0.5. Let x sit 0.2Δ above a level. The dither interval runs from −0.05Δ to 0.45Δ around that level, and the boundary is at 0.5Δ, so it never crosses. The output is the level every time, and the average misses x by 0.2Δ. Now let x sit 0.4Δ above the level. The interval runs from 0.15Δ to 0.65Δ, and 0.15Δ of it lies past the boundary. The chance of rounding up is 0.15 / 0.5 = 0.3, so the average is 0.3Δ, a miss of 0.1Δ. The symptom is a tilted but still visible staircase. The measurement is the miss. The fix is a wider dither.

Wider than one step: unbiased, but noisier

Take w = 2. The dither interval now covers two cells. The average is exact again, for the same reason as in Chapter 3. Every position of x within a cell is smeared over a whole number of cells. But the noise has grown. Its power is the rounding noise Δ²/12 plus the dither’s own power w²Δ²/12.

average squared error = (1 + w²) Δ² / 12

Check it: w = 0 gives Δ²/12, plain rounding. w = 1 gives 2Δ²/12 = Δ²/6, twice as much, or 3 dB more. w = 2 gives 5Δ²/12. The device below computes the exact numbers by integrating over the dither, and they match this line. At w = 1.5 a small bias remains, because the width is not a whole number of cells.

So w = 1 is the cheapest width with zero bias. It is the minimal support the slide talks about. Slide the width and see the dead zone close.

Turn the dither up

Top: how far the average output sits from x, for every x across two steps. Zero everywhere is the goal. Bottom: the noise power you pay, against the plain-rounding level (dashed). Try 0.5, then 1, then 2.

0.50

Exact numerical averages, not random draws: for each of 240 inputs across two steps, the device averages Q(x + d) − x over 400 evenly spaced values of d in the chosen width. The power is the mean of the squared error over all inputs, in units of Δ². Uniform dither, ties round up.

At w = 0.5 you see the sawtooth survive near each level: the curve is still a ramp there, reaching 0.25 Δ at its worst. At w = 1 it is a flat line on zero. At w = 2 it stays flat, and the power bar grows. The bar sits at about 0.167 for w = 1, twice the plain level. That is the price from Chapter 2, now seen as a function of width.

A quantizer uses uniform dither of width 0.5Δ. Compared with one-LSB dither, what is true?

Chapter 7

Dithering a Picture

Squeeze an image down to black and white and watch noise rescue every shade that a plain threshold throws away

Look at a newspaper photo with a magnifying glass. The page holds only black ink and white paper. There is no grey ink anywhere. Yet from arm’s length you see a full range of greys, from a dark coat to a pale sky. The printer made grey out of dots: more dots where the picture is dark, fewer where it is light.

That is dithering. The lecture works three kinds of example: one-dimensional signals, images and audio, and one more, the spectrum, which it defers to later. This chapter takes images. The next takes sound.

The lecture’s test image

The slides show a column of six numbers, 85%, 75%, 55%, 45%, 25% and 15%, printed in matching shades of grey, from dark to light. At 8 bits the six shades are all clear. Now squeeze to 1 bit, so that each pixel must be black or white. Quantize with a threshold at 50, on a scale from 0 to 100. Whatever is above 50 turns black. Everything else turns white.

The result is brutal. 85%, 75% and 55% all become the same solid black. 45%, 25% and 15% vanish into the white. Six shades have become two. Even worse, 55% and 45% differ by only 10 points but end up opposite, while 55% and 85% differ by 30 and look identical.

Then the slides add noise to the 8-bit image, and quantize again. Now the six values come back, as six different densities of black dots. It looks grainy up close. From a distance, it reads as the original shading.

Why the dots land at the right density

Here is the number behind that picture. On a scale of 0 to 100, the 1-bit quantizer has two levels, 0 and 100, with a step of Δ = 100. One-LSB dither is uniform from −50 to 50. A pixel with darkness v then turns black exactly when v + d is above the boundary at 50. That happens when d is above 50 − v.

The dither is uniform over a width of 100, so the chance is (v − 0) / 100 = v / 100. This is Chapter 3’s rule with the two neighbouring levels at 0 and 100. A pixel at 85% is black with chance 0.85. A pixel at 15% is black with chance 0.15. A patch of a thousand pixels at 55% will have about 550 black ones.

So the grey is carried in the density of black pixels. The eye does the rest. It blurs over a patch of pixels, as in the weighted-coin device of Chapter 4, and reads the share of black as brightness. The noise is drawn separately for every pixel, so no pattern builds up, and no bands appear.

The cost is real, and visible when you look closely: the picture is grainy. The lecture says this directly. We trade hard bands for a weak grain, and most viewers prefer grain.

Play with the test image

The device rebuilds the test. On the left are six patches with the lecture’s six darkness values. On the right is a smooth disc, a shape with no edges inside it. Pick how the picture is rounded and how many levels it keeps.

Squeeze a picture

Dark means ink. Left: six patches, 85, 75, 55, 45, 25 and 15 percent dark. Right: a smooth disc. Go to 2 levels, which is 1 bit, and compare plain rounding with dither. Then raise the levels.

2

The picture is our own test image, made to mimic the lecture’s six patch values; it is not the lecture’s file. Each square is one pixel of a 3-pixel grid. Levels are evenly spaced from no ink to full ink. The noise is uniform, one step wide, drawn independently for every pixel and fixed by a seed. Patch averages are measured over every pixel in the patch.

At 2 levels with plain rounding, the six patches become three solid blocks, and the disc is a hard-edged circle with a flat top. With dither, all six patches have different densities, and the disc has a soft, round glow. Switch to 8 levels: the two methods look closer, because the step is small and the bands are narrow. The patch readings show the numbers: dithered patches stay within a point or two of the truth.

Watch for one failure. Symptom: at 2 levels, the lightest patch shows scattered dark specks that look like dirt on the page. Measurement: the patch average is right, but each pixel is wrong by a lot. For p = 0.15 and a step of 1 (the full range), the scatter has standard deviation Δ√(p(1 − p)) = 0.36 of the range. The fix is to give the picture more levels. At 4 levels the step is one third, and the worst scatter falls to 0.17 of the range.

A 1-bit image has black or white pixels only. A region should be 30% dark. With one-LSB dither, what does a pixel in that region do?

Chapter 8

Dithering a Sound

Hear what rounding does to a pure tone and to a fading note, and why the quiet end of the sound is where dither pays off

A piano plays one note and lets it ring. The sound fades slowly to nothing. Record it on cheap equipment with few bits, and the last seconds sound wrong. The warm decay turns crunchy, buzzy, then it cuts to silence a moment too early. The note did not stop. The recorder stopped hearing it.

This chapter takes the two sound examples of the lecture: the quantized sine wave, and a piece of music squeezed to one bit.

A pure sine, rounded

The slides plot a sine wave quantized to 3 bits. Beneath it they plot the error, the rounded wave minus the true wave. Without dither, the error is a tidy periodic pattern, repeating with every cycle of the sine. With dither, the same plot looks like unstructured noise.

The reason is the one from Chapter 1. A sine repeats exactly. The rounding error is a fixed function of the input, so it repeats exactly too. A repeating wave is a sum of pure tones at the pitch of the wave and at whole-number multiples of it, called harmonics. So plain rounding adds new tones to the sound, at pitches that were never in the music. They are not hiss. They are notes, and your ear hears them as distortion.

Look back at the instrument at the top of the page. At 3 bits with plain rounding, the error spectrum shows a few tall red spikes at the odd harmonics of the signal, and almost nothing in between. Switch on dither and the spikes drop into a flat, uniform floor. The total error is bigger, as Chapter 2 predicted: 0.167 Δ² against 0.083. But it is one even hiss, and the ear forgives hiss much sooner than a tone.

Here is a worked picture of one cycle. Let the wave peak at 0.8, with a step of Δ = 0.25. Near the peak, the input stays between 0.625 and 0.8, so it rounds to 0.75 for a stretch of the cycle. The error runs from +0.125 down to −0.05 and back up. Over a full period these swings repeat the same way every time, which is what makes the line spectrum.

Music at 1 bit

The last example in the slides is a stretch of music. First the original waveform, a dense blue blob that swells and fades. Then the same audio at 1 bit: just a few tall spikes where the music was loud, and flat nothing everywhere else. Then 1 bit with dither: a waveform that looks like noise but has the shape of the original swelling inside it, and that sounds like the music with hiss on top.

Why do loud parts survive and quiet parts die? Think of the threshold. A quantizer with a big step has a dead zone around zero: any input smaller than half a step rounds to exactly zero. The quiet passage lives entirely inside the dead zone. Plain rounding outputs a flat line, and the information is gone for good. A note that decays below half a step simply stops.

Dither fixes this the way it fixed the ramp. A small input x, with a step of Δ, rounds to +Δ with chance x / Δ (for positive x), and to 0 otherwise. The output is mostly zeros, with occasional spikes, and the density of spikes follows the signal. The average of the output is the quiet note. A listener’s ear, like the eye in Chapter 7, averages over a short stretch of time. It hears the note, standing in a bed of hiss.

Hear the fade

A note with three partials fades over 2.6 seconds. The three rows show a 25 millisecond window of the original, the plainly rounded copy and the dithered copy. Move the window to the quiet tail. Turn the volume down, then press a play button.

1.60 s
2

Our own synthetic note, not the lecture’s music: partials at 220, 330 and 440 Hz with weights 0.55, 0.33 and 0.17, fading as e−t/0.7, at 22,050 samples per second, peaking at about 0.95. The step is Δ = 2 / 2bits. Dither is uniform and one step wide. Playback is quiet, and starts only when you press a button. Correlation is measured over the window.

At 2 bits, the step is 0.5. Plain rounding goes silent once the note fades below 0.25, at 0.94 seconds, while the original rings on for more than a second and a half. The dithered copy keeps the note, and hisses. Try 1 bit: the effect is stronger. Try 4 bits: the gap narrows, because the dead zone is small.

The price is audible, so measure it. At 2 bits the step is Δ = 0.5. One-step dither adds noise power Δ²/6 = 0.042, an RMS size of 0.20, about 13 dB below a note that peaks near 0.95. That is the hiss you hear. Every extra bit halves the step and cuts the hiss by 6 dB. At 4 bits the RMS is 0.05. So the symptom is hiss, the measurement is Δ²/6, and the fix is more bits, or music loud enough to cover it.

A note fades to a peak of 0.1, and the quantizer step is 0.5. What does plain rounding output for that part of the note, and what does dither do?

Chapter 9

Subtractive Dither: Exact, If You Share the Noise

Take the dither back out after rounding, and prove that the error becomes uniform and independent of the signal

Two friends agree on a secret shake pattern for an egg carton: left, left, right, up, and so on, a long list. One friend shakes the carton by that list before each marble settles. The other friend knows the list. After the marble has settled, she can undo each shake exactly, and recover where the marble would have been.

That is subtractive dither. The sender adds a dither and rounds. The receiver knows the dither, and subtracts it. What remains is a surprisingly clean error. This chapter shows how clean, and what it costs.

The key simplification

Design (B) from Chapter 2 outputs y = Q(x + d) − d. Start from the rounding error of the dithered input, e(x + d) = Q(x + d) − (x + d). Add and subtract d on the right of the output.

y − x
The reconstruction error, as always: the output minus the true input.
QΔ(x + d) − (x + d)
The rounding error of the sum z = x + d. The two d terms swapped sides, so the dither is gone from the formula.
e(x + d)
The same thing under its short name. The error is exactly the rounding error of the dithered input.

The lecture states the consequence. The reconstruction error equals the quantization error of the dithered input. If z modulo Δ is uniform, that is, if z lands uniformly inside its cell, then the error is uniform. Compare design (A), where the error was d + e(x + d): two terms added together. Here there is one.

The theorem

Let d be uniform on −Δ/2 to Δ/2, independent of x. Then for design (B), the error is uniform, and it does not depend on x.

ε ~ U(−Δ/2, Δ/2)
The error is equally likely to be anywhere in one step-wide window, for every x.
ε ⊥ x
Independent: the signal tells you nothing about the error.
E[ε | x] = 0
On average the error is zero, whatever x is.
Var(ε) = Δ² / 12
The spread of a uniform window of width Δ. This is the textbook noise power, now exact.

The proof, from the appendix

Step 1. The quantizer looks only at where z sits inside a cell. Write x = kΔ + r, with r between −Δ/2 and Δ/2, so z = kΔ + (r + d).

Step 2. Since d is uniform over a full step, r + d is uniform over an interval of width Δ. Its position moves with r, but its width does not.

Step 3. Wrap that interval into one cell. Picture a clock face whose circumference is Δ. A uniform smear that covers the clock exactly once, from any starting hour, lands on each hour with equal chance. So the wrapped position is uniform on −Δ/2 to Δ/2, and the rounding error is minus that position, also uniform.

Step 4. The uniform result holds for every r, hence for every x. A distribution that is the same whatever x is must be independent of x. That is the proof. The wrapping “forgets” where x was.

The error ranges over one step, so its variance is the variance of a uniform window of width Δ: (Δ)² / 12. Check with Δ = 1: the error is uniform from −0.5 to 0.5, so its mean square is the integral of e² from −0.5 to 0.5, which is 2 × (0.5)³ / 3 = 1/12 = 0.0833.

Non-subtractive dither is only unbiased

Design (A) is not so tidy. Its average error is zero for every x, but its noise level is not the same for every x. We know the output is the lower or upper level with chances 1 − p and p, where p is the fraction of the way x sits between them. A coin with those chances has variance Δ² p (1 − p). At a level, p = 0 and the error is exactly zero. Halfway, p = 0.5 and the power reaches Δ²/4. The noise rises and falls with the signal. The lecture calls the first design good for images and the second great for analysis.

Plug in the numbers. Averaged over all positions, p (1 − p) has mean 1/6, so design (A) has average power Δ²/6, exactly twice design (B). Slide x across the device and compare.

Where does the noise live?

Each curve is the error power at a given x, in Δ². Switch curves on and off. Press Check by simulation to scatter dots measured from 400 random draws at each x.

0.50

The curves are exact formulas: plain e(x)², non-subtractive Δ² p(1 − p) with p the fractional part of x / Δ, subtractive Δ²/12. The dots are from your browser’s random numbers, 400 draws at each of 41 positions, ties round up. Uniform dither, one step wide.

The purple bump of design (A) peaks at x = 0.5 and touches zero at each level. The flat green line of design (B) never moves. This is the real meaning of “independent of the signal”: the noise floor is the same whether the music is loud, quiet or silent. (Later work, by Lipshitz, Wannamaker and Vanderkooy in 1992, shows that a triangular dither of two steps also makes the noise level steady without subtracting. That is beyond the slides, from the audio-dither literature; read the paper before you rely on it.)

The catch: the receiver needs the noise

Subtractive dither is only exact if the receiver subtracts the same d. A pseudo-random generator is a short program that makes a long list of numbers that look random. It is fully determined by its starting value, the seed. If the sender and the receiver start the same generator from the same seed, they produce the same list of d values, and no list has to be sent. That is how real systems share a dither.

Now see how fragile it is. The receiver subtracts d′, which might not equal d. The output is Q(x + d) − d′, so the error is e(x + d) + (d − d′). If d′ is an independent draw, the first two parts together are design (A)’s error, with power Δ²/6, and d′ adds Δ²/12 on top, so the total is Δ²/4, three times the matched case.

Share the dither

The sender dithers a sine wave. Pick what the receiver subtracts. The two lists show the first six dither values each end holds, in steps. The histogram shows the error of 512 samples.

Illustrative: a sine of peak 0.8 on a range of −1 to 1, 3 bits, so Δ = 0.25, 512 samples. Errors are shown in steps. The generator is a small seeded random function, standing in for any shared pseudo-random generator. Powers are measured over the 512 samples.

Matched, the histogram is a flat block from −0.5 to 0.5 with power near 0.083. With no subtraction, it grows into a triangle from −1 to 1 and the power doubles. With the wrong d′, it grows wider still, and a stream late by only one sample is as bad as a different seed. The sharing is a real engineering burden, and the reason many systems choose design (A) instead.

Compared with plain rounding, what does subtractive one-LSB dither change in the error?

Chapter 10

Field Guide: Dithering on One Page

Keep the numbers, the terms, the checklist and the code, and see where the idea goes next

The lecture ends with five lines, and they are the lesson in short. Bennett’s theorem says when rounding error looks uniform: at high rate. Dithering is how to engineer that behaviour at finite rate. Non-subtractive dither linearizes the quantizer in expectation, which is good for images. Subtractive dither gives uniform, input-independent error, which is great for analysis, but the receiver needs the noise sequence. Stochastic rounding is an unbiased randomized quantizer, closely tied to dither, and NVFP4 uses it to stay unbiased.

The numbers

QuantityValueWhere it comes from
Step from bitsΔ = range / 2bits; 8-bit pixel to 4 bits gives Δ = 16Chapters 0 and 4, slide 14
Plain rounding errore(x) = Q(x) − x, a sawtooth in [−Δ/2, Δ/2]; average power Δ²/12Chapter 2, slide 8
One-LSB ditherd ~ U(−Δ/2, Δ/2); P(round up) = r / ΔChapters 3 and 6, slides 12 and 19
LinearizationE[Q(x + d) | x] = x for every xChapter 3, slide 13
Lecture pixelΔ = 16, x = 100: P(112) = 1/4, P(96) = 3/4, mean 100Chapter 4, slide 14
Non-subtractive errorε = d + e(x + d); mean 0; power Δ² p(1 − p), average Δ²/6 (3 dB above plain)Chapters 2 and 9, slide 9
Subtractive errorε = e(x + d) ~ U(−Δ/2, Δ/2), independent of x; power Δ²/12Chapter 9, slides 29, 30 and 33
Any dither width wpower (1 + w²) Δ²/12; largest bias (1 − w)Δ/2 for w up to 1Chapter 6 (our derivation, checked numerically)
Subtractive, wrong d′power Δ²/4, three times the matched caseChapter 9 (our derivation)
Feedback dead band|y| below Δ / (2(1 − a)) for a loop with gain −a; 5Δ for a = 0.9Chapter 1 (our example)
NVFP4 stochastic roundingP(round to vhigh) = (x − vlow) / (vhigh − vlow), after block scalingChapter 5, slide 17
E2M1 grid, 16-value blocks0, 0.5, 1, 1.5, 2, 3, 4, 6; about 4.5 bits per numberNVIDIA’s public description, not the slides

The terms

TermMeaning
QuantizerA rule that sends each number to one of a fixed list of levels.
Step, Δ, LSBThe gap between levels; the smallest change the output can show.
Banding, contouringFlat stripes in a smooth image, made by rounding.
Harmonic, spurious toneA pitch that is a whole-number multiple of the signal’s pitch, added by rounding.
Limit cycle, dead bandA loop that never settles; the range where rounding cancels every shrink.
DitherNoise added on purpose before rounding.
LinearizationThe average output equals the input for every input.
Stochastic roundingRound up or down at random, with chances set by distance, so the average is exact.
Subtractive ditherAdd d, round, subtract the same d; the receiver must know d.
SeedThe starting value that fixes a pseudo-random list, so two parties can share it.

The checklist

The core as code

All four ideas fit on one screen. The data flow is: an array x in, an array of levels out, one random number per element. The block function shows the NVFP4 pattern: a weight array of shape (n, 16 × m) is cut into blocks of 16, each block gets one scale, and the scaled values are rounded onto the grid. The 8-bit storage of the scale is left out.

pythonimport numpy as np

def q(z, step):                          # uniform rounding quantizer Q_step(z), ties up
    return step * np.floor(z / step + 0.5)

def dither_a(x, step, rng):                # (A) y = Q(x + d), d ~ U(-step/2, step/2)
    d = (rng.random(x.shape) - 0.5) * step
    return q(x + d, step)

def dither_b(x, step, seed):                # (B) y = Q(x + d) - d, receiver rebuilds d from seed
    d = (np.random.default_rng(seed).random(x.shape) - 0.5) * step
    return q(x + d, step) - d

def stochastic_round(x, grid, rng):          # grid: sorted 1-D array; x inside [grid[0], grid[-1]]
    hi = np.clip(np.searchsorted(grid, x, side="right"), 1, len(grid) - 1)
    lo = hi - 1
    p = (x - grid[lo]) / (grid[hi] - grid[lo])       # P(round up) = distance / gap
    return np.where(rng.random(x.shape) < p, grid[hi], grid[lo])

POS = np.array([0, .5, 1, 1.5, 2, 3, 4, 6])             # E2M1 magnitudes (NVIDIA's public description)
GRID = np.concatenate([-POS[:0:-1], POS])             # 15 signed values, ascending

def block_sr_fp4(w, rng, block=16):
    b = w.reshape(-1, block)                         # (n_blocks, 16)
    s = np.abs(b).max(axis=1, keepdims=True) / 6.0      # one scale per block, so b / s fits in [-6, 6]
    s = np.where(s == 0, 1.0, s)
    return (stochastic_round(b / s, GRID, rng) * s).reshape(w.shape)

rng = np.random.default_rng(0)
x = np.full(100_000, 100.0)                       # the lecture's pixel, 4-bit step 16
y = dither_a(x, 16.0, rng)
print(y.mean(), np.unique(y))                  # about 100.0, [ 96. 112.]
print(((y - x) ** 2).mean())                      # about 48 = 16**2 * p(1-p)

Where it sits, and where to go next

Dithering sits between two ideas in this course. Before it, quantization noise gave the model of rounding error, and Lloyd-Max chose where the levels should go. After it, the discrete Fourier transform is the tool behind every error spectrum on this page, and the lecture promises a spectrum example for later. The course begins in the introduction and the signals themselves in discrete signals.

Two bridges lead outside the course. Image printing and screens use cleverer dither patterns than independent noise, such as error diffusion. And low-bit training is where stochastic rounding is now most used. Both use exactly the idea of Chapter 3.

Credit: the lecture, slides and its proofs are by Mert Pilanci of Stanford (EE269, slides). The devices, the worked numbers and the extra derivations are ours. Toy numbers are labelled illustrative in each device.

The takeaway. Rounding is deterministic, and a deterministic error follows the signal. Add one step of random noise first, and the rounding becomes a weighted coin whose average is exact. You pay in noise power. You gain an error that cannot form bands, tones or stuck outputs, and you keep every bit of quiet detail.
Which statement about the two dither designs is correct?

Now press Present or Teach and explain, from memory, why the bands and tones appear, why one step of noise makes the average exact, and what subtractive dither buys and costs. Then go back to the sine wave at the top, set 3 bits, and switch between plain rounding and dither. Say aloud what each red spike is.

EE269 Lecture 5: Dithering and Stochastic Rounding
The course site · Lecture slides · All slides · Back to Gleams