Skip to content

Improve ForwardDiff Dual N=0 handling with clear ArgumentError and regression test#927

Open
AJ0070 wants to merge 3 commits intoSciML:mainfrom
AJ0070:fix/914
Open

Improve ForwardDiff Dual N=0 handling with clear ArgumentError and regression test#927
AJ0070 wants to merge 3 commits intoSciML:mainfrom
AJ0070:fix/914

Conversation

@AJ0070
Copy link
Contributor

@AJ0070 AJ0070 commented Mar 26, 2026

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the contributor guidelines, in particular the SciML Style Guide and COLPRAC
  • Any new documentation only uses public API

What changed

  • Added an early check in the ForwardDiff extension init path to detect zero-partial Dual inputs from A or b.
  • Added a targeted ArgumentError message to make the failure mode explicit and actionable.
  • Added a regression test in the ForwardDiff overload test file to lock in this behavior.

Additional context

This follows discussion on #914: zero-partial Duals are ill-formed for the intended AD path, but users should still get a clear error instead of an internal failure.

AI Disclosure: Used codex

if nA === 0 || nb === 0
throw(ArgumentError(
"LinearSolve does not support ForwardDiff.Dual values with zero partials (N = 0). " *
"Use primal values with ForwardDiff.value(...) or construct Dual numbers with at least one partial."
Copy link
Member

Choose a reason for hiding this comment

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

ForwardDiff.value is internal, but more importantly it's also generally unsafe and hence usually one should use ForwardDiff.value(T, x) instead (which is internal as well though, so I don't think it belongs into a user-facing error message).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then I’ll just replace it with a note advising not to use non-Dual numbers.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I don't think we should ever tell users to use ForwardDiff.value 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 😅 that's just asking for punishment.

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.

3 participants