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

Fix rrule for rfft and ifft for CuArray #96

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions ext/AbstractFFTsChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ 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)
scale = reshape(
[i == 1 || (i == n && 2 * (i - 1) == d) ? 1 : 2 for i in 1:n],
ntuple(i -> i == first(dims) ? n : 1, Val(ndims(x))),
)

project_x = ChainRulesCore.ProjectTo(x)
function rfft_pullback(ȳ)
x̄ = project_x(brfft(ChainRulesCore.unthunk(ȳ) ./ scale, d, dims))
dY = ChainRulesCore.unthunk(ȳ)
# apply scaling; below approach is for GPU CuArray compatibility, see PR #96
dY_scaled = similar(dY)
dY_scaled .= dY ./ 2
# assign view to a separate variable before assignment, to support Julia <1.2
# see https://github.com/JuliaLang/julia/issues/31295
v = selectdim(dY_scaled, halfdim, 1)
v .*= 2
if 2 * (n - 1) == d
v = selectdim(dY_scaled, halfdim, n)
v .*= 2
end
x̄ = project_x(brfft(dY_scaled, d, dims))
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent()
end
return y, rfft_pullback
Expand Down Expand Up @@ -67,19 +74,25 @@ 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)
twoinvN = 2 * invN
scale = reshape(
[i == 1 || (i == n && 2 * (i - 1) == d) ? invN : twoinvN for i in 1:n],
ntuple(i -> i == first(dims) ? n : 1, Val(ndims(x))),
)

project_x = ChainRulesCore.ProjectTo(x)
function irfft_pullback(ȳ)
x̄ = project_x(scale .* rfft(real.(ChainRulesCore.unthunk(ȳ)), dims))
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims)
# apply scaling; below approach is for GPU CuArray compatibility, see PR #96
dX_scaled = similar(dX)
dX_scaled .= dX .* invN .* 2
# assign view to a separate variable before assignment, to support Julia <1.2
# see https://github.com/JuliaLang/julia/issues/31295
v = selectdim(dX_scaled, halfdim, 1)
v ./= 2
if 2 * (n - 1) == d
v = selectdim(dX_scaled, halfdim, n)
v ./= 2
end
x̄ = project_x(dX_scaled)
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent()
end
return y, irfft_pullback
Expand Down Expand Up @@ -108,17 +121,24 @@ 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)
scale = reshape(
[i == 1 || (i == n && 2 * (i - 1) == d) ? 1 : 2 for i in 1:n],
ntuple(i -> i == first(dims) ? n : 1, Val(ndims(x))),
)

project_x = ChainRulesCore.ProjectTo(x)
function brfft_pullback(ȳ)
x̄ = project_x(scale .* rfft(real.(ChainRulesCore.unthunk(ȳ)), dims))
dX = rfft(real.(ChainRulesCore.unthunk(ȳ)), dims)
# apply scaling; below approach is for GPU CuArray compatibility, see PR #96
dX_scaled = similar(dX)
dX_scaled .= dX .* 2
# assign view to a separate variable before assignment, to support Julia <1.2
# see https://github.com/JuliaLang/julia/issues/31295
v = selectdim(dX_scaled, halfdim, 1)
Copy link
Contributor

@gaurav-arya gaurav-arya Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a comment above this line saying something like like # assign view to a separate variable before assignment, to support Julia <1.2?

v ./= 2
if 2 * (n - 1) == d
v = selectdim(dX_scaled, halfdim, n)
v ./= 2
end
x̄ = project_x(dX_scaled)
return ChainRulesCore.NoTangent(), x̄, ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent()
end
return y, brfft_pullback
Expand Down