TuringLang / TuringLang/DynamicPPL.jl
`hasvalue(p.params, vn, dist)`
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 286
- Forks
- 41
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 34
Description
The three-argument hasvalue method, used inside InitFromParams:
is only really needed when we provide VarNames that are 'broken up' relative to how they appear in the model. Here's an example:
using DynamicPPL, Distributions, LinearAlgebra
@model function f2d()
return x ~ MvNormal(zeros(2), I)
end
returned(f2d(), Dict(@varname(x[1]) => 0.0, @varname(x[2]) => 0.0))
On current main, this will print [0.0, 0.0]. But that's only because hasvalue(dict, vn, dist) can 'reconstruct' x from the x[1] and x[2] that we provided. If we replace this with hasvalue(dict, vn), then this will error with a message saying that x wasn't found.
For cases like returned, I actually really don't mind if the above errors. That Dict was obviously constructed by me to prove a point, and I can fix it by simply constructing it the correct way, i.e. Dict(@varname(x) => [0.0, 0.0]).
The only case where we unconditionally, absolutely, DO need hasvalue(dict, vn, dist) is when the dictionary is obtained from MCMCChains. That happens because MCMCChains 'proactively' splits the parameters up.
There are two things to investigate here:
-
Is
hasvalue(dict, vn, dist)noticeably slower thanhasvalue(dict, vn)for cases where the dictionary already has the correct form (i.e.Dict(@varname(x) => [0.0, 0.0]))? If the performance is roughly the same, then we can just leave it in and call it a day, since it is strictly an improvement. -
If it isn't the same, then what we could do is to create a separate initialisation strategy which DOES use this, and use that only in MCMCChains. That would be something like
InitFromMCMCChains. Then, to makeInitFromParamsfaster, we could cut out the third argument from this.- Note that this solution would also mean that the
hasvalue(p, vn, dist)definitions, which currently sit in an awkward AbstractPPLDistributionsExt, could be moved to MCMCChains itself.
- Note that this solution would also mean that the
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/contexts/init.jl at the three-argument hasvalue call inside InitFromParams, and inspect the hasvalue definitions in AbstractPPLDistributionsExt. Compare performance for correctly shaped dictionaries with the split VarName example, including the MCMCChains case. Done means either retaining the current approach with evidence of similar performance or separating an MCMCChains-specific initialization strategy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- machine-learning
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100