Skip to content

Make ForwardDiff a weak dependency extension - #131

Merged
ChrisRackauckas merged 6 commits into
SciML:masterfrom
ChrisRackauckas-Claude:forwarddiff-weakdep
Aug 9, 2025
Merged

ChrisRackauckas merged 6 commits into
SciML:masterfrom
ChrisRackauckas-Claude:forwarddiff-weakdep

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

This PR converts ForwardDiff from a hard dependency to a weak dependency using Julia package extensions, reducing the dependency load for users who don't need automatic differentiation features.

Changes

  • Moved ForwardDiff from [deps] to [weakdeps] in Project.toml
  • Created PreallocationToolsForwardDiffExt extension module in ext/
  • Moved all ForwardDiff-specific code to the extension:
    • FixedSizeDiffCache constructor for Dual arrays
    • chunksize method for ForwardDiff.Dual types
    • get_tmp methods for ForwardDiff.Dual types
  • Used ref cells pattern to allow extension to modify behavior at runtime
  • Provided default implementations when ForwardDiff is not available

Implementation Details

The implementation uses mutable ref cells (DUAL_ARRAY_CREATOR and CHUNK_SIZE_FUNC) that get set by the extension's __init__ function when ForwardDiff is loaded. This avoids method overwriting issues while maintaining full backward compatibility.

Benefits

  • Reduces dependency load for users who don't need ForwardDiff
  • Makes the package more modular and lightweight by default
  • Follows the Julia ecosystem trend of using weak dependencies for optional features
  • Maintains full backward compatibility - all existing code continues to work

Testing

The package tests pass with ForwardDiff loaded. The extension properly activates when ForwardDiff is imported, providing all the necessary dual number support.

- Move ForwardDiff-specific code to PreallocationToolsForwardDiffExt
- ForwardDiff is now an optional dependency loaded via package extensions
- Default implementations provided when ForwardDiff is not available
- Uses ref cells to allow extension to modify behavior at runtime
- All ForwardDiff-specific methods for Dual types moved to extension
@thomvet

thomvet commented Aug 8, 2025

Copy link
Copy Markdown
Contributor

GPU tests are failing, because ForwarDiff.jl started iterating over arrays when constructing seeds in version 1.0.1 rather than broadcasting as in previous versions. Tests are failing due to this.

Ref: JuliaDiff/ForwardDiff.jl#759

@ChrisRackauckas

Copy link
Copy Markdown
Member

That wasn't ForwardDiff v1.0.1, it was a DifferentiationInterface.jl thing. I had just fixed that last week though: SciML/OrdinaryDiffEq.jl#2788, but it looks like I forgot to tag that so I'll get that tagged right away.

@thomvet

thomvet commented Aug 8, 2025 •

Copy link
Copy Markdown
Contributor

Interesting. I understand the performance reason (well, the high level part - not the details), but I am telling you that:

using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, CUDA, ADTypes

#ODE tests
function foo(du, u, (A, tmp), t)
    tmp = get_tmp(tmp, u)
    mul!(tmp, A, u)
    @. du = u + tmp
    nothing
end
#with specified chunk_size
chunk_size = 5
u0 = cu(rand(5, 5)) #example kept small for test purposes.
A = cu(-randn(5, 5))
cache = DiffCache(cu(zeros(5, 5)), chunk_size)
prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0f0, 1.0f0), (A, cache))
sol = solve(prob, TRBDF2(autodiff = AutoForwardDiff(chunksize = chunk_size)))
@test sol.retcode == ReturnCode.Success 

Passes with ForwardDiff 1.0.0 (Environment 1), whereas it fails with 1.0.1 (Environment 2).
This is before merging the above PR. (which is not part of OrdinaryDiffEq v6.101.0 - as you said).

Environment 1:

  [47edcb42] ADTypes v1.16.0
  [052768ef] CUDA v5.8.3
⌃ [f6369f11] ForwardDiff v1.0.0
  [1dea7af3] OrdinaryDiffEq v6.101.0
  [d236fae5] PreallocationTools v0.4.29

Environment 2:

  [47edcb42] ADTypes v1.16.0
  [052768ef] CUDA v5.8.3
  [f6369f11] ForwardDiff v1.0.1
  [1dea7af3] OrdinaryDiffEq v6.101.0
  [d236fae5] PreallocationTools v0.4.29

But in the end... as long as it works... 🤷

@ChrisRackauckas

Copy link
Copy Markdown
Member

But that's not with OrdinaryDiffEqDifferentiation#master

Comment thread ext/PreallocationToolsForwardDiffExt.jl Resolved
Comment thread src/PreallocationTools.jl Resolved
Comment thread src/PreallocationTools.jl Resolved
@ChrisRackauckas
ChrisRackauckas merged commit e67c91d into SciML:master Aug 9, 2025
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