|
1 | 1 | ## Sparse Jacobian tests |
2 | | -using SparseDiffTools, Symbolics, ForwardDiff, LinearAlgebra, SparseArrays, Zygote, Enzyme |
3 | | -using Test |
| 2 | +using AllocCheck, SparseDiffTools, |
| 3 | + Symbolics, ForwardDiff, LinearAlgebra, SparseArrays, Zygote, Enzyme, Test, StaticArrays |
4 | 4 |
|
5 | 5 | @views function fdiff(y, x) # in-place |
6 | 6 | L = length(x) |
@@ -163,3 +163,29 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa |
163 | 163 | end |
164 | 164 | end |
165 | 165 | end |
| 166 | + |
| 167 | +# Testing that the non-sparse jacobian's are non-allocating. |
| 168 | +fvcat(x) = vcat(x, x) |
| 169 | + |
| 170 | +x_sa = @SVector randn(Float32, 10); |
| 171 | + |
| 172 | +J_true_sa = ForwardDiff.jacobian(fvcat, x_sa) |
| 173 | + |
| 174 | +@check_allocs function __sparse_jacobian_no_allocs(ad, sd, f::F, x) where {F} |
| 175 | + return sparse_jacobian(ad, sd, f, x) |
| 176 | +end |
| 177 | + |
| 178 | +@testset "Static Arrays" begin |
| 179 | + @testset "No Allocations: $(difftype)" for difftype in (AutoSparseForwardDiff(), |
| 180 | + AutoForwardDiff()) |
| 181 | + J = __sparse_jacobian_no_allocs(difftype, NoSparsityDetection(), fvcat, x_sa) |
| 182 | + @test J ≈ J_true_sa |
| 183 | + end |
| 184 | + |
| 185 | + @testset "Other Backends: $(difftype)" for difftype in (AutoSparseZygote(), |
| 186 | + AutoZygote(), AutoSparseEnzyme(), AutoEnzyme(), AutoSparseFiniteDiff(), |
| 187 | + AutoFiniteDiff()) |
| 188 | + J = sparse_jacobian(difftype, NoSparsityDetection(), fvcat, x_sa) |
| 189 | + @test J ≈ J_true_sa |
| 190 | + end |
| 191 | +end |
0 commit comments