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

[Merged by Bors] - Perform invlinking in assume rather than implicitly in getindex #360

Closed
wants to merge 107 commits into from

Conversation

torfjelde
Copy link
Member

@torfjelde torfjelde commented Jan 7, 2022

Currently, in assume, etc., invlink is called implicitly in getindex using the distribution extracted from vi.

This has a couple of drawbacks:

  1. We can only use the distribution for a particular vn stored in vi obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
  2. We have to store the distribution for each vn in vi. This was fine when we only had VarInfo because we also need it for other functionality, but this is not the case in SimpleVarInfo (nor will it be).

So. In this PR we introduce a getindex_raw which is getindex but without invlink if it's already linked, and uses this within assume, etc. where we now use the distributions that are passed to assume rather than those stored in vi.

E.g. the following now works:

julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))

src/varinfo.jl Outdated Show resolved Hide resolved
src/varinfo.jl Outdated Show resolved Hide resolved
test/model.jl Outdated Show resolved Hide resolved
test/model.jl Outdated Show resolved Hide resolved
@devmotion
Copy link
Member

Maybe getindex should be getindex_raw and indexing with linking should use a different notation?

src/simple_varinfo.jl Outdated Show resolved Hide resolved
@torfjelde
Copy link
Member Author

Maybe getindex should be getindex_raw and indexing with linking should use a different notation?

Ideally this is indeed how we do it. The current impl is super-confusing, e.g. if you use a sampler in getindex it will not transform the variables, and setindex expects values to be in constrained space, irregardless of whether you're indexing using a VarName or Sampler.

But I assume we need to do a bit of tracking down of all references to vi[vn] and ensure that we're not breaking anything in downstream packages, etc. Therefore my intention was to first just get a quick and dirty impl in that addresses the points mentioned in this PR, and then we make the indexing behavior consistent in a separate one.

src/simple_varinfo.jl Outdated Show resolved Hide resolved
src/DynamicPPL.jl Outdated Show resolved Hide resolved
src/simple_varinfo.jl Outdated Show resolved Hide resolved
src/DynamicPPL.jl Outdated Show resolved Hide resolved
src/simple_varinfo.jl Outdated Show resolved Hide resolved
@torfjelde
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request Jan 27, 2022
@bors
Copy link
Contributor

bors bot commented Jan 27, 2022

try

Build failed:

@torfjelde
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request Jan 27, 2022
@bors
Copy link
Contributor

bors bot commented Jan 27, 2022

try

Build failed:

@torfjelde
Copy link
Member Author

torfjelde commented Jan 27, 2022

Tests are failing on Julia 1.3 because the fix from TuringLang/Turing.jl#1758 hasn't made it through yet.

(It doesn't fail on Julia#latest because it's Julia 1.7 which makes it incompatible with latest release of Turing)

EDIT: It's passing locally using Turing#master on Julia 1.6.

@yebai
Copy link
Member

yebai commented Jan 27, 2022

@torfjelde it’s ok to make a new release from Turing#master.

@bors
Copy link
Contributor

bors bot commented Jul 21, 2022

Canceled.

@torfjelde
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jul 21, 2022
Currently, in `assume`, etc., `invlink` is called implicitly in `getindex` using the distribution extracted from `vi`.

This has a couple of drawbacks:
1. We can only use the distribution for a particular `vn` stored in `vi` obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
2. We have to store the distribution for each `vn` in `vi`. This was fine when we only had `VarInfo` because we also need it for other functionality, but this is not the case in `SimpleVarInfo` (nor will it be).

So. In this PR we introduce a `getindex_raw` which is `getindex` but without `invlink` if it's already linked, and uses this within `assume`, etc. where we now use the distributions that are passed to `assume` rather than those stored in `vi`.

E.g. the following now works:

``` julia
julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))
```

Co-authored-by: Hong Ge <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jul 21, 2022

Timed out.

@yebai
Copy link
Member

yebai commented Jul 21, 2022

bors r+

bors bot pushed a commit that referenced this pull request Jul 21, 2022
Currently, in `assume`, etc., `invlink` is called implicitly in `getindex` using the distribution extracted from `vi`.

This has a couple of drawbacks:
1. We can only use the distribution for a particular `vn` stored in `vi` obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
2. We have to store the distribution for each `vn` in `vi`. This was fine when we only had `VarInfo` because we also need it for other functionality, but this is not the case in `SimpleVarInfo` (nor will it be).

So. In this PR we introduce a `getindex_raw` which is `getindex` but without `invlink` if it's already linked, and uses this within `assume`, etc. where we now use the distributions that are passed to `assume` rather than those stored in `vi`.

E.g. the following now works:

``` julia
julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))
```

Co-authored-by: Hong Ge <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jul 21, 2022

Build failed:

@torfjelde
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jul 22, 2022
Currently, in `assume`, etc., `invlink` is called implicitly in `getindex` using the distribution extracted from `vi`.

This has a couple of drawbacks:
1. We can only use the distribution for a particular `vn` stored in `vi` obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
2. We have to store the distribution for each `vn` in `vi`. This was fine when we only had `VarInfo` because we also need it for other functionality, but this is not the case in `SimpleVarInfo` (nor will it be).

So. In this PR we introduce a `getindex_raw` which is `getindex` but without `invlink` if it's already linked, and uses this within `assume`, etc. where we now use the distributions that are passed to `assume` rather than those stored in `vi`.

E.g. the following now works:

``` julia
julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))
```

Co-authored-by: Hong Ge <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jul 22, 2022

Build failed:

@torfjelde
Copy link
Member Author

Unfortunately integration tests will fail because the tests for ESS are making assumptions about the DEMO_MODELS that are no longer true. As those are indeed the only tests that are failing, hence I suggest we just merge this and then update the tests in Turing accordingly (I have a working update locally).

In addition, I've bumped the minimum supported Julia version to 1.6 for DPPL; 1.6 is now LTS and we've already done this for Turing.jl, so continue to test against 1.3 but not 1.6 seems strange.

@torfjelde
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jul 23, 2022
Currently, in `assume`, etc., `invlink` is called implicitly in `getindex` using the distribution extracted from `vi`.

This has a couple of drawbacks:
1. We can only use the distribution for a particular `vn` stored in `vi` obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
2. We have to store the distribution for each `vn` in `vi`. This was fine when we only had `VarInfo` because we also need it for other functionality, but this is not the case in `SimpleVarInfo` (nor will it be).

So. In this PR we introduce a `getindex_raw` which is `getindex` but without `invlink` if it's already linked, and uses this within `assume`, etc. where we now use the distributions that are passed to `assume` rather than those stored in `vi`.

E.g. the following now works:

``` julia
julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))
```

Co-authored-by: Hong Ge <[email protected]>
@torfjelde
Copy link
Member Author

@yebai Can you just merge this once Bors comes back with failure, assuming the ESS tests are the only ones failing?

@torfjelde
Copy link
Member Author

Actually, given that we bump the lower bound for the Julia version, we should bump minor rather than patch version. This will also make bors happy 👍

@bors
Copy link
Contributor

bors bot commented Jul 23, 2022

Canceled.

@torfjelde
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jul 23, 2022
Currently, in `assume`, etc., `invlink` is called implicitly in `getindex` using the distribution extracted from `vi`.

This has a couple of drawbacks:
1. We can only use the distribution for a particular `vn` stored in `vi` obtained during the initial run. This means that we can't even run models where the distributions has dynamic domains, i.e. the domain of a particular random variable is dependent on the realizations of other random variables.
2. We have to store the distribution for each `vn` in `vi`. This was fine when we only had `VarInfo` because we also need it for other functionality, but this is not the case in `SimpleVarInfo` (nor will it be).

So. In this PR we introduce a `getindex_raw` which is `getindex` but without `invlink` if it's already linked, and uses this within `assume`, etc. where we now use the distributions that are passed to `assume` rather than those stored in `vi`.

E.g. the following now works:

``` julia
julia> @model demo() = x ~ InverseGamma(2, 3)
demo (generic function with 2 methods)

julia> vi = SimpleVarInfo((x = 10.0, ), true)
SimpleVarInfo((x = 10.0,), 0.0, true)

julia> _, vi = DynamicPPL.evaluate!!(model, vi, DefaultContext())
(22026.465794806718, SimpleVarInfo{NamedTuple{(:x,), Tuple{Float64}}, Float64}((x = 10.0,), -17.80291162245307, true))
```

Co-authored-by: Hong Ge <[email protected]>
@bors bors bot changed the title Perform invlinking in assume rather than implicitly in getindex [Merged by Bors] - Perform invlinking in assume rather than implicitly in getindex Jul 23, 2022
@bors bors bot closed this Jul 23, 2022
@bors bors bot deleted the tor/link-improvements branch July 23, 2022 10:21
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

Successfully merging this pull request may close these issues.

3 participants