Skip to content

Commit

Permalink
no offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed May 17, 2022
1 parent 4ba6c90 commit be62c4b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Set `check` to `Val{false}()` to disable tag checking. This can lead to perturba
"""
@inline function derivative(f!, y::AbstractArray, x::Real,
cfg::DerivativeConfig{T} = DerivativeConfig(f!, y, x), ::Val{CHK}=Val{true}()) where {T, CHK}
require_one_based_indexing(y)
CHK && checktag(T, f!, x)
ydual = cfg.duals
seed!(ydual, y)
Expand All @@ -42,6 +43,7 @@ This method assumes that `isa(f(x), Union{Real,AbstractArray})`.
"""
@inline function derivative!(result::Union{AbstractArray,DiffResult},
f::F, x::R) where {F,R<:Real}
require_one_based_indexing(result)
T = typeof(Tag(f, R))
ydual = f(Dual{T}(x, one(x)))
result = extract_value!(T, result, ydual)
Expand All @@ -60,6 +62,7 @@ Set `check` to `Val{false}()` to disable tag checking. This can lead to perturba
@inline function derivative!(result::Union{AbstractArray,DiffResult},
f!, y::AbstractArray, x::Real,
cfg::DerivativeConfig{T} = DerivativeConfig(f!, y, x), ::Val{CHK}=Val{true}()) where {T, CHK}
require_one_based_indexing(result, y)
CHK && checktag(T, f!, x)
ydual = cfg.duals
seed!(ydual, y)
Expand Down
2 changes: 2 additions & 0 deletions src/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This method assumes that `isa(f(x), Real)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function gradient(f, x::AbstractArray, cfg::GradientConfig{T} = GradientConfig(f, x), ::Val{CHK}=Val{true}()) where {T, CHK}
require_one_based_indexing(x)
CHK && checktag(T, f, x)
if chunksize(cfg) == length(x)
return vector_mode_gradient(f, x, cfg)
Expand All @@ -32,6 +33,7 @@ This method assumes that `isa(f(x), Real)`.
"""
function gradient!(result::Union{AbstractArray,DiffResult}, f::F, x::AbstractArray, cfg::GradientConfig{T} = GradientConfig(f, x), ::Val{CHK}=Val{true}()) where {T, CHK, F}
require_one_based_indexing(result, x)
CHK && checktag(T, f, x)
if chunksize(cfg) == length(x)
vector_mode_gradient!(result, f, x, cfg)
Expand Down
3 changes: 3 additions & 0 deletions src/hessian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This method assumes that `isa(f(x), Real)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function hessian(f, x::AbstractArray, cfg::HessianConfig{T} = HessianConfig(f, x), ::Val{CHK}=Val{true}()) where {T,CHK}
require_one_based_indexing(x)
CHK && checktag(T, f, x)
∇f = y -> gradient(f, y, cfg.gradient_config, Val{false}())
return jacobian(∇f, x, cfg.jacobian_config, Val{false}())
Expand All @@ -28,6 +29,7 @@ This method assumes that `isa(f(x), Real)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function hessian!(result::AbstractArray, f, x::AbstractArray, cfg::HessianConfig{T} = HessianConfig(f, x), ::Val{CHK}=Val{true}()) where {T,CHK}
require_one_based_indexing(result, x)
CHK && checktag(T, f, x)
∇f = y -> gradient(f, y, cfg.gradient_config, Val{false}())
jacobian!(result, ∇f, x, cfg.jacobian_config, Val{false}())
Expand Down Expand Up @@ -62,6 +64,7 @@ because `isa(result, DiffResult)`, `cfg` is constructed as `HessianConfig(f, res
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function hessian!(result::DiffResult, f, x::AbstractArray, cfg::HessianConfig{T} = HessianConfig(f, result, x), ::Val{CHK}=Val{true}()) where {T,CHK}
require_one_based_indexing(x)
CHK && checktag(T, f, x)
∇f! = InnerGradientForHess(result, cfg, f)
jacobian!(DiffResults.hessian(result), ∇f!, DiffResults.gradient(result), x, cfg.jacobian_config, Val{false}())
Expand Down
4 changes: 4 additions & 0 deletions src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This method assumes that `isa(f(x), AbstractArray)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function jacobian(f, x::AbstractArray, cfg::JacobianConfig{T} = JacobianConfig(f, x), ::Val{CHK}=Val{true}()) where {T,CHK}
require_one_based_indexing(x)
CHK && checktag(T, f, x)
if chunksize(cfg) == length(x)
return vector_mode_jacobian(f, x, cfg)
Expand All @@ -33,6 +34,7 @@ stored in `y`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function jacobian(f!, y::AbstractArray, x::AbstractArray, cfg::JacobianConfig{T} = JacobianConfig(f!, y, x), ::Val{CHK}=Val{true}()) where {T, CHK}
require_one_based_indexing(y, x)
CHK && checktag(T, f!, x)
if chunksize(cfg) == length(x)
return vector_mode_jacobian(f!, y, x, cfg)
Expand All @@ -53,6 +55,7 @@ This method assumes that `isa(f(x), AbstractArray)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function jacobian!(result::Union{AbstractArray,DiffResult}, f, x::AbstractArray, cfg::JacobianConfig{T} = JacobianConfig(f, x), ::Val{CHK}=Val{true}()) where {T, CHK}
require_one_based_indexing(result, x)
CHK && checktag(T, f, x)
if chunksize(cfg) == length(x)
vector_mode_jacobian!(result, f, x, cfg)
Expand All @@ -73,6 +76,7 @@ This method assumes that `isa(f(x), AbstractArray)`.
Set `check` to `Val{false}()` to disable tag checking. This can lead to perturbation confusion, so should be used with care.
"""
function jacobian!(result::Union{AbstractArray,DiffResult}, f!, y::AbstractArray, x::AbstractArray, cfg::JacobianConfig{T} = JacobianConfig(f!, y, x), ::Val{CHK}=Val{true}()) where {T,CHK}
require_one_based_indexing(result, y, x)
CHK && checktag(T, f!, x)
if chunksize(cfg) == length(x)
vector_mode_jacobian!(result, f!, y, x, cfg)
Expand Down
5 changes: 5 additions & 0 deletions src/prelude.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ function qualified_cse!(expr)
end
return cse_expr
end

# This allows us to call `Base.require_one_based_indexing` in `gradient!` etc:
Base.has_offset_axes(::DiffResults.DiffResult) = false
# And this is only needed for VERSION < v"1.2"
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))

0 comments on commit be62c4b

Please sign in to comment.