From 6f7024a0ccc3034dd1d9a1999e5b5869c0362393 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Wed, 2 Sep 2026 09:23:38 +0200 Subject: [PATCH] [tmva][sofie] Don't expect warnings from PyTorch PyTorch 2.14 was released today, and it showed up in our ROOT CI images tonight. If fixed a warning that our tutorials explicitly expected, failing loudly when they don't happen anymore so that we're reminded to remove boilerplate code for warning silencing. Effectively a revert of 18a30faadd67a6. --- .../machine_learning/TMVA_SOFIE_Models.py | 33 +------------------ tutorials/machine_learning/TMVA_SOFIE_ONNX.py | 32 +----------------- .../TMVA_SOFIE_PyTorch_HiggsModel.py | 32 +----------------- 3 files changed, 3 insertions(+), 94 deletions(-) diff --git a/tutorials/machine_learning/TMVA_SOFIE_Models.py b/tutorials/machine_learning/TMVA_SOFIE_Models.py index 6118d6f6e4662..175b47def4785 100644 --- a/tutorials/machine_learning/TMVA_SOFIE_Models.py +++ b/tutorials/machine_learning/TMVA_SOFIE_Models.py @@ -11,38 +11,14 @@ ### \macro_output ### \author Lorenzo Moneta -import contextlib import inspect import os -import warnings import numpy as np import ROOT import torch import torch.nn as nn - -@contextlib.contextmanager -def expect_warning(category, message): - # Silence a known third-party warning and raise if it stops firing. - - # Notifies us to drop the workaround once the upstream library is fixed. - with warnings.catch_warnings(record=True) as caught: - warnings.simplefilter("always") - yield - seen = False - for w in caught: - if issubclass(w.category, category) and message in str(w.message): - seen = True - else: - warnings.warn_explicit(w.message, w.category, w.filename, w.lineno) - if not seen: - raise RuntimeError( - f"Expected {category.__name__} containing {message!r} was not " - "emitted. This tutorial's workaround can probably be removed." - ) - - ## generate and train PyTorch models with different architectures @@ -101,14 +77,7 @@ def filtered_kwargs(func, **candidate_kwargs): ) print("calling torch.onnx.export with parameters", kwargs) - try: - # torch.onnx.export (dynamo path) pickles its export program through - # copyreg, which still references the deprecated LeafSpec. The warning - # is emitted from inside PyTorch and cannot be avoided from user code. - with expect_warning(FutureWarning, "isinstance(treespec, LeafSpec)"): - torch.onnx.export(model, dummy_x, modelFile, **kwargs) - except TypeError as e: - raise RuntimeError("Cannot export model from pytorch to ONNX - with version " + torch.__version__) from e + torch.onnx.export(model, dummy_x, modelFile, **kwargs) print("model exported to ONNX as", modelFile) return modelFile diff --git a/tutorials/machine_learning/TMVA_SOFIE_ONNX.py b/tutorials/machine_learning/TMVA_SOFIE_ONNX.py index 0c54e5eb0f415..ee963951ed8ed 100644 --- a/tutorials/machine_learning/TMVA_SOFIE_ONNX.py +++ b/tutorials/machine_learning/TMVA_SOFIE_ONNX.py @@ -11,9 +11,7 @@ ## \macro_output ## \author Lorenzo Moneta -import contextlib import inspect -import warnings import numpy as np import ROOT @@ -21,27 +19,6 @@ import torch.nn as nn -@contextlib.contextmanager -def expect_warning(category, message): - # Silence a known third-party warning and raise if it stops firing. - - # Notifies us to drop the workaround once the upstream library is fixed. - with warnings.catch_warnings(record=True) as caught: - warnings.simplefilter("always") - yield - seen = False - for w in caught: - if issubclass(w.category, category) and message in str(w.message): - seen = True - else: - warnings.warn_explicit(w.message, w.category, w.filename, w.lineno) - if not seen: - raise RuntimeError( - f"Expected {category.__name__} containing {message!r} was not " - "emitted. This tutorial's workaround can probably be removed." - ) - - def CreateAndTrainModel(modelName): model = nn.Sequential(nn.Linear(32, 16), nn.ReLU(), nn.Linear(16, 8), nn.ReLU(), nn.Linear(8, 2), nn.Softmax(dim=1)) @@ -84,14 +61,7 @@ def filtered_kwargs(func, **candidate_kwargs): ) print("calling torch.onnx.export with parameters", kwargs) - try: - # torch.onnx.export (dynamo path) pickles its export program through - # copyreg, which still references the deprecated LeafSpec. The warning - # is emitted from inside PyTorch and cannot be avoided from user code. - with expect_warning(FutureWarning, "isinstance(treespec, LeafSpec)"): - torch.onnx.export(model, dummy_x, modelFile, **kwargs) - except TypeError as e: - raise RuntimeError("Cannot export model from pytorch to ONNX - with version " + torch.__version__) from e + torch.onnx.export(model, dummy_x, modelFile, **kwargs) print("model exported to ONNX as", modelFile) return modelFile diff --git a/tutorials/machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py b/tutorials/machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py index b744331d6325a..0a62ad6878cbf 100644 --- a/tutorials/machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py +++ b/tutorials/machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py @@ -12,9 +12,7 @@ ### \macro_code ### \macro_output -import contextlib import inspect -import warnings import numpy as np import ROOT @@ -22,27 +20,6 @@ import torch.nn as nn -@contextlib.contextmanager -def expect_warning(category, message): - # Silence a known third-party warning and raise if it stops firing. - - # Notifies us to drop the workaround once the upstream library is fixed. - with warnings.catch_warnings(record=True) as caught: - warnings.simplefilter("always") - yield - seen = False - for w in caught: - if issubclass(w.category, category) and message in str(w.message): - seen = True - else: - warnings.warn_explicit(w.message, w.category, w.filename, w.lineno) - if not seen: - raise RuntimeError( - f"Expected {category.__name__} containing {message!r} was not " - "emitted. This tutorial's workaround can probably be removed." - ) - - def PrepareData(): # get the input data inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/Higgs_data.root" @@ -135,14 +112,7 @@ def filtered_kwargs(func, **candidate_kwargs): ) print("calling torch.onnx.export with parameters", kwargs) - try: - # torch.onnx.export (dynamo path) pickles its export program through - # copyreg, which still references the deprecated LeafSpec. The warning - # is emitted from inside PyTorch and cannot be avoided from user code. - with expect_warning(FutureWarning, "isinstance(treespec, LeafSpec)"): - torch.onnx.export(model, dummy_x, modelFile, **kwargs) - except TypeError as e: - raise RuntimeError("Cannot export model from pytorch to ONNX - with version " + torch.__version__) from e + torch.onnx.export(model, dummy_x, modelFile, **kwargs) print("model exported to ONNX as", modelFile) return modelFile