Skip to content
Edit this page View source of this page

Training with torch.optim

Optora's ambiguity sets are nn.Module subclasses whose worst_case_expectation(loss) is an ordinary differentiable function of loss. Robust training therefore needs no dedicated driver. We pass the per-scenario loss to the ambiguity set, call .backward() on the result, and step any torch.optim optimizer.

import torch
from torch import nn

from optora import GradientDescent, KLAmbiguitySet

model = nn.Linear(num_features, 1)
nominal = torch.full((num_scenarios,), 1.0 / num_scenarios)

ambiguity_set = KLAmbiguitySet(nominal=nominal, radius=0.1)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2)

for _ in range(num_steps):
    optimizer.zero_grad()
    per_scenario_loss = (model(features).squeeze(-1) - targets) ** 2
    value = ambiguity_set.worst_case_expectation(per_scenario_loss)
    value.backward()
    optimizer.step()

loss is per scenario, not reduced

worst_case_expectation expects one loss entry per support point of nominal, with shape (..., n), and performs the reduction itself. It returns the expectation under the worst-case distribution in the ambiguity set instead of the mean under the nominal one. Do not call .mean() before passing the loss in, because that would discard the per-scenario structure the ambiguity set acts on.

Why this is a correct gradient

The inner supremum is not differentiated through. Each ambiguity set locates the maximizer \(q^\star\) in a detached inner solve and then reports \(\sum_i q^\star_i \ell_i\) with the still-attached loss. By Danskin's theorem this already gives the exact gradient. The optimal value is the support function of the ambiguity set evaluated at the loss, so

\[ \nabla_\theta\, V(\theta) = \sum_{i} q^\star_i \, \nabla_\theta\, \ell_i(\theta), \qquad q^\star = \arg\max_{q \in \mathcal{Q}} \; \mathbb{E}_q[\ell(\theta)], \]

with \(q^\star\) held fixed. The derivative of \(q^\star\) with respect to \(\theta\) contributes nothing, because \(q^\star\) already maximizes the inner problem. The identity has the following practical consequences.

  • Independent of the solver path


    The gradient depends only on the point where the inner solve ended, not on the iterates that led there.

  • Constant memory


    Inner iterations are never retained for the backward pass, so the inner solve adds run time but no autograd memory.

  • Accuracy follows the inner solve


    The identity holds at the maximizer. KLAmbiguitySet bisects to the precision of the dtype on every call, and so does PhiAmbiguitySet without a dual_solver. ChiSquareAmbiguitySet solves for its maximizer exactly. With a dual_solver and a loose tol, the inexact maximizer biases the outer gradient, so tighten the tolerance before suspecting the outer optimizer.

Reading off the worst-case distribution

Because \(\partial V / \partial \ell_i = q^\star_i\), the worst-case distribution is a by-product of autograd, and no separate API is needed.

loss = (model(features).squeeze(-1) - targets) ** 2
loss.retain_grad()
ambiguity_set.worst_case_expectation(loss).backward()
worst_case_distribution = loss.grad

This by-product also shows what a robust training run does. In the animation below, Adam fits a linear model to data drawn from the same two-group model as in Training under subpopulation shift, once by empirical risk minimization and once on the KL worst case.

Weight paths, exact group risks and the adversary's minority mass along ERM and KL-DRO training Weight paths, exact group risks and the adversary's minority mass along ERM and KL-DRO training
The left panel shows the weight path of each model between the majority and minority slopes. The middle panel shows the exact risk on each group at every step. The right panel shows the total worst-case mass that the KL adversary places on minority samples, read from loss.grad, against their nominal share.

Second derivatives

Hessians and Hessian-vector products of the worst case with respect to loss are exact as well. They are available through create_graph=True, torch.autograd.functional.hessian and torch.func.hessian.

The worst case has nonzero curvature in loss, so a detached \(q^\star\) alone would report a zero Hessian. A recorded backward pass therefore rebuilds \(q^\star\) from the solved tilt by one implicit-function step, whose Jacobian is exactly \(\partial q^\star / \partial \ell\). The same step, taken in nominal and radius, gives the mixed derivatives. A plain .backward() does not compute this step.

The worst case has nonzero curvature in loss. A zero-valued term, built from the implicit function theorem applied to the optimality conditions on the support of the maximizer, supplies the Jacobian \(\partial q^\star / \partial \ell\). The Hessian is therefore exact once the interior-point solve has converged. The term is built only when loss requires grad or the call runs inside a torch.func transform.

The worst case is piecewise linear in loss, so its Hessian is zero wherever it exists, and autograd reports exactly that.

Without a dual_solver, PhiAmbiguitySet is a tilted set like KL and chi-square and has the same exact implicit-function Hessian. Its inner normalizing level gets its derivative from one Newton step of its own. With a dual_solver, it re-evaluates its dual objective \(F\) at the detached optimum \(x^\star\), which alone would miss how \(x^\star\) moves with the loss. It therefore adds a term whose value and first derivative are zero but whose Hessian is the implicit-function correction \(-F_{\ell x} F_{xx}^{-1} F_{x \ell}\), built from the Hessian of the dual at the optimum. Second derivatives are then exact once the dual solve has converged, including the mixed derivatives with nominal and radius. The term needs a few passes over the dual objective per call, compared with the many passes of the solve, and it is skipped when nothing requires grad.

torch.func transforms also work. Inside one, the inner GradientDescent differentiates the dual with torch.func rather than torch.autograd. Under torch.func.vmap a batched convergence flag has no single value to read, so every mapped element runs the whole max_iter budget of the dual solver. A modest budget is advisable there.

Unsupported second-order paths

  • torch.func.jacfwd(torch.func.jacfwd(...)) is not supported on KLAmbiguitySet, ChiSquareAmbiguitySet, SinkhornAmbiguitySet, or PhiAmbiguitySet without a dual_solver. PyTorch does not differentiate a custom forward-mode rule a second time, so the result is silently wrong rather than an error. Use torch.func.hessian (forward over reverse) or reverse over reverse instead.

Non-differentiable points

The worst case \(V(\ell) = \sup_{q \in \mathcal{U}} \langle q, \ell \rangle\) is convex in \(\ell\) and differentiable exactly where the maximizer is unique. Where several distributions attain the supremum, at a kink, autograd returns one of them. This is always a subgradient, namely a feasible distribution \(g\) with \(\langle g, \ell \rangle = V(\ell)\), so

\[ V(\ell + d) \ge V(\ell) + \langle g, d \rangle \quad \text{for every } d, \qquad -V'(\ell; -d) \le \langle g, d \rangle \le V'(\ell; d), \]

where \(V'(\ell; d)\) is the one-sided directional derivative. A subgradient method accepts any subgradient, so a torch.optim step stays well defined at a kink. Which subgradient is returned is fixed for each set and is the same on every device.

Set Kinks in loss Gradient returned at a kink
KLAmbiguitySet Tied highest losses once the radius saturates the set (the set then contains the distribution concentrated on them) The tied mass split in proportion to nominal
ChiSquareAmbiguitySet As for KL. The worst case is also not twice differentiable where a scenario enters or leaves the support of \(q^\star\), although the gradient stays continuous there As for KL
TotalVariationAmbiguitySet Ties among the highest losses, and ties among the lowest-loss scenarios that mass is drained from Ties are broken by support index. Tied low-loss scenarios are drained in index order, and all reallocated mass goes to the last tied maximum
CVaRAmbiguitySet Tied losses that straddle the boundary of the upper \(\alpha\)-tail The tail is filled in support-index order among the tied scenarios
SinkhornAmbiguitySet As for KL, with ties among the highest losses on the support of the reference measure The tied mass of each kernel row is split in proportion to that row
MMDAmbiguitySet Tied highest losses once the radius saturates the set. The worst case is also not twice differentiable where a scenario enters or leaves the support of \(q^\star\), although the gradient stays continuous there The maximizer of the interior-point method, which splits tied mass according to the central path
WassersteinAmbiguitySet Ties in the best response \(\ell_j - \gamma^\star c_{ij}\) of a support point, including coincident support points with equal losses The convex combination of the two extreme transport plans that spends the radius exactly

tests/dro/test_gradients.py checks each of these kinks by one-sided finite differences, and checks smooth points with gradcheck and gradgradcheck in float64.

Kinks at the minimizer

A kink is harmless while the iterates pass through it, but it can also sit at the minimizer of the robust risk \(R(\theta) = V(\ell(\theta))\). Minimizing a worst case pushes the highest losses toward each other, and once the radius saturates the set on those tied losses, \(R\) is not differentiable at its minimizer. For a uniform nominal on \(n\) points, the chi-square ball of radius \(\rho\) contains the uniform distribution on \(k\) tied highest losses as soon as

\[ \rho \ge \frac{n}{k} - 1, \]

so a large radius relative to the sample size reaches this regime with only a handful of ties. There the negative subgradient that autograd returns need not be a descent direction, and a gradient method with a backtracking (Armijo) line search shrinks its step until it makes no further progress, short of the minimizer. A first-order outer solver therefore needs a method for nonsmooth problems, such as a projected subgradient method with a diminishing step size and averaged iterates. With torch.optim, this is SGD under a learning-rate schedule that decays like \(1/t\) in the iteration count \(t\), with the parameters averaged over the iterations.

The Duchi and Namkoong reproduction is a worked case. At \(n = 100\) its chi-square ball has radius about 12 and holds every distribution on the roughly 8 tied highest losses at the minimizer. Projected gradient descent with an Armijo line search stalls at a robust to ERM risk ratio of 0.52, while a projected subgradient method with averaged iterates reaches 0.0055, the exact minimizer that an independent conic solver (Clarabel) confirms. The full report gives the solver settings.

Gradients with respect to nominal and radius

nominal and a tensor radius (or a tensor alpha for CVaRAmbiguitySet) are differentiable as well. This allows, for example, learning a reweighting of the training data through nominal = torch.softmax(logits, dim=-1), or tuning the radius. The inner solve again stays out of the graph. By the envelope theorem, the derivatives are read off the Lagrangian

\[ L(q, \lambda; \ell, p, \rho) = \langle q, \ell \rangle - \lambda \big( D(q \,\|\, p) - \rho \big) \]

at the saddle point \((q^\star, \lambda^\star)\) that the set has already solved for, which gives

\[ \frac{\partial V}{\partial \rho} = \lambda^\star \ge 0, \qquad \frac{\partial V}{\partial p} = \frac{\partial L}{\partial p} \Big|_{(q^\star, \lambda^\star)}. \]

The multiplier \(\lambda^\star\) is the shadow price of the radius constraint, namely the rate at which the worst case increases per unit of radius. It does not increase as the radius grows, and it vanishes once the radius saturates the set. For KL, chi-square and Sinkhorn it diverges as the radius tends to zero on a loss that is not constant, because the worst case grows like \(\sqrt{\rho}\) there.

Only directions that keep nominal a distribution are defined

The worst case is defined only for distributions, so its derivative with respect to nominal is defined only along directions \(d\) with \(\sum_i d_i = 0\). The component along the all-ones direction is implementation-defined. A softmax, or any other map onto the simplex, discards it.

The worst case is concave in \(p\) and in \(\rho\) (for CVaR, in the likelihood-ratio bound \(1/\alpha\) rather than in \(\rho\)). At a kink, such as a total-variation radius that drains a scenario exactly, the returned derivative is therefore a supergradient, namely one of the one-sided derivatives.

Second derivatives that involve nominal and radius, including the mixed derivatives with loss, are exact on the curved sets. KLAmbiguitySet, ChiSquareAmbiguitySet and SinkhornAmbiguitySet obtain them from the implicit-function step that gives their loss Hessian. That step also takes nominal and radius as inputs, so a recorded backward pass rebuilds the maximizer and both envelope derivatives with their exact Jacobians. PhiAmbiguitySet obtains them in the same way without a dual_solver. With one, it obtains them from its dual objective, and they are exact once that solve has converged. On the total-variation, CVaR, Wasserstein and MMD sets, the derivatives with respect to nominal and radius are first order only, and second derivatives that involve them are not supported. tests/dro/test_parameter_gradients.py checks every set against gradcheck and against finite differences of independent NumPy duals.

Minibatches from a population

The pattern above treats nominal as the whole training distribution. When the data instead arrive as a stream of i.i.d. minibatches from an unknown population \(P_0\), running an ambiguity set on the uniform empirical distribution \(\hat P_m\) of each minibatch gives a biased estimate of the population worst case. The supremum is nonlinear in the sample, so

\[ \mathbb{E}\Big[\sup_{D(q \,\|\, \hat P_m) \le \rho} \mathbb{E}_q[\ell]\Big] \neq \sup_{D(q \,\|\, P_0) \le \rho} \mathbb{E}_q[\ell], \]

and the gradient of this plug-in estimate is not an unbiased stochastic gradient (for KL, the plug-in is the log-sum-exp of a subsample). optora.dro.minibatch provides the two standard remedies.

MultilevelMonteCarloEstimator keeps the exact solve of any ambiguity set built from a nominal alone. It reduces the bias to that of a single solve on \(n_0 2^L\) samples, at an expected batch size of \(n_0 L / (1 - 2^{-L})\) (Blanchet and Glynn 2015; Levy et al. 2020). The batch size is drawn at every step. The factory may also return the penalized objective PenalizedChiSquareDRO, for example functools.partial(PenalizedChiSquareDRO, penalty=0.5), which is the other chi-square objective Levy et al. estimate this way.

import functools

from optora import KLAmbiguitySet, MultilevelMonteCarloEstimator

estimator = MultilevelMonteCarloEstimator(
    functools.partial(KLAmbiguitySet, radius=0.1),
    base_batch_size=32,
    max_level=4,
)

for _ in range(num_steps):
    optimizer.zero_grad()
    features, targets = sample(estimator.sample_batch_size())
    per_sample_loss = (model(features).squeeze(-1) - targets) ** 2
    estimator(per_sample_loss).backward()
    optimizer.step()

StochasticPhiDual keeps the phi-divergence dual variables \((\log \eta, \lambda)\) as learnable parameters. For fixed dual variables the dual is an expectation over \(P_0\), so its minibatch mean is an unbiased estimate at any fixed batch size. The dual variables are trained together with the model (Levy et al. 2020; Jin et al. 2021).

import torch

from optora import StochasticPhiDual
from optora.dro import kl_conjugate

dual = StochasticPhiDual(radius=0.1, phi_conjugate=kl_conjugate)
optimizer = torch.optim.Adam([*model.parameters(), *dual.parameters()])

for features, targets in loader:
    optimizer.zero_grad()
    per_sample_loss = (model(features).squeeze(-1) - targets) ** 2
    dual(per_sample_loss).backward()
    optimizer.step()

optora.dro.kl_conjugate is the KL conjugate \(e^s - 1\), continued along its tangent beyond \(s = c\), where \(c\) is a quarter of the logarithm of the largest finite value of the dtype. When \(\lambda\) lags behind a loss spike, this gives a large but finite gradient instead of the inf of torch.expm1, which would turn the dual variables into nan. Use optora.dro.chi_square_conjugate for the chi-square ball. For CVaR at level \(\alpha\), pass radius=0.0 and functools.partial(optora.dro.cvar_conjugate, alpha=alpha). The dual then reduces to the Rockafellar-Uryasev objective \(\lambda + \mathbb{E}[(\ell - \lambda)_+] / \alpha\) (Rockafellar and Uryasev 2000), and \(\eta\) drops out. Start the dual variables on the scale of the loss, for example at the exact dual optimum of a pilot minibatch. With the exponential conjugate of KL, the default start can still produce gradients large enough to stall Adam.

MultilevelMonteCarloEstimator StochasticPhiDual
Unbiased for The plug-in worst case at \(n_0 2^L\) samples The population dual at the current \((\eta, \lambda)\)
Batch size Random, \(n_0 2^J\) Fixed, any
Ambiguity sets Any set built from a nominal alone (KL, chi-square, total variation, CVaR, general phi) Phi-divergence balls with a finite conjugate (KL, chi-square, CVaR)
Per-step cost Four exact solves, of which the two half-batch solves run as one batched call One pass over the minibatch, no inner solve

Minibatch DRO against the full batch measures the bias of the plug-in estimate, trains with all three estimators and compares each with the exact full-batch optimum.

Launch-bound minibatch steps

On a GPU, a minibatch step of a small model is bound by kernel launches rather than by arithmetic. The exact solve of ChiSquareAmbiguitySet, for example, is about a hundred small kernels in the forward and backward pass, whatever the batch size. None of them reads a value back to the host, so the forward and backward pass can be captured once in a CUDA graph and replayed at the cost of a single launch (PyTorch CUDA semantics notes, "CUDA Graphs").

import torch

from optora import ChiSquareAmbiguitySet

ambiguity_set = ChiSquareAmbiguitySet(
    torch.full((batch_size,), 1.0 / batch_size, device="cuda"), radius=2.0
)
static_features = torch.zeros(batch_size, num_features, device="cuda")
static_targets = torch.zeros(batch_size, dtype=torch.long, device="cuda")


def forward_backward() -> None:
    per_sample_loss = torch.nn.functional.cross_entropy(
        model(static_features), static_targets, reduction="none"
    )
    ambiguity_set.worst_case_expectation(per_sample_loss).backward()


stream = torch.cuda.Stream()
stream.wait_stream(torch.cuda.current_stream())
with torch.cuda.stream(stream):
    for _ in range(3):
        model.zero_grad(set_to_none=True)
        forward_backward()
torch.cuda.current_stream().wait_stream(stream)
graph = torch.cuda.CUDAGraph()
model.zero_grad(set_to_none=True)
with torch.cuda.graph(graph):
    forward_backward()

for features, targets in loader:
    static_features.copy_(features)
    static_targets.copy_(targets)
    graph.replay()
    optimizer.step()

The gradients are set to None before capture, so every replay overwrites them instead of accumulating, and optimizer.zero_grad() must not be called between replays. The optimizer update runs outside the graph here. Put it inside only if the optimizer supports capture, which torch.optim marks with its capturable flag. A graph fixes every shape, so a MultilevelMonteCarloEstimator loop captures one graph per level.

Every exact set can be captured this way, and so can StochasticPhiDual, MultilevelMonteCarloEstimator at a fixed level and the OnlineGroupDRO step (tests/dro/test_cuda_graphs.py). PhiAmbiguitySet with a dual_solver, MMDAmbiguitySet and MinimaxSolver read a convergence flag on the host and cannot be captured. On a laptop RTX 3050, the guard test_chi_square_minibatch_training_step in tests/benchmarks/test_dro.py measures a chi-square step on a batch of 50 at about 0.14 ms replayed and about 7 ms eager.

Group DRO

When the training data come from known groups \(g = 1, \dots, G\), for example the combinations of a label and a spurious attribute, group DRO minimizes the risk of the worst group, \(\min_\theta \max_g R_g(\theta)\) (Sagawa et al. 2020). optora.dro.group_dro adds two components. The function group_losses(loss, groups, num_groups) averages per-sample losses within each group, and OnlineGroupDRO keeps the group weights for minibatch training. Over the group losses, the maximum over groups is the worst case of an existing ambiguity set centred on the group proportions.

import torch

from optora import CVaRAmbiguitySet
from optora.dro import group_losses

counts = torch.bincount(groups, minlength=num_groups)
worst_group = CVaRAmbiguitySet(counts / counts.sum(), alpha=0.0)

for _ in range(num_steps):
    optimizer.zero_grad()
    per_sample_loss = (model(features).squeeze(-1) - targets) ** 2
    group_loss = group_losses(per_sample_loss, groups, num_groups)
    worst_group.worst_case_expectation(group_loss).backward()
    optimizer.step()

CVaRAmbiguitySet at alpha=0.0 equals the maximum over the groups present, and its level interpolates to ERM at alpha=1.0. TotalVariationAmbiguitySet at radius one is the maximum over every group. KLAmbiguitySet and ChiSquareAmbiguitySet around the proportions give worst cases between the mixture risk and the worst-group risk. They reach the worst group \(g^\star\) once the radius reaches \(\log(1/p_{g^\star})\) and \(1/p_{g^\star} - 1\), respectively.

import torch

from optora import OnlineGroupDRO
from optora.dro import group_losses

adversary = OnlineGroupDRO(torch.full((num_groups,), 1.0 / num_groups), step_size=0.01)

for features, targets, groups in loader:
    optimizer.zero_grad()
    per_sample_loss = (model(features).squeeze(-1) - targets) ** 2
    group_loss = group_losses(per_sample_loss, groups, num_groups)
    adversary.step(group_loss)
    adversary(group_loss).backward()
    optimizer.step()

The maximum of the minibatch group means overestimates the risk of the worst group, because the expectation of a maximum is at least the maximum of the expectations. OnlineGroupDRO instead implements Algorithm 1 of Sagawa et al. Its step method moves the group weights by exponentiated-gradient ascent, \(q_g \propto q_g e^{\eta \hat\ell_g}\), and forward returns the weighted loss \(\sum_g q_g \hat\ell_g\) that the model descends. Call step first, as above, and outside any torch.func transform. forward is a pure function and composes with all of them.

Topic Guidance
Regularization Necessary when the model can fit the training set. Once the loss of every training group is near zero, the adversary has nothing left to upweight, and group DRO does no better than ERM on the worst group (Sagawa et al. 2020). Use a strong L2 penalty or early stopping, as Group DRO under a spurious correlation shows.
Absent groups group_losses assigns loss zero to a group without samples, so step leaves its unnormalized weight unchanged, which is how Algorithm 1 treats a group that was not sampled. Sampling a fixed number of examples per group keeps every group in every minibatch.
Gradient accumulation step needs the group losses of the whole minibatch before any backward pass, while a large model may fit only part of the minibatch in memory. Compute the per-sample losses of every micro-batch without gradients, call step once on their group_losses, then backpropagate adversary(partial) for each micro-batch, where partial is group_losses of the micro-batch times its group counts divided by the group counts of the whole minibatch. forward is linear in the group losses, so the accumulated gradient is the gradient of the whole minibatch. Keep the running statistics of batch normalization fixed in the pass without gradients, for example with momentum zero, so that they are updated once per micro-batch.
Generalization adjustment Sagawa et al. add \(C / \sqrt{n_g}\) to the loss of each group to favour small groups. Pass the adjusted losses to step and the raw losses to forward. The adjustment does not depend on \(\theta\), so it changes the weights but not the form of the gradient.
Many models at once Initial weights of shape (..., G) give one adversary per batch element of the losses, and a tensor step_size sweeps several step sizes in one module.
Reading the weights After \(T\) steps on fixed group losses the weights are the exponential tilt \(q_0 e^{T\eta\ell} / Z\), which is the worst case of a KL ball around \(q_0\). The iterates therefore follow the KL-DRO worst-case path toward the worst group.

Penalized adversarial training

Added in 0.5.0

transport_map and squared_distance need Optora 0.5.0 or later. Before 0.5.0 is on PyPI, install Optora from the repository to run this section.

The ambiguity sets above reweight a fixed set of scenarios. When the adversary may instead move every input of the model, as in the Wasserstein robust method (WRM) of Sinha, Namkoong and Duchi (2018), the inner problem is a maximization over the inputs. The function transport_map solves it for a batch by gradient ascent and returns the moved inputs, and the model step descends the loss at those inputs. The formulations page derives the objective.

import torch
from torch import nn

from optora.dro import squared_distance, transport_map

for inputs, labels in loader:

    def per_example_loss(points: torch.Tensor) -> torch.Tensor:
        return nn.functional.cross_entropy(model(points), labels, reduction="none")

    transported = transport_map(per_example_loss, inputs, penalty)
    optimizer.zero_grad()
    per_example_loss(transported).mean().backward()
    optimizer.step()

transport_map differentiates only the inputs, so it leaves no gradient on the parameters, and it returns detached points. By Danskin's theorem, the gradient of the penalized objective is the gradient of the loss at the maximizer, which is what the backward pass above computes. The same call under torch.no_grad() gives the certificate of Sinha et al. on held-out data.

model.eval()
with torch.no_grad():
    transported = transport_map(per_example_loss, test_inputs, penalty)
    radius = squared_distance(transported, test_inputs).mean()
    worst_case = per_example_loss(transported).mean()

Here radius is \(\hat\rho_{\mathrm{test}}\) (their Eq. 20), and worst_case is the worst-case loss over the Wasserstein ball of that radius around the test sample, a lower bound on it when the ascent has not converged.

Topic Guidance
Choosing the penalty The inner problem is strongly concave once \(\gamma\) exceeds half the Lipschitz constant of \(\nabla_z \ell\), and the ascent then converges to the unique maximizer. A smaller \(\gamma\) allows larger moves but may leave several local maxima. Sinha et al. use smooth ELU activations instead of ReLU so that the loss has Lipschitz gradients in the inputs.
Number of steps num_steps is a fixed budget, 16 by default, and there is no tolerance. The decaying step \(1/\sqrt{t}\) is slow on examples where the inner objective is only weakly concave, and the surrogate is then underestimated. Compare the result with a run of more steps on a few batches. Penalized adversarial training shows such a check.
Per-example losses Every example is solved at once by differentiating the sum of the losses, so the loss of one example must not depend on the others. Batch normalization in training mode breaks this. Compute the adversarial inputs with the model in evaluation mode, or use a normalization layer that acts on each example separately.
Several penalties A tensor penalty of shape (n,) sets one value per example, and torch.func.vmap over a tensor of penalties solves one problem per penalty in a single call.
Precision and devices The points stay on the device of inputs. Half-precision inputs are moved in float32 and passed to loss in their own dtype, and the ascent never reads a value back to the host, so it can be captured in a CUDA graph.

Practical notes

Topic Guidance
Inner cost KLAmbiguitySet locates the worst case by a bisection whose trip count is fixed by the dtype, and ChiSquareAmbiguitySet by one sort and a closed-form root. Every step therefore has the same cost, and there is no tolerance to tune and no warm start to manage.
Batched losses Pass a batched loss of shape (..., n). The inner problems decouple across the leading dimensions and are solved jointly in one call. For minibatches drawn from a population, see Minibatches from a population.
Device and dtype ambiguity_set.to(device) moves nominal and every cached buffer. The returned value is on the device of loss and has the promoted dtype of loss and nominal. Half-precision inputs, including those produced under torch.autocast, are solved in float32, and the result is rounded once. See Numerical behaviour.
Debugging validate=True checks nominal and a tensor radius for invalid values at construction, and checks every loss for non-finite values at each call. The per-call check costs one host synchronization.
Differentiable inputs These are loss, nominal and a tensor radius (alpha for CVaR), as described in Gradients with respect to nominal and radius. The extra derivatives are computed only when nominal or the radius requires grad.
Choosing the radius optora.dro.kl_radius, chi_square_radius and phi_divergence_radius turn a confidence level and a sample size into a radius under which the robust value of the phi-divergence ball is an asymptotically exact upper confidence bound on the true risk. wasserstein_radius does the same for a quadratic transport cost from the losses and their input gradients, calibrated for transport over the whole input space. See Calibrating the ambiguity radius.
Zero radius radius=0.0 makes the objective equal to the nominal-weighted empirical risk, which is the natural non-robust baseline. For WassersteinAmbiguitySet this requires a metric cost (cost[i, j] > 0 for i != j), because a zero-cost pair of distinct support points still lets mass move between them at zero radius. PhiAmbiguitySet with a dual_solver solves its dual at a zero radius as at any other radius, since a generator may vanish on an interval (the CVaR indicator). For an exact baseline, use it without a dual_solver, or use a dedicated set. MMDAmbiguitySet returns the nominal expectation at zero radius only when its Gram matrix is positive definite. Duplicate support points make the Gram matrix singular, mass can then move between them at zero radius, and the set solves that face of the simplex instead.

When to use MinimaxSolver instead

MinimaxSolver minimizes the same composed objective with an Optora Solver over a single decision tensor. It is the better choice for a low-dimensional decision variable that should be solved to convergence with reported diagnostics. The loop above is the better choice when the decision variable consists of the parameters of an nn.Module, since torch.optim already manages that state. Both minimize the same objective, and tests/dro/test_training_loop.py checks that they reach the same optimum.

MinimaxSolver with a GradientDescent outer solver, and SaddlePointSolver, compose with torch.func transforms. For example, torch.func.vmap over a tensor radius solves one robust problem per radius in a single call. Under vmap a batched convergence flag has no single value to read, so every mapped problem runs the whole max_iter budget of the solver, with its iterate frozen once it converges. Each iterate is detached, so an enclosing torch.func.grad treats the solution as a constant and does not differentiate through the steps of the solver.

For a complete training run compared with an ERM baseline and two controls, see Training under subpopulation shift.