Skip to content

Add support for Jacobian-vector products#168

Merged
pkofod merged 1 commit intoJuliaNLSolvers:masterfrom
devmotion:dmw/jvp
Jan 7, 2026
Merged

Add support for Jacobian-vector products#168
pkofod merged 1 commit intoJuliaNLSolvers:masterfrom
devmotion:dmw/jvp

Conversation

@devmotion
Copy link
Contributor

A first incomplete sketch (the make_... functions and tests are missing).

I tried to avoid creating a new ...Differentiable type, I think it's a bit unfortunate that there exists both TwiceDifferentiable and TwiceDifferentiableHV.

@pkofod
Copy link
Member

pkofod commented Nov 12, 2025

TwiceDifferentiableHV

yes, I definitely agree. I cannot remember why it is that way. In NLSolvers.jl I have hv in the scalar objective https://github.com/JuliaNLSolvers/NLSolvers.jl/blob/e1afd319c26086b348283d5225c8eb5ea8151612/src/objectives.jl#L12

@pkofod
Copy link
Member

pkofod commented Nov 13, 2025

I tried to avoid creating a new ...Differentiable type, I think it's a bit unfortunate that there exists both TwiceDifferentiable and TwiceDifferentiableHV.

we could even add hv to the TwiceDiff interface immediately if we wanted, keep the other one around for legacy reasons (or don't since all of this will be breaking anyway)

@codecov
Copy link

codecov bot commented Nov 21, 2025

Codecov Report

❌ Patch coverage is 78.37079% with 77 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.41%. Comparing base (7c56890) to head (7997fcd).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/interface.jl 67.22% 59 Missing ⚠️
src/objective_types/inplace_factory.jl 0.00% 13 Missing ⚠️
src/objective_types/incomplete.jl 93.33% 4 Missing ⚠️
src/objective_types/nondifferentiable.jl 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #168      +/-   ##
==========================================
- Coverage   93.72%   88.41%   -5.31%     
==========================================
  Files          10        9       -1     
  Lines         701      872     +171     
==========================================
+ Hits          657      771     +114     
- Misses         44      101      +57     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devmotion
Copy link
Contributor Author

@pkofod it took me quite some time to fix the constructors of OnceDifferentiable. Initially, I wanted to add support for jvp etc. and the caches as well but it's extremely messy. I wonder if we should basically get rid of most of them, at least all of them that allow for more than a single function argument. IMO it's much cleaner and easier for us to handle if users input multiple functions by using the only_.../InplaceObjective/NotInplaceObjective API. Then there's no ambiguity issues and weird call paths etc.

Existing tests seem to pass now. I'll now add tests for the new functionality.

Comment on lines +45 to +55
# Aliases with `j` (Jacobian)
# Do not apply to functions that involve Hessians!
# Maybe we should clearly separate functions that take gradients (=> scalar-valued functions) and Jacobians (=> array-valued functions)?
const only_fj! = only_fg!
const only_fj_and_jv! = only_fg_and_gv!
const only_fj_and_fjv! = only_fg_and_fgv!

const only_fj = only_fg
const only_fj_and_jv = only_fg_and_gv
const only_fj_and_fjv = only_fg_and_fgv
const only_j_and_fj = only_g_and_fg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about names and about this duplication in general. This would be somewhat consistent with the current names (I think).

@pkofod
Copy link
Member

pkofod commented Nov 23, 2025

@devmotion did you write any tests that you didn't push here?

@devmotion
Copy link
Contributor Author

No, because I wanted to be sure about the desired design first. Which type of functions do we want to add? How do we want to name them? Do we want to add all kind of new constructors to OnceDifferentiable (and TwiceDifferentiable?) or do we want to use a more modular approach (see my comment above)?

@pkofod
Copy link
Member

pkofod commented Nov 24, 2025

Method error but I think JET points in the direction of the issue in the rewrite of TwiceDifferentiable

@devmotion devmotion force-pushed the dmw/jvp branch 3 times, most recently from 2ea86c9 to 354fb0e Compare November 25, 2025 00:09
@pkofod
Copy link
Member

pkofod commented Nov 25, 2025

I see you added tests for hv, then only tests for jv remains I suppose?

@devmotion
Copy link
Contributor Author

No, I just fixed those that had to be changed since the ...HV struct is removed. There's no tests yet since the API hasn't stabilized yet. Downstream tests in Optim don't pass yet, which I think we have to ensure first. All recent changes are due to requirements and broken tests (eg broken TwiceDifferentiable and TwiceDifferentiableHV) in Optim, so until everything works together I think it's likely that we'll have to go back and change something here.

@devmotion devmotion force-pushed the dmw/jvp branch 8 times, most recently from 429c2b2 to 9794fbb Compare December 1, 2025 21:03
@devmotion devmotion force-pushed the dmw/jvp branch 2 times, most recently from 24a372d to 13a2a6f Compare December 11, 2025 00:10
@devmotion devmotion marked this pull request as ready for review January 5, 2026 10:40
@pkofod
Copy link
Member

pkofod commented Jan 5, 2026

@devmotion what's the mentioned inconsistency with finite differencing that you refer to in the code comments?

@pkofod
Copy link
Member

pkofod commented Jan 7, 2026

bump on this one, as it's the first in the sequence :) @devmotion

@devmotion
Copy link
Contributor Author

Do you mean

Currently, this is disabled as it can lead to inconsistencies with gradient calculations with finite differencing (default)

?

It refers to the observation I made when I enabled AD by using the same AD backend for all functions: Tests with finite differencing started to fail, since the finite differencing JVP is not identical to the finite differencing Jacobian multiplied with the desired vector.

@pkofod pkofod merged commit 667d7d9 into JuliaNLSolvers:master Jan 7, 2026
9 of 11 checks passed
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.

2 participants