Skip to content
Closed
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.10.16"
version = "0.10.17"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand All @@ -11,6 +11,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
NaturalSort = "c020b1a1-e9b0-503a-9c33-f039bfc54a85"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[compat]
AbstractMCMC = "2, 3.0"
Expand All @@ -20,4 +21,5 @@ ChainRulesCore = "0.9.7"
Distributions = "0.23.8, 0.24"
MacroTools = "0.5.6"
NaturalSort = "1"
ZygoteRules = "0.2"
julia = "1.3"
1 change: 1 addition & 0 deletions src/DynamicPPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AbstractMCMC
import ChainRulesCore
import NaturalSort
import MacroTools
import ZygoteRules

import Random

Expand Down
14 changes: 14 additions & 0 deletions src/compat/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ ChainRulesCore.@non_differentiable updategid!(
vn::VarName,
spl::Sampler,
)

# https://github.com/TuringLang/Turing.jl/issues/1595
ZygoteRules.@adjoint function dot_observe(
spl::Union{SampleFromPrior, SampleFromUniform},
dists::AbstractArray{<:Distribution},
value::AbstractArray,
vi,
)
function dot_observe_fallback(spl, dists, value, vi)
increment_num_produce!(vi)
return sum(map(Distributions.loglikelihood, dists, value))
end
Comment on lines +23 to +26
Copy link
Member

@torfjelde torfjelde Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just making some changes to #309 and noticed the adjoint introduced here. Isn't this wrong? If dists = [Normal()] and value = [1.0, 2.0], then we'd end up with sum([loglikelihood(Normal(), 1.0)]), right? 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but at least back then this case was not supported by the primal definition so it did not seem to be necessary to consider it here: https://github.com/TuringLang/DynamicPPL.jl/pull/235/files

It seems the primal definition was removed/changed though, maybe it's not even needed anymore?

return ZygoteRules.pullback(__context__, dot_observe_fallback, spl, dists, value, vi)
end
25 changes: 25 additions & 0 deletions test/compat/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,29 @@

test_model_ad(wishart_ad(), logp_wishart_ad)
end

# https://github.com/TuringLang/Turing.jl/issues/1595
@testset "dot_observe" begin
function f_dot_observe(x)
return DynamicPPL.dot_observe(SampleFromPrior(), [Normal(), Normal(-1.0, 2.0)], x, VarInfo())
end
function f_dot_observe_manual(x)
return logpdf(Normal(), x[1]) + logpdf(Normal(-1.0, 2.0), x[2])
end

# Manual computation of the gradient.
x = randn(2)
val = f_dot_observe_manual(x)
grad = ForwardDiff.gradient(f_dot_observe_manual, x)

@test ForwardDiff.gradient(f_dot_observe, x) ≈ grad

y, back = Tracker.forward(f_dot_observe, x)
@test Tracker.data(y) ≈ val
@test Tracker.data(back(1)[1]) ≈ grad

y, back = Zygote.pullback(f_dot_observe, x)
@test y ≈ val
@test back(1)[1] ≈ grad
end
end