Faster random numbers - #1103
Open
jpn-- wants to merge 35 commits into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds configurable vectorized RNG channels while retaining legacy reproducibility.
Changes:
- Implements PCG64 and SFC64 per-row random streams.
- Integrates RNG selection into workflow settings.
- Adds regression tests, benchmarks, and performance automation.
Reviewed changes
Copilot reviewed 17 out of 19 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
uv.lock |
Locks the CFFI dependency. |
pyproject.toml |
Declares CFFI at runtime. |
other_resources/scripts/random-performance.ipynb |
Explores RNG performance. |
other_resources/performance-checks/fast-channel-random.py |
Adds a benchmark script. |
activitysim/core/workflow/state.py |
Configures RNG channel selection. |
activitysim/core/test/test_random.py |
Expands cross-channel contract tests. |
activitysim/core/test/test_fast_random.py |
Tests vectorized generators. |
activitysim/core/test/test_fast_channel.py |
Tests FastChannel. |
activitysim/core/random.py |
Integrates fast channels into the RNG API. |
activitysim/core/fast_random/_fast_channel.py |
Implements vectorized per-row streams. |
activitysim/core/fast_random/_entropy.py |
Implements accelerated reseeding. |
activitysim/core/fast_random/__init__.py |
Exports FastChannel. |
activitysim/core/configuration/top.py |
Documents RNG settings. |
activitysim/abm/test/test_pipeline/test_pipeline.py |
Adds pipeline regression coverage. |
activitysim/abm/test/test_pipeline/output/trace/.gitignore |
Removes redundant ignores. |
activitysim/abm/test/test_pipeline/output/cache/.gitignore |
Removes redundant ignores. |
activitysim/abm/test/test_pipeline/output/.gitignore |
Removes redundant ignores. |
.github/workflows/performance-checks.yml |
Adds manual benchmark automation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+386
to
+406
| scalar_output = size is None | ||
| draw_shape = 1 if scalar_output else size | ||
|
|
||
| result = self._fast_generator.vector_random_standard_normal( | ||
| self._state_array, selected_positions=selected_positions, shape=draw_shape | ||
| ) | ||
|
|
||
| def broadcast_parameter(value, name): | ||
| """Align one scalar or one value per row to the generated draw shape.""" | ||
| value = np.asarray(value) | ||
| if value.ndim == 0: | ||
| return value | ||
| if value.shape != (len(df),): | ||
| raise ValueError( | ||
| f"{name} must be a scalar or a 1-D array with one value per row" | ||
| ) | ||
| return value.reshape((len(df),) + (1,) * (result.ndim - 1)) | ||
|
|
||
| result = result * broadcast_parameter(sigma, "sigma") + broadcast_parameter( | ||
| mu, "mu" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds high-performance, vectorized random-number channels while preserving ActivitySim’s legacy RNG behavior as an option.
Two accelerated modes are available through
rng_channel_type:fast: PCG64 with robust entropy generation. Better than simple for large models but still following rigorous "safe" randomness algorithms)faster: SFC64 with lower-overhead hash-based reseeding. Fastest overall for nearly all purposes, but employs short cuts on seeding that are probably fine for large scale simulation, but not rigorously validated as fully uncorrelated random streams to the highest possible levels of confidencesimple: legacyRandomStateimplementation and default for backward compatibilityKey changes
cffias a runtime dependency.mainand removes unrelated PR scope.Note: this PR has advanced notably from the last time we looked at it, as the EET branch introduced several new variants of randomness. I have iterated this on a couple different AI models to get what I believe to be a good result.