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

Reverse mode gradient is up to 100x slower in Enzyme v0.12 #1409

Closed
gdalle opened this issue Apr 29, 2024 · 7 comments
Closed

Reverse mode gradient is up to 100x slower in Enzyme v0.12 #1409

gdalle opened this issue Apr 29, 2024 · 7 comments

Comments

@gdalle
Copy link
Contributor

gdalle commented Apr 29, 2024

I think it is a consequence of the type instability spotted in #1401

Setup:

julia> using Chairmarks, Enzyme

julia> f(x) = sum(abs2, x);

julia> bench_outofplace(n) = @b (; x=rand(n),) gradient(Enzyme.Reverse, f, _.x)

julia> bench_inplace(n) = @b (; x=rand(n), g=rand(n)) gradient!(Enzyme.Reverse, _.g, f, _.x) evals=1

Out-of-place benchmarks:

Size Enzyme v0.11 Enzyme v0.12
10 38.921 ns (1 allocs: 144 bytes) 3.163 μs (31 allocs: 2.016 KiB, 0.01% compile time)
100 424.667 ns (1 allocs: 896 bytes) 3.766 μs (31 allocs: 2.750 KiB, 0.15% compile time)
1000 4.090 μs (1 allocs: 7.938 KiB) 9.798 μs (31 allocs: 9.812 KiB, <0.01% compile time)

In-place benchmarks (may be imprecise due to evals = 1, see https://chairmarks.lilithhafner.com/v1.2.1/tutorial#Common-pitfalls):

Size Enzyme v0.11 Enzyme v0.12
10 25.000 ns 3.039 μs (28 allocs: 1.547 KiB, <0.01% compile time)
100 361.000 ns 3.438 μs (28 allocs: 1.547 KiB, 0.16% compile time)
1000 4.279 μs 7.439 μs (28 allocs: 1.547 KiB, 0.17% compile time)
@gdalle gdalle changed the title Reverse mode gradient is 10x slower in Enzyme v0.12 Reverse mode gradient is slower in Enzyme v0.12 Apr 29, 2024
@gdalle gdalle changed the title Reverse mode gradient is slower in Enzyme v0.12 Reverse mode gradient is up to 10x slower in Enzyme v0.12 Apr 29, 2024
@gdalle gdalle changed the title Reverse mode gradient is up to 10x slower in Enzyme v0.12 Reverse mode gradient is up to 100x slower in Enzyme v0.12 Apr 29, 2024
@adrhill
Copy link

adrhill commented Apr 29, 2024

I can reproduce this on Enzyme v0.12.0 and v0.11.20.

v0.11.20:

julia> bench_outofplace(10)
17.932 ns (1 allocs: 144 bytes)

julia> bench_outofplace(100)
54.717 ns (1 allocs: 896 bytes)

julia> bench_outofplace(1000)
347.222 ns (1 allocs: 8.000 KiB)

v0.12.0:

julia> bench_outofplace(10)
1.653 μs (31 allocs: 2.016 KiB)

julia> bench_outofplace(100)
1.722 μs (31 allocs: 2.750 KiB)

julia> bench_outofplace(1000)
2.521 μs (31 allocs: 9.875 KiB)
julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M3 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 6 virtual cores)

@wsmoses
Copy link
Member

wsmoses commented Apr 29, 2024 via email

@gdalle
Copy link
Contributor Author

gdalle commented Apr 29, 2024

Thanks! If you need beta testers, let me know!

@wsmoses
Copy link
Member

wsmoses commented May 6, 2024

@gdalle can you see if #1415 resolves the issue?

@gdalle
Copy link
Contributor Author

gdalle commented May 6, 2024

On a slower laptop:

Enzyme 0.11

julia> bench_inplace(10)
141.000 ns

julia> bench_inplace(100)
866.000 ns

julia> bench_outofplace(10)
140.321 ns (1 allocs: 144 bytes)

julia> bench_outofplace(100)
907.133 ns (1 allocs: 896 bytes)

PR on Enzyme 0.12

julia> bench_inplace(10)
5.569 μs (28 allocs: 1.547 KiB, <0.01% compile time)

julia> bench_inplace(100)
6.383 μs (28 allocs: 1.547 KiB, 0.03% compile time)

julia> bench_outofplace(10)
5.483 μs (29 allocs: 1.688 KiB, 0.03% compile time)

julia> bench_outofplace(100)
6.405 μs (29 allocs: 2.422 KiB, <0.01% compile time)

Still not there it seems

@wsmoses
Copy link
Member

wsmoses commented May 7, 2024

Should be fixed by #1415 please reopen if it persists

@wsmoses wsmoses closed this as completed May 7, 2024
@gdalle
Copy link
Contributor Author

gdalle commented May 7, 2024

fixed indeed, good job!

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

No branches or pull requests

3 participants