Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Adapt.adapt_structure method for Optimisers.Leaf #180

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

[extensions]
OptimisersAdaptExt = ["Adapt"]

[compat]
Adapt = "4"
ChainRulesCore = "1"
Functors = "0.4.9"
Statistics = "1"
Expand Down
19 changes: 19 additions & 0 deletions ext/OptimisersAdaptExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module OptimisersAdaptExt

import Adapt
import Optimisers: Leaf

function Adapt.adapt_structure(to, leaf::Leaf)
@warn """`Optimisers.Leaf` object does not support device transfer via
`Adapt.jl`. Avoid this by calling `Flux.gpu/cpu` or
`MLDataDevices.cpu_device()/gpu_device()` on the optimiser state object.
See below GitHub issue for more details.
https://github.com/FluxML/Optimisers.jl/issues/179 """ maxlog=1

rule = Adapt.adapt_structure(to, leaf.rule)
state = Adapt.adapt_structure(to, leaf.state)

Leaf(rule, state, leaf.frozen)
end

end
Loading