Gradients
Only FirstOrderGRAMIS needs target gradients among the implemented samplers. Value-only operations still use the log target without requesting a gradient.
Explicit gradients
using ImportanceSamplers, Random, Statisticslogtarget(x, p) = -sum(abs2, x .- p.centre) / 2function gradient!(g, x, p) g .= p.centre .- x return nothingendtarget = LogTarget(logtarget; grad=gradient!)p = (; centre=[0.5, -0.5])bank = ProposalBank([ SphericalGaussian([-1.0, 0.0], 1.5), SphericalGaussian([ 1.0, 0.0], 1.5),])algorithm = FirstOrderGRAMIS( bank; rounds=3, round_size=2_000, repulsion_strength=0.0,)samples = importance_sample(Xoshiro(42), target, p, algorithm)mean(samples)2-element Vector{Float64}:
0.5061823392402699
-0.48013285996082705The callback fills the gradient of the log target with respect to x. The context p stays constant.
Without a context, use gradient!(g, x). On CPU, an out-of-place gradient(x) or gradient(x, p) is also accepted. An applicable in-place form has priority.
A device gradient must support the supplied device arrays. Do not retain its borrowed input or output buffers.
Automatic differentiation
LogTarget accepts an ADTypes selector for an installed AD backend. For example, with ForwardDiff installed:
using ADTypes, ForwardDifftarget = LogTarget(logtarget, AutoForwardDiff())samples = importance_sample(Xoshiro(42), target, p, algorithm)The package uses DifferentiationInterface on CPU. It prepares derivative state for the chosen target and input. It does not choose a backend automatically.
An explicit grad= takes priority over adtype. A bare first-order LogDensityProblems target can supply its own value/gradient interface. Failure in the selected source does not trigger a different source.
Named-parameter gradients
For a target taking theta = (; rate, offset), a logical gradient has matching fields:
using ImportanceSamplers, Random, Statisticslayout = ( rate=(1 => PositiveTransform()), offset=(2 => IdentityTransform()),)named_target(theta) = -theta.rate - theta.offset^2 / 2function named_gradient!(g, theta) g.rate[] = -one(theta.rate) g.offset[] = -theta.offset return nothingendbank = ProposalBank([ SphericalGaussian([-0.5, -0.5], 1.0), SphericalGaussian([ 0.5, 0.5], 1.0),])samples = importance_sample( Xoshiro(7), LogTarget(named_target; grad=named_gradient!), FirstOrderGRAMIS(bank; rounds=3, round_size=2_000, repulsion_strength=0.0); transform=layout,)mean(samples)(rate = 0.9815784517490851, offset = 0.010159003304485714)Scalar gradient fields are zero-dimensional writable views, so use g.rate[]. Vector fields use ordinary indexed writes. For a simplex of length K, supply K logical derivatives.
Differentiate only the logical log target. The package adds the transform pullback and the log-Jacobian derivative. CPU AD instead differentiates the composed flat-coordinate target.
Accelerator gradients
| Execution | Gradient route |
|---|---|
| Native CUDA | Device-compatible in-place callback or supported reverse-mode Enzyme |
| Metal | Device-compatible in-place callback |
| Reactant GPU | Supported compiled gradient, including AutoEnzyme |
| Ordinary CPU | Explicit callbacks, LogDensityProblems, or a supported DI backend |
A scalar target that runs on GPU need not be differentiable by the GPU AD backend.
For native CUDA with Enzyme, structured contexts can require explicit runtime activity:
using ADTypes, Enzymead = AutoEnzyme(; mode=Enzyme.set_runtime_activity(Enzyme.Reverse))target = LogTarget(logtarget, ad)The supplied mode is preserved. The package batches scalar target gradients internally. It does not copy each sample or gradient to CPU.
batch= controls value evaluation, not a separate batch-gradient interface. Automatic differentiation still uses the scalar target and may evaluate its primal.
See Devices for setup and support limits. The DI documentation describes backend-specific restrictions.