-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fill
assumes numerical arrays
#537
Comments
This looks like Zygote types are making their way into ChainRules rules -- if the above involved |
Actually its the other way around. ChainRules types are making it into Zygote. julia> struct BV{F,T}
A::F
α::T
end
julia> function *(c, km::BV)
new_A = c*km.A
other_params = getfield.([km], propertynames(km))[2:end]
BV(new_A, other_params...)
end
julia> gradient(bv, V) do bv, V
res = map(x -> x * bv, V)
sum(x -> x.A, res)
end
ERROR: Need an adjoint for constructor BV{Float64, Float64}. Gradient is of type ChainRulesCore.Tangent{BV{Float64, Float64}, NamedTuple{(:A, :α), Tuple{Float64, ChainRulesCore.NoTangent}}}
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] (::Zygote.Jnew{BV{Float64, Float64}, Nothing, false})(Δ::ChainRulesCore.Tangent{BV{Float64, Float64}, NamedTuple{(:A, :α), Tuple{Float64, ChainRulesCore.NoTangent}}})
@ Zygote ~/Downloads/arpa/battery/Zygote.jl/src/lib/lib.jl:323
[3] (::Zygote.var"#1768#back#219"{Zygote.Jnew{BV{Float64, Float64}, Nothing, false}})(Δ::ChainRulesCore.Tangent{BV{Float64, Float64}, NamedTuple{(:A, :α), Tuple{Float64, ChainRulesCore.NoTangent}}})
@ Zygote ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67 Notice the gradient type is a Next, julia> bv
BV{Float64, Float64}(1.0, 0.1)
julia> I_vals, V = rand(81), rand(81)
julia> function (bv::BV)(V_app, ox::Bool; kT::Real = 0.026)
local exp_arg
if ox
exp_arg = (bv.α .* V_app) ./ kT
else
exp_arg = -((1 .- bv.α) .* V_app) ./ kT
end
bv.A .* exp.(exp_arg)
end
julia> Zygote.@adjoint function BV{T,S}(A, α) where {T,S}
BV(A, α), Δ -> begin
(Δ.A, Δ.α)
end
end
julia> gradient(V, bv) do V, bv
res = fill(bv, length(V))
r1 = map((m,v) -> m(v, true), res, V)
r2 = map((m,v) -> m(v, false), res, V)
sum(r1 .- r2)
end
ERROR: MethodError: no method matching +(::NamedTuple{(:A, :α), Tuple{Float64, Float64}}, ::NamedTuple{(:A, :α), Tuple{Float64, Float64}})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
+(::ChainRulesCore.Tangent{P, T} where T, ::P) where P at /Users/dhairyagandhi/.julia/packages/ChainRulesCore/1L9My/src/tangent_arithmetic.jl:145
+(::ChainRulesCore.AbstractThunk, ::Any) at /Users/dhairyagandhi/.julia/packages/ChainRulesCore/1L9My/src/tangent_arithmetic.jl:121
...
Stacktrace:
[1] add_sum(x::NamedTuple{(:A, :α), Tuple{Float64, Float64}}, y::NamedTuple{(:A, :α), Tuple{Float64, Float64}})
@ Base ./reduce.jl:24
[2] mapreduce_impl(f::typeof(identity), op::typeof(Base.add_sum), A::Vector{NamedTuple{(:A, :α), Tuple{Float64, Float64}}}, ifirst::Int64, ilast::Int64, blksize::Int64)
@ Base ./reduce.jl:242
[3] mapreduce_impl
@ ./reduce.jl:257 [inlined]
[4] _mapreduce
@ ./reduce.jl:415 [inlined]
[5] _mapreduce_dim
@ ./reducedim.jl:318 [inlined]
[6] #mapreduce#672
@ ./reducedim.jl:310 [inlined]
[7] mapreduce
@ ./reducedim.jl:310 [inlined]
[8] #_sum#682
@ ./reducedim.jl:878 [inlined]
[9] _sum
@ ./reducedim.jl:878 [inlined]
[10] #_sum#681
@ ./reducedim.jl:877 [inlined]
[11] _sum
@ ./reducedim.jl:877 [inlined]
[12] #sum#679
@ ./reducedim.jl:873 [inlined]
[13] sum
@ ./reducedim.jl:873 [inlined]
[14] fill_pullback
@ ~/.julia/packages/ChainRules/RyXef/src/rulesets/Base/array.jl:342 [inlined] |
* wrap_chainrules_input for arrays of Ref * z2d too, for rrule_via_ad * test from JuliaDiff/ChainRulesCore.jl#440 * add test from JuliaDiff/ChainRules.jl#537 * more tests related to CRC types * union nothing, fix one case * comments
Sorry, I've lost track of where we are on this. Either way, this looks to me like both things are happening -- somehow a |
Zygote was leaking types, but has been fixed:
|
With Zygote, we habitually get NammedTuples (or Tangents) as tangents for structures, however something like the following setup would not work well (note this is an incomplete demo, but describes the basic issue.
This usually points to
The relevant real use case stack trace is something like
The text was updated successfully, but these errors were encountered: