Skip to content

Commit 4deee2b

Browse files
Wire up Mooncake extension test in CI
Add Mooncake to [extras] and [targets.test] so the new `RecursiveArrayToolsMooncakeExt` is actually loaded and exercised in the test suite, and add test/mooncake.jl as a direct unit test for the new `Mooncake.increment_and_get_rdata!(::FData{@NamedTuple{x::T}}, ::NoRData, ::ArrayPartition{P, T})` dispatch: constructs a matching FData and ArrayPartition, calls `increment_and_get_rdata!`, and checks that (a) the in-place accumulation on each inner-array leaf is correct and (b) the method returns `NoRData()`. Also exercises a three-way Float32 ArrayPartition to cover a different eltype and arity. Register the testset in runtests.jl under the Core group. Backport of #575 to v3-backport. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5cbec90 commit 4deee2b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"
8787
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
8888
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
8989
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
90+
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
9091
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
9192
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
9293
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -104,4 +105,4 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
104105
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
105106

106107
[targets]
107-
test = ["Aqua", "FastBroadcast", "ForwardDiff", "KernelAbstractions", "Measurements", "NLsolve", "Pkg", "Polyester", "Random", "SafeTestsets", "SciMLBase", "SparseArrays", "StaticArrays", "Statistics", "StructArrays", "Tables", "Test", "Unitful", "Zygote"]
108+
test = ["Aqua", "FastBroadcast", "ForwardDiff", "KernelAbstractions", "Measurements", "Mooncake", "NLsolve", "Pkg", "Polyester", "Random", "SafeTestsets", "SciMLBase", "SparseArrays", "StaticArrays", "Statistics", "StructArrays", "Tables", "Test", "Unitful", "Zygote"]

test/mooncake.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using RecursiveArrayTools, Mooncake, Test
2+
3+
# Regression test for the `RecursiveArrayToolsMooncakeExt` dispatch that
4+
# lets Mooncake's `@from_chainrules`/`@from_rrule` accumulator handle an
5+
# `ArrayPartition` cotangent returned by an upstream ChainRule (e.g.
6+
# SciMLSensitivity's `_concrete_solve_adjoint` for a `SecondOrderODEProblem`).
7+
# Without the extension, the call below fell through to Mooncake's generic
8+
# error path:
9+
#
10+
# ArgumentError: The fdata type Mooncake.FData{@NamedTuple{x::Tuple{Vector{Float64}, Vector{Float64}}}},
11+
# rdata type Mooncake.NoRData, and tangent type
12+
# RecursiveArrayTools.ArrayPartition{Float64, Tuple{Vector{Float64}, Vector{Float64}}}
13+
# combination is not supported with @from_chainrules or @from_rrule.
14+
15+
@testset "ArrayPartition increment_and_get_rdata!" begin
16+
@test Base.get_extension(RecursiveArrayTools, :RecursiveArrayToolsMooncakeExt) !==
17+
nothing
18+
19+
# Tangent produced by an upstream ChainRule.
20+
t = ArrayPartition([1.0, 2.0], [3.0, 4.0])
21+
# Pre-existing FData that the method should accumulate into in place.
22+
f = Mooncake.FData((x = ([10.0, 20.0], [30.0, 40.0]),))
23+
24+
r = Mooncake.increment_and_get_rdata!(f, Mooncake.NoRData(), t)
25+
26+
@test r === Mooncake.NoRData()
27+
@test f.data.x[1] == [11.0, 22.0]
28+
@test f.data.x[2] == [33.0, 44.0]
29+
30+
# Three-way partition with Float32 leaves — exercises the inner
31+
# per-leaf dispatch on a different eltype and arity.
32+
t32 = ArrayPartition(Float32[1, 2], Float32[3, 4, 5], Float32[6])
33+
f32 = Mooncake.FData((x = (Float32[10, 20], Float32[30, 40, 50], Float32[60]),))
34+
r32 = Mooncake.increment_and_get_rdata!(f32, Mooncake.NoRData(), t32)
35+
@test r32 === Mooncake.NoRData()
36+
@test f32.data.x[1] == Float32[11, 22]
37+
@test f32.data.x[2] == Float32[33, 44, 55]
38+
@test f32.data.x[3] == Float32[66]
39+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ end
3535
@time @safetestset "StaticArrays Tests" include("copy_static_array_test.jl")
3636
@time @safetestset "Linear Algebra Tests" include("linalg.jl")
3737
@time @safetestset "Adjoint Tests" include("adjoints.jl")
38+
@time @safetestset "Mooncake Tests" include("mooncake.jl")
3839
@time @safetestset "Measurement Tests" include("measurements.jl")
3940
end
4041

0 commit comments

Comments
 (0)