-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
333 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
name: CompatHelper | ||
|
||
on: | ||
schedule: | ||
- cron: '00 00 * * *' | ||
|
||
- cron: 0 0 * * * | ||
workflow_dispatch: | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
jobs: | ||
CompatHelper: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Pkg.add("CompatHelper") | ||
run: julia -e 'using Pkg; Pkg.add("CompatHelper")' | ||
- name: CompatHelper.main() | ||
- name: Check if Julia is already available in the PATH | ||
id: julia_in_path | ||
run: which julia | ||
continue-on-error: true | ||
- name: Install Julia, but only if it is not already available in the PATH | ||
uses: julia-actions/setup-julia@v2 | ||
with: | ||
version: '1' | ||
arch: ${{ runner.arch }} | ||
if: steps.julia_in_path.outcome != 'success' | ||
- name: "Add the General registry via Git" | ||
run: | | ||
import Pkg | ||
ENV["JULIA_PKG_SERVER"] = "" | ||
Pkg.Registry.add("General") | ||
shell: julia --color=yes {0} | ||
- name: "Install CompatHelper" | ||
run: | | ||
import Pkg | ||
name = "CompatHelper" | ||
uuid = "aa819f21-2bde-4658-8897-bab36330d9b7" | ||
version = "3" | ||
Pkg.add(; name, uuid, version) | ||
shell: julia --color=yes {0} | ||
- name: "Run CompatHelper" | ||
run: | | ||
import CompatHelper | ||
CompatHelper.main() | ||
shell: julia --color=yes {0} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: julia -e 'using CompatHelper; CompatHelper.main()' | ||
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} | ||
# COMPATHELPER_PRIV: ${{ secrets.COMPATHELPER_PRIV }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ Manifest.toml | |
.vscode/ | ||
docs/build/ | ||
.DS_Store | ||
/test.jl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module OptimisersEnzymeCoreExt | ||
|
||
import Optimisers: trainable, setup, update!, isnumeric, AbstractRule, _setup | ||
import EnzymeCore: Duplicated, Const | ||
|
||
using Functors: fmapstructure | ||
|
||
trainable(x::Duplicated) = (; val = x.val) | ||
trainable(x::Const) = (;) | ||
|
||
""" | ||
setup(rule::AbstractRule, model_grad::Duplicated) | ||
For use with Enzyme's Duplicated, this just calls `setup(rule, model_grad.val)`. | ||
""" | ||
setup(rule::AbstractRule, model_grad::Duplicated) = setup(rule, model_grad.val) | ||
|
||
_setup(rule, x::Duplicated; cache) = throw(ArgumentError( | ||
"""Objects of type `Duplicated` are only supported by Optimisers.jl at top level, | ||
they may not appear deep inside other objects.""" | ||
)) | ||
|
||
""" | ||
update!(opt_state, model_grad::Duplicated) | ||
For use with Enzyme's `Duplicated`, which holds both a model/parameters | ||
and the corresponding gradient. | ||
# Example | ||
```jldoctest | ||
julia> using Optimisers, EnzymeCore | ||
julia> x_dx = Duplicated(Float16[1,2,3], Float16[1,0,-4]) | ||
Duplicated{Vector{Float16}}(Float16[1.0, 2.0, 3.0], Float16[1.0, 0.0, -4.0]) | ||
julia> st = Optimisers.setup(Momentum(1/9), x_dx) # acts only on x not on dx | ||
Leaf(Momentum(0.111111, 0.9), Float16[0.0, 0.0, 0.0]) | ||
julia> Optimisers.update!(st, x_dx) # mutates both arguments | ||
julia> x_dx | ||
Duplicated{Vector{Float16}}(Float16[0.8887, 2.0, 3.445], Float16[1.0, 0.0, -4.0]) | ||
julia> st | ||
Leaf(Momentum(0.111111, 0.9), Float16[0.1111, 0.0, -0.4443]) | ||
``` | ||
""" | ||
function update!(opt_state, model_grad::Duplicated) | ||
_, _ = update!(opt_state, model_grad.val, _grad_or_nothing(model_grad)) | ||
nothing | ||
end | ||
|
||
# This function strips the returned gradient to be Zygote-like, | ||
# most importantly prune=nothing removes 2nd appearance of shared gradient to avoid double-counting. | ||
_grad_or_nothing(dup::Duplicated) = fmapstructure(_grad_or_nothing, dup.dval; prune=nothing) | ||
_grad_or_nothing(::Const) = nothing | ||
_grad_or_nothing(x) = isnumeric(x) ? x : nothing | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.