Run on a GPU¶
pySRX does its heavy work in PyTorch on CUDA: building PSFs and operators, Richardson-Lucy, MAP and fitting the neural field. A few metrics also use CuPy, automatically. The full list is in GPU acceleration.
Which hardware¶
Any NVIDIA GPU that runs CUDA 12 with PyTorch:
| Class | Examples |
|---|---|
| Workstation | RTX A4000 to A6000, RTX 4000 to 6000 Ada, Quadro RTX 4000 to 8000 |
| Consumer | GeForce RTX 20, 30 and 40 series |
| Data centre | V100, T4, A100, A40, L4, L40S, H100, H200 |
This guide's timings are on a Quadro RTX 4000 (8 GB). The neural field is the
step that needs memory; larger volumes fit by working in chunks
(below). Without a GPU everything runs on the CPU
(device="cpu"), more slowly; CI runs on the CPU.
1. Use the CUDA environment¶
pixi run -e cuda python -c "import torch; print(torch.cuda.is_available(), torch.cuda.get_device_name(0))"
# True Quadro RTX 4000
Run scripts the same way (pixi run -e cuda python my_recon.py). The default
environment has the CPU build of PyTorch, so there torch.cuda.is_available()
is False.
2. Choose the device once¶
Functions that create tensors take device=; everything after that follows the
operator or the PSF it is given, so the choice is made once:
import torch
dev = "cuda" if torch.cuda.is_available() else "cpu"
pattern = estimate_pattern(raw, lat, cutoff=2 * 1.4 / 0.525 * px_um, device=dev)
A = SimOperator(pattern, lat, optics, raw.shape[1:], px_um, dz_um, device=dev)
y = torch.as_tensor(raw, device=dev)
rl = rl_op(y, A, iterations=30, accelerate=True) # runs where A is
nerf = fit_field_op(y, A, spacing_um=(dz_um / 2, px_um / 2, px_um / 2), x_init=rl.volume)
Results come back as NumPy arrays on the host, whatever the device.
3. How much faster¶
The same lattice SIM reconstruction on a small crop of the Elyra data (13 phases, 8 planes, 128 x 128 camera pixels; object grid 15 x 256 x 256), run with this script:
| Step | Quadro RTX 4000 | CPU | GPU memory |
|---|---|---|---|
| Pattern fit and operator | 2.0 s | 1.0 s | |
| Richardson-Lucy, 30 iterations | 1.5 s | 13.6 s | 0.14 GB |
| NeRF, 100 distillation + 200 fitting steps | 73 s | 2976 s (50 min) | 1.82 GB |
result.seconds and result.peak_vram_gb give the same numbers for your own
runs.
4. Fit in GPU memory¶
A full Elyra stack does not fit in 8 GB at once. The memory knobs:
| Knob | Where | Effect |
|---|---|---|
InrConfig(plane_chunk=...) |
the neural field | planes rendered per chunk: smaller uses less memory, runs a little slower |
InrConfig(z_chunk=...), richardson_lucy(z_chunk=...) |
light-field operator | planes per FFT chunk |
InrConfig(log2_table=...) |
the neural field | hash-table size per level, \(2^{\text{log2\_table}}\) entries (default 21) |
| crop the input | your script | the operator's memory scales with the volume |
Other programs share the GPU: check with nvidia-smi before a long run. After
an out-of-memory error, free the cache with torch.cuda.empty_cache() and
retry with smaller chunks. A run that does not fit at all can always fall back
to device="cpu".
5. Metrics on the GPU¶
decorrelation_resolution, axial_fwhm and the rolling FRC maps use CuPy when
it is installed and a CUDA GPU is present, with no change to the code. Check and
free it with pysrx.gpu:
from pysrx import gpu
gpu.GPU # True when CuPy found a CUDA device
gpu.clear() # free the CuPy device cache
The benchmark¶
import time
cfg = InrConfig(steps=200, distill_steps=100, log2_table=18, plane_chunk=4)
raw = raw[:, 12:20, 64:192, 64:192] # 13 phases, 8 planes, 128 x 128
t = time.time()
pattern = estimate_pattern(raw, lat, cutoff=2 * 1.4 / 0.525 * px_um, device=dev)
A = SimOperator(pattern, lat, optics, raw.shape[1:], px_um, dz_um, device=dev)
print("setup", time.time() - t)
y = torch.as_tensor(raw, device=dev)
r = rl_op(y, A, iterations=30, accelerate=True)
f = fit_field_op(y, A, spacing_um=(dz_um / 2, px_um / 2, px_um / 2), x_init=r.volume, cfg=cfg)
print(r.seconds, r.peak_vram_gb, f.seconds + f.distill_seconds, f.peak_vram_gb)