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

Disable + and - rules on mixed numeric types #787

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 42 additions & 8 deletions src/rulesets/Base/fastmath_able.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,53 @@ let
return (Ω, hypot_pullback)
end

@scalar_rule x + y (true, true)
@scalar_rule x - y (true, -1)
@scalar_rule x / y (one(x) / y, -(Ω / y))

## many-arg +
function frule(Δs, ::typeof(+), x::Number, ys::Number...)
###
### +
###
# Same type so must have same tangent type
function frule(Δs, ::typeof(+), x::T, ys::T...) where {T<:Number}
+(x, ys...), +(Base.tail(Δs)...)
end

function rrule(::typeof(+), x::Number, ys::Number...)
function rrule(::typeof(+), x::T, ys::T...) where {T<:Number}
plus_back(dz) = (NoTangent(), dz, map(Returns(dz), ys)...)
+(x, ys...), plus_back
end
end

#Or Tangent type is same as primal type so op must be defined on it too
function frule((_, ẋ, ẏ)::Tuple{<:Any, A, B}, ::typeof(+), x::A, y::B) where {A<:Number, B<:Number}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
return +(x, y), +(ẋ, ẏ)
end

# Both cases (break ambiguity)
function frule((_, ẋ, ẏ)::Tuple{<:Any, T, T}, ::typeof(+), x::T, y::T) where {T<:Number}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
return +(x, y), +(ẋ, ẏ)
end

oxinabox marked this conversation as resolved.
Show resolved Hide resolved

###
### -
###
# Same type so must have same tangent type
function rrule(::typeof(-), x::T, y::T) where {T<:Number}
minus_pullback(z̄) = NoTangent(), z̄, -(z̄)
return x - y, minus_pullback
end
frule((_, ẋ, ẏ), ::typeof(-), x::T, y::T) where {T<:Number} = -(x, y), -(ẋ, ẏ)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

#Or Tangent type is same as primal type so op must be defined on it too
function frule((_, ẋ, ẏ)::Tuple{<:Any, A, B}, ::typeof(-), x::A, y::B) where {A<:Number, B<:Number}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
return -(x, y), -(ẋ, ẏ)
end

# Both cases (break ambiguity)
function frule((_, ẋ, ẏ)::Tuple{<:Any, T, T}, ::typeof(-), x::T, y::T) where {T<:Number}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
return -(x, y), -(ẋ, ẏ)
end

oxinabox marked this conversation as resolved.
Show resolved Hide resolved

@scalar_rule x / y (one(x) / y, -(Ω / y))


## power
# literal_pow is in base.jl
Expand Down
30 changes: 26 additions & 4 deletions test/rulesets/Base/fastmath_able.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ const FASTABLE_AST = quote
@assert T == typeof(f(x, y))
Δz = randn(typeof(f(x, y)))

@test frule((ZeroTangent(), Δx, Δy), f, x, y) isa Tuple{T, T}
@test frule((NoTangent(), Δx, Δy), f, x, y) isa Tuple{T, T}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
_, ∂x, ∂y = rrule(f, x, y)[2](Δz)
@test (∂x, ∂y) isa Tuple{T, T}

if f != hypot
if f ∉ (hypot, +, -)
# Issue #233
@test frule((ZeroTangent(), Δx, Δy), f, x, 2) isa Tuple{T, T}
@test frule((NoTangent(), Δx, Δy), f, x, 2) isa Tuple{T, T}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
_, ∂x, ∂y = rrule(f, x, 2)[2](Δz)
@test (∂x, ∂y) isa Tuple{T, Float64}

@test frule((ZeroTangent(), Δx, Δy), f, 2, y) isa Tuple{T, T}
@test frule((NoTangent(), Δx, Δy), f, 2, y) isa Tuple{T, T}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
_, ∂x, ∂y = rrule(f, 2, y)[2](Δz)
@test (∂x, ∂y) isa Tuple{Float64, T}
end
Expand Down Expand Up @@ -283,6 +283,28 @@ const FASTABLE_AST = quote
end
end
end

@testset "+,- on weird types" begin

oxinabox marked this conversation as resolved.
Show resolved Hide resolved
struct StoreHalfed <: Number
val::Float64
StoreHalfed(x) = new(x/2)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end
Base.:-(x::StoreHalfed, y::Number) = 2*x.val - y
Base.:+(x::StoreHalfed, y::Number) = 2*x.val + y

oxinabox marked this conversation as resolved.
Show resolved Hide resolved
sh1 = StoreHalfed(4.0)
sh2 = StoreHalfed(8.0)
f1 = 40.0
f2 = 80.0

# We have had issues with mixed number types before
# So these should not hit
@test rrule(+, sh1, f1) == nothing
@test rrule(-, sh1, f1) == nothing
@test frule((NoTangent(), Tangent{StoreHalfed}(val=2.0), 20.0),+, sh1, f1) == nothing
@test frule((NoTangent(), Tangent{StoreHalfed}(val=2.0), 20.0),-, sh1, f1) == nothing
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end
end

# Now we generate tests for fast and nonfast versions
Expand Down
Loading