FluxML/Zygote.jl

Error with control flow

Open

#1236 aperta il 3 giu 2022

Vedi su GitHub
 (4 commenti) (0 reazioni) (0 assegnatari)Julia (220 fork)batch import
bughelp wanted

Metriche repository

Star
 (1568 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

We've come across a strange bug involving control flow:

julia> using FiniteDifferences

julia> using Zygote

julia> function f(x)
         y = [[x]', [x]]
         r = 0.0
         o = 1.0
         for n in 1:2
           o *= y[n]
           if n < 2
             proj_o = o * [1.0]
           else
             # Error
             proj_o = o
             # Fix
             # proj_o = o * 1.0
           end
           r += proj_o
         end
         return r
       end
f (generic function with 1 method)

julia> x = 1.2
1.2

julia> f(x)
2.6399999999999997

julia> central_fdm(5, 1)(f, x)
3.4000000000000967

julia> f'(x)
ERROR: MethodError: no method matching +(::Float64, ::LinearAlgebra.Adjoint{Float64, Vector{Float64}})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at ~/software/julia-1.7.3/share/julia/base/operators.jl:655
  +(::Union{Float16, Float32, Float64}, ::BigFloat) at ~/software/julia-1.7.3/share/julia/base/mpfr.jl:413
  +(::ChainRulesCore.Tangent{P}, ::P) where P at ~/.julia/packages/ChainRulesCore/GUvJT/src/tangent_arithmetic.jl:146
  ...
Stacktrace:
 [1] accum(x::Float64, y::LinearAlgebra.Adjoint{Float64, Vector{Float64}})
   @ Zygote ~/.julia/packages/Zygote/DkIUK/src/lib/lib.jl:17
 [2] Pullback
   @ ./REPL[16]:15 [inlined]
 [3] (::typeof(∂(f)))(Δ::Float64)
   @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface2.jl:0
 [4] (::Zygote.var"#52#53"{typeof(∂(f))})(Δ::Float64)
   @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface.jl:41
 [5] (::Zygote.var"#54#55"{typeof(f)})(x::Float64)
   @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface.jl:83
 [6] top-level scope
   @ REPL[20]:1

Changing the line:

proj_o = o

to:

proj_o = o * 1.0

fixes the issue and outputs:

julia> f(x)
2.6399999999999997

julia> central_fdm(5, 1)(f, x)
3.4000000000000967

julia> f'(x)
3.4000000000000004

Version information:

julia> versioninfo()
Julia Version 1.7.3
Commit 742b9abb4d (2022-05-06 12:58 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) E-2176M  CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = vim

julia> using Pkg

julia> Pkg.status("Zygote")
      Status `~/.julia/environments/v1.7/Project.toml`
  [e88e6eb3] Zygote v0.6.40

julia> Pkg.status("ChainRules")
      Status `~/.julia/environments/v1.7/Project.toml`
  [082447d4] ChainRules v1.35.1

Original issue is here: https://github.com/ITensor/ITensorMPS.jl/issues/73

Guida contributor