Closed DanielVandH closed 1 year ago
Looks like it's actually missing because https://github.com/JuliaDiff/FiniteDiff.jl/blob/master/src/gradients.jl#L8-L13 is the only constructor other than the default, and the default is a little nasty to use with how it takes in types. Looks like that needs to be added and a test.
We should also convert this whole package to real docs, but that's a bit more.
Yeah, I was just thinking about documentation. It's a bit hard to pick out what each part of the cache is for initially.
If I understand it correctly, is this the type of usage you're expecting from this method (if it existed)?
"""
GradientCache(c1, c2, fx, fdtype, returntype, inplace)
Non-allocating cache constructor for computing a gradient.
# Arguments
- `c1, c2`: (Non-aliased) caches for the input vector.
- `fx=nothing`: Cache function call.
- `fdtype=Val(:central)`: Method used for computing the gradient.
- `returntype=eltype(df)`: Element type returned from the function call.
- `inplace=Val(true)`: Indicator of whether the function is computed in-place or not.
For example, the cache for a function `f(x) = x[1]^2 + x[2]` might be defined via
\```julia
julia> c1, c2 = zeros(2), zeros(2)
julia> fx = f(c1)
julia> cache = GradientCache(c1, c2, fx, Val(:central), Float64, Val(true))
\```
"""
function FiniteDiff.GradientCache(
c1,
c2,
fx=nothing,
fdtype=Val(:central),
returntype=eltype(fx),
inplace=Val(true))
# ... construct the cache ...
end
Maybe I'm misunderstanding the way these caches are used. Also - what is the usage of Val
here? I've never used it before working through some of this package.
Yes, I think it also picked up a c3 along the way too, so it needs another in there, but that's correct.
The Val
types convert runtime values to type information, allowing compile-time checks. This allows branches for things like :forward
to compile away for fast code (only really matters on small arrays).
The README lists the following method (
df
should also befx
, actually)for a non-allocating gradient cache constructor. I don't think this method exists?:
Is there an alternative method to be using if I want to get a non-allocating cache?