Skip to content

Commit

Permalink
Merge pull request #263 from nmheim/nh/rand
Browse files Browse the repository at this point in the history
More restrictive @non_differentiable rand
  • Loading branch information
oxinabox committed Sep 9, 2020
2 parents 0baf7bb + 84c47c4 commit c8679c6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "0.7.17"
version = "0.7.18"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
26 changes: 21 additions & 5 deletions src/rulesets/Random/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ end
@non_differentiable Random.randexp(::AbstractRNG)
@non_differentiable Random.randstring(::AbstractRNG)

@non_differentiable rand(::Any)
@non_differentiable rand(::Any, ::Any)
@non_differentiable rand(::Any, ::Any, ::Any)
@non_differentiable rand(::Any, ::Any, ::Any, ::Any)
@non_differentiable rand(::Any, ::Any, ::Any, ::Any, ::Any)
@non_differentiable rand()
@non_differentiable rand(::AbstractRNG)
@non_differentiable rand(::AbstractRNG, ::Random.Sampler)
@non_differentiable rand(::AbstractRNG, ::Integer)
@non_differentiable rand(::AbstractRNG, ::Integer, ::Integer)
@non_differentiable rand(::AbstractRNG, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::AbstractRNG, ::Integer, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::AbstractRNG, ::Integer, ::Integer, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Type{<:Real})
@non_differentiable rand(::Type{<:Real}, ::Tuple)
@non_differentiable rand(::Type{<:Real}, ::Integer)
@non_differentiable rand(::Type{<:Real}, ::Integer, ::Integer)
@non_differentiable rand(::Type{<:Real}, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Type{<:Real}, ::Integer, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Type{<:Real}, ::Integer, ::Integer, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Integer)
@non_differentiable rand(::Integer, ::Integer)
@non_differentiable rand(::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Integer, ::Integer, ::Integer, ::Integer)
@non_differentiable rand(::Integer, ::Integer, ::Integer, ::Integer, ::Integer)


# There are many different 1-3 arg methods, but not varargs
@non_differentiable rand!(::Any)
Expand Down
39 changes: 39 additions & 0 deletions test/rulesets/Random/random.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Simple Distributions like object for testing purposes
struct NormalDistribution
μ
σ
end
Random.rand(d::NormalDistribution) = d.μ + d.σ*randn()

@testset "random" begin
@testset "MersenneTwister" begin
@testset "no args" begin
Expand All @@ -19,4 +26,36 @@
@test all(map(x -> x isa Zero, pb(10)))
end
end

@testset "rand" begin
non_differentiables = [
((), Float64),
((MersenneTwister(123),), Float64),
((MersenneTwister(123),2,2), Matrix{<:Float64}),
((Float32,), Float32),
((Float32,2,2), Matrix{<:Float32}),
((Float32,(2,2)), Matrix{<:Float32}),
((2,2), Matrix{<:Float64}),
]

for (args, xType) in non_differentiables
x, dΩ = frule((Zero(), randn(args...)), rand, args...)
@test x isa xType
@testisa DoesNotExist

x, pb = rrule(rand, args...)
@test x isa xType
dself, dargs = Iterators.peel(pb(10.0))
@test dself isa Zero
@test all(darg isa DoesNotExist for darg in dargs)
end

# Make sure that we do *not* have these set as non_differentiable. as they are differentiable
@test nothing === frule(
(Zero(), Composite{NormalDistribution}=0.5=2.0)),
rand,
NormalDistribution(0.1,1.5),
)
@test rrule(rand, NormalDistribution(0.1,1.5)) === nothing
end
end

2 comments on commit c8679c6

@oxinabox
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 register()

@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/21144

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.7.18 -m "<description of version>" c8679c6652eff4deb7ff075d87d91e876842ae59
git push origin v0.7.18

Please sign in to comment.