Skip to content

gradient fails when output depends on multi-dimensional generator with a condition #1590

@AwesomeQuest

Description

@AwesomeQuest

Gradient fails when calling with a function that depends on a 2D generator with a condition like the following

gradient([0.,0]) do pts
        N = length(pts)
        reduce(+, i for i in 1:N , j in 1:N if j>i)
end

The error does not happen when replacing the ',' with a "for"

gradient([0.,0]) do pts
        N = length(pts)
        reduce(+, i for i in 1:N for j in 1:N if j>i)
end

Full Stack-trace, package version, and julia version bellow

(@AD) pkg> st
Status `C:\Users\torfi\.julia\environments\AD\Project.toml`
  [e88e6eb3] Zygote v0.7.10

julia> versioninfo()
Julia Version 1.12.1
Commit ba1e628ee4 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, icelake-client)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 8 virtual cores)

julia> gradient([0.,0]) do pts
           N = length(pts)
           reduce(+, i for i in 1:N , j in 1:N if j>i)
       end

julia> show(err)
1-element ExceptionStack:
MethodError: no method matching length(::Nothing)
The function `length` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  length(::Tables.DictRowTable)
   @ Tables C:\Users\torfi\.julia\packages\Tables\cRTb7\src\dicts.jl:118
  length(::IRTools.Inner.IR)
   @ IRTools C:\Users\torfi\.julia\packages\IRTools\v0mn8\src\ir\ir.jl:208
  length(::Core.SimpleVector)
   @ Base essentials.jl:936
  ...

Stacktrace:
 [1] productfunc(xs::Tuple{UnitRange{Int64}, UnitRange{Int64}}, dy::Matrix{Union{Nothing, Tuple{Float64, Nothing}}})
   @ Zygote C:\Users\torfi\.julia\packages\Zygote\55SqB\src\lib\array.jl:278
 [2] (::Zygote.var"#product_pullback#product_pullback##0"{Tuple{UnitRange{Int64}, UnitRange{Int64}}})(dy::Matrix{Union{Nothing, Tuple{Float64, Nothing}}})
   @ Zygote C:\Users\torfi\.julia\packages\Zygote\55SqB\src\lib\array.jl:295
 [3] (::Zygote.var"#977#978"{Zygote.var"#product_pullback#product_pullback##0"{Tuple{UnitRange{Int64}, UnitRange{Int64}}}})(Δ::ChainRulesCore.InplaceableThunk{ChainRulesCore.Thunk{ChainRules.var"#thunked_∇getindex##2#thunked_∇getindex##3"{Matrix{Tuple{Int64, Int64}}, Vector{ChainRulesCore.Tangent{Any, Tuple{Float64, ChainRulesCore.ZeroTangent}}}, Tuple{Vector{CartesianIndex{2}}}}}, ChainRules.var"#thunked_∇getindex##0#thunked_∇getindex##1"{Matrix{Tuple{Int64, Int64}}, Vector{ChainRulesCore.Tangent{Any, Tuple{Float64, ChainRulesCore.ZeroTangent}}}, Tuple{Vector{CartesianIndex{2}}}}})
   @ Zygote C:\Users\torfi\.julia\packages\ZygoteRules\CkVIK\src\adjoint.jl:72
 [4] #49
   @ .\REPL[10]:3 [inlined]
 [5] (::Zygote.Pullback{Tuple{var"#49#50", Vector{Float64}}, Tuple{Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, ComposedFunction{Zygote.ZBack{ChainRules.var"#filter_pullback#rrule##125"{ChainRules.var"#getindex_pullback#rrule##252"{Matrix{Tuple{Int64, Int64}}, Tuple{Vector{CartesianIndex{2}}}, Tuple{ChainRulesCore.NoTangent}}}}, typeof(ZygoteRules.unthunk_tangent)}, Zygote.var"#405#406"{Zygote.Jnew{var"#51#52", Nothing, false}}, Zygote.Pullback{Tuple{Type{Base.Generator}, var"#51#52", Vector{Tuple{Int64, Int64}}}, Tuple{Zygote.var"#405#406"{Zygote.Jnew{Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}, Nothing, false}}}}, Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, Zygote.var"#231#232"{Zygote.var"#229#230"{Zygote.Context{false}, GlobalRef, Type{var"#51#52"}}}, Zygote.var"#405#406"{Zygote.Jnew{var"#53#54", Nothing, false}}, Zygote.var"#231#232"{Zygote.var"#229#230"{Zygote.Context{false}, GlobalRef, Type{var"#53#54"}}}, Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, Zygote.var"#977#978"{Zygote.var"#product_pullback#product_pullback##0"{Tuple{UnitRange{Int64}, UnitRange{Int64}}}}, Zygote.Pullback{Tuple{typeof(reduce), typeof(+), Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}}, Tuple{Zygote.Pullback{Tuple{Type{NamedTuple}}, Tuple{}}, Zygote.Pullback{Tuple{Base.var"##reduce#276", @Kwargs{}, typeof(reduce), typeof(+), Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}}, Any}, Zygote.var"#525#526"{Zygote.var"#pairs_namedtuple_pullback#pairs_namedtuple_pullback##0"{(), @NamedTuple{}}}}}}})(Δ::Int64)
   @ Zygote C:\Users\torfi\.julia\packages\Zygote\55SqB\src\compiler\interface2.jl:0
 [6] (::Zygote.var"#pullback##0#pullback##1"{Zygote.Pullback{Tuple{var"#49#50", Vector{Float64}}, Tuple{Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, ComposedFunction{Zygote.ZBack{ChainRules.var"#filter_pullback#rrule##125"{ChainRules.var"#getindex_pullback#rrule##252"{Matrix{Tuple{Int64, Int64}}, Tuple{Vector{CartesianIndex{2}}}, Tuple{ChainRulesCore.NoTangent}}}}, typeof(ZygoteRules.unthunk_tangent)}, Zygote.var"#405#406"{Zygote.Jnew{var"#51#52", Nothing, false}}, Zygote.Pullback{Tuple{Type{Base.Generator}, var"#51#52", Vector{Tuple{Int64, Int64}}}, Tuple{Zygote.var"#405#406"{Zygote.Jnew{Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}, Nothing, false}}}}, Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, Zygote.var"#231#232"{Zygote.var"#229#230"{Zygote.Context{false}, GlobalRef, Type{var"#51#52"}}}, Zygote.var"#405#406"{Zygote.Jnew{var"#53#54", Nothing, false}}, Zygote.var"#231#232"{Zygote.var"#229#230"{Zygote.Context{false}, GlobalRef, Type{var"#53#54"}}}, Zygote.ZBack{Returns{Tuple{ChainRulesCore.NoTangent, ChainRulesCore.NoTangent, ChainRulesCore.NoTangent}}}, Zygote.var"#977#978"{Zygote.var"#product_pullback#product_pullback##0"{Tuple{UnitRange{Int64}, UnitRange{Int64}}}}, Zygote.Pullback{Tuple{typeof(reduce), typeof(+), Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}}, Tuple{Zygote.Pullback{Tuple{Type{NamedTuple}}, Tuple{}}, Zygote.Pullback{Tuple{Base.var"##reduce#276", @Kwargs{}, typeof(reduce), typeof(+), Base.Generator{Vector{Tuple{Int64, Int64}}, var"#51#52"}}, Any}, Zygote.var"#525#526"{Zygote.var"#pairs_namedtuple_pullback#pairs_namedtuple_pullback##0"{(), @NamedTuple{}}}}}}}})(Δ::Int64)
   @ Zygote C:\Users\torfi\.julia\packages\Zygote\55SqB\src\compiler\interface.jl:97
 [7] gradient(f::Function, args::Vector{Float64})
   @ Zygote C:\Users\torfi\.julia\packages\Zygote\55SqB\src\compiler\interface.jl:154
 [8] top-level scope
   @ REPL[10]:1

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