xnn.common.deploy.domain#

Spatial domain decomposition: one large structure evaluated on several GPUs.

The box is cut into a grid of sub-domains, one per process. A process evaluates the atoms of its sub-domain (its owned atoms) and copies of the atoms around it (the ghosts), and keeps the energies of its owned atoms only. The ghost shell takes one of two forms:

  • halo="layer": one cutoff of ghosts. Before every layer that reads its neighbors’ features, the model replaces the ghosts’ features by the values their owners computed (refresh_ghosts(), one exchange between neighboring processes per layer). For the models that set refreshes_ghosts (MACE, TensorNet, AIMNet2). The ghosts receive no messages: only the edges into owned atoms are evaluated.

  • halo="field": ghosts out to the model’s receptive field and no exchange inside the model. The ghosts at the outer edge of the shell are evaluated wrongly, but they are too far away to reach an owned atom.

The positions of the ghosts come from their owners through the same exchange, so the derivatives of a process’s energy with respect to its ghosts return to their owners: every process ends with the forces of its own atoms, and only the energy and the virial are summed over the processes.

A plan (the owner of every atom, the ghosts and the neighbor list) is built with a skin: it stays valid while the atoms have moved, and the cell has deformed, less than the skin allows, so a simulation rebuilds it only now and then.

Periodic boxes and molecules are both supported. Along a periodic axis that is not cut, the sub-domains keep the box’s periodicity; along a cut axis, the ghosts are explicit periodic images.

Launch one process per GPU with torchrun. With the whole structure on every process:

import torch, torch.distributed as dist
from xnn.common.deploy import DomainParallel

dist.init_process_group("nccl")
torch.cuda.set_device(dist.get_rank() % torch.cuda.device_count())
dp = DomainParallel(model.cuda().eval(), skin=1.0)
out = dp(graph.to("cuda"))     # every process gets energy, forces and stress

With the positions of its own atoms only on each process (the forces are not gathered):

plan = dp.plan(graph)                          # once, from the whole structure
pos, vel = graph.pos[plan.owned], vel[plan.owned]
for step in range(n_steps):
    if not dp.holds(plan, pos):
        plan, pos, vel = dp.reshard(plan, pos, vel)
    out = dp.evaluate(plan, pos)               # out["forces"]: the owned atoms
    ...                                        # move the owned atoms

Module Attributes

HALO_MARGIN

distance added to the shell and the neighbor list, so that rounding never drops an atom on their edge

Functions

choose_grid(n_domains, widths, halo)

The grid of n_domains sub-domains that evaluates the fewest ghosts.

Classes

DomainParallel(model[, grid, ...])

Evaluate one structure split over the processes of a distributed launch.

DomainPlan(grid, rank, world, owned, ghosts, ...)

The atoms one process evaluates, its exchanges and its neighbor list.