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

More restrictive @non_differentiable rand #263

Merged
merged 16 commits into from
Sep 9, 2020
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
23 changes: 23 additions & 0 deletions test/rulesets/Random/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,27 @@
@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
nmheim marked this conversation as resolved.
Show resolved Hide resolved
x, dΩ = frule((), rand, args...)
nmheim marked this conversation as resolved.
Show resolved Hide resolved
@test x isa xType
@test dΩ isa DoesNotExist

x, pb = rrule(rand, args...)
@test x isa xType
@test first(pb(10)) isa Zero
nmheim marked this conversation as resolved.
Show resolved Hide resolved
end

nmheim marked this conversation as resolved.
Show resolved Hide resolved
@test frule((), rand, ones(2,2)) === nothing
nmheim marked this conversation as resolved.
Show resolved Hide resolved
@test rrule(rand, ones(2,2)) === nothing
end
end