-
Notifications
You must be signed in to change notification settings - Fork 36
Fix periodic kernel AD #531
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8329e3c
Get tests for Zygote to pass
simsurace 57a24b4
Also test other AD backends
simsurace 43d1f3a
Add return keyword
simsurace ea5156d
Remove whitespace
simsurace a8baa31
Improve code formatting
simsurace 498cdd8
Add return keyword
simsurace 9e598d5
Improve code formatting
simsurace 8f01fc4
Improve code formatting
simsurace 691921e
Add return keyword
simsurace cb3a7f3
Improve code formatting
simsurace afe209c
Add basic tests for `rrule`s
simsurace fbc1fd6
Add test for `colwise` of `Sinus`
simsurace 73d940d
Cover StaticArrays
simsurace d0c889c
Improve formatting
simsurace 1118c90
Improve formatting
simsurace c15d095
Merge branch 'master' into fix-periodic
simsurace 92b4576
Merge remote-tracking branch 'origin/master' into fix-periodic
simsurace 6add335
Remove whitespace
simsurace 006393d
Merge branch 'master' into fix-periodic
simsurace 16a3d02
Bump version
simsurace 774ac73
Merge branch 'master' into fix-periodic
simsurace 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -112,15 +112,113 @@ end | |
function ChainRulesCore.rrule(s::Sinus, x::AbstractVector, y::AbstractVector) | ||
d = x - y | ||
sind = sinpi.(d) | ||
abs2_sind_r = abs2.(sind) ./ s.r | ||
abs2_sind_r = abs2.(sind) ./ s.r .^ 2 | ||
val = sum(abs2_sind_r) | ||
gradx = twoπ .* cospi.(d) .* sind ./ (s.r .^ 2) | ||
gradx = twoπ .* cospi.(d) .* sind ./ s.r .^ 2 | ||
function evaluate_pullback(Δ::Any) | ||
return (r=-2Δ .* abs2_sind_r,), Δ * gradx, -Δ * gradx | ||
r̄ = -2Δ .* abs2_sind_r ./ s.r | ||
s̄ = ChainRulesCore.Tangent{typeof(s)}(; r=r̄) | ||
return s̄, Δ * gradx, -Δ * gradx | ||
end | ||
return val, evaluate_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule( | ||
::typeof(Distances.pairwise), d::Sinus, x::AbstractMatrix; dims=2 | ||
) | ||
project_x = ProjectTo(x) | ||
function pairwise_pullback(z̄) | ||
Δ = unthunk(z̄) | ||
n = size(x, dims) | ||
x̄ = collect(zero(x)) | ||
r̄ = zero(d.r) | ||
if dims == 1 | ||
for j in 1:n, i in 1:n | ||
xi = view(x, i, :) | ||
xj = view(x, j, :) | ||
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- xj) .* cospi.(xi .- xj) ./ d.r .^ 2 | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- xj) .^ 2 ./ d.r .^ 3 | ||
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. This assumes that rbar supports 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. I think in this case, it is because the field of |
||
x̄[i, :] += ds | ||
x̄[j, :] -= ds | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
elseif dims == 2 | ||
for j in 1:n, i in 1:n | ||
xi = view(x, :, i) | ||
xj = view(x, :, j) | ||
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- xj) .* cospi.(xi .- xj) ./ d.r .^ 2 | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- xj) .^ 2 ./ d.r .^ 3 | ||
x̄[:, i] += ds | ||
x̄[:, j] -= ds | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄) | ||
return NoTangent(), d̄, @thunk(project_x(x̄)) | ||
end | ||
return Distances.pairwise(d, x; dims), pairwise_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule( | ||
::typeof(Distances.pairwise), d::Sinus, x::AbstractMatrix, y::AbstractMatrix; dims=2 | ||
) | ||
project_x = ProjectTo(x) | ||
project_y = ProjectTo(y) | ||
function pairwise_pullback(z̄) | ||
Δ = unthunk(z̄) | ||
n = size(x, dims) | ||
m = size(y, dims) | ||
x̄ = collect(zero(x)) | ||
ȳ = collect(zero(y)) | ||
r̄ = zero(d.r) | ||
if dims == 1 | ||
for j in 1:m, i in 1:n | ||
xi = view(x, i, :) | ||
yj = view(y, j, :) | ||
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- yj) .* cospi.(xi .- yj) ./ d.r .^ 2 | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- yj) .^ 2 ./ d.r .^ 3 | ||
x̄[i, :] += ds | ||
ȳ[j, :] -= ds | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
elseif dims == 2 | ||
for j in 1:m, i in 1:n | ||
xi = view(x, :, i) | ||
yj = view(y, :, j) | ||
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- yj) .* cospi.(xi .- yj) ./ d.r .^ 2 | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- yj) .^ 2 ./ d.r .^ 3 | ||
x̄[:, i] += ds | ||
ȳ[:, j] -= ds | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄) | ||
return NoTangent(), d̄, @thunk(project_x(x̄)), @thunk(project_y(ȳ)) | ||
end | ||
return Distances.pairwise(d, x, y; dims), pairwise_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule( | ||
::typeof(Distances.colwise), d::Sinus, x::AbstractMatrix, y::AbstractMatrix | ||
) | ||
project_x = ProjectTo(x) | ||
project_y = ProjectTo(y) | ||
function colwise_pullback(z̄) | ||
Δ = unthunk(z̄) | ||
n = size(x, 2) | ||
x̄ = collect(zero(x)) | ||
ȳ = collect(zero(y)) | ||
r̄ = zero(d.r) | ||
for i in 1:n | ||
xi = view(x, :, i) | ||
yi = view(y, :, i) | ||
ds = twoπ .* Δ[i] .* sinpi.(xi .- yi) .* cospi.(xi .- yi) ./ d.r .^ 2 | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r̄ .-= 2 .* Δ[i] .* sinpi.(xi .- yi) .^ 2 ./ d.r .^ 3 | ||
x̄[:, i] += ds | ||
ȳ[:, i] -= ds | ||
simsurace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄) | ||
return NoTangent(), d̄, @thunk(project_x(x̄)), @thunk(project_y(ȳ)) | ||
end | ||
return Distances.colwise(d, x, y), colwise_pullback | ||
end | ||
|
||
## Reverse Rules for matrix wrappers | ||
|
||
function ChainRulesCore.rrule(::Type{<:ColVecs}, X::AbstractMatrix) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.