trunk/93df58781e93005bae99d4299afda9a3efebae4f: Implement MKLGenerator (#151218)
New implementation eliminates 21 sequential repetitions in random number generation, crucial for scientific ML.
The PyTorch team has resolved a significant bug affecting the reproducibility of machine learning experiments. The issue, tracked as #132395, stemmed from the library's Intel Math Kernel Library (MKL) random number generator. The old implementation repeatedly reseeded the MKL generator (`vslStream`) with values from the main CPU generator, causing the random sequence to reset and produce repeating variates. This flaw was particularly problematic for sequential draws in functions like `torch.multinomial`, a common operation in reinforcement learning and probabilistic models, where non-random repeats can skew results.
The new fix, implemented in pull request #151218 by developer Fadi Arafeh, introduces a redesigned `MKLGeneratorImpl`. This generator is seeded just once using the CPU generator and then maintains a persistent `vslStream` state, unlocking the generator's full statistical period. Crucially, to maintain a seamless user experience, the state of this new MKL generator is automatically saved and restored in sync with the CPU generator's state, meaning researchers don't need to manage it separately. A dedicated test (`test_multinomial_sequential_draw`) that previously recorded 21 repetitions now shows zero, confirming the fix's effectiveness for reproducible science.
- Fixes bug #132395 where the MKL RNG produced 21 sequential repetitions in `torch.multinomial` tests.
- New `MKLGeneratorImpl` seeds once and reuses a persistent `vslStream`, ensuring the full RNG period.
- State is managed automatically via the CPUGenerator, requiring no change to user code for reproducibility.
Why It Matters
Ensures statistically sound and reproducible results for ML research, especially in reinforcement learning and probabilistic modeling.