-
Notifications
You must be signed in to change notification settings - Fork 94
Add chunk size selection strategies #2659
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
8335c1c
336fd3c
d974beb
a61b08a
4865292
b4078d1
6d4ceaf
dc5ff05
a08d7a3
ae02284
73cb9ec
bec2f09
9e18f03
7493267
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Test | ||
| using EnzymeCore | ||
|
|
||
| @testset "SingleChunk" begin | ||
| @test pick_chunksize(SingleChunk(), ones(10)) == Val(10) | ||
| @test pick_chunksize(SingleChunk(), ones(100)) == Val(100) | ||
| end | ||
|
|
||
| @testset "AutoChunk" begin | ||
| @test pick_chunksize(AutoChunk(), ones(10)) == Val(10) | ||
| @test pick_chunksize(AutoChunk(), ones(100)) == Val(16) | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -428,6 +428,10 @@ end | |
| return ((one(x),),) | ||
| end | ||
|
|
||
| @inline function chunkedonehot(x, strategy::ChunkStrategy) | ||
| return chunkedonehot(x, pick_chunksize(strategy, x)) | ||
| end | ||
|
|
||
| @inline tupleconcat(x) = x | ||
| @inline tupleconcat(x, y) = (x..., y...) | ||
| @inline tupleconcat(x, y, z...) = (x..., tupleconcat(y, z...)...) | ||
|
|
@@ -502,10 +506,11 @@ end | |
| @inline specialize_output(output, input) = output | ||
|
|
||
| """ | ||
| gradient(::ForwardMode, f, x; shadows=onehot(x), chunk=nothing) | ||
| gradient(::ForwardMode, f, x, args...; chunk=nothing, shadows=create_shadows(chunk, x, args...)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should change chunk=nothing, to the relevant correct explicit default. we should also not support val/nothing inside of here and isntead add a deprecated method (or perhaps first check in the expr) if its one of the legacy methods and mark as deprecated
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in the latest commit. An issue with the current code is that the deprecation warning will only be visible at the first function call, since that is the only time where the generating function is actually generated: julia> jacobian(Forward, copy, ones(2); chunk=nothing)
┌ Warning: The `chunk=nothing` configuration will be deprecated in a future release. Please use `chunk=SmallestChunk()` instead.
│ caller = #s719#135 at sugar.jl:461 [inlined]
└ @ Core ~/Documents/GitHub/Julia/Enzyme.jl/src/sugar.jl:461
┌ Warning: The `chunk=nothing` configuration will be deprecated in a future release. Please use `chunk=SmallestChunk()` instead.
│ caller = #s717#137 at sugar.jl:621 [inlined]
└ @ Core ~/Documents/GitHub/Julia/Enzyme.jl/src/sugar.jl:621
([1.0 0.0; 0.0 1.0],)
julia> jacobian(Forward, copy, ones(2); chunk=nothing)
([1.0 0.0; 0.0 1.0],)Not sure whether that's an issue or not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we move it to a permanent warning at every call, we should probably add tests for it too |
||
|
|
||
| Compute the gradient of an array-input function `f` using forward mode. The | ||
| optional keyword argument `shadow` is a vector of one-hot vectors of type `x` | ||
| Compute the gradient of an array-input function `f` using forward mode. | ||
| The optional keyword argument `chunk` optionally denotes the chunk size to use: it can be either `nothing`, `Val(C)` for some `C`, `SingleChunk()` or `AutoChunk()`. | ||
|
gdalle marked this conversation as resolved.
Outdated
|
||
| The optional keyword argument `shadow` is a vector of one-hot vectors of type `x` | ||
| which are used to forward-propagate into the return. For performance reasons, | ||
| this should be computed once, outside the call to `gradient`, rather than | ||
| within this call. | ||
|
|
@@ -788,7 +793,7 @@ end | |
| """ | ||
| jacobian(::ForwardMode, args...; kwargs...) | ||
|
|
||
| Equivalent to gradient(::ForwardMode, args...; kwargs...) | ||
| Equivalent to `gradient(::ForwardMode, args...; kwargs...)`. | ||
| """ | ||
| @inline function jacobian(fm::ForwardMode, args...; kwargs...) | ||
| gradient(fm, args...; kwargs...) | ||
|
|
@@ -915,6 +920,8 @@ end | |
| chunksize = if chunk <: Val | ||
| chunk.parameters[1] | ||
| else | ||
| # TODO: handle SingleChunk and MaxChunk | ||
| # this will change the generated function because the chunksize might be determined at runtime | ||
| 1 | ||
| end | ||
| num = ((n_out_val + chunksize - 1) ÷ chunksize) | ||
|
|
@@ -1173,7 +1180,7 @@ end | |
| jacobian(::ReverseMode, f, x) | ||
|
|
||
| Compute the jacobian of a array-output function `f` using (potentially vector) | ||
| reverse mode. The `chunk` argument optionally denotes the chunk size to use and | ||
| reverse mode. The `chunk` argument optionally denotes the chunk size to use (it can be either `nothing` or `Val(C)` for some `C`) and | ||
|
gdalle marked this conversation as resolved.
Outdated
|
||
| `n_outs` optionally denotes the shape of the array returned by `f` (e.g `size(f(x))`). | ||
|
|
||
| Example: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.