PreallocationTools
Tools for building non-allocating pre-cached functions in Julia, allowing for GC-free usage of automatic differentiation in complex codes
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (15.2%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Tools for building non-allocating pre-cached functions in Julia, allowing for GC-free usage of automatic differentiation in complex codes
Basic Info
- Host: GitHub
- Owner: SciML
- License: other
- Language: Julia
- Default Branch: master
- Homepage: https://docs.sciml.ai/PreallocationTools/stable/
- Size: 1.3 MB
Statistics
- Stars: 123
- Watchers: 7
- Forks: 21
- Open Issues: 6
- Releases: 45
Topics
Metadata Files
README.md
PreallocationTools.jl
PreallocationTools.jl is a set of tools for helping build non-allocating pre-cached functions for high-performance computing in Julia. Its tools handle edge cases of automatic differentiation to make it easier for users to get high performance even in the cases where code generation may change the function that is being called.
DiffCache
DiffCache is a type for doubly-preallocated vectors which are
compatible with non-allocating forward-mode automatic differentiation by
ForwardDiff.jl.
Since ForwardDiff.jl uses chunked duals in its forward pass, two
vector sizes are required in order for the arrays to be properly defined.
DiffCache creates a dispatching type to solve this, so that by passing a
qualifier it can automatically switch between the required cache. This method
is fully type-stable and non-dynamic, made for when the highest performance is
needed.
The DiffCache also supports sparsity detection via
SparseConnectivityTracer.jl.
However, the implementation may allocate memory in this case since we assume that
sparsity detection happens only once (or maybe a few times). Allocating memory
allows to save memory in the long run since no additional cache needs to be stored
forever.
Using DiffCache
julia
DiffCache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u)); levels::Int = 1)
DiffCache(u::AbstractArray, N::AbstractArray{<:Int})
The DiffCache function builds a DiffCache object that stores both a version
of the cache for u and for the Dual version of u, allowing use of
pre-cached vectors with forward-mode automatic differentiation. Note that
DiffCache, due to its design, is only compatible with arrays that contain concretely
typed elements.
To access the caches, one uses:
julia
get_tmp(tmp::DiffCache, u)
When u has an element subtype of Dual numbers, then it returns the Dual
version of the cache. Otherwise it returns the standard cache (for use in the
calls without automatic differentiation).
In order to preallocate to the right size, the DiffCache needs to be specified
to have the correct N matching the chunk size of the dual numbers or larger.
If the chunk size N specified is too large, get_tmp will automatically resize
when dispatching; this remains type-stable and non-allocating, but comes at the
expense of additional memory.
In a differential equation, optimization, etc., the default chunk size is computed
from the state vector u, and thus if one creates the DiffCache via
DiffCache(u) it will match the default chunking of the solver libraries.
DiffCache is also compatible with nested automatic differentiation calls through
the levels keyword (N for each level computed using based on the size of the
state vector) or by specifying N as an array of integers of chunk sizes, which
enables full control of chunk sizes on all differentation levels.
DiffCache Example 1: Direct Usage
```julia using ForwardDiff, PreallocationTools randmat = rand(5, 3) sto = similar(randmat) stod = DiffCache(sto)
function claytonsample!(sto, τ, α; randmat = randmat) sto = get_tmp(sto, τ) sto .= randmat τ == 0 && return sto
n = size(sto, 1)
for i in 1:n
v = sto[i, 2]
u = sto[i, 1]
sto[i, 1] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ) * α
sto[i, 2] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ)
end
return sto
end
ForwardDiff.derivative(τ -> claytonsample!(stod, τ, 0.0), 0.3) ForwardDiff.jacobian(x -> claytonsample!(stod, x[1], x[2]), [0.3; 0.0]) ```
In the above, the chunk size of the dual numbers has been selected based on the size
of randmat, resulting in a chunk size of 8 in this case. However, since the derivative
is calculated with respect to τ and the Jacobian is calculated with respect to τ and α,
specifying the DiffCache with stod = DiffCache(sto, 1) or stod = DiffCache(sto, 2),
respectively, would have been the most memory efficient way of performing these calculations
(only really relevant for much larger problems).
DiffCache Example 2: ODEs
julia
using LinearAlgebra, OrdinaryDiffEq
function foo(du, u, (A, tmp), t)
mul!(tmp, A, u)
@. du = u + tmp
nothing
end
prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), zeros(5, 5)))
solve(prob, TRBDF2())
fails because tmp is only real numbers, but during automatic differentiation
we need tmp to be a cache of dual numbers. Since u is the value that will
have the dual numbers, we dispatch based on that:
julia
using LinearAlgebra, OrdinaryDiffEq, PreallocationTools
function foo(du, u, (A, tmp), t)
tmp = get_tmp(tmp, u)
mul!(tmp, A, u)
@. du = u + tmp
nothing
end
chunk_size = 5
prob = ODEProblem(foo,
ones(5, 5),
(0.0, 1.0),
(ones(5, 5), DiffCache(zeros(5, 5), chunk_size)))
solve(prob, TRBDF2(chunk_size = chunk_size))
or just using the default chunking:
julia
using LinearAlgebra, OrdinaryDiffEq, PreallocationTools
function foo(du, u, (A, tmp), t)
tmp = get_tmp(tmp, u)
mul!(tmp, A, u)
@. du = u + tmp
nothing
end
chunk_size = 5
prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), DiffCache(zeros(5, 5))))
solve(prob, TRBDF2())
DiffCache Example 3: Nested AD calls in an optimization problem involving a Hessian matrix
```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools, Optimization, OptimizationOptimJL function foo(du, u, p, t) tmp = p[2] A = reshape(p[1], size(tmp.du)) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end
coeffs = -collect(0.1:0.1:0.4) cache = DiffCache(zeros(2, 2), levels = 3) prob = ODEProblem(foo, ones(2, 2), (0.0, 1.0), (coeffs, cache)) realsol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8)
function objfun(x, prob, realsol, cache) prob = remake(prob, u0 = eltype(x).(prob.u0), p = (x, cache)) sol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8)
ofv = 0.0
if any((s.retcode != :Success for s in sol))
ofv = 1e12
else
ofv = sum((sol .- realsol) .^ 2)
end
return ofv
end fn(x, p) = objfun(x, p[1], p[2], p[3]) optfun = OptimizationFunction(fn, Optimization.AutoForwardDiff()) optprob = OptimizationProblem(optfun, zeros(length(coeffs)), (prob, realsol, cache)) solve(optprob, Newton()) ```
Solves an optimization problem for the coefficients, coeffs, appearing in a differential equation.
The optimization is done with Optim.jl's Newton()
algorithm. Since this involves automatic differentiation in the ODE solver and the calculation
of Hessians, three automatic differentiations are nested within each other. Therefore, the DiffCache
is specified with levels = 3.
FixedSizeDiffCache
FixedSizeDiffCache is a lot like DiffCache, but it stores dual numbers in its caches
instead of a flat array. Because of this, it can avoid a view, making it a little bit
more performant for generating caches of non-Array types. However, it is a lot less
flexible than DiffCache, and is thus only recommended for cases where the chunk size
is known in advance (for example, ODE solvers) and where u is not an Array.
The interface is almost exactly the same, except with the constructor:
julia
FixedSizeDiffCache(u::AbstractArray, chunk_size = Val{ForwardDiff.pickchunksize(length(u))})
FixedSizeDiffCache(u::AbstractArray, chunk_size::Integer)
Note that the FixedSizeDiffCache can support duals that are of a smaller chunk size than
the preallocated ones, but not a larger size. Nested duals are not supported with this
construct.
LazyBufferCache
julia
LazyBufferCache(f::F = identity)
A LazyBufferCache is a Dict-like type for the caches which automatically defines
new cache arrays on demand when they are required. The function f maps
size_of_cache = f(size(u)), which by default creates cache arrays of the same size.
By default the created buffers are not initialized, but a function initializer!
can be supplied which is applied to the buffer when it is created, for instance buf -> fill!(buf, 0.0).
Note that LazyBufferCache is type-stable and contains no dynamic dispatch. This gives
it a ~15ns overhead. The upside of LazyBufferCache is that the user does not have to
worry about potential issues with chunk sizes and such: LazyBufferCache is much easier!
Example
julia
using LinearAlgebra, OrdinaryDiffEq, PreallocationTools
function foo(du, u, (A, lbc), t)
tmp = lbc[u]
mul!(tmp, A, u)
@. du = u + tmp
nothing
end
prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), LazyBufferCache()))
solve(prob, TRBDF2())
GeneralLazyBufferCache
julia
GeneralLazyBufferCache(f = identity)
A GeneralLazyBufferCache is a Dict-like type for the caches which automatically defines
new caches on demand when they are required. The function f generates the cache matching
for the type of u, and subsequent indexing reuses that cache if that type of u has
already ben seen.
Note that GeneralLazyBufferCache's return is not type-inferred.
This means it's the slowest of the preallocation methods, but it's the most general.
Example
In all of the previous cases our cache was an array. However, in this case we want to preallocate
a DifferentialEquations ODEIntegrator object. This object is the one created via
DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t), and we
want to optimize p in a way that changes its type to ForwardDiff. Thus what we can do is make a
GeneralLazyBufferCache which holds these integrator objects, defined by p, and indexing it with
p in order to retrieve the cache. The first time it's called it will build the integrator, and
in subsequent calls it will reuse the cache.
Defining the cache as a function of p to build an integrator thus looks like:
julia
lbc = GeneralLazyBufferCache(function (p)
DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t)
end)
then lbc[p] (or, equivalently, get_tmp(lbc, p)) will be smart and reuse the caches. A full example looks like the following:
```julia using Random, DifferentialEquations, LinearAlgebra, Optimization, OptimizationNLopt, OptimizationOptimJL, PreallocationTools
lbc = GeneralLazyBufferCache(function (p) DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t) end)
Random.seed!(2992999) λ, y₀, σ = -0.5, 15.0, 0.1 T, n = 5.0, 200 Δt = T / n t = [j * Δt for j in 0:n] y = y₀ * exp.(λ * t) yᵒ = y .+ [0.0, σ * randn(n)...] ode_fnc(u, p, t) = p * u function loglik(θ, data, integrator) yᵒ, n, ε = data λ, σ, u0 = θ integrator.p = λ reinit!(integrator, u0) solve!(integrator) ε = yᵒ .- integrator.sol.u ℓ = -0.5n * log(2π * σ^2) - 0.5 / σ^2 * sum(ε .^ 2) end θ₀ = [-1.0, 0.5, 19.73] negloglik = (θ, p) -> -loglik(θ, p, lbc[θ[1]]) fnc = OptimizationFunction(negloglik, Optimization.AutoForwardDiff()) ε = zeros(n) prob = OptimizationProblem(fnc, θ₀, (yᵒ, n, ε), lb = [-10.0, 1e-6, 0.5], ub = [10.0, 10.0, 25.0]) solve(prob, LBFGS()) ```
Similar Projects
AutoPreallocation.jl tries to do this automatically at the compiler level. Alloc.jl tries to do this with a bump allocator.
Owner
- Name: SciML Open Source Scientific Machine Learning
- Login: SciML
- Kind: organization
- Email: contact@chrisrackauckas.com
- Website: https://sciml.ai
- Twitter: SciML_Org
- Repositories: 170
- Profile: https://github.com/SciML
Open source software for scientific machine learning
Citation (CITATION.bib)
@article{DifferentialEquations.jl-2017,
author = {Rackauckas, Christopher and Nie, Qing},
doi = {10.5334/jors.151},
journal = {The Journal of Open Research Software},
keywords = {Applied Mathematics},
note = {Exported from https://app.dimensions.ai on 2019/05/05},
number = {1},
pages = {},
title = {DifferentialEquations.jl – A Performant and Feature-Rich Ecosystem for Solving Differential Equations in Julia},
url = {https://app.dimensions.ai/details/publication/pub.1085583166 and http://openresearchsoftware.metajnl.com/articles/10.5334/jors.151/galley/245/download/},
volume = {5},
year = {2017}
}
GitHub Events
Total
- Create event: 17
- Release event: 8
- Issues event: 7
- Watch event: 10
- Delete event: 7
- Issue comment event: 47
- Push event: 94
- Pull request review comment event: 2
- Pull request review event: 5
- Pull request event: 29
- Fork event: 6
Last Year
- Create event: 17
- Release event: 8
- Issues event: 7
- Watch event: 10
- Delete event: 7
- Issue comment event: 47
- Push event: 94
- Pull request review comment event: 2
- Pull request review event: 5
- Pull request event: 29
- Fork event: 6
Committers
Last synced: 6 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Christopher Rackauckas | a****s@c****m | 106 |
| Thomas Vetter | 8****t | 36 |
| Arno Strouwen | a****n@t****e | 20 |
| Lilith Orion Hafner | l****r@g****m | 13 |
| Qingyu Qu | 2****3@q****m | 10 |
| dependabot[bot] | 4****] | 10 |
| Frank Schaefer | k****e@w****e | 8 |
| CompatHelper Julia | c****y@j****g | 6 |
| Anant Thazhemadam | a****m@g****m | 6 |
| Krishna Bhogaonker | c****q@g****m | 6 |
| Daniel Wennberg | d****g@g****m | 3 |
| Hans Würfel | g****t@w****o | 2 |
| jClugstor | j****n@g****m | 2 |
| Aayush Sabharwal | a****l@j****m | 1 |
| Bart de Koning | b****g@d****l | 1 |
| David Widmann | d****n | 1 |
| Hendrik Ranocha | m****l@r****e | 1 |
| Jaakko Ruohio | j****2 | 1 |
| Oscar Smith | o****h@g****m | 1 |
| Sathvik Bhagavan | 3****n | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 28
- Total pull requests: 132
- Average time to close issues: 22 days
- Average time to close pull requests: 11 days
- Total issue authors: 26
- Total pull request authors: 21
- Average comments per issue: 7.79
- Average comments per pull request: 0.64
- Merged pull requests: 101
- Bot issues: 0
- Bot pull requests: 25
Past Year
- Issues: 5
- Pull requests: 27
- Average time to close issues: 3 days
- Average time to close pull requests: about 21 hours
- Issue authors: 5
- Pull request authors: 8
- Average comments per issue: 1.4
- Average comments per pull request: 0.41
- Merged pull requests: 14
- Bot issues: 0
- Bot pull requests: 3
Top Authors
Issue Authors
- baggepinnen (2)
- DanielVandH (2)
- daviehh (1)
- vyudu (1)
- amrods (1)
- moble (1)
- dbstein (1)
- JuliaTagBot (1)
- elbert5770 (1)
- andreichalapco (1)
- JianghuiDu (1)
- LilithHafner (1)
- franckgaga (1)
- thomvet (1)
- frankschae (1)
Pull Request Authors
- ChrisRackauckas (32)
- ArnoStrouwen (19)
- dependabot[bot] (13)
- github-actions[bot] (12)
- thomvet (10)
- LilithHafner (9)
- ChrisRackauckas-Claude (6)
- 00krishna (5)
- ranocha (4)
- thazhemadam (4)
- frankschae (2)
- SouthEndMusic (2)
- ErikQQY (2)
- hexaeder (2)
- jClugstor (2)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- julia 9,288 total
- Total dependent packages: 20
- Total dependent repositories: 0
- Total versions: 45
juliahub.com: PreallocationTools
Tools for building non-allocating pre-cached functions in Julia, allowing for GC-free usage of automatic differentiation in complex codes
- Homepage: https://docs.sciml.ai/PreallocationTools/stable/
- Documentation: https://docs.juliahub.com/General/PreallocationTools/stable/
- License: MIT
-
Latest release: 0.4.34
published 4 months ago
Rankings
Dependencies
- actions/cache v1 composite
- actions/checkout v2 composite
- codecov/codecov-action v1 composite
- julia-actions/julia-buildpkg v1 composite
- julia-actions/julia-processcoverage v1 composite
- julia-actions/julia-runtest v1 composite
- julia-actions/setup-julia v1 composite
- actions/checkout v2 composite
- codecov/codecov-action v1 composite
- julia-actions/julia-processcoverage v1 composite
- julia-actions/setup-julia latest composite
- actions/checkout v1 composite
- julia-actions/setup-julia latest composite
- actions/checkout v3 composite
- julia-actions/julia-buildpkg v1 composite
- julia-actions/julia-invalidations v1 composite
- julia-actions/setup-julia v1 composite
- JuliaRegistries/TagBot v1 composite