-
-
Notifications
You must be signed in to change notification settings - Fork 215
Accumulate mutable structs with Ref
#1574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DhairyaLGandhi
wants to merge
7
commits into
FluxML:master
Choose a base branch
from
DhairyaLGandhi:dg/remake
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+24
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 tasks
Ref
Not sure why the CUDA test fails, with Metal, I see: julia> a_gpu = a |> mtl
9-element MtlVector{Float32, Metal.PrivateStorage}:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
julia> gradient(x -> sum(x .^ 3) / count(x .> 3), a_gpu)[1]
9-element MtlVector{Float32, Metal.PrivateStorage}:
0.5
2.0
4.5
8.0
12.5
18.0
24.5
32.0
40.5 |
Interestingly, I see the same issue as buildkite with current tags (jl_h6hRNW) pkg> st
Status `/tmp/jl_h6hRNW/Project.toml`
[052768ef] CUDA v5.8.1
[e88e6eb3] Zygote v0.7.7 julia> a = Float32.(1:9)
9-element Vector{Float32}:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
julia> a_gpu = a |> cu
9-element CuArray{Float32, 1, CUDA.DeviceMemory}:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
julia> g3 = gradient(x -> sum(x .^ 3) / count(x .> 3), a)[1]
9-element Vector{Float32}:
0.5
2.0
4.5
8.0
12.5
18.0
24.5
32.0
40.5
julia> gradient(x -> sum(x .^ 3) / count(x .> 3), a_gpu)[1]
9-element CuArray{Float32, 1, CUDA.DeviceMemory}:
0.42857146
1.7142859
3.8571432
6.8571434
10.714287
15.428573
21.000002
27.428574
34.714287 |
mutable struct MWEGetter{G, U, P, T}
idxs::G
u::U
p::P
t::T
end
u = ones(3)
p = ones(3)
t_ = 1.5
idxs = [1, 2]
mwe = MWEGetter(idxs, u, p, t_)
function fn(mwe)
map(i -> mwe.u[i], mwe.idxs)
end
gs = Zygote.gradient(mwe) do mwe
sum(fn(mwe))
end produces ERROR: MethodError: no method matching +(::Base.RefValue{Any}, ::@NamedTuple{getter::Nothing, u::Vector{Float64}, p::Nothing, t::Nothing})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...)
@ Base operators.jl:587
+(::ZeroTangent, ::Any)
@ ChainRulesCore ~/.julia/packages/ChainRulesCore/U6wNx/src/tangent_arithmetic.jl:99
+(::Any, ::Symbolics.SemiMonomial)
@ Symbolics ~/.julia/packages/Symbolics/kQzvO/src/semipoly.jl:31
...
Stacktrace:
[1] accum(x::Base.RefValue{Any}, y::@NamedTuple{getter::Nothing, u::Vector{Float64}, p::Nothing, t::Nothing})
@ Zygote ~/Downloads/arpa/jsmo/t2/Zygote.jl/src/lib/lib.jl:9
[2] fn2
@ ~/Downloads/arpa/jsmo/t2/JuliaSimExampleComponents/base_err.jl:455 [inlined]
[3] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Tuple{Float64, Float64})
@ Zygote ~/Downloads/arpa/jsmo/t2/Zygote.jl/src/compiler/interface2.jl:0
[4] #739
@ ~/Downloads/arpa/jsmo/t2/JuliaSimExampleComponents/base_err.jl:459 [inlined]
[5] (::Zygote.var"#88#89"{Zygote.Pullback{Tuple{…}, Tuple{…}}})(Δ::Float64)
@ Zygote ~/Downloads/arpa/jsmo/t2/Zygote.jl/src/compiler/interface.jl:97
[6] gradient(f::Function, args::MWEGetter{Tuple{Int64, Int64}, Vector{Float64}, Vector{Float64}, Float64})
@ Zygote ~/Downloads/arpa/jsmo/t2/Zygote.jl/src/compiler/interface.jl:154
[7] top-level scope
@ ~/Downloads/arpa/jsmo/t2/JuliaSimExampleComponents/base_err.jl:458
Some type information was truncated. Use `show(err)` to see complete types. |
bump cc @ChrisRackauckas |
I don't have review or merge here. @mcabbott could you please take a look? This is pretty urgent from SciML since we have a lot of regressions on v0.7 without this. |
Bump |
bump @mcabbott @ToucheSir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In several downstream packages in SciML, ref SciML/ModelingToolkit.jl#3585, SciML/SciMLSensitivity.jl#1212 and others, accumulation into a mutable Tangent throws errors of the form.
The reason is that the
literal_getproperty
adjoint returnsRef
instead of the Tangent itself for mutable structs. This creates an issue at the accumulation stage when only a partial graph has accumulated.Motivating case:
Notice how the
Ref
is removed in the output.Since the accumulation happens for the mutables in the pullback (and has several other issues with creating statefulness), this way unifies the return types from both branches, and thus fixes the errors seen downstream.
PR Checklist