Quantize with stochastic rounding from given random draws
Why it matters Stochastic rounding keeps low-precision training and optimizer updates unbiased, so small gradient steps are not always lost.
Round-to-nearest always pushes a value the same way, so tiny updates can vanish. Stochastic rounding rounds up with probability equal to the fractional part, which is unbiased on average.
Write stochastic_round(x, scale, u, qmin=-128, qmax=127). x and u are arrays of the same shape; u holds uniform random numbers in [0, 1). Return integers q = clip(floor(x / scale + u), qmin, qmax).
def stochastic_round(x, scale, u, qmin=-128, qmax=127)Example
x [0.25, 0.25, -0.75], scale 1, u [0.5, 0.8, 0.2] gives [0, 1, -1].
Understand the idea
Suppose you must pay in whole rupees but your bill is 0.3 rupees every day. Always rounding to the nearest gives 0 forever, and the shop never gets paid. If instead you pay 1 rupee on 30 percent of days, on average you pay exactly 0.3. That is stochastic rounding. Worked example: x = 0.25 with u = 0.5 gives floor(0.75) = 0; with u = 0.8 gives floor(1.05) = 1. Over many draws, 0.25 rounds up a quarter of the time. For -0.75 with u = 0.2, floor(-0.55) = -1. Rule: q = floor(x / s + u), u ~ Uniform[0, 1), then clip. Low-precision training uses it for weight updates so tiny gradient steps add up instead of disappearing.