For random numbers on the GPU, encrypt a counter
This piece has three interactive demos!
I'm writing this to tell you about an elegant and simple idea that might make your code go faster. Depending on how you use GPUs, your code might already be using this idea: it has gone mainstream in ML frameworks since I first heard about it and wrote a library ten years ago in 2016.
The idea is not mine! I just find it aesthetically pleasing, and so might you. (Don't use my old library! It used unsupported internal cuRAND functions. There are better options now, like cuRANDDx.)
Here's the idea: if you're generating random numbers on a GPU, by default you should avoid RNGs with internal state that's updated for every number produced. Instead, prefer a stateless RNG that works by encrypting or hashing a counter.
There are some excellent options so you don't need to roll your own, called Philox and Threefry. PyTorch, JAX and cuRANDDx all use these counter-based RNGs.
You can tell you're using a stateful RNG if you have to allocate memory for per-thread RNG state, and then read and write that state.
Why counters are nice
Both VRAM and memory bandwidth are scarce resources on the GPU. It's likely that your GPU algorithm is memory bandwidth-bound, or perhaps VRAM-bound. By using a counter-based RNG, each GPU thread can generate its own sequence of random numbers using pure computation, without having to read or write VRAM. You also avoid any stalls caused by waiting for those memory operations.
The amount of state can be large! Mersenne Twister needs 2.5 kB of state per thread, which is why no-one uses it unmodified on GPUs.
When this advice doesn't apply
Counter-based RNGs do more computation, so if your benchmarks reveal that your code is compute-bound, you might want to spend memory to save compute. In WGSL, Philox is particularly slow because it needs 64-bit multiplies that need to be emulated. Or if you initialize an RNG and use it for a single kernel launch and never need random numbers after that, then you don't need to persist state to memory.
If you've read this far, I think I've communicated the main idea. If you like, you can keep reading for more explanation and some tips, or you can just play with these demos!
Demo time!
All these demos use WebGPU. They compare three methods:
- a fast stateful RNG (XORWOW)
- a counter-based RNG but with counter state stored in VRAM
- a counter-based RNG with no stored state
The latter two give a clean measurement of the cost of reading and writing the state. You'll see that the stateless RNG doesn't always win! Philox needs the top 32 bits of a 32x32-bit multiply. cuda has an instruction for that (__umulhi), but WGSL doesn't. So these browser demos somewhat exacerbate Philox's higher compute intensity because one of the key operations needs to be emulated using several 16-bit multiplies.
The first demo is really simple: it estimates pi by throwing lots of random darts in a square and counting the ones that land inside the circle. With Chrome on my 2021 M1 Pro, stateless Philox is about 70% faster than XORWOW:
This next one shows ink diffusion. That sounds fancy! But this demo is super simple, it has no fluid dynamics or anything like that. It models each particle of ink as a jiggling wiggling random walker, being jostled by tiny molecules. (For fancier and more accurate physics simulations, see some of my other projects). So at each timestep, each ink particle gets a small random gaussian added to its position in each dimension.
On my machine, stateless Philox is about 75% faster than XORWOW on this demo:
The two demos above use a lot of random numbers and do little computation otherwise. Here's something with more computation per random number sampled. It's a WebGPU port of a simple path tracer I wrote in cuda. It renders an image by sending multiple rays through each pixel, each one slightly jittered, and letting them bounce around several times. By doing this we get physical effects like soft shadows and bounced colored light.
On my laptop, the stateful XORWOW RNG performs the best on this workload:
Your results will be different depending on your hardware and your browser's WebGPU implementation; Philox performance is worse on Firefox on my laptop. But the stateless version will always use less memory (zero memory for RNG state).
Why do people need random numbers on the GPU?
These days, the most common reason is probably machine learning. Most famously, LLM inference uses random numbers to sample from the next-token distribution. LLM training often uses dropout, which needs a lot of random numbers: every forward pass needs a fresh random mask over activations. For image and audio models, the training set is sometimes augmented on-device with random crops, flips and color jitter, so that models learn to be invariant to those transformations. In diffusion models, training adds random noise at a random timestep to each example, and sampling starts from pure Gaussian noise and might inject more during denoising steps. Reinforcement learning strategies often use huge amounts of random numbers. And on and on. Modern ML is all on GPU and uses random numbers everywhere.
The path tracer demo above is a simple example of physically based rendering, which needs multiple random numbers for every pixel. GPU random numbers are pervasive in physics simulations. I've used them for diffusion monte carlo for high-accuracy quantum chemistry calculations, and also for setting the temperature in molecular dynamics simulations, where you need to sample velocities from the Maxwell-Boltzmann distribution.
Another place I've used random numbers is MCMC sampling of a Bayesian model. For example, I designed this website by pitting pairs of designs against each other and picking the winner, many times over. To pick the right pair to compare I used MCMC to sample from the posterior distribution, after including my votes. I'll write about this elsewhere but yes, that's right! Somehow even frontend design can use large amounts of random numbers! (My implementation used numpy on the CPU, so it wasn't, strictly speaking, GPU random numbers. But Bayesian inference on the GPU is common.)
What's wrong with using normal RNGs on GPUs?
Most RNGs have two main parts: state transition and output. There's some internal state S, a state transition function t(S), and an output function o(S). The way you use the RNG is, you call o(S) to get the random numbers you actually want, then you call t(S) to move the state along:
S = initialize_rng(seed)
while (...):
r = o(S)
S = t(S)
# Use the random number r for whatever
On a GPU with many threads, each thread needs its own independent sequence of random numbers. (If each thread generated the same sequence, the whatever you're doing with the random numbers would break, because they're totally not random any more.) This looks something like:
# threadIdx is some integer that identifies this thread.
def concat_bits(a,b):
return (a << 32) | b
None of these RNG schemes produces cryptographically secure random numbers! They could be designed to do that but for most applications that would be overkill. So it's faster to steal the ideas from cryptography but change the specifics to suit the problem at hand.
Bonus: reasons to reuse random numbers
I'd like to leave you with some interesting reasons to reuse random numbers on purpose. You can do this easily with counter-based RNGs by simply arranging to pass in the same seed and the same counter values.
Suppose you're computing something that uses random numbers as well as some fixed parameters, for example pricing options. You can numerically compute a derivative with respect to some parameter by changing the value of the parameter and rerunning the computation with the same sequence of random numbers. If you'd used fresh random numbers, the noise due to randomness might swamp the difference that's due to the change in the parameter. Another way to think of this is that you might need a lot more random numbers to converge the calculation enough to where the noise is smaller than the actual difference you're trying to measure. By reusing random numbers, you can cancel out most of the noise.
When training a model with dropout, you want to drop out the same units in both the forward and backward passes. You might think this means you need to store which units are dropped. That takes memory. Instead, you can replay the random numbers exactly so you end up choosing the same units to drop in the backward pass. This is what FlashAttention does: it saves the RNG state instead of the dropout mask, and rebuilds the mask in the backward pass.
By using counter-based RNGs, simulations can be made precisely deterministic even if the underlying work was split differently on the hardware. For example, if you run the same simulation on a bigger GPU, you might use more threads for each kernel launch. If the random number generation is done carefully, for example by tying the counter to particle ids rather than thread ids, you can have both simulations produce exactly the same trajectory. This is important so that scientific findings can be reproduced by different labs.
Comments
No comments yet. Start the discussion.