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

Increasing robustness of adjoint plan optimizations #123

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,11 @@
pinv = inv(p)
# Optimization: when pinv is a ScaledPlan, check if we can avoid a loop over x.
# Even if not, ensure that we do only one pass by combining the normalization with the plan.
RT = AbstractArray{<:T} # canonicalize eltype of returned array to be extra safe about type stability
if pinv isa ScaledPlan && pinv.scale == N
return pinv.p * x
return convert(RT, pinv.p * x)
else
return (1/N * pinv) * x
return convert(RT, (inv(N) * pinv) * x)

Check warning on line 690 in src/definitions.jl

View check run for this annotation

Codecov / codecov/patch

src/definitions.jl#L690

Added line #L690 was not covered by tests
gaurav-arya marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand All @@ -698,7 +699,7 @@
pinv = inv(p)
n = size(pinv, halfdim)
# Optimization: when pinv is a ScaledPlan, fuse the scaling into our map to ensure we do not loop over x twice.
scale = pinv isa ScaledPlan ? pinv.scale / 2N : 1 / 2N
scale = pinv isa ScaledPlan ? pinv.scale / 2N : one(pinv.scale) / 2N
gaurav-arya marked this conversation as resolved.
Show resolved Hide resolved
twoscale = 2 * scale
unscaled_pinv = pinv isa ScaledPlan ? pinv.p : pinv
y = map(x, CartesianIndices(x)) do xj, j
Expand All @@ -721,7 +722,7 @@
pinv = inv(p)
d = size(pinv, halfdim)
# Optimization: when pinv is a ScaledPlan, fuse the scaling into our map to ensure we do not loop over x twice.
scale = pinv isa ScaledPlan ? pinv.scale / N : 1 / N
scale = pinv isa ScaledPlan ? pinv.scale / N : one(pinv.scale) / N
gaurav-arya marked this conversation as resolved.
Show resolved Hide resolved
twoscale = 2 * scale
unscaled_pinv = pinv isa ScaledPlan ? pinv.p : pinv
y = unscaled_pinv * x
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ end
end
end

@testset "Adjoint plan on single-precision" begin
# fft
p = plan_fft(zeros(ComplexF32, 3))
u = rand(ComplexF32, 3)
@test eltype(p' * (p * u)) == eltype(u)
# rfft
p = plan_rfft(zeros(Float32, 3))
u = rand(Float32, 3)
@test eltype(p' * (p * u)) == eltype(u)
# brfft
p = plan_brfft(zeros(ComplexF32, 3), 5)
u = rand(ComplexF32, 3)
@test eltype(p' * (p * u)) == eltype(u)
end

@testset "ChainRules" begin
@testset "shift functions" begin
for x in (randn(3), randn(3, 4), randn(3, 4, 5))
Expand Down
Loading