Skip to content

New construction for TapedTask #155

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 29, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
license = "MIT"
desc = "Tape based task copying in Turing"
repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.8"
version = "0.8.1"

[deps]
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
Expand Down
8 changes: 4 additions & 4 deletions src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ mutable struct TapedFunction{F, TapeType}
binding_values::Bindings
arg_binding_slots::Vector{Int} # arg indices in binding_values
retval_binding_slot::Int # 0 indicates the function has not returned
deepcopy_types::Vector{Any}
deepcopy_types::Type # use a Union type for multiple types

function TapedFunction{F, T}(f::F, args...; cache=false, deepcopy_types=[]) where {F, T}
function TapedFunction{F, T}(f::F, args...; cache=false, deepcopy_types=Union{}) where {F, T}
args_type = _accurate_typeof.(args)
cache_key = (f, args_type...)

Expand All @@ -78,7 +78,7 @@ mutable struct TapedFunction{F, TapeType}
return tf
end

TapedFunction(f, args...; cache=false, deepcopy_types=[]) =
TapedFunction(f, args...; cache=false, deepcopy_types=Union{}) =
TapedFunction{typeof(f), RawTape}(f, args...; cache=cache, deepcopy_types=deepcopy_types)

function TapedFunction{F, T0}(tf::TapedFunction{F, T1}) where {F, T0, T1}
Expand Down Expand Up @@ -472,7 +472,7 @@ tape_shallowcopy(x::Core.Box) = Core.Box(tape_shallowcopy(x.contents))
tape_deepcopy(x::Core.Box) = Core.Box(tape_deepcopy(x.contents))

function _tape_copy(v, deepcopy_types)
if any(t -> isa(v, t), deepcopy_types)
if isa(v, deepcopy_types)
tape_deepcopy(v)
else
tape_shallowcopy(v)
Expand Down
5 changes: 3 additions & 2 deletions src/tapedtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ end

# NOTE: evaluating model without a trace, see
# https://github.com/TuringLang/Turing.jl/pull/1757#diff-8d16dd13c316055e55f300cd24294bb2f73f46cbcb5a481f8936ff56939da7ceR329
function TapedTask(f, args...; deepcopy_types=[Array, Ref]) # deepcoy Array and Ref by default.
function TapedTask(f, args...; deepcopy_types=Union{Array, Ref}) # deepcoy Array and Ref by default.
tf = TapedFunction(f, args...; cache=true, deepcopy_types=deepcopy_types)
TapedTask(tf, args...)
end

TapedTask(t::TapedTask, args...) = TapedTask(func(t), args...)
TapedTask(finfo::Tuple{Any, Type}, args...) = TapedTask(finfo[1], args...; deepcopy_types=finfo[2])
TapedTask(t::TapedTask, args...) = TapedTask(func(t), args...; deepcopy_types=t.tf.deepcopy_types)
func(t::TapedTask) = t.tf.func

#=
Expand Down
16 changes: 16 additions & 0 deletions test/tapedtask.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
@testset "tapedtask" begin
@testset "construction" begin
function f()
t = 1
while true
produce(t)
t = 1 + t
end
end

ttask = TapedTask(f)
@test consume(ttask) == 1

ttask = TapedTask((f, Union{}))
@test consume(ttask) == 1
end

@testset "iteration" begin
function f()
t = 1
Expand Down