Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_RSofieReader.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This macro provides an example of using a trained model with PyTorch and make inference using SOFIE with the RSofieReader class This macro uses as input an ONNX model generated with the TMVA_SOFIE_PyTorch_HiggsModel.py tutorial You need to run that macro before to generate the trained PyTorch model

Execute in this order:

using namespace TMVA::Experimental;
RSofieReader model("HiggsModel.onnx", {}, true);
// the input shape for this model is a tensor with shape (1,7)
std::vector<float> input = {0.1,0.2,0.3,0.4,0.5,0.6,0.7};
// predict model on a single event (takes a std::vector<float>)
auto output = model.Compute(input);
std::cout << "Event prediction = " << output[0] << std::endl;
// predict model now on a input file using RDataFrame
std::string inputFileName = "Higgs_data.root";
std::string inputFile = std::string{gROOT->GetTutorialDir()} + "/machine_learning/data/" + inputFileName;
auto h1 = df1.Define("DNN_Values", Compute<7, float>(model),
{"m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"})
.Define("y","DNN_Values[0]")
.Histo1D({"h_sig", "", 100, 0, 1}, "y");
auto h2 = df2.Define("DNN_Values", Compute<7, float>(model),
{"m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"})
.Define("y","DNN_Values[0]")
.Histo1D({"h_bkg", "", 100, 0, 1}, "y");
h2->SetLineColor(kBlue);
auto c1 = new TCanvas();
h2->DrawClone();
h1->DrawClone("SAME");
c1->BuildLegend();
}
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
#define gROOT
Definition TROOT.h:417
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
The Canvas class.
Definition TCanvas.h:23
TMVA::RSofieReader class for reading external Machine Learning models in ONNX files,...
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
Definition TObject.cxx:318
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1641
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
input model type is ONNX
Parsing SOFIE model HiggsModel of type onnx
generating the code with batch size = 1 ...
...ParserCode being executed...:
{
TMVA::Experimental::SOFIE::RModelParser_ONNX parser ;
TMVA::Experimental::SOFIE::RModel model = parser.Parse("HiggsModel.onnx");
model.Generate(TMVA::Experimental::SOFIE::Options::kDefault,1, 0, 1);
model.OutputGenerated("HiggsModel_fromRSofieR.hxx");
model.PrintRequiredInputTensors();
model.PrintIntermediateTensors();
model.PrintOutputTensors();
int nInputs = model.GetInputTensorNames().size();
return nInputs;
}
Model requires following inputs:
Fully Specified Tensor name: input type: float shape: [1,7]
Model specify the following dynamic tensors:
Initializing operator 0 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Gemm (or MatMul) ---> relu shape { 1 , 64 }
Initializing operator 1 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Gemm (or MatMul) ---> relu_1 shape { 1 , 64 }
Initializing operator 2 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Gemm (or MatMul) ---> linear_2 shape { 1 , 1 }
Initializing operator 3 N4TMVA12Experimental5SOFIE17ROperator_SigmoidIfEE
generate Main session code - model HiggsModel
******************
analyzing input/output operator 0 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Total chunks allocated
*** AllocateIntermediateMemory: Loop on op output tensors
output tensor relu size 256
no chunk available - add in total stack a new chunk with size of tensor and idx : 0
*** CheckAndFlushIntermediateMemory: Loop on input tensors for op 0
available chunks before freeing them :
.. input tensors : input flash condition is met - looping on chunks to find matching one
--- chunk 0 , relu size 256.. input tensors : 0weight flash condition is met - looping on chunks to find matching one
--- chunk 0 , relu size 256.. input tensors : 0bias flash condition is met - looping on chunks to find matching one
--- chunk 0 , relu size 256
******************
analyzing input/output operator 1 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Total chunks allocated
..... chunk 0 size 256 relu
*** AllocateIntermediateMemory: Loop on op output tensors
output tensor relu_1 size 256
no chunk available - add in total stack a new chunk with size of tensor and idx : 256
*** CheckAndFlushIntermediateMemory: Loop on input tensors for op 1
available chunks before freeing them :
.. input tensors : relu flash condition is met - looping on chunks to find matching one
--- chunk 0 , relu size 256 -- Found chunk corresponding to input tensor: 0 insert in the available stack the chunk with size 256
.. input tensors : 2weight
.. input tensors : 2bias
******************
analyzing input/output operator 2 N4TMVA12Experimental5SOFIE14ROperator_GemmIfEE
Total chunks allocated
..... chunk 0 size 256 free
..... chunk 256 size 256 relu_1
*** AllocateIntermediateMemory: Loop on op output tensors
output tensor linear_2 size 4
.. available chunk 0 with size = 256 is re-used and split in a new of size 4 at 252
*** CheckAndFlushIntermediateMemory: Loop on input tensors for op 2
available chunks before freeing them :
-- free chunk 0 size = 252
.. input tensors : relu_1 flash condition is met - looping on chunks to find matching one
--- chunk 0 , free size 252--- chunk 252 , linear_2 size 4--- chunk 256 , relu_1 size 256 -- Found chunk corresponding to input tensor: 256 insert in the available stack the chunk with size 256
.. input tensors : 4weight
.. input tensors : 4bias
******************
analyzing input/output operator 3 N4TMVA12Experimental5SOFIE17ROperator_SigmoidIfEE
Total chunks allocated
..... chunk 0 size 252 free
..... chunk 252 size 4 linear_2
..... chunk 256 size 256 free
*** AllocateIntermediateMemory: Loop on op output tensors
output tensor output size 4
.. available chunk 0 with size = 252 is re-used and split in a new of size 4 at 248
*** CheckAndFlushIntermediateMemory: Loop on input tensors for op 3
available chunks before freeing them :
-- free chunk 0 size = 248
-- free chunk 256 size = 256
.. input tensors : linear_2 flash condition is met - looping on chunks to find matching one
--- chunk 0 , free size 248--- chunk 248 , output size 4--- chunk 252 , linear_2 size 4 -- Found chunk corresponding to input tensor: 252 is adjacent in memory with following one - merge
Generating main inference code for HiggsModel
Generating code for operator .... 0
Generating code for operator .... 1
Generating code for operator .... 2
Generating code for operator .... 3
Model requires following inputs:
Fully Specified Tensor name: input type: float shape: [1,7]
Model specify the following intermediate tensors:
Tensor name: "output" type: float shape: [1,1]
Tensor name: "relu_1" type: float shape: [1,64]
Tensor name: "linear_2" type: float shape: [1,1]
Tensor name: "relu" type: float shape: [1,64]
Model specify the following output tensors:
Tensor name: "output" with shape: { 1 , 1 }
Model Header file is generated!
compile generated code from file HiggsModel_fromRSofieR.hxx
Creating Inference function for model HiggsModel
//global session declaration
#pragma cling optimize(2)
#include "HiggsModel_fromRSofieR.hxx"
TMVA_SOFIE_HiggsModel::Session session_073170ae8f3f11f1aa010200590abeef("HiggsModel_fromRSofieR.dat");
//Inference function code using global session instance
std::vector<float> SofieInference_073170ae8f3f11f1aa010200590abeef( void * ptr, float * data0) {
TMVA_SOFIE_HiggsModel::Session * s = (TMVA_SOFIE_HiggsModel::Session*) (ptr);
return s->infer(data0);
}
Event prediction = 0.370472
Author
Lorenzo Moneta

Definition in file TMVA_SOFIE_RSofieReader.C.