mcframework.backends.TorchCPUBackend#

class mcframework.backends.TorchCPUBackend[source]#

Bases: object

Torch CPU batch execution backend.

Uses PyTorch for vectorized execution on CPU. Requires simulations to implement torch_batch() and set supports_batch to True.

Notes

RNG architecture. Uses explicit torch.Generator objects seeded from numpy.random.SeedSequence.spawn(). This preserves:

  • Deterministic parallel streams

  • Counter-based RNG (Philox) semantics

  • Identical statistical structure across backends

Never uses torch.manual_seed() (global state).

Examples

>>> backend = TorchCPUBackend()
>>> results = backend.run(sim, n_simulations=100000, seed_seq=seed_seq)

Methods

run

Run simulations using Torch CPU batch execution.

run(sim: MonteCarloSimulation, n_simulations: int, seed_seq: np.random.SeedSequence | None, progress_callback: Callable[[int, int], None] | None = None, **_simulation_kwargs: Any) np.ndarray[source]#

Run simulations using Torch CPU batch execution.

Parameters:
simMonteCarloSimulation

The simulation instance to run. Must have supports_batch = True and implement torch_batch().

n_simulationsint

Number of simulation draws to perform.

seed_seqSeedSequence or None

Seed sequence for reproducible random streams.

progress_callbackcallable() or None

Optional callback f(completed, total) for progress reporting.

**_simulation_kwargsAny

Ignored for Torch backend (batch method handles all parameters).

Returns:
np.ndarray

Array of simulation results with shape (n_simulations, ...).

Raises:
ValueError

If the simulation does not support batch execution.

NotImplementedError

If the simulation does not implement torch_batch().

__init__()[source]#

Initialize Torch CPU backend.

Raises:
ImportError

If PyTorch is not installed.

classmethod __new__(*args, **kwargs)#