Skip to content

Safely initialize the inplace Jacobian cache #160

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JamesWrigley
Copy link

This fixes support for BigFloat in LsqFit.jl. Making a copy instead of similar ensures that the elements aren't undef, which is what previous versions did.

CC @gdalle

@gdalle
Copy link
Contributor

gdalle commented Apr 6, 2025

Do you have an MWE with the error you are encountering? I have a strong feeling that the bug is located downstream, because using similar here is not incorrect

Copy link
Contributor

@gdalle gdalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, on second thought this looks like a correct patch, maybe we should add a test that used to fail? Or at least a comment pointing to the rationale?

@@ -78,7 +78,7 @@ function OnceDifferentiable(f, x_seed::AbstractArray, F::AbstractArray, DF::Abst
fdfF = make_fdf(f, x_seed, F)
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
else
F2 = similar(F)
F2 = copy(F)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is F2 mutated in the jacobian! call below? Would it still work if F2 can't be mutated anymore? similar is guaranteed to create a mutable array whereas copy may return a static array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
F2 = copy(F)
F2 = copyto!(similar(F), F)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in facf227. I attempted to add a test but couldn't figure out how to make it fail outside of LsqFit so I added a comment instead.

This fixes support for BigFloat in LsqFit.jl, ForwardDiff.jl requires that the
array is initialized.
@pkofod
Copy link
Member

pkofod commented Apr 7, 2025

Thanks. This PR needs a test for the correct behavior.

@devmotion
Copy link
Contributor

This workaround might not be necessary: JuliaDiff/ForwardDiff.jl#743

@pkofod
Copy link
Member

pkofod commented Apr 9, 2025

Would definitely be nice if we didn't have to rely on fixes here

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

Successfully merging this pull request may close these issues.

4 participants