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

Specifying rng with DynamicPPL.evaluate!! leads to StackOverflow #370

Closed
ParadaCarleton opened this issue Feb 13, 2022 · 10 comments
Closed

Comments

@ParadaCarleton
Copy link
Member

I assume this is to do with some kind of accidental recursion/method ambiguity; the command:

# If no sampler is specified, we sample from the prior by default
_, x1 = DynamicPPL.evaluate!!(model, rng, SimpleVarInfo(model), SamplingContext())

leads to:

StackOverflowError:

Stacktrace:
  [1] evaluate!!(::Model{typeof(demo), (:x,), (), (), Tuple{Vector{Int64}}, Tuple{}, DefaultContext}, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Vararg{Any})
    @ DynamicPPL ~/.julia/dev/DynamicPPL.jl/src/model.jl:420
  [2] evaluate!!(::Model{typeof(demo), (:x,), (), (), Tuple{Vector{Int64}}, Tuple{}, DefaultContext}, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Random._GLOBAL_RNG, ::Vararg{Any}) (repeats 10911 times)
    @ DynamicPPL ~/.julia/dev/DynamicPPL.jl/src/model.jl:421
  [3] top-level scope
    @ ~/.julia/dev/DynamicPPL.jl/docs/src/tutorials/met_hast_dppl.ipynb:2
  [4] eval
    @ ./boot.jl:373 [inlined]
  [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196
  [6] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
  [7] invokelatest
    @ ./essentials.jl:714 [inlined]
  [8] (::VSCodeServer.var"#150#151"{VSCodeServer.NotebookRunCellArguments, String})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/packages/VSCodeServer/src/serve_notebook.jl:18
  [9] withpath(f::VSCodeServer.var"#150#151"{VSCodeServer.NotebookRunCellArguments, String}, path::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/packages/VSCodeServer/src/repl.jl:185
 [10] notebook_runcell_request(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, params::VSCodeServer.NotebookRunCellArguments)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/packages/VSCodeServer/src/serve_notebook.jl:14
 [11] dispatch_msg(x::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeServer.JSONRPC.MsgDispatcher, msg::Dict{String, Any})
    @ VSCodeServer.JSONRPC ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/packages/JSONRPC/src/typed.jl:67
 [12] serve_notebook(pipename::String; crashreporting_pipename::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/packages/VSCodeServer/src/serve_notebook.jl:94
 [13] top-level scope
    @ ~/.vscode/extensions/julialang.language-julia-1.5.11/scripts/notebook/notebook.jl:12
 [14] include(mod::Module, _path::String)
    @ Base ./Base.jl:418
 [15] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:292
 [16] _start()
    @ Base ./client.jl:495
@ParadaCarleton
Copy link
Member Author

ParadaCarleton commented Feb 13, 2022

(Removing the RNG leads it to run as expected)

@devmotion
Copy link
Member

xref: #360 (comment)

@devmotion
Copy link
Member

Speecifying both a sampling context and an RNG is redundant, hence IMO this should not be supported. But as mentioned in the comment linked above, we should clean the dispatches and function signatures such that it throws a MethodError instead of a StackOverflowError.

@ParadaCarleton
Copy link
Member Author

Speecifying both a sampling context and an RNG is redundant, hence IMO this should not be supported. But as mentioned in the comment linked above, we should clean the dispatches and function signatures such that it throws a MethodError instead of a StackOverflowError.

Makes sense, but then the docstring should be fixed, as currently it says:

  evaluate!!(model::Model[, rng, varinfo, sampler, context])

Which implies it should be possible to pass both an rng and a context.

@ParadaCarleton
Copy link
Member Author

_, x4 = DynamicPPL.evaluate!!(model, x1, SamplingContext(rng))

Hmm, this still throws an error:

MethodError: no method matching DynamicPPL.SamplingContext(::Random.MersenneTwister)

Closest candidates are:

DynamicPPL.SamplingContext(::R, !Matched::S, !Matched::C) where {S<:AbstractMCMC.AbstractSampler, C<:DynamicPPL.AbstractContext, R} at ~/.julia/packages/DynamicPPL/c8MjC/src/contexts.jl:131

DynamicPPL.SamplingContext(::Any, !Matched::Any) at ~/.julia/packages/DynamicPPL/c8MjC/src/contexts.jl:135

DynamicPPL.SamplingContext() at ~/.julia/packages/DynamicPPL/c8MjC/src/contexts.jl:138

...

top-level scope@[Local: 1](http://localhost:1234/edit?id=159c8e8c-8cf4-11ec-0a77-4b4454424b8c#)[inlined]

@ParadaCarleton
Copy link
Member Author

Ahh, ok, looks like it requires either all the arguments to be passed or none of them. So this works:

SamplingContext(rng, SampleFromPrior(), DefaultContext())

But dropping SampleFromPrior() or DefaultContext() throws an error. Should I make a PR setting these as defaults?

@devmotion
Copy link
Member

Ahh, ok, looks like it requires either all the arguments to be passed or none of them.

No, there are a bunch of different constructors:

SamplingContext(sampler, context) = SamplingContext(Random.GLOBAL_RNG, sampler, context)
SamplingContext(context::AbstractContext) = SamplingContext(SampleFromPrior(), context)
SamplingContext(sampler::AbstractSampler) = SamplingContext(sampler, DefaultContext())
SamplingContext() = SamplingContext(SampleFromPrior())
You can pass only a sampler, only a child context, a sampler and a child context, or an RNG, a sampler, and a child context. But it seems we could add some additional constructors and change it to e.g.

function SamplingContext(
    rng::AbstractRNG=Random.GLOBAL_RNG, sampler::AbstractSampler=SampleFromPrior()
)
    return SamplingContext(rng, sampler, DefaultContext())
end
function SamplingContext(rng::AbstractRNG, context::AbstractContext)
    return SamplingContext(rng, SampleFromPrior(), context)
end
function SamplingContext(sampler::AbstractSampler, context::AbstractContext=DefaultContext())
    return SamplingContext(Random.GLOBAL_RNG, sampler, context)
end
function SamplingContext(context::AbstractContext)
    return SamplingContext(Random.GLOBAL_RNG, SampleFromPrior(), context)
end

@ParadaCarleton
Copy link
Member Author

That looks good to me.

@yebai
Copy link
Member

yebai commented Sep 9, 2024

@mhauru is this fixed now recently?

@mhauru
Copy link
Member

mhauru commented Sep 9, 2024

Yes, fixed by #629

@mhauru mhauru closed this as completed Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants