-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fix rrule
for rfft
and ifft
for CuArray
#96
Changes from 1 commit
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 |
---|---|---|
|
@@ -26,19 +26,24 @@ end | |
function ChainRulesCore.rrule(::typeof(rfft), x::AbstractArray{<:Real}, dims) | ||
y = rfft(x, dims) | ||
|
||
# compute scaling factors | ||
halfdim = first(dims) | ||
d = size(x, halfdim) | ||
n = size(y, halfdim) | ||
|
||
project_x = ChainRulesCore.ProjectTo(x) | ||
function rfft_pullback(ȳ) | ||
dY = ChainRulesCore.unthunk(ȳ) ./ 2 | ||
selectdim(dY, halfdim, 1) .*= 2 | ||
dY = ChainRulesCore.unthunk(ȳ) | ||
# apply scaling | ||
dY_scaled = similar(dY) | ||
dY_scaled .= dY | ||
dY_scaled ./= 2 | ||
v = selectdim(dY_scaled, halfdim, 1) | ||
v .*= 2 | ||
if 2 * (n - 1) == d | ||
selectdim(dY, halfdim, n) .*= 2 | ||
v = selectdim(dY_scaled, halfdim, n) | ||
v .*= 2 | ||
end | ||
x̄ = project_x(brfft(dY, d, dims)) | ||
x̄ = project_x(brfft(dY_scaled, d, dims)) | ||
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent() | ||
end | ||
return y, rfft_pullback | ||
|
@@ -68,19 +73,24 @@ end | |
function ChainRulesCore.rrule(::typeof(irfft), x::AbstractArray, d::Int, dims) | ||
y = irfft(x, d, dims) | ||
|
||
# compute scaling factors | ||
halfdim = first(dims) | ||
n = size(x, halfdim) | ||
invN = AbstractFFTs.normalization(y, dims) | ||
|
||
project_x = ChainRulesCore.ProjectTo(x) | ||
function irfft_pullback(ȳ) | ||
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims) .* invN .* 2 | ||
selectdim(dX, halfdim, 1) ./= 2 | ||
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims) | ||
# apply scaling | ||
dX_scaled = similar(dX) | ||
dX_scaled .= dX | ||
dX_scaled .*= invN .* 2 | ||
v = selectdim(dX_scaled, halfdim, 1) | ||
v ./= 2 | ||
if 2 * (n - 1) == d | ||
selectdim(dX, halfdim, n) ./= 2 | ||
v = selectdim(dX_scaled, halfdim, n) | ||
v ./= 2 | ||
end | ||
x̄ = project_x(dX) | ||
x̄ = project_x(dX_scaled) | ||
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent() | ||
end | ||
return y, irfft_pullback | ||
|
@@ -109,18 +119,23 @@ end | |
function ChainRulesCore.rrule(::typeof(brfft), x::AbstractArray, d::Int, dims) | ||
y = brfft(x, d, dims) | ||
|
||
# compute scaling factors | ||
halfdim = first(dims) | ||
n = size(x, halfdim) | ||
|
||
project_x = ChainRulesCore.ProjectTo(x) | ||
function brfft_pullback(ȳ) | ||
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims) .* 2 | ||
selectdim(dX, halfdim, 1) ./= 2 | ||
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims) | ||
# apply scaling | ||
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. maybe also add a comment saying something like |
||
dX_scaled = similar(dX) | ||
dX_scaled .= dX | ||
dX_scaled .*= 2 | ||
v = selectdim(dX_scaled, halfdim, 1) | ||
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. maybe a comment above this line saying something like like |
||
v ./= 2 | ||
if 2 * (n - 1) == d | ||
selectdim(dX, halfdim, n) ./= 2 | ||
v = selectdim(dX_scaled, halfdim, n) | ||
v ./= 2 | ||
end | ||
x̄ = project_x(dX) | ||
x̄ = project_x(dX_scaled) | ||
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent() | ||
end | ||
return y, brfft_pullback | ||
|
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.
you could actually fuse this line with the previous one I think, e.g.
dY_scaled .= dY ./ 2
and similarly for the other scalings