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

Expose API for Turing integration #90

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 27 additions & 29 deletions ext/AdvancedPSLibtaskExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@ end
"""
LibtaskModel{F}

State wrapper to hold `Libtask.CTask` model initiated from `f`
State wrapper to hold `Libtask.CTask` model initiated from `f`.
"""
struct LibtaskModel{F1,F2}
f::F1
ctask::Libtask.TapedTask{F2}

LibtaskModel(f::F1, ctask::Libtask.TapedTask{F2}) where {F1,F2} = new{F1,F2}(f, ctask)
end

function LibtaskModel(f, args...)
return LibtaskModel(
function AdvancedPS.LibtaskModel(
f::AdvancedPS.AbstractGenericModel, rng::Random.AbstractRNG, args...
) # Changed the API, need to take care of the RNG properly
return AdvancedPS.LibtaskModel(
f,
Libtask.TapedTask(f, args...; deepcopy_types=Union{AdvancedPS.TracedRNG,typeof(f)}),
Libtask.TapedTask(
f, rng, args...; deepcopy_types=Union{AdvancedPS.TracedRNG,typeof(f)}
),
)
end

Base.copy(model::LibtaskModel) = LibtaskModel(model.f, copy(model.ctask))
function Base.copy(model::AdvancedPS.LibtaskModel)
return AdvancedPS.LibtaskModel(model.f, copy(model.ctask))
end

const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
const LibtaskTrace{R} = AdvancedPS.Trace{<:AdvancedPS.LibtaskModel,R}

function AdvancedPS.Trace(
model::AdvancedPS.AbstractGenericModel, rng::Random.AbstractRNG, args...
)
return AdvancedPS.Trace(LibtaskModel(model, args...), rng)
return AdvancedPS.Trace(AdvancedPS.LibtaskModel(model, rng, args...), rng)
end

# step to the next observe statement and
Expand All @@ -56,7 +55,7 @@ function AdvancedPS.advance!(t::LibtaskTrace, isref::Bool=false)
end

# create a backward reference in task_local_storage
function addreference!(task::Task, trace::LibtaskTrace)
function AdvancedPS.addreference!(task::Task, trace::LibtaskTrace)
if task.storage === nothing
task.storage = IdDict()
end
Expand All @@ -65,9 +64,7 @@ function addreference!(task::Task, trace::LibtaskTrace)
return task
end

current_trace() = current_task().storage[:__trace]

function update_rng!(trace::LibtaskTrace)
function AdvancedPS.update_rng!(trace::LibtaskTrace)
rng, = trace.model.ctask.args
trace.rng = rng
return trace
Expand All @@ -76,12 +73,12 @@ end
# Task copying version of fork for Trace.
function AdvancedPS.fork(trace::LibtaskTrace, isref::Bool=false)
newtrace = copy(trace)
update_rng!(newtrace)
AdvancedPS.update_rng!(newtrace)
isref && AdvancedPS.delete_retained!(newtrace.model.f)
isref && delete_seeds!(newtrace)

# add backward reference
addreference!(newtrace.model.ctask.task, newtrace)
AdvancedPS.addreference!(newtrace.model.ctask.task, newtrace)
return newtrace
end

Expand All @@ -94,11 +91,11 @@ function AdvancedPS.forkr(trace::LibtaskTrace)
ctask = Libtask.TapedTask(
newf, trace.rng; deepcopy_types=Union{AdvancedPS.TracedRNG,typeof(trace.model.f)}
)
new_tapedmodel = LibtaskModel(newf, ctask)
new_tapedmodel = AdvancedPS.LibtaskModel(newf, ctask)

# add backward reference
newtrace = AdvancedPS.Trace(new_tapedmodel, trace.rng)
addreference!(ctask.task, newtrace)
AdvancedPS.addreference!(ctask.task, newtrace)
AdvancedPS.gen_refseed!(newtrace)
return newtrace
end
Expand Down Expand Up @@ -134,10 +131,10 @@ function AbstractMCMC.step(
# Create reference trajectory.
AdvancedPS.forkr(copy(state.trajectory))
else
println(model)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
println(model)

trng = AdvancedPS.TracedRNG()
gen_model = LibtaskModel(deepcopy(model), trng)
trace = AdvancedPS.Trace(LibtaskModel(deepcopy(model), trng), trng)
addreference!(gen_model.ctask.task, trace) # Do we need it here ?
trace = AdvancedPS.Trace(deepcopy(model), trng)
AdvancedPS.addreference!(trace.model.ctask.task, trace) # Do we need it here ?
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
AdvancedPS.addreference!(trace.model.ctask.task, trace) # Do we need it here ?
AdvancedPS.addreference!(trace.model.ctask.task, trace) # TODO: Do we need it here ?

trace
end
end
Expand Down Expand Up @@ -174,9 +171,8 @@ function AbstractMCMC.sample(

traces = map(1:(sampler.nparticles)) do i
trng = AdvancedPS.TracedRNG()
gen_model = LibtaskModel(deepcopy(model), trng)
trace = AdvancedPS.Trace(LibtaskModel(deepcopy(model), trng), trng)
addreference!(gen_model.ctask.task, trace) # Do we need it here ?
trace = AdvancedPS.Trace(deepcopy(model), trng)
Copy link
Member

@yebai yebai Nov 3, 2023

Choose a reason for hiding this comment

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

Did we define deepcopy(::LibtaskModel)? I can't find it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think model here is still the original model type: AbstractGenericModel of some sort. We wrap it inside a LibtaskModel in the Trace constructor

AdvancedPS.addreference!(trace.model.ctask.task, trace) # Do we need it here ?
trace
end

Expand All @@ -202,7 +198,9 @@ function AdvancedPS.replay(particle::AdvancedPS.Particle)
trng = deepcopy(particle.rng)
Random123.set_counter!(trng.rng, 0)
trng.count = 1
trace = AdvancedPS.Trace(LibtaskModel(deepcopy(particle.model.f), trng), trng)
trace = AdvancedPS.Trace(
AdvancedPS.LibtaskModel(deepcopy(particle.model.f), trng), trng
)
score = AdvancedPS.advance!(trace, true)
while !isnothing(score)
score = AdvancedPS.advance!(trace, true)
Expand Down
31 changes: 22 additions & 9 deletions src/model.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Trace{F,R}
Trace{F,R}
"""
mutable struct Trace{F,R}
model::F
Expand All @@ -10,24 +10,37 @@
const SSMTrace{R} = Trace{<:AbstractStateSpaceModel,R}
const GenericTrace{R} = Trace{<:AbstractGenericModel,R}

# reset log probability
reset_logprob!(::AdvancedPS.Particle) = nothing

reset_model(f) = deepcopy(f)
delete_retained!(f) = nothing

Base.copy(trace::Trace) = Trace(copy(trace.model), deepcopy(trace.rng))
"""
copy(trace::Trace)

# This is required to make it visible from outside extensions
function observe end
function replay end
Copy a trace. The `TracedRNG` is deep-copied. The inner model is shallow-copied.
"""
Base.copy(trace::Trace) = Trace(copy(trace.model), deepcopy(trace.rng))

"""
gen_refseed!(particle::Particle)
gen_refseed!(particle::Particle)

Generate a new seed for the reference particle
Generate a new seed for the reference particle.
"""
function gen_refseed!(particle::Particle)
seed = split(state(particle.rng.rng), 1)
return safe_set_refseed!(particle.rng, seed[1])
end

# A few internal functions used in the Libtask extension. Since it is not possible to access objects defined
# in an extension, we just define dummy in the main module and implement them in the extension.
function observe end
function replay end
function addreference! end

current_trace() = current_task().storage[:__trace]

Check warning on line 40 in src/model.jl

View check run for this annotation

Codecov / codecov/patch

src/model.jl#L40

Added line #L40 was not covered by tests

# We need this one to be visible outside of the extension for dispatching (Turing.jl).
struct LibtaskModel{F,T}
f::F
ctask::T
end
2 changes: 2 additions & 0 deletions src/rng.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ Random123.set_counter!(r::TracedRNG, n::Integer) = r.count = n
Increase the model step counter by `n`
"""
inc_counter!(r::TracedRNG, n::Integer=1) = r.count += n

function update_rng! end
2 changes: 1 addition & 1 deletion test/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@

# Test task copy version of trace
trng = AdvancedPS.TracedRNG()
tr = AdvancedPS.Trace(Model(Ref(0)), trng, trng)
tr = AdvancedPS.Trace(Model(Ref(0)), trng)

consume(tr.model.ctask)
consume(tr.model.ctask)
Expand Down
Loading