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

Specifying type for list comprehension sometimes crashes Julia #1344

Open
LilithHafner opened this issue Dec 25, 2022 · 4 comments
Open

Specifying type for list comprehension sometimes crashes Julia #1344

LilithHafner opened this issue Dec 25, 2022 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@LilithHafner
Copy link
Contributor

Specifying type for list comprehension sometimes causes an unexpected runtime error that sometimes crashes Julia. This is a heightened version of #1343 because it breaks Julia rather than throwing an error. Reproduction is a bit finicky (probably because the Julia runtime is broken) but I consistently get a dire error of some sort when I run this as the first thing in a REPL session after loading Zygote. The error is present on both 1.8.4 and 1.9.0-alpha1.

gradient(1) do x
    v = eltype(x)[i for i in x]
    only(v)
end
Full MWE:
x@X ~ % julia +alpha
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.9.0-alpha1 (2022-11-15)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(@v1.9) pkg> activate @Zygote
  Activating project at `~/.julia/environments/Zygote`

(@Zygote) pkg> status
Status `~/.julia/environments/Zygote/Project.toml`
  [e88e6eb3] Zygote v0.6.51

julia> using Zygote

julia> gradient(1) do x
           v = eltype(x)[i for i in x]
           only(v)
       end
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.VarState, (2,)}[Core.Compiler.VarState(typ=Zygote.Pullback{Tuple{typeof(Base.Iterators.only), Array{Int64, 0}}, Any}, undef=false), Core.Compiler.VarState(typ=Int64, undef=false)], i=(3,))
ijl_bounds_error_ints at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
setindex! at ./array.jl:969 [inlined]
stoverwrite1! at ./compiler/typelattice.jl:599
typeinf_local at ./compiler/abstractinterpretation.jl:2712
typeinf_nocycle at ./compiler/abstractinterpretation.jl:2811
_typeinf at ./compiler/typeinfer.jl:244
typeinf at ./compiler/typeinfer.jl:215
typeinf_ext at ./compiler/typeinfer.jl:1074
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1107
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1103
jfptr_typeinf_ext_toplevel_18850.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_type_infer at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_generate_fptr_impl at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
jl_compile_method_internal at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
Pullback at ./REPL[4]:3 [inlined]
Pullback at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
#60 at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
unknown function (ip: 0x1176ce6a5)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
gradient at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
unknown function (ip: 0x115bec549)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
do_call at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval_body at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
top-level scope at REPL[4]:1
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_toplevel_eval_in at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval at ./boot.jl:370 [inlined]
eval_user_input at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:152
repl_backend_loop at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:248
#start_repl_backend#46 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:233
start_repl_backend at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:230 [inlined]
#run_repl#59 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:376
run_repl at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:362
jfptr_run_repl_58109.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#1016 at ./client.jl:421
jfptr_YY.1016_37092.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_f__call_latest at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:816 [inlined]
invokelatest at ./essentials.jl:813 [inlined]
run_main_repl at ./client.jl:405
exec_options at ./client.jl:322
_start at ./client.jl:522
jfptr__start_48720.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
true_main at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_repl_entrypoint at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ERROR: Mutating arrays is not supported -- called setindex!(Array{Int64, 0}, ...)
This error occurs when you ask Zygote to differentiate operations that change
the elements of arrays in place (e.g. setting values with x .= ...)

Possible fixes:
- avoid mutating operations (preferred)
- or read the documentation and solutions for this error
  https://fluxml.ai/Zygote.jl/latest/limitations

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] _throw_mutation_error(f::Function, args::Array{Int64, 0})
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/lib/array.jl:86
 [3] (::Zygote.var"#389#390"{Array{Int64, 0}})(#unused#::Nothing)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/lib/array.jl:98
 [4] (::Zygote.var"#2509#back#391"{Zygote.var"#389#390"{Array{Int64, 0}}})(Δ::Nothing)
   @ Zygote ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67
 [5] Pullback
   @ ./REPL[4]:2 [inlined]
 [6] (::typeof((#3)))(Δ::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
 [7] (::Zygote.var"#60#61"{typeof((#3))})(Δ::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
 [8] gradient(f::Function, args::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
 [9] top-level scope
   @ REPL[4]:1

julia> 
x@X ~ % julia +alpha
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.9.0-alpha1 (2022-11-15)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(@v1.9) pkg> activate @Zygote
  Activating project at `~/.julia/environments/Zygote`

(@Zygote) pkg> status
Status `~/.julia/environments/Zygote/Project.toml`
  [e88e6eb3] Zygote v0.6.51

julia> using Zygote

julia> gradient(1) do x
           v = eltype(x)[i for i in x]
           only(v)
       end
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.VarState, (2,)}[Core.Compiler.VarState(typ=Zygote.Pullback{Tuple{typeof(Base.Iterators.only), Array{Int64, 0}}, Any}, undef=false), Core.Compiler.VarState(typ=Int64, undef=false)], i=(3,))
ijl_bounds_error_ints at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
setindex! at ./array.jl:969 [inlined]
stoverwrite1! at ./compiler/typelattice.jl:599
typeinf_local at ./compiler/abstractinterpretation.jl:2712
typeinf_nocycle at ./compiler/abstractinterpretation.jl:2811
_typeinf at ./compiler/typeinfer.jl:244
typeinf at ./compiler/typeinfer.jl:215
typeinf_ext at ./compiler/typeinfer.jl:1074
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1107
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1103
jfptr_typeinf_ext_toplevel_18850.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_type_infer at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_generate_fptr_impl at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
jl_compile_method_internal at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
Pullback at ./REPL[4]:3 [inlined]
Pullback at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
#60 at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
unknown function (ip: 0x11b0f0155)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
gradient at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
unknown function (ip: 0x11477efb9)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
do_call at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval_body at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
top-level scope at REPL[4]:1
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_toplevel_eval_in at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval at ./boot.jl:370 [inlined]
eval_user_input at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:152
repl_backend_loop at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:248
#start_repl_backend#46 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:233
start_repl_backend at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:230 [inlined]
#run_repl#59 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:376
run_repl at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:362
jfptr_run_repl_58109.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#1016 at ./client.jl:421
jfptr_YY.1016_37092.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_f__call_latest at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:816 [inlined]
invokelatest at ./essentials.jl:813 [inlined]
run_main_repl at ./client.jl:405
exec_options at ./client.jl:322
_start at ./client.jl:522
jfptr__start_48720.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
true_main at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_repl_entrypoint at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)

[31025] signal (11.1): Segmentation fault: 11
in expression starting at REPL[4]:1
_ZL18convert_julia_typeR12jl_codectx_tRK10jl_cgval_tP11_jl_value_tPPN4llvm5ValueE at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_ZL19emit_varinfo_assignR12jl_codectx_tR12jl_varinfo_t10jl_cgval_tP11_jl_value_t at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_ZL9emit_exprR12jl_codectx_tP11_jl_value_tl at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_ZL18emit_ssaval_assignR12jl_codectx_tlP11_jl_value_t at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_ZL13emit_functionRN4llvm3orc16ThreadSafeModuleEP21_jl_method_instance_tP15_jl_code_info_tP11_jl_value_tR20_jl_codegen_params_t at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_Z12jl_emit_codeRN4llvm3orc16ThreadSafeModuleEP21_jl_method_instance_tP15_jl_code_info_tP11_jl_value_tR20_jl_codegen_params_t at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_Z16jl_emit_codeinstRN4llvm3orc16ThreadSafeModuleEP19_jl_code_instance_tP15_jl_code_info_tR20_jl_codegen_params_t at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
_ZL20_jl_compile_codeinstP19_jl_code_instance_tP15_jl_code_info_tmN4llvm3orc17ThreadSafeContextE at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
jl_generate_fptr_for_unspecialized_impl at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.9.dylib (unknown line)
jl_compile_method_internal at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
Pullback at ./REPL[4]:3 [inlined]
Pullback at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
#60 at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
unknown function (ip: 0x11b0f0155)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
gradient at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
unknown function (ip: 0x11477efb9)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
do_call at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval_body at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_toplevel_eval_in at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
eval at ./boot.jl:370 [inlined]
eval_user_input at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:152
repl_backend_loop at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:248
#start_repl_backend#46 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:233
start_repl_backend at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:230 [inlined]
#run_repl#59 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:376
run_repl at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-2.0/build/default-macmini-x64-2-0/julialang/julia-release-1-dot-9/usr/share/julia/stdlib/v1.9/REPL/src/REPL.jl:362
jfptr_run_repl_58109.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#1016 at ./client.jl:421
jfptr_YY.1016_37092.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_f__call_latest at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:816 [inlined]
invokelatest at ./essentials.jl:813 [inlined]
run_main_repl at ./client.jl:405
exec_options at ./client.jl:322
_start at ./client.jl:522
jfptr__start_48720.clone_1 at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
true_main at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_repl_entrypoint at /Users/x/.julia/juliaup/julia-1.9.0-alpha1+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
Allocations: 37340056 (Pool: 37320020; Big: 20036); GC: 48
x@X ~ % julia       
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.8.4 (2022-12-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(@v1.8) pkg> activate --temp
  Activating new project at `/var/folders/hc/fn82kz1j5vl8w7lwd4l079y80000gn/T/jl_SxFs8t`

julia> using Zygote
 │ Package Zygote not found, but a package named Zygote is available from a registry. 
 │ Install package?
 │   (jl_SxFs8t) pkg> add Zygote 
 └ (y/n/o) [y]: y
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `/private/var/folders/hc/fn82kz1j5vl8w7lwd4l079y80000gn/T/jl_SxFs8t/Project.toml`
  [e88e6eb3] + Zygote v0.6.51
    Updating `/private/var/folders/hc/fn82kz1j5vl8w7lwd4l079y80000gn/T/jl_SxFs8t/Manifest.toml`
  [621f4979] + AbstractFFTs v1.2.1
  [79e6a3ab] + Adapt v3.4.0
  [fa961155] + CEnum v0.4.2
  [082447d4] + ChainRules v1.46.0
  [d360d2e6] + ChainRulesCore v1.15.6
  [9e997f8a] + ChangesOfVariables v0.1.4
  [bbf7d656] + CommonSubexpressions v0.3.0
  [34da2185] + Compat v4.5.0
  [9a962f9c] + DataAPI v1.14.0
  [e2d170a0] + DataValueInterfaces v1.0.0
  [163ba53b] + DiffResults v1.1.0
  [b552c78f] + DiffRules v1.12.2
  [ffbed154] + DocStringExtensions v0.9.3
  [1a297f60] + FillArrays v0.13.6
  [f6369f11] + ForwardDiff v0.10.34
  [0c68f7d7] + GPUArrays v8.5.0
  [46192b85] + GPUArraysCore v0.1.2
  [7869d1d1] + IRTools v0.4.7
  [3587e190] + InverseFunctions v0.1.8
  [92d709cd] + IrrationalConstants v0.1.1
  [82899510] + IteratorInterfaceExtensions v1.0.0
  [692b3bcd] + JLLWrappers v1.4.1
  [929cbde3] + LLVM v4.14.1
  [2ab3a3ac] + LogExpFunctions v0.3.19
  [1914dd2f] + MacroTools v0.5.10
  [77ba4419] + NaNMath v1.0.1
  [bac558e1] + OrderedCollections v1.4.1
  [21216c6a] + Preferences v1.3.0
  [c1ae055f] + RealDot v0.1.0
  [189a3867] + Reexport v1.2.2
  [ae029012] + Requires v1.3.0
  [276daf66] + SpecialFunctions v2.1.7
  [90137ffa] + StaticArrays v1.5.11
  [1e83bf80] + StaticArraysCore v1.4.0
  [09ab397b] + StructArrays v0.6.14
  [3783bdb8] + TableTraits v1.0.1
  [bd369af6] + Tables v1.10.0
  [e88e6eb3] + Zygote v0.6.51
  [700de1a5] + ZygoteRules v0.2.2
  [dad2f222] + LLVMExtra_jll v0.0.16+0
  [efe28fd5] + OpenSpecFun_jll v0.5.5+0
  [0dad84c5] + ArgTools v1.1.1
  [56f22d72] + Artifacts
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [8ba89e20] + Distributed
  [f43a241f] + Downloads v1.6.0
  [7b1f6079] + FileWatching
  [b77e0a4c] + InteractiveUtils
  [4af54fe1] + LazyArtifacts
  [b27032c2] + LibCURL v0.6.3
  [76f85450] + LibGit2
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [ca575930] + NetworkOptions v1.2.0
  [44cfe95a] + Pkg v1.8.0
  [de0858da] + Printf
  [3fa0cd96] + REPL
  [9a3f8284] + Random
  [ea8e919c] + SHA v0.7.0
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [fa267f1f] + TOML v1.0.0
  [a4e569a6] + Tar v1.10.1
  [8dfed614] + Test
  [cf7118a7] + UUIDs
  [4ec0a83e] + Unicode
  [e66e0078] + CompilerSupportLibraries_jll v1.0.1+0
  [deac9b47] + LibCURL_jll v7.84.0+0
  [29816b5a] + LibSSH2_jll v1.10.2+0
  [c8ffd9c3] + MbedTLS_jll v2.28.0+0
  [14a3606d] + MozillaCACerts_jll v2022.2.1
  [4536629a] + OpenBLAS_jll v0.3.20+0
  [05823500] + OpenLibm_jll v0.8.1+0
  [83775a58] + Zlib_jll v1.2.12+3
  [8e850b90] + libblastrampoline_jll v5.1.1+0
  [8e850ede] + nghttp2_jll v1.48.0+0
  [3f19e933] + p7zip_jll v17.4.0+0
Precompiling project...
  2 dependencies successfully precompiled in 17 seconds. 46 already precompiled.

julia> gradient(1) do x
           v = eltype(x)[i for i in x]
           only(v)
       end
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.VarState, (2,)}[Core.Compiler.VarState(typ=Zygote.Pullback{Tuple{typeof(Base.Iterators.only), Array{Int64, 0}}, Any}, undef=false), Core.Compiler.VarState(typ=Int64, undef=false)], i=(3,))
ijl_bounds_error_ints at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
setindex! at ./array.jl:966 [inlined]
stupdate! at ./compiler/typelattice.jl:380
typeinf_local at ./compiler/abstractinterpretation.jl:2424
typeinf_nocycle at ./compiler/abstractinterpretation.jl:2482
_typeinf at ./compiler/typeinfer.jl:230
typeinf at ./compiler/typeinfer.jl:213
typeinf_ext at ./compiler/typeinfer.jl:967
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1000
typeinf_ext_toplevel at ./compiler/typeinfer.jl:996
jfptr_typeinf_ext_toplevel_13895.clone_1 at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_type_infer at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_generate_fptr_impl at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.8.dylib (unknown line)
jl_compile_method_internal at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
Pullback at ./REPL[3]:3 [inlined]
Pullback at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
#60 at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
unknown function (ip: 0x11bf4dc65)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
gradient at /Users/x/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
unknown function (ip: 0x11a1e8f69)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_call at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval_body at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
top-level scope at REPL[3]:1
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_toplevel_eval_flex at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_toplevel_eval_in at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval at ./boot.jl:368 [inlined]
eval_user_input at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-4.0/build/default-macmini-x64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/REPL/src/REPL.jl:151
repl_backend_loop at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-4.0/build/default-macmini-x64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/REPL/src/REPL.jl:247
start_repl_backend at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-4.0/build/default-macmini-x64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/REPL/src/REPL.jl:232
#run_repl#47 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-4.0/build/default-macmini-x64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/REPL/src/REPL.jl:369
run_repl at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-4.0/build/default-macmini-x64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/REPL/src/REPL.jl:355
jfptr_run_repl_65470.clone_1 at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#967 at ./client.jl:419
jfptr_YY.967_47614.clone_1 at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f__call_latest at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:729 [inlined]
invokelatest at ./essentials.jl:726 [inlined]
run_main_repl at ./client.jl:404
exec_options at ./client.jl:318
_start at ./client.jl:522
jfptr__start_28650.clone_1 at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
true_main at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_repl_entrypoint at /Users/x/.julia/juliaup/julia-1.8.4+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ERROR: Mutating arrays is not supported -- called setindex!(Array{Int64, 0}, ...)
This error occurs when you ask Zygote to differentiate operations that change
the elements of arrays in place (e.g. setting values with x .= ...)

Possible fixes:
- avoid mutating operations (preferred)
- or read the documentation and solutions for this error
  https://fluxml.ai/Zygote.jl/latest/limitations

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] _throw_mutation_error(f::Function, args::Array{Int64, 0})
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/lib/array.jl:86
 [3] (::Zygote.var"#389#390"{Array{Int64, 0}})(#unused#::Nothing)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/lib/array.jl:98
 [4] (::Zygote.var"#2488#back#391"{Zygote.var"#389#390"{Array{Int64, 0}}})(Δ::Nothing)
   @ Zygote ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67
 [5] Pullback
   @ ./REPL[3]:2 [inlined]
 [6] (::typeof((#1)))(Δ::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface2.jl:0
 [7] (::Zygote.var"#60#61"{typeof((#1))})(Δ::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:45
 [8] gradient(f::Function, args::Int64)
   @ Zygote ~/.julia/packages/Zygote/SmJK6/src/compiler/interface.jl:97
 [9] top-level scope
   @ REPL[3]:1

julia> 
@ToucheSir
Copy link
Member

I've never seen this kind of non-fatal type inference error before, interesting. Passing [1] instead of 1 works fine, as does just running the primal/forward pass. It's the pullback that's problematic somehow, but poking around with Cthulhu doesn't turn up any smoking guns. Confusingly, inspecting both forward and backward passes separately with Cthulhu works, but it throws an error on the gradient function which combines both. Therefore, I'd say this issue needs a pair of eyes from someone with deeper compiler knowledge than you'll likely find here.

@ToucheSir ToucheSir added bug Something isn't working help wanted Extra attention is needed labels Dec 27, 2022
@mcabbott
Copy link
Member

Note that the crash is avoided if you say v[1] instead of only.

And that a similar-looking error can be produced as follows, after which the above example no longer crashes Julia:

julia> gradient(only, fill(1))
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.VarState, (2,)}[Core.Compiler.VarState(typ=Zygote.Pullback{Tuple{typeof(Base.Iterators.only), Array{Int64, 0}}, Any}, undef=false), Core.Compiler.VarState(typ=Int64, undef=false)], i=(3,))
...
(nothing,)

julia> gradient(1) do x
           v = eltype(x)[i for i in x]
           only(v)
       end
ERROR: Mutating arrays is not supported -- called setindex!(Array{Int64, 0}, ...)

@ToucheSir
Copy link
Member

reducing a bit:

julia> _, back = pullback(only, fill(1))
(1, Zygote.var"#60#61"{typeof((only))}((only)))

julia> @code_lowered back.back(1.)
CodeInfo(
1$(Expr(:meta, :inline))
│   %2  = Base.getfield(#self#, :t)%3  = Base.getindex(%2, 2)
│   %4  = Base.getindex(%2, 1)
│   %5  = %3 !== 0x01
└──       goto #4 if not %5
2 ─       goto #3
3 ─       phi_1_1 = nothing
└──       goto #1
    @ iterators.jl:1425 within `Pullback`
4%10 = (%4)(nothing)
│   %11 = Zygote.gradindex(%10, 2)
│   %12 = Zygote.tuple(nothing, %11)
└──       return %12
)

julia> @code_warntype back.back(1.)
ERROR: BoundsError: attempt to access 2-element Vector{Core.Compiler.VarState} at index [3]
Stacktrace:
  [1] setindex!
    @ ./array.jl:966 [inlined]
  [2] stupdate!(state::Nothing, changes::Core.Compiler.StateUpdate)
    @ Core.Compiler ./compiler/typelattice.jl:380
  [3] typeinf_local(interp::Core.Compiler.NativeInterpreter, frame::Core.Compiler.InferenceState)
    @ Core.Compiler ./compiler/abstractinterpretation.jl:2424
  [4] typeinf_nocycle(interp::Core.Compiler.NativeInterpreter, frame::Core.Compiler.InferenceState)
    @ Core.Compiler ./compiler/abstractinterpretation.jl:2482
  [5] _typeinf(interp::Core.Compiler.NativeInterpreter, frame::Core.Compiler.InferenceState)
    @ Core.Compiler ./compiler/typeinfer.jl:230
  [6] typeinf(interp::Core.Compiler.NativeInterpreter, frame::Core.Compiler.InferenceState)
    @ Core.Compiler ./compiler/typeinfer.jl:213
  [7] typeinf_frame(interp::Core.Compiler.NativeInterpreter, method::Method, atype::Any, sparams::Core.SimpleVector, run_optimizer::Bool)
    @ Core.Compiler ./compiler/typeinfer.jl:909
  [8] typeinf_code(interp::Core.Compiler.NativeInterpreter, method::Method, atype::Any, sparams::Core.SimpleVector, run_optimizer::Bool)
    @ Core.Compiler ./compiler/typeinfer.jl:894
  [9] code_typed_by_type(tt::Type; optimize::Bool, debuginfo::Symbol, world::UInt64, interp::Core.Compiler.NativeInterpreter)
    @ Base ./reflection.jl:1280
 [10] code_typed(f::Any, types::Any; optimize::Bool, debuginfo::Symbol, world::UInt64, interp::Core.Compiler.NativeInterpreter)
    @ Base ./reflection.jl:1239
 [11] code_warntype(io::Base.TTY, f::Any, t::Any; debuginfo::Symbol, optimize::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ InteractiveUtils ~/.julia/juliaup/julia-1.8.4+0.x64.linux.gnu/share/julia/stdlib/v1.8/InteractiveUtils/src/codeview.jl:64
 [12] code_warntype(io::Base.TTY, f::Any, t::Any)
    @ InteractiveUtils ~/.julia/juliaup/julia-1.8.4+0.x64.linux.gnu/share/julia/stdlib/v1.8/InteractiveUtils/src/codeview.jl:60
 [13] code_warntype(f::Any, t::Any; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ InteractiveUtils ~/.julia/juliaup/julia-1.8.4+0.x64.linux.gnu/share/julia/stdlib/v1.8/InteractiveUtils/src/codeview.jl:142
 [14] code_warntype(f::Any, t::Any)
    @ InteractiveUtils ~/.julia/juliaup/julia-1.8.4+0.x64.linux.gnu/share/julia/stdlib/v1.8/InteractiveUtils/src/codeview.jl:142
 [15] top-level scope
    @ REPL[4]:1

@trahflow
Copy link
Contributor

As a workaround, and maybe a further hint, using v[] instead of only(v) works.
MWE:

using LinearAlgebra, Zygote

struct NoZeroArray
    a::Vector{Float64}
    b::Vector{Float64}
end

struct HasZeroArray
    a::Vector{Float64}
    b::Array{Float64, 0}
end

loss(h) = dot(h.a, h.a) + only(h.b)  # crashes for 0-dim Array, works for 1-element-Vector

loss2(h) = dot(h.a, h.a) + h.b[]  # works always

hz = HasZeroArray([1., 2., 3., 4., 5.], fill(3.8))
nz = NoZeroArray([1., 2., 3., 4., 5.], [3.8])

(grad,) = Zygote.gradient(loss, nz)  # works
(grad,) = Zygote.gradient(loss, hz)  # crashes

(grad,) = Zygote.gradient(loss2, nz)  # works
(grad,) = Zygote.gradient(loss2, hz)  # works

also:

julia> _, back = pullback(v -> v[], fill(1.0))
(1.0, Zygote.var"#75#76"{Zygote.Pullback{Tuple{var"#7#8", Array{Float64, 0}}, Tuple{Zygote.var"#2610#back#529"{Zygote.var"#539#541"{0, Float64, Array{Float64, 0}, Tuple{}}}}}}((#7)))

julia> @code_lowered back.back(1.)
CodeInfo(
1$(Expr(:meta, :inline))
│   %2 = Base.getfield(#self#, :t)%3 = Base.getindex(%2, 1)
│   @ REPL[5]:1 within `Pullback`%4 = (%3)(Δ)
│   %5 = Zygote.gradindex(%4, 2)
│   %6 = Zygote.tuple(nothing, %5)
└──      return %6
)

julia> @code_warntype back.back(1.)
MethodInstance for (::Zygote.Pullback{Tuple{var"#7#8", Array{Float64, 0}}, Tuple{Zygote.var"#2610#back#529"{Zygote.var"#539#541"{0, Float64, Array{Float64, 0}, Tuple{}}}}})(::Float64)
  from (j::Zygote.Pullback)(Δ) @ Zygote ~/.julia/packages/Zygote/JeHtr/src/compiler/interface2.jl:105
Arguments
  #self#::Zygote.Pullback{Tuple{var"#7#8", Array{Float64, 0}}, Tuple{Zygote.var"#2610#back#529"{Zygote.var"#539#541"{0, Float64, Array{Float64, 0}, Tuple{}}}}}
  Δ::Float64
Body::Tuple{Nothing, Zygote.OneElement{Float64, 0, Tuple{}, Tuple{}}}
1$(Expr(:meta, :inline))
│   %2 = Base.getfield(#self#, :t)::Tuple{Zygote.var"#2610#back#529"{Zygote.var"#539#541"{0, Float64, Array{Float64, 0}, Tuple{}}}}%3 = Base.getindex(%2, 1)::Zygote.var"#2610#back#529"{Zygote.var"#539#541"{0, Float64, Array{Float64, 0}, Tuple{}}}
│   %4 = (%3)(Δ)::Tuple{Nothing, Zygote.OneElement{Float64, 0, Tuple{}, Tuple{}}}%5 = Zygote.gradindex(%4, 2)::Zygote.OneElement{Float64, 0, Tuple{}, Tuple{}}%6 = Zygote.tuple(nothing, %5)::Tuple{Nothing, Zygote.OneElement{Float64, 0, Tuple{}, Tuple{}}}
└──      return %6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants