Skip to content

Cache non-isbits ForwardDiff workspaces - #209

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:codex/nonbits-forwarddiff-cache
Aug 26, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:codex/nonbits-forwarddiff-cache

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

What changed and why

ForwardDiff Dual values whose scalar storage is not an isbits type could not use FixedSizeDiffCache and received a newly zeroed workspace on every DiffCache lookup. This caches correctly typed workspaces for both cache variants while retaining the existing reinterpret path for isbits Duals. Same-shaped array workspaces are returned directly so the warmed vector lookup used by Symbolics is allocation-free.

This makes the workaround discussed in JuliaSymbolics/Symbolics.jl#1968 unnecessary once Symbolics can require a PreallocationTools release containing this change.

Verification

Failing before the implementation, with only the regression test applied:

GROUP=Core julia --project -e "using Pkg; Pkg.test()"
DiffCache Dispatch | 85 pass, 3 fail, 1 error, 10 broken, 99 total

The three failures showed that DiffCache returned fresh zeroed workspaces instead of retaining writes. FixedSizeDiffCache errored while trying to reinterpret a non-isbits Dual:

ArgumentError: cannot reinterpret ForwardDiff.Dual{Nothing, Float64, 2} as ForwardDiff.Dual{Nothing, NonBitsReal, 2}, type ForwardDiff.Dual{Nothing, NonBitsReal, 2} is not a bits type

Passing after the implementation:

GROUP=Core julia --project -e "using Pkg; Pkg.test()"
DiffCache Dispatch | 100 pass, 10 broken, 110 total
Allocation Regression Tests | 27 pass, 27 total
Testing PreallocationTools tests passed

The new regression test covers Dual value, Dual type, and Dual array dispatch for DiffCache and FixedSizeDiffCache. Every warmed vector lookup reports 0 allocated bytes.

GROUP=QA julia --project -e "using Pkg; Pkg.test()"
QA | 27 pass, 27 total
Testing PreallocationTools tests passed

julia --project="$HOME/.julia/environments/runic" -m Runic --check --diff src/PreallocationTools.jl ext/PreallocationToolsForwardDiffExt.jl test/core_dispatch.jl
exit 0

typos src/PreallocationTools.jl ext/PreallocationToolsForwardDiffExt.jl test/core_dispatch.jl
exit 0

git diff --check
exit 0

Not verified

Docs were not built because this changes no public API, docstring, or documentation. GPU and downstream jobs were not run locally.

Review note

The same-shape fast path in _restructure is what removes the final array-header allocation for vector typed buffers. Non-vector reshaping can still allocate only the reshape wrapper; the backing workspace remains cached. The existing isbits reinterpret behavior is unchanged.

Please ignore this PR until it has been reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code
https://chatgpt.com/codex/tasks/01a03a04-70ae-77a0-ba1b-ec7a1ed9ef47

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review August 26, 2026 19:51
@ChrisRackauckas
ChrisRackauckas merged commit 5cd82e4 into SciML:master Aug 26, 2026
7 checks passed
ChrisRackauckas added a commit that referenced this pull request Aug 26, 2026
Releases the unreleased changes on `master` since 1.6.0:

- #209 — `Cache non-isbits ForwardDiff workspaces`. ForwardDiff `Dual` values whose scalar storage is not `isbits` previously received a newly zeroed workspace on every `DiffCache` lookup; both cache variants now cache correctly typed workspaces, and same-shaped array workspaces are returned directly so the warmed vector lookup is allocation-free.
- #208 — compat: drop stale majors.

**Minor**, matching this repo's convention (1.5.0 -> 1.6.0 carried the #206 batch). No exported or `public` name is added or removed, but downstreams need a floor to depend on the new allocation behaviour - JuliaSymbolics/Symbolics.jl#1968 is waiting on exactly this.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R
ChrisRackauckas added a commit that referenced this pull request Aug 26, 2026
Releases the unreleased changes on `master` since 1.6.0:

- #209 — `Cache non-isbits ForwardDiff workspaces`. ForwardDiff `Dual` values whose scalar storage is not `isbits` previously received a newly zeroed workspace on every `DiffCache` lookup; both cache variants now cache correctly typed workspaces, and same-shaped array workspaces are returned directly so the warmed vector lookup is allocation-free.
- #208 — compat: drop stale majors.

**Minor**, matching this repo's convention (1.5.0 -> 1.6.0 carried the #206 batch). No exported or `public` name is added or removed, but downstreams need a floor to depend on the new allocation behaviour - JuliaSymbolics/Symbolics.jl#1968 is waiting on exactly this.



Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude <[email protected]>
ChrisRackauckas-Claude pushed a commit to JuliaSymbolics/Symbolics.jl that referenced this pull request Aug 27, 2026
Drops the SymbolicsPreallocationToolsExt workaround now that PreallocationTools 1.7.0 caches non-isbits ForwardDiff workspaces itself (SciML/PreallocationTools.jl#209), and raises the PreallocationTools compat floor to 1.7 so the workaround cannot be removed while an older PreallocationTools is resolvable.

Supersedes #1968, which dropped the workaround without the floor. Symbolics runs no Downgrade lane, so that gap would not have been caught by CI.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R
ChrisRackauckas added a commit to JuliaSymbolics/Symbolics.jl that referenced this pull request Aug 27, 2026
* Simplify symbolic PreallocationTools cache dispatch

Drops the SymbolicsPreallocationToolsExt workaround now that PreallocationTools 1.7.0 caches non-isbits ForwardDiff workspaces itself (SciML/PreallocationTools.jl#209), and raises the PreallocationTools compat floor to 1.7 so the workaround cannot be removed while an older PreallocationTools is resolvable.

Supersedes #1968, which dropped the workaround without the floor. Symbolics runs no Downgrade lane, so that gap would not have been caught by CI.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

* Remove the PreallocationTools extension

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://chatgpt.com/codex/tasks/01a03a04-70ae-77a0-ba1b-ec7a1ed9ef47

---------

Co-authored-by: Claude <[email protected]>
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