perf: preallocate Cache during preparation for ForwardDiff#741
Merged
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #741 +/- ##
==========================================
- Coverage 97.94% 97.47% -0.47%
==========================================
Files 124 124
Lines 6510 6546 +36
==========================================
+ Hits 6376 6381 +5
- Misses 134 165 +31
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
|
Yes using BenchmarkTools
using DifferentiationInterface
import ForwardDiff
x = float.(1:8)
y = Vector{eltype(x)}(undef, length(x)-1)
cache = zeros(eltype(x), length(y))
function f!(y, x, cache)
y2 = cache
for i in eachindex(y)
y[i] = (x[i+1]^2-x[i]^2)
end
for i in eachindex(y)
j = length(x) - i
y[i] += (x[j]^2-x[j+1]^2)
end
y2 .= y
return y
end
dense_backend = AutoForwardDiff()
dense_prep = prepare_jacobian(f!, y, dense_backend, x, Cache(cache))
∇f = Matrix{eltype(x)}(undef, length(y), length(x))
jacobian!(f!, y, ∇f, dense_prep, dense_backend, x, Cache(cache))
display(∇f)
@benchmark jacobian!($f!, $y, $∇f, $dense_prep, $dense_backend, $x, Cache($cache))giving: 7×8 Matrix{Float64}:
-2.0 4.0 0.0 0.0 0.0 0.0 14.0 -16.0
0.0 -4.0 6.0 0.0 0.0 12.0 -14.0 0.0
0.0 0.0 -6.0 8.0 10.0 -12.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 6.0 -8.0 -10.0 12.0 0.0 0.0
0.0 4.0 -6.0 0.0 0.0 -12.0 14.0 0.0
2.0 -4.0 0.0 0.0 0.0 0.0 -14.0 16.0
BenchmarkTools.Trial: 10000 samples with 907 evaluations per sample.
Range (min … max): 108.548 ns … 567.631 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 125.157 ns ┊ GC (median): 0.00%
Time (mean ± σ): 135.268 ns ± 33.886 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▄▅▆▇▆█▆▆▆▄▅▅▄▃▃▂▁▁ ▂
█████████████████████▆▇▅▇▆▆▅▆▅▅▅▅▄▆▆▆▇▇████▇██▆▆▆▅▅▄▅▅▆▅▃▆▅▄▃ █
109 ns Histogram: log(frequency) by time 289 ns <
Memory estimate: 0 bytes, allocs estimate: 0.Also allocation-free with Good work! |
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.
contexts_dualfield to most preparation objects in the ForwardDiff extensioncontexts, except thatCachecontexts are replaced bynothingCachecontexts are replaced bysimilar(c, Dual{...})Cachecontexts fromprep.contexts_dualConfigs withf=nothingbecause thetagcaptures the relevant typeCachecontexts