From d1142833c20ce359b9035109a2494c4838868b7c Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 28 Mar 2025 23:25:12 +0000 Subject: [PATCH 1/8] add ForwardDiff@1 --- Project.toml | 4 ++-- test/Project.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index c196e748..61d63d73 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Bijectors" uuid = "76274a88-744f-5084-9051-94815aaf08c4" -version = "0.15.6" +version = "0.15.7" [deps] ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197" @@ -51,7 +51,7 @@ Distributions = "0.25.33" DistributionsAD = "0.6" DocStringExtensions = "0.9" EnzymeCore = "0.8.4" -ForwardDiff = "0.10" +ForwardDiff = "0.10, 1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" IrrationalConstants = "0.1, 0.2" diff --git a/test/Project.toml b/test/Project.toml index 3fe1adbc..d6fdddb6 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -36,7 +36,7 @@ Enzyme = "0.13.12" EnzymeTestUtils = "0.2.1" FillArrays = "1" FiniteDifferences = "0.11, 0.12" -ForwardDiff = "0.10.12" +ForwardDiff = "0.10.12, 1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" LazyArrays = "1, 2" From 8cd3f7b05db0ffda22eb933099a31dc6043e63dd Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Sun, 30 Mar 2025 23:35:19 +0100 Subject: [PATCH 2/8] Force ForwardDiff=1 in tests --- test/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index d6fdddb6..398756c4 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -36,7 +36,7 @@ Enzyme = "0.13.12" EnzymeTestUtils = "0.2.1" FillArrays = "1" FiniteDifferences = "0.11, 0.12" -ForwardDiff = "0.10.12, 1" +ForwardDiff = "1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" LazyArrays = "1, 2" From bdb1eb449986daefbd1a5004a313a7669631bf0b Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 31 Mar 2025 23:46:53 +0100 Subject: [PATCH 3/8] Don't test Dirichlet AD at non-differentiable points --- src/Bijectors.jl | 1 - test/interface.jl | 14 +++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Bijectors.jl b/src/Bijectors.jl index df7e3db0..27b18251 100644 --- a/src/Bijectors.jl +++ b/src/Bijectors.jl @@ -86,7 +86,6 @@ _eps(::Type{<:Integer}) = eps(Float64) function _clamp(x, a, b) T = promote_type(typeof(x), typeof(a), typeof(b)) - ϵ = _eps(T) clamped_x = ifelse(x < a, convert(T, a), ifelse(x > b, convert(T, b), x)) DEBUG && _debug("x = $x, bounds = $((a, b)), clamped_x = $clamped_x") return clamped_x diff --git a/test/interface.jl b/test/interface.jl index f5ac7457..d54006ae 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -145,8 +145,8 @@ end @testset "Multivariate" begin vector_dists = [ Dirichlet(2, 3), - Dirichlet([1000 * one(Float64), eps(Float64)]), - Dirichlet([eps(Float64), 1000 * one(Float64)]), + Dirichlet([10.0, 0.1]), + Dirichlet([0.1, 10.0]), MvNormal(randn(10), Diagonal(exp.(randn(10)))), MvLogNormal(MvNormal(randn(10), Diagonal(exp.(randn(10))))), Dirichlet([1000 * one(Float64), eps(Float64)]), @@ -173,15 +173,7 @@ end # similar to what we do in test/transform.jl for Dirichlet if dist isa Dirichlet b = Bijectors.SimplexBijector() - # HACK(torfjelde): Calling `rand(dist)` will sometimes lead to `[0.999..., 0.0]` - # which in turn will lead to differences between `ForwardDiff.jacobian` - # and `logabsdetjac` due to how we handle the boundary values in `SimplexBijector`. - # We therefore test the realizations _on_ the boundary rather if we're near the boundary. - x = if any(rand(dist) .> 0.9999) - [0.0, 1.0][sortperm(rand(dist))] - else - rand(dist) - end + x = rand(dist) y = b(x) @test b(param(x)) isa TrackedArray @test logabsdet(ForwardDiff.jacobian(b, x)[:, 1:(end - 1)])[1] ≈ From af33293d593e9ce006f02f05fe4ed513bc3b5659 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Apr 2025 19:46:12 +0100 Subject: [PATCH 4/8] Don't run duplicate tests --- test/interface.jl | 2 -- test/transform.jl | 2 -- 2 files changed, 4 deletions(-) diff --git a/test/interface.jl b/test/interface.jl index d54006ae..ac7ad53b 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -149,8 +149,6 @@ end Dirichlet([0.1, 10.0]), MvNormal(randn(10), Diagonal(exp.(randn(10)))), MvLogNormal(MvNormal(randn(10), Diagonal(exp.(randn(10))))), - Dirichlet([1000 * one(Float64), eps(Float64)]), - Dirichlet([eps(Float64), 1000 * one(Float64)]), MvTDist(1, randn(10), Matrix(Diagonal(exp.(randn(10))))), transformed(MvNormal(randn(10), Diagonal(exp.(randn(10))))), transformed(MvLogNormal(MvNormal(randn(10), Diagonal(exp.(randn(10)))))), diff --git a/test/transform.jl b/test/transform.jl index 85477535..6df85a9f 100644 --- a/test/transform.jl +++ b/test/transform.jl @@ -153,8 +153,6 @@ end Dirichlet([eps(Float64), 1000 * one(Float64)]), MvNormal(randn(10), Diagonal(exp.(randn(10)))), MvLogNormal(MvNormal(randn(10), Diagonal(exp.(randn(10))))), - Dirichlet([1000 * one(Float64), eps(Float64)]), - Dirichlet([eps(Float64), 1000 * one(Float64)]), ] for dist in vector_dists if dist isa Dirichlet From 55938c665c62230a3e99edf0452a145739ef77d8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Apr 2025 19:46:18 +0100 Subject: [PATCH 5/8] Use ForwardDiff 1.0.1, fix LKJCholesky Jacobian test --- Project.toml | 2 +- test/Project.toml | 2 +- test/transform.jl | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 61d63d73..6a8a8ce6 100644 --- a/Project.toml +++ b/Project.toml @@ -51,7 +51,7 @@ Distributions = "0.25.33" DistributionsAD = "0.6" DocStringExtensions = "0.9" EnzymeCore = "0.8.4" -ForwardDiff = "0.10, 1" +ForwardDiff = "0.10, 1.0.1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" IrrationalConstants = "0.1, 0.2" diff --git a/test/Project.toml b/test/Project.toml index 398756c4..d456846f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -36,7 +36,7 @@ Enzyme = "0.13.12" EnzymeTestUtils = "0.2.1" FillArrays = "1" FiniteDifferences = "0.11, 0.12" -ForwardDiff = "1" +ForwardDiff = "1.0.1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" LazyArrays = "1, 2" diff --git a/test/transform.jl b/test/transform.jl index 6df85a9f..46ec367d 100644 --- a/test/transform.jl +++ b/test/transform.jl @@ -238,14 +238,14 @@ end @testset "uplo: $uplo" for uplo in [:L, :U] dist = LKJCholesky(3, 1, uplo) single_sample_tests(dist) - x = rand(dist) - - inds = [ - LinearIndices(size(x))[I] for I in CartesianIndices(size(x)) if - (uplo === :L && I[2] < I[1]) || (uplo === :U && I[2] > I[1]) - ] J = ForwardDiff.jacobian(z -> link(dist, Cholesky(z, x.uplo, x.info)), x.UL) + # Remove columns of Jacobian that are all zero (i.e. those + # corresponding to entries above the diagonal for uplo = :U, or below + # the diagonal for uplo = :L). This slightly unscientific approach + # based on filter() is needed to handle both ForwardDiff 0.10 and 1 as + # the exact indices will differ for the two versions. + inds = filter(i -> !all(iszero, J[:, i]), 1:size(J, 2)) J = J[:, inds] logpdf_turing = logpdf_with_trans(dist, x, true) @test logpdf(dist, x) - _logabsdet(J) ≈ logpdf_turing From 9d305d822bd880a2aeb0d5206a96818944bcd047 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Apr 2025 19:52:01 +0100 Subject: [PATCH 6/8] Remove dead code --- test/transform.jl | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/test/transform.jl b/test/transform.jl index 46ec367d..b2a0e36a 100644 --- a/test/transform.jl +++ b/test/transform.jl @@ -157,26 +157,6 @@ end for dist in vector_dists if dist isa Dirichlet single_sample_tests(dist) - - # This should fail at the minute. Not sure what the correct way to test this is. - - # Workaround for intermittent test failures, result of `logpdf_with_trans(dist, x, true)` - # is incorrect for `x == [0.9999999999999998, 0.0]`: - x = - if params(dist) == - params(Dirichlet([1000 * one(Float64), eps(Float64)])) - [1.0, 0.0] - else - rand(dist) - end - # `Dirichlet` is no longer mapping between spaces of the same dimensionality, - # so the block below no longer works. - if !(dist isa Dirichlet) - logpdf_turing = logpdf_with_trans(dist, x, true) - J = ForwardDiff.jacobian(x -> link(dist, x), x) - @test logpdf(dist, x .+ ϵ) - _logabsdet(J) ≈ logpdf_turing - end - # Issue #12 stepsize = 1e10 dim = Bijectors.output_length(bijector(dist), length(dist)) From 6cf4d2414e47310e0ff021d91e796330c9b1e53d Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Apr 2025 22:15:04 +0100 Subject: [PATCH 7/8] Don't force FD=1 in test suite --- test/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index d456846f..5ca0660d 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -36,7 +36,7 @@ Enzyme = "0.13.12" EnzymeTestUtils = "0.2.1" FillArrays = "1" FiniteDifferences = "0.11, 0.12" -ForwardDiff = "1.0.1" +ForwardDiff = "0.10, 1.0.1" Functors = "0.1, 0.2, 0.3, 0.4, 0.5" InverseFunctions = "0.1" LazyArrays = "1, 2" From f209df83a2281e3023d176e1df25dfe976b446f8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 14 Apr 2025 16:54:52 +0100 Subject: [PATCH 8/8] Add reference to ForwardDiff issue --- test/transform.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/transform.jl b/test/transform.jl index b2a0e36a..bbcbef05 100644 --- a/test/transform.jl +++ b/test/transform.jl @@ -224,7 +224,8 @@ end # corresponding to entries above the diagonal for uplo = :U, or below # the diagonal for uplo = :L). This slightly unscientific approach # based on filter() is needed to handle both ForwardDiff 0.10 and 1 as - # the exact indices will differ for the two versions. + # the exact indices will differ for the two versions; see + # https://github.com/JuliaDiff/ForwardDiff.jl/issues/738. inds = filter(i -> !all(iszero, J[:, i]), 1:size(J, 2)) J = J[:, inds] logpdf_turing = logpdf_with_trans(dist, x, true)