|
| 1 | +using FiniteDiff, LinearAlgebra, SparseArrays, StaticArrays, Test |
| 2 | + |
| 3 | +# Tests for issue #213: caches must be safe to reuse at a new x, regardless of |
| 4 | +# how their internal scratch fields (e.g. JacobianCache.x1) were initialized. |
| 5 | +# The original symptom was DI building a JacobianCache from `similar(x)` |
| 6 | +# (uninitialized) and getting junk Jacobians in :central mode. |
| 7 | + |
| 8 | +const J_REF = [2.0 0.0; 0.0 3.0; 4.0 0.0] |
| 9 | +foo_oop(x) = [2x[1], 3x[2], 4x[1]] |
| 10 | +foo_iip!(y, x) = (y[1] = 2x[1]; y[2] = 3x[2]; y[3] = 4x[1]; y) |
| 11 | + |
| 12 | +# A non-zero point where the bug becomes obvious — at zeros(2) the junk in x1 |
| 13 | +# cancels by symmetry on this affine function and hides the issue. |
| 14 | +const X_TEST = [1.0, 2.0] |
| 15 | + |
| 16 | +"""Build a JacobianCache whose scratch fields are explicitly poisoned with a |
| 17 | +huge value, mimicking what happens when a caller hands FiniteDiff a cache |
| 18 | +allocated via `similar(x)` (which gives uninitialized memory).""" |
| 19 | +function poisoned_jcache(fdtype; x_template = X_TEST, y_template = foo_oop(X_TEST), poison = 1.0e10) |
| 20 | + x1 = fill(poison, length(x_template)) |
| 21 | + fx = fill(poison, length(y_template)) |
| 22 | + if fdtype === Val(:complex) |
| 23 | + FiniteDiff.JacobianCache(x1, fx, nothing, fdtype) |
| 24 | + else |
| 25 | + fx1 = fill(poison, length(y_template)) |
| 26 | + FiniteDiff.JacobianCache(x1, fx, fx1, fdtype) |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +@testset "Cache reuse safety (issue #213)" begin |
| 31 | + |
| 32 | +@testset "JacobianCache out-of-place reuse" begin |
| 33 | + @testset "fresh cache reused at new x" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 34 | + cache = FiniteDiff.JacobianCache(zeros(2), zeros(3), fdtype) |
| 35 | + # Exercise the cache once at x_old, then reuse at X_TEST. |
| 36 | + FiniteDiff.finite_difference_jacobian(foo_oop, zeros(2), cache) |
| 37 | + J = FiniteDiff.finite_difference_jacobian(foo_oop, X_TEST, cache) |
| 38 | + @test J ≈ J_REF atol=1e-6 |
| 39 | + end |
| 40 | + |
| 41 | + @testset "cache built with garbage x1/fx ($(fdtype))" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 42 | + cache = poisoned_jcache(fdtype) |
| 43 | + J = FiniteDiff.finite_difference_jacobian(foo_oop, X_TEST, cache) |
| 44 | + @test J ≈ J_REF atol=1e-6 |
| 45 | + end |
| 46 | + |
| 47 | + @testset "cache built with garbage x1/fx + sparsity ($(fdtype))" for fdtype in (Val(:forward), Val(:central)) |
| 48 | + spJ = sparse(J_REF) |
| 49 | + cache = poisoned_jcache(fdtype) |
| 50 | + J = FiniteDiff.finite_difference_jacobian(foo_oop, X_TEST, cache; |
| 51 | + sparsity = spJ, jac_prototype = spJ) |
| 52 | + @test Matrix(J) ≈ J_REF atol=1e-6 |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +@testset "JacobianCache in-place reuse" begin |
| 57 | + @testset "fresh cache reused at new x ($(fdtype))" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 58 | + cache = FiniteDiff.JacobianCache(zeros(2), zeros(3), fdtype) |
| 59 | + J = zeros(3, 2) |
| 60 | + FiniteDiff.finite_difference_jacobian!(J, foo_iip!, zeros(2), cache) |
| 61 | + fill!(J, 0) |
| 62 | + FiniteDiff.finite_difference_jacobian!(J, foo_iip!, X_TEST, cache) |
| 63 | + @test J ≈ J_REF atol=1e-6 |
| 64 | + end |
| 65 | + |
| 66 | + @testset "cache built with garbage x1/fx ($(fdtype))" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 67 | + cache = poisoned_jcache(fdtype) |
| 68 | + J = zeros(3, 2) |
| 69 | + FiniteDiff.finite_difference_jacobian!(J, foo_iip!, X_TEST, cache) |
| 70 | + @test J ≈ J_REF atol=1e-6 |
| 71 | + end |
| 72 | + |
| 73 | + @testset "in-place :central must not mutate x (sparse path)" begin |
| 74 | + spJ = sparse(J_REF) |
| 75 | + cache = poisoned_jcache(Val(:central)) |
| 76 | + J = zeros(3, 2) |
| 77 | + x = copy(X_TEST) |
| 78 | + x_orig = copy(x) |
| 79 | + FiniteDiff.finite_difference_jacobian!(J, foo_iip!, x, cache; |
| 80 | + sparsity = spJ, colorvec = 1:2) |
| 81 | + @test Matrix(J) ≈ J_REF atol=1e-6 |
| 82 | + @test x == x_orig # x should be restored / unmutated |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +@testset "GradientCache reuse" begin |
| 87 | + # `:complex` requires an analytic function, so don't use abs2 here. |
| 88 | + g(x) = x[1]^2 + x[2]^2 + x[3]^2 |
| 89 | + grad_ref = [2.0, 4.0, 6.0] |
| 90 | + x = [1.0, 2.0, 3.0] |
| 91 | + |
| 92 | + # Use the allocating constructor so buffer types are correct, then poison |
| 93 | + # any non-`nothing` buffer to simulate a stale cache. |
| 94 | + @testset "vector → scalar with poisoned cache ($(fdtype))" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 95 | + df = zeros(3) |
| 96 | + cache = FiniteDiff.GradientCache(df, x, fdtype, Float64, Val(false)) |
| 97 | + for fld in (:c1, :c2, :c3) |
| 98 | + buf = getfield(cache, fld) |
| 99 | + buf isa AbstractArray && fill!(buf, 1e10) |
| 100 | + end |
| 101 | + grad = zeros(3) |
| 102 | + FiniteDiff.finite_difference_gradient!(grad, g, x, cache) |
| 103 | + @test grad ≈ grad_ref atol=1e-5 |
| 104 | + end |
| 105 | + |
| 106 | + @testset "fresh cache reused at new x ($(fdtype))" for fdtype in (Val(:forward), Val(:central)) |
| 107 | + cache = FiniteDiff.GradientCache(zeros(3), zeros(3), fdtype) |
| 108 | + grad = zeros(3) |
| 109 | + FiniteDiff.finite_difference_gradient!(grad, g, zeros(3), cache) |
| 110 | + FiniteDiff.finite_difference_gradient!(grad, g, x, cache) |
| 111 | + @test grad ≈ grad_ref atol=1e-5 |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +@testset "JVPCache reuse" begin |
| 116 | + foo_iip!_3 = (y, x) -> (y[1] = 2x[1]; y[2] = 3x[2]; y[3] = 4x[1]; y) |
| 117 | + v = [1.0, 0.0] |
| 118 | + jvp_ref = J_REF * v |
| 119 | + |
| 120 | + @testset "garbage cache ($(fdtype))" for fdtype in (Val(:forward), Val(:central)) |
| 121 | + x1 = fill(1e10, 2) |
| 122 | + fx1 = fill(1e10, 3) |
| 123 | + cache = FiniteDiff.JVPCache(x1, fx1, fdtype) |
| 124 | + jvp = zeros(3) |
| 125 | + FiniteDiff.finite_difference_jvp!(jvp, foo_iip!_3, X_TEST, v, cache) |
| 126 | + @test jvp ≈ jvp_ref atol=1e-6 |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | +@testset "HessianCache reuse" begin |
| 131 | + h(x) = x[1]^2 + 2 * x[2]^2 |
| 132 | + H_ref = [2.0 0.0; 0.0 4.0] |
| 133 | + |
| 134 | + xpp = fill(1e10, 2); xpm = fill(1e10, 2); xmp = fill(1e10, 2); xmm = fill(1e10, 2) |
| 135 | + cache = FiniteDiff.HessianCache(xpp, xpm, xmp, xmm, Val(:hcentral), Val(true)) |
| 136 | + H = zeros(2, 2) |
| 137 | + FiniteDiff.finite_difference_hessian!(H, h, X_TEST, cache) |
| 138 | + @test H ≈ H_ref atol=1e-3 |
| 139 | +end |
| 140 | + |
| 141 | +# Mirrors the failure mode from JuliaDiff/DifferentiationInterface.jl#983: a |
| 142 | +# caller building a cache with `similar(x)` fields and then asking for a |
| 143 | +# Jacobian via the non-allocating entry point. |
| 144 | +@testset "DI-style similar() cache (issue #983 reproduction)" begin |
| 145 | + foo(x) = [2x[1], 3x[2], 4x[1]] |
| 146 | + y = foo(X_TEST) |
| 147 | + |
| 148 | + @testset "$(fdtype)" for fdtype in (Val(:forward), Val(:central), Val(:complex)) |
| 149 | + x1 = similar(X_TEST) |
| 150 | + fx = similar(y) |
| 151 | + if fdtype === Val(:complex) |
| 152 | + cache = FiniteDiff.JacobianCache(x1, fx, nothing, fdtype) |
| 153 | + else |
| 154 | + fx1 = similar(y) |
| 155 | + cache = FiniteDiff.JacobianCache(x1, fx, fx1, fdtype) |
| 156 | + end |
| 157 | + J = FiniteDiff.finite_difference_jacobian(foo, X_TEST, cache) |
| 158 | + @test J ≈ J_REF atol=1e-6 |
| 159 | + end |
| 160 | +end |
| 161 | + |
| 162 | +end # outer testset |
0 commit comments