|
| 1 | +## Hessian tests |
| 2 | +using SparsityDetection, SparseDiffTools |
| 3 | +using ForwardDiff |
| 4 | +using LinearAlgebra, SparseArrays |
| 5 | + |
| 6 | +function fscalar(x) |
| 7 | + return -dot(x, x) + 2 * x[2] * x[3]^2 |
| 8 | +end |
| 9 | + |
| 10 | +x = randn(5) |
| 11 | +sparsity = hessian_sparsity(fscalar, x) |
| 12 | +colors = matrix_colors(tril(sparsity)) |
| 13 | +ncolors = maximum(colors) |
| 14 | +D = hcat([float.(i .== colors) for i in 1:ncolors]...) |
| 15 | +buffer = similar(D) |
| 16 | +G1 = zero(x) |
| 17 | +G2 = zero(x) |
| 18 | + |
| 19 | +buffers_tup = SparseDiffTools.make_hessian_buffers(colors, x) |
| 20 | +@test buffers_tup.ncolors == ncolors |
| 21 | +@test buffers_tup.D == D |
| 22 | +@test size(buffers_tup.buffer) == size(buffer) |
| 23 | +@test eltype(buffers_tup.buffer) == eltype(buffer) |
| 24 | +@test typeof(buffers_tup.buffer) == typeof(buffer) |
| 25 | +@test size(buffers_tup.G1) == size(G1) |
| 26 | +@test eltype(buffers_tup.G1) == eltype(G1) |
| 27 | +@test size(buffers_tup.G2) == size(G2) |
| 28 | +@test eltype(buffers_tup.G2) == eltype(G2) |
| 29 | + |
| 30 | + |
| 31 | +gconfig = ForwardDiff.GradientConfig(fscalar, x) |
| 32 | +g(x) = ForwardDiff.gradient(fscalar, x) # allocating |
| 33 | +g!(G, x, gconfig) = ForwardDiff.gradient!(G, fscalar, x, gconfig) # non-allocating |
| 34 | + |
| 35 | +hescache1 = ForwardColorHesCache(sparsity, colors, ncolors, D, buffer, g!, gconfig, G1, G2) |
| 36 | +hescache2 = ForwardColorHesCache(fscalar, x, colors, sparsity, g!) |
| 37 | +hescache3 = ForwardColorHesCache(fscalar, x, colors, sparsity) |
| 38 | +# custom gradient function |
| 39 | +hescache4 = ForwardColorHesCache(fscalar, x, colors, sparsity, |
| 40 | + (G, x) -> ForwardDiff.gradient!(G, fscalar, x)) |
| 41 | +hescache5 = ForwardColorHesCache(fscalar, x) |
| 42 | +# custom gradient has to have 2 or 3 arguments... |
| 43 | +@test_throws ArgumentError ForwardColorHesCache(fscalar, x, colors, sparsity, (a) -> 1.0) |
| 44 | +@test_throws ArgumentError ForwardColorHesCache(fscalar, x, colors, sparsity, (a, b, c, d) -> 1.0) |
| 45 | +# ...and needs to accept (Vector, Vector, ForwardDiff.GradientConfig) |
| 46 | +@test_throws ArgumentError ForwardColorHesCache(fscalar, x, colors, sparsity, (a::Int, b::Int) -> 1.0,) |
| 47 | +@test_throws ArgumentError ForwardColorHesCache(fscalar, x, colors, sparsity, (a::Int, b::Int, c::Int) -> 1.0) |
| 48 | + |
| 49 | +for name in [:sparsity, :colors, :ncolors, :D] |
| 50 | + @eval @test hescache1.$name == hescache2.$name |
| 51 | + @eval @test hescache1.$name == hescache3.$name |
| 52 | + @eval @test hescache1.$name == hescache4.$name |
| 53 | + # hescache5 is the default dense version, so only first axis will match |
| 54 | + @eval @test size(hescache1.$name, 1) == size(hescache5.$name, 1) |
| 55 | +end |
| 56 | +for name in [:buffer, :G1, :G2] |
| 57 | + @eval @test size(hescache1.$name) == size(hescache2.$name) |
| 58 | + @eval @test size(hescache1.$name) == size(hescache3.$name) |
| 59 | + @eval @test size(hescache1.$name) == size(hescache4.$name) |
| 60 | + # hescache5 is the default dense version, so only first axis will match |
| 61 | + @eval @test size(hescache1.$name, 1) == size(hescache5.$name, 1) |
| 62 | + |
| 63 | + @eval @test eltype(hescache1.$name) == eltype(hescache2.$name) |
| 64 | + @eval @test eltype(hescache1.$name) == eltype(hescache3.$name) |
| 65 | + @eval @test eltype(hescache1.$name) == eltype(hescache4.$name) |
| 66 | + @eval @test eltype(hescache1.$name) == eltype(hescache5.$name) |
| 67 | +end |
| 68 | + |
| 69 | +Hforward = ForwardDiff.hessian(fscalar, x) |
| 70 | +for (i, hescache) in enumerate([hescache1, hescache2, hescache3, hescache4, hescache5]) |
| 71 | + H = numauto_color_hessian(fscalar, x, colors, sparsity) |
| 72 | + H1 = numauto_color_hessian(fscalar, x, hescache) |
| 73 | + H2 = numauto_color_hessian(fscalar, x) |
| 74 | + @test all(isapprox.(Hforward, H, rtol=1e-6)) |
| 75 | + @test all(isapprox.(H, H1, rtol=1e-6)) |
| 76 | + @test all(isapprox.(H2, H1, rtol=1e-6)) |
| 77 | + |
| 78 | + H1 = similar(H) |
| 79 | + numauto_color_hessian!(H1, fscalar, x, collect(hescache.colors), hescache.sparsity) |
| 80 | + @test all(isapprox.(H1, H)) |
| 81 | + |
| 82 | + numauto_color_hessian!(H2, fscalar, x) |
| 83 | + @test all(isapprox.(H2, H)) |
| 84 | + |
| 85 | + numauto_color_hessian!(H1, fscalar, x, hescache) |
| 86 | + @test all(isapprox.(H1, H)) |
| 87 | + |
| 88 | + numauto_color_hessian!(H1, fscalar, x, hescache, safe=false) |
| 89 | + @test all(isapprox.(H1, H)) |
| 90 | + |
| 91 | + # the following tests usually pass, but once in a while don't (it's not a big difference |
| 92 | + # in timing on these small matrices, and sometimes its less than the timing variability). |
| 93 | + # Commenting out for now to avoid rare stochastic test failures. |
| 94 | + # # confirm unsafe is faster |
| 95 | + # t_safe = minimum(@elapsed(numauto_color_hessian!(H1, fscalar, x, hescache, safe=true)) |
| 96 | + # for _ in 1:100) |
| 97 | + # t_unsafe = minimum(@elapsed(numauto_color_hessian!(H1, fscalar, x, hescache, safe=false)) |
| 98 | + # for _ in 1:100) |
| 99 | + # @test t_unsafe <= t_safe |
| 100 | +end |
0 commit comments