Skip to content

Commit

Permalink
Make ChainRulesCore a weak dependency on Julia >= 1.9 (#85)
Browse files Browse the repository at this point in the history
* Make ChainRulesCore a weak dependency on Julia >= 1.9

* Qualify `normalization`

* Check on nightly if extension works correctly
  • Loading branch information
devmotion authored Mar 7, 2023
1 parent 7d698db commit b2dd69c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
version:
- '1.0'
- '1'
# - 'nightly'
- 'nightly'
os:
- ubuntu-latest
- macOS-latest
Expand Down
11 changes: 9 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
name = "AbstractFFTs"
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
version = "1.2.1"
version = "1.3.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

[extensions]
AbstractFFTsChainRulesCoreExt = "ChainRulesCore"

[compat]
ChainRulesCore = "1"
julia = "^1.0"

[extras]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["ChainRulesTestUtils", "Random", "Test", "Unitful"]
test = ["ChainRulesCore", "ChainRulesTestUtils", "Random", "Test", "Unitful"]
12 changes: 9 additions & 3 deletions src/chainrules.jl → ext/AbstractFFTsChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# ffts
module AbstractFFTsChainRulesCoreExt

using AbstractFFTs
import ChainRulesCore

function ChainRulesCore.frule((_, Δx, _), ::typeof(fft), x::AbstractArray, dims)
y = fft(x, dims)
Δy = fft(Δx, dims)
Expand Down Expand Up @@ -46,7 +50,7 @@ function ChainRulesCore.frule((_, Δx, _), ::typeof(ifft), x::AbstractArray, dim
end
function ChainRulesCore.rrule(::typeof(ifft), x::AbstractArray, dims)
y = ifft(x, dims)
invN = normalization(y, dims)
invN = AbstractFFTs.normalization(y, dims)
project_x = ChainRulesCore.ProjectTo(x)
function ifft_pullback(ȳ)
= project_x(invN .* fft(ChainRulesCore.unthunk(ȳ), dims))
Expand All @@ -66,7 +70,7 @@ function ChainRulesCore.rrule(::typeof(irfft), x::AbstractArray, d::Int, dims)
# compute scaling factors
halfdim = first(dims)
n = size(x, halfdim)
invN = normalization(y, dims)
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],
Expand Down Expand Up @@ -150,3 +154,5 @@ function ChainRulesCore.rrule(::typeof(ifftshift), x::AbstractArray, dims)
end
return y, ifftshift_pullback
end

end # module
7 changes: 4 additions & 3 deletions src/AbstractFFTs.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module AbstractFFTs

import ChainRulesCore

export fft, ifft, bfft, fft!, ifft!, bfft!,
plan_fft, plan_ifft, plan_bfft, plan_fft!, plan_ifft!, plan_bfft!,
rfft, irfft, brfft, plan_rfft, plan_irfft, plan_brfft,
fftdims, fftshift, ifftshift, fftshift!, ifftshift!, Frequencies, fftfreq, rfftfreq

include("definitions.jl")
include("chainrules.jl")

if !isdefined(Base, :get_extension)
include("../ext/AbstractFFTsChainRulesCoreExt.jl")
end

end # module

2 comments on commit b2dd69c

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/79113

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" b2dd69cc19b4dfaeed377fd9f81d7631459b079d
git push origin v1.3.0

Please sign in to comment.