FluxML / Zygote.jl

21st century AD
https://fluxml.ai/Zygote.jl/
Other
1.48k stars 210 forks source link

Sin taylor series breaks #249

Open ViralBShah opened 5 years ago

ViralBShah commented 5 years ago

This used to work with Zygote 0.3.1 but now gives the following compiler error for 0.3.2.

julia> function s(x) 
                t = 0.0
                sign = -1.0 
                for i in 1:19   
                   if isodd(i)
                      newterm = x^i/factorial(i)
                      abs(newterm)<1e-8 && return t
                      println("i=",i)
                      sign = -sign
                      t += sign * newterm
                   end
                end
                return t 
              end
s (generic function with 1 method)

julia> Zygote.gradient(s, 1.0)  
i=1
i=3
i=5
i=7
i=9
i=11
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.NewNode, (2,)}[
  Core.Compiler.NewNode(pos=5, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(2), 0x01), line=0),
  Core.Compiler.NewNode(pos=43, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(42), 0x01), line=0)], i=(22,))
rec_backtrace at /Users/julia/buildbot/worker/package_macos64/build/src/stackwalk.c:94
record_backtrace at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:217
jl_throw at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:417
jl_bounds_error_ints at /Users/julia/buildbot/worker/package_macos64/build/src/rtutils.c:183
getindex at ./array.jl:729 [inlined]
getindex at ./compiler/ssair/ir.jl:727 [inlined]
abstract_eval_ssavalue at ./compiler/ssair/queries.jl:61 [inlined]
argextype at ./compiler/utilities.jl:171
argextype at ./compiler/utilities.jl:157 [inlined]
assemble_inline_todo! at ./compiler/ssair/inlining.jl:796
ssa_inlining_pass! at ./compiler/ssair/inlining.jl:59 [inlined]
run_passes at ./compiler/ssair/driver.jl:118
optimize at ./compiler/optimize.jl:164
typeinf at ./compiler/typeinfer.jl:35
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_eval_call at ./compiler/abstractinterpretation.jl:803
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_1.clone_1 at /Users/viral/Desktop/Julia Releases/Julia-1.1.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
jl_apply_generic at /Users/julia/buildbot/worker/package_macos64/build/src/gf.c:2197 [inlined]
jl_apply at /Users/julia/buildbot/worker/package_macos64/build/src/./julia.h:1571 [inlined]
jl_type_infer at /Users/julia/buildbot/worker/package_macos64/build/src/gf.c:255
jl_compile_method_internal at /Users/julia/buildbot/worker/package_macos64/build/src/gf.c:1797
jl_fptr_trampoline at /Users/julia/buildbot/worker/package_macos64/build/src/gf.c:1841
gradient at /Users/viral/.julia/packages/Zygote/fuj2C/src/compiler/interface.jl:47
unknown function (ip: 0x128dfe4b8)
jl_fptr_trampoline at /Users/julia/buildbot/worker/package_macos64/build/src/gf.c:1842
do_call at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:323
eval_stmt_value at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:362 [inlined]
eval_body at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:759
jl_interpret_toplevel_thunk_callback at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:885
Interpreter frame (ip: 1)
Core.CodeInfo(code=Array{Any, (3,)}[
  Expr(:call, Base.getproperty, :Zygote, :(:gradient)),
  Expr(:call, SSAValue(1), :s, 1),
  Expr(:return, SSAValue(2))], codelocs=Array{Int32, (3,)}[1, 1, 1], method_for_inference_limit_heuristics=nothing, ssavaluetypes=3, linetable=Array{Any, (1,)}[Core.LineInfoNode(mod=Main, method=Symbol("top-level scope"), file=:none, line=0, inlined_at=0)], ssaflags=Array{UInt8, (0,)}[], slotflags=Array{UInt8, (0,)}[], slotnames=Array{Any, (0,)}[], inferred=false, inlineable=false, propagate_inbounds=false, pure=false)jl_interpret_toplevel_thunk at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:894
jl_toplevel_eval_flex at /Users/julia/buildbot/worker/package_macos64/build/src/toplevel.c:764
jl_toplevel_eval at /Users/julia/buildbot/worker/package_macos64/build/src/toplevel.c:773 [inlined]
jl_toplevel_eval_in at /Users/julia/buildbot/worker/package_macos64/build/src/toplevel.c:793
eval at ./boot.jl:328
eval_user_input at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:85
macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:117 [inlined]
#26 at ./task.jl:259
jl_apply at /Users/julia/buildbot/worker/package_macos64/build/src/./julia.h:1571 [inlined]
start_task at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:572
(0.5403023037918872,)
MikeInnes commented 5 years ago

Odd, it's working for me on both 1.0 and 1.2 with tagged versions. Can you show your versioinfo, manifest status etc?

Julia 1.0 ```julia julia> using Zygote [ Info: Recompiling stale cache file /home/mike/.julia/compiled/v1.0/Zygote/4kbLI.ji for Zygote [e88e6eb3-aa80-5325-afca-941959d7151f] julia> function s(x) t = 0.0 sign = -1.0 for i in 1:19 if isodd(i) newterm = x^i/factorial(i) abs(newterm)<1e-8 && return t println("i=",i) sign = -sign t += sign * newterm end end return t end s (generic function with 1 method) julia> gradient(s, 1.0) i=1 i=3 i=5 i=7 i=9 i=11 (0.5403023037918872,) julia> versioninfo() Julia Version 1.0.4-pre.0 Commit 5b7e8d9d4e (2018-12-19 09:38 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.0 (ORCJIT, skylake) (v1.0) pkg> st Status `~/.julia/environments/v1.0/Project.toml` [c52e3926] Atom v0.8.7 [3a865a2d] CuArrays v1.0.2 [587475ba] Flux v0.8.3 #master (https://github.com/FluxML/Flux.jl.git) [e5e0dc1b] Juno v0.7.0 [e88e6eb3] Zygote v0.3.2 ```
Julia 1.2 ```julia julia> using Zygote [ Info: Recompiling stale cache file /home/mike/.julia/compiled/v1.2/Zygote/4kbLI.ji for Zygote [e88e6eb3-aa80-5325-afca-941959d7151f] julia> function s(x) t = 0.0 sign = -1.0 for i in 1:19 if isodd(i) newterm = x^i/factorial(i) abs(newterm)<1e-8 && return t println("i=",i) sign = -sign t += sign * newterm end end return t end s (generic function with 1 method) julia> gradient(s, 1.0) i=1 i=3 i=5 i=7 i=9 i=11 (0.5403023037918872,) julia> versioninfo() Julia Version 1.2.0-rc1.0 Commit 7097799cf1 (2019-05-30 02:22 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.1 (ORCJIT, skylake) (v1.2) pkg> st Status `~/.julia/environments/v1.2/Project.toml` [c52e3926] Atom v0.8.7 [00ebfdb7] CSTParser v0.6.0+ #location (https://github.com/MikeInnes/CSTParser.jl) [3a865a2d] CuArrays v1.0.2 [31c24e10] Distributions v0.20.0 [587475ba] Flux v0.8.3 [7073ff75] IJulia v1.18.1 [7869d1d1] IRTools v0.2.2 [e5e0dc1b] Juno v0.7.0 [872c559c] NNlib v0.6.0 [85f4b15a] Vimes v0.1.0 [`../../../projects/Vimes`] [e88e6eb3] Zygote v0.3.2 ```
aviks commented 5 years ago

I got this error as well, on 1.1. This is a bummer, since I was hoping to show this tonight :(

i=1
i=3
i=5
i=7
i=9
i=11
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.NewNode, (4,)}[
  Core.Compiler.NewNode(pos=5, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(2), 0x01), line=0),
  Core.Compiler.NewNode(pos=41, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(40), 0x01), line=0),
  Core.Compiler.NewNode(pos=51, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(50), 0x01), line=0),
  Core.Compiler.NewNode(pos=78, attach_after=false, typ=Bool, node=Expr(:call, Core.:(===), SSAValue(75), 0x01), line=0)], i=(25,))
rec_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/stackwalk.c:94
record_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:217 [inlined]
jl_throw at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:417
jl_bounds_error_ints at 
ViralBShah commented 5 years ago

I was using Julia 1.1.

Note that it still gives the right answer. And the error does not happen on Zygote 0.3.1 with Julia 1.1.

ViralBShah commented 5 years ago

BTW, I would be ok if we say Julia 1.2 is the new min for Zygote. Hopefully it is not too far.

MikeInnes commented 5 years ago

@aviks are you able to try 1.2 with it? I'm hoping this is specific to 1.1, but don't have a copy handy to test this instant.

MikeInnes commented 5 years ago

I think there's a reasonable chance (though I'm not certain) that this is https://github.com/JuliaLang/julia/pull/31129, in which case there's not much to be done other than not using 1.1.

It's not ideal to put a hard version bound on 1.2, since several users want to use Zygote on 1.0 and upgrade gradually, but we can certainly make it clear that 1.1 and below are not officially supported.

MikeInnes commented 5 years ago

Just a note to self, but indeed this does reproduce for me on 1.1.