nvalchemi.training.ComposedLossFunction#
- class nvalchemi.training.ComposedLossFunction(components, *, weights=None, normalize_weights=True, dtype_policy=None)[source]#
Weighted sum of
BaseLossFunctioncomponents.This class owns the per-component weighting — leaves are weightless. Weights may be plain floats or
LossWeightScheduleinstances; they are resolved to floats at call time. By default the resolved weights are normalized to sum to1.0so scheduling controls relative contributions while the learning rate controls the absolute loss magnitude. Opt out withnormalize_weights=False.Components live in an
torch.nn.ModuleListfor.modules()/.state_dict()/ nested-__repr__support. When a component is itself aComposedLossFunction, its components and weights are flattened into the parent element-wise so(A + B) + Cis equivalent toA + B + C.- Parameters:
components (Sequence[BaseLossFunction | ComposedLossFunction]) – Loss terms to combine; must contain at least one element.
weights (Sequence[LossWeightSchedule | float | None] | None) – Optional per-component weights. When provided,
weightsmust have the same length ascomponentsat construction time (i.e. top-level components — child weights inside nested compositions are multiplied element-wise by the parent weight during flattening). ANoneentry is shorthand for1.0, soweights=[None, 2.0, None]means “component 1 gets 2x, others default”. Passingweights=Nonedefaults every component to1.0.normalize_weights (bool) – When
True(default), resolved weights are divided by their sum at each call so the effective weights sum to1.0. A zero-sum raisesValueError. WhenFalse, raw weighted sums are returned.dtype_policy (DTypePolicy | None) – Optional composed-level dtype policy applied at call time for components whose own
dtype_policyis still"strict". This avoids mutating reusable leaf instances while allowing one composed loss to opt into automatic dtype alignment.
- components#
torch.nn.ModuleListof the flattened leaf components.
- normalize_weights#
Whether effective weights are renormalized to sum to
1.0.
- dtype_policy#
Composed-level dtype alignment policy, or
Nonewhen each leaf controls dtype handling independently.
- current_weight(step=0, epoch=None)[source]#
Resolve each component’s weight to a float for
(step, epoch).When
normalize_weightsisTruethe returned list sums to1.0; otherwise it is the raw resolved weights. With normalization enabled the raw sum must be a strictly positive float orValueErroris raised.- Parameters:
step (int) – Current global training step.
epoch (int | None) – Current training epoch, or
Nonewhen unused.
- Returns:
One effective weight per component, in order.
- Return type:
list[float]
- Raises:
ValueError – If normalization is enabled and the raw weights do not sum to a strictly positive, finite float.
- property dtype_policy: Literal['strict', 'prediction_to_target', 'target_to_prediction'] | None#
Composed-level dtype policy applied to strict leaves at call time.
- extra_repr()[source]#
Expose component count and normalization alongside the default repr.
- Return type:
str
- forward(predictions, targets, *, step=0, epoch=None, **kwargs)[source]#
Return the weighted total loss and per-component diagnostics.
Each component is called with the routed
pred/targettensors, then its raw loss is scaled by the effective weight for this step. The output’sper_component_unweightedcontains each raw component loss before effective weighting;per_component_weightholds the scalar weights that were applied (after normalization, if enabled);per_component_raw_weightholds the pre-normalization resolved weights so schedule ramps remain observable on single-component normalized compositions; seeBaseLossFunction.per_sample_lossfor theper_component_samplecontract.- Parameters:
- Return type:
- requires_eval_grad()[source]#
Whether evaluating this loss needs autograd enabled.
Inspects each leaf component’s
requires_eval_gradflag. A component reportingTrue(e.g. a force/stress loss that differentiates the energy) forces gradient-enabled evaluation; components reportingFalsedo not. A component reportingNoneis undeclared and cannot be inferred automatically.- Returns:
Truewhen at least one component requires gradients,Falsewhen every component explicitly declares it does not.- Return type:
bool
- Raises:
ValueError – When one or more components report
requires_eval_grad=Noneand none require gradients, so the requirement is ambiguous.