Skip to content

[tmva][sofie] New Keras Parser from SOFIE#20871

Merged
lmoneta merged 6 commits intoroot-project:masterfrom
lmoneta:tmva_sofie_keras_parser_prasanna
Feb 7, 2026
Merged

[tmva][sofie] New Keras Parser from SOFIE#20871
lmoneta merged 6 commits intoroot-project:masterfrom
lmoneta:tmva_sofie_keras_parser_prasanna

Conversation

@lmoneta
Copy link
Member

@lmoneta lmoneta commented Jan 13, 2026

This PR superseeds #19692 fixing the conflicts and add some bug fixes

@guitargeek
Copy link
Contributor

Thanks for the PR! One two things to do before a more detailed review is to merge #15790 and fix the test failures in this PR. But I also have a general question, the same as in #19692 (comment): why not remove the old C++ parser at the same time, so we don't risk code duplication and user confusion?

@github-actions
Copy link

github-actions bot commented Jan 13, 2026

Test Results

    22 files  +    1      22 suites  +1   3d 14h 22m 15s ⏱️ + 9h 38m 6s
 3 788 tests +   13   3 788 ✅ +   13  0 💤 ±0  0 ❌ ±0 
76 171 runs  +3 855  76 171 ✅ +3 855  0 💤 ±0  0 ❌ ±0 

Results for commit 9aa6e2a. ± Comparison against base commit 34efbdb.

♻️ This comment has been updated with latest results.

@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch 4 times, most recently from 0c9d958 to d592d31 Compare January 16, 2026 09:56
@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch 2 times, most recently from e7a5b7c to fc8f6a4 Compare January 30, 2026 13:12
@guitargeek
Copy link
Contributor

@lmoneta, it seems the new Python files are missing in the relevant CMakeLists.txt now, which is my fault because I have moved that CMakeLists.txt file in 3281aa9 to bindings/pyroot/pythonizations/python/CMakeLists.txt. Can you please add the Python files there now? Thanks!

diff --git a/bindings/pyroot/pythonizations/python/CMakeLists.txt b/bindings/pyroot/pythonizations/python/CMakeLists.txt
index 4c96c7556c7..e9bbed07628 100644
--- a/bindings/pyroot/pythonizations/python/CMakeLists.txt
+++ b/bindings/pyroot/pythonizations/python/CMakeLists.txt
@@ -58,7 +58,33 @@ if(tmva)
         ROOT/_pythonization/_tmva/_rtensor.py
         ROOT/_pythonization/_tmva/_tree_inference.py
         ROOT/_pythonization/_tmva/_utils.py
-        ROOT/_pythonization/_tmva/_gnn.py)
+        ROOT/_pythonization/_tmva/_gnn.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/__init__.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/generate_keras_functional.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/generate_keras_sequential.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/parser.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/parser_test_function.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/__init__.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/batchnorm.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/binary.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/concat.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/conv.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/dense.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/elu.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/flatten.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/identity.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/layernorm.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/leaky_relu.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/permute.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/pooling.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/reshape.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/relu.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/rnn.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/selu.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/sigmoid.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/softmax.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/swish.py
+        ROOT/_pythonization/_tmva/_sofie/_parser/_keras/layers/tanh.py)
     if(dataframe)
       list(APPEND PYROOT_EXTRA_PYTHON_SOURCES
           ROOT/_pythonization/_tmva/_batchgenerator.py)

@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch 8 times, most recently from dd9635a to ed4c2bd Compare February 3, 2026 18:11
@guitargeek
Copy link
Contributor

Hi @lmoneta, about these ruff linter errors: I know it's annoying to fix all of them, but many can actually be fixed automatically by running:

ruff check bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tmva/_sofie/_parser/_keras --fix

Ruff can just be installed with pip:
https://github.com/astral-sh/ruff

I think it would be worth it to add a commit that does does these automatic fixes! Then we don't have to update the code again later just for that.

@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch 2 times, most recently from 079dc53 to ca33772 Compare February 4, 2026 18:27
@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch from ca33772 to cfb7d53 Compare February 5, 2026 10:00
@guitargeek guitargeek closed this Feb 5, 2026
@guitargeek guitargeek reopened this Feb 5, 2026
@guitargeek
Copy link
Contributor

There is also a mac14 specific error:

Node: 'model_9/average_pooling2d/AvgPool'
Default AvgPoolingOp only supports NHWC on device type CPU
 	 [[{{node model_9/average_pooling2d/AvgPool}}]] [Op:__inference_train_function_2459]

I guess it's there because it's the only Intel mac platform we test. Maybe it's not necessarily worth for SOFIE to support it? I think setting tmva-sofie=OFF for mac14 would be an option:
https://github.com/root-project/root/blob/master/.github/workflows/root-ci-config/buildconfig/mac14.txt#L33

@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch 4 times, most recently from 2a0382e to 9aa6e2a Compare February 6, 2026 14:34
@lmoneta lmoneta requested a review from sanjibansg February 6, 2026 17:32
Copy link
Contributor

@guitargeek guitargeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for carrying this over the finish line! I'm glad to see that the C++ implementation which called back to Python was translated to a native Python version that also supports Keras 3.

PrasannaKasar and others added 6 commits February 7, 2026 16:03
New Keras parser - added support for LayerNorm, BatchNorm ND, ELU layers and added tests for them. Imported Keras within the required functions. Created new CMakeLists.txt file for the keras parser. Made changes in the pythonization CMake file to build the keras parser files

removed get_keras_version function call from tmva __init__ file. Replaced import keras_version with get_keras_version and called it in necessary files

Removed SOFIE Keras Parser CMakeLists.txt file from the Pythonization directory. Used import numpy statements within the parser functions to avoid slowing down the import of ROOT.

Added print statements to display the TensorFlow Keras version used in CI

Correctly inject RModelParser_Keras class into Python interfaces
… Keras3 Sequential

In Keras3 Sequential output of a layer can have a different name than input of the next layer.
Since in sequnrial model each layer has a single input/output use as output names the layer name (which is unique) and set as input name for the next layer

[tmva][sofie] Adapt SOFIE tutorial for new Keras parser and remove old C++ parser

- use new python keras parser for parsing a model into SOFIE.
Since new parser is only Python base, move some tutorials from C++ to Python

Remove also tutorial dependency on TMVA_Higgs_Classification by creating and training a model in tutorial TMVA_SOFIE_Keras_HiggsModel.py

Adapt also RSofieReader for using new Python Keras parser

[tmva][sofie] Disable Conv2D tests of keras parser and add keras dependency to test

Fix also an issue in a SOFIE tutorial

[tmva][sofie] Fix issue in Keras parser in Redhape operator

With this fix now the Keras parser can parse the convolutional model generated by TMVA_CNN_Classification

[tmva][sofie] Fix test Keras parser and a bug in Squeeze

Fix bug in Squeeze operator when the axes to squeeze are provided. In that case a wrong usage of vector.erase was done.

Fix the Keras parser tests when convolutions with channel_first are not supported (e.g. on CPU impelmentations of tensorflows)

[tmva][sofie] Fix some remining conflicts left from master rebasing
The bug fixes the closing of the loops on the outer axis. The tests worked because the number of outer axes and normalization axes was the same in the test scripts.
…axpy

Avoid using saxpy and use directly the wrapper function in Sofie_common to call blas and automatically also include the bias addition

fix doing bias for Conv operator, beta must be equal to 1 now we don't use saxpy
The Python files were moved in root-project@3281aa9  in a different location and it was not done during the rebase. This is now fixed

Fix also an issue in parsing input shape from the Keras model and add support also for the different types of input shapes

Increase also test tolerance from 10-3 to 10-2

Add also test RModelKerasParser test

Fix an issue with RSofieReader forcing a loading of libROOTTMVASofie when compiling the SOFIE code on the flight with RReader

[tmva][sofie] Apply review comments from Jonas

Move python files used only for testing from the bindings/python code directory to the binding/python test directory
Apply to tutorials and bindings files used by the new keras parser
@lmoneta lmoneta force-pushed the tmva_sofie_keras_parser_prasanna branch from 9aa6e2a to cc7d91d Compare February 7, 2026 15:05
@lmoneta lmoneta merged commit 4d0fb3b into root-project:master Feb 7, 2026
24 of 29 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.

3 participants