How to solve error while compiling custom package that uses Turing with Revise

Hi,

I have a custom package named MyPkg and it contains a function named test_fun() as follows, that copied and modified from Turing documentation.

function test_fun()
    # Set the true probability of heads in a coin.
    p_true = 0.5

    # Iterate from having seen 0 observations to 100 observations.
    Ns = 0:100;

    # Draw data from a Bernoulli distribution, i.e. draw heads or tails.
    Random.seed!(12)
    data = rand(Bernoulli(p_true), last(Ns))

    # Declare our Turing model.
    @model coinflip(y, ::Type{T}=Float64) where {T} = begin
        # Our prior belief about the probability of heads in a coin.
        p ~ Beta(1, 1)

        # The number of observations.
        N = length(y)
        for n in 1:N
            # Heads or tails of a coin are drawn from a Bernoulli distribution.
            y[n] ~ Bernoulli(p)
        end
    end;

    # Settings of the Hamiltonian Monte Carlo (HMC) sampler.
    iterations = 1000
    Ο΅ = 0.05
    Ο„ = 10

    # Start sampling.
    chain = sample(coinflip(data), HMC(Ο΅, Ο„), iterations);
    return chain
end

When I precompile this custom package the second time, as

using Revise
using MyPkg

I am getting an error as follows:

julia> β”Œ Error: evaluation error starting at C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:34
β”‚   mod = MyPkg
β”‚   ex =
β”‚    quote
β”‚        #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:34 =#
β”‚        function test_fun()
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:36 =#
β”‚            p_true = 0.5
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:39 =#
β”‚            Ns = 0:100
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:42 =#
β”‚            Random.seed!(12)
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:43 =#
β”‚            data = rand(Bernoulli(p_true), last(Ns))
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:46 =#
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:46 =# @model (coinflip(y, ::Type{T}=Float64) where T) = begin
β”‚                        #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:46 =#
β”‚                        #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:48 =#
β”‚                        p ~ Beta(1, 1)
β”‚                        #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:51 =#
β”‚                        N = length(y)
β”‚                        #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:52 =#
β”‚                        for n = 1:N
β”‚                            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:54 =#
β”‚                            y[n] ~ Bernoulli(p)
β”‚                        end
β”‚                    end
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:59 =#
β”‚            iterations = 1000
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:60 =#
β”‚            Ο΅ = 0.05
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:61 =#
β”‚            Ο„ = 10
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:64 =#
β”‚            chain = sample(coinflip(data), HMC(Ο΅, Ο„), iterations)
β”‚            #= C:\Users\zac\.julia\dev\MyPkg\src\MyPkg.jl:65 =#
β”‚            return chain
β”‚        end
β”‚    end
β”‚   exception =
β”‚    UndefVarError: #s95 not defined
β”‚    Stacktrace:
β”‚     [1] step_expr!(::Any, ::JuliaInterpreter.Frame, ::Any, ::Bool) at C:\Users\zac\.julia\packages\JuliaInterpreter\hyz9z\src\interpret.jl:532
β”” @ Revise C:\Users\zac\.julia\packages\Revise\S7mrl\src\lowered.jl:105

When I modify the model definition in the function @model coinflip(y, ::Type{T}=Float64) where {T} = begin to @model coinflip(y) = begin, then there is no error. But in my other functions, I need to use the first type of model definition. So, how can I solve this error?

Thanks in Advance !
Manu

Can you check to see if it’s fixed by https://github.com/JuliaDebug/LoweredCodeUtils.jl/pull/25? You can get it with pkg> add LoweredCodeUtils#teh/rvs422 and restarting.

Actually, that’s been merged and released, along with several other improvements/bugfixes. You should be able to do pkg> up and get all the goodness.

@tim.holy : First I tried to add LoweredCodeUtils using following commands:
]add LoweredCodeUtils#teh/rvs422
and I got an error as following:

  Updating registry at `C:\Users\z5168736\.julia\registries\General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Updating git-repo `https://github.com/JuliaDebug/LoweredCodeUtils.jl.git`
  Updating git-repo `https://github.com/JuliaDebug/LoweredCodeUtils.jl.git`
ERROR: git object teh/rvs422 could not be found

Then I removed Revise package and add fresh packages:

add Revise
add LoweredCodeUtils

after that, I restarted and updated by using ]up. But still, I am getting above error, when I do the same procedure.

Am I did in right way? If there is any mistake could you please point out that?

But the great thing is, when I do revise the package after second try…means third , fourth and so on revise, it is working….
The main thing is, if there is no @model definition using Turing package with a datatype of T as discussed in question(eg: @model coinflip(y, ::Type{T}=Float64)), then no error at all.

Thanks in Advance !
Manu

Once the pull request got merged the teh/rvs422 branch got deleted; at that point you should just add the master branch or better yet, since a new version was released, just update the package. To make sure your update worked, try this:

(v1.3) pkg> st Revise
    Status `~/.julia/environments/v1.3/Project.toml`
  [da1fd8a2] CodeTracking v0.5.8
  [aa1ae85d] JuliaInterpreter v0.7.12
  [6f1432cf] LoweredCodeUtils v0.4.3
  [bac558e1] OrderedCollections v1.1.0
  [295af30f] Revise v2.5.1
  [cf7118a7] UUIDs 

If you’re behind on any of those versions, that could be the problem. If not, there’s still a bug somewhere in the Revise stack. Can you supply a runnable example? In your code above I get ERROR: LoadError: UndefVarError: @model not defined. I could probably figure it out from other clues, but it would be nice not to have to guess.

The fact that it works the second time is nice, but there’s still likely to be a problem with, e.g., specific methods.

Hi @tim.holy ,

Revise is updated to latest version and dependencies are as follows:

(v1.3) pkg> st Revise
    Status `C:\Users\z5168736\.julia\environments\v1.3\Project.toml`
  [da1fd8a2] CodeTracking v0.5.8
  [aa1ae85d] JuliaInterpreter v0.7.12
  [6f1432cf] LoweredCodeUtils v0.4.3
  [bac558e1] OrderedCollections v1.1.0
  [295af30f] Revise v2.5.1
  [cf7118a7] UUIDs

Still I am getting the same error.

I can provide a runnable example.

I have created a package named TestPkg using the following commands in ~.julia\dev folder:

cd("C:\\Users\\zac\.julia\dev")
]generate TestPkg

Then the structure of the package is as follows:

Generating project TestPkg:
    TestPkg/Project.toml
    TestPkg/src/TestPkg.jl

After that I run the following command:

dev C:\\Users\\zac\.julia\dev\\TestPkg

I have added dependencies to the package using the following command:

add https://github.com/TuringLang/Turing.jl.git
free Turing

I used master branch because I got some error during compiling when simply used add Turing

Then I modified TestPkg.jl in ~.julia/dev/TestPkg/src/ as follows:

module TestPkg

using Turing, Random


greet() = print("Hello World!")

function test_fun()
    # Set the true probability of heads in a coin.
    p_true = 0.5

    # Iterate from having seen 0 observations to 100 observations.
    Ns = 0:100;

    # Draw data from a Bernoulli distribution, i.e. draw heads or tails.
    Random.seed!(12)
    data = rand(Bernoulli(p_true), last(Ns))
    # g_data = CuArray(data)

    # Declare our Turing model.
    @model coinflip(y,::Type{T}=Float64) where {T} = begin
        # Our prior belief about the probability of heads in a coin.
        p ~ Beta(1, 1)

        # The number of observations.
        N = length(y)
        for n in 1:N
            # Heads or tails of a coin are drawn from a Bernoulli distribution.
            y[n] ~ Bernoulli(p)
        end
    end;

    # Settings of the Hamiltonian Monte Carlo (HMC) sampler.
    iterations = 1000
    Ο΅ = 0.05
    Ο„ = 10

    # Start sampling.
    chain = sample(coinflip(data), HMC(Ο΅, Ο„), iterations);
    return chain
end

end # module

After that, I used this package using the following commands:

using Revise
using TestPkg

This execution was error-free.

I can access the function test_fun() using the following command:

TestPkg.test_fun()

After that I just modified greet() function as follows:
greet() = print("Hello World123")
just modified the text in that function.

Then I revised the package using the following commands:

using Revise
Then I got the error:

julia> using Revise
β”Œ Error: evaluation error starting at C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:8
β”‚   mod = TestPkg
β”‚   ex =
β”‚    quote
β”‚        #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:8 =#
β”‚        function test_fun()
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:10 =#
β”‚            p_true = 0.5
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:13 =#
β”‚            Ns = 0:100
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:16 =#
β”‚            Random.seed!(12)
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:17 =#
β”‚            data = rand(Bernoulli(p_true), last(Ns))
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:21 =#
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:21 =# @model (coinflip(y, ::Type{T}=Float64) where T) = begin
β”‚                        #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:21 =#
β”‚                        #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:23 =#
β”‚                        p ~ Beta(1, 1)
β”‚                        #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:26 =#
β”‚                        N = length(y)
β”‚                        #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:27 =#
β”‚                        for n = 1:N
β”‚                            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:29 =#
β”‚                            y[n] ~ Bernoulli(p)
β”‚                        end
β”‚                    end
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:34 =#
β”‚            iterations = 1000
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:35 =#
β”‚            Ο΅ = 0.05
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:36 =#
β”‚            Ο„ = 10
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:39 =#
β”‚            chain = sample(coinflip(data), HMC(Ο΅, Ο„), iterations)
β”‚            #= C:\Users\z5168736\.julia\dev\TestPkg\src\TestPkg.jl:40 =#
β”‚            return chain
β”‚        end
β”‚    end
β”‚   exception =
β”‚    UndefVarError: #s84 not defined
β”‚    Stacktrace:
β”‚     [1] step_expr!(::Any, ::JuliaInterpreter.Frame, ::Any, ::Bool) at C:\Users\z5168736\.julia\packages\JuliaInterpreter\VxcSp\src\interpret.jl:535
β”” @ Revise C:\Users\z5168736\.julia\packages\Revise\ZOWOa\src\lowered.jl:105

But if I modify the function test_fun() as follows, then there is no error during revise:

function test_fun()
    # Set the true probability of heads in a coin.
    p_true = 0.5

    # Iterate from having seen 0 observations to 100 observations.
    Ns = 0:100;

    # Draw data from a Bernoulli distribution, i.e. draw heads or tails.
    Random.seed!(12)
    data = rand(Bernoulli(p_true), last(Ns))
    # g_data = CuArray(data)

    # Declare our Turing model.
    @model coinflip(y) = begin
        # Our prior belief about the probability of heads in a coin.
        p ~ Beta(1, 1)

        # The number of observations.
        N = length(y)
        for n in 1:N
            # Heads or tails of a coin are drawn from a Bernoulli distribution.
            y[n] ~ Bernoulli(p)
        end
    end;

    # Settings of the Hamiltonian Monte Carlo (HMC) sampler.
    iterations = 1000
    Ο΅ = 0.05
    Ο„ = 10

    # Start sampling.
    chain = sample(coinflip(data), HMC(Ο΅, Ο„), iterations);
    return chain
end

The only difference is at the line containing @model, just modified @model coinflip(y,::Type{T}=Float64) where {T} = begin to @model coinflip(y) = begin

But my actual purpose, I need the first case, that is why I am sticking to that method.

Thanking you,
Manu

This is a really bizarre bug, I confess I have no idea what’s going on. Currently, the most plausible theory is that something (Turing.jl?) is breaking Julia: https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/380. Can you reproduce this in a scenario where you’re not loading Turing.jl?

1 Like

@tim.holy : Thanks again for looking in my problem. Actually, I saw this problem only with Turing in a particular scenario that I mentioned above related to @model. If I saw this problem in different case, I can inform you.

Thank You,
Manu

@tim.holy : Sorry to bother you. Is there any possibility to solve this issue?

Thank You,
Manu

Any bug is fixable in principle, but it’s an extremely weird bug. We have to figure out what’s breaking Julia. If you want to help out (I am currently swamped), try stripping everything down to the most minimal dependencies possible. Turing is not exactly a small dependency.

2 Likes

Thank you for your reply. I will try to find the smallest dependency.