Skip to content
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

muladd rule for zero tangent #752

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:R

function backslash_pullback(ȳ)
Ȳ = unthunk(ȳ)

Ȳf = Ȳ
@static if VERSION >= v"1.9"
# Need to ensure Ȳ is an array since since https://github.com/JuliaLang/julia/pull/44358
Expand All @@ -360,7 +360,7 @@ function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:R
end
end
Yf = Y
@static if VERSION >= v"1.9"
@static if VERSION >= v"1.9"
# Need to ensure Yf is an array since since https://github.com/JuliaLang/julia/pull/44358
if !isa(Y, AbstractArray)
Yf = [Y]
Expand All @@ -371,7 +371,7 @@ function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:R
B̄ = A' \ Ȳf
Ā = -B̄ * Y'
t = (B - A * Y) * B̄'
@static if VERSION >= v"1.9"
@static if VERSION >= v"1.9"
# Need to ensure t is an array since since https://github.com/JuliaLang/julia/pull/44358
if !isa(t, AbstractArray)
t = [t]
Expand Down
1 change: 1 addition & 0 deletions src/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ end

@scalar_rule fma(x, y, z) (y, x, true)
@scalar_rule muladd(x, y, z) (y, x, true)
@scalar_rule muladd(x::Union{Number, ZeroTangent}, y::Union{Number, ZeroTangent}, z::Union{Number, ZeroTangent}) (y, x, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@scalar_rule muladd(x::Union{Number, ZeroTangent}, y::Union{Number, ZeroTangent}, z::Union{Number, ZeroTangent}) (y, x, true)
@scalar_rule muladd(
x::Union{Number,ZeroTangent}, y::Union{Number,ZeroTangent}, z::Union{Number,ZeroTangent}
) (y, x, true)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no I don't like that

@scalar_rule rem2pi(x, r::RoundingMode) (true, NoTangent())
@scalar_rule(
mod(x, y),
Expand Down
10 changes: 10 additions & 0 deletions test/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@
test_rrule(muladd, 10randn(), randn(), randn())
end

@testset "muladd ZeroTangent" begin
test_frule(muladd, 2.0, 3.0, ZeroTangent())
test_frule(muladd, 2.0, ZeroTangent(), 4.0)
test_frule(muladd, ZeroTangent(), 3.0, 4.0)

test_rrule(muladd, 2.0, 3.0, ZeroTangent())
test_rrule(muladd, 2.0, ZeroTangent(), 4.0)
test_rrule(muladd, ZeroTangent(), 3.0, 4.0)
end

@testset "fma" begin
test_frule(fma, 10randn(), randn(), randn())
test_rrule(fma, 10randn(), randn(), randn())
Expand Down
Loading