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

Refactor CollectVariables #151

Merged
merged 9 commits into from
Mar 7, 2024
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
109 changes: 46 additions & 63 deletions src/compiler_pass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,15 @@
is_resolved(::Any) = false

@inline function is_specified_by_data(
::NamedTuple{data_keys}, var::Symbol
data::NamedTuple{data_keys}, var::Symbol
) where {data_keys}
if var ∉ data_keys
return false
else
if data[var] isa AbstractArray
throw(ArgumentError("In BUGS, implicit indexing on the LHS is not allowed."))
error("In BUGS, implicit indexing on the LHS is not allowed.")
else
return true
end
end
end
Expand All @@ -273,7 +275,7 @@
else
if values isa Missing
return false
elseif values <: Union{Int,Float64}
elseif values isa Union{Int,Float64}
return true
else
error("Unexpected type: $(typeof(values))")
Expand All @@ -295,71 +297,52 @@
end
end

function analyze_assignment(
pass::CollectVariables{data_arrays,arrays}, expr::Expr, env::NamedTuple{data_vars}
) where {data_arrays,arrays,data_vars}
if Meta.isexpr(expr, :(=))
lhs_expr = expr.args[1]
else # Expr(:call, :(~), ...)
lhs_expr = expr.args[2]
function analyze_assignment(pass::CollectVariables, expr::Expr, env::NamedTuple)
lhs_expr = Meta.isexpr(expr, :(=)) ? expr.args[1] : expr.args[2]
v = simplify_lhs(env, lhs_expr)

if v isa Symbol
handle_symbol_lhs(pass, expr, v, env)
else
handle_ref_lhs(pass, expr, v, env)
end
end

v = simplify_lhs(env, lhs_expr)
function handle_symbol_lhs(::CollectVariables, expr::Expr, v::Symbol, env::NamedTuple)
if Meta.isexpr(expr, :(=)) && is_specified_by_data(env, v)
error("Variable $v is specified by data, can't be assigned to.")

Check warning on line 313 in src/compiler_pass.jl

View check run for this annotation

Codecov / codecov/patch

src/compiler_pass.jl#L313

Added line #L313 was not covered by tests
end
end

function handle_ref_lhs(pass::CollectVariables, expr::Expr, v::Tuple, env::NamedTuple)
var, indices... = v
if Meta.isexpr(expr, :(=))
if v isa Symbol
if is_specified_by_data(env, v)
throw(
ArgumentError("Variable $v is specified by data, can't be assigned to.")
)
end
else
var, indices... = v
if is_specified_by_data(env, var, indices...)
throw(
ArgumentError(
"$var[$(join(indices, ", "))] partially observed, not allowed, rewrite so that the variables are either all observed or all unobserved.",
),
)
end
if var in data_vars
if !Base.checkbounds(Bool, env[var], indices...)
error(
"Statement $expr is trying to assign to a data variable $var with indices $indices that are out of bounds.",
)
end
else
for i in eachindex(pass.array_sizes[var])
pass.array_sizes[var][i] = max(
pass.array_sizes[var][i], last(indices[i])
)
end
end
if is_specified_by_data(env, var, indices...)
error(
"$var[$(join(indices, ", "))] partially observed, not allowed, rewrite so that the variables are either all observed or all unobserved.",
)
end
update_array_sizes_for_assignment(pass, var, env, indices...)
else
if v isa Symbol
return nothing
else
var, indices... = v
if is_partially_specified_as_data(env, var, indices...)
throw(
ArgumentError(
"$var[$(join(indices, ", "))] partially observed, not allowed, rewrite so that the variables are either all observed or all unobserved.",
),
)
end
if var in data_vars
if !Base.checkbounds(Bool, env[var], indices...)
error(
"Statement $expr is trying to assign to a data variable $var with indices $indices that are out of bounds.",
)
end
else
for i in eachindex(pass.array_sizes[var])
pass.array_sizes[var][i] = max(
pass.array_sizes[var][i], last(indices[i])
)
end
end
if is_partially_specified_as_data(env, var, indices...)
error(
"$var[$(join(indices, ", "))] partially observed, not allowed, rewrite so that the variables are either all observed or all unobserved.",
)
end
update_array_sizes_for_assignment(pass, var, env, indices...)
end
end

function update_array_sizes_for_assignment(
pass::CollectVariables,
var::Symbol,
::NamedTuple{data_vars},
indices::Vararg{Union{Int,UnitRange{Int}}},
) where {data_vars}
# `is_specified_by_data` checks if the index is inbound
if var ∉ data_vars
for i in eachindex(pass.array_sizes[var])
pass.array_sizes[var][i] = max(pass.array_sizes[var][i], last(indices[i]))
end
end
end
Expand Down
109 changes: 74 additions & 35 deletions test/passes.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
using JuliaBUGS: analyze_program, CollectVariables, DataTransformation
using JuliaBUGS: is_resolved
using JuliaBUGS: is_specified_by_data, is_partially_specified_as_data

@testset "CollectVariables Error Cases" begin
# assign to data
model_def = @bugs begin
b = a
end
data = (b=[1, 2],)
@test_throws ErrorException analyze_program(
CollectVariables(model_def, data), model_def, data
)

model_def = @bugs begin
x[1:3] = y[1:3]
end
data = (x=[1, missing, missing], y=[1, 2, 3])
@test_throws ErrorException analyze_program(
CollectVariables(model_def, data), model_def, data
)

# partially specified as data
model_def = @bugs begin
x[1:3] ~ dmnorm(y[1:3], E[:, :])
end
data = (x=[1, missing, missing], y=[1, 2, 3], E=[1 0 0; 0 1 0; 0 0 1])
@test_throws ErrorException analyze_program(
CollectVariables(model_def, data), model_def, data
)

# check access data array out-of-bound
model_def = @bugs begin
x[4] = 2
end
data = (x=[1, 2, 3],)
@test_throws BoundsError analyze_program(
CollectVariables(model_def, data), model_def, data
)
end

@testset "is_specified_by_data" begin
data = (a=2, b=[1, 2, 3], c=[1, 2, missing], d=[missing, missing, missing])

# a is data
@test is_specified_by_data(data, :a)
# c is Array, but no indices
@test_throws ErrorException is_specified_by_data(data, :c)
# index doesn't contain UnitRange
@test is_specified_by_data(data, :b, 1)
@test !is_specified_by_data(data, :d, 1)
@test is_specified_by_data(data, :c, 1)
@test !is_specified_by_data(data, :c, 3)
# index contains UnitRange
@test is_specified_by_data(data, :b, 1:2)
@test is_specified_by_data(data, :c, 1:2)
@test is_specified_by_data(data, :c, 2:3)
@test !is_specified_by_data(data, :d, 1:2)

@test !is_partially_specified_as_data(data, :c, 1:2)
@test is_partially_specified_as_data(data, :c, 2:3)
@test !is_partially_specified_as_data(data, :b, 1:2)
@test !is_partially_specified_as_data(data, :d, 1:2)
end

@testset "Constant propagation" begin
model_def = @bugs begin
a = b + 1
Expand All @@ -6,54 +71,28 @@
end
data = (b=1, e=[1, 2])

scalars, array_sizes = analyze_program(CollectVariables(), model_def, data)
has_new_val, transformed_variables = analyze_program(
DataTransformation(scalars, array_sizes), model_def, data
scalars, array_sizes = analyze_program(
CollectVariables(model_def, data), model_def, data
)
@test has_new_val == true
@test transformed_variables[:a] == 2

has_new_val, transformed_variables = analyze_program(
DataTransformation(false, transformed_variables), model_def, data
transformed_variables = Dict{Symbol,Any}(
:a => missing, :c => missing, :d => Union{Int,Missing}[missing,]
)
@test has_new_val == true
@test transformed_variables[:c] == 6

has_new_val, transformed_variables = analyze_program(
DataTransformation(false, transformed_variables), model_def, data
)
@test has_new_val == false
end

@testset "Test compiler passes with $m" for m in [:leuk, :leukfr]
model_def = JuliaBUGS.BUGSExamples.VOLUME_I[m].model_def
data = JuliaBUGS.BUGSExamples.VOLUME_I[m].data
inits = JuliaBUGS.BUGSExamples.VOLUME_I[m].inits[1]

scalars, array_sizes = analyze_program(CollectVariables(), model_def, data)

has_new_val, transformed_variables = analyze_program(
DataTransformation(scalars, array_sizes), model_def, data
)
@test has_new_val == true
@test all(!ismissing, transformed_variables[:Y])
@test transformed_variables[:a] == 2

has_new_val, transformed_variables = analyze_program(
DataTransformation(false, transformed_variables), model_def, data
)

@test has_new_val == true
@test all(!ismissing, transformed_variables[:dN])

array_bitmap, transformed_variables = analyze_program(
PostChecking(data, transformed_variables), model_def, data
)
@test transformed_variables[:c] == 6

vars, array_sizes, array_bitmap, node_args, node_functions, dependencies = analyze_program(
NodeFunctions(array_sizes, array_bitmap),
model_def,
merge_with_coalescence(data, transformed_variables),
has_new_val, transformed_variables = analyze_program(
DataTransformation(false, transformed_variables), model_def, data
)

compile(model_def, data, inits)
@test has_new_val == false
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ else
include("utils.jl")
end

include("passes.jl")

@testset "Log Probability Test" begin
include("run_logp_tests.jl")
@testset "Single stochastic variable test" begin
Expand Down
Loading