Skip to content

Commit 2986b85

Browse files
authored
Add Mooncake extension for propagate copy_xj fast path and enabled tests (#677)
Signed-off-by: Parvm1102 <parvmittal31757@gmail.com>
1 parent f09fcc4 commit 2986b85

5 files changed

Lines changed: 84 additions & 8 deletions

File tree

‎GNNlib/Project.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2020
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
2121
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
2222
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
23+
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
2324

2425
[extensions]
2526
GNNlibAMDGPUExt = "AMDGPU"
2627
GNNlibCUDAExt = "CUDA"
2728
GNNlibMetalExt = "Metal"
29+
GNNlibMooncakeExt = "Mooncake"
2830

2931
[compat]
3032
AMDGPU = "1"
@@ -35,6 +37,7 @@ GNNGraphs = "1.4"
3537
LinearAlgebra = "1"
3638
Metal = "1.0"
3739
MLUtils = "0.4"
40+
Mooncake = "0.5"
3841
NNlib = "0.9"
3942
Random = "1"
4043
Statistics = "1"

‎GNNlib/ext/GNNlibMooncakeExt.jl‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module GNNlibMooncakeExt
2+
3+
using GNNlib: GNNlib, propagate, copy_xj
4+
using GNNGraphs: GNNGraph, adjacency_matrix
5+
using LinearAlgebra: adjoint
6+
using Base: IEEEFloat
7+
import Mooncake
8+
using Mooncake: CoDual, DefaultCtx, NoRData, @is_primitive
9+
10+
# Mooncake reverse rule for the sparse message-passing fast path
11+
# propagate(copy_xj, g, +, xi, xj, e) == xj * adjacency_matrix(g)
12+
# `A` is constant w.r.t. the inputs, so the pullback is just `dxj = dy * A'`.
13+
# Without it Mooncake differentiates the generic sparse matmul, which is far
14+
# slower than Zygote.
15+
16+
@is_primitive DefaultCtx Tuple{
17+
typeof(propagate),
18+
typeof(copy_xj),
19+
GNNGraph,
20+
typeof(+),
21+
Nothing,
22+
AbstractMatrix{P},
23+
Nothing,
24+
} where {P <: IEEEFloat}
25+
26+
function Mooncake.rrule!!(
27+
::CoDual{typeof(propagate)},
28+
::CoDual{typeof(copy_xj)},
29+
g::CoDual{<:GNNGraph},
30+
::CoDual{typeof(+)},
31+
::CoDual{Nothing},
32+
xj::CoDual{<:AbstractMatrix{P}},
33+
::CoDual{Nothing},
34+
) where {P <: IEEEFloat}
35+
pg = Mooncake.primal(g)
36+
pxj = Mooncake.primal(xj)
37+
A = adjacency_matrix(pg, P; weighted = false)
38+
y = pxj * A
39+
res = Mooncake.zero_fcodual(y)
40+
function propagate_copy_xj_add_pullback!!(::NoRData)
41+
dy = Mooncake.tangent(res)
42+
dxj = Mooncake.tangent(xj)
43+
dxj .+= dy * adjoint(A)
44+
return NoRData(), NoRData(), Mooncake.zero_rdata(pg),
45+
NoRData(), NoRData(), NoRData(), NoRData()
46+
end
47+
return res, propagate_copy_xj_add_pullback!!
48+
end
49+
50+
end # module

‎GNNlib/test/Project.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ GNNlib = "a6a84749-d869-43f8-aacc-be26a1996e48"
77
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
88
MLDataDevices = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
99
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
10+
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
1011
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
1112
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1213
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

‎GNNlib/test/msgpass.jl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,22 @@ end
132132
@testset "copy_xj +" begin
133133
for g in TEST_GRAPHS
134134
f(g, x) = propagate(copy_xj, g, +, xj = x)
135-
test_gradients(f, g, g.x; test_grad_f=false)
135+
test_gradients(f, g, g.x; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
136136
end
137137
end
138138

139139
@testset "copy_xj mean" begin
140140
for g in TEST_GRAPHS
141141
f(g, x) = propagate(copy_xj, g, mean, xj = x)
142-
test_gradients(f, g, g.x; test_grad_f=false)
142+
test_gradients(f, g, g.x; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
143143
end
144144
end
145145

146146
@testset "e_mul_xj +" begin
147147
for g in TEST_GRAPHS
148148
e = rand(Float32, size(g.x, 1), g.num_edges)
149149
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
150-
test_gradients(f, g, g.x, e; test_grad_f=false)
150+
test_gradients(f, g, g.x, e; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
151151
end
152152
end
153153

@@ -158,7 +158,7 @@ end
158158
g = set_edge_weight(g, w)
159159
return propagate(w_mul_xj, g, +, xj = x)
160160
end
161-
test_gradients(f, g, g.x, w; test_grad_f=false)
161+
test_gradients(f, g, g.x, w; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
162162
end
163163
end
164164
end

‎GNNlib/test/test_module.jl‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ using FiniteDifferences: FiniteDifferences
4242
using Zygote: Zygote
4343
using Flux: Flux
4444

45+
# Mooncake.jl requires Julia >= 1.12
46+
const TEST_MOONCAKE = VERSION >= v"1.12"
47+
if TEST_MOONCAKE
48+
import Mooncake
49+
end
50+
4551
# from this module
4652
export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS,
4753
test_gradients, finitediff_withgradient,
48-
check_equal_leaves, gpu_backend
54+
check_equal_leaves, gpu_backend, TEST_MOONCAKE
4955

5056

5157
const D_IN = 3
@@ -81,12 +87,13 @@ function test_gradients(
8187
test_grad_f = true,
8288
test_grad_x = true,
8389
compare_finite_diff = true,
90+
test_mooncake = false,
8491
loss = (f, g, xs...) -> mean(f(g, xs...)),
8592
)
8693

87-
if !test_gpu && !compare_finite_diff
88-
error("You should either compare finite diff vs CPU AD \
89-
or CPU AD vs GPU AD.")
94+
if !test_gpu && !compare_finite_diff && !test_mooncake
95+
error("You should either compare finite diff vs CPU AD, \
96+
CPU AD vs GPU AD, or test Mooncake AD.")
9097
end
9198

9299
## Let's make sure first that the forward pass works.
@@ -115,6 +122,14 @@ function test_gradients(
115122
check_equal_leaves(g, g_fd; rtol, atol)
116123
end
117124

125+
if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals
126+
# Mooncake gradient with respect to input via Flux integration, compared against Zygote.
127+
loss_mc_x = (xs...) -> loss(f, graph, xs...)
128+
y_mc, g_mc = Flux.withgradient(loss_mc_x, Flux.AutoMooncake(), xs...)
129+
@assert isapprox(y, y_mc; rtol, atol)
130+
check_equal_leaves(g, g_mc; rtol, atol)
131+
end
132+
118133
if test_gpu
119134
# Zygote gradient with respect to input on GPU.
120135
y_gpu, g_gpu = Zygote.withgradient((xs...) -> loss(f_gpu, graph_gpu, xs...), xs_gpu...)
@@ -138,6 +153,13 @@ function test_gradients(
138153
check_equal_leaves(g, g_fd; rtol, atol)
139154
end
140155

156+
if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals
157+
# Mooncake gradient with respect to f via Flux integration, compared against Zygote.
158+
y_mc, g_mc = Flux.withgradient(f -> loss(f, graph, xs...), Flux.AutoMooncake(), f)
159+
@assert isapprox(y, y_mc; rtol, atol)
160+
check_equal_leaves(g, g_mc; rtol, atol)
161+
end
162+
141163
if test_gpu
142164
# Zygote gradient with respect to f on GPU.
143165
y_gpu, g_gpu = Zygote.withgradient(f -> loss(f,graph_gpu, xs_gpu...), f_gpu)

0 commit comments

Comments
 (0)