Skip to content
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

add @adjoin for relu to increase speed #367

Merged
merged 12 commits into from
Oct 24, 2019
Merged

Conversation

findmyway
Copy link
Contributor

using Flux, Zygote, BenchmarkTools

x = rand(128)

# before
@benchmark gradient((x) -> sum(relu.(x)), $x)

#=
BenchmarkTools.Trial: 
  memory estimate:  15.41 KiB
  allocs estimate:  425
  --------------
  minimum time:     9.980 μs (0.00% GC)
  median time:      10.649 μs (0.00% GC)
  mean time:        12.495 μs (4.12% GC)
  maximum time:     2.701 ms (98.24% GC)
  --------------
  samples:          10000
  evals/sample:     1
=#

# after

@benchmark gradient((x) -> sum(relu.(x)), $x)
#=
BenchmarkTools.Trial: 
  memory estimate:  2.30 KiB
  allocs estimate:  3
  --------------
  minimum time:     258.013 ns (0.00% GC)
  median time:      303.099 ns (0.00% GC)
  mean time:        368.242 ns (16.51% GC)
  maximum time:     4.796 μs (85.99% GC)
  --------------
  samples:          10000
  evals/sample:     394
=#

@MikeInnes
Copy link
Member

MikeInnes commented Oct 9, 2019

This is useful, but you probably don't need the loop vs. just using another broadcast in the backwards pass. See also the gradients in broadcast.jl for examples. The ifelse function might also be useful.

src/lib/nnlib.jl Outdated Show resolved Hide resolved
@mcabbott
Copy link
Member

mcabbott commented Oct 9, 2019

This is neat. While you’re in there, perhaps add something similar for identity, as this is as slow as relu before this PR: @btime gradient(x -> sum(identity.(x)), $x);. (And gets done by Dense etc. by default.)

src/lib/nnlib.jl Outdated Show resolved Hide resolved
src/Zygote.jl Show resolved Hide resolved
src/lib/nnlib.jl Outdated Show resolved Hide resolved
@pevnak
Copy link

pevnak commented Oct 14, 2019

I have handwritten gradients for sigmoid, tanh and selu, as I needed those for one of my projects. Would it make sense to add those?

explicitgrad(::typeof(tanh), x) = 1f0 - tanh(x)^2
explicitgrad(::typeof(NNlib.σ), x) = σ(x)*(1f0 - σ(x))
function explicitgrad(::typeof(NNlib.selu), x) 
  λ = oftype(x/1, 1.0507009873554804934193349852946)
  α = oftype(x/1, 1.6732632423543772848170429916717)
  λ * ifelse(x > 0, 1, α * exp(x))
end

@MikeInnes
Copy link
Member

Thanks @findmyway!

@pevnak yeah, those would be great to have as a PR.

bors r+

bors bot added a commit that referenced this pull request Oct 24, 2019
367: add @adjoin for relu to increase speed r=MikeInnes a=findmyway

```julia

using Flux, Zygote, BenchmarkTools

x = rand(128)

# before
@benchmark gradient((x) -> sum(relu.(x)), $x)

#=
BenchmarkTools.Trial: 
  memory estimate:  15.41 KiB
  allocs estimate:  425
  --------------
  minimum time:     9.980 μs (0.00% GC)
  median time:      10.649 μs (0.00% GC)
  mean time:        12.495 μs (4.12% GC)
  maximum time:     2.701 ms (98.24% GC)
  --------------
  samples:          10000
  evals/sample:     1
=#

# after

@benchmark gradient((x) -> sum(relu.(x)), $x)
#=
BenchmarkTools.Trial: 
  memory estimate:  2.30 KiB
  allocs estimate:  3
  --------------
  minimum time:     258.013 ns (0.00% GC)
  median time:      303.099 ns (0.00% GC)
  mean time:        368.242 ns (16.51% GC)
  maximum time:     4.796 μs (85.99% GC)
  --------------
  samples:          10000
  evals/sample:     394
=#
```

Co-authored-by: Jun Tian <tianjun.cpp@gmail.com>
Co-authored-by: Mike J Innes <mike.j.innes@gmail.com>
@bors
Copy link
Contributor

bors bot commented Oct 24, 2019

Build succeeded

@bors bors bot merged commit bdd7ddd into FluxML:master Oct 24, 2019
@racinmat racinmat mentioned this pull request Jul 29, 2020
bors bot added a commit that referenced this pull request Jul 29, 2020
748: Speed up of selu r=DhairyaLGandhi a=racinmat

Implementing speedup of selu based on this comment: #367 (comment)
Utilizing vectorized ifelse based on https://github.com/FluxML/NNlib.jl/blob/master/src/activation.jl#L159.


Co-authored-by: Matěj Račinský <matej.racinsky@avast.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants