Skip to content

Commit

Permalink
Fix force_update discard bug
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-lew committed Jan 18, 2019
1 parent 84fff18 commit c8ce0d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dynamic/force_update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function add_unvisited_to_discard!(discard::DynamicAssignment,
elseif has_internal_node(visited, key)
subvisited = get_internal_node(visited, key)
subdiscard = get_subassmt(discard, key)
add_unvisited_to_discard!(subdiscard, subvisited, subassmt)
add_unvisited_to_discard!(isempty(subdiscard) ? DynamicAssignment() : subdiscard, subvisited, subassmt)
set_subassmt!(discard, key, subdiscard)
else
# none of this subassmt was visited, so we discard the whole thing
Expand Down
33 changes: 33 additions & 0 deletions test/dynamic_dsl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,39 @@ end

# test retdiff
@test retdiff === DefaultRetDiff()

# Addresses under the :data namespace will be visited,
# but nothing there will be discarded.
@gen function loopy()
a = @addr(normal(0, 1), :a)
for i=1:5
@addr(normal(a, 1), :data => i)
end
end

# Get an initial trace
constraints = DynamicAssignment()
constraints[:a] = 0
for i=1:5
constraints[:data => i] = 0
end
(trace,) = initialize(loopy, (), constraints)

# Update a
constraints = DynamicAssignment()
constraints[:a] = 1
(new_trace, weight, discard, retdiff) = force_update(
(), noargdiff, trace, constraints)

# Test discard, score, weight, retdiff
@test get_value(discard, :a) == 0
prev_score = logpdf(normal, 0, 0, 1) * 6
expected_new_score = logpdf(normal, 1, 0, 1) + 5 * logpdf(normal, 0, 1, 1)
expected_weight = expected_new_score - prev_score
@test isapprox(expected_new_score, get_score(new_trace))
@test isapprox(expected_weight, weight)
@test retdiff === DefaultRetDiff()

end


Expand Down

0 comments on commit c8ce0d7

Please sign in to comment.