cscherrer / Soss.jl

Probabilistic programming via source rewriting
https://cscherrer.github.io/Soss.jl/stable/
MIT License
412 stars 30 forks source link

Example in Readme fails #336

Closed sethaxen closed 2 years ago

sethaxen commented 2 years ago

The example in the Readme is

using MeasureTheory, Soss, SampleChainsDynamicHMC

m = @model x begin
    α ~ Lebesgue(ℝ)
    β ~ Normal()
    σ ~ Exponential()
    y ~ For(x) do xj
        Normal(α + β * xj, σ)
    end
    return y
end
x = randn(20)
predα = predictive(m, :α)
y = rand(predα(x=x,α=10.0))
post = sample(m(x=x) | (y=y,), dynamichmc())

The last line fails on Julia v1.7.2 with

ERROR: MethodError: no method matching LogDensityProblems.TransformedLogDensity(::TransformVariables.TransformTuple{NamedTuple{(:σ, :β, :α), Tuple{TransformVariables.ShiftedExp{true, Int64}, TransformVariables.Identity, TransformVariables.Identity}}}, ::Soss.var"#ℓ#154"{Soss.ConditionalModel{NamedTuple{(:x,)}, GeneralizedGenerated.NGG.TypeLevel{Expr, "Buf{23}()"}, GeneralizedGenerated.NGG.TypeLevel{Module, "Buf{17}()"}, NamedTuple{(:x,), Tuple{Vector{Float64}}}, NamedTuple{(:y,), Tuple{Vector{Float64}}}}})
Closest candidates are:
  LogDensityProblems.TransformedLogDensity(::T, ::L) where {T<:TransformVariables.AbstractTransform, L} at ~/.julia/packages/LogDensityProblems/UwY7S/src/LogDensityProblems.jl:158
Stacktrace:
 [1] newchain(rng::Random._GLOBAL_RNG, config::SampleChainsDynamicHMC.DynamicHMCConfig{Tuple{}, Tuple{InitialStepsizeSearch, TuningNUTS{Nothing, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{Nothing, DualAveraging{Float64}}}, DynamicHMC.NUTS{Val{:generalized}}, NoProgressReport, Val{:ForwardDiff}}, ℓ::Function, tr::TransformVariables.TransformTuple{NamedTuple{(:σ, :β, :α), Tuple{TransformVariables.ShiftedExp{true, Int64}, TransformVariables.Identity, TransformVariables.Identity}}})
   @ SampleChainsDynamicHMC ~/.julia/packages/SampleChainsDynamicHMC/F1tdl/src/SampleChainsDynamicHMC.jl:129
 [2] newchain
   @ ~/.julia/packages/SampleChains/fS5ze/src/multichain.jl:36 [inlined]
 [3] sample(rng::Random._GLOBAL_RNG, m::Soss.ConditionalModel{NamedTuple{(:x,)}, GeneralizedGenerated.NGG.TypeLevel{Expr, "Buf{23}()"}, GeneralizedGenerated.NGG.TypeLevel{Module, "Buf{17}()"}, NamedTuple{(:x,), Tuple{Vector{Float64}}}, NamedTuple{(:y,), Tuple{Vector{Float64}}}}, config::SampleChainsDynamicHMC.DynamicHMCConfig{Tuple{}, Tuple{InitialStepsizeSearch, TuningNUTS{Nothing, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{LinearAlgebra.Diagonal, DualAveraging{Float64}}, TuningNUTS{Nothing, DualAveraging{Float64}}}, DynamicHMC.NUTS{Val{:generalized}}, NoProgressReport, Val{:ForwardDiff}}, nsamples::Int64, nchains::Int64)
   @ Soss ~/.julia/packages/Soss/zb3t5/src/samplechains/dynamichmc.jl:19
 [4] sample (repeats 2 times)
   @ ~/.julia/packages/Soss/zb3t5/src/samplechains/dynamichmc.jl:31 [inlined]
 [5] top-level scope
   @ REPL[7]:1
environment ```julia julia> using Pkg julia> Pkg.status() Status `/tmp/jl_HamTdQ/Project.toml` [eadaa1a4] MeasureTheory v0.16.0 [6d9fd711] SampleChainsDynamicHMC v0.3.5 [8ce77f84] Soss v0.21.0 julia> versioninfo() Julia Version 1.7.2 Commit bf53498635 (2022-02-06 15:21 UTC) Platform Info: OS: Linux (x86_64-pc-linux-gnu) CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-12.0.1 (ORCJIT, tigerlake) Environment: JULIA_CMDSTAN_HOME = /home/sethaxen/software/cmdstan/2.29.1 JULIA_NUM_THREADS = auto JULIA_EDITOR = code ```
cscherrer commented 2 years ago

Thanks @sethaxen for catching this. Looks like the example needs to be in the tests.

Weirdly, in both 1.7 and 1.8 I'm getting a different error:

julia> post = sample(m(x=x) | (y=y,), dynamichmc())
ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getproperty
    @ ./Base.jl:38 [inlined]
  [2] macro expansion
    @ ~/.julia/packages/GeneralizedGenerated/72VHh/src/closure_conv.jl:132 [inlined]
cscherrer commented 2 years ago

@thautwarm do you think this could be the same issue as https://github.com/cscherrer/Soss.jl/issues/331?

Here's the error I'm getting:

julia> logdensityof(m(x=x) | (y=y,), (σ=2,α=3,β=5))
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty
   @ ./Base.jl:38 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/GeneralizedGenerated/72VHh/src/closure_conv.jl:132 [inlined]
 [3] _logdensityof(M::Type{GeneralizedGenerated.NGG.TypeLevel{Module, "Buf{17}()"}}, _m::Model{NamedTuple{(:x,)}, GeneralizedGenerated.NGG.TypeLevel{Expr, "Buf{23}()"}, GeneralizedGenerated.NGG.TypeLevel{Module, "Buf{17}()"}}, _args::NamedTuple{(:x,), Tuple{Vector{Float64}}}, _data::NamedTuple{(:y,), Tuple{Vector{Float64}}}, _pars::NamedTuple{(:σ, :α, :β), Tuple{Int64, Int64, Int64}})
   @ Soss ~/git/Soss.jl/src/primitives/logdensity.jl:39
 [4] logdensityof(c::Soss.ConditionalModel{NamedTuple{(:x,)}, GeneralizedGenerated.NGG.TypeLevel{Expr, "Buf{23}()"}, GeneralizedGenerated.NGG.TypeLevel{Module, "Buf{17}()"}, NamedTuple{(:x,), Tuple{Vector{Float64}}}, NamedTuple{(:y,), Tuple{Vector{Float64}}}}, x::NamedTuple{(:σ, :α, :β), Tuple{Int64, Int64, Int64}})
   @ Soss ~/git/Soss.jl/src/primitives/logdensity.jl:8
 [5] top-level scope
   @ REPL[21]:1

sourceLogdensityOf()(m) produces this, which looks ok to me:

julia> sourceLogdensityOf()(m)
quote
    _ℓ = 0.0
    _ℓ += Soss.logdensityof(Exponential(), σ)
    σ = Soss.predict(Exponential(), σ)
    _ℓ += Soss.logdensityof(Normal(), β)
    β = Soss.predict(Normal(), β)
    _ℓ += Soss.logdensityof(Lebesgue(ℝ), α)
    α = Soss.predict(Lebesgue(ℝ), α)
    _ℓ += Soss.logdensityof(For(x) do xj
                #= REPL[6]:6 =#
                Normal(α + β * xj, σ)
            end, y)
    y = Soss.predict(For(x) do xj
                #= REPL[6]:6 =#
                Normal(α + β * xj, σ)
            end, y)
    nothing
    return _ℓ
end

@sethaxen , I'm guessing your error might be different with a different version of TransformVariables. Can you give me your ]status -m?

cscherrer commented 2 years ago

That has the direct dependencies, but I'm wondering if some indirect ones might be different. -m here is for "manifest"

sethaxen commented 2 years ago

Here's the manifest:

(jl_iQj5fq) pkg> status -m
      Status `/tmp/jl_iQj5fq/Manifest.toml`
  [398f06c4] AbstractLattices v0.2.1
  [1520ce14] AbstractTrees v0.3.4
  [7d9f7c33] Accessors v0.1.11
  [79e6a3ab] Adapt v3.3.3
  [dce04be8] ArgCheck v2.3.0
  [4fba245c] ArrayInterface v6.0.1
  [30b0a656] ArrayInterfaceCore v0.1.2
  [b0d46f97] ArrayInterfaceStaticArrays v0.1.2
  [4c555306] ArrayLayouts v0.7.10
  [65a8f2f4] ArraysOfArrays v0.5.8
  [15f4f7f2] AutoHashEquals v0.2.0
  [198e06fe] BangBang v0.3.36
  [9718e550] Baselet v0.1.1
  [e2ed5e7c] Bijections v0.1.3
  [49dc2e85] Calculus v0.5.1
  [d360d2e6] ChainRulesCore v1.15.0
  [9e997f8a] ChangesOfVariables v0.1.3
  [861a8166] Combinatorics v1.0.2
  [bbf7d656] CommonSubexpressions v0.3.0
  [34da2185] Compat v3.44.0
  [a33af91c] CompositionsBase v0.1.1
  [2569d6c7] ConcreteStructs v0.2.3
  [187b0558] ConstructionBase v1.3.0
  [9a962f9c] DataAPI v1.10.0
  [864edb3b] DataStructures v0.18.13
  [e2d170a0] DataValueInterfaces v1.0.0
  [244e2a9f] DefineSingletons v0.1.2
  [b429d917] DensityInterface v0.4.0
  [163ba53b] DiffResults v1.0.3
  [b552c78f] DiffRules v1.11.0
  [31c24e10] Distributions v0.25.59
  [ffbed154] DocStringExtensions v0.8.6
  [fa6b7ba4] DualNumbers v0.6.8
  [bbc10e6e] DynamicHMC v3.1.1
  [6c76993d] DynamicIterators v0.4.2
  [7c1d4256] DynamicPolynomials v0.4.5
  [fdbdab4c] ElasticArrays v1.2.9
  [e2ba6199] ExprTools v0.1.8
  [1a297f60] FillArrays v0.12.8
  [6a86dc24] FiniteDiff v2.12.0
  [b67e1e5a] FlexLinearAlgebra v0.1.0
  [f6369f11] ForwardDiff v0.10.30
  [6b9d7cbe] GeneralizedGenerated v0.3.3
  [34004b35] HypergeometricFunctions v0.3.10
  [615f187c] IfElse v0.1.1
  [e1ba4f0e] Infinities v0.1.4
  [22cec73e] InitialValues v0.3.1
  [842dd82b] InlineStrings v1.1.2
  [18e54dd8] IntegerMathUtils v0.1.0
  [d8418881] Intervals v1.6.0
  [3587e190] InverseFunctions v0.1.4
  [92d709cd] IrrationalConstants v0.1.1
  [c8e1da08] IterTools v1.4.0
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.4.1
  [b14d175d] JuliaVariables v0.2.4
  [4d827475] KeywordCalls v0.2.4
  [2ee39098] LabelledArrays v1.10.0
  [5078a376] LazyArrays v0.21.20
  [9c8b4983] LightXML v0.9.0
  [d3d80556] LineSearches v7.1.1
  [9b3f67b0] LinearAlgebraX v0.1.9
  [6fdf6af0] LogDensityProblems v0.11.2
  [2ab3a3ac] LogExpFunctions v0.3.15
  [aa2f6b4e] LogarithmicNumbers v1.2.0
  [6e857e4b] MCMCDiagnostics v0.3.0
  [d8e11817] MLStyle v0.4.11
  [1914dd2f] MacroTools v0.5.9
  [dbb5928d] MappedArrays v0.4.1
  [a3b82374] MatrixFactorizations v0.8.7
  [fa1605e6] MeasureBase v0.9.1
  [eadaa1a4] MeasureTheory v0.16.0
  [eff96d63] Measurements v2.7.2
  [e9d8d322] Metatheory v1.3.3
  [128add7d] MicroCollections v0.1.2
  [e1d29d7a] Missings v1.0.2
  [78c3b35d] Mocking v0.7.3
  [7475f97c] Mods v1.3.2
  [3b2b4ff1] Multisets v0.4.4
  [102ac46a] MultivariatePolynomials v0.4.6
  [d8a4904e] MutableArithmetics v1.0.2
  [d41bc354] NLSolversBase v7.8.2
  [77ba4419] NaNMath v0.3.7
  [71a1bf82] NameResolution v0.1.5
  [d9ec5142] NamedTupleTools v0.14.0
  [a734d2a7] NestedTuples v0.3.9
  [429524aa] Optim v1.7.0
  [bac558e1] OrderedCollections v1.4.1
  [90014a1f] PDMats v0.11.10
  [d96e819e] Parameters v0.12.3
  [69de0a69] Parsers v2.3.1
  [2ae35dd2] Permutations v0.4.14
  [f27b6e38] Polynomials v2.0.25
  [85a6dd25] PositiveFactorizations v0.2.4
  [21216c6a] Preferences v1.3.0
  [8162dcfd] PrettyPrint v0.2.0
  [54e16d92] PrettyPrinting v0.4.0
  [27ebfcd6] Primes v0.5.2
  [92933f4c] ProgressMeter v1.7.2
  [1fd47b50] QuadGK v2.4.2
  [3cdcf5f2] RecipesBase v1.2.1
  [189a3867] Reexport v1.2.2
  [42d2dcc6] Referenceables v0.1.2
  [ae029012] Requires v1.3.0
  [286e9d63] RingLists v0.2.7
  [79098fc4] Rmath v0.7.0
  [7e49a35a] RuntimeGeneratedFunctions v0.5.3
  [754583d1] SampleChains v0.5.0
  [6d9fd711] SampleChainsDynamicHMC v0.3.5
  [efcf1570] Setfield v0.8.2
  [55797a34] SimpleGraphs v0.7.18
  [ec83eff0] SimplePartitions v0.3.0
  [cc47b68c] SimplePolynomials v0.2.9
  [b2aef97b] SimplePosets v0.1.5
  [a6525b86] SimpleRandom v0.3.1
  [a2af1166] SortingAlgorithms v1.0.1
  [8ce77f84] Soss v0.21.0
  [276daf66] SpecialFunctions v2.1.5
  [171d559e] SplittablesBase v0.1.14
  [aedffcd0] Static v0.6.6
  [90137ffa] StaticArrays v1.4.4
  [82ae8749] StatsAPI v1.3.0
  [2913bbd2] StatsBase v0.33.16
  [4c63d2b9] StatsFuns v1.0.1
  [09ab397b] StructArrays v0.6.8
  [d1185830] SymbolicUtils v0.19.7
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.7.0
  [8ea1fca8] TermInterface v0.2.3
  [ac1d9e8a] ThreadsX v0.1.10
  [f269a46b] TimeZones v1.7.3
  [a759f4b9] TimerOutputs v0.5.19
  [2c80a279] Trajectories v0.2.2
  [28d57a85] Transducers v0.4.73
  [84d833dd] TransformVariables v0.6.1
  [410a4b4d] Tricks v0.1.6
  [615932cf] TupleVectors v0.1.5
  [3a884ed6] UnPack v1.0.2
  [c4a57d5a] UnsafeArrays v1.0.3
  [700de1a5] ZygoteRules v0.2.2
  [94ce4f54] Libiconv_jll v1.16.1+1
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [f50d1b31] Rmath_jll v0.3.0+0
  [02c8fc9c] XML2_jll v2.9.12+0
  [0dad84c5] ArgTools
  [56f22d72] Artifacts
  [2a0f44e3] Base64
  [ade2ca70] Dates
  [8bb1440f] DelimitedFiles
  [8ba89e20] Distributed
  [f43a241f] Downloads
  [9fa8497b] Future
  [b77e0a4c] InteractiveUtils
  [4af54fe1] LazyArtifacts
  [b27032c2] LibCURL
  [76f85450] LibGit2
  [8f399da3] Libdl
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [d6f4376e] Markdown
  [a63ad114] Mmap
  [ca575930] NetworkOptions
  [44cfe95a] Pkg
  [de0858da] Printf
  [3fa0cd96] REPL
  [9a3f8284] Random
  [ea8e919c] SHA
  [9e88b42a] Serialization
  [1a1011a3] SharedArrays
  [6462fe0b] Sockets
  [2f01184e] SparseArrays
  [10745b16] Statistics
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML
  [a4e569a6] Tar
  [8dfed614] Test
  [cf7118a7] UUIDs
  [4ec0a83e] Unicode
  [e66e0078] CompilerSupportLibraries_jll
  [deac9b47] LibCURL_jll
  [29816b5a] LibSSH2_jll
  [c8ffd9c3] MbedTLS_jll
  [14a3606d] MozillaCACerts_jll
  [4536629a] OpenBLAS_jll
  [05823500] OpenLibm_jll
  [83775a58] Zlib_jll
  [8e850b90] libblastrampoline_jll
  [8e850ede] nghttp2_jll
  [3f19e933] p7zip_jll
cscherrer commented 2 years ago

Thanks. This is before your last comment in https://github.com/cscherrer/Soss.jl/issues/335, could you try again now? Good starting point to make sure we're getting the same error.

sethaxen commented 2 years ago

@cscherrer right you are, now I'm getting the same error you're getting.

cscherrer commented 2 years ago

Progress! :)

I've opened a new issue so we're not distracted by the different errors we were getting, closing this now