Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error #34232

Closed
mohamed82008 opened this issue Jan 1, 2020 · 13 comments
Closed

Compiler error #34232

mohamed82008 opened this issue Jan 1, 2020 · 13 comments

Comments

@mohamed82008
Copy link
Contributor

mohamed82008 commented Jan 1, 2020

Hi!

The following code triggers what looks like a compiler bug.

using Pkg
pkg"dev https://github.com/TuringLang/DynamicPPL.jl"
Pkg.activate("DynamicPPL")
Pkg.instantiate()

using DynamicPPL, AdvancedHMC, Random
dir = splitdir(splitdir(pathof(DynamicPPL))[1])[1]
include(dir*"/test/Turing/Turing.jl")
using .Turing

function get_spl()
    @model demo() = begin
        p ~ Normal()
    end
    return Turing.Sampler(HMC(0.75, 3), demo())
end
f(spl) = AdvancedHMC.step(Random.GLOBAL_RNG, spl.state.h, spl.state.traj, spl.state.z)
spl = get_spl()
f(spl)

Here is the stacktrace:

Internal error: encountered unexpected error in runtime:
rec_backtrace at /home/Administrator/buildbot/worker/package_win64/build/src\stackwalk.c:94
record_backtrace at /home/Administrator/buildbot/worker/package_win64/build/src\task.c:217 [inlined]
jl_throw at /home/Administrator/buildbot/worker/package_win64/build/src\task.c:417
jl_type_error_rt at /home/Administrator/buildbot/worker/package_win64/build/src\rtutils.c:118
jl_type_error at /home/Administrator/buildbot/worker/package_win64/build/src\rtutils.c:125
jl_f_issubtype at /home/Administrator/buildbot/worker/package_win64/build/src\builtins.c:408
typeof_tfunc at .\compiler\tfuncs.jl:444
typeof_tfunc at .\compiler\tfuncs.jl:442
typeof_tfunc at .\compiler\tfuncs.jl:447
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219
jl_apply at /home/Administrator/buildbot/worker/package_win64/build/src\julia.h:1571 [inlined]
jl_f__apply at /home/Administrator/buildbot/worker/package_win64/build/src\builtins.c:556
builtin_tfunction at .\compiler\tfuncs.jl:1286
builtin_tfunction at .\compiler\tfuncs.jl:1200 [inlined]
abstract_call at .\compiler\abstractinterpretation.jl:593
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219
abstract_apply at .\compiler\abstractinterpretation.jl:519
abstract_call at .\compiler\abstractinterpretation.jl:567
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_edge at .\compiler\typeinfer.jl:497
abstract_call_method at .\compiler\abstractinterpretation.jl:345
abstract_call_gf_by_type at .\compiler\abstractinterpretation.jl:85
abstract_call at .\compiler\abstractinterpretation.jl:776
abstract_eval_call at .\compiler\abstractinterpretation.jl:805
abstract_eval at .\compiler\abstractinterpretation.jl:890
typeinf_local at .\compiler\abstractinterpretation.jl:1135
typeinf_nocycle at .\compiler\abstractinterpretation.jl:1191
typeinf at .\compiler\typeinfer.jl:14
typeinf_ext at .\compiler\typeinfer.jl:576
typeinf_ext at .\compiler\typeinfer.jl:613
jfptr_typeinf_ext_12 at C:\Users\user\AppData\Local\Julia-1.1.0\lib\julia\sys.dll (unknown line)
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219 [inlined]
jl_apply at /home/Administrator/buildbot/worker/package_win64/build/src\julia.h:1571 [inlined]
jl_type_infer at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:277
jl_compile_method_internal at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:1819 [inlined]
jl_fptr_trampoline at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:1863
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219
do_call at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:323
eval_value at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:411
eval_stmt_value at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:362 [inlined]
eval_body at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:755
jl_interpret_toplevel_thunk_callback at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:885
Interpreter frame (ip: 0)
jl_toplevel_eval_flex at /home/Administrator/buildbot/worker/package_win64/build/src\toplevel.c:764
jl_toplevel_eval at /home/Administrator/buildbot/worker/package_win64/build/src\toplevel.c:773 [inlined]
jl_toplevel_eval_in at /home/Administrator/buildbot/worker/package_win64/build/src\toplevel.c:793
eval at .\boot.jl:328
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219
eval_user_input at C:\Users\user\AppData\Local\Julia-1.1.0\share\julia\stdlib\v1.1\REPL\src\REPL.jl:85
macro expansion at C:\Users\user\AppData\Local\Julia-1.1.0\share\julia\stdlib\v1.1\REPL\src\REPL.jl:117 [inlined]
#26 at .\task.jl:259
jl_fptr_trampoline at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:1864
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2219
jl_apply at /home/Administrator/buildbot/worker/package_win64/build/src\julia.h:1571 [inlined]
start_task at /home/Administrator/buildbot/worker/package_win64/build/src\task.c:572
TypeError(AdvancedHMCfunc=.:Transition<:, {contextAdvancedHMC=".PhasePoint"{, expectedArray={Type, Float64got,=T1<:}AbstractFloat,)
AdvancedHMCCore..CodeInfoDualValue(code{=Float64Array{Any,, (Array2,)}[{
  Float64Expr(:call, ,:1f, }:}spl)},
  ,Expr(:returnNamedTuple, SSAValue(1){)(], :codelocs=n_stepsArray{,Int32, ( 2,)}[:1, is_accept1,] , method_for_inference_limit_heuristics:=nothingacceptance_rate, ,ssavaluetypes= 2:, linetablelog_density=,Array{Any , (:1,)}[hamiltonian_energyCore,.LineInfoNode (:mod=hamiltonian_energy_errorMain,, method =:Symbol("top-level scopestep_size"),, file =::nonenom_step_size, )line=,0, Tupleinlined_at{=0Int64)],, Boolssaflags=,Array{Float64UInt8, (,0,Float64)}[],, Float64slotflags=,Array{Float64UInt8, (,0,Float64)}[],, Float64slotnames=}Array{}Any, (}0,()}[]AdvancedHMC, inferred.=PhasePointfalse{, inlineableArray=false{, propagate_inboundsFloat64=false,, 1pure}=false,)AdvancedHMC.DualValue{Float64,Array{Float64,1}}}([0.525051], [-1.05344], AdvancedHMC.DualValue{Float64,Array{Float64,1}}(-1.056778029414681, [0.525051]), AdvancedHMC.DualValue{Float64,Array{Float64,1}}(-0.5548677541779807, [1.05344])), (n_steps = 3, is_accept = true, acceptance_rate = 1.0, log_density = -1.056778029414681, hamiltonian_energy = 1.6116457835926616, hamiltonian_energy_error = -0.08100187821636595, step_size = 0.75, nom_step_size = 0.75))

and my versioninfo():

Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

What seems to be a similar error also happens on other platforms (https://travis-ci.com/TuringLang/DynamicPPL.jl). The error doesn't affect the result of the function though, it just happens once when the function is compiled.

@mohamed82008
Copy link
Contributor Author

Narrowed down the trigger a bit more:

const AbstractScalarOrVec{T} = Union{T,AbstractVector{T}} where {T<:AbstractFloat}

function mh1(
    rng::Union{AbstractRNG,AbstractVector{<:AbstractRNG}},
    Horiginal::AbstractScalarOrVec{<:T},
    Hproposal::AbstractScalarOrVec{<:T},
) where {T<:AbstractFloat}
    α = min.(1.0, exp.(Horiginal .- Hproposal))
    accept = rand(rng) .< α
    return accept, α
end
function mh2(
    rng::Union{AbstractRNG,AbstractVector{<:AbstractRNG}},
    Horiginal::AbstractScalarOrVec{<:T},
    Hproposal::AbstractScalarOrVec{<:T},
) where {T<:AbstractFloat}
    α = min(1.0, exp(Horiginal - Hproposal))
    accept = rand(rng) < α
    return accept, α
end
function mh3(
    rng,
    Horiginal,
    Hproposal,
)
    α = min.(1.0, exp.(Horiginal .- Hproposal))
    accept = rand(rng) .< α
    return accept, α
end
function g(spl)
    h = spl.state.h
    rng = Random.GLOBAL_RNG
    θ = spl.state.z.θ
    value, gradient = h.∂ℓπ∂θ(θ)
    e1, e2 = value, value

    # Doesn't work
    is_accept, α = mh1(rng, e1, e2)
    
    # Work
    #is_accept, α = mh2(rng, e1, e2)
    #is_accept, α = mh3(rng, e1, e2)
     
    is_accept, α
end
g(spl)

Using mh1 in g triggers the error while mh2 and mh3 don't. The error seems to be a result of bad interplay between broadcasting scalars and dispatch.

@KristofferC
Copy link
Member

What is the result on julia 1.3 (and if you can check, master).

@mohamed82008
Copy link
Contributor Author

Seems to be fixed on 1.3 https://travis-ci.com/TuringLang/DynamicPPL.jl/jobs/271769735.

@mohamed82008
Copy link
Contributor Author

Actually even 1.2.

@KristofferC
Copy link
Member

Okay so not sure what is left to do here, we are not making any more 1.1 releases.

@mohamed82008
Copy link
Contributor Author

What about 1.0?

@KristofferC
Copy link
Member

But it seems to pass on 1.0 as well?

@mohamed82008
Copy link
Contributor Author

@KristofferC
Copy link
Member

Ah, I just looked at the build pass status. If the commit that fixed it can be found and backported then it could be included in a new 1.0.x release, yes.

@vtjnash
Copy link
Member

vtjnash commented Jan 2, 2020

Restored backport-pending-1.0 label to #31405. This time, we'll just drop the new tests from the backport (#30954 (comment)).

@JeffBezanson
Copy link
Member

const AbstractScalarOrVec{T} = Union{T,AbstractVector{T}} where {T<:AbstractFloat}

FYI, this is redundant. A{T} = B is syntax for A = B where T, so this is just adding an extra where wrapper that gets dropped.

@mohamed82008
Copy link
Contributor Author

FYI, this is redundant. A{T} = B is syntax for A = B where T, so this is just adding an extra where wrapper that gets dropped.

Cool, thanks!

@vtjnash vtjnash closed this as completed Sep 15, 2020
@vtjnash
Copy link
Member

vtjnash commented Sep 15, 2020

Fixed on master (actually on v1.2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants