From 0ee831b2132128fab97aa826dfa786675e1bface Mon Sep 17 00:00:00 2001 From: Harsha Byadarahalli Mahesh Date: Thu, 19 Aug 2021 15:14:19 +0530 Subject: [PATCH] Fixes warnings during REPL startup while using custom SYSIMG's (#185) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixes warnings during REPL startup while using custom SYSIMG's We encounter following warning during Julia REPL startup if we include DistributionsAD in the default SYSIMG, this commit fixes that issue ``` ┌ Warning: Error requiring `ForwardDiff` from `DistributionsAD` │ exception = │ LoadError: SystemError: opening file "/home/bmharsha/.julia/packages/DistributionsAD/pj8zf/src/reversediffx.jl": No such file or directory │ Stacktrace: │ [1] systemerror(p::String, errno::Int32; extrainfo::Nothing) │ @ Base ./error.jl:168 │ [2] #systemerror#62 │ @ ./error.jl:167 [inlined] │ [3] systemerror │ @ ./error.jl:167 [inlined] │ [4] open(fname::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing) │ @ Base ./iostream.jl:293 │ [5] open │ @ ./iostream.jl:282 [inlined] │ [6] open(f::Base.var"#326#327"{String}, args::String; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) │ @ Base ./io.jl:328 │ [7] open │ @ ./io.jl:328 [inlined] │ [8] read │ @ ./io.jl:434 [inlined] │ [9] _include(mapexpr::Function, mod::Module, _path::String) │ @ Base ./loading.jl:1166 │ [10] include(mod::Module, _path::String) │ @ Base ./Base.jl:386 │ [11] include(x::String) │ @ DistributionsAD.ReverseDiffX ./reversediff.jl:1 │ [12] top-level scope │ @ reversediff.jl:55 │ [13] eval │ @ ./boot.jl:360 [inlined] │ [14] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String) │ @ Base ./loading.jl:1116 │ [15] include_string(m::Module, txt::String, fname::String) │ @ Base ./loading.jl:1126 │ [16] macro expansion │ @ ~/.julia/packages/Requires/7Ncym/src/Requires.jl:42 [inlined] │ [17] top-level scope │ @ ~/.julia/packages/DistributionsAD/pj8zf/src/DistributionsAD.jl:74 │ [18] eval │ @ ./boot.jl:360 [inlined] │ [19] eval │ @ ~/.julia/packages/DistributionsAD/pj8zf/src/DistributionsAD.jl:1 [inlined] │ [20] (::DistributionsAD.var"#331#334")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:99 │ [21] err(f::Any, listener::Module, modname::String) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:47 │ [22] (::DistributionsAD.var"#330#333")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:98 │ [23] withpath(f::Any, path::String) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:37 │ [24] (::DistributionsAD.var"#329#332")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:97 │ [25] listenpkg(f::Any, pkg::Base.PkgId) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:20 │ [26] macro expansion │ @ ~/.julia/packages/Requires/7Ncym/src/require.jl:95 [inlined] │ [27] top-level scope │ @ ~/.julia/packages/DistributionsAD/pj8zf/src/DistributionsAD.jl:73 │ [28] eval │ @ ./boot.jl:360 [inlined] │ [29] eval │ @ ~/.julia/packages/DistributionsAD/pj8zf/src/DistributionsAD.jl:1 [inlined] │ [30] (::DistributionsAD.var"#307#320")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:99 │ [31] err(f::Any, listener::Module, modname::String) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:47 │ [32] (::DistributionsAD.var"#306#319")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:98 │ [33] withpath(f::Any, path::String) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:37 │ [34] (::DistributionsAD.var"#305#318")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/require.jl:97 │ [35] listenpkg(f::Any, pkg::Base.PkgId) │ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:20 │ [36] macro expansion │ @ ~/.julia/packages/Requires/7Ncym/src/require.jl:95 [inlined] │ [37] macro expansion │ @ ~/.julia/packages/DistributionsAD/pj8zf/src/DistributionsAD.jl:70 [inlined] │ [38] (::DistributionsAD.var"#301#314")() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/init.jl:11 │ [39] __init__() │ @ DistributionsAD ~/.julia/packages/Requires/7Ncym/src/init.jl:18 │ in expression starting at reversediff.jl:1 └ @ Requires ~/.julia/packages/Requires/7Ncym/src/require.jl:49 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.6.2 (2021-07-14) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | [?2004h julia>   [?2004l ``` * Bumps the version number in Project.toml Preparing for a minor release * Reverts the usage of Requires.@include Macro --- Project.toml | 2 +- src/reversediff.jl | 79 ++++++++++++++++++++++++++++++++++++++++++- src/reversediffx.jl | 82 --------------------------------------------- 3 files changed, 79 insertions(+), 84 deletions(-) delete mode 100644 src/reversediffx.jl diff --git a/Project.toml b/Project.toml index 1fe747d..3f6c560 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DistributionsAD" uuid = "ced4e74d-a319-5a8a-b0ac-84af2272839c" -version = "0.6.28" +version = "0.6.29" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/reversediff.jl b/src/reversediff.jl index 4a48099..54344f1 100644 --- a/src/reversediff.jl +++ b/src/reversediff.jl @@ -52,7 +52,84 @@ using ..DistributionsAD: TuringPoissonBinomial, VectorOfMultivariate, FillVectorOfMultivariate -include("reversediffx.jl") +############### +## logsumexp ## +############### + +logsumexp(x::TrackedArray; dims=:) = track(logsumexp, x, dims = dims) +@grad function logsumexp(x::AbstractArray; dims) + x_value = value(x) + lse = logsumexp(x_value; dims=dims) + return lse, Δ -> (Δ .* exp.(x_value .- lse),) +end + +############ +## linalg ## +############ + +function LinearAlgebra.cholesky(A::Symmetric{<:Any, <:TrackedMatrix}; check=true) + uplo = A.uplo == 'U' ? (:U) : (:L) + factors, info = symm_turing_chol(parent(A), check, uplo) + return Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) +end +function LinearAlgebra.cholesky(A::TrackedMatrix; check=true) + factors, info = turing_chol(A, check) + return Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) +end + +function symm_turing_chol(x::TrackedArray{V,D}, check, uplo) where {V,D} + tp = tape(x) + x_value = value(x) + (factors,info), back = DistributionsAD.symm_turing_chol_back(x_value, check, uplo) + C = Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) + out = track(C.factors, D, tp) + record!(tp, SpecialInstruction, symm_turing_chol, (x, check, uplo), out, (back, issuccess(C))) + return out, C.info +end +function turing_chol(x::TrackedArray{V,D}, check) where {V,D} + tp = tape(x) + x_value = value(x) + (factors,info), back = DistributionsAD.turing_chol_back(x_value, check) + C = Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) + out = track(C.factors, D, tp) + record!(tp, SpecialInstruction, turing_chol, (x, check), out, (back, issuccess(C))) + return out, C.info +end + +for f in (:turing_chol, :symm_turing_chol) + @eval begin + @noinline function ReverseDiff.special_reverse_exec!( + instruction::SpecialInstruction{typeof($f)}, + ) + output = instruction.output + instruction.cache[2] || throw(PosDefException(C.info)) + input = instruction.input + input_deriv = deriv(input[1]) + P = instruction.cache[1] + input_deriv .+= P((factors = deriv(output),))[1] + unseed!(output) + return nothing + end + end +end + +@noinline function ReverseDiff.special_forward_exec!( + instruction::SpecialInstruction{typeof(turing_chol)}, +) + output, input = instruction.output, instruction.input + factors = turing_chol(value.(input)...)[1] + value!(output, factors) + return nothing +end + +@noinline function ReverseDiff.special_forward_exec!( + instruction::SpecialInstruction{typeof(symm_turing_chol)}, +) + output, input = instruction.output, instruction.input + factors = symm_turing_chol(value.(input)...)[1] + value!(output, factors) + return nothing +end adapt_randn(rng::Random.AbstractRNG, x::TrackedArray, dims...) = adapt_randn(rng, value(x), dims...) diff --git a/src/reversediffx.jl b/src/reversediffx.jl deleted file mode 100644 index b25b1ac..0000000 --- a/src/reversediffx.jl +++ /dev/null @@ -1,82 +0,0 @@ -# A lot of this module is adapted from Tracker.jl and ReverseDiff.jl -# ReverseDiff.jl is not actively developed but it would be nice to move the code in this -# module to ReverseDiff at some point. - -############### -## logsumexp ## -############### - -logsumexp(x::TrackedArray; dims=:) = track(logsumexp, x, dims = dims) -@grad function logsumexp(x::AbstractArray; dims) - x_value = value(x) - lse = logsumexp(x_value; dims=dims) - return lse, Δ -> (Δ .* exp.(x_value .- lse),) -end - -############ -## linalg ## -############ - -function LinearAlgebra.cholesky(A::Symmetric{<:Any, <:TrackedMatrix}; check=true) - uplo = A.uplo == 'U' ? (:U) : (:L) - factors, info = symm_turing_chol(parent(A), check, uplo) - return Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) -end -function LinearAlgebra.cholesky(A::TrackedMatrix; check=true) - factors, info = turing_chol(A, check) - return Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) -end - -function symm_turing_chol(x::TrackedArray{V,D}, check, uplo) where {V,D} - tp = tape(x) - x_value = value(x) - (factors,info), back = DistributionsAD.symm_turing_chol_back(x_value, check, uplo) - C = Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) - out = track(C.factors, D, tp) - record!(tp, SpecialInstruction, symm_turing_chol, (x, check, uplo), out, (back, issuccess(C))) - return out, C.info -end -function turing_chol(x::TrackedArray{V,D}, check) where {V,D} - tp = tape(x) - x_value = value(x) - (factors,info), back = DistributionsAD.turing_chol_back(x_value, check) - C = Cholesky{eltype(factors), typeof(factors)}(factors, 'U', info) - out = track(C.factors, D, tp) - record!(tp, SpecialInstruction, turing_chol, (x, check), out, (back, issuccess(C))) - return out, C.info -end - -for f in (:turing_chol, :symm_turing_chol) - @eval begin - @noinline function ReverseDiff.special_reverse_exec!( - instruction::SpecialInstruction{typeof($f)}, - ) - output = instruction.output - instruction.cache[2] || throw(PosDefException(C.info)) - input = instruction.input - input_deriv = deriv(input[1]) - P = instruction.cache[1] - input_deriv .+= P((factors = deriv(output),))[1] - unseed!(output) - return nothing - end - end -end - -@noinline function ReverseDiff.special_forward_exec!( - instruction::SpecialInstruction{typeof(turing_chol)}, -) - output, input = instruction.output, instruction.input - factors = turing_chol(value.(input)...)[1] - value!(output, factors) - return nothing -end - -@noinline function ReverseDiff.special_forward_exec!( - instruction::SpecialInstruction{typeof(symm_turing_chol)}, -) - output, input = instruction.output, instruction.input - factors = symm_turing_chol(value.(input)...)[1] - value!(output, factors) - return nothing -end