Skip to content

Commit

Permalink
Tests II: more Julia function tests (#969)
Browse files Browse the repository at this point in the history
* Julia function tests

* Nested reverse test

* Disable failing tests on earlier Julia versions

* Enable try/catch test

* Printing to find CI error

* Mark try test as broken on Julia 1.6

* Mark try test as broken on Julia 1.7

* Remove skipmissing test

* Remove print debugging

* Revert changes

* Remove version check

* updates for Enzyme changes

* remove higher order test
  • Loading branch information
jgreener64 authored Dec 8, 2024
1 parent 6ab27de commit 1d3b801
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,111 @@ end
Enzyme.API.strictAliasing!(true)
f10(x) = hypot(x, 2x)
@test autodiff(Reverse, f10, Active, Active(2.0))[1][1] == sqrt(5)
@test autodiff(Forward, f10, Duplicated(2.0, 1.0))[1] == sqrt(5)

f11(x) = x * sum(LinRange(x, 10.0, 6))
@test autodiff(Reverse, f11, Active, Active(2.0))[1][1] == 42
@test autodiff(Forward, f11, Duplicated(2.0, 1.0))[1] == 42

f12(x, k) = get(Dict(1 => 1.0, 2 => x, 3 => 3.0), k, 1.0)
@test autodiff(Reverse, f12, Active, Active(2.0), Const(2))[1] == (1.0, nothing)
@test autodiff(Forward, f12, Duplicated(2.0, 1.0), Const(2)) == (1.0,)
@test autodiff(Reverse, f12, Active, Active(2.0), Const(3))[1] == (0.0, nothing)
@test autodiff(Forward, f12, Duplicated(2.0, 1.0), Const(3)) == (0.0,)
@test autodiff(Reverse, f12, Active, Active(2.0), Const(4))[1] == (0.0, nothing)
@test autodiff(Forward, f12, Duplicated(2.0, 1.0), Const(4)) == (0.0,)

f13(x) = muladd(x, 3, x)
@test autodiff(Reverse, f13, Active, Active(2.0))[1][1] == 4
@test autodiff(Forward, f13, Duplicated(2.0, 1.0))[1] == 4

f14(x) = x * cmp(x, 3)
@test autodiff(Reverse, f14, Active, Active(2.0))[1][1] == -1
@test autodiff(Forward, f14, Duplicated(2.0, 1.0))[1] == -1

f15(x) = x * argmax([1.0, 3.0, 2.0])
@test autodiff(Reverse, f15, Active, Active(3.0))[1][1] == 2
@test autodiff(Forward, f15, Duplicated(3.0, 1.0))[1] == 2

f16(x) = evalpoly(2, (1, 2, x))
@test autodiff(Reverse, f16, Active, Active(3.0))[1][1] == 4
@test autodiff(Forward, f16, Duplicated(3.0, 1.0))[1] == 4

f17(x) = @evalpoly(2, 1, 2, x)
@test autodiff(Reverse, f17, Active, Active(3.0))[1][1] == 4
@test autodiff(Forward, f17, Duplicated(3.0, 1.0))[1] == 4

f18(x) = widemul(x, 5.0f0)
@test autodiff(Reverse, f18, Active, Active(2.0f0))[1][1] == 5
@test autodiff(Forward, f18, Duplicated(2.0f0, 1.0f0))[1] == 5

f19(x) = copysign(x, -x)
@test autodiff(Reverse, f19, Active, Active(2.0))[1][1] == -1
@test autodiff(Forward, f19, Duplicated(2.0, 1.0))[1] == -1

f20(x) = sum([ifelse(i > 5, i, zero(i)) for i in [x, 2x, 3x, 4x]])
@test autodiff(Reverse, f20, Active, Active(2.0))[1][1] == 7
@test autodiff(Forward, f20, Duplicated(2.0, 1.0))[1] == 7

function f21(x)
nt = (a=x, b=2x, c=3x)
return nt.c
end
@test autodiff(Reverse, f21, Active, Active(2.0))[1][1] == 3
@test autodiff(Forward, f21, Duplicated(2.0, 1.0))[1] == 3

f22(x) = sum(fill(x, (3, 3)))
@test autodiff(Reverse, f22, Active, Active(2.0))[1][1] == 9
@test autodiff(Forward, f22, Duplicated(2.0, 1.0))[1] == 9

function f23(x)
a = similar(rand(3, 3))
fill!(a, x)
return sum(a)
end
@test autodiff(Reverse, f23, Active, Active(2.0))[1][1] == 9
@test autodiff(Forward, f23, Duplicated(2.0, 1.0))[1] == 9

function f24(x)
try
return 3x
catch
return 2x
end
end
@test autodiff(Reverse, f24, Active, Active(2.0))[1][1] == 3
@test autodiff(Forward, f24, Duplicated(2.0, 1.0))[1] == 3

function f25(x)
try
sqrt(-1.0)
return 3x
catch
return 2x
end
end
@test autodiff(Reverse, f25, Active, Active(2.0))[1][1] == 2
@test autodiff(Forward, f25, Duplicated(2.0, 1.0))[1] == 2

f26(x) = circshift([1.0, 2x, 3.0], 1)[end]
@test autodiff(Reverse, f26, Active, Active(2.0))[1][1] == 2
@test autodiff(Forward, f26, Duplicated(2.0, 1.0))[1] == 2

f27(x) = repeat([x 3x], 3)[2, 2]
@test autodiff(Reverse, f27, Active, Active(2.0))[1][1] == 3
@test autodiff(Forward, f27, Duplicated(2.0, 1.0))[1] == 3

f28(x) = x * sum(trues(4, 3))
@test autodiff(Reverse, f28, Active, Active(2.0))[1][1] == 12
@test autodiff(Forward, f28, Duplicated(2.0, 1.0))[1] == 12

f29(x) = sum(Set([1.0, x, 2x, x]))
@test autodiff(Reverse, f29, Active, Active(2.0))[1][1] == 3
@test autodiff(Forward, f29, Duplicated(2.0, 1.0))[1] == 3

f30(x) = reverse([x 2.0 3x])[1]
@test autodiff(Reverse, f30, Active, Active(2.0))[1][1] == 3
@test autodiff(Forward, f30, Duplicated(2.0, 1.0))[1] == 3
end

function deadarg_pow(z::T, i) where {T<:Real}
Expand Down

0 comments on commit 1d3b801

Please sign in to comment.