Skip to content

Commit 917700c

Browse files
authored
fix: made the gradient testing more generic (#681)
* fix: made the gradient testing more generic Signed-off-by: Parvm1102 <parvmittal31757@gmail.com> * Refactor gradient testing to utilize AD_BACKENDS instead of TEST_MOONCAKE across various test files Signed-off-by: Parvm1102 <parvmittal31757@gmail.com> * Fix: AD gradients now do not need to be passed every time Signed-off-by: Parvm1102 <parvmittal31757@gmail.com> --------- Signed-off-by: Parvm1102 <parvmittal31757@gmail.com>
1 parent c860586 commit 917700c

8 files changed

Lines changed: 154 additions & 152 deletions

File tree

‎GNNlib/test/msgpass.jl‎

Lines changed: 9 additions & 9 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, test_mooncake=TEST_MOONCAKE)
135+
test_gradients(f, g, g.x; test_grad_f=false)
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, test_mooncake=TEST_MOONCAKE)
142+
test_gradients(f, g, g.x; test_grad_f=false)
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, test_mooncake=TEST_MOONCAKE)
150+
test_gradients(f, g, g.x, e; test_grad_f=false)
151151
end
152152
end
153153

@@ -157,7 +157,7 @@ end
157157
for g in TEST_GRAPHS
158158
e = rand(Float32, g.num_edges)
159159
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
160-
test_gradients(f, g, g.x, e; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
160+
test_gradients(f, g, g.x, e; test_grad_f=false)
161161
end
162162
end
163163

@@ -168,7 +168,7 @@ end
168168
g = set_edge_weight(g, w)
169169
return propagate(w_mul_xj, g, +, xj = x)
170170
end
171-
test_gradients(f, g, g.x, w; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
171+
test_gradients(f, g, g.x, w; test_grad_f=false)
172172
end
173173
end
174174
end
@@ -182,7 +182,7 @@ end
182182
broken = false
183183
f(g, x) = propagate(copy_xj, g, +, xj = x)
184184
@test test_gradients(
185-
f, g, g.x; test_gpu=true, test_grad_f=false, compare_finite_diff=false
185+
f, g, g.x; test_gpu=true, test_grad_f=false
186186
) broken=broken
187187
end
188188
end
@@ -193,7 +193,7 @@ end
193193
broken = false
194194
f(g, x) = propagate(copy_xj, g, mean, xj = x)
195195
@test test_gradients(
196-
f, g, g.x; test_gpu=true, test_grad_f=false, compare_finite_diff=false
196+
f, g, g.x; test_gpu=true, test_grad_f=false
197197
) broken=broken
198198
end
199199
end
@@ -205,7 +205,7 @@ end
205205
e = rand(Float32, size(g.x, 1), g.num_edges)
206206
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
207207
@test test_gradients(
208-
f, g, g.x, e; test_gpu=true, test_grad_f=false, compare_finite_diff=false
208+
f, g, g.x, e; test_gpu=true, test_grad_f=false
209209
) broken=broken
210210
end
211211
end
@@ -221,7 +221,7 @@ end
221221
# broken = get_graph_type(g) == :sparse
222222
broken = true
223223
@test test_gradients(
224-
f, g, g.x, w; test_gpu=true, test_grad_f=false, compare_finite_diff=false
224+
f, g, g.x, w; test_gpu=true, test_grad_f=false
225225
) broken=broken
226226
end
227227
end

‎GNNlib/test/test_module.jl‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ function check_equal_leaves(a, b; rtol=1e-4, atol=1e-4)
7878
@assert equal
7979
end
8080

81+
# Gradient from a generic AD backend, compared against the reference (y, g).
82+
function test_gradients_with_backend(backend, loss_fn, y, g, args...; rtol, atol)
83+
y2, g2 = Flux.withgradient(loss_fn, backend, args...)
84+
@assert isapprox(y, y2; rtol, atol)
85+
check_equal_leaves(g2, g; rtol, atol)
86+
end
87+
8188
function test_gradients(
8289
f,
8390
graph::GNNGraph,
@@ -86,14 +93,15 @@ function test_gradients(
8693
test_gpu = false,
8794
test_grad_f = true,
8895
test_grad_x = true,
89-
compare_finite_diff = true,
90-
test_mooncake = false,
96+
# Reference AD: finite differences for CPU tests, Zygote (on CPU) for GPU tests.
97+
reference = test_gpu ? Flux.AutoZygote() : :finitediff,
98+
ad_backends = test_gpu ? [] : [Flux.AutoZygote(), Flux.AutoMooncake()],
9199
loss = (f, g, xs...) -> mean(f(g, xs...)),
92100
)
93101

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.")
102+
# Mooncake requires Julia >= 1.12 and errors on sparse graph internals.
103+
if !TEST_MOONCAKE || graph.graph isa AbstractSparseMatrix
104+
ad_backends = filter(b -> !(b isa Flux.AutoMooncake), ad_backends)
97105
end
98106

99107
## Let's make sure first that the forward pass works.
@@ -110,24 +118,19 @@ function test_gradients(
110118
end
111119

112120
if test_grad_x
113-
# Zygote gradient with respect to input.
114-
y, g = Zygote.withgradient((xs...) -> loss(f, graph, xs...), xs...)
115-
116-
if compare_finite_diff
121+
# Reference gradient with respect to input.
122+
y, g = if reference === :finitediff
117123
# Cast to Float64 to avoid precision issues.
118124
f64 = f |> Flux.f64
119125
xs64 = xs .|> Flux.f64
120-
y_fd, g_fd = finitediff_withgradient((xs...) -> loss(f64, graph, xs...), xs64...)
121-
@assert isapprox(y, y_fd; rtol, atol)
122-
check_equal_leaves(g, g_fd; rtol, atol)
126+
finitediff_withgradient((xs...) -> loss(f64, graph, xs...), xs64...)
127+
else
128+
Flux.withgradient((xs...) -> loss(f, graph, xs...), reference, xs...)
123129
end
130+
@assert isapprox(l, y; rtol, atol)
124131

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)
132+
for backend in ad_backends
133+
test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol)
131134
end
132135

133136
if test_gpu
@@ -140,24 +143,23 @@ function test_gradients(
140143
end
141144

142145
if test_grad_f
143-
# Zygote gradient with respect to f.
144-
y, g = Zygote.withgradient(f -> loss(f, graph, xs...), f)
145-
146-
if compare_finite_diff
146+
# Reference gradient with respect to f.
147+
y, g = if reference === :finitediff
147148
# Cast to Float64 to avoid precision issues.
148149
f64 = f |> Flux.f64
149150
ps, re = Flux.destructure(f64)
150-
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps),graph, xs...), ps)
151-
g_fd = (re(g_fd[1]),)
152-
@assert isapprox(y, y_fd; rtol, atol)
153-
check_equal_leaves(g, g_fd; rtol, atol)
151+
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps), graph, xs...), ps)
152+
y_fd, (re(g_fd[1]),)
153+
else
154+
Flux.withgradient(f -> loss(f, graph, xs...), reference, f)
154155
end
156+
@assert isapprox(l, y; rtol, atol)
155157

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)
158+
# Skip comparison for parameterless layers (no gradients wrt f).
159+
if !isempty(Flux.trainables(f))
160+
for backend in ad_backends
161+
test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; rtol, atol)
162+
end
161163
end
162164

163165
if test_gpu

‎GraphNeuralNetworks/test/layers/basic.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
Flux.testmode!(gnn)
2020

21-
test_gradients(gnn, g, x, rtol = 1e-5, test_mooncake = false)
21+
test_gradients(gnn, g, x, rtol = 1e-5, ad_backends = [Flux.AutoZygote()])
2222

2323
@testset "constructor with names" begin
2424
m = GNNChain(GCNConv(din => d),
@@ -53,7 +53,7 @@
5353

5454
Flux.trainmode!(gnn)
5555

56-
test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4, test_mooncake = false)
56+
test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4, ad_backends = [Flux.AutoZygote()])
5757
end
5858
end
5959

0 commit comments

Comments
 (0)