@@ -11,41 +11,76 @@ Concrete AD backend strategy wrapping DifferentiationInterface.jl backends.
1111`AutoZygote()`) and uses it to compute gradients via the DifferentiationInterface.jl
1212ecosystem.
1313
14+ Parameterized on the execution device `P`:
15+ - `DifferentiationInterface{CPU}`: default backend `AutoForwardDiff()` (default device);
16+ - `DifferentiationInterface{GPU}`: default backend `AutoZygote()` (GPU-capable AD).
17+
18+ `DifferentiationInterface(...)` builds a `DifferentiationInterface{CPU}` — the device
19+ parameterization is fully backward compatible with existing call sites.
20+
1421# Arguments
15- - `backend=AutoForwardDiff() `: The DifferentiationInterface.jl backend to use. Defaults
16- to `AutoForwardDiff()` from `ADTypes.jl` (a hard dependency).
22+ - `backend`: The DifferentiationInterface.jl backend to use. Defaults to the device default
23+ ( `AutoForwardDiff()` on CPU, `AutoZygote()` on GPU) from `ADTypes.jl` (a hard dependency).
1724- `kwargs...`: Additional options passed to `StrategyOptions`.
1825
1926# Notes
20- - `ADTypes.jl` is a hard dependency, so `AutoForwardDiff()` is always available in core.
27+ - `ADTypes.jl` is a hard dependency, so the default backend markers are always available in core;
28+ `AutoZygote()` is a marker type and needs no Zygote loaded to construct.
2129 - Gradient computation requires the `CTBaseDifferentiationInterface` extension.
2230 - Without the extension, the gradient methods throw `NotImplemented` with a helpful message.
2331
2432See also: [`CTBase.Differentiation.AbstractADBackend`](@ref),
2533[`CTBase.Differentiation.hamiltonian_gradient`](@ref),
2634[`CTBase.Differentiation.variable_gradient`](@ref).
2735"""
28- struct DifferentiationInterface{O<: Strategies.StrategyOptions } <: AbstractADBackend
36+ struct DifferentiationInterface{
37+ P<: Union{Strategies.CPU,Strategies.GPU} ,O<: Strategies.StrategyOptions
38+ } <: AbstractADBackend
2939 options:: O
3040end
3141
3242"""
3343$(TYPEDSIGNATURES)
3444
35- Constructor for `DifferentiationInterface` with a specific backend.
45+ Construct a `DifferentiationInterface{CPU}` (the default device). Equivalent to
46+ `DifferentiationInterface{CPU}(...)`; delegates through
47+ [`CTBase.Strategies.default_parameter`](@ref).
3648
3749# Arguments
3850- `mode::Symbol=:strict`: Validation mode forwarded to `build_strategy_options`.
3951- `kwargs...`: Options passed to `StrategyOptions`. The AD backend is set through the
4052 `:ad_backend` option (aliases `backend` and `ad`), e.g. `backend=AutoForwardDiff()`;
41- it defaults to `AutoForwardDiff()`.
53+ it defaults to the device default ( `AutoForwardDiff()` on CPU) .
4254
4355# Returns
44- - `DifferentiationInterface`: A new backend strategy instance.
56+ - `DifferentiationInterface{CPU} `: A new backend strategy instance.
4557"""
4658function DifferentiationInterface (; mode:: Symbol = :strict , kwargs... )
47- opts = Strategies. build_strategy_options (DifferentiationInterface; mode= mode, kwargs... )
48- return DifferentiationInterface {typeof(opts)} (opts)
59+ P = Strategies. default_parameter (DifferentiationInterface)
60+ return DifferentiationInterface {P} (; mode= mode, kwargs... )
61+ end
62+
63+ """
64+ $(TYPEDSIGNATURES)
65+
66+ Construct a parameterized `DifferentiationInterface{P}` for the execution device `P`
67+ (`CPU` or `GPU`). The default `:ad_backend` is device-aware (`AutoForwardDiff()` on CPU,
68+ `AutoZygote()` on GPU) and can be overridden through the `:ad_backend` option.
69+
70+ # Arguments
71+ - `mode::Symbol=:strict`: Validation mode forwarded to `build_strategy_options`.
72+ - `kwargs...`: Options passed to `StrategyOptions` (`:ad_backend`, aliases `backend`/`ad`).
73+
74+ # Returns
75+ - `DifferentiationInterface{P}`: A new backend strategy instance.
76+ """
77+ function DifferentiationInterface {P} (;
78+ mode:: Symbol = :strict , kwargs...
79+ ) where {P<: Strategies.AbstractStrategyParameter }
80+ opts = Strategies. build_strategy_options (
81+ DifferentiationInterface{P}; mode= mode, kwargs...
82+ )
83+ return DifferentiationInterface {P,typeof(opts)} (opts)
4984end
5085
5186# ==============================================================================
@@ -62,6 +97,35 @@ Strategies.id(::Type{<:DifferentiationInterface}) = :di
6297"""
6398$(TYPEDSIGNATURES)
6499
100+ Return the execution parameter type of a `DifferentiationInterface` strategy.
101+
102+ Extracts `P` from `DifferentiationInterface{P}` (`CPU` or `GPU`). Overrides the
103+ `AbstractADBackend` family default (`nothing`) since this strategy is device-parameterized.
104+
105+ # Returns
106+ - `Type{<:Union{CPU,GPU}}`: the execution parameter type.
107+
108+ See also: [`CTBase.Strategies.CPU`](@ref), [`CTBase.Strategies.GPU`](@ref)
109+ """
110+ Strategies. parameter (
111+ :: Type{<:DifferentiationInterface{P}}
112+ ) where {P<: Union{Strategies.CPU,Strategies.GPU} } = P
113+
114+ """
115+ $(TYPEDSIGNATURES)
116+
117+ Return the default execution parameter for `DifferentiationInterface` when none is specified.
118+
119+ Returns `CPU`, so `DifferentiationInterface(...)` builds a `DifferentiationInterface{CPU}` and
120+ every existing call site is unaffected by the device parameterization.
121+
122+ See also: [`CTBase.Strategies.CPU`](@ref)
123+ """
124+ Strategies. default_parameter (:: Type{<:DifferentiationInterface} ) = Strategies. CPU
125+
126+ """
127+ $(TYPEDSIGNATURES)
128+
65129Return a human-readable description of the `DifferentiationInterface` strategy.
66130"""
67131function Strategies. description (:: Type{<:DifferentiationInterface} )
71135"""
72136$(TYPEDSIGNATURES)
73137
74- Return metadata defining `DifferentiationInterface` options and their specifications.
138+ Return metadata defining `DifferentiationInterface{P}` options and their specifications.
139+
140+ The `:ad_backend` default is device-aware: `AutoForwardDiff()` on `CPU`, `AutoZygote()` on `GPU`
141+ (via [`CTBase.Differentiation.__ad_backend`](@ref)). The bare `metadata(DifferentiationInterface)`
142+ delegates here through `DifferentiationInterface{CPU}`.
75143"""
76- function Strategies. metadata (:: Type{<:DifferentiationInterface} )
144+ function Strategies. metadata (
145+ :: Type{<:DifferentiationInterface{P}}
146+ ) where {P<: Union{Strategies.CPU,Strategies.GPU} }
77147 return Strategies. StrategyMetadata (
78148 Strategies. OptionDefinition (;
79149 name= :ad_backend ,
80150 type= ADTypes. AbstractADType,
81- default= __ad_backend (),
82- description= " DifferentiationInterface.jl backend (e.g. AutoForwardDiff())." ,
151+ default= __ad_backend (P),
152+ computed= true , # Default is computed from parameter P (CPU→ForwardDiff, GPU→Zygote)
153+ description= " DifferentiationInterface.jl backend (e.g. AutoForwardDiff() on CPU, AutoZygote() on GPU)." ,
83154 aliases= (:backend , :ad ),
84155 ),
85156 )
88159"""
89160$(TYPEDSIGNATURES)
90161
162+ Fallback for the non-parameterized `DifferentiationInterface` type that delegates to
163+ `DifferentiationInterface{CPU}`. Preserves backward compatibility for
164+ `metadata(DifferentiationInterface)`.
165+ """
166+ function Strategies. metadata (:: Type{DifferentiationInterface} )
167+ return Strategies. metadata (
168+ DifferentiationInterface{Strategies. default_parameter (DifferentiationInterface)}
169+ )
170+ end
171+
172+ """
173+ $(TYPEDSIGNATURES)
174+
91175Extract the AD backend from a `DifferentiationInterface` strategy.
92176
93177# Arguments
0 commit comments