NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
▲For random numbers on the GPU, encrypt a counter (klauria.tech)
blintz 15 hours ago [-]
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.
blaaarg123 14 hours ago [-]
> prefer a stateless RNG that works by encrypting or hashing a counter.

isn't that the definition of stateful?

fc417fc802 13 hours ago [-]
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 13 hours ago [-]
I think the answer is yes, a counter is state, but it's state that is already available anyway.
articulatepang 13 hours ago [-]
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.

loeg 12 hours ago [-]
The one more relevant to RMW PRNG state from the article is named "iterIdx."
cowlevel 13 hours ago [-]
Not really, you can skip the counter forward.
rahimnathwani 13 hours ago [-]
How do you determine how much to ship it forward?
cowlevel 13 hours ago [-]
frame count * pixels per frame * max random calls per pixel
loeg 13 hours ago [-]
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 13 hours ago [-]
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 13 hours ago [-]
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.

Agingcoder 11 hours ago [-]
This idea is a lot older than 2011. I’ve seen it in production systems from 2001-2002 ( and it’s still in use in these systems today ), in an engineering context.

If anything I’m surprised anyone would claim it as original in 2011.

Edit: I think these methods were actually described in the old Numerical Recipes books so the research is probably even older .

leni536 13 hours ago [-]
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 13 hours ago [-]
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 ).
loeg 12 hours ago [-]
Basically all of them.
hinkley 14 hours ago [-]
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 13 hours ago [-]
This article isn't talking about CSPRNGs.
fc417fc802 13 hours ago [-]
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.
loeg 12 hours ago [-]
The article isn't talking about full strength ciphers or hashes, either.
fc417fc802 11 hours ago [-]
Yes, I'm aware. My point there is that counter based RNG algorithms (the thing that the linked essay is about) are only insecure to the extent that they are intentionally weakened.
loeg 10 hours ago [-]
I sort of disagree with that framing. They're insecure to the extent that they aren't designed to be secure. It doesn't require intentional weakening to fail to create a secure design. Still off topic for the article.
fc417fc802 2 hours ago [-]
Sure, you don't necessarily have to start from a secure block cipher or hash function when constructing one. But historically that's how they were arrived at (at least AFAIK) and the title of the linked essay directly refers to encrypting a counter.

For example you can literally aes_128_ecb( key, counter ) which matches the title of the linked essay, clearly qualifies as a counter based RNG, and it is about as secure as it gets. It's also painfully slow (unless you're utilizing hardware acceleration) thus solutions such as philox and threefry were born. Threefry in particular is a deliberately weakened version of an otherwise secure cipher.

hinkley 12 hours ago [-]
I think you're overestimating the gravity of CSPRNGs.
loeg 12 hours ago [-]
They're just completely tangential to the topic of the article.
hinkley 11 hours ago [-]
Only operationally. How much time have you spent reading CSPRNG code? Because I’ve spent months.

Mechanically they are very similar. The guarantees they offer are different in distinct ways, yes, but this is essentially a non compliant CSPRNG.

loeg 10 hours ago [-]
I've spent a lot of time with CSPRNGs, actually. Non-compliant CSPRNGs aren't CSPRNGs. And the whole topic is completely orthogonal to the article.
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 09:34:15 GMT+0000 (Coordinated Universal Time) with Vercel.