Skip to content

Commit

Permalink
Fix errors with Adapt 3.3.0 (#161)
Browse files Browse the repository at this point in the history
* Fix errors with Adapt 3.3.0

* Use `@non_differentiable`

* Bump version

* Do not drop ChainRules dependency
  • Loading branch information
devmotion authored Apr 19, 2021
1 parent f074b8d commit 978b1fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DistributionsAD"
uuid = "ced4e74d-a319-5a8a-b0ac-84af2272839c"
version = "0.6.21"
version = "0.6.22"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -25,7 +25,7 @@ ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"
[compat]
Adapt = "2, 3"
ChainRules = "0.7"
ChainRulesCore = "0.9.9"
ChainRulesCore = "0.9.21"
Compat = "3.6"
DiffRules = "0.1, 1.0"
Distributions = "0.23.3, 0.24"
Expand Down
20 changes: 11 additions & 9 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ end
# Tracker's implementation of ldiv isn't good. We'll use Zygote's instead.
zygote_ldiv(A::AbstractMatrix, B::AbstractVecOrMat) = A \ B

# fixes `randn` on GPU (https://github.com/TuringLang/DistributionsAD.jl/pull/108)
function adapt_randn(rng::AbstractRNG, x::AbstractArray, dims...)
adapt(typeof(x), randn(rng, eltype(x), dims...))
return adapt_randn(rng, eltype(x), x, dims...)
end

# TODO: should be replaced by @non_differentiable when
# https://github.com/JuliaDiff/ChainRulesCore.jl/issues/212 is fixed
function ChainRules.rrule(::typeof(adapt_randn), rng::AbstractRNG, x::AbstractArray, dims...)
function adapt_randn_pullback(ΔQ)
return (NO_FIELDS, Zero(), Zero(), map(_ -> Zero(), dims)...)
end
adapt_randn(rng, x, dims...), adapt_randn_pullback
function adapt_randn(rng::AbstractRNG, ::Type{T}, x::AbstractArray, dims...) where {T}
return adapt(parameterless_type(x), randn(rng, T, dims...))
end

# required by Adapt >= 3.3.0: https://github.com/SciML/OrdinaryDiffEq.jl/issues/1369
Base.@pure __parameterless_type(T) = Base.typename(T).wrapper
parameterless_type(x) = parameterless_type(typeof(x))
parameterless_type(x::Type) = __parameterless_type(x)

@non_differentiable adapt_randn(::Any...)
2 changes: 1 addition & 1 deletion src/forwarddiff.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function adapt_randn(rng::AbstractRNG, x::AbstractArray{<:ForwardDiff.Dual}, dims...)
adapt(typeof(x), randn(rng, ForwardDiff.valtype(eltype(x)), dims...))
return adapt_randn(rng, ForwardDiff.valtype(eltype(x)), x, dims...)
end

## Binomial ##
Expand Down

2 comments on commit 978b1fe

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/34683

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.22 -m "<description of version>" 978b1fece5784369ebbf8e7d4cbebb5034ff4961
git push origin v0.6.22

Please sign in to comment.