Skip to content

Commit 692a1ed

Browse files
committed
[tutorials] Migrate the SOFIE GNN tutorials to PyTorch and ONNX
The SOFIE GNN tutorials defined their model with DeepMind graph_nets and dm-sonnet, whose last releases date from 2020 and which cannot be installed together with current Python and TensorFlow versions anymore. Define the same model with plain PyTorch instead: an Encode-Process-Decode graph network with the identical architecture (graph-network blocks following Battaglia et al., arXiv:1806.01261, with 4-layer ReLU MLP update functions, LayerNorm in the core network and summation aggregation). The components are exported to ONNX with the torch.export-based exporter and the inference code is generated with the SOFIE ONNX parser, so the GNN-specific SOFIE classes and the graph_nets parser are no longer involved. The graph-network block maps onto standard ONNX operators: Gemm+Relu, Concat, Gather of the sender/receiver node features, ScatterElements(reduction=add) for the edge aggregation (the equivalent of unsorted_segment_sum), ReduceSum and LayerNormalization. - TMVA_SOFIE_GNN.py validates the generated inference against PyTorch (agreement at float32 precision) and compares the execution times. - TMVA_SOFIE_GNN_Parser.py exports the model with dynamic node and edge counts, generates the inference code and writes variable-sized input graphs plus PyTorch reference results to a ROOT file. - TMVA_SOFIE_GNN_Application.C evaluates the generated code in C++ on that file, like before, now with the ONNX-generated session interface. The tutorials are now gated on the torch and onnx Python modules instead of sonnet and graph_nets. 🤖 Done with the help of AI
1 parent 0eeb284 commit 692a1ed

4 files changed

Lines changed: 463 additions & 573 deletions

File tree

‎tutorials/CMakeLists.txt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,6 @@ else()
357357
#these depends on external packages
358358
ROOT_FIND_PYTHON_MODULE(torch)
359359
ROOT_FIND_PYTHON_MODULE(keras)
360-
ROOT_FIND_PYTHON_MODULE(sonnet)
361-
ROOT_FIND_PYTHON_MODULE(graph_nets)
362360
ROOT_FIND_PYTHON_MODULE(onnx)
363361

364362

@@ -380,7 +378,8 @@ else()
380378
endif()
381379
#veto this tutorial since it is added directly
382380
list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN_Parser.py)
383-
if (NOT ROOT_SONNET_FOUND OR NOT ROOT_GRAPH_NETS_FOUND)
381+
# The GNN tutorials define their model with PyTorch and export it to ONNX
382+
if (NOT ROOT_TORCH_FOUND OR NOT ROOT_ONNX_FOUND)
384383
list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN.py)
385384
list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN_Application.C)
386385
endif()
@@ -683,7 +682,7 @@ if(geom)
683682
FAILREGEX "Error in" "error:" "warning: Failed to call" LABELS tutorial ENVIRONMENT ${TUTORIAL_ENV})
684683
endif()
685684
# define Python GNN parsing tutorial needed to run before
686-
if (ROOT_SONNET_FOUND AND ROOT_GRAPH_NETS_FOUND)
685+
if (ROOT_TORCH_FOUND AND ROOT_ONNX_FOUND)
687686
ROOT_ADD_TEST(tutorial-machine_learning-TMVA_SOFIE_GNN_Parser COMMAND ${Python3_EXECUTABLE}
688687
${CMAKE_CURRENT_SOURCE_DIR}/machine_learning/TMVA_SOFIE_GNN_Parser.py
689688
PASSRC 0 FAILREGEX "Error in" ": error:" LABELS tutorial ENVIRONMENT ${TUTORIAL_ENV})

0 commit comments

Comments
 (0)