nvalchemi.dynamics.BaseDynamics#
- class nvalchemi.dynamics.BaseDynamics(model, hooks=None, convergence_hook=None, n_steps=None, exit_status=1, **kwargs)[source]#
Base class for all dynamics simulations.
This class coordinates a
BaseModelMixinmodel with a numerical integrator to evolve aBatchof atomic systems over time. It manages the step loop, hook execution at stage boundaries, and model evaluation.BaseDynamicsinherits from_CommunicationMixin, which provides inter-rank communication and buffer management for pipeline execution. All dynamics subclasses automatically have communication capabilities.The public interface centers on three methods.
run(batch)is the top-level entry point: it repeatedly callsstep()forn_stepsiterations and is the only method most users need.n_stepscan be set at construction time or passed torun().step(batch)executes a single simulation step, orchestrating the full hook-wrapped sequencepre_update → compute → post_update, with hooks fired at each stage boundary, followed by convergence checking. Subclasses should generally NOT overridestep.compute(batch)performs the model forward pass: it callsmodel(batch)which must return a fully adaptedModelOutputsdict, validates outputs against__needs_keys__, and writes results (forces, energies, stresses) back to the batch in-place. Subclasses should generally NOT overridecompute.- Parameters:
model (BaseModelMixin)
hooks (list[Hook] | None)
convergence_hook (Any)
n_steps (int | None)
exit_status (int)
kwargs (Any)
- model#
The neural network potential model.
- Type:
- step_count#
The current step number, starting from 0.
- Type:
int
- hooks#
Dictionary mapping each stage to a list of registered hooks.
- Type:
dict[HookStageEnum, list[Hook]]
- model_is_conservative#
Indicates that the model uses automatic differentiation to obtain forces.
- Type:
bool
- convergence_hook#
Hook that evaluates composable convergence criteria. Defaults to a single
fmaxcriterion with threshold0.05.- Type:
- n_steps#
Total number of simulation steps for
run().Nonemeans the step count must be supplied when callingrun().- Type:
int | None
- exit_status#
Status code threshold for graduated samples. Samples with
status >= exit_statusare treated as no-ops duringstep()— their positions and velocities are preserved through the integrator. Default is 1.- Type:
int
- __needs_keys__#
Set of output keys that this dynamics requires from the model. Empty by default on
BaseDynamics. Subclasses declare their own requirements (e.g., typically forces for optimization and MD). Checked in_validate_model_outputs()after each forward pass.- Type:
set[str]
- __provides_keys__#
Set of keys that this dynamics produces or updates on the batch beyond model outputs. Empty by default. Subclasses declare what additional state they provide (e.g.,
{"velocities", "positions"}for velocity verlet). Used for validation and buffer preallocation.- Type:
set[str]
Notes
Developers implementing a new integrator should override
pre_update(batch)andpost_update(batch)to implement the integration scheme. These are called aroundcompute()—pre_updatebefore,post_updateafter. For example, Velocity Verlet updates positions inpre_updateand velocities inpost_update. The class-level sets__needs_keys__and__provides_keys__declare what outputs the dynamics requires from the model and what additional state it produces; requirements are checked in_validate_model_outputs()after each forward pass.masked_update(batch, mask)is used byFusedStageto applypre_update/post_updateonly to a subset of samples in a batched setting. Models must beBaseModelMixininstances — plainnn.Moduleis not accepted.Examples
>>> model = MyPotentialModel() >>> dynamics = BaseDynamics(model, n_steps=1000) >>> dynamics.run(batch)
- __init__(model, hooks=None, convergence_hook=None, n_steps=None, exit_status=1, **kwargs)[source]#
Initialize the dynamics engine.
- Parameters:
model (BaseModelMixin) – The neural network potential model.
hooks (list[Hook] | None, optional) – Initial list of hooks to register. Each hook will be organized by its stage attribute.
convergence_hook (ConvergenceHook | dict | None, optional) – Hook that evaluates composable convergence criteria.
n_steps (int | None, optional) – Total number of simulation steps. If provided,
run()will use this value when called without an explicitn_stepsargument. Default isNone. If a dict is provided, it is unpacked asConvergenceHook(**convergence_hook). IfNone, no convergence will be assessed.exit_status (int, optional) – Status code threshold for graduated samples. Samples with
status >= exit_statusare treated as no-ops duringstep()— their positions and velocities are preserved through the integrator. Default is 1. Subclasses likeFusedStagemay compute this dynamically.**kwargs (Any) – Additional keyword arguments forwarded to the next class in the MRO (for cooperative multiple inheritance).
- Return type:
None
Methods
__init__(model[, hooks, convergence_hook, ...])Initialize the dynamics engine.
compute(batch)Perform the model forward pass to compute forces and energies.
masked_update(batch, mask)Apply pre_update and post_update only to selected samples in the batch.
post_update(batch)Perform the second half of the integration step.
pre_update(batch)Perform the first half of the integration step.
refill_check(batch, exit_status)Replace graduated samples via index-select and append.
register_bookkeeping_key(key, init_fn)Register a graph-level bookkeeping field to survive refill_check.
register_hook(hook)Register a hook to be executed at its designated stage(s).
run(batch[, n_steps])Run the dynamics simulation for a specified number of steps.
step(batch)Execute a single dynamics step with the full hook-wrapped sequence.
Attributes
active_batch_has_roomReturn whether the active batch can accept more samples.
active_batch_sizeReturn the number of samples currently in the active batch.
deviceCompute the torch device for this rank.
global_rankGet the global rank for this process.
has_neighborConvenient property to see if rank is isolated
inflight_modeReturn whether inflight batching is enabled.
is_final_stageReturn whether this is the last stage in the pipeline.
is_first_stageReturn whether this is the first stage in the pipeline.
local_rankGet the node-local rank for this process.
Returns whether or not the model uses conservative forces
room_in_active_batchReturn the number of additional samples the active batch can hold.
streamReturn the active CUDA stream, if any.