You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 978a857
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/user/reproducibility.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,11 +41,11 @@ One of the main reasons for seeding the global states is to control the determin
41
41
42
42
## Seeding TorchSim SimStates
43
43
44
-
In PyTorch, `torch.manual_seed()` only seeds the default global generator, i.e. the generator used in `torch.rand(..., generator=None)` if the generator isn't specified. An explicit `torch.Generator()`is independent and is initialized at the time of calling from the OS entropy source. Since TorchSim internally uses a`torch.Generator()`in the form of `SimState.rng` for all algorithmic randomness in core code setting the global seed is insufficient to ensure determinism within TorchSim. **You must explicitly seed your SimStates to ensure determinism**.
44
+
In PyTorch, `torch.manual_seed()` only seeds the default global generator, i.e. the generator used in `torch.rand(..., generator=None)` if the generator isn't specified. TorchSim internally uses a `torch.Generator()`in the form of `SimState.rng` for all algorithmic randomness in core code. An unseeded state seeds it from the global generator when `rng` is first used. After`torch.manual_seed()`a fixed script is therefore reproducible, but its random streams shift whenever other code draws from the global generator first, for example a model initializing its weights. **You must explicitly seed your SimStates to ensure determinism**.
sim_state.rng =42#needed for reproducibility: torch.manual_seed() has no effect
48
+
sim_state.rng =42#fixes the stream, whatever else uses the global generator
49
49
```
50
50
51
51
### Deterministic vs stochastic integrators in TorchSim
@@ -54,15 +54,17 @@ sim_state.rng = 42 # needed for reproducibility: torch.manual_seed() has no eff
54
54
terms by design. When seeded via `state.rng`, they produce identical trajectories.
55
55
The `rng` generator controls **both** the initial momenta sampling **and** all per-step stochastic noise (Langevin OU noise, V-Rescale draws, C-Rescale barostat noise, etc.). It is stored on the state and automatically advances on every step, so running the same seed twice produces identical trajectories.
56
56
-`ts.Integrator.nvt_nose_hoover` and `ts.Integrator.nve` are deterministic at the
57
-
algorithmic level and require no seeding.
57
+
algorithmic level, but they still draw their starting momenta from `state.rng`.
58
58
59
-
For the simplest path to reproducibility, use a deterministic integrator such as Nosé-Hoover:
59
+
For the simplest path to reproducibility, seed the state and use a deterministic integrator such as Nosé-Hoover:
60
60
61
61
```python
62
62
import torch_sim as ts
63
63
64
+
state = ts.initialize_state(atoms, device, dtype)
65
+
state.rng =42
64
66
state = ts.integrate(
65
-
system=atoms,
67
+
system=state,
66
68
model=model,
67
69
n_steps=500,
68
70
timestep=0.001,
@@ -77,6 +79,8 @@ In practice, exact reproducibility also depends on hardware, driver/library vers
77
79
78
80
Because TorchSim runs batched simulations, all systems in a batch share a single `torch.Generator`. Random numbers are drawn in a fixed order each step, so **identical batch composition** is required for exact reproducibility. Changing which systems are in a batch (or their order) will consume random numbers differently and cause trajectories to diverge.
79
81
82
+
A `BinningAutoBatcher` gives each batch after the first its own seed drawn from the state's generator, so replicas in different batches get independent noise.
83
+
80
84
If strict reproducibility is required, keep your batching setup fixed.
0 commit comments