| ▲ | For random numbers on the GPU, encrypt a counter(klauria.tech) |
| 33 points by evakhoury 3 days ago | 16 comments |
| |
|
| ▲ | loeg 24 minutes ago | parent | next [-] |
| Where does the counter come from? Couldn't you just use any small-state PRNG (LCG/PCG/Xoroshiro), assuming you don't need truly unpredictable random numbers (given the author mentioned Mersenne Twister)? I know PCG has the seek property the author mentions, although I don't know how cheap it is. Real encryption is a crazy expensive operation compared to anything these PRNGs are doing, although I guess Philox isn't AES. |
| |
| ▲ | articulatepang 10 minutes ago | parent | next [-] | | Whenever a GPU kernel is launched, each thread has an id. In cuda that's threadIdx.x, threadIdx.y, blockIdx.x, etc. So in any GPU kernel there's a very natural way to have a counter within each kernel. To get a unique stream of random numbers across an entire program execution, you also need different kernel launches to each add an amount so it's different from the previous kernel's counters. The problem with small-state PRNG's is that each GPU thread has to do memory reads and writes to VRAM to get and persist the state. That takes memory bandwidth, which is what most GPU workloads (but not all!) are typically bottlenecked on. Classic PCG is a stateful generator, and takes O(log n) steps to seek ahead. The log n may or may not be okay in GPU code; one risk is that the branching causes warp divergence. I agree that encryption is expensive! But encryption based PRNGs usually end up being the fastest for most GPU workloads, because the methods used in production are derived from ciphers, basically by reducing the number of rounds. That's how Philox and Threefry work. For benchmarks, see https://klauria.tech/wgsl-rng-benchmark/ | |
| ▲ | fc417fc802 11 minutes ago | parent | prev [-] | | There are various tradeoffs as mentioned in the linked essay. The obvious issue when running on the GPU is synchronization - you need an RNG per thread (or pixel or whatever) whereas a counter based solution means you can call it like rng( shared_key, unique_index ). Algorithms such as philox use a weakened encryption primitive so they aren't as expensive as you might expect. |
|
|
| ▲ | blaaarg123 an hour ago | parent | prev | next [-] |
| > prefer a stateless RNG that works by encrypting or hashing a counter. isn't that the definition of stateful? |
| |
| ▲ | fc417fc802 23 minutes ago | parent | next [-] | | From the perspective of the overall process, yes there is still state. However what is typically meant by a stateful RNG is a function which you first seed that has to persist data between calls (and usually generates a sequential stream). | | |
| ▲ | loeg 16 minutes ago | parent [-] | | I think the answer is yes, a counter is state, but it's state that is already available anyway. | | |
| ▲ | articulatepang 7 minutes ago | parent [-] | | Correct, each thread in a GPU kernel launch has its threadIdx.x and so forth. It needs this just in order to know, "Which part of the work am I supposed to do?" For example, if you're writing matrix multiplication, each thread needs to know which tile it's working on. If you're simulating particles, each thread might have some block of particles to work on. And so forth. Any GPU code always has this available. |
|
| |
| ▲ | cowlevel 39 minutes ago | parent | prev [-] | | Not really, you can skip the counter forward. | | |
|
|
| ▲ | blintz 2 hours ago | parent | prev | next [-] |
| If you ever need a lot of cryptographically random numbers on the GPU, you can do cool lattice stuff to do high throughput PRF evaluation. Basically, you can use ‘learning with rounding’ (https://latticeassumptionzoo.org/lwr/) to make the RNG (mostly) a big matmul, which GPUs are great at. This needs to use integers, so Hopper is way better than Blackwell for it. It’s also probably not better than AES in most cases (bc of available intrinsics), but it is way more fun. |
|
| ▲ | leni536 44 minutes ago | parent | prev | next [-] |
| Aren't there PRNGs that are designed so that they can be cheaply "forked" into "independent" generators without shared state? (For some value of "indpendent") |
| |
| ▲ | fc417fc802 18 minutes ago | parent [-] | | Yes, and that's exactly what's being described in the linked essay. They generally work by using a counter as state. Forking is typically implemented by incrementing the top bits while fetching the next number from a given stream is implemented by incrementing the bottom bits. Thus skipping forward and backward in a given stream is O( 1 ). |
|
|
| ▲ | hinkley an hour ago | parent | prev [-] |
| The typical way a CSPRNG is to use cryptography to encrypt something and carving it up into the requested amount of output. This is just a less common variant. Educational perhaps, but newsworthy I don’t know. |
| |
| ▲ | loeg 15 minutes ago | parent [-] | | This article isn't talking about CSPRNGs. | | |
| ▲ | fc417fc802 5 minutes ago | parent [-] | | Aren't they cryptographically secure if a full strength cipher or hash is used? For example the output of sha3( shared_key, index ) is certainly secure. It's just that they typically employ weakened variants in order to achieve better performance. |
|
|