Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc v0.7 deprecation fixes #293

Merged
merged 1 commit into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.6.0
Compat 0.31.0
Compat 0.45.0
StaticArrays 0.5.0
DiffResults 0.0.1
DiffRules 0.0.1
Expand Down
4 changes: 2 additions & 2 deletions docs/src/user/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ SIMD instructions (i.e. not starting Julia with `-O3`):
julia> using ForwardDiff: Dual

julia> a = Dual(1., 2., 3., 4.)
Dual{Void}(1.0,2.0,3.0,4.0)
Dual{Nothing}(1.0,2.0,3.0,4.0)

julia> b = Dual(5., 6., 7., 8.)
Dual{Void}(5.0,6.0,7.0,8.0)
Dual{Nothing}(5.0,6.0,7.0,8.0)

julia> @code_llvm a + b

Expand Down
2 changes: 1 addition & 1 deletion src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Tag(f::F, ::Type{V}) where {F,V}
Tag{F,V}()
end

Tag(::Void, ::Type{V}) where {V} = nothing
Tag(::Nothing, ::Type{V}) where {V} = nothing


@inline function ≺(::Type{Tag{F1,V1}}, ::Type{Tag{F2,V2}}) where {F1,V1,F2,V2}
Expand Down
12 changes: 6 additions & 6 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end
@inline (::Type{Dual{T}})(value::Real, partials::Real...) where {T} = Dual{T}(value, partials)
@inline (::Type{Dual{T}})(value::V, ::Chunk{N}, p::Val{i}) where {T,V<:Real,N,i} = Dual{T}(value, single_seed(Partials{N,V}, p))

@inline Dual(args...) = Dual{Void}(args...)
@inline Dual(args...) = Dual{Nothing}(args...)

##############################
# Utility/Accessor Functions #
Expand Down Expand Up @@ -84,8 +84,8 @@ partials(::Type{T}, d::Dual{S}, i...) where {T,S} = throw(DualMismatchError(T,S)
@inline valtype(::Dual{T,V,N}) where {T,V,N} = V
@inline valtype(::Type{Dual{T,V,N}}) where {T,V,N} = V

@inline tagtype(::V) where {V} = Void
@inline tagtype(::Type{V}) where {V} = Void
@inline tagtype(::V) where {V} = Nothing
@inline tagtype(::Type{V}) where {V} = Nothing
@inline tagtype(::Dual{T,V,N}) where {T,V,N} = T
@inline tagtype(::Type{Dual{T,V,N}}) where {T,V,N} = T

Expand Down Expand Up @@ -302,11 +302,11 @@ for R in (:BigFloat, :Bool, :Irrational, :Real)
end

Base.convert(::Type{Dual{T,V,N}}, d::Dual{T}) where {T,V<:Real,N} = Dual{T}(convert(V, value(d)), convert(Partials{N,V}, partials(d)))
Base.convert(::Type{Dual{T,V,N}}, x::Real) where {T,V<:Real,N} = Dual{T}(V(x), zero(Partials{N,V}))
Base.convert(::Type{Dual{T,V,N}}, x::Real) where {T,V<:Real,N} = Dual{T}(convert(V, x), zero(Partials{N,V}))
Base.convert(::Type{D}, d::D) where {D<:Dual} = d

Base.float(d::Dual{T,V,N}) where {T,V,N} = Dual{T,promote_type(V, Float16),N}(d)
Base.AbstractFloat(d::Dual{T,V,N}) where {T,V,N} = Dual{T,promote_type(V, Float16),N}(d)
Base.float(d::Dual{T,V,N}) where {T,V,N} = convert(Dual{T,promote_type(V, Float16),N}, d)
Base.AbstractFloat(d::Dual{T,V,N}) where {T,V,N} = convert(Dual{T,promote_type(V, Float16),N}, d)

###################################
# General Mathematical Operations #
Expand Down
6 changes: 2 additions & 4 deletions src/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ function extract_gradient!(::Type{T}, result::DiffResult, dual::Dual) where {T}
return result
end

extract_gradient!(::Type{T}, result::AbstractArray, y::Real) where {T} =
fill!(result, zero(y))
extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T}=
copy!(result, partials(T, dual))
extract_gradient!(::Type{T}, result::AbstractArray, y::Real) where {T} = fill!(result, zero(y))
extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T}= copyto!(result, partials(T, dual))

function extract_gradient_chunk!(::Type{T}, result, dual, index, chunksize) where {T}
offset = index - 1
Expand Down
10 changes: 4 additions & 6 deletions test/ConfusionTest.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module ConfusionTest

using Base.Test
using Compat
using Compat.Test
using ForwardDiff

using Base.Test


# Perturbation Confusion (Issue #83) #
#------------------------------------#

Expand Down Expand Up @@ -37,13 +35,13 @@ L(t,q,q̇) = m/2 * dot(q̇,q̇) - m*g*q[2]

∂L∂q̇(L, t, q, q̇) = ForwardDiff.gradient(a->L(t,q,a), q̇)
Dqq̇(L, t, q, q̇) = ForwardDiff.jacobian(a->∂L∂q̇(L,t,a,q̇), q)
@test Dqq̇(L, t, q, q̇) == zeros(2,2)
@test Dqq̇(L, t, q, q̇) == fill(0.0, 2, 2)


q = [1,2]
p = [5,6]
function Legendre_transformation(F, w)
z = zeros(w)
z = fill(0.0, size(w))
M = ForwardDiff.hessian(F, z)
b = ForwardDiff.gradient(F, z)
v = cholfact(M)\(w-b)
Expand Down
3 changes: 2 additions & 1 deletion test/DeprecatedTest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DeprecatedTest

using Base.Test
using Compat
using Compat.Test
using ForwardDiff, DiffResults

using ForwardDiff: AbstractConfig, GradientConfig,
Expand Down
5 changes: 3 additions & 2 deletions test/DerivativeTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module DerivativeTest

import Calculus

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using DiffTests

Expand Down Expand Up @@ -49,7 +50,7 @@ end
for f! in DiffTests.INPLACE_NUMBER_TO_ARRAY_FUNCS
println(" ...testing $f!")
m, n = 3, 2
y = zeros(m, n)
y = fill(0.0, m, n)
f = x -> (tmp = similar(y, promote_type(eltype(y), typeof(x)), m, n); f!(tmp, x); tmp)
v = f(x)
cfg = ForwardDiff.DerivativeConfig(f!, y, x)
Expand Down
7 changes: 4 additions & 3 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DualTest

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff: Partials, Dual, value, partials

Expand Down Expand Up @@ -302,8 +303,8 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
@test convert(Dual{TestTag(),Dual{TestTag(),V,M},N}, NESTED_FDNUM) === NESTED_FDNUM
@test convert(Dual{TestTag(),WIDE_T,N}, PRIMAL) === Dual{TestTag()}(WIDE_T(PRIMAL), zero(Partials{N,WIDE_T}))
@test convert(Dual{TestTag(),Dual{TestTag(),WIDE_T,M},N}, PRIMAL) === Dual{TestTag()}(Dual{TestTag()}(WIDE_T(PRIMAL), zero(Partials{M,WIDE_T})), zero(Partials{N,Dual{TestTag(),V,M}}))
@test convert(Dual{TestTag(),Dual{TestTag(),V,M},N}, FDNUM) === Dual{TestTag()}(Dual{TestTag(),V,M}(PRIMAL), convert(Partials{N,Dual{TestTag(),V,M}}, PARTIALS))
@test convert(Dual{TestTag(),Dual{TestTag(),WIDE_T,M},N}, FDNUM) === Dual{TestTag()}(Dual{TestTag(),WIDE_T,M}(PRIMAL), convert(Partials{N,Dual{TestTag(),WIDE_T,M}}, PARTIALS))
@test convert(Dual{TestTag(),Dual{TestTag(),V,M},N}, FDNUM) === Dual{TestTag()}(convert(Dual{TestTag(),V,M}, PRIMAL), convert(Partials{N,Dual{TestTag(),V,M}}, PARTIALS))
@test convert(Dual{TestTag(),Dual{TestTag(),WIDE_T,M},N}, FDNUM) === Dual{TestTag()}(convert(Dual{TestTag(),WIDE_T,M}, PRIMAL), convert(Partials{N,Dual{TestTag(),WIDE_T,M}}, PARTIALS))

##############
# Arithmetic #
Expand Down
3 changes: 2 additions & 1 deletion test/GradientTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module GradientTest

import Calculus

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff: Dual, Tag
using StaticArrays
Expand Down
3 changes: 2 additions & 1 deletion test/HessianTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module HessianTest

import Calculus

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff: Dual, Tag
using StaticArrays
Expand Down
47 changes: 24 additions & 23 deletions test/JacobianTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module JacobianTest

import Calculus

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff: Dual, Tag, JacobianConfig
using StaticArrays
Expand All @@ -22,7 +23,7 @@ f! = (y, x) -> begin
y[4] = x[3]
return nothing
end
f = x -> (y = zeros(promote_type(eltype(x), Float64), 4); f!(y, x); return y)
f = x -> (y = fill(zero(promote_type(eltype(x), Float64)), 4); f!(y, x); return y)
x = [1, 2, 3]
v = f(x)
j = [0.8242369704835132 0.4121184852417566 -10.933563142616123
Expand All @@ -34,7 +35,7 @@ for c in (1, 2, 3), tags in ((nothing, nothing),
(Tag(f, eltype(x)), Tag(f!, eltype(x))))
println(" ...running hardcoded test with chunk size = $c and tag = $tags")
cfg = JacobianConfig(f, x, ForwardDiff.Chunk{c}(), tags[1])
ycfg = JacobianConfig(f!, zeros(4), x, ForwardDiff.Chunk{c}(), tags[2])
ycfg = JacobianConfig(f!, fill(0.0, 4), x, ForwardDiff.Chunk{c}(), tags[2])

@test eltype(cfg) == Dual{typeof(tags[1]), eltype(x), c}
@test eltype(ycfg) == Dual{typeof(tags[2]), eltype(x), c}
Expand All @@ -43,47 +44,47 @@ for c in (1, 2, 3), tags in ((nothing, nothing),
@test isapprox(j, ForwardDiff.jacobian(f, x, cfg))
@test isapprox(j, ForwardDiff.jacobian(f, x))

out = zeros(4, 3)
out = fill(0.0, 4, 3)
ForwardDiff.jacobian!(out, f, x, cfg)
@test isapprox(out, j)

out = zeros(4, 3)
out = fill(0.0, 4, 3)
ForwardDiff.jacobian!(out, f, x)
@test isapprox(out, j)

out = DiffResults.JacobianResult(zeros(4), zeros(3))
out = DiffResults.JacobianResult(fill(0.0, 4), fill(0.0, 3))
ForwardDiff.jacobian!(out, f, x, cfg)
@test isapprox(DiffResults.value(out), v)
@test isapprox(DiffResults.jacobian(out), j)

# testing f!(y, x)
y = zeros(4)
y = fill(0.0, 4)
@test isapprox(j, ForwardDiff.jacobian(f!, y, x, ycfg))
@test isapprox(v, y)

y = zeros(4)
y = fill(0.0, 4)
@test isapprox(j, ForwardDiff.jacobian(f!, y, x))
@test isapprox(v, y)

out, y = zeros(4, 3), zeros(4)
out, y = fill(0.0, 4, 3), fill(0.0, 4)
ForwardDiff.jacobian!(out, f!, y, x, ycfg)
@test isapprox(out, j)
@test isapprox(y, v)

out, y = zeros(4, 3), zeros(4)
out, y = fill(0.0, 4, 3), fill(0.0, 4)
ForwardDiff.jacobian!(out, f!, y, x)
@test isapprox(out, j)
@test isapprox(y, v)

out = DiffResults.JacobianResult(zeros(4), zeros(3))
y = zeros(4)
out = DiffResults.JacobianResult(fill(0.0, 4), fill(0.0, 3))
y = fill(0.0, 4)
ForwardDiff.jacobian!(out, f!, y, x, ycfg)
@test DiffResults.value(out) == y
@test isapprox(y, v)
@test isapprox(DiffResults.jacobian(out), j)

out = DiffResults.JacobianResult(zeros(4), zeros(3))
y = zeros(4)
out = DiffResults.JacobianResult(fill(0.0, 4), fill(0.0, 3))
y = fill(0.0, 4)
ForwardDiff.jacobian!(out, f!, y, x)
@test DiffResults.value(out) == y
@test isapprox(y, v)
Expand Down Expand Up @@ -125,33 +126,33 @@ for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
end

for f! in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
v = zeros(Y)
v = fill!(similar(Y), 0.0)
f!(v, X)
j = ForwardDiff.jacobian(f!, zeros(Y), X)
@test isapprox(j, Calculus.jacobian(x -> (y = zeros(Y); f!(y, x); vec(y)), X, :forward), atol=FINITEDIFF_ERROR)
j = ForwardDiff.jacobian(f!, fill!(similar(Y), 0.0), X)
@test isapprox(j, Calculus.jacobian(x -> (y = fill!(similar(Y), 0.0); f!(y, x); vec(y)), X, :forward), atol=FINITEDIFF_ERROR)
for c in CHUNK_SIZES, tag in (nothing, Tag(f!, eltype(X)))
println(" ...testing $(f!) with chunk size = $c and tag = $tag")
ycfg = JacobianConfig(f!, zeros(Y), X, ForwardDiff.Chunk{c}(), tag)
ycfg = JacobianConfig(f!, fill!(similar(Y), 0.0), X, ForwardDiff.Chunk{c}(), tag)

y = zeros(Y)
y = fill!(similar(Y), 0.0)
out = ForwardDiff.jacobian(f!, y, X, ycfg)
@test isapprox(y, v)
@test isapprox(out, j)

y = zeros(Y)
y = fill!(similar(Y), 0.0)
out = similar(Y, length(Y), length(X))
ForwardDiff.jacobian!(out, f!, y, X)
@test isapprox(y, v)
@test isapprox(out, j)

y = zeros(Y)
y = fill!(similar(Y), 0.0)
out = DiffResults.JacobianResult(y, X)
ForwardDiff.jacobian!(out, f!, y, X)
@test DiffResults.value(out) == y
@test isapprox(y, v)
@test isapprox(DiffResults.jacobian(out), j)

y = zeros(Y)
y = fill!(similar(Y), 0.0)
out = DiffResults.JacobianResult(y, X)
ForwardDiff.jacobian!(out, f!, y, X, ycfg)
@test DiffResults.value(out) == y
Expand Down Expand Up @@ -205,7 +206,7 @@ result3 = ForwardDiff.jacobian!(result3, diff, sx, scfg)
@test DiffResults.jacobian(result2) == DiffResults.jacobian(result)
@test DiffResults.jacobian(result3) == DiffResults.jacobian(result)

sy = zeros(SVector{6,eltype(sx)})
sy = @SVector fill(zero(eltype(sx)), 6)
sresult1 = DiffResults.JacobianResult(sy, sx)
sresult2 = DiffResults.JacobianResult(sy, sx)
sresult3 = DiffResults.JacobianResult(sy, sx)
Expand Down
9 changes: 5 additions & 4 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module MiscTest

import NaNMath

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using DiffTests

Expand Down Expand Up @@ -110,8 +111,8 @@ jinvx = ForwardDiff.jacobian(inv, x)
#-----------------------------------#

N = 4
a = ones(N)
jac0 = reshape(vcat([[zeros(N*(i-1)); a; zeros(N^2-N*i)] for i = 1:N]...), N^2, N)
a = fill(1.0, N)
jac0 = reshape(vcat([[fill(0.0, N*(i-1)); a; fill(0.0, N^2-N*i)] for i = 1:N]...), N^2, N)

for op in (:-, :+, :.-, :.+, :./, :.*)
f = @eval x -> [$op(x[1], a); $op(x[2], a); $op(x[3], a); $op(x[4], a)]
Expand Down Expand Up @@ -142,7 +143,7 @@ z267 = ([(1, (2), [(3, (4, 5, [1, 2, (3, (4, 5), [5])]), (5))])])
let z = z267
g = x -> f267(z, x)
h = x -> g(x)
@test ForwardDiff.hessian(h, [1.]) == zeros(1, 1)
@test ForwardDiff.hessian(h, [1.]) == fill(0.0, 1, 1)
end

end
11 changes: 6 additions & 5 deletions test/PartialsTest.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module PartialsTest

using Base.Test
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff.Partials
using ForwardDiff: Partials

samerng() = MersenneTwister(1)

for N in (0, 3), T in (Int, Float32, Float64)
println(" ...testing Partials{$N,$T}")

VALUES = (rand(T,N)...)
VALUES = (rand(T,N)...,)
PARTIALS = Partials{N,T}(VALUES)

VALUES2 = (rand(T,N)...)
VALUES2 = (rand(T,N)...,)
PARTIALS2 = Partials{N,T}(VALUES2)

##############################
Expand Down Expand Up @@ -122,7 +123,7 @@ for N in (0, 3), T in (Int, Float32, Float64)
@test ForwardDiff._mul_partials(PARTIALS, ZERO_PARTIALS, X, Y) == X * PARTIALS

if ForwardDiff.NANSAFE_MODE_ENABLED
ZEROS = Partials((zeros(T, N)...))
ZEROS = Partials((fill(zero(T), N)...,))

@test (NaN * ZEROS).values == ZEROS.values
@test (Inf * ZEROS).values == ZEROS.values
Expand Down
3 changes: 2 additions & 1 deletion test/SIMDTest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module SIMDTest

using Base.Test
using Compat
using Compat.Test
using ForwardDiff: Dual, valtype

const DUALS = (Dual(1., 2., 3., 4.),
Expand Down
Loading