Inline dual_definition_retval to fix nested Dual performance#825
Open
KristofferC wants to merge 1 commit into
Open
Inline dual_definition_retval to fix nested Dual performance#825KristofferC wants to merge 1 commit into
dual_definition_retval to fix nested Dual performance#825KristofferC wants to merge 1 commit into
Conversation
`dual_definition_retval` carries no `@inline` annotation. For plain
`Dual`s the inliner picks it up anyway, but for nested `Dual`s (as used
by `hessian`) the struct is large enough that Julia's inlining cost
model declines it. Every DiffRules-generated operation -- including `*`
and `/` -- then compiles to a scalar op plus out-of-line calls that
return the full `Dual` through an `sret` memcpy (648 bytes for
`Dual{T,Dual{T,Float64,8},8}`, per call, per op).
Forcing the inline turns e.g. the nested-`Dual` multiply into
straight-line inlined code. Measured on
`Dual{Nothing,Dual{Nothing,Float64,8},8}` vectors (256 elements, ns per
element):
| | before | after |
|--------------|--------|-------|
| `*` (M4) | 65.4 | 22.7 |
| `*` (Zen 4) | 127.6 | 53.5 |
| `sqrt` (M4) | 34.0 | 14.7 |
| `sqrt` (Zen 4) | 81.1 | 13.0 |
End-to-end `ForwardDiff.hessian!` at n=256, chunk 8: 28.3 -> 20.4 ms
(ackley) and 41.2 -> 23.9 ms (rosenbrock) on an Apple M4; 46.3 -> 25.7 ms
and 56.7 -> 34.0 ms on an AMD EPYC 9354. First-order (non-nested) `Dual`s
already inlined before this change and are unaffected.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #825 +/- ##
=======================================
Coverage 90.74% 90.74%
=======================================
Files 11 11
Lines 1070 1070
=======================================
Hits 971 971
Misses 99 99 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
👨 I am generating some benchmarks for my JuliaCon talk for https://github.com/KristofferC/HyperHessians.jl, and I wanted to drill down into why HyperHessians was faster than ForwardDiff. One of the reasons was a failure to inline, which just seemed like a mistake, and I don't want to base conclusions of the presentation on performance bugs in other libraries. With this PR the performance of hessian is greatly improved.
🤖
dual_definition_retvalcarries no@inlineannotation. For plainDuals the inliner picks it up anyway, but for nestedDuals (as used byhessian) the struct is large enough that Julia's inlining cost model declines it. Every DiffRules-generated operation -- including*and/-- then compiles to a scalar op plus out-of-line calls that return the fullDualthrough ansretmemcpy (648 bytes forDual{T,Dual{T,Float64,8},8}, per call, per op).Forcing the inline turns e.g. the nested-
Dualmultiply into straight-line inlined code. Measured onDual{Nothing,Dual{Nothing,Float64,8},8}vectors (256 elements, ns per element):*(M4)*(Zen 4)sqrt(M4)sqrt(Zen 4)End-to-end
ForwardDiff.hessian!at n=256, chunk 8: 28.3 -> 20.4 ms (ackley) and 41.2 -> 23.9 ms (rosenbrock) on an Apple M4; 46.3 -> 25.7 ms and 56.7 -> 34.0 ms on an AMD EPYC 9354. First-order (non-nested)Duals already inlined before this change and are unaffected.