Skip to content

Commit b69174e

Browse files
committed
tests, many broken
1 parent f0731dc commit b69174e

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

test/rulesets/Base/base.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@
192192
test_frule(Base.literal_pow, ^, 3.5, Val(3))
193193
test_rrule(Base.literal_pow, ^, 3.5, Val(3))
194194

195-
@testset "$x^$p" for x in [-1.5, 0.0, 3.5], p in [-3, -1, 0, 1, 3]
195+
@testset "regular: $x^$p" for x in [-1.5, 0.0, 3.5], p in [-3, -1, 0, 1, 3]
196196
x == 0 && p < 0 && continue
197-
test_frule(Base.literal_pow, ^, -1.5, Val(3))
198-
test_rrule(Base.literal_pow, ^, -1.5, Val(3))
197+
test_frule(Base.literal_pow, ^, x, Val(p))
198+
test_rrule(Base.literal_pow, ^, x, Val(p))
199199
end
200200

201-
@testset "singularities" begin
201+
@testset "singularities: 0^0, 0^-1, 0^-2" begin
202202
# Trivial one: 0^0 == 1 in Julia
203203
@test frule((1,1,1,1), Base.literal_pow, ^, 0.0, Val(0)) == ((0.0)^0, 0)
204204
@test rrule(Base.literal_pow, ^, 0.0, Val(0))[2](1.0)[3] == 0.0
@@ -220,6 +220,13 @@
220220
@test frule((1,1,1,1), Base.literal_pow, ^, -0.0, Val(-2)) == ((-0.0)^-2, +Inf)
221221
@test rrule(Base.literal_pow, ^, -0.0, Val(-2))[1] == (-0.0)^-2 == Inf
222222
@test rrule(Base.literal_pow, ^, -0.0, Val(-2))[2](1.0)[3] == +Inf
223+
224+
# Not singluar, but ^ messed these up: x^1 and x^2
225+
@test frule((1,1,1,1), Base.literal_pow, ^, 0.0, Val(2)) == (0.0, 0)
226+
@test rrule(Base.literal_pow, ^, 0.0, Val(2))[2](1.0)[3] == 0.0
227+
228+
@test frule((1,1,1,1), Base.literal_pow, ^, 0.0, Val(1)) == (0.0, 1)
229+
@test rrule(Base.literal_pow, ^, 0.0, Val(1))[2](1.0)[3] == 1.0
223230
end
224231
end
225232

test/rulesets/Base/fastmath_able.jl

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,62 @@ const FASTABLE_AST = quote
166166
# then x must be positive to avoid a DomainError
167167
test_frule(^, rand(T) + 3, rand(T) + 3)
168168
test_rrule(^, rand(T) + 3, rand(T) + 3)
169+
170+
T <: Real && S <: Real && continue
171+
test_frule(^, randn(T), rand(T))
172+
test_rrule(^, rand(T), rand(T))
169173
end
174+
170175
# @testset "^(x::$T, $p::Int)" for T in (Float64, ComplexF64), p in -2:2
171-
# x = rand(T) .+ 3
176+
# test_frule(^, randn(T) + 3, p ⊢ NoTangent()) # this doesn't just skip p's tangent
177+
# test_rrule(^, randn(T) + 3, p ⊢ NoTangent())
172178
# end
173179

180+
@testset "^(x::Float64, p::$S) near x=0, p=1,0,-1,-2" for S in (Int, Float64)
181+
p = S(+2)
182+
@test frule((1,1,1), ^, 0.0, p)[1] == 0
183+
@test_broken frule((1,1,1), ^, 0.0, p)[2] == 0
184+
@test rrule(^, 0.0, p)[1] == 0
185+
@test unthunk(rrule(^, 0.0, p)[2](1.0)[2]) == 0
186+
187+
# Identity function x^1, at zero
188+
p = S(+1)
189+
@test frule((1,1,1), ^, 0.0, p)[1] == 0
190+
@test_broken frule((1,1,1), ^, 0.0, p)[2] == 1
191+
@test rrule(^, 0.0, p)[1] == 0
192+
@test unthunk(rrule(^, 0.0, p)[2](1.0)[2]) == 1
193+
194+
# Trivial singularity: 0^0 == 1 in Julia
195+
p = S(0)
196+
@test_skip frule((1,1,1), ^, 0.0, p)[1] == (0.0)^0
197+
@test_broken frule((1,1,1), ^, 0.0, p)[2] == 0
198+
@test_broken unthunk(rrule(^, 0.0, p)[2](1.0)[3]) == 0.0
199+
200+
# Odd power, 1/x
201+
p = S(-1)
202+
@test_skip frule((1,1,1), ^, 0.0, p)[1] == (0.0)^-1
203+
@test_broken frule((1,1,1), ^, 0.0, p)[2] == -Inf
204+
@test_skip rrule(^, 0.0, p)[1] == (0.0)^-1 == Inf
205+
@test unthunk(rrule(^, 0.0, p)[2](1.0)[2]) == -Inf
206+
207+
@test_skip frule((1,1,1), ^, -0.0, p)[1] == (-0.0)^-1
208+
@test_broken frule((1,1,1), ^, -0.0, p)[2] == -Inf
209+
@test_skip rrule(^, -0.0, p)[1] == (-0.0)^-1 == -Inf
210+
@test unthunk(rrule(^, -0.0, p)[2](1.0)[2]) == -Inf
211+
212+
# Even power, 1/x^2
213+
p = S(-2)
214+
@test_skip frule((1,1,1), ^, 0.0, p)[1] == (0.0)^-2
215+
@test_broken frule((1,1,1), ^, 0.0, p)[2] == -Inf
216+
@test_skip rrule(^, 0.0, p)[1] == (0.0)^-2 == Inf
217+
@test unthunk(rrule(^, 0.0, p)[2](1.0)[2]) == -Inf
218+
219+
@test_skip frule((1,1,1), ^, -0.0, p)[1] == (-0.0)^-2
220+
@test_broken frule((1,1,1), ^, -0.0, p)[2] == +Inf
221+
@test_skip rrule(^, -0.0, p)[1] == (-0.0)^-2 == Inf
222+
@test unthunk(rrule(^, -0.0, p)[2](1.0)[2]) == +Inf
223+
end
224+
174225
# T <: Real && @testset "discontinuity for ^(x::Real, n::Int) when x ≤ 0" begin
175226
# # finite differences doesn't work for x < 0, so we check manually
176227
# x = -rand(T) .- 3
@@ -189,15 +240,6 @@ const FASTABLE_AST = quote
189240
# @test ∂y ≈ 0
190241
# end
191242
# end
192-
193-
# @testset "edge cases with ^" begin
194-
# # FIXME
195-
# @test_skip test_frule(^, 0.0, rand() + 3 ⊢ NoTangent(); fdm=forward_fdm(5,1))
196-
# test_rrule(^, 0.0, rand() + 3; fdm=forward_fdm(5,1))
197-
198-
# test_frule(^, 0.0, 1.0 ⊢ NoTangent(); fdm=forward_fdm(5,1))
199-
# test_rrule(^, 0.0, 1.0; fdm=forward_fdm(5,1))
200-
# end
201243
end
202244

203245
@testset "sign" begin

0 commit comments

Comments
 (0)