Skip to content

Use Threads.nthreads() * 2 in TSVI #936

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
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# DynamicPPL Changelog

## 0.36.11

Make `ThreadSafeVarInfo` hold a total of `Threads.nthreads() * 2` logp values, instead of just `Threads.nthreads()`.
This fix helps to paper over the cracks in using `threadid()` to index into the `ThreadSafeVarInfo` object.

## 0.36.10

Added compatibility with ForwardDiff 1.0.
Expand Down
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.36.10"
version = "0.36.11"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
6 changes: 4 additions & 2 deletions src/simple_varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,12 @@ end
# Threadsafe stuff.
# For `SimpleVarInfo` we don't really need `Ref` so let's not use it.
function ThreadSafeVarInfo(vi::SimpleVarInfo)
return ThreadSafeVarInfo(vi, zeros(typeof(getlogp(vi)), Threads.nthreads()))
return ThreadSafeVarInfo(vi, zeros(typeof(getlogp(vi)), Threads.nthreads() * 2))
end
function ThreadSafeVarInfo(vi::SimpleVarInfo{<:Any,<:Ref})
return ThreadSafeVarInfo(vi, [Ref(zero(getlogp(vi))) for _ in 1:Threads.nthreads()])
return ThreadSafeVarInfo(
vi, [Ref(zero(getlogp(vi))) for _ in 1:(Threads.nthreads() * 2)]
)
end

has_varnamedvector(vi::SimpleVarInfo) = vi.values isa VarNamedVector
13 changes: 12 additions & 1 deletion src/threadsafe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ struct ThreadSafeVarInfo{V<:AbstractVarInfo,L} <: AbstractVarInfo
logps::L
end
function ThreadSafeVarInfo(vi::AbstractVarInfo)
return ThreadSafeVarInfo(vi, [Ref(zero(getlogp(vi))) for _ in 1:Threads.nthreads()])
# In ThreadSafeVarInfo we use threadid() to index into the array of logp
# fields. This is not good practice --- see
# https://github.com/TuringLang/DynamicPPL.jl/issues/924 for a full
# explanation --- but it has worked okay so far.
# The use of nthreads()*2 here ensures that threadid() doesn't exceed
# the length of the logps array. Ideally, we would use maxthreadid(),
# but Mooncake can't differentiate through that. Empirically, nthreads()*2
# seems to provide an upper bound to maxthreadid(), so we use that here.
# See https://github.com/TuringLang/DynamicPPL.jl/pull/936
return ThreadSafeVarInfo(
Copy link
Member

Choose a reason for hiding this comment

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

Could we add a comment here explaining the situation and maybe linking to this PR or the relevant issue? The * 2 would otherwise appear quite mysterious.

vi, [Ref(zero(getlogp(vi))) for _ in 1:(Threads.nthreads() * 2)]
)
end
ThreadSafeVarInfo(vi::ThreadSafeVarInfo) = vi

Expand Down
2 changes: 1 addition & 1 deletion test/threadsafe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@test threadsafe_vi.varinfo === vi
@test threadsafe_vi.logps isa Vector{typeof(Ref(getlogp(vi)))}
@test length(threadsafe_vi.logps) == Threads.nthreads()
@test length(threadsafe_vi.logps) == Threads.nthreads() * 2
@test all(iszero(x[]) for x in threadsafe_vi.logps)
end

Expand Down