div(::UnitRange{Int64}... error

@ChrisRackauckas
I get the error:

ERROR: LoadError: MethodError: no method matching div(::UnitRange{Int64}, ::Int64, ::RoundingMode{:ToZero})

that I cannot figure out. This occurred while I was producing a MWE related to some other error related to SetIndex!. I realize some of my constructs might look strange, but that is because I tried to keep the structure of the code as close as possible to the original code producing the error (which was not this one). Once this error is fixed (which might be related to the real cause of my issues), I can retrace back to my original code slowly. Julia 1.8.5 . Any insight is appreciated. I have already spent a few days on this.

I show the code followed by the error trace:

using Optim, DifferentialEquations
using Optimization, OptimizationOptimisers, OptimizationOptimJL
using DiffEqFlux, Zygote


function dudt_univ_opt(u, p, t)
    du = [0.]
end

function single_run()
    tspan = (0.0f0, 1.0f0)
    σ0 = [0.]

    p_system = Float32[0., 0.]
    θi = [0.]

    loss_fn(θ) = loss_univ([θ; p_system], tspan, σ0)
    adtype = Optimization.AutoZygote()
    optf = Optimization.OptimizationFunction((x,p)->loss_fn(x),  adtype)
    optprob = Optimization.OptimizationProblem(optf, θi)
    parameter_res = Optimization.solve(optprob, Optimisers.AMSGrad(), sensealg=ReverseDiffVJP(true), allow_f_increases=false, maxiters=20)
end

function ensemble_solve(θ, ensemble, tspans, σ0)
    θ = [1.]
    prob = ODEProblem(dudt_univ_opt, σ0, tspans[1], θ)

    function prob_func(prob, i, repeat)
        remake(prob, f=dudt_univ_opt, tspan=tspans[i])
    end

    ensemble_prob = EnsembleProblem(prob, prob_func=prob_func)
    sim = solve(ensemble_prob, Tsit5(), ensemble, trajectories=1:1)
end

function loss_univ(θ, tspans, σ0)
    loss = 0.
    results = ensemble_solve(θ, EnsembleThreads(), tspans, σ0)
    return loss
end

single_run()

Here is the error trace produced:

ERROR: LoadError: MethodError: no method matching div(::UnitRange{Int64}, ::Int64, ::RoundingMode{:ToZero})

Some of the types have been truncated in the stacktrace for improved reading. To emit complete information
in the stack trace, evaluate `TruncatedStacktraces.VERBOSE[] = true` and re-run the code.

Closest candidates are:
  div(::Union{Static.StaticBool{X}, Static.StaticFloat64{X}, Static.StaticInt{X}}, ::Real, ::RoundingMode) where X at ~/.julia/packages/Static/tQ45Q/src/Static.jl:440
  div(::MultivariatePolynomials.AbstractPolynomialLike, ::Number, ::Any...) at ~/.julia/packages/MultivariatePolynomials/sWAOE/src/division.jl:10
  div(::P, ::Real, ::RoundingMode) where P<:Dates.Period at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/Dates/src/periods.jl:83
  ...
Stacktrace:
  [1] div(a::UnitRange{Int64}, b::Int64)
    @ Base ./div.jl:40
  [2] rrule(#unused#::typeof(div), 764::UnitRange{Int64}, 765::Int64)
    @ ChainRules ~/.julia/packages/ChainRules/bEtjZ/src/rulesets/Base/nondiff.jl:139
  [3] rrule(::Zygote.ZygoteRuleConfig{Zygote.Context{false}}, ::Function, ::UnitRange{Int64}, ::Int64)
    @ ChainRulesCore ~/.julia/packages/ChainRulesCore/a4mIA/src/rules.jl:134
  [4] chain_rrule
    @ ~/.julia/packages/Zygote/g2w9o/src/compiler/chainrules.jl:223 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0 [inlined]
  [6] _pullback(::Zygote.Context{false}, ::typeof(div), ::UnitRange{Int64}, ::Int64)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:9
  [7] _pullback
    @ ~/.julia/packages/SciMLBase/gTrkJ/src/ensemble/basic_ensemble_solve.jl:45 [inlined]
  [8] _pullback(::Zygote.Context{false}, ::SciMLBase.var"#__solve##kw", ::NamedTuple{(:trajectories,), Tuple{UnitRange{Int64}}}, ::typeof(SciMLBase.__solve), ::EnsembleProblem{ODEProblem{false,Vector{Float64},Tuple{Float32, Float32},…}, var"#prob_func#4"{Tuple{Float32, Float32}}, typeof(SciMLBase.DEFAULT_OUTPUT_FUNC), typeof(SciMLBase.DEFAULT_REDUCTION), Nothing}, ::Tsit5{Static.False,…}, ::EnsembleThreads)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
  [9] _apply(::Function, ::Vararg{Any})
    @ Core ./boot.jl:816
 [10] adjoint
    @ ~/.julia/packages/Zygote/g2w9o/src/lib/lib.jl:203 [inlined]
 [11] _pullback
    @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:65 [inlined]
 [12] _pullback
    @ ~/.julia/packages/DiffEqBase/JH4gt/src/solve.jl:956 [inlined]
 [13] _pullback(::Zygote.Context{false}, ::DiffEqBase.var"##solve#31", ::Base.Pairs{Symbol, UnitRange{Int64}, Tuple{Symbol}, NamedTuple{(:trajectories,), Tuple{UnitRange{Int64}}}}, ::typeof(solve), ::EnsembleProblem{ODEProblem{false,Vector{Float64},Tuple{Float32, Float32},…}, var"#prob_func#4"{Tuple{Float32, Float32}}, typeof(SciMLBase.DEFAULT_OUTPUT_FUNC), typeof(SciMLBase.DEFAULT_REDUCTION), Nothing}, ::Tsit5{Static.False,…}, ::EnsembleThreads)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [14] _apply(::Function, ::Vararg{Any})
    @ Core ./boot.jl:816
 [15] adjoint
    @ ~/.julia/packages/Zygote/g2w9o/src/lib/lib.jl:203 [inlined]
 [16] _pullback
    @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:65 [inlined]
 [17] _pullback
    @ ~/.julia/packages/DiffEqBase/JH4gt/src/solve.jl:952 [inlined]
 [18] _pullback(::Zygote.Context{false}, ::CommonSolve.var"#solve##kw", ::NamedTuple{(:trajectories,), Tuple{UnitRange{Int64}}}, ::typeof(solve), ::EnsembleProblem{ODEProblem{false,Vector{Float64},Tuple{Float32, Float32},…}, var"#prob_func#4"{Tuple{Float32, Float32}}, typeof(SciMLBase.DEFAULT_OUTPUT_FUNC), typeof(SciMLBase.DEFAULT_REDUCTION), Nothing}, ::Tsit5{Static.False,…}, ::EnsembleThreads)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [19] _pullback
    @ ~/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:33 [inlined]
 [20] _pullback(::Zygote.Context{false}, ::typeof(ensemble_solve), ::Vector{Float64}, ::EnsembleThreads, ::Tuple{Float32, Float32}, ::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [21] _pullback
    @ ~/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:38 [inlined]
 [22] _pullback(::Zygote.Context{false}, ::typeof(loss_univ), ::Vector{Float64}, ::Tuple{Float32, Float32}, ::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [23] _pullback
    @ ~/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:17 [inlined]
 [24] _pullback(ctx::Zygote.Context{false}, f::var"#loss_fn#2"{Vector{Float32}, Vector{Float64}, Tuple{Float32, Float32}}, args::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [25] _pullback
    @ ~/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:19 [inlined]
 [26] _pullback(::Zygote.Context{false}, ::var"#1#3"{var"#loss_fn#2"{Vector{Float32}, Vector{Float64}, Tuple{Float32, Float32}}}, ::Vector{Float64}, ::SciMLBase.NullParameters)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [27] _apply
    @ ./boot.jl:816 [inlined]
 [28] adjoint
    @ ~/.julia/packages/Zygote/g2w9o/src/lib/lib.jl:203 [inlined]
 [29] _pullback
    @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:65 [inlined]
 [30] _pullback
    @ ~/.julia/packages/SciMLBase/gTrkJ/src/scimlfunctions.jl:3904 [inlined]
 [31] _pullback(::Zygote.Context{false}, ::OptimizationFunction{true,Optimization.AutoZygote,…}, ::Vector{Float64}, ::SciMLBase.NullParameters)
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [32] _apply(::Function, ::Vararg{Any})
    @ Core ./boot.jl:816
 [33] adjoint
    @ ~/.julia/packages/Zygote/g2w9o/src/lib/lib.jl:203 [inlined]
 [34] _pullback
    @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:65 [inlined]
 [35] _pullback
    @ ~/.julia/packages/Optimization/XjqVZ/src/function/zygote.jl:30 [inlined]
 [36] _pullback(ctx::Zygote.Context{false}, f::Optimization.var"#137#146"{OptimizationFunction{true,Optimization.AutoZygote,…}, SciMLBase.NullParameters}, args::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [37] _apply(::Function, ::Vararg{Any})
    @ Core ./boot.jl:816
 [38] adjoint
    @ ~/.julia/packages/Zygote/g2w9o/src/lib/lib.jl:203 [inlined]
 [39] _pullback
    @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:65 [inlined]
 [40] _pullback
    @ ~/.julia/packages/Optimization/XjqVZ/src/function/zygote.jl:34 [inlined]
 [41] _pullback(ctx::Zygote.Context{false}, f::Optimization.var"#139#148"{Tuple{}, Optimization.var"#137#146"{OptimizationFunction{true,Optimization.AutoZygote,…}, SciMLBase.NullParameters}}, args::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface2.jl:0
 [42] pullback(f::Function, cx::Zygote.Context{false}, args::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface.jl:44
 [43] pullback
    @ ~/.julia/packages/Zygote/g2w9o/src/compiler/interface.jl:42 [inlined]
 [44] gradient(f::Function, args::Vector{Float64})
    @ Zygote ~/.julia/packages/Zygote/g2w9o/src/compiler/interface.jl:96
 [45] (::Optimization.var"#138#147"{Optimization.var"#137#146"{OptimizationFunction{true,Optimization.AutoZygote,…}, SciMLBase.NullParameters}})(::Vector{Float64}, ::Vector{Float64})
    @ Optimization ~/.julia/packages/Optimization/XjqVZ/src/function/zygote.jl:32
 [46] macro expansion
    @ ~/.julia/packages/OptimizationOptimisers/KGKWE/src/OptimizationOptimisers.jl:36 [inlined]
 [47] macro expansion
    @ ~/.julia/packages/Optimization/XjqVZ/src/utils.jl:37 [inlined]
 [48] __solve(prob::OptimizationProblem{true, OptimizationFunction{true,Optimization.AutoZygote,…}, Vector{Float64}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, opt::Optimisers.AMSGrad{Float32}, data::Base.Iterators.Cycle{Tuple{Optimization.NullData}}; maxiters::Int64, callback::Function, progress::Bool, save_best::Bool, kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:sensealg, :allow_f_increases), Tuple{ReverseDiffVJP{true}, Bool}}})
    @ OptimizationOptimisers ~/.julia/packages/OptimizationOptimisers/KGKWE/src/OptimizationOptimisers.jl:35
 [49] #solve#552
    @ ~/.julia/packages/SciMLBase/gTrkJ/src/solve.jl:85 [inlined]
 [50] single_run()
    @ Main ~/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:21
in expression starting at /Users/erlebach/src/2022/rude/giesekus/GE_rude.jl/rude_optimized/mwe/rude_functions.jl:42

Note that the trace is substantially longer than the one produced when using DaemonMode (lots of inline stuff), but likely more accurate. The error message at the top is the same.

Here is the project.toml file:

AbbreviatedStackTraces = "ac637c84-cc71-43bf-9c33-c1b4316be3d4"
ArgMacros = "dbc42088-9de8-42a0-8ec8-2cd114e1ea3e"
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DaemonMode = "d749ddd5-2b29-4920-8305-6ff5a704e36e"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
DiffEqFlux = "aae7a2af-3d4f-5e19-a356-7da93b79d9d0"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Pickle = "fbb45041-c46e-462f-888f-7c521cafbc2c"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Profile = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
TruncatedStacktraces = "781d530d-4396-4725-bb49-402e4bee1e77"
Tullio = "bc48ee85-29a4-5162-ae0b-a64e1601d4bc"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

Is this a tête-à-tête forum?

I do not understand. Given the confusion in my other thread, I created a new thread. I have been trying to solve my problem for a few days now.

I am no expert so apologies if I only add noise. But the error message suggests that a range is being used where it should not be used. Have you tried replacing trajectories=1:1 to trajectories=1 in ensemble_solve?.

Thanks. It was 1 before I switched to 1:1, because of the function signature in the documentation. But you are correct, based on the documentation:
https://docs.sciml.ai/DiffEqDocs/dev/features/ensemble/
I have been making too many changes in my quest for the most useful MWE. I will think some more before posting again.