TuringLang / TuringLang/DynamicPPL.jl

`hasvalue(p.params, vn, dist)`

Open
#1,138 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Julia
Stars
286
Forks
41
Avg merge
1d 11h
Merged PRs (30d)
34

Description

The three-argument hasvalue method, used inside InitFromParams:

https://github.com/TuringLang/DynamicPPL.jl/blob/8e359cec781d023bf9fea8639624c0c29a88d4c3/src/contexts/init.jl#L113-L118

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:

  1. Is hasvalue(dict, vn, dist) noticeably slower than hasvalue(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.

  2. 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 make InitFromParams faster, 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.