# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Hook context dataclasses for passing workflow state to hooks."""
from __future__ import annotations
from collections import OrderedDict
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
import torch
from jaxtyping import Bool
from torch.nn import ModuleDict
from torch.optim.lr_scheduler import LRScheduler
if TYPE_CHECKING:
from nvalchemi._typing import ModelOutputs
from nvalchemi.data.batch import Batch
from nvalchemi.models.base import BaseModelMixin
[docs]
@dataclass(kw_only=True)
class HookContext:
"""Common context object passed to hooks.
``HookContext`` contains fields shared by all hook-enabled workflows.
Workflow-specific subclasses add state that is only meaningful in that
domain, such as dynamics step counts or training losses.
Attributes
----------
batch : Batch | None
Current batch being processed. ``None`` is used for lifecycle stages
that run before the first batch is available.
model : BaseModelMixin | None
Model being used (if applicable).
global_rank : int
Distributed rank of this process.
workflow : Any
Back-reference to the engine running the hooks. ``None`` when
the workflow does not inject itself.
"""
batch: Batch | None
model: BaseModelMixin | None = None
global_rank: int = 0
workflow: Any = None
[docs]
@dataclass(kw_only=True)
class DynamicsContext(HookContext):
"""Context object passed to dynamics hooks.
Attributes
----------
step_count : int
Current dynamics step number.
converged_mask : Bool[torch.Tensor, "B"] | None
Boolean mask of samples that converged at the current hook stage.
``None`` when convergence has not fired for this dispatch.
active_graph_mask : Bool[torch.Tensor, "B"] | None
Boolean mask of shape ``(batch.num_graphs,)`` selecting graphs active in
the current dynamics dispatch. ``None`` when the dispatch has no status-based
filtering (typically standalone ``BaseDynamics``). In a ``FusedStage``
substage, it selects graphs whose status matches that substage.
graduated_mask : Bool[torch.Tensor, "B"] | None
Boolean mask of shape ``(batch.num_graphs,)`` selecting the graphs
that graduated during the current step, meaning their status reached
the engine's ``exit_status``. Set only for ``ON_GRADUATE`` dispatches,
where it may be all ``False``; ``None`` at every other stage.
"""
step_count: int = 0
converged_mask: Bool[torch.Tensor, "B"] | None = None # noqa: F722, F821
active_graph_mask: Bool[torch.Tensor, "B"] | None = None # noqa: F722, F821
graduated_mask: Bool[torch.Tensor, "B"] | None = None # noqa: F722, F821
@dataclass(kw_only=True)
class BiasContext(DynamicsContext):
"""Context object passed to an enhanced-sampling bias's ``update``.
Attributes
----------
contribution : ModelOutputs
What this bias returned during the force evaluation that preceded the
capture — the detached mapping, not a recomputation. A metadynamics
bias sizing its next hill from the bias energy it just applied needs
the real value. Empty when the bias has not been evaluated yet.
"""
contribution: ModelOutputs = field(default_factory=OrderedDict)
[docs]
@dataclass(kw_only=True)
class TrainContext(HookContext):
"""Context object passed to training hooks.
Attributes
----------
step_count : int
Current optimizer step number on this worker.
global_step_count : int
Current optimizer step number across all data-parallel workers.
batch_count : int
Number of training batches consumed, including batches whose
optimizer step was skipped by update hooks.
epoch_step_count : int
Number of batches consumed within the current training epoch.
epoch : int
Current training epoch.
loss : torch.Tensor | None
Aggregate loss for the current step.
losses : dict[str, torch.Tensor] | None
Named loss components for the current step.
models : dict[str, BaseModelMixin] | ModuleDict | None
Models participating in the training step; this differs
from the ``model`` attribute which is intended to
represent a 'main' model in multi-model workflows. The
key/model mapping should be semantic, e.g. 'student' and
'teacher' in distillation workflows, with 'student' being
the intended 'main' model.
optimizers : list[torch.optim.Optimizer]
Optimizers participating in the training step. Empty when no
optimizer is attached (e.g. eval-only or manually-driven hook
contexts); ``TrainingUpdateOrchestrator`` and similar consumers
treat an empty list as a no-op.
lr_schedulers : list[torch.optim.lr_scheduler.LRScheduler | None]
Learning rate schedulers participating in the training step.
Aligned positionally with ``optimizers`` when populated; entries
may be ``None`` when an optimizer has no scheduler. Empty when no
scheduler is attached.
gradients : dict[str, torch.Tensor] | None
Parameter gradients for the current step.
grad_scaler : torch.amp.GradScaler | None
AMP gradient scaler for mixed-precision training; ``None`` when
AMP is not in use.
validation : dict[str, Any] | None
Latest validation summary produced by the training strategy's
validation checkpoint (``TrainingStrategy.validate()``).
``None`` until validation has run or after the latest summary is
consumed by metric-driven schedulers. In distributed runs, the reduced
summary is available on every rank.
"""
step_count: int = 0
global_step_count: int = 0
batch_count: int = 0
epoch_step_count: int = 0
epoch: int = 0
loss: torch.Tensor | None = None
losses: dict[str, torch.Tensor] | None = None
models: dict[str, BaseModelMixin] | ModuleDict | None = None
optimizers: list[torch.optim.Optimizer] = field(default_factory=list)
lr_schedulers: list[LRScheduler | None] = field(default_factory=list)
gradients: dict[str, torch.Tensor] | None = None
grad_scaler: torch.amp.GradScaler | None = None
validation: dict[str, Any] | None = None
[docs]
@dataclass(kw_only=True)
class GenerationContext(HookContext):
"""Context object passed to generation hooks.
One context instance spans a single
:meth:`~nvalchemi.gen.generator.AtomisticGenerator.sample` call: the same
object is dispatched at every
:class:`~nvalchemi.gen.stages.GenerationStage` and the
:class:`~nvalchemi.gen.generator.AtomisticGenerator` re-reads it after each
dispatch, so hooks mutate generation state by *replacing* context fields
(``ctx.batch = ctx.batch[keep]``), not by editing in place.
Attributes
----------
batch : Batch | None
The generated batch on the contract path: set when the generating
function returns a :class:`~nvalchemi.data.Batch` (``AFTER_GENERATE``
hooks see it here), ``None`` on the raw path. At call start it holds
``inputs`` when they are a :class:`~nvalchemi.data.Batch` (``None``
otherwise). The raw sample in whatever container the generating
function produced lives on :attr:`sample`, not here.
inputs : Any
The input for the current call: at call start, exactly what was
passed to :meth:`~nvalchemi.gen.generator.AtomisticGenerator.sample`: a
tensor container (``Batch``, ``TensorDict``, ...) with text or other
raw modalities already encoded, or ``None`` for unconditional
generation. When a condition step is provided for the call, the
driver runs it between the ``BEFORE_CONDITION`` and
``AFTER_CONDITION`` dispatches and stores the conditioned value back
here, so from ``AFTER_CONDITION`` on this is what the generating
function is called with; with no condition step it stays the raw
call input.
intermediates : dict[str, Any]
Scratch space for hook-to-hook state within one call (e.g. an
embedding computed at ``AFTER_CONDITION`` and consumed at
``AFTER_GENERATE``).
step_count : int
Which generation call this is within a stream; ``0`` for a one-shot
call. Drives hook frequency gating.
sample : Any
The raw sample for this call, set when the generating function
returns. The driver returns it through the ``Batch`` path when it is
a :class:`~nvalchemi.data.Batch` (and ``AFTER_GENERATE`` hooks see it
as ``ctx.batch`` there), as-is otherwise. This is the hot path: the
sample should be GPU tensors; it may be any structure the function
emits.
accepted_mask : torch.Tensor | None
Boolean acceptance mask written by a hook. The writing hook defines
which rows it refers to; there is no universal alignment with the
original call's candidates. ``DeduplicateHook`` aligns its mask with
the Batch entering that hook and replaces any earlier mask. The
generation driver does not use this field to filter, stop, or
resample. ``None`` until a hook records a mask.
"""
batch: Batch | None = None
inputs: Any = None
intermediates: dict[str, Any] = field(default_factory=dict)
step_count: int = 0
sample: Any = None
accepted_mask: torch.Tensor | None = None