Skip to content

PresetTimeCallback errors with Zygote differentiation #502

Closed
@mfalt

Description

@mfalt

Trying to solve a problem similar to
https://diffeqflux.sciml.ai/dev/examples/hybrid_diffeq/
seems to create some problems. The example above runs as it is, but creating the callback inside predict_n_ode results in error, e.g.:

function predict_n_ode()
    _prob = remake(prob,p=p)
    cb = PresetTimeCallback(dosetimes,affect!,save_positions=(false,false))
    Array(solve(_prob,Tsit5(),u0=z0,p=p,callback=cb,saveat=t,sensealg=ReverseDiffAdjoint()))[1:2,:]
end

I need my callback to depend on the training data, so it would be good to create the affect! function on the fly. It's probably possible to get around using global variables, but I would prefer not.

ERROR: LoadError: Mutating arrays is not supported
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] (::Zygote.var"#372#373")(#unused#::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/lib/array.jl:58
  [3] (::Zygote.var"#2249#back#374"{Zygote.var"#372#373"})(Δ::Nothing)
    @ Zygote ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
  [4] Pullback
    @ ./bitarray.jl:344 [inlined]
  [5] (::typeof((copy_to_bitarray_chunks!)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
  [6] Pullback
    @ ./bitarray.jl:534 [inlined]
  [7] (::typeof((BitVector)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
  [8] Pullback
    @ ./bitarray.jl:503 [inlined]
  [9] (::typeof((BitArray)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [10] Pullback
    @ ~/.julia/packages/DiffEqBase/krS36/src/callbacks.jl:282 [inlined]
 [11] (::typeof((DiscreteCallback)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [12] Pullback
    @ ~/.julia/packages/DiffEqBase/krS36/src/callbacks.jl:288 [inlined]
 [13] (::typeof((#DiscreteCallback#21)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [14] Pullback
    @ ~/.julia/packages/DiffEqBase/krS36/src/callbacks.jl:288 [inlined]
 [15] (::typeof((Type##kw)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [16] Pullback
    @ ~/.julia/packages/DiffEqCallbacks/3xzvj/src/preset_time.jl:29 [inlined]
 [17] (::typeof((#PresetTimeCallback#60)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [18] Pullback
    @ ~/.julia/packages/DiffEqCallbacks/3xzvj/src/preset_time.jl:5 [inlined]
 [19] (::typeof((PresetTimeCallback##kw)))(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [20] Pullback
    @ /work/mattiasf/nn-quad/test.jl:34 [inlined]
 [21] (::typeof((predict_n_ode)))(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [22] Pullback
    @ /work/mattiasf/nn-quad/test.jl:40 [inlined]
 [23] (::typeof((loss_n_ode)))(Δ::Float32)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface2.jl:0
 [24] #151
    @ ~/.julia/packages/Zygote/KpME9/src/lib/lib.jl:191 [inlined]
 [25] #1682#back
    @ ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59 [inlined]
 [26] Pullback
    @ ~/.julia/packages/Flux/goUGu/src/optimise/train.jl:103 [inlined]
 [27] (::Zygote.var"#54#55"{Zygote.Params, typeof((#15)), Zygote.Context})(Δ::Float32)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface.jl:172
 [28] gradient(f::Function, args::Zygote.Params)
    @ Zygote ~/.julia/packages/Zygote/KpME9/src/compiler/interface.jl:49
 [29] macro expansion
    @ ~/.julia/packages/Flux/goUGu/src/optimise/train.jl:102 [inlined]
 [30] macro expansion
    @ ~/.julia/packages/Juno/n6wyj/src/progress.jl:134 [inlined]
 [31] train!(loss::Function, ps::Zygote.Params, data::Base.Iterators.Take{Base.Iterators.Repeated{Tuple{}}}, opt::ADAM; cb::var"#310#312")
    @ Flux.Optimise ~/.julia/packages/Flux/goUGu/src/optimise/train.jl:100
 [32] top-level scope

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions