-
-
Notifications
You must be signed in to change notification settings - Fork 213
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 Fastmath operators #388
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,4 +1,5 @@ | ||
using DiffRules, SpecialFunctions, NaNMath | ||
using Base.FastMath: fast_op, make_fastmath | ||
|
||
@nograd isinf, isnan, isfinite, div | ||
|
||
|
@@ -14,25 +15,25 @@ for (M, f, arity) in DiffRules.diffrules() | |
dx = :(conj($dx)) | ||
end | ||
@eval begin | ||
@adjoint $M.$f(x::Number) = $M.$f(x), | ||
Δ -> ($Δ * $dx,) | ||
@adjoint $M.$f(x::Number) = $M.$f(x), Δ -> ($Δ * $dx,) | ||
end | ||
end | ||
|
||
for (M, f, arity) in DiffRules.diffrules() | ||
arity == 2 || continue | ||
da, db = DiffRules.diffrule(M, f, :a, :b) | ||
@eval begin | ||
@adjoint $M.$f(a::Number, b::Number) = $M.$f(a, b), | ||
Δ -> (Δ * conj($da), Δ * conj($db)) | ||
@adjoint $M.$f(a::Number, b::Number) = | ||
$M.$f(a, b), Δ -> (Δ * conj($da), Δ * conj($db)) | ||
end | ||
end | ||
|
||
@adjoint Base.convert(T::Type{<:Real}, x::Real) = convert(T, x), ȳ -> (nothing, ȳ) | ||
@adjoint Base.convert(T::Type{<:Real}, x::Real) = | ||
convert(T, x), ȳ -> (nothing, ȳ) | ||
@adjoint (T::Type{<:Real})(x::Real) = T(x), ȳ -> (nothing, ȳ) | ||
|
||
for T in Base.uniontypes(Core.BuiltinInts) | ||
@adjoint (::Type{T})(x::Core.BuiltinInts) = T(x), Δ -> (Δ,) | ||
@adjoint (::Type{T})(x::Core.BuiltinInts) = T(x), Δ -> (Δ,) | ||
end | ||
|
||
@adjoint Base.:+(xs::Number...) = +(xs...), Δ -> map(_ -> Δ, xs) | ||
|
@@ -45,19 +46,57 @@ end | |
|
||
@adjoint function sincos(x) | ||
s, c = sincos(x) | ||
(s, c), ((s̄, c̄),) -> (s̄*c - c̄*s,) | ||
(s, c), ((s̄, c̄),) -> (s̄ * c - c̄ * s,) | ||
end | ||
|
||
@adjoint a // b = (a // b, c̄ -> (c̄ * 1//b, - c̄ * a // b // b)) | ||
@adjoint a // b = (a // b, c̄ -> (c̄ * 1 // b, -c̄ * a // b // b)) | ||
|
||
@nograd floor, ceil, trunc, round, hash | ||
|
||
# Complex Numbers | ||
|
||
@adjoint (T::Type{<:Complex})(re, im) = T(re, im), c̄ -> (nothing, real(c̄), imag(c̄)) | ||
@adjoint (T::Type{<:Complex})(re, im) = | ||
T(re, im), c̄ -> (nothing, real(c̄), imag(c̄)) | ||
|
||
@adjoint real(x::Number) = real(x), r̄ -> (real(r̄),) | ||
@adjoint conj(x::Number) = conj(x), r̄ -> (conj(r̄),) | ||
@adjoint imag(x::Number) = imag(x), ī -> (real(ī)*im,) | ||
@adjoint imag(x::Number) = imag(x), ī -> (real(ī) * im,) | ||
|
||
DiffRules._abs_deriv(x::Complex) = x / abs(x) | ||
|
||
# Function add adjoint for Fastmath operations | ||
# DiffRules is used to find derivatives for each operation | ||
function fastmath() | ||
|
||
for f in keys(fast_op) | ||
unarydx, binarydx = DiffRules.hasdiffrule(:Base, f, 1) ? | ||
DiffRules.diffrule(:Base, f, :x) : nothing, | ||
DiffRules.hasdiffrule(:Base, f, 2) ? | ||
DiffRules.diffrule(:Base, f, :x, :y) : nothing | ||
|
||
fastf = fast_op[f] | ||
|
||
if unarydx != nothing | ||
dx = unarydx | ||
Δ = :Δ | ||
if f in [:abs, :abs2] | ||
Δ = :(real($Δ)) | ||
else | ||
dx = :(conj($dx)) | ||
end | ||
@eval begin | ||
@adjoint Base.FastMath.$fastf(x::Number) = | ||
Base.FastMath.$fastf(x), Δ -> ($Δ * make_fastmath($dx),) | ||
end | ||
elseif binarydx != nothing | ||
dx, dy = binarydx | ||
@eval begin | ||
@adjoint Base.FastMath.$fastf(x::Number, y::Number) = | ||
Base.FastMath.$fastf(x, y), | ||
Δ -> (Δ * make_fastmath(conj($dx)), Δ * make_fastmath(conj($dy))) | ||
end | ||
end | ||
end | ||
end | ||
|
||
DiffRules._abs_deriv(x::Complex) = x/abs(x) | ||
fastmath() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why split this out rather than using a top-level loop as above? |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a strange way to write it. Why not just test
hasdiffrule
directly here?