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

More methods for round and rand, fix NNlib #162

Merged
merged 7 commits into from
Aug 13, 2024
Merged
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
Prev Previous commit
Next Next commit
Increase code coverage on round
adrhill committed Aug 13, 2024
commit e5690be18d4f73b90f9f3835769f729544bfad09
2 changes: 1 addition & 1 deletion src/overloads/gradient_tracer.jl
Original file line number Diff line number Diff line change
@@ -231,7 +231,7 @@ function Base.round(
return Dual(p, t)
end

for RR in (Integer, Bool)
for RR in (Real, Integer, Bool)
Base.round(::Type{R}, ::T) where {R<:RR,T<:GradientTracer} = myempty(T)
function Base.round(::Type{R}, d::D) where {R<:RR,P,T<:GradientTracer,D<:Dual{P,T}}
p = round(R, primal(d))
2 changes: 1 addition & 1 deletion src/overloads/hessian_tracer.jl
Original file line number Diff line number Diff line change
@@ -337,7 +337,7 @@ function Base.round(
return Dual(p, t)
end

for RR in (Integer, Bool)
for RR in (Real, Integer, Bool)
Base.round(::Type{R}, ::T) where {R<:RR,T<:HessianTracer} = myempty(T)
function Base.round(::Type{R}, d::D) where {R<:RR,P,T<:HessianTracer,D<:Dual{P,T}}
p = round(R, primal(d))
21 changes: 18 additions & 3 deletions test/test_gradient.jl
Original file line number Diff line number Diff line change
@@ -63,7 +63,6 @@ REAL_TYPES = (Float64, Int, Bool, UInt8, Float16, Rational{Int})
@test J(x -> (2//3)^x, 1) ≈ [1;;]
@test J(x -> x^ℯ, 1) ≈ [1;;]
@test J(x -> ℯ^x, 1) ≈ [1;;]
@test J(x -> round(x, RoundNearestTiesUp), 1) ≈ [0;;]
@test J(x -> 0, 1) ≈ [0;;]

# Test special cases on empty tracer
@@ -72,6 +71,16 @@ REAL_TYPES = (Float64, Int, Bool, UInt8, Float16, Rational{Int})
@test J(x -> zero(x)^ℯ, 1) ≈ [0;;]
@test J(x -> ℯ^zero(x), 1) ≈ [0;;]

# Round
@test J(round, 1.1) ≈ [0;;]
@test J(x -> round(Int, x), 1.1) ≈ [0;;]
@test J(x -> round(Bool, x), 1.1) ≈ [0;;]
@test J(x -> round(Float16, x), 1.1) ≈ [0;;]
@test J(x -> round(x, RoundNearestTiesAway), 1.1) ≈ [0;;]
@test J(x -> round(x; digits=3, base=2), 1.1) ≈ [0;;]

# Random

# Linear Algebra
@test J(x -> dot(x[1:2], x[4:5]), rand(5)) == [1 1 0 1 1]

@@ -202,9 +211,15 @@ end
@test J(x -> (2//3)^x, 1) ≈ [1;;]
@test J(x -> x^ℯ, 1) ≈ [1;;]
@test J(x -> ℯ^x, 1) ≈ [1;;]
@test J(x -> round(x, RoundNearestTiesUp), 1) ≈ [0;;]
@test J(x -> 0, 1) ≈ [0;;]

# Round
@test J(round, 1.1) ≈ [0;;]
@test J(x -> round(Int, x), 1.1) ≈ [0;;]
@test J(x -> round(Bool, x), 1.1) ≈ [0;;]
@test J(x -> round(x, RoundNearestTiesAway), 1.1) ≈ [0;;]
@test J(x -> round(x; digits=3, base=2), 1.1) ≈ [0;;]

# Linear algebra
@test J(logdet, [1.0 -1.0; 2.0 2.0]) == [1 1 1 1] # (#68)
@test J(x -> log(det(x)), [1.0 -1.0; 2.0 2.0]) == [1 1 1 1]
@@ -231,7 +246,7 @@ end
@test J(NNlib.swish, 0) ≈ [1;;]
@test J(NNlib.swish, 5) ≈ [1;;]

@test J(NNlib.hardswish, -5) ≈ [1;;]
@test J(NNlib.hardswish, -5) ≈ [0;;]
@test J(NNlib.hardswish, 0) ≈ [1;;]
@test J(NNlib.hardswish, 5) ≈ [1;;]

16 changes: 15 additions & 1 deletion test/test_hessian.jl
Original file line number Diff line number Diff line change
@@ -32,9 +32,16 @@ H(f, x) = hessian_sparsity(f, x, method)
@test H(x -> (2//3)^x, 1) ≈ [1;;]
@test H(x -> x^ℯ, 1) ≈ [1;;]
@test H(x -> ℯ^x, 1) ≈ [1;;]
@test H(x -> round(x, RoundNearestTiesUp), 1) ≈ [0;;]
@test H(x -> 0, 1) ≈ [0;;]

# Round
@test H(round, 1.1) ≈ [0;;]
@test H(x -> round(Int, x), 1.1) ≈ [0;;]
@test H(x -> round(Bool, x), 1.1) ≈ [0;;]
@test H(x -> round(Float16, x), 1.1) ≈ [0;;]
@test H(x -> round(x, RoundNearestTiesAway), 1.1) ≈ [0;;]
@test H(x -> round(x; digits=3, base=2), 1.1) ≈ [0;;]

@test H(x -> x[1] / x[2] + x[3] / 1 + 1 / x[4], rand(4)) == [
0 1 0 0
1 1 0 0
@@ -324,6 +331,13 @@ end
@test H(x -> ℯ^x, 1) ≈ [1;;]
@test H(x -> 0, 1) ≈ [0;;]

# Round
@test H(round, 1.1) ≈ [0;;]
@test H(x -> round(Int, x), 1.1) ≈ [0;;]
@test H(x -> round(Bool, x), 1.1) ≈ [0;;]
@test H(x -> round(x, RoundNearestTiesAway), 1.1) ≈ [0;;]
@test H(x -> round(x; digits=3, base=2), 1.1) ≈ [0;;]

# Test special cases on empty tracer
@test H(x -> zero(x)^(2//3), 1) ≈ [0;;]
@test H(x -> (2//3)^zero(x), 1) ≈ [0;;]