Skip to content

New fast channel for reproducible random - #12

Open
jpn-- wants to merge 44 commits into
RSGInc:mainfrom
driftlesslabs:reroll
Open

jpn-- wants to merge 44 commits into
RSGInc:mainfrom
driftlesslabs:reroll

Conversation

@jpn--

@jpn-- jpn-- commented May 2, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces a new activitysim.core.fast_random package. It has one public class, FastChannel, which exactly reproduces (I believe) the entire functionality of activitysim.core.random.SimpleChannel, except for init_row_states_for_step, which is only ever used internally by SimpleChannel.

We also add plumbing to activitysim.core.random.Random and activitysim.core.configuration.top to use the new fast channel.

The FastChannel implementation uses numpy's PCG64 bit generator, and stores the actual 256 bit random state for each index value in the channel. Since we store this state, we do not need to reseed inside a step, nor do we do any state forwarding.

We do still need to reseed for each index value once per ActivitySim step to maintain reproducibility, so the runtime benefits for this change will mostly accrue to models that need to make multiple random draws inside a single ActivitySim step.

Also, all existing tests that are contingent on having a stable random result will fail if you change the random generator. Prior code used the (old and slow) Mersenne Twister via the old numpy.random.RandomState, and the new code uses a different mathematical algorithm, so it cannot be set to give the same stream of random values. Therefore, the default setting is to not use this new feature unless it is explicitly activated.

This PR includes unit tests for FastChannel which have been tested and pass on Linux, Mac and Windows.

jpn-- added 28 commits August 8, 2026 18:10
Introduce a configurable RNG channel type and exercise both implementations in tests. Add Settings.rng_channel_type to choose between the new FastChannel (PCG64 vectorised) and legacy SimpleChannel for reproducibility. Random now accepts a channel_type on init and add_channel accepts fast=None to default to the global channel_type; existing code will pick up settings.rng_channel_type via State initialization and rng access. Implement FastChannel.extend_domain to allow adding new domain rows (initialising per-row PCG64 state when a step is active) and tighten index handling. Update many pipeline tests to parametrize over channel types, isolate per-channel output dirs, and include per-channel expected regression values and checks.
jpn-- and others added 15 commits August 8, 2026 21:41
(cherry picked from commit 4f48b1f)
(cherry picked from commit 7157d6f)
Add EET scaling coverage, workload profiles, reproducibility checks, result artifacts, and CI integration.
Generate stable-universe shocks in chooser batches rather than allocating the full chooser-by-sample-by-alternative array. This prevents large EET models from exhausting memory while retaining only active uniforms or winning positions.

Preserve per-row draw order and validate the entire chooser index before batching. Regression tests enforce allocation bounds and exact results and subsequent streams for both accelerated channels.
Use explicit row counts when reshaping vectorized draws so zero-length dimensions return correctly shaped empty arrays. Return immediately for zero-size choice instead of consuming a full population permutation, and support zero samples in batched Gumbel-max draws.

Add cross-channel empty-draw regressions and low-level tests covering both bit generators, multidimensional empty shapes, and empty row selections. Verify that subsequent draws and generator states remain unchanged.
Reject negative or fractional sample dimensions, non-vector populations, empty populations with nonzero samples, and oversized samples without replacement. Use integer-index validation and a Python dimension product to avoid silent truncation or overflow.

Validate before lazy seeding or draw consumption so rejected calls leave reproducible streams untouched. Add NumPy parity and state-preservation regressions, plus coverage for valid tuple sizes and empty populations with zero samples.
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.

2 participants