Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undef appeared during prepre_jacobian for Dual of BigFloat #650

Closed
ErikQQY opened this issue Dec 1, 2024 · 3 comments
Closed

undef appeared during prepre_jacobian for Dual of BigFloat #650

ErikQQY opened this issue Dec 1, 2024 · 3 comments

Comments

@ErikQQY
Copy link
Contributor

ErikQQY commented Dec 1, 2024

Also, find this issue while implementing SciML/BoundaryValueDiffEq.jl#258

Actually, I narrowed this down from BoundaryValueDiffEq.jl to NonlinearSolve.jl, and finally to DI, so this may affect NonlinearSolve as well. This MWE is just proof of the idea and may be not meaningful, but can still represent the issue here. When the x in prepare_jacobian is a Dual number of BigFloat, there would be undef in the output.

using ForwardDiff, DifferentiationInterface, SparseMatrixColorings, ADTypes
const DI = DifferentiationInterface

backend = AutoSparse(
    AutoForwardDiff(),
    sparsity_detector = ADTypes.KnownJacobianSparsityDetector(ones(3, 3)),
    coloring_algorithm = ConstantColoringAlgorithm(ones(3, 3), ones(Int64, 3))
)
a = ForwardDiff.Dual{:tag}(BigFloat(1.0), BigFloat(1.0))
u0 = [a for _ in 1:3]
jac_cache = DI.prepare_jacobian(nothing, BigFloat.([1.0,1.0,1.0]), backend, u0)
DifferentiationInterfaceSparseMatrixColoringsExt.PushforwardSparseJacobianPrep{DifferentiationInterface.BatchSizeSettings{1, true, true}, SparseMatrixColorings.ColumnColoringResult{Matrix{Float64}, SparseMatrixColorings.BipartiteGraph{Int64}, Vector{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}}, Matrix{BigFloat}, Vector{Tuple{Vector{ForwardDiff.Dual{:tag, BigFloat, 1}}}}, Vector{Tuple{Vector{BigFloat}}}, DifferentiationInterfaceForwardDiffExt.ForwardDiffTwoArgPushforwardPrep{Nothing, Vector{ForwardDiff.Dual{Nothing, ForwardDiff.Dual{:tag, BigFloat, 1}, 1}}, Vector{ForwardDiff.Dual{Nothing, BigFloat, 1}}}}(DifferentiationInterface.BatchSizeSettings{1, true, true}(1, 1, 0), SparseMatrixColorings.ColumnColoringResult{Matrix{Float64}, SparseMatrixColorings.BipartiteGraph{Int64}, Vector{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}}([1.0 1.0 1.0; 1.0 1.0 1.0; 1.0 1.0 1.0], SparseMatrixColorings.BipartiteGraph{Int64}(SparseMatrixColorings.SparsityPatternCSC{Int64}(3, 3, [1, 4, 7, 10], [1, 2, 3, 1, 2, 3, 1, 2, 3]), SparseMatrixColorings.SparsityPatternCSC{Int64}(3, 3, [1, 4, 7, 10], [1, 2, 3, 1, 2, 3, 1, 2, 3])), [1, 1, 1], SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}[[1, 2, 3]], [1, 2, 3, 1, 2, 3, 1, 2, 3]), BigFloat[#undef; #undef; #undef;;], Tuple{Vector{ForwardDiff.Dual{:tag, BigFloat, 1}}}[([Dual{:tag}(1.0,0.0), Dual{:tag}(1.0,0.0), Dual{:tag}(1.0,0.0)],)], Tuple{Vector{BigFloat}}[([#undef, #undef, #undef],)], DifferentiationInterfaceForwardDiffExt.ForwardDiffTwoArgPushforwardPrep{Nothing, Vector{ForwardDiff.Dual{Nothing, ForwardDiff.Dual{:tag, BigFloat, 1}, 1}}, Vector{ForwardDiff.Dual{Nothing, BigFloat, 1}}}(ForwardDiff.Dual{Nothing, ForwardDiff.Dual{:tag, BigFloat, 1}, 1}[#undef, #undef, #undef], ForwardDiff.Dual{Nothing, BigFloat, 1}[#undef, #undef, #undef]))
image

These undef make the afterward Jacobian computation throw errors.

@gdalle
Copy link
Member

gdalle commented Dec 1, 2024

Hi @ErikQQY! Thanks for reporting this.
I think these #undefs are supposed to be there, because the Jacobian preparation initializes some storage with similar , and BigFloat is not a bitstype so #undef is what you get as elements of an uninitialized array.
Can you give me an example where the Jacobian itself errors? I tried the following and it works:

DI.jacobian(copyto!, similar(u0), backend, u0)

Note that your version would fail

DI.jacobian(copyto!, BigFloat.([1.0, 1.0, 1.0]), backend, u0)

but that's only because you initialize y with the wrong type (BigFloat instead of Dual{BigFloat}). Does that make sense?

@gdalle
Copy link
Member

gdalle commented Dec 1, 2024

Also note that the constant coloring you give in your example is not correct. With a dense Jacobian sparsity pattern like ones(3, 3), you need to give each column a different color to be able to recover all values.

@ErikQQY
Copy link
Contributor Author

ErikQQY commented Dec 2, 2024

Oh, after further investigation, it turns out that the error is on the NonlinearSolve side, which initializes the storage with similar on u0(direct similar on Dual of BigFloat would be undef), and passes the undef y with u0 to prepare_jacobian, sorry for the false alarm and thank you for your patient response🙂

@ErikQQY ErikQQY closed this as completed Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants