Skip to content

Commit

Permalink
Bring over tests from ChainRulesCore (#1231)
Browse files Browse the repository at this point in the history
* Bring over tests from ChainRulesCore

* add missing begin

Co-authored-by: Yuto Horikawa <[email protected]>

* use axes to store the axes and not size

* Update ext/StaticArraysChainRulesCoreExt.jl

Co-authored-by: Frames White <[email protected]>

---------

Co-authored-by: Frames White <[email protected]>
Co-authored-by: Frames White <[email protected]>
Co-authored-by: Yuto Horikawa <[email protected]>
Co-authored-by: Mateusz Baran <[email protected]>
  • Loading branch information
5 people committed Jan 30, 2024
1 parent f06af93 commit 48dec2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 4 additions & 2 deletions ext/StaticArraysChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ end

# Project SArray to SArray
function ProjectTo(x::SArray{S, T}) where {S, T}
return ProjectTo{SArray}(; element = CRC._eltype_projectto(T), axes = Size(x))
# We have a axes field because it is expected by other ProjectTo's like the one for Transpose
return ProjectTo{SArray}(; element = CRC._eltype_projectto(T), axes = axes(x),
size = Size(x))
end

@inline _sarray_from_array(::Size{T}, dx::AbstractArray) where {T} = SArray{Tuple{T...}}(dx)

(project::ProjectTo{SArray})(dx::AbstractArray) = _sarray_from_array(project.axes, dx)
(project::ProjectTo{SArray})(dx::AbstractArray) = _sarray_from_array(project.size, dx)

# Adjoint for SArray constructor
function rrule(::Type{T}, x::Tuple) where {T <: SArray}
Expand Down
21 changes: 19 additions & 2 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
using StaticArrays, ChainRulesCore, ChainRulesTestUtils, JLArrays, Test
using StaticArrays, ChainRulesCore, ChainRulesTestUtils, JLArrays, LinearAlgebra, Test

@testset "Chain Rules Integration" begin
@testset "ChainRules Integration" begin
@testset "Projection" begin
# There is no code for this, but when argument isa StaticArray, axes(x) === axes(dx)
# implies a check, and reshape will wrap a Vector into a static SizedVector:
pstat = ProjectTo(SA[1, 2, 3])
@test axes(pstat(rand(3))) === (SOneTo(3),)

# This recurses into structured arrays:
pst = ProjectTo(transpose(SA[1, 2, 3]))
@test axes(pst(rand(1,3))) === (SOneTo(1), SOneTo(3))
@test pst(rand(1,3)) isa Transpose

# When the argument is an ordinary Array, static gradients are allowed to pass,
# like FillArrays. Collecting to an Array would cost a copy.
pvec3 = ProjectTo([1, 2, 3])
@test pvec3(SA[1, 2, 3]) isa StaticArray
end

@testset "Constructor rrules" begin
test_rrule(SMatrix{1, 4}, (1.0, 1.0, 1.0, 1.0))
test_rrule(SMatrix{4, 1}, (1.0, 1.0, 1.0, 1.0))
test_rrule(SMatrix{2, 2}, (1.0, 1.0, 1.0, 1.0))
Expand Down

2 comments on commit 48dec2c

@hyrodium
Copy link
Collaborator

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/99830

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 v1.9.2 -m "<description of version>" 48dec2c7e420f02d6085d346d4cc5125b204e022
git push origin v1.9.2

Please sign in to comment.