Skip to content

Commit

Permalink
fix: disallow complex basis vectors for now (#669)
Browse files Browse the repository at this point in the history
* fix: disallow complex basis vectors for now

* Skip complex tests

* Fix fromprimitive

* Fix basis
  • Loading branch information
gdalle authored Jan 11, 2025
1 parent 9b17e3e commit 240e7e8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DifferentiationInterface/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DifferentiationInterface"
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
authors = ["Guillaume Dalle", "Adrian Hill"]
version = "0.6.28"
version = "0.6.29"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using ReverseDiff:

DI.check_available(::AutoReverseDiff) = true

function DI.basis(::AutoReverseDiff, a::AbstractArray{T}, i) where {T}
function DI.basis(::AutoReverseDiff, a::AbstractArray{T}, i) where {T<:Real}
return DI.OneElement(i, one(T), a)
end

Expand Down
22 changes: 12 additions & 10 deletions DifferentiationInterface/src/first_order/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ end
function prepare_derivative(
f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
pushforward_prep = prepare_pushforward(f, backend, x, (one(x),), contexts...)
pushforward_prep = prepare_pushforward(f, backend, x, (realone(x),), contexts...)
return PushforwardDerivativePrep(pushforward_prep)
end

function prepare_derivative(
f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
pushforward_prep = prepare_pushforward(f!, y, backend, x, (one(x),), contexts...)
pushforward_prep = prepare_pushforward(f!, y, backend, x, (realone(x),), contexts...)
return PushforwardDerivativePrep(pushforward_prep)
end

Expand All @@ -95,7 +95,7 @@ function value_and_derivative(
contexts::Vararg{Context,C},
) where {F,C}
y, ty = value_and_pushforward(
f, prep.pushforward_prep, backend, x, (one(x),), contexts...
f, prep.pushforward_prep, backend, x, (realone(x),), contexts...
)
return y, only(ty)
end
Expand All @@ -109,7 +109,7 @@ function value_and_derivative!(
contexts::Vararg{Context,C},
) where {F,C}
y, _ = value_and_pushforward!(
f, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...
f, (der,), prep.pushforward_prep, backend, x, (realone(x),), contexts...
)
return y, der
end
Expand All @@ -121,7 +121,7 @@ function derivative(
x,
contexts::Vararg{Context,C},
) where {F,C}
ty = pushforward(f, prep.pushforward_prep, backend, x, (one(x),), contexts...)
ty = pushforward(f, prep.pushforward_prep, backend, x, (realone(x),), contexts...)
return only(ty)
end

Expand All @@ -133,7 +133,7 @@ function derivative!(
x,
contexts::Vararg{Context,C},
) where {F,C}
pushforward!(f, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...)
pushforward!(f, (der,), prep.pushforward_prep, backend, x, (realone(x),), contexts...)
return der
end

Expand All @@ -148,7 +148,7 @@ function value_and_derivative(
contexts::Vararg{Context,C},
) where {F,C}
y, ty = value_and_pushforward(
f!, y, prep.pushforward_prep, backend, x, (one(x),), contexts...
f!, y, prep.pushforward_prep, backend, x, (realone(x),), contexts...
)
return y, only(ty)
end
Expand All @@ -163,7 +163,7 @@ function value_and_derivative!(
contexts::Vararg{Context,C},
) where {F,C}
y, _ = value_and_pushforward!(
f!, y, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...
f!, y, (der,), prep.pushforward_prep, backend, x, (realone(x),), contexts...
)
return y, der
end
Expand All @@ -176,7 +176,7 @@ function derivative(
x,
contexts::Vararg{Context,C},
) where {F,C}
ty = pushforward(f!, y, prep.pushforward_prep, backend, x, (one(x),), contexts...)
ty = pushforward(f!, y, prep.pushforward_prep, backend, x, (realone(x),), contexts...)
return only(ty)
end

Expand All @@ -189,7 +189,9 @@ function derivative!(
x,
contexts::Vararg{Context,C},
) where {F,C}
pushforward!(f!, y, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...)
pushforward!(
f!, y, (der,), prep.pushforward_prep, backend, x, (realone(x),), contexts...
)
return der
end

Expand Down
6 changes: 3 additions & 3 deletions DifferentiationInterface/src/misc/from_primitive.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
abstract type FromPrimitive <: AbstractADType end

function basis(fromprim::FromPrimitive, x::AbstractArray, i)
function basis(fromprim::FromPrimitive, x::AbstractArray{<:Real}, i)
return basis(fromprim.backend, x, i)
end

function multibasis(fromprim::FromPrimitive, x::AbstractArray, inds)
function multibasis(fromprim::FromPrimitive, x::AbstractArray{<:Real}, inds)
return multibasis(fromprim.backend, x, inds)
end

check_available(fromprim::FromPrimitive) = check_available(fromprim.backend)
inplace_support(fromprim::FromPrimitive) = inplace_support(fromprim.backend)

function BatchSizeSettings(fromprim::FromPrimitive, x::AbstractArray)
function BatchSizeSettings(fromprim::FromPrimitive, x::AbstractArray{<:Real})
return BatchSizeSettings(fromprim.backend, x)
end

Expand Down
10 changes: 6 additions & 4 deletions DifferentiationInterface/src/utils/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Construct the `i`-th standard basis array in the vector space of `a` with elemen
If an AD backend benefits from a more specialized basis array implementation,
this function can be extended on the backend type.
"""
basis(::AbstractADType, a::AbstractArray, i) = basis(a, i)
basis(::AbstractADType, a::AbstractArray{<:Real}, i) = basis(a, i)

"""
multibasis(backend, a::AbstractArray, inds::AbstractVector)
Expand All @@ -58,16 +58,18 @@ Construct the sum of the `i`-th standard basis arrays in the vector space of `a`
If an AD backend benefits from a more specialized basis array implementation,
this function can be extended on the backend type.
"""
multibasis(::AbstractADType, a::AbstractArray, inds) = multibasis(a, inds)
multibasis(::AbstractADType, a::AbstractArray{<:Real}, inds) = multibasis(a, inds)

function basis(a::AbstractArray{T,N}, i) where {T,N}
function basis(a::AbstractArray{T,N}, i) where {T<:Real,N}
return zero(a) + OneElement(i, one(T), a)
end

function multibasis(a::AbstractArray{T,N}, inds::AbstractVector) where {T,N}
function multibasis(a::AbstractArray{T,N}, inds::AbstractVector) where {T<:Real,N}
seed = zero(a)
for i in inds
seed += OneElement(i, one(T), a)
end
return seed
end

realone(x::Real) = one(x)
6 changes: 3 additions & 3 deletions DifferentiationInterface/test/Back/FiniteDiff/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_differentiation(
@testset verbose = true "Complex number support" begin
backend = AutoSparse(AutoFiniteDiff(); coloring_algorithm=GreedyColoringAlgorithm())
x = float.(1:3) .+ im
@test_nowarn jacobian(identity, backend, x)
@test_nowarn jacobian(copyto!, similar(x), backend, x)
@test_nowarn hessian(sum, backend, x)
@test_skip @test_nowarn jacobian(identity, backend, x)
@test_skip @test_nowarn jacobian(copyto!, similar(x), backend, x)
@test_skip @test_nowarn hessian(sum, backend, x)
end

2 comments on commit 240e7e8

@gdalle
Copy link
Member Author

@gdalle gdalle commented on 240e7e8 Jan 13, 2025

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=DifferentiationInterface

@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/122914

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 DifferentiationInterface-v0.6.29 -m "<description of version>" 240e7e848705cd0d22890d076b8e5fa1e90300a5
git push origin DifferentiationInterface-v0.6.29

Please sign in to comment.