-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define rand defaults for AbstractProbabilisticProgram (#79)
This PR adds a 3-arg form of `rand` (suggested by @devmotion in TuringLang/DynamicPPL.jl#466 (comment)) to the interface for `AbstractProbabilisticProgram` and implements the default 1- and 2-arg methods that dispatch to this. Currently tests fail because this breaks the fallbacks for `GraphPPL.Model`, which expects `rand` to forward to its `rand!` method. I'm not certain how we want to define the interface for this `Model`. Co-authored-by: Xianda Sun <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using AbstractPPL | ||
using Random | ||
using Test | ||
|
||
mutable struct RandModel <: AbstractProbabilisticProgram | ||
rng | ||
T | ||
end | ||
|
||
function Base.rand(rng::Random.AbstractRNG, ::Type{T}, model::RandModel) where {T} | ||
model.rng = rng | ||
model.T = T | ||
return nothing | ||
end | ||
|
||
@testset "AbstractProbabilisticProgram" begin | ||
@testset "rand defaults" begin | ||
model = RandModel(nothing, nothing) | ||
rand(model) | ||
@test model.rng == Random.default_rng() | ||
@test model.T === NamedTuple | ||
rngs = [Random.default_rng(), Random.MersenneTwister(42)] | ||
Ts = [NamedTuple, Dict] | ||
@testset for T in Ts | ||
model = RandModel(nothing, nothing) | ||
rand(T, model) | ||
@test model.rng == Random.default_rng() | ||
@test model.T === T | ||
end | ||
@testset for rng in rngs | ||
model = RandModel(nothing, nothing) | ||
rand(rng, model) | ||
@test model.rng === rng | ||
@test model.T === NamedTuple | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters