include/brotensor/detail/hash_rng.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // brotensor detail/hash_rng.h — counter-based hash RNG shared by the CPU and | ||
| 4 | // CUDA backends (and by tests that need to reproduce an op's noise). | ||
| 5 | // | ||
| 6 | // A stateless, order-independent uniform generator: the uniform for element | ||
| 7 | // `index` of stream (`seed`, `domain`) is a pure function of those three | ||
| 8 | // integers, so a CPU loop and a CUDA grid produce bit-identical noise for | ||
| 9 | // bit-identical inputs. The mixer is splitmix64 (Steele/Lea/Flood) — a | ||
| 10 | // bijective 64-bit finaliser with full avalanche, which is exactly what a | ||
| 11 | // counter-based generator needs; the input is `seed ^ (domain + index)` so a | ||
| 12 | // caller varies `seed` per step and each op keeps a distinct `domain` for | ||
| 13 | // each of its noise streams (a position stream and a vocabulary stream must | ||
| 14 | // not collide at index 0). | ||
| 15 | // | ||
| 16 | // The uniform takes the high 24 bits of the hash to a float in [0, 1) — the | ||
| 17 | // same mapping the Philox ops use (rand_uniform, sample_logits), so the value | ||
| 18 | // set is identical: multiples of 2^-24, never 1.0. | ||
| 19 | // | ||
| 20 | // Every function is `inline`, so CPU, test, and pure-C++ CUDA callers share | ||
| 21 | // identical integer arithmetic (and `__CUDACC__` stays guarded if an external | ||
| 22 | // CUDA translation unit includes it). Metal cannot include this header (MSL is | ||
| 23 | // compiled from source strings); src/metal/masked_diffusion.mm carries a | ||
| 24 | // verbatim copy. | ||
| 25 | |||
| 26 | #include <cstdint> | ||
| 27 | |||
| 28 | #if defined(__CUDACC__) | ||
| 29 | #define BROTENSOR_HASH_RNG_HD __host__ __device__ | ||
| 30 | #else | ||
| 31 | #define BROTENSOR_HASH_RNG_HD | ||
| 32 | #endif | ||
| 33 | |||
| 34 | namespace brotensor::detail { | ||
| 35 | |||
| 36 | // Domain constants for the ops that draw from this generator. Arbitrary | ||
| 37 | // large odd constants, far apart, so `domain + index` never overlaps across | ||
| 38 | // streams for any index a tensor can hold. | ||
| 39 | inline constexpr std::uint64_t kHashDomainMaskedDiffusionPosition = 0x5851F42D4C957F2DULL; | ||
| 40 | inline constexpr std::uint64_t kHashDomainMaskedDiffusionClass = 0x14057B7EF767814FULL; | ||
| 41 | |||
| 42 | // splitmix64 finaliser: one round of the SplitMix64 output function applied | ||
| 43 | // to `x` (including the golden-ratio increment, so x == 0 does not map to 0). | ||
| 44 | 659766 | BROTENSOR_HASH_RNG_HD inline std::uint64_t splitmix64(std::uint64_t x) { | |
| 45 | 659766 | x += 0x9E3779B97F4A7C15ULL; | |
| 46 | 659766 | x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ULL; | |
| 47 | 659766 | x = (x ^ (x >> 27)) * 0x94D049BB133111EBULL; | |
| 48 | 659766 | return x ^ (x >> 31); | |
| 49 | } | ||
| 50 | |||
| 51 | // Raw 64-bit hash for element `index` of stream (seed, domain). | ||
| 52 | 659327 | BROTENSOR_HASH_RNG_HD inline std::uint64_t hash_u64(std::uint64_t seed, | |
| 53 | std::uint64_t domain, | ||
| 54 | std::uint64_t index) { | ||
| 55 | 659327 | return splitmix64(seed ^ (domain + index)); | |
| 56 | } | ||
| 57 | |||
| 58 | // Uniform in [0, 1): top 24 bits of the hash / 2^24. Exact in FP32. | ||
| 59 | 659362 | BROTENSOR_HASH_RNG_HD inline float hash_uniform(std::uint64_t seed, | |
| 60 | std::uint64_t domain, | ||
| 61 | std::uint64_t index) { | ||
| 62 | 659362 | return static_cast<float>(hash_u64(seed, domain, index) >> 40) * | |
| 63 | (1.0f / 16777216.0f); | ||
| 64 | } | ||
| 65 | |||
| 66 | } // namespace brotensor::detail | ||
| 67 | |||
| 68 | #undef BROTENSOR_HASH_RNG_HD | ||
| 69 |