Always expect a Libtask trace to be present#2757
Conversation
|
Turing.jl documentation for PR #2757 is available at: |
| arg_vi_id = objectid(vi) | ||
| vi = get_trace_local_varinfo_maybe(vi) | ||
| using_local_vi = objectid(vi) == arg_vi_id | ||
|
|
||
| trng = get_trace_local_rng_maybe(ctx.rng) | ||
| resample = get_trace_local_resampled_maybe(true) |
There was a problem hiding this comment.
I think some explanation is necessary, mostly for the unknown future person (which might well be me) who comes across this, and wonders why I got rid of these checks.
Generally, these functions used to attempt to check whether the function was being executed from inside a Libtask taped task. If not, then we attempted to use a "normal" execution path, i.e., using the VarInfo that is passed to tilde_assume!!, etc. -- but if we were, then we had to use the VarInfo that was stored as a 'taped global' in Libtask.
The problem with this is that the check objectid(vi) == arg_vi_id used to sometimes return true, even if there was a trace present, i.e., even if get_trace_local_varinfo_maybe returned the VarInfo stored in the taped globals. I actually completely don't know why. The fact that this was true meant that several lines below this, we never set the VarInfo back in the task's taped globals, which was responsible for the behaviour in #2754.
Why can we remove these checks...? Well, there is no way to actually use ParticleMCMCContext without going through sample(model, ::Union{SMC,PG}, ...). That function (technically, the functions that it calls) is what sets the leaf context of a model to be ParticleMCMCContext:
Turing.jl/src/mcmc/particle_mcmc.jl
Lines 31 to 38 in 026d07c
It is also what wraps the model in a Libtask trace. (That code is in AdvancedPSLibtaskExt.)
Furthermore, it is meaningless to use ParticleMCMCContext outside of a Libtask trace (it is pretty much impossible to run SMC on a Turing model without the functionality that Libtask provides -- trust me, I've tried). So there is a one-to-one correspondence: if you are using ParticleMCMCContext, you are within a Libtask trace, and vice versa.
So there's really no point in checking whether it is local or not. My current hypothesis is that the check is a vestigial bit of code from the old implementation, which used SamplingContext{<:SMC}, and in those cases maybe it was easier to accidentally run into this situation where the model was being executed with that context, but outside of a Libtask trace. Personally, I still don't know how that could ever happen; but anyway, here we are.
Technically, the code that I'm changing here was written in #2585; but the whole logic of 'checking whether we're in a trace or not' predates that PR, so goes back quite a while.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2757 +/- ##
==========================================
+ Coverage 84.96% 85.05% +0.08%
==========================================
Files 20 20
Lines 1264 1258 -6
==========================================
- Hits 1074 1070 -4
+ Misses 190 188 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
CI failures are in the optimisation interface, which is completely unexpected, but also unrelated to this PR, and will be fixed in #2747. It appears to be random since not all runners are failing. |
Closes #2754: with this patch, I get the following results