Skip to content

Fix GATConv and GATv2Conv on graphs without edges or nodes - #638

Merged
CarloLucibello merged 5 commits into
JuliaGraphs:masterfrom
lenianiva:gat-conv
Nov 29, 2026
Merged

CarloLucibello merged 5 commits into
JuliaGraphs:masterfrom
lenianiva:gat-conv

Conversation

@lenianiva

@lenianiva lenianiva commented Jan 18, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #637.

Fixes GATConv and GATv2Conv on graphs with no edges or no nodes, which is common in heterographs with empty relations:

  • Forward pass: the final reshape(x, :, size(x, 3)) failed when there are no destination nodes. The output is now reshaped to (out * heads, :).
  • Gradient with no edges: Zygote's broadcast pullback returns wrongly shaped cotangents for zero-length arrays (Allow pullback through 0-length arrays in unbroadcast FluxML/Zygote.jl#1530). The attention step is now skipped when there are no edges, since its output is zero anyway. No ChainRulesCore patch is needed.
  • GATConv gradient on bipartite input (xj, xi): failed with a DimensionMismatch on Zygote < 0.7.17, which mixed up the cotangents of the xi !== xj branch (fixed upstream in Sum the cotangents of a branch's arguments only when it is taken FluxML/Zygote.jl#1747). The shared vs. separate projection is now chosen by dispatch on the input type. This works on any Zygote version and still applies dense_x only once on homogeneous graphs.

Changelog entry (GNNlib, Fixed): GATConv and GATv2Conv no longer fail on graphs, or heterograph relations, without edges or without nodes. The GATConv gradient on bipartite inputs no longer fails with Zygote < 0.7.17.

🧔‍♂️🤖 Assisted by Claude Opus 5.5

@lenianiva

lenianiva commented Jan 18, 2026 •

Copy link
Copy Markdown
Contributor Author

How would this work if bias is set to false?

Also, we need the same patch on gatv2. Simply adding a new conditional in gat_conv only solves the solution for inference. During training it does not work for some reason.

Comment thread GNNlib/src/layers/conv.jl Resolved
@CarloLucibello

Copy link
Copy Markdown
Member

regarding the wrong gradient shape for empty array, do you have any clue why it is happening? ideally the chainrule's projection shouldn't be patched, it should just receive a dx in the correct shape.

@lenianiva

lenianiva commented Jan 19, 2026 •

Copy link
Copy Markdown
Contributor Author

regarding the wrong gradient shape for empty array, do you have any clue why it is happening? ideally the chainrule's projection shouldn't be patched, it should just receive a dx in the correct shape.

I have absolutely no idea. A hint there is the error only occurs for 0-sized arrays. I think it has something to do with computing gradients and is outside the scope of this package.

Edit: Maybe its related to FluxML/Flux.jl#2648 where processing zero-sized array doesn't override an existing cache. I searched Flux.jl, ChainRules.jl (and core), and Zygote.jl and I could not find any suspicious isempty calls. However in the first stacktrace in this issue, the last entry in ChainRules.jl is

https://github.com/JuliaDiff/ChainRules.jl/blob/65410e5b17f56b5ffcc1136fa646957cc37338bb/src/rulesets/Base/indexing.jl#L62

and there is a specific bypass used for AbstractZero. Maybe this is the problem.

@lenianiva

Copy link
Copy Markdown
Contributor Author

regarding the wrong gradient shape for empty array, do you have any clue why it is happening? ideally the chainrule's projection shouldn't be patched, it should just receive a dx in the correct shape.

It could be because of the : in some array shapes. The : operator doesn't work well with zero-sized arrays.

@lenianiva
lenianiva requested a review from CarloLucibello May 2, 2026 23:24
@CarloLucibello

Copy link
Copy Markdown
Member

could you add the corresponding tests?

@lenianiva

lenianiva commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

could you add the corresponding tests?

I may have to fix the empty array problem without hacks to ChainRulesCore

Edit: As is, the fix requires a custom version of ChainRulesCore: JuliaDiff/ChainRulesCore.jl#702

@lenianiva
lenianiva force-pushed the gat-conv branch 2 times, most recently from 801e0d9 to a5651b9 Compare September 3, 2026 06:31
@lenianiva

lenianiva commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

could you add the corresponding tests?

I have added the unit test, but theres something going on with coverage of backpropagation code.

@priyanka4271

Copy link
Copy Markdown

could you add the corresponding tests?

@CarloLucibello

Sorry for messaging here i am new to julia and wants to start my contribution for Gsoc2027 can you help me where i can start?

lenianiva and others added 5 commits November 29, 2026 14:37
Choose shared vs. separate source/destination projections by dispatch
on the input type: this avoids the `xi !== xj` branch that Zygote < 0.7.17
differentiates wrongly, without computing dense_x twice on homogeneous
graphs. Drop the explicit stack sizes, which `reshape(_, chout, heads, :)`
already handles for zero nodes, and add tests on graphs without edges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CarloLucibello CarloLucibello changed the title fix: Gate empty result in GATConv Fix GATConv and GATv2Conv on graphs without edges or nodes Nov 29, 2026
@CarloLucibello
CarloLucibello merged commit 406aed1 into JuliaGraphs:master Nov 29, 2026
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GATConv doesn't work on hetero graphs with empty edge arrays or during backpropagation

3 participants