Comments (3)
I expect this has an identical root cause as #5, though the specific error has come out differently. The same bandaid works – 0.5(x'*S*x)
.
julia> derivative(g, @SVector randn(4))
4-element SArray{Tuple{4},Float64,1,4}:
-8.43300310272326
28.03595720287608
-4.489528942268124
-6.965151312812216
As an aside, it's very pleasing that custom array types already work nicely like this; it's not something I had tested up to now. StaticArrays are an interesting case, because for things like this they should be very fast.
from zygote.jl.
Feel free to close this issue in favor of the old one. This works for me.
Thanks for the great work here - I'm excitedly following the development of this and Capstan.
from zygote.jl.
Right now, this code is type stable, but much slower than ForwardDiff (or the analytic gradient):
julia> using StaticArrays, BenchmarkTools, ForwardDiff
julia> import Zygote: gradient #always re-precompiles?
[ Info: Precompiling Zygote [e88e6eb3-aa80-5325-afca-941959d7151f]
[ Info: Precompiling IRTools [7869d1d1-7146-5819-86e3-90919afe41df]
julia> const S = (@SMatrix randn(6,4)) |> x -> x' * x;
julia> g(x) = 0.5 * (x' * S * x);
julia> x = @SVector randn(4);
julia> g(x)
12.515760061124144
julia> x' * S
1×4 LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}:
-1.82664 -13.4078 -5.39434 1.61724
julia> gradient(g, x)
([-1.82664, -13.4078, -5.39434, 1.61724],)
julia> ForwardDiff.gradient(g, x)'
1×4 LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}:
-1.82664 -13.4078 -5.39434 1.61724
julia> @benchmark gradient(g, $x)
BenchmarkTools.Trial:
memory estimate: 1.64 KiB
allocs estimate: 22
--------------
minimum time: 1.327 μs (0.00% GC)
median time: 1.374 μs (0.00% GC)
mean time: 2.254 μs (35.01% GC)
maximum time: 5.420 ms (99.91% GC)
--------------
samples: 10000
evals/sample: 10
julia> @benchmark ForwardDiff.gradient(g, $x)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 47.753 ns (0.00% GC)
median time: 48.698 ns (0.00% GC)
mean time: 48.696 ns (0.00% GC)
maximum time: 70.348 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 988
julia> @benchmark $x' * S
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 4.029 ns (0.00% GC)
median time: 4.034 ns (0.00% GC)
mean time: 4.209 ns (0.00% GC)
maximum time: 18.588 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
julia> @code_warntype gradient(g, x)
Body::Tuple{SArray{Tuple{4},Float64,1,4}}
32 1 ── %1 = (getfield)(args, 1)::SArray{Tuple{4},Float64,1,4} │
│ %2 = Zygote.nothing::Nothing │╻╷ forward
│ %3 = %new(Zygote.Context, %2)::Zygote.Context ││┃││ _forward
│ %4 = %new(LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}, %1)::LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}
│ %5 = Main.S::SArray{Tuple{4,4},Float64,2,16} ││││
│ %6 = invoke Zygote._forward(%3::Zygote.Context, Main.:*::typeof(*), %4::LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}, $(QuoteNode([4.22863 0.218509 -1.17347 1.24761; 0.218509 8.47895 2.104 0.21028; -1.17347 2.104 3.31319 -1.31538; 1.24761 0.21028 -1.31538 3.23605]))::SArray{Tuple{4,4},Float64,2,16}, %1::SArray{Tuple{4},Float64,1,4})::Tuple{Float64,Zygote.J{Tuple{typeof(*),LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16},SArray{Tuple{4},Float64,1,4}},Tuple{typeof(*),LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16},SArray{Tuple{4},Float64,1,4},Tuple{},getfield(Zygote, Symbol("##165#back2#123")){getfield(Zygote, Symbol("##121#122")){Tuple{Tuple{Nothing,Nothing},Tuple{}},Zygote.J{Tuple{typeof(Base.afoldl),typeof(*),Float64},Tuple{typeof(Base.afoldl),typeof(*),Float64}}}},getfield(Zygote, Symbol("##147#back2#110")){typeof(identity)},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4},Float64,1,4}}},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}}}}}
│ %7 = (Base.getfield)(%6, 1, true)::Float64 ││││╻ getindex
│ %8 = (Base.getfield)(%6, 2, true)::Zygote.J{Tuple{typeof(*),LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16},SArray{Tuple{4},Float64,1,4}},Tuple{typeof(*),LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16},SArray{Tuple{4},Float64,1,4},Tuple{},getfield(Zygote, Symbol("##165#back2#123")){getfield(Zygote, Symbol("##121#122")){Tuple{Tuple{Nothing,Nothing},Tuple{}},Zygote.J{Tuple{typeof(Base.afoldl),typeof(*),Float64},Tuple{typeof(Base.afoldl),typeof(*),Float64}}}},getfield(Zygote, Symbol("##147#back2#110")){typeof(identity)},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4},Float64,1,4}}},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}}}}
└─── (Base.mul_float)(0.5, %7) │││││╻╷ macro expansion
34 2 ┄─ (Base.mul_float)(1.0, %7) │╻╷╷╷╷╷ #66
│ %11 = (Base.mul_float)(1.0, 0.5)::Float64 ││╻ g
│ %12 = (Base.getfield)(%8, :t)::Tuple{typeof(*),LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16},SArray{Tuple{4},Float64,1,4},Tuple{},getfield(Zygote, Symbol("##165#back2#123")){getfield(Zygote, Symbol("##121#122")){Tuple{Tuple{Nothing,Nothing},Tuple{}},Zygote.J{Tuple{typeof(Base.afoldl),typeof(*),Float64},Tuple{typeof(Base.afoldl),typeof(*),Float64}}}},getfield(Zygote, Symbol("##147#back2#110")){typeof(identity)},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4},Float64,1,4}}},getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}}}
│ %13 = (Base.getfield)(%12, 8, true)::getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4},Float64,1,4}}}
│ %14 = (Base.getfield)(%12, 9, true)::getfield(Zygote, Symbol("##1014#back2#564")){getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}}
│ %15 = π (%11, Float64) │││││╻ #121
│ %16 = (Core.getfield)(%13, Symbol("#1013#back"))::getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4},Float64,1,4}}
│ %17 = (Core.getfield)(%16, :b)::SArray{Tuple{4},Float64,1,4} │││││╻ #562
│ (Base.ifelse)(false, 0, 4) ││││││╻╷╷╷╷╷╷╷╷╷╷╷ *
│ %19 = (Base.getfield)(%17, :data)::NTuple{4,Float64} │││││││╻╷╷╷ broadcast
│ %20 = (Base.getfield)(%19, 1, false)::Float64 ││││││││╻ broadcast
│ %21 = (Base.mul_float)(%15, %20)::Float64 │││││││││╻ materialize
│ %22 = (Base.getfield)(%17, :data)::NTuple{4,Float64} ││││││││││╻ copy
│ %23 = (Base.getfield)(%22, 2, false)::Float64 │││││││││││╻ _broadcast
│ %24 = (Base.mul_float)(%15, %23)::Float64 ││││││││││││╻ macro expansion
│ %25 = (Base.getfield)(%17, :data)::NTuple{4,Float64} │││││││││││││╻ getindex
│ %26 = (Base.getfield)(%25, 3, false)::Float64 ││││││││││││││╻ getindex
│ %27 = (Base.mul_float)(%15, %26)::Float64 ││││││││││││││╻ *
│ %28 = (Base.getfield)(%17, :data)::NTuple{4,Float64} ││││││││││││││╻ getproperty
│ %29 = (Base.getfield)(%28, 4, false)::Float64 ││││││││││││││╻ getindex
│ %30 = (Base.mul_float)(%15, %29)::Float64 ││││││││││││││╻ *
│ %31 = (StaticArrays.tuple)(%21, %24, %27, %30)::NTuple{4,Float64} │││││││││││││
│ %32 = %new(SArray{Tuple{4},Float64,1,4}, %31)::SArray{Tuple{4},Float64,1,4}│││││││││││╻ Type
└─── goto #4 │││││││││││││
3 ── $(Expr(:unreachable)) │││││││││││││
4 ┄─ goto #5 │││││││││││
5 ── goto #6 ││││││││││
6 ── goto #7 │││││││││
7 ── %38 = %new(LinearAlgebra.Transpose{Float64,SArray{Tuple{4},Float64,1,4}}, %32)::LinearAlgebra.Transpose{Float64,SArray{Tuple{4},Float64,1,4}}
└─── goto #8 ││││││││
8 ── goto #9 │││││││
9 ── %41 = (Core.getfield)(%16, :a)::LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}}
│ %42 = (Base.getfield)(%41, :parent)::SArray{Tuple{4},Float64,1,4} │││││││╻ getproperty
│ (Base.ifelse)(false, 0, 4) │││││││╻╷╷╷╷╷╷╷╷╷ broadcast
│ %44 = (Base.getfield)(%42, :data)::NTuple{4,Float64} ││││││││╻╷╷╷ materialize
│ %45 = (Base.getfield)(%44, 1, false)::Float64 │││││││││╻ copy
│ %46 = (Base.mul_float)(%45, %15)::Float64 ││││││││││╻ _broadcast
│ %47 = (Base.getfield)(%42, :data)::NTuple{4,Float64} │││││││││││╻ macro expansion
│ %48 = (Base.getfield)(%47, 2, false)::Float64 ││││││││││││╻ getindex
│ %49 = (Base.mul_float)(%48, %15)::Float64 ││││││││││││╻ *
│ %50 = (Base.getfield)(%42, :data)::NTuple{4,Float64} │││││││││││││╻ getproperty
│ %51 = (Base.getfield)(%50, 3, false)::Float64 │││││││││││││╻ getindex
│ %52 = (Base.mul_float)(%51, %15)::Float64 ││││││││││││╻ *
│ %53 = (Base.getfield)(%42, :data)::NTuple{4,Float64} │││││││││││││╻ getproperty
│ %54 = (Base.getfield)(%53, 4, false)::Float64 │││││││││││││╻ getindex
│ %55 = (Base.mul_float)(%54, %15)::Float64 ││││││││││││╻ *
└─── goto #11 ││││││││││││
10 ─ $(Expr(:unreachable)) ││││││││││││
11 ┄ goto #12 ││││││││││
12 ─ goto #13 │││││││││
13 ─ goto #14 ││││││││
14 ─ goto #15 │││││││
15 ─ goto #16 ││││││
16 ─ goto #17 │││││
17 ─ %64 = (Core.getfield)(%14, Symbol("#1013#back"))::getfield(Zygote, Symbol("##562#563")){LinearAlgebra.Adjoint{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}
│ %65 = invoke %64(%38::LinearAlgebra.Transpose{Float64,SArray{Tuple{4},Float64,1,4}})::Tuple{LinearAlgebra.Transpose{Float64,SArray{Tuple{4},Float64,1,4}},SArray{Tuple{4,4},Float64,2,16}}
│ %66 = (getfield)(%65, 1)::LinearAlgebra.Transpose{Float64,SArray{Tuple{4},Float64,1,4}} _gradtuple
│ %67 = (getfield)(%65, 2)::SArray{Tuple{4,4},Float64,2,16} ││││││
└─── goto #18 ││││
18 ─ invoke Zygote.accum_param(%3::Zygote.Context, %5::SArray{Tuple{4,4},Float64,2,16}, %67::SArray{Tuple{4,4},Float64,2,16})
│ %70 = (Base.getfield)(%66, :parent)::SArray{Tuple{4},Float64,1,4} ││││╻╷╷ #568
│ (Base.ifelse)(false, 0, 4) ││││╻╷╷╷╷╷╷╷╷ materialize
│ (Base.ifelse)(false, 0, 4) │││││╻╷╷╷╷╷ instantiate
│ %73 = (4 === 4)::Bool ││││││╻╷╷╷╷ combine_axes
│ %74 = (Base.and_int)(true, %73)::Bool │││││││╻ broadcast_shape
└─── goto #20 if not %74 ││││││││┃││ _bcs
19 ─ goto #21 │││││││││┃│ _bcs1
20 ─ goto #21 ││││││││││┃ _bcsm
21 ┄ %78 = φ (#19 => %74, #20 => false)::Bool ││││││││││
└─── goto #23 if not %78 ││││││││││
22 ─ goto #29 ││││││││││
23 ─ %81 = (4 === 4)::Bool │││││││││││╻╷ ==
│ %82 = (Base.and_int)(true, %81)::Bool ││││││││││││╻ &
└─── goto #25 if not %82 │││││││││││
24 ─ goto #26 │││││││││││
25 ─ goto #26 │││││││││││
26 ┄ %86 = φ (#24 => %82, #25 => false)::Bool ││││││││││
└─── goto #28 if not %86 ││││││││││
27 ─ goto #29 ││││││││││
28 ─ %89 = %new(Base.DimensionMismatch, "arrays could not be broadcast to a common size")::DimensionMismatchpe
│ (Base.Broadcast.throw)(%89) ││││││││││
└─── $(Expr(:unreachable)) ││││││││││
29 ┄ goto #30 │││││││││
30 ─ goto #31 ││││││││
31 ─ goto #32 │││││││
32 ─ goto #33 │││││╻ instantiate
33 ─ %96 = (Base.getfield)(%70, :data)::NTuple{4,Float64} ││││││╻╷╷╷ _broadcast
│ %97 = (Base.getfield)(%96, 1, false)::Float64 │││││││╻ macro expansion
│ %98 = (Base.add_float)(%46, %97)::Float64 ││││││││╻ accum
│ %99 = (Base.getfield)(%70, :data)::NTuple{4,Float64} │││││││││╻ getproperty
│ %100 = (Base.getfield)(%99, 2, false)::Float64 │││││││││╻ getindex
│ %101 = (Base.add_float)(%49, %100)::Float64 │││││││││╻ +
│ %102 = (Base.getfield)(%70, :data)::NTuple{4,Float64} │││││││││╻ getproperty
│ %103 = (Base.getfield)(%102, 3, false)::Float64 │││││││││╻ getindex
│ %104 = (Base.add_float)(%52, %103)::Float64 │││││││││╻ +
│ %105 = (Base.getfield)(%70, :data)::NTuple{4,Float64} │││││││││╻ getproperty
│ %106 = (Base.getfield)(%105, 4, false)::Float64 │││││││││╻ getindex
│ %107 = (Base.add_float)(%55, %106)::Float64 │││││││││╻ +
│ %108 = (StaticArrays.tuple)(%98, %101, %104, %107)::NTuple{4,Float64} ││││││││
│ %109 = %new(SArray{Tuple{4},Float64,1,4}, %108)::SArray{Tuple{4},Float64,1,4}│││││╻ Type
└─── goto #35 ││││││││
34 ─ $(Expr(:unreachable)) ││││││││
35 ┄ goto #36 ││││││
36 ─ goto #37 │││││
37 ─ goto #38 ││││
38 ─ goto #39 │││
39 ─ %116 = (Core.tuple)(%109)::Tuple{SArray{Tuple{4},Float64,1,4}} │││╻ tail
└─── goto #40 ││
40 ─ return %116 │
41 ─ goto #2
I also do not see any change from Zygote.refresh()
:
julia> using Zygote
julia> Zygote.refresh()
julia> gradient(g, x)
([-1.82664, -13.4078, -5.39434, 1.61724],)
julia> @benchmark gradient(g, $x)
BenchmarkTools.Trial:
memory estimate: 1.64 KiB
allocs estimate: 22
--------------
minimum time: 1.346 μs (0.00% GC)
median time: 1.393 μs (0.00% GC)
mean time: 2.312 μs (35.93% GC)
maximum time: 5.704 ms (99.92% GC)
--------------
samples: 10000
evals/sample: 10
from zygote.jl.
Related Issues (20)
- `sort(x; rev=true)` is not supported HOT 1
- Incorrect gradients for `plan_rfft(x) * x` HOT 2
- Gradient of scalar function of gradient giving mutating array error HOT 4
- `sum` with CUDA and view on array errors HOT 3
- Cannot take gradient of sort on 2D CuArray HOT 1
- `Ref` and broadcasting issue HOT 2
- Strip zygote frames from mutation error stack trace HOT 1
- Zygote in Julia 1.10+ not reading rrules for default constructors HOT 7
- Few issues in the Zygote Home Page documentation HOT 1
- OOM when computing the gradient in an embarrassingly parallel problem HOT 1
- Pullback over jacobian HOT 6
- `withjacobian` flattens the output when it is a matrix HOT 1
- Gradient wrt to a sparse matrix is mathematically wrong HOT 6
- Increasing memory usage in each call of gradient HOT 1
- Precompilation error in Julia nightly HOT 1
- Gradient involving `LinearAlgebra.tr` errors HOT 1
- Device-to-host copies with GPU code HOT 11
- Inferring `Any` on gradient w.r.t. wrapper of recursive type
- Manually changing the Flux parameters and optimizing using Zygote
- Support try/catch by assuming try branch HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zygote.jl.