stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.58k stars 368 forks source link

Ensure mixmax rng is not initialized with all zeros #3287

Closed WardBrian closed 4 months ago

WardBrian commented 4 months ago

Submission Checklist

Summary

Prevents the poor behavior seen in #3285.

The boost::mixmax rng is actually initialized with 128 bits of state. The overload we were previously calling was synonymous with rng(0,0, 0, seed+chain). This PR changes to rng(0,1,seed,chain). In addition to ensuring that the full 128 bits are never all zero, it also makes adjacent seeds more independent; previously seed=1 chain=1, seed=2 chain=0, and seed=0 chain=2 would all yield the same seeding.

I am still awaiting on confirmation from the boost devs that this configuration of the RNG preserves the desirable property that chain and chain+1 yield independent streams, hence the draft status.

Intended Effect

Closes #3285

How to Verify

See the updated godbolt which shows that the single 1 prevents the deterministic behavior

Side Effects

Seeds will be different from 2.35RC1. This also required updating the tests which are seed-dependent.

Documentation

None

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

Simons Foundation

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

WardBrian commented 4 months ago

The original reference implementation from which boost's is derived makes it clear that this is a valid configuration:

void seed_uniquestream(rng_state_t* X, myID_t clusterID, myID_t machineID, myID_t runID, myID_t  streamID );
/*
 best choice: will make a state vector from which you can get at least 10^100 numbers 
 guaranteed mathematically to be non-colliding with any other stream prepared from another set of 32bit IDs,
 so long as it is different by at least one bit in at least one of the four IDs
            -- useful if you are running a parallel simulation with many clusters, many CPUs each
 */
WardBrian commented 4 months ago

@andrjohns @nhuurre mind reviewing?

Downstream failures addressed by https://github.com/stan-dev/cmdstan/pull/1274 (tolerance too tight) and by re-running the gold tests after merge (expected for the model that fails)