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

**URL:** <https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745>\
**Category:** General Usage\
**Tags:** turing, revise\
**Created:** [February 17, 2020, 4:43am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745 "2020-02-17T04:43:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 17, 2020, 4:43am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/1 "2020-02-17T04:43:55Z")

</div>

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.

```julia
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

```julia
using Revise
using MyPkg

```

I am getting an error as follows:

```julia
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

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 17, 2020, 8:06am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/2 "2020-02-17T08:06:41Z")

</div>

Can you check to see if it’s fixed by [Ensure SlotNumbers are assigned when deciding top of signature by timholy · Pull Request #25 · JuliaDebug/LoweredCodeUtils.jl · GitHub](https://github.com/JuliaDebug/LoweredCodeUtils.jl/pull/25)? You can get it with `pkg> add LoweredCodeUtils#teh/rvs422` and restarting.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 17, 2020, 4:49pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/3 "2020-02-17T16:49:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 18, 2020, 12:32am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/4 "2020-02-18T00:32:50Z")

</div>

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

```julia
  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:

```julia
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

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 18, 2020, 7:53am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/5 "2020-02-18T07:53:09Z")

</div>

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:

```julia
(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.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 19, 2020, 1:55am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/6 "2020-02-19T01:55:08Z")

</div>

Hi @tim.holy ,

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

```julia
(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:

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

```

Then the structure of the package is as follows:

```julia
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:

```julia
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:

```julia
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:

```julia
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
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:

```julia
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

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 19, 2020, 9:03pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/7 "2020-02-19T21:03:50Z")

</div>

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: [Puzzling failure to set the value of a symbol · Issue #380 · JuliaDebug/JuliaInterpreter.jl · GitHub](https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/380). Can you reproduce this in a scenario where you’re not loading Turing.jl?

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 19, 2020, 11:43pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/8 "2020-02-19T23:43:37Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [March 3, 2020, 3:52am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/9 "2020-03-03T03:52:14Z")

</div>

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

Thank You,  
Manu

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 3, 2020, 7:42am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/10 "2020-03-03T07:42:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [March 3, 2020, 10:25am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-compiling-custom-package-that-uses-turing-with-revise/34745/11 "2020-03-03T10:25:49Z")

</div>

> [@tim.holy](#):
>
> swamped

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