Skip to content

Conversation

@torfjelde
Copy link
Member

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).

@torfjelde
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request Apr 10, 2021
@bors
Copy link
Contributor

bors bot commented Apr 10, 2021

try

Build failed:

@torfjelde
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request Apr 10, 2021
@torfjelde
Copy link
Member Author

@devmotion You okay with this? As I said, I'm down to doing some serious thinking on whether we can simplify things, but I think this is a nice addon for the current impl without any negative side-effects.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

LGTM 👍

function link!(vi::TypedVarInfo, spl::AbstractSampler)
return link!(vi, spl, Val(getspace(spl)))
end
function link!(vi::TypedVarInfo, spl::AbstractSampler, spaceval::Val)
Copy link
Member

Choose a reason for hiding this comment

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

IIRC it is/was sometimes better to use

Suggested change
function link!(vi::TypedVarInfo, spl::AbstractSampler, spaceval::Val)
function link!(vi::TypedVarInfo, spl::AbstractSampler, ::Val{spaceval}) where spaceval

instead. Not completely sure though, I just know that Julia does not specialize on T::Type and one has to use ::Type{T}. On the other hand, if it's just passed through to another function it shouldn't matter.

Copy link
Member Author

Choose a reason for hiding this comment

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

But will this matter here? Given that this Val is just passed on, and then what you're suggesting is done in the method we're calling, which should force specialization, no?

Copy link
Member

Choose a reason for hiding this comment

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

It's fine 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha, no I mean, is my logic correct?

I.e. will

f(x::Val) = g(x)
g(::Val{value}) = value

and

f(::Val{value}) where {value} = g(Val{value}())
g(::Val{value}) = value

always lead to equivalent compiled code?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, AFAIK it should lead to the same compiled code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool:) Thanks man!

end
end
function link!(vi::TypedVarInfo, spl::AbstractSampler)
return link!(vi, spl, Val(getspace(spl)))
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes type inference is improved with Val{...}(), but I think it's fine to not micro-optimize the implementation if there sre no issues.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a good point though. I was just not 100% certain whether getspace was always known at compile-time (but couldn't find any counter-examples so happy to make the change if that's the intention!)

Copy link
Member

Choose a reason for hiding this comment

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

It's fine as it is, just something to keep in mind.

Copy link
Member Author

Choose a reason for hiding this comment

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

True, true 👍


# Transforming only a subset of the variables
link!(vi, spl, Val((:m, )))
@test all(x -> !istrans(vi, x), meta.s.vns)
Copy link
Member

Choose a reason for hiding this comment

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

BTW do you know about Base.Fix1 and Base.Fix2? Can be nice sometimes (IMO) in such cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes, I am aware, though still getting into the habbit of using it 😅
But in this case I just copy-pasted from above, so didn't cross my mind.

@torfjelde
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Apr 11, 2021
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).
@bors bors bot changed the title Small addition to link! and invlink! [Merged by Bors] - Small addition to link! and invlink! Apr 11, 2021
@bors bors bot closed this Apr 11, 2021
@bors bors bot deleted the tor/link-extension branch April 11, 2021 13:32
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