-
Notifications
You must be signed in to change notification settings - Fork 89
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
Switch to Blue Style #165
Switch to Blue Style #165
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,62 +2,64 @@ | |
@scalar_rule(zero(x), Zero()) | ||
@scalar_rule(sign(x), Zero()) | ||
|
||
@scalar_rule(abs(x::Real), sign(x)) | ||
@scalar_rule(abs2(x), 2x) | ||
@scalar_rule(exp(x), Ω) | ||
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. Not required by Blue Style, but I alphabetised the blocks of code in this file, to make the rules easier to find (they are already in blocks of "similar" functions, but i fixed up a few inconsistencies in that too) |
||
@scalar_rule(exp10(x), Ω * log(oftype(x, 10))) | ||
@scalar_rule(exp2(x), Ω * log(oftype(x, 2))) | ||
@scalar_rule(expm1(x), exp(x)) | ||
@scalar_rule(log(x), inv(x)) | ||
@scalar_rule(log10(x), inv(x) / log(oftype(x, 10))) | ||
@scalar_rule(log2(x), inv(x) / log(oftype(x, 2))) | ||
@scalar_rule(log1p(x), inv(x + 1)) | ||
@scalar_rule(expm1(x), exp(x)) | ||
@scalar_rule(log2(x), inv(x) / log(oftype(x, 2))) | ||
|
||
@scalar_rule(sin(x), cos(x)) | ||
@scalar_rule(cos(x), -sin(x)) | ||
@scalar_rule(sinpi(x), π * cospi(x)) | ||
@scalar_rule(cosd(x), -(π / oftype(x, 180)) * sind(x)) | ||
@scalar_rule(cospi(x), -π * sinpi(x)) | ||
@scalar_rule(sin(x), cos(x)) | ||
@scalar_rule(sincos(x), @setup((sinx, cosx) = Ω), cosx, -sinx) | ||
@scalar_rule(sind(x), (π / oftype(x, 180)) * cosd(x)) | ||
@scalar_rule(cosd(x), -(π / oftype(x, 180)) * sind(x)) | ||
@scalar_rule(sinpi(x), π * cospi(x)) | ||
|
||
@scalar_rule(asin(x), inv(sqrt(1 - x^2))) | ||
@scalar_rule(acos(x), -inv(sqrt(1 - x^2))) | ||
@scalar_rule(atan(x), inv(1 + x^2)) | ||
@scalar_rule(asec(x::Real), inv(abs(x) * sqrt(x^2 - 1))) | ||
@scalar_rule(asec(x), inv(x^2 * sqrt(1 - x^-2))) | ||
@scalar_rule(acsc(x::Real), -inv(abs(x) * sqrt(x^2 - 1))) | ||
@scalar_rule(acsc(x), -inv(x^2 * sqrt(1 - x^-2))) | ||
@scalar_rule(acot(x), -inv(1 + x^2)) | ||
@scalar_rule(acsc(x), -inv(x^2 * sqrt(1 - x^-2))) | ||
@scalar_rule(acsc(x::Real), -inv(abs(x) * sqrt(x^2 - 1))) | ||
@scalar_rule(asec(x), inv(x^2 * sqrt(1 - x^-2))) | ||
@scalar_rule(asec(x::Real), inv(abs(x) * sqrt(x^2 - 1))) | ||
@scalar_rule(asin(x), inv(sqrt(1 - x^2))) | ||
@scalar_rule(atan(x), inv(1 + x^2)) | ||
@scalar_rule(atan(y, x), @setup(u = x^2 + y^2), (x / u, -y / u)) | ||
|
||
@scalar_rule(asind(x), oftype(x, 180) / π / sqrt(1 - x^2)) | ||
@scalar_rule(acosd(x), -oftype(x, 180) / π / sqrt(1 - x^2)) | ||
@scalar_rule(atand(x), oftype(x, 180) / π / (1 + x^2)) | ||
@scalar_rule(asecd(x::Real), oftype(x, 180) / π / abs(x) / sqrt(x^2 - 1)) | ||
@scalar_rule(asecd(x), oftype(x, 180) / π / x^2 / sqrt(1 - x^-2)) | ||
@scalar_rule(acscd(x::Real), -oftype(x, 180) / π / abs(x) / sqrt(x^2 - 1)) | ||
@scalar_rule(acscd(x), -oftype(x, 180) / π / x^2 / sqrt(1 - x^-2)) | ||
@scalar_rule(acotd(x), -oftype(x, 180) / π / (1 + x^2)) | ||
@scalar_rule(acscd(x), -oftype(x, 180) / π / x^2 / sqrt(1 - x^-2)) | ||
@scalar_rule(acscd(x::Real), -oftype(x, 180) / π / abs(x) / sqrt(x^2 - 1)) | ||
@scalar_rule(asecd(x), oftype(x, 180) / π / x^2 / sqrt(1 - x^-2)) | ||
@scalar_rule(asecd(x::Real), oftype(x, 180) / π / abs(x) / sqrt(x^2 - 1)) | ||
@scalar_rule(asind(x), oftype(x, 180) / π / sqrt(1 - x^2)) | ||
@scalar_rule(atand(x), oftype(x, 180) / π / (1 + x^2)) | ||
|
||
@scalar_rule(sinh(x), cosh(x)) | ||
@scalar_rule(cosh(x), sinh(x)) | ||
@scalar_rule(tanh(x), 1-Ω^2) | ||
@scalar_rule(coth(x), -(csch(x)^2)) | ||
@scalar_rule(sinh(x), cosh(x)) | ||
@scalar_rule(tanh(x), 1-Ω^2) | ||
|
||
@scalar_rule(asinh(x), inv(sqrt(x^2 + 1))) | ||
@scalar_rule(acosh(x), inv(sqrt(x^2 - 1))) | ||
@scalar_rule(atanh(x), inv(1 - x^2)) | ||
@scalar_rule(asech(x), -inv(x * sqrt(1 - x^2))) | ||
@scalar_rule(acsch(x::Real), -inv(abs(x) * sqrt(1 + x^2))) | ||
@scalar_rule(acsch(x), -inv(x^2 * sqrt(1 + x^-2))) | ||
@scalar_rule(acoth(x), inv(1 - x^2)) | ||
@scalar_rule(acsch(x), -inv(x^2 * sqrt(1 + x^-2))) | ||
@scalar_rule(acsch(x::Real), -inv(abs(x) * sqrt(1 + x^2))) | ||
@scalar_rule(asech(x), -inv(x * sqrt(1 - x^2))) | ||
@scalar_rule(asinh(x), inv(sqrt(x^2 + 1))) | ||
@scalar_rule(atanh(x), inv(1 - x^2)) | ||
|
||
@scalar_rule(deg2rad(x), π / oftype(x, 180)) | ||
@scalar_rule(rad2deg(x), oftype(x, 180) / π) | ||
|
||
@scalar_rule(conj(x::Real), One()) | ||
@scalar_rule(adjoint(x::Real), One()) | ||
@scalar_rule(conj(x::Real), One()) | ||
@scalar_rule(transpose(x), One()) | ||
|
||
@scalar_rule(abs(x::Real), sign(x)) | ||
@scalar_rule(hypot(x::Real), sign(x)) | ||
@scalar_rule(rem2pi(x, r::RoundingMode), (One(), DoesNotExist())) | ||
|
||
@scalar_rule(+(x), One()) | ||
@scalar_rule(-(x), -1) | ||
@scalar_rule(+(x, y), (One(), One())) | ||
|
@@ -66,39 +68,42 @@ | |
@scalar_rule(\(x, y), (-(y / x / x), inv(x))) | ||
@scalar_rule(^(x, y), (ifelse(iszero(y), zero(Ω), y * x^(y - 1)), Ω * log(x))) | ||
|
||
@scalar_rule(cbrt(x), inv(3 * Ω^2)) | ||
@scalar_rule(inv(x), -Ω^2) | ||
@scalar_rule(sqrt(x), inv(2 * Ω)) | ||
@scalar_rule(cbrt(x), inv(3 * Ω^2)) | ||
@scalar_rule(exp(x), Ω) | ||
@scalar_rule(exp2(x), Ω * log(oftype(x, 2))) | ||
@scalar_rule(exp10(x), Ω * log(oftype(x, 10))) | ||
|
||
@scalar_rule(tan(x), 1 + Ω^2) | ||
@scalar_rule(sec(x), Ω * tan(x)) | ||
@scalar_rule(csc(x), -Ω * cot(x)) | ||
@scalar_rule(cot(x), -(1 + Ω^2)) | ||
@scalar_rule(tand(x), (π / oftype(x, 180)) * (1 + Ω^2)) | ||
@scalar_rule(secd(x), (π / oftype(x, 180)) * Ω * tand(x)) | ||
@scalar_rule(cscd(x), -(π / oftype(x, 180)) * Ω * cotd(x)) | ||
@scalar_rule(cotd(x), -(π / oftype(x, 180)) * (1 + Ω^2)) | ||
@scalar_rule(sech(x), -tanh(x) * Ω) | ||
@scalar_rule(csc(x), -Ω * cot(x)) | ||
@scalar_rule(cscd(x), -(π / oftype(x, 180)) * Ω * cotd(x)) | ||
@scalar_rule(csch(x), -coth(x) * Ω) | ||
@scalar_rule(sec(x), Ω * tan(x)) | ||
@scalar_rule(secd(x), (π / oftype(x, 180)) * Ω * tand(x)) | ||
@scalar_rule(sech(x), -tanh(x) * Ω) | ||
@scalar_rule(tan(x), 1 + Ω^2) | ||
@scalar_rule(tand(x), (π / oftype(x, 180)) * (1 + Ω^2)) | ||
|
||
@scalar_rule(angle(x::Real), Zero()) | ||
@scalar_rule(hypot(x::Real), sign(x)) | ||
@scalar_rule(hypot(x::Real, y::Real), (x / Ω, y / Ω)) | ||
@scalar_rule(sincos(x), @setup((sinx, cosx) = Ω), cosx, -sinx) | ||
@scalar_rule(atan(y, x), @setup(u = x^2 + y^2), (x / u, -y / u)) | ||
@scalar_rule(imag(x::Real), Zero()) | ||
|
||
@scalar_rule(fma(x, y, z), (y, x, One())) | ||
@scalar_rule(max(x, y), @setup(gt = x > y), (gt, !gt)) | ||
@scalar_rule(min(x, y), @setup(gt = x > y), (!gt, gt)) | ||
@scalar_rule(mod(x, y), @setup((u, nan) = promote(x / y, NaN16), isint = isinteger(x / y)), | ||
(ifelse(isint, nan, one(u)), ifelse(isint, nan, -floor(u)))) | ||
@scalar_rule(rem(x, y), @setup((u, nan) = promote(x / y, NaN16), isint = isinteger(x / y)), | ||
(ifelse(isint, nan, one(u)), ifelse(isint, nan, -trunc(u)))) | ||
@scalar_rule(fma(x, y, z), (y, x, One())) | ||
@scalar_rule(muladd(x, y, z), (y, x, One())) | ||
@scalar_rule(angle(x::Real), Zero()) | ||
@scalar_rule( | ||
mod(x, y), | ||
@setup((u, nan) = promote(x / y, NaN16), isint = isinteger(x / y)), | ||
ifelse(isint, nan, one(u)), ifelse(isint, nan, -floor(u)), | ||
) | ||
@scalar_rule(real(x::Real), One()) | ||
@scalar_rule(imag(x::Real), Zero()) | ||
@scalar_rule(rem2pi(x, r::RoundingMode), (One(), DoesNotExist())) | ||
@scalar_rule( | ||
rem(x, y), | ||
@setup((u, nan) = promote(x / y, NaN16), isint = isinteger(x / y)), | ||
ifelse(isint, nan, one(u)), ifelse(isint, nan, -trunc(u)), | ||
) | ||
|
||
# product rule requires special care for arguments where `mul` is non-commutative | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,12 +85,7 @@ function rrule(::typeof(BLAS.asum), n, X, incx) | |
∂X = Zero() | ||
else | ||
ΔΩ = extern(ΔΩ) | ||
∂X = @thunk scal!( | ||
n, | ||
ΔΩ, | ||
blascopy!(n, sign.(X), incx, _zeros(X), incx), | ||
incx | ||
) | ||
∂X = @thunk scal!(n, ΔΩ, blascopy!(n, sign.(X), incx, _zeros(X), incx), incx) | ||
end | ||
return (NO_FIELDS, DoesNotExist(), ∂X, DoesNotExist()) | ||
end | ||
|
@@ -129,8 +124,9 @@ function rrule(::typeof(gemv), tA::Char, α::T, A::AbstractMatrix{T}, | |
return y, gemv_pullback | ||
end | ||
|
||
function rrule(::typeof(gemv), tA::Char, A::AbstractMatrix{T}, | ||
x::AbstractVector{T}) where T<:BlasFloat | ||
function rrule( | ||
::typeof(gemv), tA::Char, A::AbstractMatrix{T}, x::AbstractVector{T} | ||
) where T<:BlasFloat | ||
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. This is one of the few actual style changes -- it now matches the style we use everywhere else |
||
y, inner_pullback = rrule(gemv, tA, one(T), A, x) | ||
function gemv_pullback(Ȳ) | ||
(_, dtA, _, dA, dx) = inner_pullback(Ȳ) | ||
|
@@ -143,8 +139,9 @@ end | |
##### `BLAS.gemm` | ||
##### | ||
|
||
function rrule(::typeof(gemm), tA::Char, tB::Char, α::T, | ||
A::AbstractMatrix{T}, B::AbstractMatrix{T}) where T<:BlasFloat | ||
function rrule( | ||
::typeof(gemm), tA::Char, tB::Char, α::T, A::AbstractMatrix{T}, B::AbstractMatrix{T} | ||
) where T<:BlasFloat | ||
C = gemm(tA, tB, α, A, B) | ||
function gemv_pullback(C̄) | ||
β = one(T) | ||
|
@@ -194,8 +191,9 @@ function rrule(::typeof(gemm), tA::Char, tB::Char, α::T, | |
return C, gemv_pullback | ||
end | ||
|
||
function rrule(::typeof(gemm), tA::Char, tB::Char, | ||
A::AbstractMatrix{T}, B::AbstractMatrix{T}) where T<:BlasFloat | ||
function rrule( | ||
::typeof(gemm), tA::Char, tB::Char, A::AbstractMatrix{T}, B::AbstractMatrix{T} | ||
) where T<:BlasFloat | ||
C, inner_pullback = rrule(gemm, tA, tB, one(T), A, B) | ||
function gemv_pullback(Ȳ) | ||
(_, dtA, dtB, _, dA, dB) = inner_pullback(Ȳ) | ||
|
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.
Bumping to
0.6.1
allows us to remove the lineusing ChainRulesCore: AbstractZero