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

Union types #157

Merged
merged 3 commits into from
Oct 30, 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.2"
version = "0.8.3"

[deps]
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
Expand Down
11 changes: 9 additions & 2 deletions src/tapedtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ function TapedTask(tf::TapedFunction, args...)
return t
end

BASE_COPY_TYPES = Union{Array, Ref}

# NOTE: evaluating model without a trace, see
# https://github.com/TuringLang/Turing.jl/pull/1757#diff-8d16dd13c316055e55f300cd24294bb2f73f46cbcb5a481f8936ff56939da7ceR329
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)
function TapedTask(f, args...; deepcopy_types=nothing) # deepcoy Array and Ref by default.
if isnothing(deepcopy_types)
deepcopy = BASE_COPY_TYPES
else
deepcopy = Union{BASE_COPY_TYPES, deepcopy_types}
end
tf = TapedFunction(f, args...; cache=true, deepcopy_types=deepcopy)
TapedTask(tf, args...)
end

Expand Down
21 changes: 21 additions & 0 deletions test/tape_copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,25 @@
y[][2] = 19
@test y[][2] == 19
end

@testset "override deepcopy_types #57" begin
struct DummyType end

function f(start::Int)
t = [start]
while true
produce(t[1])
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f, 0; deepcopy_types=DummyType)
consume(ttask)

ttask2 = copy(ttask)
consume(ttask2)

@test consume(ttask) == 1
@test consume(ttask2) == 2
end
end