|
| 1 | +.. _torchsim-batched: |
| 2 | + |
| 3 | +Batched simulations |
| 4 | +=================== |
| 5 | + |
| 6 | +TorchSim supports batching multiple systems into a single ``SimState`` |
| 7 | +for efficient parallel evaluation on GPU. ``MetatomicModel`` handles |
| 8 | +this transparently. |
| 9 | + |
| 10 | +Creating a batched state |
| 11 | +------------------------ |
| 12 | + |
| 13 | +Pass a list of ASE ``Atoms`` objects to ``atoms_to_state``: |
| 14 | + |
| 15 | +.. code-block:: python |
| 16 | +
|
| 17 | + import ase.build |
| 18 | + import torch_sim as ts |
| 19 | + from metatomic_torchsim import MetatomicModel |
| 20 | +
|
| 21 | + model = MetatomicModel("model.pt", device="cpu") |
| 22 | +
|
| 23 | + atoms_list = [ |
| 24 | + ase.build.bulk("Cu", "fcc", a=3.6, cubic=True), |
| 25 | + ase.build.bulk("Ni", "fcc", a=3.52, cubic=True), |
| 26 | + ase.build.bulk("Al", "fcc", a=4.05, cubic=True), |
| 27 | + ] |
| 28 | +
|
| 29 | + sim_state = ts.io.atoms_to_state(atoms_list, model.device, model.dtype) |
| 30 | +
|
| 31 | +Evaluating the batch |
| 32 | +-------------------- |
| 33 | + |
| 34 | +A single forward call evaluates all systems: |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + results = model(sim_state) |
| 39 | +
|
| 40 | +The output shapes reflect the batch: |
| 41 | + |
| 42 | +- ``results["energy"]`` has shape ``[3]`` (one energy per system) |
| 43 | +- ``results["forces"]`` has shape ``[n_total_atoms, 3]`` (all atoms |
| 44 | + concatenated) |
| 45 | +- ``results["stress"]`` has shape ``[3, 3, 3]`` (one 3x3 tensor per |
| 46 | + system) |
| 47 | + |
| 48 | +How system_idx works |
| 49 | +-------------------- |
| 50 | + |
| 51 | +``SimState`` tracks which atom belongs to which system via the |
| 52 | +``system_idx`` tensor. For three 4-atom systems, ``system_idx`` looks |
| 53 | +like:: |
| 54 | + |
| 55 | + [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2] |
| 56 | + |
| 57 | +``MetatomicModel.forward`` uses this to split the batched positions and |
| 58 | +types into per-system ``System`` objects before calling the underlying |
| 59 | +model. |
| 60 | + |
| 61 | +Batch consistency |
| 62 | +----------------- |
| 63 | + |
| 64 | +Energies computed in a batch match those computed individually. This is |
| 65 | +guaranteed because each system gets its own neighbor list and |
| 66 | +independent evaluation. The existing test |
| 67 | +``test_energy_consistency_single_vs_batch`` validates this property. |
| 68 | + |
| 69 | +Performance considerations |
| 70 | +-------------------------- |
| 71 | + |
| 72 | +Batching is most beneficial on GPU, where the neighbor list computation |
| 73 | +and model forward pass can run in parallel across systems. On CPU, the |
| 74 | +speedup comes from reduced Python overhead (one call instead of N). |
| 75 | + |
| 76 | +For very large systems or many small ones, adjust the batch size to fit |
| 77 | +in GPU memory. TorchSim does not impose a maximum batch size, but each |
| 78 | +system gets its own neighbor list, so memory scales with the sum of |
| 79 | +per-system sizes. |
0 commit comments