nvalchemi.dynamics.FusedStage#
- class nvalchemi.dynamics.FusedStage(sub_stages, *, entry_status=0, exit_status=-1, compile_step=False, compile_kwargs=None, init_fn=None, **kwargs)[source]#
Composite dynamics engine fusing multiple sub-stages on a single GPU.
FusedStagecomposes multipleBaseDynamicssub-stages to share oneBatchand one model forward pass per step, avoiding redundant forward passes when multiple simulation phases (e.g., relaxation then MD) operate on the same batch.Unlike
BaseDynamics, ``step(batch)`` is overridden. Instead of the standardpre_update → compute → post_updateloop,FusedStageperforms: (1) iterates over sub-stages, applying maskedpre_updateon each sub-stage’s dynamics, (2) a singlecompute()call on the full batch, then (3) iterates over sub-stages, applying maskedpost_update. Each update applies only to samples whosebatch.statusmatches that sub-stage’s status code. Only ONE forward pass happens per step regardless of the number of sub-stages. ``run(batch)`` is also overridden — then_stepsattribute (inherited fromBaseDynamics) and anyn_stepsargument passed torun()are both the maximum number of steps; the loop runs until all samples have migrated to theexit_status, the sampler is exhausted, orn_stepsis reached. Convergence-driven migration is handled byConvergenceHookinstances auto-registered between adjacent sub-stages: when samples converge in sub-stage i, theirbatch.statusis updated to sub-stage i+1’s code, causing them to be processed by the next dynamics on the following step. The+operator composes sub-stages:dyn_a + dyn_bcreates aFusedStage, andfused + dyn_cappends a third sub-stage. The|operator (inherited fromBaseDynamicsvia_CommunicationMixin) creates aDistributedPipelinefor multi-rank execution instead.Developers generally do NOT subclass
FusedStage. Instead, createBaseDynamicssubclasses (integrators) and compose them using+.FusedStagehandles orchestration, including masking, automatically. The batch must have astatustensor.Hook Firing Semantics#
Because
FusedStageshares a single forward pass across all sub-stages, hooks can be registered both on the fused stage and on its sub-stages. All step, compute, and integrator update boundaries fire at both levels. Fused-stage hooks receive the full batch and overall active mask. Sub-stage hooks at step and compute boundaries receive that sub-stage’s status mask. This includes reprime-pending graphs so the shared compute can refresh their forces. Hooks aroundpre_updateandpost_updateinstead exclude those graphs from both updates until the next fused step.The fused stage acts as the outer lifecycle boundary:
At shared
BEFORE_*boundaries, fused-stage hooks fire once before sub-stage hooks fire in sub-stage order.At shared
AFTER_*boundaries, sub-stage hooks fire in sub-stage order before fused-stage hooks fire once.ON_CONVERGEis sub-stage-only because convergence is evaluated and represented independently for each sub-stage.ON_GRADUATEfires after the step-budget migration and theON_CONVERGEdispatch.ctx.graduated_maskmarks the graphs whose status reachedexit_statusat any stage of the step. Sub-stage hooks fire first, each with the mask limited to the graphs its sub-stage owned, and fused-stage hooks fire last with the full mask.
Initial force priming uses the same nested ordering for
BEFORE_COMPUTEandAFTER_COMPUTE. Graphs that change into a sub-stage created withreprime_on_entry=Trueskip one update while the shared model compute and that sub-stage’s compute hooks refresh their forces. The entry sub-stage’s flag also applies to graphs received from a previous pipeline rank.Step count semantics: Each sub-stage’s
step_countis incremented alongside theFusedStage’s ownstep_countafter every fused step, ensuring that hook frequency (e.g.,every_n_steps) is respected correctly across all sub-stages.- param sub_stages:
Ordered
(status_code, dynamics)pairs. Status codes are auto-assigned starting from 0 when using the+operator.- type sub_stages:
list[tuple[int, BaseDynamics]]
- param entry_status:
Status code assigned to incoming samples (default: 0).
- type entry_status:
int
- param exit_status:
Status code that triggers graduation to the next pipeline stage. Auto-set to
len(sub_stages)(one past the last sub-stage code).- type exit_status:
int
- param compile_step:
If
True, replaceself.stepwithtorch.compile(self.step, **compile_kwargs).- type compile_step:
bool
- param compile_kwargs:
Keyword arguments forwarded to
torch.compile.- type compile_kwargs:
dict
- param **kwargs:
Additional keyword arguments forwarded to
BaseDynamics.
- sub_stages#
Ordered
(status_code, dynamics)pairs.- Type:
list[tuple[int, BaseDynamics]]
- entry_status#
Status code for incoming samples.
- Type:
int
- exit_status#
Status code that triggers graduation.
- Type:
int
- compile_step#
Whether the step method is compiled.
- Type:
bool
- compile_kwargs#
Arguments passed to
torch.compile.- Type:
dict
- __needs_keys__#
Union of all sub-stage
__needs_keys__sets. Populated automatically during__init__.- Type:
set[str]
- __provides_keys__#
Union of all sub-stage
__provides_keys__sets. Populated automatically during__init__.- Type:
set[str]
Examples
>>> from nvalchemi.dynamics import FusedStage, BaseDynamics >>> dynamics0 = BaseDynamics(model=model) >>> dynamics1 = BaseDynamics(model=model) >>> fused = FusedStage(sub_stages=[(0, dynamics0), (1, dynamics1)]) >>> fused.exit_status 2
- static all_complete(batch, exit_status)[source]#
Check if all samples have reached the exit status.
- Parameters:
batch (Batch) – The current batch.
exit_status (int) – The status code that indicates completion.
- Returns:
Trueif every sample hasstatus == exit_status.- Return type:
bool
- compile(**kwargs)[source]#
Compile the fused step with
torch.compile.Merges kwargs with any
compile_kwargsstored at construction time (values passed here take precedence), then wraps_step_implwithtorch.compile. Calling this method also setscompile_step = Trueso that thestepdispatch path uses the compiled callable.This method is idempotent in intent but will re-compile if called again (e.g. with different kwargs).
- Parameters:
**kwargs (Any) – Keyword arguments forwarded to
torch.compile. Merged withcompile_kwargsfrom__init__; values here win.- Returns:
This instance, enabling fluent chaining such as
fused.compile(fullgraph=True).run(batch).- Return type:
- refill_check(batch, exit_status)[source]#
Replace graduated samples and clear stale convergence indices.
Delegates to the parent
BaseDynamics.refill_check()to remove graduated graphs and append replacements from the sampler. When the batch composition changes,_last_convergedis cleared on thisFusedStageand all its sub-stages so that subsequent hooks do not receive an invalidconverged_mask. In an already-primed stage, newly added graphs skip integrator updates for one iteration while the regular model evaluation computes their initial forces.
- register_fused_hook(hook)[source]#
Register a hook on the full fused batch.
Deprecated since version 0.3.0: Use
register_hook()instead.FusedStage’s inherited hook registry already observes the complete batch.- Parameters:
hook (Hook) – The hook to register.
- Warns:
DeprecationWarning – Always emitted because this method is deprecated.
- Return type:
None
- register_hook(hook, stage=None)[source]#
Register a hook and warn about sub-stage-only lifecycle stages.
Hooks targeting an unsupported fused-stage boundary remain registered for backward compatibility, but those boundaries are never dispatched by
FusedStage.- Parameters:
hook (Hook) – Hook to register.
stage (Enum | None, optional) – Stage override forwarded to the base hook registry.
- Warns:
UserWarning – If the hook targets a stage dispatched only on sub-stages.
- Return type:
None
- run(batch=None, n_steps=None)[source]#
Run the fused stage until all samples converge or the sampler is exhausted.
Supports two modes of execution:
Mode 1 (external batch loop): When
batchis provided, runs the dynamics untilall_completeor untiln_stepshave been executed (whichever comes first).Mode 2 (inflight batching): When
batch is Noneand a sampler is configured, builds the initial batch from the sampler and replaces graduated samples everyrefill_frequencysteps.Note
In Mode 2,
refill_checkreplaces graduated samples by extracting remaining graphs viaBatch.index_select(), requesting replacements from the sampler, and appending them viaBatch.append(). This produces a newBatchobject; thebatch = resultreassignment in the loop body updates the local reference.Noneis returned when the sampler is exhausted and no active samples remain, which triggers termination.- Parameters:
batch (Batch | None, optional) – The initial batch. If
None, uses the sampler to build one.n_steps (int | None, optional) – Maximum number of steps to run. When
None, falls back toself.n_steps. When both areNone, the loop runs untilall_complete(Mode 1) or sampler exhaustion (Mode 2). Sub-stages that have no exit criterion (e.g. a plain MD stage) will loop forever without a step limit, so always passn_stepswhen such a stage is the final sub-stage. Note: sub-stages withn_stepsset use that value as a per-system step budget for automatic migration to the next stage.
- Returns:
The batch after all steps, or
Noneif the sampler was exhausted and all samples graduated.- Return type:
Batch | None
- Raises:
ValueError – If
batch is Noneand no sampler is configured.
- step(batch)[source]#
Execute one fused step: single forward pass + masked updates.
If
compile_step=Truewas set, this delegates to the compiled step implementation.As in
BaseDynamics, initial force priming causes two compute-hook dispatches atstep_count == 0. Transition repriming instead occurs within the normal shared compute, so hooks are dispatched once andstep_countadvances at the end of that fused iteration. Transitioning graphs skip their dynamics update, so their per-system stage counters do not advance.- Parameters:
batch (Batch) – The batch with a
statusfield.- Returns:
The updated batch, and a 1-D integer tensor of sample indices that newly graduated (reached
exit_status) during this step, orNoneif no samples graduated.- Return type:
tuple[Batch, torch.Tensor | None]
- Parameters:
sub_stages (list[tuple[int, BaseDynamics]])
entry_status (int)
exit_status (int)
compile_step (bool)
compile_kwargs (dict[str, Any] | None)
init_fn (Callable[[Batch], None] | None)
kwargs (Any)