-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from cgeoga/enzyme
Add `EnzymeCore` weakdep and an extension with a custom rule for the Levin transformation
- Loading branch information
Showing
15 changed files
with
1,127 additions
and
117 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 |
---|---|---|
|
@@ -21,7 +21,7 @@ jobs: | |
matrix: | ||
version: | ||
- '1' | ||
- '1.8' | ||
- '1.9' | ||
- 'nightly' | ||
os: | ||
- ubuntu-latest | ||
|
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,34 @@ | ||
module BesselsEnzymeCoreExt | ||
|
||
using Bessels, EnzymeCore | ||
using EnzymeCore.EnzymeRules | ||
using Bessels.Math | ||
|
||
# A manual method that separately transforms the `val` and `dval`, because | ||
# sometimes the `val` can converge while the `dval` hasn't, so just using an | ||
# early return or something can give incorrect derivatives in edge cases. | ||
function EnzymeRules.forward(func::Const{typeof(levin_transform)}, | ||
::Type{<:Duplicated}, | ||
s::Duplicated, | ||
w::Duplicated) | ||
(sv, dv, N) = (s.val, s.dval, length(s.val)) | ||
ls = levin_transform(sv, w.val) | ||
dls = levin_transform(dv, w.dval) | ||
Duplicated(ls, dls) | ||
end | ||
|
||
# This is fixing a straight bug in Enzyme. | ||
function EnzymeRules.forward(func::Const{typeof(sinpi)}, | ||
::Type{<:Duplicated}, | ||
x::Duplicated) | ||
(sp, cp) = sincospi(x.val) | ||
Duplicated(sp, pi*cp*x.dval) | ||
end | ||
|
||
function EnzymeRules.forward(func::Const{typeof(sinpi)}, | ||
::Type{<:Const}, | ||
x::Const) | ||
sinpi(x.val) | ||
end | ||
|
||
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
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.