Derivative products¶
Jacobian-, Gauss-Newton- and Hessian-vector products of a propagator's misfit in one call: exact by forward mode where the propagator carries a tangent, by central differences of first derivatives everywhere else. Usage guide: Second derivatives; the routes compared in the Hessian-vector products notebook.
sweep.autodiff ¶
Jacobian-, Gauss-Newton- and Hessian-vector products of a propagator's misfit.
For a record d = F(m) of the models m and a misfit J(m) = loss(F(m)),
J h the Jacobian-vector product (the Born record of h),
J^T (d2 loss) J h the Gauss-Newton product,
H h = J^T (d2 loss) J h + sum_i (d loss / d d_i) d2F_i h,
each from one call, choosing among the routes a propagator can run:
'forward'
Forward-mode AD carries h through the uncompiled eager step with
memory=Full(); the tangent of the record is J h. For H h, the gradient
of <grad loss(d), s.detach()> + <s, grad loss(d).detach()> with
s = J h is exact and takes first derivatives only (reverse over
forward).
'double_backward'
H h only: the gradient taken with create_graph=True and differentiated
again. Exact; the uncompiled eager step, Full() or Ckpt(). Its
graph holds the forward and the first backward; inside
:func:sweep.operators.torch.slice_stencils it holds 3-4x less (the
stencils as shifted slices), at 1.4-1.7x the time.
'difference'
Central differences, (g(m + e h) - g(m - e h)) / (2 e) for H h and
(F(m + e h) - F(m - e h)) / (2 e) for J h: two gradients or two
forwards, with every impl and memory strategy and with ModelParallel.
The step e changes no model by more than rel_step of its peak; a
model that is all zero (the zero reflectivity LSRTM starts from) counts as
1, and under ModelParallel the peaks are those of the whole model, so
every rank takes the same step from its own tile. The error is O(e^2)
plus float32 round-off of O(1 / e): a strongly curved loss needs a smaller
rel_step, a model whose peak is far below the size of its changes a
larger one.
'auto' (default)
'forward' where the propagator carries a tangent (the uncompiled eager
step with Full()), else 'difference' -- also on the uncompiled
step with Ckpt(), where 'double_backward' is exact too. Forward
mode is exact but not the fastest: two gradients take less time than
reverse over forward.
directions holds one tensor per model, or None for a model held fixed:
it is not perturbed, needs no gradient (impl='c' ElasticBorn and the qP
Born pair refuse a background model that requires one, as AcousticBorn does
under checkpointing) and its entry of a product is None. A zero direction
keeps a model a variable, whose entry is then a cross term. Directions are
cast to their model's dtype and device. loss maps the record to a scalar
and must be twice differentiable for the exact routes; terms of the misfit
that depend on the models directly (a regulariser) are not part of these
products. An equation can refuse the curvature products (Gauss-Newton and
H h, its curvature_refusal) or the 'difference' route (its
difference_refusal) where they are not reliable: the qP equations
(AcousticVTI, AcousticTTI and their Born pair) refuse both, because
where epsilon differs from delta their record and gradient are not smooth
functions of the model. J h of AcousticVTI and AcousticTTI along a
smooth direction still holds by forward mode (s_gradient='exact').
'forward' and 'difference' differentiate the gradient the propagator
returns, which on the outermost ring of model cells is the cropped one (the
model is edge-replicated into the absorbing layer and its backward crops): for
a direction on that ring they give C H E h, which is not symmetric, while
'double_backward' gives the symmetric C H C^T h. Keep directions off
the outermost ring when the routes are compared or when a solver needs a
symmetric operator.
:func:hessian_vector_products takes K directions at once (Hessian columns,
probing vectors) and runs them as copies of the survey in one call, where the
equation takes a model per shot. Batching through PyTorch's vmap
(torch.autograd.grad(..., is_grads_batched=True),
torch.autograd.functional.hessian(..., vectorize=True)) does not batch the
eager step: its legacy vmap has no batching rule for the convolutions'
derivatives (nor for copy_, zeros_like, index_select), so it loops
per direction inside them.
jacobian_vector_product ¶
jacobian_vector_product(
propagator,
wavelet,
sources,
receivers,
models,
directions,
*,
route="auto",
rel_step=0.01
)
(record, J h): the record at models and its derivative along
directions (one per model, None for a model held fixed).
gauss_newton_vector_product ¶
gauss_newton_vector_product(
propagator,
wavelet,
sources,
receivers,
models,
directions,
loss,
*,
route="auto",
rel_step=0.01
)
J^T (d2 loss) J h along directions (one per model, None for a
model held fixed): one tensor per model, None for a model held fixed;
positive semi-definite for a convex loss, the product a Gauss-Newton
method takes.
hessian_vector_product ¶
hessian_vector_product(
propagator,
wavelet,
sources,
receivers,
models,
directions,
loss,
*,
route="auto",
rel_step=0.01
)
H h of loss(propagator(..., models)) along directions (one per
model, None for a model held fixed): one tensor per model, None for
a model held fixed.
hessian_vector_products ¶
hessian_vector_products(
propagator,
wavelet,
sources,
receivers,
models,
directions,
loss,
*,
route="auto",
rel_step=0.01,
batch=None
)
H h along K directions at once: directions holds, per model, the
K directions stacked on a leading axis, (K, *model.shape) (None for
a model held fixed), and so does each returned product -- what calling
:func:hessian_vector_product once per direction returns, stacked.
Where the equation takes a model per shot (supports_batched_models,
e.g. the 2-D Acoustic and Elastic) and the models are shared,
batch directions (all K by default) run as copies of the survey, each
with its own models, in one call: one forward and one double backward for
the lot (one dual forward for 'forward', one gradient of 2 * batch
copies for 'difference', on every impl). A copy holds what one survey
holds, so batch bounds the memory. Elsewhere the directions run one
after another.