Skip to content

Completely Refactor #179

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

Open
wants to merge 79 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
55dcca5
Rework test cases a bit
willtebbutt Feb 27, 2025
8f9c3ce
Add formatter config
willtebbutt Feb 27, 2025
9f0ad12
Update gitignore to remove redundant items
willtebbutt Feb 27, 2025
ffd86e2
Bump minor version because small breaking change
willtebbutt Feb 27, 2025
2d06346
Tell users that various old types have been actually removed
willtebbutt Feb 27, 2025
0c545bb
Formatting of perf
willtebbutt Feb 27, 2025
dc5ab50
Remove old code
willtebbutt Feb 27, 2025
5228dc2
Rework includes in package and runtests
willtebbutt Feb 27, 2025
fc01732
More formatting
willtebbutt Feb 27, 2025
5ddfb60
Add in copyable_task files
willtebbutt Feb 27, 2025
159fe13
Clean up runtests
willtebbutt Feb 27, 2025
61594e4
Update project deps etc
willtebbutt Feb 27, 2025
ae6d000
Drop 1.7 from CI
willtebbutt Feb 27, 2025
dc4a0be
Initial transfer of code
willtebbutt Feb 27, 2025
7e9ba1e
Fix bug for function with no produce statements
willtebbutt Feb 27, 2025
8e6f1b6
Test for construction of new mutable struct
willtebbutt Feb 27, 2025
71e6444
Fix more test cases
willtebbutt Feb 27, 2025
4d18d47
Add another test case
willtebbutt Feb 27, 2025
f1b247d
More test cases
willtebbutt Feb 27, 2025
85ec6a2
More work
willtebbutt Feb 27, 2025
04544ca
Formatting
willtebbutt Feb 27, 2025
ecbf41c
Remove unhelpful docstring
willtebbutt Feb 27, 2025
22f6f67
Relax Test compat a bit
willtebbutt Feb 27, 2025
04e4dcd
Lower minor version to make integration tests run
willtebbutt Feb 27, 2025
b25d2f4
Handle code_coverage_effect
willtebbutt Feb 27, 2025
94aed2f
Some tweaks + docs
willtebbutt Feb 28, 2025
1579bff
Fix copying
willtebbutt Feb 28, 2025
5114d64
Formatting
willtebbutt Feb 28, 2025
b803f58
Enable more tests
willtebbutt Feb 28, 2025
6aeaac3
Implement dynamic scope
willtebbutt Mar 7, 2025
b546f21
Test dynamic scope correctness
willtebbutt Mar 7, 2025
8f4e4b5
README and NEWS overhaul
willtebbutt Mar 7, 2025
4d0b423
Export get_dynamic_scope and set_dynamic_scope
willtebbutt Mar 7, 2025
bde142f
Placeholder docstring for produce
willtebbutt Mar 7, 2025
4e58e11
Initial docs
willtebbutt Mar 7, 2025
21816ee
Ignore build folder of docs
willtebbutt Mar 7, 2025
3205273
Update cache action
willtebbutt Mar 7, 2025
06c168f
Formatting
willtebbutt Mar 7, 2025
258a4cd
Ignore all top-level manifest files
willtebbutt Mar 7, 2025
a3c2162
Add dependency on ScopedValues
willtebbutt Mar 7, 2025
4bb0dfa
Fix on LTS
willtebbutt Mar 7, 2025
a63660e
Do not check for stale deps on 1.11
willtebbutt Mar 7, 2025
4e1e5df
Some docs
willtebbutt Mar 7, 2025
54c745c
Docs action
willtebbutt Mar 7, 2025
0102653
Tidy up docs
willtebbutt Mar 7, 2025
e751db3
Tidy up docs slightly
willtebbutt Mar 7, 2025
6a221c2
Refactor + basic nested produce handling
willtebbutt Mar 11, 2025
f94ea17
Fomatting
willtebbutt Mar 11, 2025
57b808c
Dynamic nested calls and uses of return values of calls which might p…
willtebbutt Mar 13, 2025
bf1893c
Fix docs build
willtebbutt Mar 13, 2025
2c82159
Update CI
willtebbutt Mar 13, 2025
b393c54
Handle callable structs
willtebbutt Mar 13, 2025
906cb66
Fix docs and add doctest
willtebbutt Mar 20, 2025
4f80127
Test kwargs
willtebbutt Apr 8, 2025
065fb19
More tests
willtebbutt Apr 8, 2025
1ba2dfe
Fix inference bug
willtebbutt Apr 8, 2025
999f5bc
Improve documentation
willtebbutt Apr 8, 2025
ec9edb7
Performance enhancements
willtebbutt Apr 15, 2025
5135c4a
Caching and tweaks
willtebbutt Apr 15, 2025
b42227e
Fix docs build
willtebbutt Apr 15, 2025
aa86594
Docs and tidy up
willtebbutt Apr 15, 2025
5b07ec1
Include specifics from Mooncake
willtebbutt Apr 15, 2025
bc480c2
Tidy up further
willtebbutt Apr 15, 2025
35e9b7a
Tidy up further
willtebbutt Apr 15, 2025
42096b3
Merge branch 'main' into wct/refactor
willtebbutt Apr 15, 2025
c54dc5c
Remove undefined export
willtebbutt Apr 15, 2025
b2e65b8
Bump patch version
willtebbutt Apr 15, 2025
01a0b34
Test produce global performance
willtebbutt Apr 15, 2025
ebe8f91
Remove more references to dynamic scope
willtebbutt Apr 15, 2025
2907f03
Document type assertion
willtebbutt Apr 15, 2025
1198038
Fix benchmarks (#180)
yebai Apr 22, 2025
c4237db
Apply suggestions from code review
willtebbutt Apr 22, 2025
08cd796
Fix loopinfo expression
willtebbutt Apr 22, 2025
052ec0a
Remove redundant extended help section
willtebbutt Apr 22, 2025
4f48fcb
Note why __v exists
willtebbutt Apr 22, 2025
2086748
Sort out consume
willtebbutt Apr 22, 2025
89b0e90
Explain produce function
willtebbutt Apr 22, 2025
ec1d89d
Document callable_ret_type
willtebbutt Apr 22, 2025
8e7f784
Update benchmark.jl
yebai Apr 22, 2025
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
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
1 change: 0 additions & 1 deletion .github/workflows/Testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
strategy:
matrix:
version:
- '1.7'
- '1.10'
- '1'
- 'nightly'
Expand Down
57 changes: 0 additions & 57 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

# Projects files
Manifest.toml
deps/build.log
deps/deps.jl
deps/usr/
deps/tmp-build.jl
*.cov
21 changes: 12 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.8.8"

[deps]
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
MistyClosures = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
FunctionWrappers = "1.1"
LRUCache = "1.3"
julia = "1.7"
Aqua = "0.8.11"
JuliaFormatter = "1.0.62"
MistyClosures = "2.0.0"
Mooncake = "0.4.99"
Test = "1"
julia = "1.10.8"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "BenchmarkTools"]
test = ["Aqua", "JuliaFormatter", "Test"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ Notes:
to a tape and copying that tape. Before that version, it is based on
a tricky hack on the Julia internals. You can check the commit
history of this repo to see the details.

- From version 0.9.0, the old `TArray` and `TRef` types are completely removed, where previously they were deprecated.
2 changes: 1 addition & 1 deletion perf/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
julia = "1.3"
julia = "1.10.8"

[targets]
test = ["Test", "BenchmarkTools"]
37 changes: 20 additions & 17 deletions perf/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ function benchmark_driver!(f, x...; f_displayname=string(f))
GC.gc()

print(" Run TapedTask: ")
x = (x[1:end-1]..., produce)
x = (x[1:(end - 1)]..., produce)
# show the number of produce calls inside `f`
function f_task(f, x; verbose=false)
tt = TapedTask(f, x...)
c = 0
while consume(tt)!==nothing
c+=1
while consume(tt) !== nothing
c += 1
end
verbose && print("#produce=", c, "; ")
return verbose && print("#produce=", c, "; ")
end
# Note that we need to pass `f` instead of `tf` to avoid
# default continuation in `TapedTask` constructor, see, e.g.
# https://github.com/TuringLang/Libtask.jl/pull/135
f_task(f, x; verbose=true) # print #produce calls
@btime $f_task($f, $x)
GC.gc()
return nothing
end

####################################################################


function rosenbrock(x, callback=nothing)
i = x[2:end]
j = x[1:end-1]
ret = sum((1 .- j).^2 + 100*(i - j.^2).^2)
j = x[1:(end - 1)]
ret = sum((1 .- j) .^ 2 + 100 * (i - j .^ 2) .^ 2)
callback !== nothing && callback(ret)
return ret
end
Expand All @@ -59,17 +59,20 @@ benchmark_driver!(rosenbrock, x)
####################################################################

function ackley(x::AbstractVector, callback=nothing)
a, b, c = 20.0, -0.2, 2.0*π
a, b, c = 20.0, -0.2, 2.0 * π
len_recip = inv(length(x))
sum_sqrs = zero(eltype(x))
sum_cos = sum_sqrs
for i in x
sum_cos += cos(c*i)
sum_cos += cos(c * i)
sum_sqrs += i^2
callback !== nothing && callback(sum_sqrs)
end
return (-a * exp(b * sqrt(len_recip*sum_sqrs)) -
exp(len_recip*sum_cos) + a + MathConstants.e)
return (
-a * exp(b * sqrt(len_recip * sum_sqrs)) - exp(len_recip * sum_cos) +
a +
MathConstants.e
)
end

x = rand(100000)
Expand All @@ -79,8 +82,8 @@ benchmark_driver!(ackley, x)
function generate_matrix_test(n)
return (x, callback=nothing) -> begin
# @assert length(x) == 2n^2 + n
a = reshape(x[1:n^2], n, n)
b = reshape(x[n^2 + 1:2n^2], n, n)
a = reshape(x[1:(n^2)], n, n)
b = reshape(x[(n^2 + 1):(2n^2)], n, n)
ret = log.((a * b) + a - b)
callback !== nothing && callback(ret)
return ret
Expand All @@ -94,7 +97,7 @@ benchmark_driver!(matrix_test, x; f_displayname="matrix_test")

####################################################################
relu(x) = log.(1.0 .+ exp.(x))
sigmoid(n) = 1. / (1. + exp(-n))
sigmoid(n) = 1.0 / (1.0 + exp(-n))

function neural_net(w1, w2, w3, x1, callback=nothing)
x2 = relu(w1 * x1)
Expand All @@ -104,7 +107,7 @@ function neural_net(w1, w2, w3, x1, callback=nothing)
return ret
end

xs = (randn(10,10), randn(10,10), randn(10), rand(10))
xs = (randn(10, 10), randn(10, 10), randn(10), rand(10))
benchmark_driver!(neural_net, xs...)

####################################################################
Expand All @@ -114,11 +117,11 @@ println("======= breakdown benchmark =======")
x = rand(100000)
tf = Libtask.TapedFunction(ackley, x, nothing)
tf(x, nothing);
idx = findlast((x)->isa(x, Libtask.Instruction), tf.tape)
idx = findlast((x) -> isa(x, Libtask.Instruction), tf.tape)
ins = tf.tape[idx]
b = ins.input[1]

@show ins.input |> length
@show length(ins.input)
@btime map(x -> Libtask._lookup(tf, x), ins.input)
@btime Libtask._lookup(tf, b)
@btime tf.binding_values[b]
Expand Down
9 changes: 4 additions & 5 deletions perf/p0.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ using BenchmarkTools

@model gdemo(x, y) = begin
# Assumptions
σ ~ InverseGamma(2,3)
μ ~ Normal(0,sqrt(σ))
σ ~ InverseGamma(2, 3)
μ ~ Normal(0, sqrt(σ))
# Observations
x ~ Normal(μ, sqrt(σ))
y ~ Normal(μ, sqrt(σ))
end


# Case 1: Sample from the prior.
rng = MersenneTwister()
m = Turing.Core.TracedModel(gdemo(1.5, 2.), SampleFromPrior(), VarInfo(), rng)
m = Turing.Core.TracedModel(gdemo(1.5, 2.0), SampleFromPrior(), VarInfo(), rng)
f = m.evaluator[1];
args = m.evaluator[2:end];

Expand All @@ -28,7 +27,7 @@ println("Run a tape...")
@btime t.tf(args...)

# Case 2: SMC sampler
m = Turing.Core.TracedModel(gdemo(1.5, 2.), Sampler(SMC(50)), VarInfo(), rng)
m = Turing.Core.TracedModel(gdemo(1.5, 2.0), Sampler(SMC(50)), VarInfo(), rng)
f = m.evaluator[1];
args = m.evaluator[2:end];

Expand Down
25 changes: 11 additions & 14 deletions perf/p1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ using Turing, Test, AbstractMCMC, DynamicPPL, Random

import AbstractMCMC.AbstractSampler

function check_numerical(chain,
symbols::Vector,
exact_vals::Vector;
atol=0.2,
rtol=0.0)
function check_numerical(chain, symbols::Vector, exact_vals::Vector; atol=0.2, rtol=0.0)
for (sym, val) in zip(symbols, exact_vals)
E = val isa Real ?
mean(chain[sym]) :
vec(mean(chain[sym], dims=1))
E = val isa Real ? mean(chain[sym]) : vec(mean(chain[sym]; dims=1))
@info (symbol=sym, exact=val, evaluated=E)
@test E ≈ val atol=atol rtol=rtol
@test E ≈ val atol = atol rtol = rtol
end
end

function check_MoGtest_default(chain; atol=0.2, rtol=0.0)
check_numerical(chain,
[:z1, :z2, :z3, :z4, :mu1, :mu2],
[1.0, 1.0, 2.0, 2.0, 1.0, 4.0],
atol=atol, rtol=rtol)
return check_numerical(
chain,
[:z1, :z2, :z3, :z4, :mu1, :mu2],
[1.0, 1.0, 2.0, 2.0, 1.0, 4.0];
atol=atol,
rtol=rtol,
)
end

@model gdemo_d(x, y) = begin
Expand All @@ -36,4 +33,4 @@ chain = sample(gdemo_d(1.5, 2.0), alg, 5_000)

@show chain

check_numerical(chain, [:s, :m], [49/24, 7/6], atol=0.1)
check_numerical(chain, [:s, :m], [49 / 24, 7 / 6]; atol=0.1)
30 changes: 12 additions & 18 deletions src/Libtask.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
module Libtask

using FunctionWrappers: FunctionWrapper
using LRUCache
# Need this for BBCode.
using Mooncake
using Mooncake: BBCode, BBlock, ID, new_inst, stmt, seed_id!
using Mooncake: IDGotoIfNot, IDGotoNode, IDPhiNode, Switch

export TapedTask, consume, produce

export TArray, tzeros, tfill, TRef # legacy types back compat
# We'll emit `MistyClosure`s rather than `OpaqueClosure`s.
using MistyClosures

# Import some names from the compiler.
const CC = Core.Compiler
using Core.Compiler: Argument, IRCode, ReturnNode

@static if isdefined(Core, :TypedSlot) || isdefined(Core.Compiler, :TypedSlot)
# Julia v1.10 moved Core.TypedSlot to Core.Compiler
# Julia v1.11 removed Core.Compiler.TypedSlot
const TypedSlot = @static if isdefined(Core, :TypedSlot)
Core.TypedSlot
else
Core.Compiler.TypedSlot
end
else
struct TypedSlot end # Dummy
end
include("copyable_task.jl")
include("test_utils.jl")

include("tapedfunction.jl")
include("tapedtask.jl")
export TapedTask, consume, produce

end
Loading
Loading