FluxML / Zygote.jl

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

Compatibility with logging #386

Open xukai92 opened 4 years ago

xukai92 commented 4 years ago

MWE

julia> using Zygote

julia> function f(x)
       @info x
       2 * x
       end
f (generic function with 1 method)

julia> f'(1.0)
[ Info: 1.0
ERROR: Compiling Tuple{typeof(f),Float64}: try/catch is not supported.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] instrument(::IRTools.Inner.IR) at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/reverse.jl:88
 [3] #Primal#6 at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/reverse.jl:158 [inlined]
 [4] Type at ./none:0 [inlined]
 [5] #Adjoint#22(::Nothing, ::Bool, ::Type{Zygote.Adjoint}, ::IRTools.Inner.IR) at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/reverse.jl:271
 [6] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs, :normalise),Tuple{Nothing,Bool}}, ::Type{Zygote.Adjoint}, ::IRTools.Inner.IR) at ./none:0
 [7] _lookup_grad(::Type) at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/emit.jl:101
 [8] #s2641#1468 at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/interface2.jl:21 [inlined]
 [9] #s2641#1468(::Any, ::Any, ::Any) at ./none:0
 [10] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:524
 [11] (::getfield(Zygote, Symbol("##28#29")){typeof(∂(f))})(::Float64) at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/interface.jl:38
 [12] gradient at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/interface.jl:47 [inlined]
 [13] (::getfield(Zygote, Symbol("##30#31")){typeof(f)})(::Float64) at /Users/kai/.julia/packages/Zygote/9Mec5/src/compiler/interface.jl:50
 [14] top-level scope at REPL[3]:1
baggepinnen commented 4 years ago

Duplicate of https://github.com/FluxML/Zygote.jl/issues/269

MikeInnes commented 4 years ago

This is hard because it immediately expands into try/catch, so we can't just nograd it.

One option is to split out a new function containing the @info and @nograd that. Another is to just fix exception support in Zygote, which we're probably past due on.

xukai92 commented 4 years ago

One option is to split out a new function containing the @info and @nograd that. Another is to just fix exception support in Zygote, which we're probably past due on.

This is my current hack. Just a bit annoying one has to do this when using e.g. TensorBoardLogger as one of the advantage is you can simply do @info...