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

SYSTEMDS-3556 Counter based random number generator #2186

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ichbinstudent
Copy link

Summary

Adding counter based RNG which improves random quality and speed

Details

The PRNG uses Philox4x64_10 to generate batches of random double values with a 64 bit randomness. The algorithm was tested for correctness by comparing its output for certain counters and keys with an existing implementation in c.
This reference implementation called openRAND was tested by the authors using various statistical methods described here.

Advantages

  • Quality: Instead of 32 bits in java.util.Random, Philox4_64 produces 64 bits of randomness. While java.util.Random has a period of only (2^48), while the period of Philox4_64 is 2^256 - 1.
  • Speed: While the Java version of Philox4x64_10 is only about half as fast as java.util.Random, there is a Cuda kernel version available producing the exact same sequence of random numbers. This means that the Cuda and Java versions can be used interchangeably. If a system has support for Cuda, the kernel ist used, if not, the Java version can be used as a fallback. The kernel version is around 200 times faster than java.util.Random, and even faster, if the results are not copied to the CPU but kept in the GPU's memory.
  • Parallelisation: When using state based PRNGs, it is impossible to generate the same random matrix when changing the block size. With counter based PRNGs it is possible to change the block size but still compute the same random matrix by using the global index (row * row_size + col) as the counter.

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 86.25954% with 18 lines in your changes missing coverage. Please review.

Project coverage is 71.87%. Comparing base (9484f11) to head (79c58c4).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
...ysds/runtime/util/PhiloxUniformCBPRNGenerator.java 90.58% 4 Missing and 4 partials ⚠️
...he/sysds/runtime/matrix/data/LibMatrixDatagen.java 68.42% 3 Missing and 3 partials ⚠️
...sysds/runtime/util/PhiloxNormalCBPRNGenerator.java 80.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2186      +/-   ##
============================================
+ Coverage     71.84%   71.87%   +0.03%     
- Complexity    44596    44731     +135     
============================================
  Files          1448     1453       +5     
  Lines        168967   169394     +427     
  Branches      32934    33012      +78     
============================================
+ Hits         121393   121758     +365     
- Misses        38244    38299      +55     
- Partials       9330     9337       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@Baunsgaard Baunsgaard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many good things here, please address the minor comments, and describe the performance differences in the PR.

Next steps, is to integrate it into the compiler as options, i would suggest to do it via arguments to random "CB_uniform" could be the argument for instance.

@Baunsgaard
Copy link
Contributor

If you resolve some of the comments, please feel free to mark them as such.

if (valuePRNG instanceof PRNGenerator) {
rngStream = Stream.generate(() -> min + (range * ((PRNGenerator) valuePRNG).nextDouble())).iterator();
} else if (valuePRNG instanceof CounterBasedPRNGenerator) {
rngStream = Arrays.stream(((CounterBasedPRNGenerator)valuePRNG).getDoubles(ctr, blockrows * blockcols)).map(i -> min + (range * i)).iterator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup the stream looks nice, however, since it modifies old code, we need to be careful that the new code returns the same values as the previous.

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

Successfully merging this pull request may close these issues.

3 participants