-
Notifications
You must be signed in to change notification settings - Fork 34
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
6 changed files
with
75 additions
and
20 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
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,55 @@ | ||
module BijectorsEnzymeCoreExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using EnzymeCore: Const, Duplicated, DuplicatedNoNeed, EnzymeRules | ||
using Bijectors: find_alpha | ||
else | ||
using ..EnzymeCore: Const, Duplicated, DuplicatedNoNeed, EnzymeRules | ||
using ..Bijectors: find_alpha | ||
end | ||
|
||
_add(x::Real, ::Nothing) = x | ||
_add(x::Real, y::Real) = x + y | ||
|
||
_muladd(x::Real, y::Real, ::Nothing) = x * y | ||
_muladd(x::Real, y::Real, z::Real) = muladd(x, y, z) | ||
|
||
function EnzymeRules.forward( | ||
::Const{typeof(find_alpha)}, | ||
::Type{RT}, | ||
wt_y::Union{Const,Duplicated}, | ||
wt_u_hat::Union{Const,Duplicated}, | ||
b::Union{Const,Duplicated}, | ||
) where {RT<:Union{Const,DuplicatedNoNeed,Duplicated}} | ||
# Compute primal value | ||
Ω = find_alpha(wt_y.val, wt_u_hat.val, b.val) | ||
|
||
# Early exit if no derivatives are requested | ||
if RT <: Const | ||
return Ω | ||
end | ||
|
||
if wt_y isa Const && wt_u_hat isa Const && b isa Const | ||
# Trivial case: All partial derivatives are 0 | ||
Ω̇ = zero(Ω) | ||
else | ||
# In all other cases we have to compute the partial derivatives | ||
# As in the rule for ChainRules, we reuse the following term in the computation of all derivatives | ||
c = wt_u_hat.val * sech(Ω + b.val)^2 | ||
|
||
x = b isa Duplicated ? (-c) * b.dval : nothing | ||
y = wt_u_hat isa Duplicated ? _muladd(-tanh(Ω + b.val), wt_u_hat.dval, x) : x | ||
z = wt_y isa Duplicated ? _add(wt_y.dval, y) : y | ||
|
||
Ω̇ = z / (1 + c) | ||
end | ||
|
||
if RT <: Duplicated | ||
return Duplicated(Ω, Ω̇) | ||
else | ||
@assert RT <: DuplicatedNoNeed | ||
return Ω̇ | ||
end | ||
end | ||
|
||
end # module |
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
@testset "Enzyme: Bijectors.find_alpha" begin | ||
x = randn() | ||
y = expm1(randn()) | ||
z = randn() | ||
|
||
@testset "forward" begin | ||
@testset for RT in (Const, DuplicatedNoNeed, Duplicated), | ||
Tx in (Const, Duplicated), | ||
Ty in (Const, Duplicated), | ||
Tz in (Const, Duplicated) | ||
|
||
test_forward(Bijectors.find_alpha, RT, (x, Tx), (y, Ty), (z, Tz)) | ||
end | ||
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