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: disallow complex basis vectors for now #669

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
2 changes: 1 addition & 1 deletion DifferentiationInterface/src/misc/from_primitive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
Loading