mcframework.backends.TorchCUDABackend#
- class mcframework.backends.TorchCUDABackend[source]#
Bases:
objectTorch CUDA batch execution backend for NVIDIA GPUs.
CUDA backend with adaptive batch sizing, dual RNG modes, and native float64 support. Requires simulations to implement
torch_batch()(orcupy_batch()for cuRAND mode) and setsupports_batch = True.- Parameters:
- device_id
int, default 0 CUDA device index to use. Use
torch.cuda.device_count()to check available devices.- use_curand
bool, defaultFalse Use cuRAND (via CuPy) instead of torch.Generator for RNG. Requires CuPy installation and simulation to implement
cupy_batch().- batch_size
intorNone, defaultNone Fixed batch size for simulation execution. If None, automatically estimates optimal batch size based on available GPU memory.
- use_streams
bool, defaultTrue Use CUDA streams for overlapped execution. Recommended for performance.
- device_id
- Attributes:
See also
is_cuda_available()Check CUDA availability before instantiation.
TorchMPSBackendApple Silicon alternative.
TorchCPUBackendCPU fallback.
Notes
RNG architecture: Uses explicit generators seeded from
numpy.random.SeedSequenceviaspawn(). Never uses global RNG state (torch.manual_seed()orcupy.random.RandomState.seed()).Adaptive batching: When
batch_size=None, performs a probe run with 1000 samples to estimate memory requirements, then calculates optimal batch size to use ~75% of available GPU memory.Native float64: CUDA fully supports float64 tensors. If simulation’s
torch_batch()orcupy_batch()returns float64, the backend uses it directly with zero conversion overhead. If float32, it converts to float64 on GPU before moving to CPU for stats engine compatibility.CUDA streams: When
use_streams=True, executes each batch in a dedicated stream for better GPU utilization and overlapped execution.Examples
>>> # Default configuration (adaptive batching, torch.Generator) >>> if is_cuda_available(): ... backend = TorchCUDABackend(device_id=0) ... results = backend.run(sim, n_simulations=1_000_000, seed_seq=seed_seq) ...
>>> # High-performance configuration (fixed batching, CuPy) >>> if is_cuda_available(): ... backend = TorchCUDABackend( ... device_id=0, ... use_curand=True, ... batch_size=100_000, ... use_streams=True ... ) ... results = backend.run(sim, n_simulations=10_000_000, seed_seq=seed_seq) ...
Methods
Run simulations using Torch CUDA batch execution with adaptive batching.
- 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 CUDA batch execution with adaptive batching.
- Parameters:
- sim
MonteCarloSimulation The simulation instance to run. Must have
supports_batch = Trueand implementtorch_batch()(orcurand_batch()for cuRAND mode).- 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,). Results are float64 regardless of internal tensor dtype.
- Raises:
AttributeErrorIf simulation class is missing ‘supports_batch’ attribute.
ValueErrorIf the simulation does not support batch execution.
NotImplementedErrorIf the simulation does not implement required batch method.
RuntimeErrorIf CUDA out-of-memory error occurs during execution.
Notes
Adaptive batching: When
batch_size=None(default), automatically estimates optimal batch size. Large workloads are split across multiple batches with progress tracking.Memory safety: Monitors GPU memory and adjusts batch size to prevent OOM errors. Uses PyTorch’s caching allocator for efficient memory reuse.
Determinism: With same seed, produces identical results (bitwise for torch.Generator, statistical for cuRAND).
- __init__(device_id: int = 0, use_curand: bool = False, batch_size: int | None = None, use_streams: bool = True)[source]#
Initialize Torch CUDA backend with specified configuration.
- Parameters:
- Raises:
ImportErrorIf PyTorch is not installed, or if CuPy is required but not installed.
RuntimeErrorIf CUDA is not available or device index is invalid.
- classmethod __new__(*args, **kwargs)#