Giter Club home page Giter Club logo

Comments (4)

MikeInnes avatar MikeInnes commented on May 16, 2024

From the compiler's perspective this is not so simple :) x^2 actually lowers to something like Base.literal_pow(x, Val(2)) – I just pushed a fix to make sure we're handling that properly. The a+b+c also involves handling varargs and apply, which for now is slow; but you can avoid that with (a+b)+c.

Another sticky point about benchmarking Zygote – for reasons I don't fully understand, but which are probably to do with world ages and function visibility, the optimisations don't kick in properly unless you run things once and then refresh(). For example, this timing script gives results in under 2ns, whereas a simpler version gives more like 1mus.

using Zygote, BenchmarkTools
using Zygote: gradient

f(x) = (3x^2+2x)+1
gradient(f, 5)

Zygote.refresh()

gradient(f, 5)
@benchmark gradient(f, 5)

from zygote.jl.

chriselrod avatar chriselrod commented on May 16, 2024

I am getting a minor performance improvement, but still around 1 microsecond.

julia> using Zygote, BenchmarkTools
[ Info: Recompiling stale cache file /home/chris/.julia/compiled/v1.0/BenchmarkTools/ZXPQo.ji for BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]

julia> using Zygote: gradient

julia> f(x) = (3x^2+2x)+1
f (generic function with 1 method)

julia> gradient(f, 5)
(32.0,)

julia> @benchmark gradient(f, 5)
BenchmarkTools.Trial: 
  memory estimate:  1.73 KiB
  allocs estimate:  51
  --------------
  minimum time:     1.563 μs (0.00% GC)
  median time:      1.660 μs (0.00% GC)
  mean time:        2.334 μs (26.69% GC)
  maximum time:     4.647 ms (99.91% GC)
  --------------
  samples:          10000
  evals/sample:     10

julia> Zygote.refresh()

julia> gradient(f, 5)
(32.0,)

julia> @benchmark gradient(f, 5)
BenchmarkTools.Trial: 
  memory estimate:  1.23 KiB
  allocs estimate:  33
  --------------
  minimum time:     1.159 μs (0.00% GC)
  median time:      1.223 μs (0.00% GC)
  mean time:        1.987 μs (35.79% GC)
  maximum time:     4.913 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     10

julia> Zygote.refresh()

julia> @benchmark gradient(f, 5)
BenchmarkTools.Trial: 
  memory estimate:  1.23 KiB
  allocs estimate:  33
  --------------
  minimum time:     1.117 μs (0.00% GC)
  median time:      1.197 μs (0.00% GC)
  mean time:        1.961 μs (36.38% GC)
  maximum time:     4.905 ms (99.93% GC)
  --------------
  samples:          10000
  evals/sample:     10

julia> versioninfo()
Julia Version 1.0.0
Commit ece21e1ae5 (2018-08-29 14:15 UTC)

from zygote.jl.

MikeInnes avatar MikeInnes commented on May 16, 2024

I got this down to a few hundred ns; not perfect, but at least that gets rid of all the really dumb issues.

For now, you'll need the Julia branch mentioned on the readme to get really good performance. I'm hoping to fix that for many more cases in future.

from zygote.jl.

chriselrod avatar chriselrod commented on May 16, 2024

Note that

julia> versioninfo()
Julia Version 1.0.0
Commit ece21e1ae5 (2018-08-29 14:15 UTC)

is the latest commit on this branch: https://github.com/JuliaLang/julia/tree/mji/zygote

I'll rebuild next time I notice you've pushed updates and test again.

EDIT:
I was accidentally not on Zygote master.

julia> using Zygote, BenchmarkTools

julia> using Zygote: gradient

julia> f(x) = (3x^2+2x)+1
f (generic function with 1 method)

julia> gradient(f, 5)
(32,)

julia> @benchmark gradient(f, 5)
BenchmarkTools.Trial: 
  memory estimate:  672 bytes
  allocs estimate:  23
  --------------
  minimum time:     424.166 ns (0.00% GC)
  median time:      458.955 ns (0.00% GC)
  mean time:        592.181 ns (20.64% GC)
  maximum time:     249.501 μs (99.78% GC)
  --------------
  samples:          10000
  evals/sample:     199

julia> Zygote.refresh()

julia> gradient(f, 5)
(32,)

julia> @benchmark gradient(f, 5)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     1.232 ns (0.00% GC)
  median time:      1.252 ns (0.00% GC)
  mean time:        1.276 ns (0.00% GC)
  maximum time:     1.724 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000

from zygote.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.