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 frule for static array constructor that converts eltype #275

Merged
merged 2 commits into from
Mar 5, 2024
Merged
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
10 changes: 8 additions & 2 deletions src/extra_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,14 @@ end
Base.view(t::Tangent{T}, inds) where T<:SVector = view(T(ChainRulesCore.backing(t.data)), inds)
Base.getindex(t::Tangent{<:SVector, <:NamedTuple}, ind::Int) = ChainRulesCore.backing(t.data)[ind]

function ChainRules.frule((_, ∂x), ::Type{SArray{S, T, N, L}}, x::NTuple{L,Any}) where {S, T, N, L}
SArray{S, T, N, L}(x), SArray{S}(∂x)
function ChainRules.frule(
(_, ∂x)::Tuple{Any, Tangent{TUP}},
staticfloat marked this conversation as resolved.
Show resolved Hide resolved
::Type{SArray{S, T, N, L}},
x::TUP,
) where {L, TUP<:NTuple{L, Number}, S, T<:Number, N}
y = SArray{S, T, N, L}(x)
∂y = SArray{S, T, N, L}(ChainRulesCore.backing(∂x))
return y, ∂y
end

@ChainRulesCore.non_differentiable StaticArrays.promote_tuple_eltype(T)
Expand Down
35 changes: 35 additions & 0 deletions test/extra_rules.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Diffractor
using StaticArrays
using ChainRulesCore
using Test

@testset "StaticArrays constructor" begin
#frule(::Tuple{ChainRulesCore.NoTangent, ChainRulesCore.Tangent{Tuple{Int64, Vararg{Float64, 9}}, Tuple{Int64, Vararg{Float64, 9}}}}, ::Type{StaticArraysCore.SVector{10, Float64}}, x::Tuple{Int64, Vararg{Float64, 9}})
# @ Diffractor ~/.julia/packages/Diffractor/yCsbI/src/extra_rules.jl:183

@testset "homogenious type" begin
x = (10.0, 20.0, 30.0)
ẋ = zero_tangent(x)
y, ẏ = frule((NoTangent(), ẋ), StaticArraysCore.SVector{3, Float64}, x)
@test y == @SVector [10.0, 20.0, 30.0]
@test ẏ == @SVector [0.0, 0.0, 0.0]
end

@testset "convertable type" begin
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this case different than the first?

Copy link
Member Author

Choose a reason for hiding this comment

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

The first element of x is an Int rather than an Float64

x::Tuple{Int, Float64, Float64} = (10, 20.0, 30.0)
ẋ = zero_tangent(x)
y, ẏ = frule((NoTangent(), ẋ), StaticArraysCore.SVector{3, Float64}, x)
# all are float
@test y == @SVector [10.0, 20.0, 30.0]
@test ẏ == @SVector [0.0, 0.0, 0.0]
end

@testset "convertable type with ZeroTangent()" begin
x = (10, 20.0, 30.0)
ẋ = Tangent{typeof(x)}(ZeroTangent(), 1.0, 2.0)
y, ẏ = frule((NoTangent(), ẋ), StaticArraysCore.SVector{3, Float64}, x)
# all are float
@test y == @SVector [10.0, 20.0, 30.0]
@test ẏ == @SVector [0.0, 1.0, 2.0]
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const bwd = Diffractor.PrimeDerivativeBack
@testset verbose=true "Diffractor.jl" begin # overall testset, ensures all tests run

@testset "$file" for file in (
"extra_rules.jl"
"stage2_fwd.jl",
"tangent.jl",
"forward_diff_no_inf.jl",
Expand Down
Loading