From 2f3ac88118b9d88910413605fbe438e9557e2417 Mon Sep 17 00:00:00 2001 From: Simone Carlo Surace Date: Mon, 26 Oct 2026 22:39:31 +0100 Subject: [PATCH] Let `GNNChain` return states of the same structure as the input --- CHANGELOG.md | 29 +++++++++++-------- GNNLux/Project.toml | 2 +- GNNLux/docs/src/index.md | 3 +- .../src/tutorials/graph_classification.md | 2 +- .../src_tutorials/graph_classification.jl | 2 +- GNNLux/src/layers/basic.jl | 5 +++- GNNLux/test/layers/basic.jl | 11 +++++++ 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ae90818b..b7fc82d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,19 +19,10 @@ Entries link to the pull request that introduced them. - Added an `EnzymeCore` package extension marking `scaled_laplacian` inactive for Enzyme, matching its existing `@non_differentiable` ChainRules declaration. Enzyme previously differentiated the Krylov eigensolve inside it and failed; this unblocks `ChebConv`, `GConvGRUCell` and `GConvLSTMCell` ([#706]). - Enzyme can now differentiate through the adjacency-matrix → COO graph conversion: a new internal keyword-free helper `_to_coo_graph` avoids the union-typed keyword handling of the `GNNGraph(g; graph_type)` constructor and of `to_coo`, which Enzyme's type analysis cannot compile. Together with upstream fixes in Enzyme ≥ 0.13.197 this removes the `EnzymeInternalError` crash on `:dense`/`:sparse` graphs ([#703]). -## GNNLux.jl — Unreleased (towards 0.2.0) +## GNNLux.jl — Unreleased (towards 0.3.0) -**Added** -- Added pooling layers (`GlobalPool`, `GlobalAttentionPool`, `TopKPool`) ([#576]). -- `TGCN` now supports non-linear activation functions ([#596]). -- Rewrote the recurrent temporal layers following the Flux frontend's design ([#560]): added the `GNNRecurrence` wrapper and split every temporal layer into a `*Cell` and a full-sequence layer, exporting both — `TGCNCell`/`TGCN`, `GConvGRUCell`/`GConvGRU`, `GConvLSTMCell`/`GConvLSTM`, `DCGRUCell`/`DCGRU`, `EvolveGCNOCell`/`EvolveGCNO`. A layer now consumes a whole sequence at once (an `in × timesteps × num_nodes` array on a `GNNGraph`, or a vector of snapshots on a `TemporalSnapshotsGNNGraph`) and returns the full output sequence ([#696]). - -**Changed** -- Bumped the NNlib, OneHotArrays, StableRNGs, and DocumenterInterLinks compat bounds ([#687], [#686], [#684], [#685]). -- The temporal cells now share their forward-pass math with the Flux frontend via new `GNNlib` functions, and several formulas were corrected in the process (e.g. the `GConvGRU` candidate state, previously `tanh` of the wrong term, and the `GConvLSTM` peephole connections) ([#696]). - -**Removed** -- Removed the `A3TGCN` layer, mirroring its removal from the Flux frontend ([#696]). +**Fixed** +- `GNNChain` now returns its state with the same structure it receives, `(; layers = (; layer_1, ...))`, as the Lux layer interface requires. It previously returned the per-layer states without the `layers` wrapper, so feeding the returned state back into the chain (as `Lux.Training.single_train_step!` does) failed with a `FieldError`. This is breaking for code that worked around the bug by re-wrapping the state as `(; layers = st)`, as the docs examples did: drop the wrapper. ## GNNlib.jl — Unreleased (towards 1.4.1) @@ -44,6 +35,20 @@ Entries link to the pull request that introduced them. - `GNNRecurrence` now hands each cell an indexed time slice instead of an `eachslice` view, matching the Lux frontend. Enzyme's type analysis fails on `SubArray` cell inputs, so this unblocks `DCGRU`, `EvolveGCNO`, `GConvGRU` and `GConvLSTM` under Enzyme ([#707]). - The recurrent temporal cells now delegate their forward-pass math to shared `GNNlib` functions (requires GNNlib ≥ 1.4); the layer behaviour is unchanged ([#696]). +## GNNLux.jl 0.2.0 — 2026-07-22 + +**Added** +- Added pooling layers (`GlobalPool`, `GlobalAttentionPool`, `TopKPool`) ([#576]). +- `TGCN` now supports non-linear activation functions ([#596]). +- Rewrote the recurrent temporal layers following the Flux frontend's design ([#560]): added the `GNNRecurrence` wrapper and split every temporal layer into a `*Cell` and a full-sequence layer, exporting both — `TGCNCell`/`TGCN`, `GConvGRUCell`/`GConvGRU`, `GConvLSTMCell`/`GConvLSTM`, `DCGRUCell`/`DCGRU`, `EvolveGCNOCell`/`EvolveGCNO`. A layer now consumes a whole sequence at once (an `in × timesteps × num_nodes` array on a `GNNGraph`, or a vector of snapshots on a `TemporalSnapshotsGNNGraph`) and returns the full output sequence ([#696]). + +**Changed** +- Bumped the NNlib, OneHotArrays, StableRNGs, and DocumenterInterLinks compat bounds ([#687], [#686], [#684], [#685]). +- The temporal cells now share their forward-pass math with the Flux frontend via new `GNNlib` functions, and several formulas were corrected in the process (e.g. the `GConvGRU` candidate state, previously `tanh` of the wrong term, and the `GConvLSTM` peephole connections) ([#696]). + +**Removed** +- Removed the `A3TGCN` layer, mirroring its removal from the Flux frontend ([#696]). + ## GNNlib.jl 1.4.0 — 2026-07-22 **Added** diff --git a/GNNLux/Project.toml b/GNNLux/Project.toml index 0fb3b2e05..67700794b 100644 --- a/GNNLux/Project.toml +++ b/GNNLux/Project.toml @@ -1,6 +1,6 @@ name = "GNNLux" uuid = "e8545f4d-a905-48ac-a8c4-ca114b98986d" -version = "0.2.0" +version = "0.3.0-DEV" authors = ["Carlo Lucibello and contributors"] [workspace] diff --git a/GNNLux/docs/src/index.md b/GNNLux/docs/src/index.md index b94f92fb1..30066a55f 100644 --- a/GNNLux/docs/src/index.md +++ b/GNNLux/docs/src/index.md @@ -62,7 +62,7 @@ Finally, we use a standard Lux training pipeline to fit our dataset. function custom_loss(model, ps, st, tuple) g,x,y = tuple y_pred,st = model(g, x, ps, st) - return MSELoss()(y_pred, y), (layers = st,), 0 + return MSELoss()(y_pred, y), st, 0 end function train_model!(model, ps, st, train_graphs, test_graphs) @@ -81,7 +81,6 @@ function train_model!(model, ps, st, train_graphs, test_graphs) test_loss =0 for g in test_graphs ŷ, st_ = model(g, g.x, train_state.parameters, st_) - st_ = (layers = st_,) test_loss += MSELoss()(g.y,ŷ) end test_loss = test_loss/length(test_graphs) diff --git a/GNNLux/docs/src/tutorials/graph_classification.md b/GNNLux/docs/src/tutorials/graph_classification.md index e9e2443d6..01201d5f1 100644 --- a/GNNLux/docs/src/tutorials/graph_classification.md +++ b/GNNLux/docs/src/tutorials/graph_classification.md @@ -216,7 +216,7 @@ function custom_loss(model, ps, st, tuple) logitcrossentropy = CrossEntropyLoss(; logits=Val(true)) st = Lux.trainmode(st) ŷ, st = model(g, x, ps, st) - return logitcrossentropy(ŷ, y), (; layers = st), 0 + return logitcrossentropy(ŷ, y), st, 0 end function eval_loss_accuracy(model, ps, st, data_loader) diff --git a/GNNLux/docs/src_tutorials/graph_classification.jl b/GNNLux/docs/src_tutorials/graph_classification.jl index caab4e409..9d19595ce 100644 --- a/GNNLux/docs/src_tutorials/graph_classification.jl +++ b/GNNLux/docs/src_tutorials/graph_classification.jl @@ -132,7 +132,7 @@ function custom_loss(model, ps, st, tuple) logitcrossentropy = CrossEntropyLoss(; logits=Val(true)) st = Lux.trainmode(st) ŷ, st = model(g, x, ps, st) - return logitcrossentropy(ŷ, y), (; layers = st), 0 + return logitcrossentropy(ŷ, y), st, 0 end function eval_loss_accuracy(model, ps, st, data_loader) diff --git a/GNNLux/src/layers/basic.jl b/GNNLux/src/layers/basic.jl index e906e91c0..7632e7a5a 100644 --- a/GNNLux/src/layers/basic.jl +++ b/GNNLux/src/layers/basic.jl @@ -89,7 +89,10 @@ Base.firstindex(c::GNNChain) = firstindex(c.layers) LuxCore.outputsize(c::GNNChain) = LuxCore.outputsize(c.layers[end]) -(c::GNNChain)(g::GNNGraph, x, ps, st) = _applychain(c.layers, g, x, ps.layers, st.layers) +function (c::GNNChain)(g::GNNGraph, x, ps, st) + x, st_layers = _applychain(c.layers, g, x, ps.layers, st.layers) + return x, (; layers = st_layers) +end function _applychain(layers, g::GNNGraph, x, ps, st) # type-unstable path, helps compile times newst = (;) diff --git a/GNNLux/test/layers/basic.jl b/GNNLux/test/layers/basic.jl index 0064d12e6..136004fb7 100644 --- a/GNNLux/test/layers/basic.jl +++ b/GNNLux/test/layers/basic.jl @@ -17,5 +17,16 @@ @test GNNChain <: LuxCore.AbstractLuxContainerLayer{(:layers,)} c = GNNChain(GraphConv(3 => 5, tanh), GCNConv(5 => 3)) test_lux_layer(rng, c, g, x, outputsize=(3,), container=true) + + # The returned state has the same structure as the input state, + # so it can be fed back into the chain (as in a training loop). + c = GNNChain(GCNConv(3 => 5, relu), Dropout(0.5), Dense(5 => 3)) + ps, st = LuxCore.setup(rng, c) + y, st′ = c(g, x, ps, st) + @test typeof(st′) == typeof(st) + @test st′.layers.layer_2.rng != st.layers.layer_2.rng + y′, st″ = c(g, x, ps, st′) + @test typeof(st″) == typeof(st) + @test y′ != y end end