Skip to content

Commit

Permalink
Forbid offsets (#586)
Browse files Browse the repository at this point in the history
* no offsets

* ... and no piracy

* import from Base
  • Loading branch information
mcabbott authored Dec 13, 2022
1 parent 60a6230 commit 76335e6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ForwardDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using StaticArrays
using Preferences
using Random
using LinearAlgebra

using Base: require_one_based_indexing
import Printf
import NaNMath
import SpecialFunctions
Expand Down
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}
result isa DiffResult || 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}
result isa DiffResult ? require_one_based_indexing(y) : 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}
result isa DiffResult ? require_one_based_indexing(x) : 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}
result isa DiffResult ? require_one_based_indexing(x) : 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}
result isa DiffResult ? require_one_based_indexing(y, x) : 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

0 comments on commit 76335e6

Please sign in to comment.