@@ -30,5 +30,35 @@ function (alg::PrecomputedJacobianColorvec)(ad::AbstractSparseADType, args...; k
3030 return MatrixColoringResult (colorvec, J, nz_rows, nz_cols)
3131end
3232
33+ # Approximate Jacobian Sparsity Detection
34+ # # Right now we hardcode it to use `ForwardDiff`
35+ function (alg:: ApproximateJacobianSparsity )(ad:: AbstractSparseADType , f, x; fx = nothing ,
36+ kwargs... )
37+ @unpack ntrials, rng = alg
38+ fx = fx === nothing ? f (x) : fx
39+ J = fill! (similar (fx, length (fx), length (x)), 0 )
40+ cfg = ForwardDiff. JacobianConfig (f, x)
41+ for _ in 1 : ntrials
42+ x_ = similar (x)
43+ randn! (rng, x_)
44+ J .+ = abs .(ForwardDiff. jacobian (f, x_, cfg))
45+ end
46+ return (JacPrototypeSparsityDetection (; jac_prototype = sparse (J), alg. alg))(ad, f, x;
47+ fx, kwargs... )
48+ end
49+
50+ function (alg:: ApproximateJacobianSparsity )(ad:: AbstractSparseADType , f!, fx, x; kwargs... )
51+ @unpack ntrials, rng = alg
52+ cfg = ForwardDiff. JacobianConfig (f!, fx, x)
53+ J = fill! (similar (fx, length (fx), length (x)), 0 )
54+ for _ in 1 : ntrials
55+ x_ = similar (x)
56+ randn! (rng, x_)
57+ J .+ = abs .(ForwardDiff. jacobian (f!, fx, x_, cfg))
58+ end
59+ return (JacPrototypeSparsityDetection (; jac_prototype = sparse (J), alg. alg))(ad, f!, fx,
60+ x; kwargs... )
61+ end
62+
3363# TODO : Heuristics to decide whether to use Sparse Differentiation or not
3464# Simple Idea: Check min(max(colorvec_cols), max(colorvec_rows))
0 commit comments