mcframework.backends.TorchCPUBackend#
- class mcframework.backends.TorchCPUBackend[source]#
Bases:
objectTorch CPU batch execution backend.
Uses PyTorch for vectorized execution on CPU. Requires simulations to implement
torch_batch()and setsupports_batchtoTrue.Notes
RNG architecture. Uses explicit
torch.Generatorobjects seeded fromnumpy.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 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:
- sim
MonteCarloSimulation The simulation instance to run. Must have
supports_batch=Trueand implementtorch_batch().- n_simulations
int Number of simulation draws to perform.
- seed_seq
SeedSequenceorNone Seed sequence for reproducible random streams.
- progress_callback
callable()orNone Optional callback
f(completed, total)for progress reporting.- **_simulation_kwargs
Any Ignored for Torch backend (batch method handles all parameters).
- sim
- Returns:
np.ndarrayArray of simulation results with shape
(n_simulations, ...).
- Raises:
ValueErrorIf the simulation does not support batch execution.
NotImplementedErrorIf the simulation does not implement
torch_batch().
- __init__()[source]#
Initialize Torch CPU backend.
- Raises:
ImportErrorIf PyTorch is not installed.
- classmethod __new__(*args, **kwargs)#