Skip to content

Commit

Permalink
Small addition to link! and invlink! (#231)
Browse files Browse the repository at this point in the history
Makes it possible to only link/invlink a subset of the space of a sampler *for static models only* (as there's no equivalent for `UntypedVarInfo`). Is non-breaking since this is just adding an intermediate method to an existing implementation, giving increased flexbility.

Probably don't want to encourage use of this, but it can be useful in cases such as the MH sampler TuringLang/Turing.jl#1582 (comment).
  • Loading branch information
torfjelde committed Apr 11, 2021
1 parent 7a45694 commit 9d4137e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.10.14"
version = "0.10.15"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
10 changes: 8 additions & 2 deletions src/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,11 @@ function link!(vi::UntypedVarInfo, spl::Sampler)
end
end
function link!(vi::TypedVarInfo, spl::AbstractSampler)
return link!(vi, spl, Val(getspace(spl)))
end
function link!(vi::TypedVarInfo, spl::AbstractSampler, spaceval::Val)
vns = _getvns(vi, spl)
return _link!(vi.metadata, vi, vns, Val(getspace(spl)))
return _link!(vi.metadata, vi, vns, spaceval)
end
@generated function _link!(metadata::NamedTuple{names}, vi, vns, ::Val{space}) where {names, space}
expr = Expr(:block)
Expand Down Expand Up @@ -770,8 +773,11 @@ function invlink!(vi::UntypedVarInfo, spl::AbstractSampler)
end
end
function invlink!(vi::TypedVarInfo, spl::AbstractSampler)
return invlink!(vi, spl, Val(getspace(spl)))
end
function invlink!(vi::TypedVarInfo, spl::AbstractSampler, spaceval::Val)
vns = _getvns(vi, spl)
return _invlink!(vi.metadata, vi, vns, Val(getspace(spl)))
return _invlink!(vi.metadata, vi, vns, spaceval)
end
@generated function _invlink!(metadata::NamedTuple{names}, vi, vns, ::Val{space}) where {names, space}
expr = Expr(:block)
Expand Down
12 changes: 11 additions & 1 deletion test/turing/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
@test all(x -> !istrans(vi, x), meta.m.vns)
@test meta.s.vals == v_s
@test meta.m.vals == v_m

# Transforming only a subset of the variables
link!(vi, spl, Val((:m, )))
@test all(x -> !istrans(vi, x), meta.s.vns)
@test all(x -> istrans(vi, x), meta.m.vns)
invlink!(vi, spl, Val((:m, )))
@test all(x -> !istrans(vi, x), meta.s.vns)
@test all(x -> !istrans(vi, x), meta.m.vns)
@test meta.s.vals == v_s
@test meta.m.vals == v_m
end
@testset "orders" begin
csym = gensym() # unique per model
Expand Down Expand Up @@ -329,4 +339,4 @@
@test vi.metadata.w.gids[1] == Set([hmc.selector])
@test vi.metadata.u.gids[1] == Set([hmc.selector]) =#
end
end
end

2 comments on commit 9d4137e

@torfjelde
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/34569

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.15 -m "<description of version>" 9d4137eb33e83f34c484bf78f9a57f828b3c92a0
git push origin v0.10.15

Please sign in to comment.