-
Notifications
You must be signed in to change notification settings - Fork 69
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
sort! rules #1000
Merged
Merged
sort! rules #1000
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0e5804
sort! rules
jgreener64 38fcfb4
Sort in augmented primal
jgreener64 99c22a8
sort! rule tests
jgreener64 e8c4185
Batched sort! rule
jgreener64 bd4b40b
Move A \ B rule test
jgreener64 7de6433
Fix after rebase
jgreener64 3b4c25d
Add missing end
jgreener64 9015204
Merge branch 'main' into sort-rule
wsmoses File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module InternalRules | ||
|
||
using Enzyme | ||
using Enzyme.EnzymeRules | ||
using Test | ||
|
||
@testset "Internal rules" begin | ||
function f1(x) | ||
a = [1.0, 3.0, x] | ||
sort!(a) | ||
return a[2] | ||
end | ||
|
||
@test autodiff(Forward, f1, Duplicated(2.0, 1.0))[1] == 1 | ||
@test autodiff(Forward, f1, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=1.0, var"2"=2.0) | ||
@test autodiff(Reverse, f1, Active, Active(2.0))[1][1] == 1 | ||
@test autodiff(Forward, f1, Duplicated(4.0, 1.0))[1] == 0 | ||
@test autodiff(Forward, f1, BatchDuplicated(4.0, (1.0, 2.0)))[1] == (var"1"=0.0, var"2"=0.0) | ||
@test autodiff(Reverse, f1, Active, Active(4.0))[1][1] == 0 | ||
|
||
function f2(x) | ||
a = [1.0, -3.0, -x, -2x, x] | ||
sort!(a; rev=true, lt=(x, y) -> abs(x) < abs(y) || (abs(x) == abs(y) && x < y)) | ||
return sum(a .* [1, 2, 3, 4, 5]) | ||
end | ||
|
||
@test autodiff(Forward, f2, Duplicated(2.0, 1.0))[1] == -3 | ||
@test autodiff(Forward, f2, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=-3.0, var"2"=-6.0) | ||
@test autodiff(Reverse, f2, Active, Active(2.0))[1][1] == -3 | ||
end | ||
|
||
@testset "Linear Solve" begin | ||
A = Float64[2 3; 5 7] | ||
dA = zero(A) | ||
b = Float64[11, 13] | ||
db = zero(b) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Duplicated{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Duplicated(b, db)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Duplicated(A, dA), Duplicated(b, db), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test dA ≈ (-z * transpose(y)) | ||
@test db ≈ z | ||
|
||
db = zero(b) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Const{typeof(A)}, Duplicated{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Const(A), Duplicated(b, db)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Const(A), Duplicated(b, db), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test db ≈ z | ||
|
||
dA = zero(A) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Const{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Const(b)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Duplicated(A, dA), Const(b), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test dA ≈ (-z * transpose(y)) | ||
end | ||
|
||
end # InternalRules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like sortperm itself just makes a list of 1:N, then sorts that in place using the original data as the order: https://github.com/JuliaLang/julia/blob/750df9fb5bede16f321f5d5405943d12aec7b83e/base/sort.jl#L1756
Could we change this to first sort the derivative array in place using the primal array as the order, then do the actual sort on the primal? That way we don't have that temporary