ML Foundations

Spawn reproducible NumPy random streams for workers

Assign child seed sequences to workers instead of resetting every worker to the same seed.

Example results for Spawn reproducible NumPy random streams for workers. spawn replay matches: True
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A repeated seed repeats a stream

Creating a fresh NumPy generator with the same seed for each worker can reproduce the same sequence in every worker. Reproducibility is useful, but identical worker draws may be an unintended sampling pattern. NumPy provides SeedSequence spawning to derive child seed sequences for parallel work. [1, 2]

The existing single-generator seed rule still applies: state advances as values are drawn. This example addresses a separate question, assigning different child streams to named worker positions while retaining a replayable root seed.

References: [1], [2]

Spawn children once and assign them consistently

Run python examples/numpy_spawn_worker_streams.py. A root SeedSequence spawns two children. A generator is created from each child and draws five integers. A fresh root and the same spawn and draw sequence reproduce both lists in this fixture. Resetting two generators directly with the same integer instead produces matching lists. [1, 2]

Assertions check the replay and the observed distinction in these five draws. Different outputs in a tiny fixture do not prove statistical independence or rule out matching samples. NumPy describes spawned streams as independent with very high probability, rather than an unconditional guarantee. [1]

Assign child positions to stable worker identities. If the number of children, task assignment or draw order changes, the same root seed alone does not promise identical per-task results. Store enough configuration to reproduce that mapping.

Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.

import numpy as np

def sample_workers():
    seed = np.random.SeedSequence(2026)
    children = seed.spawn(2)
    generators = [np.random.default_rng(child) for child in children]
    return [rng.integers(0, 1000000, size=5).tolist() for rng in generators]

first = sample_workers()
replay = sample_workers()
duplicate = [np.random.default_rng(2026).integers(0, 1000000, size=5).tolist() for _ in range(2)]
assert first == replay
assert first[0] != first[1]
assert duplicate[0] == duplicate[1]
print("spawn replay matches:", first == replay)
print("child draws differ in fixture:", first[0] != first[1])
print("same-seed draws match:", duplicate[0] == duplicate[1])

Recorded output

spawn replay matches: True
child draws differ in fixture: True
same-seed draws match: True

References: [1], [2]

Keep the replay contract narrow

This fixture runs in one process and models worker assignment rather than starting actual workers. It does not measure throughput, establish collision probabilities, or control randomness in other libraries.

Record the NumPy version, generator configuration, root seed and mapping as appropriate for a replay. Test the actual parallel application as well; scheduling and other sources of nondeterminism can matter beyond these sample streams. [1, 2]

References: [1], [2]

Sources

  1. NumPy parallel random generation
  2. NumPy SeedSequence