diff --git a/tmva/sofie/README.md b/tmva/sofie/README.md index c571d2e948c23..7216d72867227 100644 --- a/tmva/sofie/README.md +++ b/tmva/sofie/README.md @@ -185,11 +185,8 @@ parser.CheckModel("example_model.ONNX"); ## Additional Links - **Tutorials** - - [TMVA_SOFIE_Inference](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_Inference.py) - - [TMVA_SOFIE_ONNX](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_ONNX.C) - [TMVA_SOFIE_PyTorch_HiggsModel](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py) - [TMVA_SOFIE_RDataFrame](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_RDataFrame.C) - [TMVA_SOFIE_RDataFrame](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_RDataFrame.py) - [TMVA_SOFIE_RDataFrame_JIT](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_RDataFrame_JIT.C) - [TMVA_SOFIE_RSofieReader](https://github.com/root-project/root/blob/master/tutorials/machine_learning/TMVA_SOFIE_RSofieReader.C) - diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index 963d610db6d20..dfafbb86291ef 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -137,7 +137,6 @@ if (NOT dataframe) list(APPEND dataframe_veto machine_learning/tmva*.C) list(APPEND dataframe_veto machine_learning/TMVA_SOFIE_RDataFrame*.C) list(APPEND dataframe_veto machine_learning/TMVA_SOFIE_RDataFrame*.py) - list(APPEND dataframe_veto machine_learning/TMVA_SOFIE_Inference.py) list(APPEND dataframe_veto machine_learning/ml_dataloader_NumPy.py) list(APPEND dataframe_veto machine_learning/ml_dataloader_TensorFlow.py) list(APPEND dataframe_veto machine_learning/ml_dataloader_PyTorch.py) @@ -365,44 +364,16 @@ else() if (NOT BLAS_FOUND) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN_Application.C) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RDataFrame.C) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RSofieReader.C) endif() - # The safetensors weight files used by this tutorial are little-endian only - if (CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN") - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_Models.py) - endif() # These SOFIE tutorials take the ONNX HiggsModel trained with PyTorch as input if (NOT ROOT_TORCH_FOUND OR NOT ROOT_ONNX_FOUND) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_ONNX.py) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_Models.py) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RDataFrame.C) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RDataFrame_JIT.C) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RSofieReader.C) list(APPEND tmva_veto machine_learning/TMVA_SOFIE_RDataFrame.py) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_Inference.py) - endif() - #veto this tutorial since it is added directly - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN_Parser.py) - # The GNN tutorials define their model with PyTorch and export it to ONNX - if (NOT ROOT_TORCH_FOUND OR NOT ROOT_ONNX_FOUND) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN.py) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_GNN_Application.C) - endif() - if (NOT ROOT_ONNX_FOUND) - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_ONNX.C) - else() - #generate ONNX file needed for the tutorial - execute_process( - COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tmva/sofie/test/generate_input_models.py - --no-references --outdir ${CMAKE_BINARY_DIR}/tutorials/machine_learning Linear_16 - RESULT_VARIABLE _sofie_onnx_gen_status) - if (NOT _sofie_onnx_gen_status EQUAL 0) - message(WARNING "Could not generate Linear_16.onnx: the TMVA_SOFIE_ONNX.C tutorial will be disabled.") - list(APPEND tmva_veto machine_learning/TMVA_SOFIE_ONNX.C) - endif() endif() endif() @@ -637,7 +608,6 @@ set (machine_learning-pytorch-ApplicationRegressionPyTorch-depends tutorial-mach set (machine_learning-TMVA_SOFIE_RDataFrame-depends tutorial-machine_learning-TMVA_SOFIE_PyTorch_HiggsModel-py) set (machine_learning-TMVA_SOFIE_RDataFrame_JIT-depends tutorial-machine_learning-TMVA_SOFIE_RDataFrame) set (machine_learning-TMVA_SOFIE_RSofieReader-depends tutorial-machine_learning-TMVA_SOFIE_RDataFrame_JIT) -set (machine_learning-TMVA_SOFIE_Inference-depends tutorial-machine_learning-TMVA_SOFIE_RSofieReader) set (machine_learning-keras-RegressionKeras-depends tutorial-machine_learning-pytorch-RegressionPyTorch-py) set (machine_learning-keras-ClassificationKeras-depends tutorial-machine_learning-pytorch-ClassificationPyTorch-py) set (machine_learning-keras-ApplicationRegressionKeras-depends tutorial-machine_learning-keras-RegressionKeras-py) @@ -672,13 +642,6 @@ if(geom) ROOT_ADD_TEST(tutorial-legacy-g3d-geometry COMMAND ${ROOT_root_CMD} -b -n -q ${CMAKE_CURRENT_SOURCE_DIR}/legacy/g3d/geometry.C FAILREGEX "Error in" "error:" "warning: Failed to call" LABELS tutorial ENVIRONMENT ${TUTORIAL_ENV}) endif() -# define Python GNN parsing tutorial needed to run before -if (ROOT_TORCH_FOUND AND ROOT_ONNX_FOUND) - ROOT_ADD_TEST(tutorial-machine_learning-TMVA_SOFIE_GNN_Parser COMMAND ${Python3_EXECUTABLE} - ${CMAKE_CURRENT_SOURCE_DIR}/machine_learning/TMVA_SOFIE_GNN_Parser.py - PASSRC 0 FAILREGEX "Error in" ": error:" LABELS tutorial ENVIRONMENT ${TUTORIAL_ENV}) - set (machine_learning-TMVA_SOFIE_GNN_Application-depends tutorial-machine_learning-TMVA_SOFIE_GNN_Parser) -endif() #---Loop over all tutorials and define the corresponding test--------- foreach(t ${tutorials}) @@ -870,7 +833,6 @@ if(pyroot) machine_learning/ml_dataloader_PyTorch.py machine_learning/ml_dataloader_resampling.py machine_learning/ml_dataloader_Higgs_Classification.py - machine_learning/TMVA_SOFIE_Models.py machine_learning/TMVA_SOFIE_PyTorch_HiggsModel.py ) file(GLOB requires_xgboost RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/tutorials/machine_learning/TMVA_SOFIE_GNN.py b/tutorials/machine_learning/TMVA_SOFIE_GNN.py deleted file mode 100644 index 4112effc6fa51..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_GNN.py +++ /dev/null @@ -1,304 +0,0 @@ -## \file -## \ingroup tutorial_ml -## \notebook -nodraw -## -## Tutorial showing inference of a Graph Neural Network with SOFIE. -## -## A graph network model following DeepMind's Encode-Process-Decode architecture -## (see arXiv:1806.01261) is defined in PyTorch and exported to ONNX. The ONNX -## models are then parsed with the SOFIE ONNX parser, C++ inference code is -## generated and compiled, and its output is validated against PyTorch. -## -## \macro_code -## -## \author - -import time - -import numpy as np -import ROOT -import torch -import torch.nn as nn - -# defining graph properties -num_nodes = 5 -num_edges = 20 -snd = np.array([1, 2, 3, 4, 2, 3, 4, 3, 4, 4, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3], dtype="int64") -rec = np.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 1, 2, 3, 4, 2, 3, 4, 3, 4, 4], dtype="int64") -node_size = 4 -edge_size = 4 -global_size = 1 -LATENT_SIZE = 100 -NUM_LAYERS = 4 -processing_steps = 5 -numevts = 40 - -torch.manual_seed(42) -torch.set_grad_enabled(False) - - -# method for returning dictionary of graph data -def get_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1): - return { - "globals": 10 * np.random.rand(1, GLOBAL_FEATURE_SIZE).astype(np.float32) - 5.0, - "nodes": 10 * np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32) - 5.0, - "edges": 10 * np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32) - 5.0, - "senders": snd, - "receivers": rec, - } - - -# method to instantiate an MLP model to be added in the GNN -# (a stack of Linear+ReLU layers, with a final LayerNorm for the core network) -def make_mlp_model(num_inputs, with_layer_norm=False): - layers = [] - for _ in range(NUM_LAYERS): - layers += [nn.Linear(num_inputs, LATENT_SIZE), nn.ReLU()] - num_inputs = LATENT_SIZE - if with_layer_norm: - layers.append(nn.LayerNorm(LATENT_SIZE)) - return nn.Sequential(*layers) - - -# module applying independent MLPs to the node, edge and global features -class MLPGraphIndependent(nn.Module): - def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs): - super().__init__() - self.node_fn = make_mlp_model(num_node_inputs) - self.edge_fn = make_mlp_model(num_edge_inputs) - self.global_fn = make_mlp_model(num_global_inputs) - - def forward(self, node_data, edge_data, global_data): - return self.node_fn(node_data), self.edge_fn(edge_data), self.global_fn(global_data) - - -# module implementing a full graph-network block (see arXiv:1806.01261): -# - edge update from [edge, receiver node, sender node, global] -# - node update from [sum of received edges, node, global] -# - global update from [sum of edges, sum of nodes, global] -class MLPGraphNetwork(nn.Module): - def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs): - super().__init__() - self.edge_fn = make_mlp_model(num_edge_inputs + 2 * num_node_inputs + num_global_inputs, True) - self.node_fn = make_mlp_model(LATENT_SIZE + num_node_inputs + num_global_inputs, True) - self.global_fn = make_mlp_model(2 * LATENT_SIZE + num_global_inputs, True) - - def forward(self, node_data, edge_data, global_data, receivers, senders): - n_nodes = node_data.shape[0] - n_edges = edge_data.shape[0] - edge_input = torch.cat( - [edge_data, node_data[receivers], node_data[senders], global_data.expand(n_edges, -1)], dim=1 - ) - edge_output = self.edge_fn(edge_input) - # aggregate the updated edge data per receiving node - received_edges = torch.zeros(n_nodes, edge_output.shape[1]).scatter_add( - 0, receivers.unsqueeze(1).expand(n_edges, edge_output.shape[1]), edge_output - ) - node_input = torch.cat([received_edges, node_data, global_data.expand(n_nodes, -1)], dim=1) - node_output = self.node_fn(node_input) - global_input = torch.cat( - [edge_output.sum(0, keepdim=True), node_output.sum(0, keepdim=True), global_data], dim=1 - ) - global_output = self.global_fn(global_input) - return node_output, edge_output, global_output - - -# defining a Encode-Process-Decode module for LHCb toy model -class EncodeProcessDecode(nn.Module): - def __init__(self): - super().__init__() - self._encoder = MLPGraphIndependent(node_size, edge_size, global_size) - self._core = MLPGraphNetwork(2 * LATENT_SIZE, 2 * LATENT_SIZE, 2 * LATENT_SIZE) - self._decoder = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE) - self._output_transform = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE) - - def forward(self, node_data, edge_data, global_data, receivers, senders, num_processing_steps): - latent = self._encoder(node_data, edge_data, global_data) - latent0 = latent - output_ops = [] - for _ in range(num_processing_steps): - core_input = tuple(torch.cat([a, b], dim=1) for a, b in zip(latent0, latent)) - latent = self._core(*core_input, receivers, senders) - decoded_op = self._decoder(*latent) - output_ops.append(self._output_transform(*decoded_op)) - return output_ops - - -# Instantiating EncodeProcessDecode Model -ep_model = EncodeProcessDecode() -ep_model.eval() - -# Export the four component models to ONNX -sample_indices = (torch.from_numpy(rec), torch.from_numpy(snd)) - - -def export_component(component, name, num_features): - sample_input = ( - torch.zeros(num_nodes, num_features[0]), - torch.zeros(num_edges, num_features[1]), - torch.zeros(1, num_features[2]), - ) - input_names = ["node_data", "edge_data", "global_data"] - if isinstance(component, MLPGraphNetwork): - sample_input += sample_indices - input_names += ["receivers", "senders"] - torch.onnx.export( - component, - sample_input, - name + ".onnx", - input_names=input_names, - output_names=["node_output", "edge_output", "global_output"], - dynamo=True, - ) - - -export_component(ep_model._encoder, "gnn_encoder", (node_size, edge_size, global_size)) -export_component(ep_model._core, "gnn_core", (2 * LATENT_SIZE,) * 3) -export_component(ep_model._decoder, "gnn_decoder", (LATENT_SIZE,) * 3) -export_component(ep_model._output_transform, "gnn_output_transform", (LATENT_SIZE,) * 3) - -# Parse the ONNX models with SOFIE and generate the C++ inference code -parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX() -for name in ["gnn_encoder", "gnn_core", "gnn_decoder", "gnn_output_transform"]: - model = parser.Parse(name + ".onnx") - model.Generate() - model.OutputGenerated() - -# Compile now the generated C++ code from SOFIE -gen_code = """#pragma cling optimize(2) -#include "gnn_encoder.hxx" -#include "gnn_core.hxx" -#include "gnn_decoder.hxx" -#include "gnn_output_transform.hxx" -""" -ROOT.gInterpreter.Declare(gen_code) - - -# Build SOFIE GNN Model and run inference -class SofieGNN: - def __init__(self): - self.encoder_session = ROOT.TMVA_SOFIE_gnn_encoder.Session() - self.core_session = ROOT.TMVA_SOFIE_gnn_core.Session() - self.decoder_session = ROOT.TMVA_SOFIE_gnn_decoder.Session() - self.output_transform_session = ROOT.TMVA_SOFIE_gnn_output_transform.Session() - - @staticmethod - def _as_arrays(result, num_nodes, num_edges): - # a session returns the flat node, edge and global output tensors - return ( - np.asarray(result[0], dtype=np.float32).reshape(num_nodes, -1), - np.asarray(result[1], dtype=np.float32).reshape(num_edges, -1), - np.asarray(result[2], dtype=np.float32).reshape(1, -1), - ) - - def infer(self, graphData): - n_nodes = len(graphData["nodes"]) - n_edges = len(graphData["edges"]) - - def c(x): - return np.ascontiguousarray(x, dtype=np.float32) - - receivers = np.ascontiguousarray(graphData["receivers"], dtype=np.int64) - senders = np.ascontiguousarray(graphData["senders"], dtype=np.int64) - - latent = self._as_arrays( - self.encoder_session.infer(c(graphData["nodes"]), c(graphData["edges"]), c(graphData["globals"])), - n_nodes, n_edges, - ) - latent0 = latent - output_ops = [] - for _ in range(processing_steps): - core_input = tuple(np.concatenate([a, b], axis=1) for a, b in zip(latent0, latent)) - latent = self._as_arrays( - self.core_session.infer(c(core_input[0]), c(core_input[1]), c(core_input[2]), receivers, senders), - n_nodes, n_edges, - ) - decoded = self._as_arrays( - self.decoder_session.infer(c(latent[0]), c(latent[1]), c(latent[2])), n_nodes, n_edges - ) - output_ops.append( - self._as_arrays( - self.output_transform_session.infer(c(decoded[0]), c(decoded[1]), c(decoded[2])), - n_nodes, n_edges, - ) - ) - return output_ops - - -# Test both GNN on some simulated events -dataSet = [get_graph_data_dict(num_nodes, num_edges, node_size, edge_size, global_size) for i in range(numevts)] - - -# Function to run the PyTorch model -def RunGNet(graphData): - return ep_model( - torch.from_numpy(graphData["nodes"]), - torch.from_numpy(graphData["edges"]), - torch.from_numpy(graphData["globals"]), - torch.from_numpy(graphData["receivers"]), - torch.from_numpy(graphData["senders"]), - processing_steps, - ) - - -start = time.time() -hG = ROOT.TH1D("hG", "Result from PyTorch", 20, 1, 0) -torchOutput = [] -for i in range(numevts): - out = RunGNet(dataSet[i]) - torchOutput.append([[t.numpy() for t in step] for step in out]) - hG.Fill(np.mean(torchOutput[-1][1][2])) - -end = time.time() -print("elapsed time for ", numevts, "events = ", end - start) - -# running SOFIE-GNN -hS = ROOT.TH1D("hS", "Result from SOFIE", 20, 1, 0) -start0 = time.time() -gnn = SofieGNN() -start = time.time() -print("time to create SOFIE GNN class", start - start0) -sofieOutput = [] -for i in range(numevts): - out = gnn.infer(dataSet[i]) - sofieOutput.append(out) - hS.Fill(np.mean(out[1][2])) - -end = time.time() -print("elapsed time for ", numevts, "events = ", end - start) - -c0 = ROOT.TCanvas() -c0.Divide(1, 2) -c1 = c0.cd(1) -c1.Divide(2, 1) -c1.cd(1) -hG.Draw() -c1.cd(2) -hS.Draw() - -hDn = ROOT.TH1D("hDn", "Difference for node data", 40, 1, 0) -hDe = ROOT.TH1D("hDe", "Difference for edge data", 40, 1, 0) -hDg = ROOT.TH1D("hDg", "Difference for global data", 40, 1, 0) -# compute differences between SOFIE and PyTorch -maxDifference = 0.0 -for i in range(numevts): - for hist, j in [(hDn, 0), (hDe, 1), (hDg, 2)]: - difference = sofieOutput[i][1][j] - torchOutput[i][1][j] - for value in difference.flatten(): - hist.Fill(value) - maxDifference = max(maxDifference, np.abs(difference).max()) - -print("maximum difference between SOFIE and PyTorch = ", maxDifference) -if maxDifference > 1e-4: - raise RuntimeError("SOFIE and PyTorch outputs disagree") - -c2 = c0.cd(2) -c2.Divide(3, 1) -c2.cd(1) -hDn.Draw() -c2.cd(2) -hDe.Draw() -c2.cd(3) -hDg.Draw() - -c0.Draw() diff --git a/tutorials/machine_learning/TMVA_SOFIE_GNN_Application.C b/tutorials/machine_learning/TMVA_SOFIE_GNN_Application.C deleted file mode 100644 index 9069f704de0b5..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_GNN_Application.C +++ /dev/null @@ -1,184 +0,0 @@ -/// \file -/// \ingroup tutorial_ml -/// \notebook -nodraw -/// Macro evaluating a GNN model which was generated with the Parser macro -/// TMVA_SOFIE_GNN_Parser.py -/// -/// \macro_code -/// -/// \author - -// need to add include path to find generated model file -#ifdef __CLING__ -R__ADD_INCLUDE_PATH($PWD) -#endif - -#include "encoder.hxx" -#include "core.hxx" -#include "decoder.hxx" -#include "output_transform.hxx" - -#include "TRandom3.h" -#include "TH1.h" -#include "TCanvas.h" -#include "TFile.h" -#include "TTree.h" -#include "TSystem.h" -#include "TStopwatch.h" -#include "TMath.h" -#include "ROOT/RDataFrame.hxx" - -#include - -const int num_max_nodes = 100; -const int num_max_edges = 300; -const int NODE_FEATURE_SIZE = 4; -const int EDGE_FEATURE_SIZE = 4; -const int GLOBAL_FEATURE_SIZE = 1; -const int LATENT_SIZE = 100; - -double check_mem(std::string s = ""){ - ProcInfo_t p; - printf("%s - ",s.c_str()); - gSystem->GetProcInfo(&p); - printf(" Rmem = %8.3f MB, Vmem = %8.f3 MB \n", - p.fMemResident /1024., /// convert memory from kB to MB - p.fMemVirtual /1024. - ); - return p.fMemResident / 1024.; -} - -// graph data for one event -struct GNN_Data { - size_t num_nodes = 0; - size_t num_edges = 0; - std::vector node_data; // { num_nodes, node features } - std::vector edge_data; // { num_edges, edge features } - std::vector global_data; // { 1, global features } - std::vector receivers; // { num_edges } - std::vector senders; // { num_edges } -}; - -// concatenate the feature dimensions of two row-major {rows, f1} and {rows, f2} tensors -std::vector ConcatenateFeatures(const std::vector & a, const std::vector & b, size_t rows) -{ - size_t fa = a.size() / rows, fb = b.size() / rows; - std::vector out(rows * (fa + fb)); - for (size_t i = 0; i < rows; i++) { - std::copy(a.begin() + i * fa, a.begin() + (i + 1) * fa, out.begin() + i * (fa + fb)); - std::copy(b.begin() + i * fb, b.begin() + (i + 1) * fb, out.begin() + i * (fa + fb) + fa); - } - return out; -} - -struct SOFIE_GNN { - // the sessions are created for the maximum number of nodes/edges, given in - // the same order as the shape parameters of the infer() functions - TMVA_SOFIE_encoder::Session encoder{"encoder.dat", num_max_nodes, num_max_edges}; - TMVA_SOFIE_core::Session core{"core.dat", num_max_nodes, num_max_edges}; - TMVA_SOFIE_decoder::Session decoder{"decoder.dat", num_max_nodes, num_max_edges}; - TMVA_SOFIE_output_transform::Session output_transform{"output_transform.dat", num_max_nodes, num_max_edges}; - - // each session returns the {node, edge, global} output tensors - std::vector> Infer(const GNN_Data & d, int nsteps) { - auto latent = encoder.infer(d.num_nodes, d.node_data.data(), d.num_edges, d.edge_data.data(), - d.global_data.data()); - auto latent0 = latent; - std::vector> output; - for (int i = 0; i < nsteps; i++) { - auto node_input = ConcatenateFeatures(latent0[0], latent[0], d.num_nodes); - auto edge_input = ConcatenateFeatures(latent0[1], latent[1], d.num_edges); - auto global_input = ConcatenateFeatures(latent0[2], latent[2], 1); - latent = core.infer(d.num_nodes, node_input.data(), d.num_edges, edge_input.data(), - global_input.data(), d.receivers.data(), d.senders.data()); - auto decoded = decoder.infer(d.num_nodes, latent[0].data(), d.num_edges, latent[1].data(), - latent[2].data()); - output = output_transform.infer(d.num_nodes, decoded[0].data(), d.num_edges, decoded[1].data(), - decoded[2].data()); - } - return output; - } -}; - -std::vector ReadData(std::string treename, std::string filename) { - ROOT::RDataFrame df(treename,filename); - auto ndata = df.Take>("node_data"); - auto edata = df.Take>("edge_data"); - auto gdata = df.Take>("global_data"); - auto rdata = df.Take>("receivers"); - auto sdata = df.Take>("senders"); - int nevts = ndata.GetPtr()->size(); - std::vector dataSet; - dataSet.reserve(nevts); - for (int i = 0; i < nevts; i++) { - GNN_Data gd; - auto & n = (*(ndata.GetPtr()))[i]; - auto & e = (*(edata.GetPtr()))[i]; - auto & g = (*(gdata.GetPtr()))[i]; - auto & r = (*(rdata.GetPtr()))[i]; - auto & s = (*(sdata.GetPtr()))[i]; - gd.num_nodes = n.size()/NODE_FEATURE_SIZE; - gd.num_edges = e.size()/EDGE_FEATURE_SIZE; - gd.node_data.assign(n.begin(), n.end()); - gd.edge_data.assign(e.begin(), e.end()); - gd.global_data.assign(g.begin(), g.end()); - gd.receivers.assign(r.begin(), r.end()); - gd.senders.assign(s.begin(), s.end()); - dataSet.emplace_back(std::move(gd)); - } - return dataSet; -} - - -void TMVA_SOFIE_GNN_Application (bool verbose = false) -{ - check_mem("Initial memory"); - SOFIE_GNN gnn; - check_mem("After creating GNN"); - - const int nproc_steps = 5; - - std::cout << "reading data\n"; - auto inputData = ReadData("gdata","graph_data.root"); - int nevts = inputData.size(); - - auto h1 = new TH1D("h1_sofie","SOFIE Node data",40,1,0); - auto h2 = new TH1D("h2_sofie","SOFIE Edge data",40,1,0); - auto h3 = new TH1D("h3_sofie","SOFIE Global data",40,1,0); - std::cout << "doing inference...\n"; - - check_mem("Before evaluating"); - TStopwatch w; w.Start(); - for (int i = 0; i < nevts; i++) { - auto result = gnn.Infer(inputData[i], nproc_steps); - // compute resulting means and plot them - h1->Fill(TMath::Mean(result[0].begin(), result[0].end())); - h2->Fill(TMath::Mean(result[1].begin(), result[1].end())); - h3->Fill(TMath::Mean(result[2].begin(), result[2].end())); - } - w.Stop(); - w.Print(); - check_mem("End evaluation"); - auto c1 = new TCanvas("c1","SOFIE Results"); - c1->Divide(1,3); - c1->cd(1); h1->Draw(); - c1->cd(2); h2->Draw(); - c1->cd(3); h3->Draw(); - - // compare with the reference PyTorch result made by the Parser tutorial - auto c2 = new TCanvas("c2","Reference Results"); - auto file = TFile::Open("graph_data.root"); - auto o1 = file->Get("h1"); - auto o2 = file->Get("h2"); - auto o3 = file->Get("h3"); - c2->Divide(1,3); - c2->cd(1); o1->Draw(); - c2->cd(2); o2->Draw(); - c2->cd(3); o3->Draw(); - - // check the mean of the global-data output distribution against the reference - if (verbose) - std::cout << "SOFIE global mean " << h3->GetMean() << " reference " << o3->GetMean() << std::endl; - if (std::abs(h3->GetMean() - o3->GetMean()) > 5e-4) - std::cerr << "Error in comparing SOFIE and reference results" << std::endl; -} diff --git a/tutorials/machine_learning/TMVA_SOFIE_GNN_Parser.py b/tutorials/machine_learning/TMVA_SOFIE_GNN_Parser.py deleted file mode 100644 index 92f55c713ebe6..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_GNN_Parser.py +++ /dev/null @@ -1,250 +0,0 @@ -## \file -## \ingroup tutorial_ml -## \notebook -nodraw -## -## Tutorial parsing a Graph Neural Network from ONNX and generating SOFIE -## inference code. -## -## A graph network model following DeepMind's Encode-Process-Decode architecture -## (see arXiv:1806.01261) is defined in PyTorch and exported to ONNX with -## dynamic node and edge counts. The SOFIE ONNX parser then generates C++ -## inference code for the four component networks. The tutorial also generates -## input data, evaluated here with PyTorch as a reference, which serves as -## input for the tutorial TMVA_SOFIE_GNN_Application.C. -## -## \macro_code -## -## \author - -import time - -import numpy as np -import ROOT -import torch -import torch.nn as nn - -# defining graph properties. Number of nodes/edges are the maximum -num_max_nodes = 100 -num_max_edges = 300 -node_size = 4 -edge_size = 4 -global_size = 1 -LATENT_SIZE = 100 -NUM_LAYERS = 4 -processing_steps = 5 -numevts = 100 - -torch.manual_seed(42) -torch.set_grad_enabled(False) - - -# method for returning dictionary of graph data -def get_dynamic_graph_data_dict(NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1): - num_nodes = np.random.randint(num_max_nodes - 2, size=1)[0] + 2 - num_edges = np.random.randint(num_max_edges - 2, size=1)[0] + 2 - return { - "globals": 10 * np.random.rand(1, GLOBAL_FEATURE_SIZE).astype(np.float32) - 5.0, - "nodes": 10 * np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32) - 5.0, - "edges": 10 * np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32) - 5.0, - "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int64), - "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int64), - } - - -# method to instantiate an MLP model to be added in the GNN -# (a stack of Linear+ReLU layers, with a final LayerNorm for the core network) -def make_mlp_model(num_inputs, with_layer_norm=False): - layers = [] - for _ in range(NUM_LAYERS): - layers += [nn.Linear(num_inputs, LATENT_SIZE), nn.ReLU()] - num_inputs = LATENT_SIZE - if with_layer_norm: - layers.append(nn.LayerNorm(LATENT_SIZE)) - return nn.Sequential(*layers) - - -# module applying independent MLPs to the node, edge and global features -class MLPGraphIndependent(nn.Module): - def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs): - super().__init__() - self.node_fn = make_mlp_model(num_node_inputs) - self.edge_fn = make_mlp_model(num_edge_inputs) - self.global_fn = make_mlp_model(num_global_inputs) - - def forward(self, node_data, edge_data, global_data): - return self.node_fn(node_data), self.edge_fn(edge_data), self.global_fn(global_data) - - -# module implementing a full graph-network block (see arXiv:1806.01261): -# - edge update from [edge, receiver node, sender node, global] -# - node update from [sum of received edges, node, global] -# - global update from [sum of edges, sum of nodes, global] -class MLPGraphNetwork(nn.Module): - def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs): - super().__init__() - self.edge_fn = make_mlp_model(num_edge_inputs + 2 * num_node_inputs + num_global_inputs, True) - self.node_fn = make_mlp_model(LATENT_SIZE + num_node_inputs + num_global_inputs, True) - self.global_fn = make_mlp_model(2 * LATENT_SIZE + num_global_inputs, True) - - def forward(self, node_data, edge_data, global_data, receivers, senders): - n_nodes = node_data.shape[0] - n_edges = edge_data.shape[0] - edge_input = torch.cat( - [edge_data, node_data[receivers], node_data[senders], global_data.expand(n_edges, -1)], dim=1 - ) - edge_output = self.edge_fn(edge_input) - # aggregate the updated edge data per receiving node - received_edges = torch.zeros(n_nodes, edge_output.shape[1]).scatter_add( - 0, receivers.unsqueeze(1).expand(n_edges, edge_output.shape[1]), edge_output - ) - node_input = torch.cat([received_edges, node_data, global_data.expand(n_nodes, -1)], dim=1) - node_output = self.node_fn(node_input) - global_input = torch.cat( - [edge_output.sum(0, keepdim=True), node_output.sum(0, keepdim=True), global_data], dim=1 - ) - global_output = self.global_fn(global_input) - return node_output, edge_output, global_output - - -# defining a Encode-Process-Decode module for LHCb toy model -class EncodeProcessDecode(nn.Module): - def __init__(self): - super().__init__() - self._encoder = MLPGraphIndependent(node_size, edge_size, global_size) - self._core = MLPGraphNetwork(2 * LATENT_SIZE, 2 * LATENT_SIZE, 2 * LATENT_SIZE) - self._decoder = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE) - self._output_transform = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE) - - def forward(self, node_data, edge_data, global_data, receivers, senders, num_processing_steps): - latent = self._encoder(node_data, edge_data, global_data) - latent0 = latent - output_ops = [] - for _ in range(num_processing_steps): - core_input = tuple(torch.cat([a, b], dim=1) for a, b in zip(latent0, latent)) - latent = self._core(*core_input, receivers, senders) - decoded_op = self._decoder(*latent) - output_ops.append(self._output_transform(*decoded_op)) - return output_ops - - -######################################################################################################## - -# Instantiating EncodeProcessDecode Model -ep_model = EncodeProcessDecode() -ep_model.eval() - -# Export the four component models to ONNX, with dynamic node and edge counts -num_nodes_dim = torch.export.Dim("num_nodes", min=2, max=num_max_nodes) -num_edges_dim = torch.export.Dim("num_edges", min=2, max=num_max_edges) - - -def export_component(component, name, num_features): - sample_input = ( - torch.zeros(num_max_nodes, num_features[0]), - torch.zeros(num_max_edges, num_features[1]), - torch.zeros(1, num_features[2]), - ) - input_names = ["node_data", "edge_data", "global_data"] - dynamic_shapes = { - "node_data": {0: num_nodes_dim}, - "edge_data": {0: num_edges_dim}, - "global_data": None, - } - if isinstance(component, MLPGraphNetwork): - sample_input += ( - torch.randint(num_max_nodes, (num_max_edges,)), - torch.randint(num_max_nodes, (num_max_edges,)), - ) - input_names += ["receivers", "senders"] - dynamic_shapes.update({"receivers": {0: num_edges_dim}, "senders": {0: num_edges_dim}}) - torch.onnx.export( - component, - sample_input, - name + ".onnx", - input_names=input_names, - output_names=["node_output", "edge_output", "global_output"], - dynamic_shapes=dynamic_shapes, - dynamo=True, - ) - - -export_component(ep_model._encoder, "encoder", (node_size, edge_size, global_size)) -export_component(ep_model._core, "core", (2 * LATENT_SIZE,) * 3) -export_component(ep_model._decoder, "decoder", (LATENT_SIZE,) * 3) -export_component(ep_model._output_transform, "output_transform", (LATENT_SIZE,) * 3) - -# Make the SOFIE models: parse the ONNX files and generate the inference code -parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX() -for name in ["encoder", "core", "decoder", "output_transform"]: - model = parser.Parse(name + ".onnx") - model.Generate() - model.OutputGenerated() - print("generated SOFIE model", name + ".hxx") - -#################################################################################################################################### - -# generate data and save in a ROOT TTree -fileOut = ROOT.TFile.Open("graph_data.root", "RECREATE") -tree = ROOT.TTree("gdata", "GNN data") - -node_data = ROOT.std.vector["float"]() -edge_data = ROOT.std.vector["float"]() -global_data = ROOT.std.vector["float"]() -receivers = ROOT.std.vector["int"]() -senders = ROOT.std.vector["int"]() - -tree.Branch("node_data", "std::vector", node_data) -tree.Branch("edge_data", "std::vector", edge_data) -tree.Branch("global_data", "std::vector", global_data) -tree.Branch("receivers", "std::vector", receivers) -tree.Branch("senders", "std::vector", senders) - -print("\n\nSaving data in a ROOT File:") -h1 = ROOT.TH1D("h1", "GNN nodes output", 40, 1, 0) -h2 = ROOT.TH1D("h2", "GNN edges output", 40, 1, 0) -h3 = ROOT.TH1D("h3", "GNN global output", 40, 1, 0) -dataset = [] -for i in range(numevts): - graphData = get_dynamic_graph_data_dict(node_size, edge_size, global_size) - node_data.assign(graphData["nodes"].flatten()) - edge_data.assign(graphData["edges"].flatten()) - global_data.assign(graphData["globals"].flatten()) - receivers.assign(graphData["receivers"].astype(np.int32)) - senders.assign(graphData["senders"].astype(np.int32)) - tree.Fill() - dataset.append(graphData) - -tree.Print() - -# evaluate the reference PyTorch model on these events -start = time.time() -for graphData in dataset: - output_gnn = ep_model( - torch.from_numpy(graphData["nodes"]), - torch.from_numpy(graphData["edges"]), - torch.from_numpy(graphData["globals"]), - torch.from_numpy(graphData["receivers"]), - torch.from_numpy(graphData["senders"]), - processing_steps, - ) - h1.Fill(np.mean(output_gnn[-1][0].numpy())) - h2.Fill(np.mean(output_gnn[-1][1].numpy())) - h3.Fill(np.mean(output_gnn[-1][2].numpy())) - -end = time.time() -print("time to evaluate ", numevts, " events", end - start) - -c1 = ROOT.TCanvas() -c1.Divide(1, 3) -c1.cd(1) -h1.DrawCopy() -c1.cd(2) -h2.DrawCopy() -c1.cd(3) -h3.DrawCopy() - -tree.Write() -h1.Write() -h2.Write() -h3.Write() -fileOut.Close() diff --git a/tutorials/machine_learning/TMVA_SOFIE_Inference.py b/tutorials/machine_learning/TMVA_SOFIE_Inference.py deleted file mode 100644 index 26b3c905957f4..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_Inference.py +++ /dev/null @@ -1,107 +0,0 @@ -### \file -### \ingroup tutorial_ml -### \notebook -nodraw -### This macro provides an example of using a trained model with PyTorch -### and make inference using SOFIE directly from Numpy -### This macro uses as input an ONNX model generated with the -### TMVA_SOFIE_PyTorch_HiggsModel.py tutorial -### You need to run that macro before this one. -### In this case we are parsing the input file and then run the inference in the same -### macro making use of the ROOT JITing capability -### -### -### \macro_code -### \macro_output -### \author Lorenzo Moneta - -from os.path import exists - -import numpy as np -import ROOT - -# check if the input file exists -modelFile = "HiggsModel.onnx" - -if not exists(modelFile): - raise FileNotFoundError("You need to run TMVA_SOFIE_PyTorch_HiggsModel.py to generate the ONNX trained model") - - -# parse the input ONNX model into RModel object -parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX() -model = parser.Parse(modelFile) - -generatedHeaderFile = modelFile.replace(".onnx", ".hxx") -print("Generating inference code for the ONNX model from ", modelFile, "in the header ", generatedHeaderFile) -#Generating inference code -model.Generate() -model.OutputGenerated(generatedHeaderFile) -model.PrintGenerated() - -# now compile using ROOT JIT trained model -modelName = modelFile.replace(".onnx", "") -print("compiling SOFIE model ", modelName) -ROOT.gInterpreter.Declare('#include "' + generatedHeaderFile + '"') - -inputFileName = "Higgs_data.root" -inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/" + inputFileName - - - - - -# make SOFIE inference on signal data - -df1 = ROOT.RDataFrame("sig_tree", inputFile) -sigData = df1.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb']) -#print(sigData) - -# stack all the 7 numpy array in a single array (nevents x nvars) -xsig = np.column_stack(list(sigData.values())) -dataset_size = xsig.shape[0] -print("size of signal data", dataset_size) - -#instantiate SOFIE session class -#session = ROOT.TMVA_SOFIE_HiggsModel.Session() -#get the sofie session namespace -sofie = getattr(ROOT, 'TMVA_SOFIE_' + modelName) -session = sofie.Session() - -print("Evaluating SOFIE models on signal data") -hs = ROOT.TH1D("hs","Signal result",100,0,1) -for i in range(0,dataset_size): - result = session.infer(xsig[i,:]) - if (i % dataset_size/10 == 0) : - print("result for signal event ",i,result[0]) - hs.Fill(result[0]) - -print("using RDsataFrame to extract input data in a numpy array") -# make SOFIE inference on background data -df2 = ROOT.RDataFrame("bkg_tree", inputFile) -bkgData = df2.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb']) - -xbkg = np.column_stack(list(bkgData.values())) -dataset_size = xbkg.shape[0] -print("size of background data", dataset_size) - -hb = ROOT.TH1D("hb","Background result",100,0,1) -for i in range(0,dataset_size): - result = session.infer(xbkg[i,:]) - if (i % dataset_size/10 == 0) : - print("result for background event ",i,result[0]) - - hb.Fill(result[0]) - - -c1 = ROOT.TCanvas() -ROOT.gStyle.SetOptStat(0) -hs.SetLineColor("kRed") -hs.Draw() -hb.SetLineColor("kBlue") -hb.Draw("SAME") -c1.BuildLegend() -c1.Draw() - - -print("Number of signal entries",hs.GetEntries()) -print("Number of background entries",hb.GetEntries()) - diff --git a/tutorials/machine_learning/TMVA_SOFIE_Models.py b/tutorials/machine_learning/TMVA_SOFIE_Models.py deleted file mode 100644 index cddb781a84fd7..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_Models.py +++ /dev/null @@ -1,272 +0,0 @@ -### \file -### \ingroup tutorial_ml -### \notebook -nodraw -### Example of inference with SOFIE using a set of models trained with PyTorch -### and exported to ONNX. -### This tutorial shows how to store several models in a single header file and -### the weights in a ROOT binary file. -### The models are then evaluated using the RDataFrame -### -### \macro_code -### \macro_output -### \author Lorenzo Moneta - -import inspect -import os - -import numpy as np -import ROOT -import torch -import torch.nn as nn - -## generate and train PyTorch models with different architectures - - -def CreateModel(nlayers=4, nunits=64): - layers = [] - ninputs = 7 - for i in range(nlayers): - layers += [nn.Linear(ninputs, nunits), nn.ReLU()] - ninputs = nunits - layers += [nn.Linear(ninputs, 1), nn.Sigmoid()] - model = nn.Sequential(*layers) - print(model) - return model - - -def TrainModel(model, x, y, epochs=5, batch_size=50): - x = torch.from_numpy(x) - y = torch.from_numpy(y) - criterion = nn.BCELoss() - optimizer = torch.optim.Adam(model.parameters()) - nbatches = x.shape[0] // batch_size - for epoch in range(epochs): - perm = torch.randperm(x.shape[0]) - running_loss = 0.0 - for i in range(nbatches): - idx = perm[i * batch_size : (i + 1) * batch_size] - optimizer.zero_grad() - loss = criterion(model(x[idx]), y[idx]) - loss.backward() - optimizer.step() - running_loss += loss.item() - print(f"Epoch {epoch + 1}/{epochs} - average loss: {running_loss / nbatches:.4f}") - - -def ExportModel(model, modelName): - # need to evaluate the model before exporting to ONNX - # and to provide a dummy input tensor to set the input model shape - # (the batch size is fixed to 1 for the SOFIE inference) - model.eval() - - modelFile = modelName + ".onnx" - dummy_x = torch.randn(1, 7) - model(dummy_x) - - # check for torch.onnx.export parameters - def filtered_kwargs(func, **candidate_kwargs): - sig = inspect.signature(func) - return {k: v for k, v in candidate_kwargs.items() if k in sig.parameters} - - kwargs = filtered_kwargs( - torch.onnx.export, - input_names=["input"], - output_names=["output"], - external_data=False, # may not exist - dynamo=True, # may not exist - ) - print("calling torch.onnx.export with parameters", kwargs) - - torch.onnx.export(model, dummy_x, modelFile, **kwargs) - - print("model exported to ONNX as", modelFile) - return modelFile - - -def PrepareData(): - # get the input data - inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/Higgs_data.root" - - df1 = ROOT.RDataFrame("sig_tree", inputFile) - sigData = df1.AsNumpy(columns=["m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"]) - # print(sigData) - - # stack all the 7 numpy array in a single array (nevents x nvars) - xsig = np.column_stack(list(sigData.values())) - data_sig_size = xsig.shape[0] - print("size of data", data_sig_size) - - # make SOFIE inference on background data - df2 = ROOT.RDataFrame("bkg_tree", inputFile) - bkgData = df2.AsNumpy(columns=["m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"]) - xbkg = np.column_stack(list(bkgData.values())) - data_bkg_size = xbkg.shape[0] - - ysig = np.ones(data_sig_size) - ybkg = np.zeros(data_bkg_size) - inputs_data = np.concatenate((xsig, xbkg), axis=0).astype(np.float32) - inputs_targets = np.concatenate((ysig, ybkg), axis=0).astype(np.float32) - - # split data in training and test data - rng = np.random.default_rng(1234) - idx = rng.permutation(inputs_data.shape[0]) - ntrain = inputs_data.shape[0] // 2 - - x_train = inputs_data[idx[:ntrain]] - y_train = inputs_targets[idx[:ntrain]].reshape(-1, 1) - x_test = inputs_data[idx[ntrain:]] - y_test = inputs_targets[idx[ntrain:]].reshape(-1, 1) - - return x_train, y_train, x_test, y_test - - -def TrainModels(x_train, y_train, modelNames): - # train the models with PyTorch and export them to ONNX - modelFiles = [] - for modelName in modelNames: - model = CreateModel(4, 64) - TrainModel(model, x_train, y_train) - modelFiles.append(ExportModel(model, modelName)) - return modelFiles - - -### run the models - -x_train, y_train, x_test, y_test = PrepareData() - -## create models and train them - -# All three models use the same small architecture (4 layers of 64 units) to -# keep the tutorial runtime under control, whatever their names suggest. -modelNames = ["Higgs_Model_4L_50", "Higgs_Model_4L_200", "Higgs_Model_2L_500"] -model1, model2, model3 = TrainModels(x_train, y_train, modelNames) - -# evaluate with SOFIE the 3 trained models - - -def GenerateModelCode(modelFile, generatedHeaderFile, weightFile): - parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX() - model = parser.Parse(modelFile) - - print("Generating inference code for the ONNX model from ", modelFile, "in the header ", generatedHeaderFile) - # Generating inference code; the weights are written to a binary - # safetensors file - model.Generate(ROOT.TMVA.Experimental.SOFIE.Options.kSafetensorsWeightFile) - # add option to append to the same file the generated headers (pass True for append flag) - model.OutputGenerated(generatedHeaderFile, True) - # OutputGenerated writes the weights next to the header; a safetensors file - # holds one model's weights, so move it to a per-model file - os.replace(generatedHeaderFile.replace(".hxx", ".safetensors"), weightFile) - # model.PrintGenerated() - return generatedHeaderFile - - -generatedHeaderFile = "Higgs_Model.hxx" -# need to remove existing header file since we are appending on same one -if os.path.exists(generatedHeaderFile): - print("removing existing file", generatedHeaderFile) - os.remove(generatedHeaderFile) - -weightFiles = [name + ".safetensors" for name in modelNames] -for weightFile in weightFiles: - if os.path.exists(weightFile): - print("removing existing file", weightFile) - os.remove(weightFile) - -GenerateModelCode(model1, generatedHeaderFile, weightFiles[0]) -GenerateModelCode(model2, generatedHeaderFile, weightFiles[1]) -GenerateModelCode(model3, generatedHeaderFile, weightFiles[2]) - -# compile the generated code - -ROOT.gInterpreter.Declare('#include "' + generatedHeaderFile + '"') - - -# run the inference on the test data -session1 = ROOT.TMVA_SOFIE_Higgs_Model_4L_50.Session(weightFiles[0]) -session2 = ROOT.TMVA_SOFIE_Higgs_Model_4L_200.Session(weightFiles[1]) -session3 = ROOT.TMVA_SOFIE_Higgs_Model_2L_500.Session(weightFiles[2]) - -hs1 = ROOT.TH1D("hs1", "Signal result 4L 50", 100, 0, 1) -hs2 = ROOT.TH1D("hs2", "Signal result 4L 200", 100, 0, 1) -hs3 = ROOT.TH1D("hs3", "Signal result 2L 500", 100, 0, 1) - -hb1 = ROOT.TH1D("hb1", "Background result 4L 50", 100, 0, 1) -hb2 = ROOT.TH1D("hb2", "Background result 4L 200", 100, 0, 1) -hb3 = ROOT.TH1D("hb3", "Background result 2L 500", 100, 0, 1) - - -def EvalModel(session, x): - result = session.infer(x) - return result[0] - - -for i in range(0, x_test.shape[0]): - result1 = EvalModel(session1, x_test[i, :]) - result2 = EvalModel(session2, x_test[i, :]) - result3 = EvalModel(session3, x_test[i, :]) - if y_test[i] == 1: - hs1.Fill(result1) - hs2.Fill(result2) - hs3.Fill(result3) - else: - hb1.Fill(result1) - hb2.Fill(result2) - hb3.Fill(result3) - - -def PlotHistos(hs, hb): - hs.SetLineColor("kRed") - hb.SetLineColor("kBlue") - hs.Draw() - hb.Draw("same") - - -c1 = ROOT.TCanvas() -c1.Divide(1, 3) -c1.cd(1) -PlotHistos(hs1, hb1) -c1.cd(2) -PlotHistos(hs2, hb2) -c1.cd(3) -PlotHistos(hs3, hb3) -c1.Draw() - -## draw also ROC curves - - -def GetContent(h): - n = h.GetNbinsX() - x = ROOT.std.vector["float"](n) - w = ROOT.std.vector["float"](n) - for i in range(0, n): - x[i] = h.GetBinCenter(i + 1) - w[i] = h.GetBinContent(i + 1) - return x, w - - -def MakeROCCurve(hs, hb): - xs, ws = GetContent(hs) - xb, wb = GetContent(hb) - roc = ROOT.TMVA.ROCCurve(xs, xb, ws, wb) - print("ROC integral for ", hs.GetName(), roc.GetROCIntegral()) - curve = roc.GetROCCurve() - curve.SetName(hs.GetName()) - return roc, curve - - -c2 = ROOT.TCanvas() - -r1, curve1 = MakeROCCurve(hs1, hb1) -curve1.SetLineColor("kRed") -curve1.Draw("AC") - -r2, curve2 = MakeROCCurve(hs2, hb2) -curve2.SetLineColor("kBlue") -curve2.Draw("C") - -r3, curve3 = MakeROCCurve(hs3, hb3) -curve3.SetLineColor("kGreen") -curve3.Draw("C") - -c2.Draw() diff --git a/tutorials/machine_learning/TMVA_SOFIE_ONNX.C b/tutorials/machine_learning/TMVA_SOFIE_ONNX.C deleted file mode 100644 index 8c192789e1210..0000000000000 --- a/tutorials/machine_learning/TMVA_SOFIE_ONNX.C +++ /dev/null @@ -1,51 +0,0 @@ -/// \file -/// \ingroup tutorial_ml -/// \notebook -nodraw -/// This macro provides a simple example for the parsing of ONNX files into -/// RModel object and further generating the .hxx header files for inference. -/// -/// \macro_code -/// \macro_output -/// \author Sanjiban Sengupta - -using namespace TMVA::Experimental; - -void TMVA_SOFIE_ONNX(std::string inputFile = ""){ - if (inputFile.empty() ) - inputFile = std::string(gROOT->GetTutorialsDir()) + "/machine_learning/Linear_16.onnx"; - - //Creating parser object to parse ONNX files - SOFIE::RModelParser_ONNX parser; - SOFIE::RModel model = parser.Parse(inputFile, true); - - //Generating inference code - model.Generate(); - // write the code in a file (by default Linear_16.hxx and Linear_16.dat - model.OutputGenerated(); - - //Printing required input tensors - model.PrintRequiredInputTensors(); - - //Printing initialized tensors (weights) - std::cout<<"\n\n"; - model.PrintInitializedTensors(); - - //Printing intermediate tensors - std::cout<<"\n\n"; - model.PrintIntermediateTensors(); - - //Checking if tensor already exist in model - std::cout<<"\n\nTensor \"16weight\" already exist: "< tensorShape = model.GetTensorShape("16weight"); - std::cout<<"Shape of tensor \"16weight\": "; - for(auto& it:tensorShape){ - std::cout< output using SOFIE = ", y_sofie) - -# check inference with onnx -try: - import onnxruntime as ort - - # Load model - print("Running inference with ONNXRuntime ") - ort_session = ort.InferenceSession(modelFile) - - # Run inference - outputs = ort_session.run(None, {"input": x}) - y_ort = outputs[0] - print("-> output using ORT =", y_ort) - - testFailed = abs(y_sofie - y_ort) > 0.01 - if np.any(testFailed): - raise RuntimeError("Result is different between SOFIE and ONNXRT") - else: - print("OK") - -except ImportError: - print("Missing ONNXRuntime: skipping comparison test") diff --git a/tutorials/machine_learning/index.md b/tutorials/machine_learning/index.md index b592e7094c649..db0a65ea8008a 100644 --- a/tutorials/machine_learning/index.md +++ b/tutorials/machine_learning/index.md @@ -107,10 +107,7 @@ | **Tutorial** || **Description** | |---------------|-----------------|-----------------| -| | TMVA_SOFIE_Inference.py | Using a trained model with PyTorch and make inference using SOFIE directly from Numpy. | -| | TMVA_SOFIE_Models.py | Inference with SOFIE using a set of models trained with PyTorch. | -| TMVA_SOFIE_ONNX.C | | Parsing of ONNX files into RModel object and further generating the .hxx header files for inference. | -| | TMVA_SOFIE_PyTorch_HiggsModel.py | Train a model on the Higgs dataset with PyTorch, export it to ONNX and run the SOFIE parser on it to generate the C++ inference code used by the other Higgs SOFIE tutorials. | +| | TMVA_SOFIE_PyTorch_HiggsModel.py | Train a model on the Higgs dataset with PyTorch, export it to ONNX and run the SOFIE parser on it to generate the C++ inference code used by the RDataFrame Higgs SOFIE tutorials. | | TMVA_SOFIE_RDataFrame.C | TMVA_SOFIE_RDataFrame.py | Inference with SOFIE and RDataFrame, of a model trained with PyTorch. | | TMVA_SOFIE_RDataFrame_JIT.C | | Using a trained model with PyTorch and make inference using SOFIE and RDataFrame. | | TMVA_SOFIE_RSofieReader.C | | Using a trained model with PyTorch and make inference using SOFIE with the RSofieReader class. |