Skip to content

Commit 3ffc785

Browse files
jrevelsmlubin
authored andcommitted
upgrade to ForwardDiff v0.5.0 (#42)
* upgrade to ForwardDiff v0.5.0 * fix Julia/Travis versions in REQUIRE * fix DualNumbers.Dual constructors in tests * upper-bound ForwardDiff at next minor version just to be safe
1 parent 7e4c4af commit 3ffc785

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.5
87
- 0.6
98
- nightly
109
notifications:

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
julia 0.5
1+
julia 0.6.0-rc
22
Calculus
33
DataStructures
44
MathProgBase
55
NaNMath 0.2.1
6-
ForwardDiff 0.3 0.5
6+
ForwardDiff 0.5 0.6

src/ReverseDiffSparse.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ using Base.Meta
44
using ForwardDiff
55
import Calculus
66
import MathProgBase
7+
8+
const TAG = :rds_tag
9+
710
# Override basic math functions to return NaN instead of throwing errors.
811
# This is what NLP solvers expect, and
912
# sometimes the results aren't needed anyway,

src/forward.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,31 +250,31 @@ function forward_eval_ϵ{N,T}(storage::Vector{T},storage_ϵ::DenseVector{Forward
250250
if op == 3 # :*
251251
# Lazy approach for now
252252
anyzero = false
253-
tmp_prod = one(ForwardDiff.Dual{N,T})
253+
tmp_prod = one(ForwardDiff.Dual{TAG,T,N})
254254
for c_idx in children_idx
255255
ix = children_arr[c_idx]
256256
sval = storage[ix]
257-
gnum = ForwardDiff.Dual(sval,storage_ϵ[ix])
257+
gnum = ForwardDiff.Dual{TAG}(sval,storage_ϵ[ix])
258258
tmp_prod *= gnum
259259
anyzero = ifelse(sval*sval == zero(T), true, anyzero)
260260
end
261261
# By a quirk of floating-point numbers, we can have
262262
# anyzero == true && ForwardDiff.value(tmp_prod) != zero(T)
263263
if anyzero # inefficient
264264
for c_idx in children_idx
265-
prod_others = one(ForwardDiff.Dual{N,T})
265+
prod_others = one(ForwardDiff.Dual{TAG,T,N})
266266
for c_idx2 in children_idx
267267
(c_idx == c_idx2) && continue
268268
ix = children_arr[c_idx2]
269-
gnum = ForwardDiff.Dual(storage[ix],storage_ϵ[ix])
269+
gnum = ForwardDiff.Dual{TAG}(storage[ix],storage_ϵ[ix])
270270
prod_others *= gnum
271271
end
272272
partials_storage_ϵ[children_arr[c_idx]] = ForwardDiff.partials(prod_others)
273273
end
274274
else
275275
for c_idx in children_idx
276276
ix = children_arr[c_idx]
277-
prod_others = tmp_prod/ForwardDiff.Dual(storage[ix],storage_ϵ[ix])
277+
prod_others = tmp_prod/ForwardDiff.Dual{TAG}(storage[ix],storage_ϵ[ix])
278278
partials_storage_ϵ[ix] = ForwardDiff.partials(prod_others)
279279
end
280280
end
@@ -288,14 +288,14 @@ function forward_eval_ϵ{N,T}(storage::Vector{T},storage_ϵ::DenseVector{Forward
288288
@inbounds base_ϵ = storage_ϵ[ix1]
289289
@inbounds exponent = storage[ix2]
290290
@inbounds exponent_ϵ = storage_ϵ[ix2]
291-
base_gnum = ForwardDiff.Dual(base,base_ϵ)
292-
exponent_gnum = ForwardDiff.Dual(exponent,exponent_ϵ)
291+
base_gnum = ForwardDiff.Dual{TAG}(base,base_ϵ)
292+
exponent_gnum = ForwardDiff.Dual{TAG}(exponent,exponent_ϵ)
293293
if exponent == 2
294294
partials_storage_ϵ[ix1] = 2*base_ϵ
295295
else
296296
partials_storage_ϵ[ix1] = ForwardDiff.partials(exponent_gnum*pow(base_gnum,exponent_gnum-1))
297297
end
298-
result_gnum = ForwardDiff.Dual(storage[k],storage_ϵ[k])
298+
result_gnum = ForwardDiff.Dual{TAG}(storage[k],storage_ϵ[k])
299299
partials_storage_ϵ[ix2] = ForwardDiff.partials(result_gnum*log(base_gnum))
300300
elseif op == 5 # :/
301301
@assert n_children == 2
@@ -307,9 +307,9 @@ function forward_eval_ϵ{N,T}(storage::Vector{T},storage_ϵ::DenseVector{Forward
307307
@inbounds numerator_ϵ = storage_ϵ[ix1]
308308
@inbounds denominator = storage[ix2]
309309
@inbounds denominator_ϵ = storage_ϵ[ix2]
310-
recip_denominator = 1/ForwardDiff.Dual(denominator,denominator_ϵ)
310+
recip_denominator = 1/ForwardDiff.Dual{TAG}(denominator,denominator_ϵ)
311311
partials_storage_ϵ[ix1] = ForwardDiff.partials(recip_denominator)
312-
partials_storage_ϵ[ix2] = ForwardDiff.partials(-ForwardDiff.Dual(numerator,numerator_ϵ)*recip_denominator*recip_denominator)
312+
partials_storage_ϵ[ix2] = ForwardDiff.partials(-ForwardDiff.Dual{TAG}(numerator,numerator_ϵ)*recip_denominator*recip_denominator)
313313
elseif op >= USER_OPERATOR_ID_START
314314
error("User-defined operators not supported for hessian computations")
315315
end

test/runtests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,14 @@ function dualforward(ex, x; ignore_nan=false)
315315
@test isapprox(fval_ϵ[1], dot(grad,ones(length(x))))
316316

317317
# compare with running dual numbers
318-
forward_dual_storage = zeros(Dual{Float64},length(nd))
319-
partials_dual_storage = zeros(Dual{Float64},length(nd))
320-
output_dual_storage = zeros(Dual{Float64},length(x))
321-
reverse_dual_storage = zeros(Dual{Float64},length(nd))
322-
x_dual = [Dual(x[i],1.0) for i in 1:length(x)]
318+
forward_dual_storage = zeros(DualNumbers.Dual{Float64},length(nd))
319+
partials_dual_storage = zeros(DualNumbers.Dual{Float64},length(nd))
320+
output_dual_storage = zeros(DualNumbers.Dual{Float64},length(x))
321+
reverse_dual_storage = zeros(DualNumbers.Dual{Float64},length(nd))
322+
x_dual = [DualNumbers.Dual(x[i],1.0) for i in 1:length(x)]
323323
fval = forward_eval(forward_dual_storage,partials_dual_storage,nd,adj,const_values,[],x_dual,[])
324324
reverse_eval(reverse_dual_storage,partials_dual_storage,nd,adj)
325-
reverse_extract(output_dual_storage,reverse_dual_storage,nd,adj,[],Dual(2.0))
325+
reverse_extract(output_dual_storage,reverse_dual_storage,nd,adj,[],DualNumbers.Dual(2.0))
326326
for k in 1:length(nd)
327327
@test isapprox(epsilon(forward_dual_storage[k]), forward_storage_ϵ[k][1])
328328
if !(isnan(epsilon(partials_dual_storage[k])) && ignore_nan)

0 commit comments

Comments
 (0)