Skip to content

Faster random numbers - #1103

Open
jpn-- wants to merge 35 commits into
ActivitySim:mainfrom
driftlesslabs:reroll
Open

Faster random numbers#1103
jpn-- wants to merge 35 commits into
ActivitySim:mainfrom
driftlesslabs:reroll

Conversation

@jpn--

@jpn-- jpn-- commented Aug 9, 2026

Copy link
Copy Markdown
Member

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 confidence
  • simple: legacy RandomState implementation and default for backward compatibility

Key changes

  • Adds vectorized uniform, normal, lognormal, choice, Gumbel, and stable-alternative draws.
  • Supports reproducible per-row streams, lazy reseeding, step restarts, and selective offset resets.
  • Preserves existing output shapes and parameter broadcasting behavior.
  • Integrates RNG selection with ActivitySim settings and workflow state.
  • Adds cross-channel contract and pipeline regression coverage for all three modes.
  • Adds a performance benchmark and manually triggered GitHub Actions workflow.
  • Declares cffi as a runtime dependency.
  • Documents configuration choices, compatibility considerations, and performance tradeoffs.
  • Rebases the work onto current main and 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.

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--
jpn-- requested a balanced review from Copilot August 9, 2026 00:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 thread activitysim/core/fast_random/_entropy.py Outdated
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"
)
@jpn--
jpn-- requested review from dhensle and janzill August 9, 2026 03:52
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