Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.1.0 #3

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open

V1.1.0 #3

wants to merge 41 commits into from

Conversation

Grochocinski
Copy link
Collaborator

@Grochocinski Grochocinski commented Feb 10, 2025

Overview

This PR overhauls the mrg32k3a random number generator to significantly improve performance, reduce redundancy, and modernize the internal structure. Highlights:

  • 30–40% faster distribution sampling and RNG calls
  • ~95% test coverage across all streams and variate generators
  • Removed outdated components and streamlined internal logic
  • Added GitHub actions to ensure compatibility and consistency between Python and Numpy versions

Key Changes

mrg32k3a

  • Replaced math-heavy logic with faster, minimal-overhead equivalents using Python's built-in types and operators
  • Precomputed transition matrices (A[1|2]p[47|94|141]) and constants (mrgm1_plus_1, etc.) to eliminate redundant computation
  • Internal stream states now use np.ndarray for efficient slicing and modular matrix math, while seed() still accepts a Python tuple
  • Centralized and deduplicated modular state transitions via _advance_state() and _apply_transition() helpers
  • Simplified stream/substream/subsubstream advancement logic
  • Ensured consistency between return values and type hints throughout the codebase

Testing

  • Expanded test suite to include:
    • Stream reset and advancement behavior
    • Random variate correctness and distribution validation
    • Vector generation summation and bounds checking
  • Tests reorganized by class for better structure and readability
  • Achieved ~95% line coverage of generator logic
  • Added GitHub actions to automatically run the testing suite with all supported versions of Python (minus 3.5 which had dependency issues)

Performance Summary

Component Change
RNG core (random()) +32%
BSM (normal quantile approx) +16%
Normal Variate Generation +18%
Lognormal Variate Generation +17%
Poisson Variate Generation +22%
Gumbel Variate Generation +39%
Binomial Variate Generation +57%
Integer random vector (simplex) +16%
Continuous random vector +13–14%
Stream advancement (start_*) +7–9%
Reset methods (reset_*) −93% to −110%
(See note below)
Start Fixed S_SS_SSS +6–17%

Note on regressions:
The reset_*() methods show a microbenchmark-level regression due to the addition of a tuple() conversion from NumPy arrays. This cost is on the order of microseconds, and resets are typically infrequent in most applications. Overall performance remains excellent even in scenarios with frequent resets.


Summary

This refactor delivers a leaner, faster, and more maintainable implementation of mrg32k3a, with significant speedups in all key random variate generation paths and much broader test coverage. Ready for review and merge.

@Grochocinski
Copy link
Collaborator Author

Grochocinski commented Mar 19, 2025

Speed Up Testing Results (TLDR ~30% Improvement)

# Values Processor (Architecture) Old Avg (sec) New Avg (sec) Improvement
1M Snapdragon X1P-42-100 (ARM64) 2.79 1.88 32.54%
1M AMD 5900X (AMD64) 1.29 0.91 29.91%
10M Snapdragon X1P-42-100 (ARM64) 13.59 9.35 33.10%
10M AMD 5900X (AMD64) 2.79 1.88 31.14%

sample size of 10 runs each, executed in randomized order

@Grochocinski Grochocinski marked this pull request as ready for review March 20, 2025 17:02
@Grochocinski Grochocinski changed the title Numpy Rewrite + Optimization V1.1.0 Mar 21, 2025
@Grochocinski
Copy link
Collaborator Author

Alright, I think it's actually good to go at this point (passes all tests / Ruff formatting/linting checks / type checks, as well as showing substantial performance uplift and having easy-to-read code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant