Skip to content

Commit b6f137c

Browse files
amontoisonMaxenceGollier
authored andcommitted
Update the documentation for set_backend
1 parent 8972306 commit b6f137c

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

docs/src/backend.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,27 @@ nlp = ADNLPModel(f, x0)
9696
get_adbackend(nlp) # returns the `ADModelBackend` structure that regroup all the various backends.
9797
```
9898

99-
There are currently two ways to modify instantiated backends. The first one is to instantiate a new `ADModelBackend` and use `set_adbackend!` to modify `nlp`.
99+
To instantiate a new `ADModelBackend` while preserving existing backends, use `set_adbackend`.
100100

101101
```@example adnlp2
102102
adback = ADNLPModels.ADModelBackend(nlp.meta.nvar, nlp.f, gradient_backend = ADNLPModels.ForwardDiffADGradient)
103-
set_adbackend!(nlp, adback)
104-
get_adbackend(nlp)
103+
new_nlp = set_adbackend(nlp, adback)
104+
get_adbackend(new_nlp)
105105
```
106106

107-
The alternative is to use `set_adbackend!` and pass the new backends via `kwargs`. In the second approach, it is possible to pass either the type of the desired backend or an instance as shown below.
107+
An alternative way to use `set_adbackend` is to pass the new backends as keyword arguments.
108+
In this approach, you can pass either the type of the desired backend or an existing instance, as shown below.
108109

109110
```@example adnlp2
110-
set_adbackend!(
111+
new_nlp = set_adbackend(
111112
nlp,
112113
gradient_backend = ADNLPModels.ForwardDiffADGradient,
113114
jtprod_backend = ADNLPModels.GenericForwardDiffADJtprod(),
114115
)
115-
get_adbackend(nlp)
116+
get_adbackend(new_nlp)
116117
```
117118

118-
### Support multiple precision without having to recreate the model
119+
### Multi-precision model creation with backend reuse
119120

120121
One of the strength of `ADNLPModels.jl` is the type flexibility. Let's assume, we first instantiate an `ADNLPModel` with a `Float64` initial guess.
121122

@@ -133,11 +134,11 @@ x64 = rand(2)
133134
grad(nlp, x64)
134135
```
135136

136-
It is now possible to move to a different type, for instance `Float32`, while keeping the instance `nlp`.
137+
It is now possible to move to a different type for the gradient, for instance `Float32`, while keeping the other backends from the original model `nlp`.
137138

138139
```@example adnlp3
139140
x0_32 = ones(Float32, 2)
140-
set_adbackend!(nlp, gradient_backend = ADNLPModels.ForwardDiffADGradient, x0 = x0_32)
141+
new_nlp = set_adbackend(nlp, gradient_backend = ADNLPModels.ForwardDiffADGradient, x0 = x0_32)
141142
x32 = rand(Float32, 2)
142-
grad(nlp, x32)
143+
grad(new_nlp, x32)
143144
```

src/ADNLPModels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function ADNLSModel!(model::AbstractNLSModel; kwargs...)
160160
end
161161
end
162162

163-
export get_adbackend
163+
export get_adbackend, set_adbackend
164164

165165
"""
166166
get_c(nlp)
@@ -248,7 +248,7 @@ get_adbackend(nlp::ADModel) = nlp.adbackend
248248
new_nlp = set_adbackend(nlp; kwargs...)
249249
250250
Create a copy of nlp that replaces the current `adbackend` with `new_adbackend` or instantiate a new one with `kwargs`, see `ADModelBackend`.
251-
By default, the setter with kwargs will reuse existing backends.
251+
By default, the setter with keyword arguments will reuse existing backends.
252252
"""
253253
function _set_adbackend(nlp::ADM, new_adbackend::ADModelBackend) where{ADM}
254254
values = [f == :adbackend ? new_adbackend : getfield(nlp, f) for f in fieldnames(ADM)]

0 commit comments

Comments
 (0)