JuliaDiff / AbstractDifferentiation.jl

An abstract interface for automatic differentiation.
https://juliadiff.org/AbstractDifferentiation.jl/
MIT License
135 stars 18 forks source link

Improve performance of `derivative` with FiniteDifferences and ForwardDiff #97

Closed devmotion closed 12 months ago

devmotion commented 1 year ago

Fixes #87. With this PR, the benchmark in #87 yields on my computer

julia> @benchmark with_AD(1.)
BenchmarkTools.Trial: 10000 samples with 960 evaluations.
 Range (min … max):  87.060 ns …  1.505 μs  ┊ GC (min … max): 0.00% … 88.90%
 Time  (median):     89.291 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   92.532 ns ± 30.948 ns  ┊ GC (mean ± σ):  0.75% ±  2.17%

  █▆▆▆▃▂ ▄▅▄▂▁▁▁ ▄▂▁▁                                       ▁ ▂
  ███████████████████▇▆▇▇█▇▇▇▇▆▅▆▅▅▅▅▄▄▃▃▄▄▁▆▃▁▃▄▃▃▅▃▃▁▁▄█▆▄█ █
  87.1 ns      Histogram: log(frequency) by time       138 ns <

 Memory estimate: 32 bytes, allocs estimate: 2.

julia> @benchmark without_AD(1.)
BenchmarkTools.Trial: 10000 samples with 961 evaluations.
 Range (min … max):  86.429 ns …  1.699 μs  ┊ GC (min … max): 0.00% … 91.77%
 Time  (median):     89.110 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   91.506 ns ± 35.367 ns  ┊ GC (mean ± σ):  0.90% ±  2.23%

  ▃█▇▃▃██▄▄▂▁   ▂▃▄▄▄▂▁▁       ▃▂                             ▂
  ████████████▇██████████▇██▇▇████▇███▆▆▅▂▅▄▅▇█████▅▅▆▆▅▆▅▂▃▆ █
  86.4 ns      Histogram: log(frequency) by time       113 ns <

 Memory estimate: 32 bytes, allocs estimate: 2.
codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.13% :tada:

Comparison is base (041d760) 84.25% compared to head (8ba18a0) 84.38%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #97 +/- ## ========================================== + Coverage 84.25% 84.38% +0.13% ========================================== Files 8 8 Lines 470 474 +4 ========================================== + Hits 396 400 +4 Misses 74 74 ``` | [Files Changed](https://app.codecov.io/gh/JuliaDiff/AbstractDifferentiation.jl/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaDiff) | Coverage Δ | | |---|---|---| | [ext/AbstractDifferentiationFiniteDifferencesExt.jl](https://app.codecov.io/gh/JuliaDiff/AbstractDifferentiation.jl/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaDiff#diff-ZXh0L0Fic3RyYWN0RGlmZmVyZW50aWF0aW9uRmluaXRlRGlmZmVyZW5jZXNFeHQuamw=) | `100.00% <100.00%> (ø)` | | | [ext/AbstractDifferentiationForwardDiffExt.jl](https://app.codecov.io/gh/JuliaDiff/AbstractDifferentiation.jl/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaDiff#diff-ZXh0L0Fic3RyYWN0RGlmZmVyZW50aWF0aW9uRm9yd2FyZERpZmZFeHQuamw=) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.