From 515da85fcd9e62f07a066cf0c6ad78aabed21349 Mon Sep 17 00:00:00 2001 From: cpecar Date: Mon, 1 May 2023 16:14:41 -0400 Subject: [PATCH 01/10] Start of changes to produce NN predictions with ONNX --- src/Kinematics.cxx | 117 ++++++++++++++++++++++++++++++--------------- src/Kinematics.h | 33 +++++++------ 2 files changed, 94 insertions(+), 56 deletions(-) diff --git a/src/Kinematics.cxx b/src/Kinematics.cxx index dfb2bbae..32525333 100644 --- a/src/Kinematics.cxx +++ b/src/Kinematics.cxx @@ -15,12 +15,15 @@ Kinematics::Kinematics( ) { srand(time(NULL)); - // importing from local python script for ML predictions - // requires tensorflow, energyflow packages installed -#ifdef SIDIS_MLPRED - efnpackage = py::module_::import("testEFlowimport"); - pfnimport = efnpackage.attr("eflowPredict"); -#endif + + // vectors initialized for ML predictions + //input_node_names.push_back("hfsin"); + //input_node_names.push_back("globalin"); + //output_node_names.push_back("pfnout"); + + dims = {60,7}; + dimsglobal = {10}; + // set ion mass IonMass = ProtonMass(); @@ -89,7 +92,6 @@ Kinematics::Kinematics( countPIDsmeared=countPIDtrue=0; }; - // --------------------------------- // calculates 4-momenta components of q and W (`vecQ` and `vecW`) as well as // derived invariants `W` and `nu` @@ -202,10 +204,43 @@ void Kinematics::GetQWNu_electronic(){ }; void Kinematics::GetQWNu_ML(){ - hfsinfo.clear(); + Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING,"test"); + Ort::SessionOptions session_options; + session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED); + Ort::AllocatorWithDefaultOptions allocator; + Ort::Session ORTsession(env, modelname, session_options); + + /* std::vector input_ptrs; + input_ptrs.push_back(ORTsession.GetInputNameAllocated(0,allocator)); + input_ptrs.push_back(ORTsession.GetInputNameAllocated(1,allocator)); + const char* input_node_names[] = {input_ptrs[0]->get(),input_ptrs[1]->get()}; + + std::vector output_ptrs; + output_ptrs.push_back(ORTsession.GetOutputNameAllocated(0,allocator)); + const char* output_node_names[] = {output_ptrs[0]->get()}; + */ + std::vector input_node_names, output_node_names; + for (int i = 0; i < ORTsession.GetInputCount(); i++) { + input_node_names.push_back(ORTsession.GetInputNameAllocated(i, allocator).get()); + } + + output_node_names.push_back(ORTsession.GetOutputNameAllocated(0, allocator).get()); + + + auto memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault); + std::vector ort_inputs; + ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, + input_tensor_values_hfs.data(),2, + dims.data(),dims.size() + )); + ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, + input_tensor_values_global.data(),1, + dimsglobal.data(),dimsglobal.size() + )); + + input_tensor_values_hfs.clear(); float pidadj = 0; if(nHFS >= 2){ - std::vector partHold; for(int i = 0; i < nHFS; i++){ double pidsgn=(hfspid[i]/abs(hfspid[i])); if(abs(hfspid[i])==211) pidadj = 0.4*pidsgn; @@ -213,20 +248,18 @@ void Kinematics::GetQWNu_ML(){ if(abs(hfspid[i])==321) pidadj = 0.6*pidsgn; if(abs(hfspid[i])==2212) pidadj = 0.8*pidsgn; if(abs(hfspid[i])==11) pidadj = 1.0*pidsgn; - partHold.push_back(hfseta[i]); - partHold.push_back(hfsphi[i]); - partHold.push_back(hfspx[i]); - partHold.push_back(hfspy[i]); - partHold.push_back(hfspz[i]); - partHold.push_back(hfsE[i]); - partHold.push_back(pidadj); - hfsinfo.push_back(partHold); - partHold.clear(); + input_tensor_values_hfs.push_back(hfseta[i]); + input_tensor_values_hfs.push_back(hfsphi[i]); + input_tensor_values_hfs.push_back(hfspx[i]); + input_tensor_values_hfs.push_back(hfspy[i]); + input_tensor_values_hfs.push_back(hfspz[i]); + input_tensor_values_hfs.push_back(hfsE[i]); + input_tensor_values_hfs.push_back(pidadj); } double Q2ele, Q2DA, Q2JB; double xele, xDA, xJB; TLorentzVector vecQEle; - globalinfo.clear(); + input_tensor_values_global.clear(); this->CalculateDISbyElectron(); vecQEle.SetPxPyPzE(vecQ.Px(), vecQ.Py(), vecQ.Pz(), vecQ.E()); Q2ele = Q2; @@ -238,50 +271,56 @@ void Kinematics::GetQWNu_ML(){ Q2JB = Q2; xJB = x; if( Q2DA > 0 && Q2DA < 1e4){ - globalinfo.push_back(log10(Q2DA)); + input_tensor_values_global.push_back(log10(Q2DA)); } else{ - globalinfo.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); + input_tensor_values_global.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); } if( Q2ele > 0 && Q2ele < 1e4){ - globalinfo.push_back(log10(Q2ele)); + input_tensor_values_global.push_back(log10(Q2ele)); } else{ - globalinfo.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); + input_tensor_values_global.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); } if( Q2JB > 0 && Q2JB < 1e4){ - globalinfo.push_back(log10(Q2JB)); + input_tensor_values_global.push_back(log10(Q2JB)); } else{ - globalinfo.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); + input_tensor_values_global.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); } if(xDA>0 && xDA < 1){ - globalinfo.push_back(-1*log10(xDA)); + input_tensor_values_global.push_back(-1*log10(xDA)); } else{ - globalinfo.push_back(-1*log10( (float) (rand()) / (float) (RAND_MAX/1.0) )); + input_tensor_values_global.push_back(-1*log10( (float) (rand()) / (float) (RAND_MAX/1.0) )); } if(xele>0 && xele < 1){ - globalinfo.push_back(-1*log10(xele)); + input_tensor_values_global.push_back(-1*log10(xele)); } else{ - globalinfo.push_back(-1*log10( (float) (rand()) / (float) (RAND_MAX/1.0) )); + input_tensor_values_global.push_back(-1*log10( (float) (rand()) / (float) (RAND_MAX/1.0) )); } if(xJB>0 && xJB < 1){ - globalinfo.push_back(-1*log10(xJB)); + input_tensor_values_global.push_back(-1*log10(xJB)); } else{ - globalinfo.push_back( -1*log10((float) (rand()) / (float) (RAND_MAX/1.0) ) ); + input_tensor_values_global.push_back( -1*log10((float) (rand()) / (float) (RAND_MAX/1.0) ) ); } - globalinfo.push_back(vecQEle.Px()); - globalinfo.push_back(vecQEle.Py()); - globalinfo.push_back(vecQEle.Pz()); - globalinfo.push_back(vecQEle.E()); -#ifdef SIDIS_MLPRED - py::object nnoutput = pfnimport(hfsinfo, globalinfo); - std::vector nnvecq = nnoutput.cast>(); + input_tensor_values_global.push_back(vecQEle.Px()); + input_tensor_values_global.push_back(vecQEle.Py()); + input_tensor_values_global.push_back(vecQEle.Pz()); + input_tensor_values_global.push_back(vecQEle.E()); + + //std::vector + std::vector ort_outputs = ORTsession.Run(Ort::RunOptions{nullptr}, + input_node_names.data(), + ort_inputs.data(), + ort_inputs.size(), + output_node_names.data(), + 1); + float* nnvecq = ort_outputs[0].GetTensorMutableData(); vecQ.SetPxPyPzE(nnvecq[0],nnvecq[1],nnvecq[2],nnvecq[3]); -#endif + } else{ this->CalculateDISbyElectron(); diff --git a/src/Kinematics.h b/src/Kinematics.h index 7ed87c8c..f3d3ee14 100644 --- a/src/Kinematics.h +++ b/src/Kinematics.h @@ -29,14 +29,8 @@ #ifndef EXCLUDE_DELPHES #include "classes/DelphesClasses.h" #endif -// pybind (for ML models using python packages) -#ifdef SIDIS_MLPRED -#include -#include -#include -#include -namespace py = pybind11; -#endif +// onxx (for ML models prediction in c++) +#include using std::map; using std::cout; @@ -123,7 +117,7 @@ class Kinematics TLorentzVector vecElectron, vecW, vecQ; TLorentzVector vecHadron; - // HFS tree objects + // HFS tree objects Int_t nHFS; Int_t nPi; Double_t hfspx[100]; @@ -294,14 +288,19 @@ class Kinematics Double_t rotAboutX, rotAboutY; // other TLorentzVector vecSpin, IvecSpin; -#ifdef SIDIS_MLPRED - py::object keras, tensorflow; - py::object efnpackage; - py::function pfnimport; - py::object model; - py::object modelload; - std::string modelname = "pfn_testEpic_000-2_vecQele_nHFS2_500_bs10k_bestValLoss"; -#endif + // ONNX + const char* modelname = "pfn_testEpic_000-2_vecQele_nHFS2_500_bs10k_bestValLoss"; + //std::vector input_node_names; + //std::vector output_node_names; + + std::vector> input_node_dims; + std::vector input_tensor_size; + std::vector dims; + std::vector dimsglobal; + std::vector input_tensor_values_hfs{60*7}; + std::vector input_tensor_values_global{10}; + + ClassDef(Kinematics,1); }; From 3581f165f363508613355e2b8336b89b858d49d2 Mon Sep 17 00:00:00 2001 From: cpecar Date: Tue, 2 May 2023 12:08:08 -0400 Subject: [PATCH 02/10] setting 0 padding for ML prediction --- src/Kinematics.cxx | 1 - src/Kinematics.h | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Kinematics.cxx b/src/Kinematics.cxx index 32525333..259b8445 100644 --- a/src/Kinematics.cxx +++ b/src/Kinematics.cxx @@ -311,7 +311,6 @@ void Kinematics::GetQWNu_ML(){ input_tensor_values_global.push_back(vecQEle.Pz()); input_tensor_values_global.push_back(vecQEle.E()); - //std::vector std::vector ort_outputs = ORTsession.Run(Ort::RunOptions{nullptr}, input_node_names.data(), ort_inputs.data(), diff --git a/src/Kinematics.h b/src/Kinematics.h index f3d3ee14..1b3647c5 100644 --- a/src/Kinematics.h +++ b/src/Kinematics.h @@ -290,10 +290,10 @@ class Kinematics TLorentzVector vecSpin, IvecSpin; // ONNX + // TODO: make this variable that can be input at macro level const char* modelname = "pfn_testEpic_000-2_vecQele_nHFS2_500_bs10k_bestValLoss"; - //std::vector input_node_names; - //std::vector output_node_names; - + int nPad = 60; + std::vector> input_node_dims; std::vector input_tensor_size; std::vector dims; From 2c9561f5ecfb84fb412e3a79cf88519cd59564c1 Mon Sep 17 00:00:00 2001 From: cpecar Date: Tue, 2 May 2023 12:09:33 -0400 Subject: [PATCH 03/10] script to download onnx .tar file --- deps/install_onnx.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 deps/install_onnx.sh diff --git a/deps/install_onnx.sh b/deps/install_onnx.sh new file mode 100755 index 00000000..1c0bc3eb --- /dev/null +++ b/deps/install_onnx.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2023 Christopher Dilks + +# update or download and install delphes + +### download/update source +source environ.sh + +echo "[+] downloading onnx runtime source" +wget https://github.com/microsoft/onnxruntime/releases/download/v1.14.1/onnxruntime-linux-x64-1.14.1.tgz +mv onnxruntime-linux-x64-1.14.1.tgz deps +tar -zxvf deps/onnxruntime-linux-x64-1.14.1.tgz + From b8b96ae4e01533bdc9fd61ffb44cf633e904ac85 Mon Sep 17 00:00:00 2001 From: cpecar Date: Tue, 2 May 2023 12:10:17 -0400 Subject: [PATCH 04/10] updated makefile for onnx changes (need to make onnx build optional) --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index 3b715fba..f64b7fc0 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,11 @@ ifdef INCLUDE_CENTAURO FLAGS += -DINCLUDE_CENTAURO endif +# ONNX plugin: ML model predictions +DEP_INCLUDES += -I${ONNX_HOME}/include +DEP_INCLUDES += -L${ONNX_HOME}/lib -lonnxruntime + + # MSTWPDF DEP_INCLUDES += -I${MSTWPDF_HOME} DEP_LIBRARIES += -L${MSTWPDF_HOME} -lmstwpdf From 67aca3449540c0a71d1ec6ff00043b28f271d080 Mon Sep 17 00:00:00 2001 From: cpecar Date: Tue, 2 May 2023 12:11:09 -0400 Subject: [PATCH 05/10] added onnx includes to environment setup script (need to make optional) --- environ.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/environ.sh b/environ.sh index ee09dc2d..deb1f31f 100644 --- a/environ.sh +++ b/environ.sh @@ -37,5 +37,9 @@ export ADAGE_HOME=$EPIC_ANALYSIS_HOME/deps/adage echo "ADAGE found at $ADAGE_HOME" export LD_LIBRARY_PATH=$ADAGE_HOME/lib:$LD_LIBRARY_PATH +### onnx +export ONNX_HOME=$EPIC_ANALYSIS_HOME/deps/onnxruntime-linux-x64-1.14.1 +export LD_LIBRARY_PATH=$ONNX_HOME/lib:$LD_LIBRARY_PATH + # prioritize EPIC_ANALYSIS libraries export LD_LIBRARY_PATH=$EPIC_ANALYSIS_HOME/lib:$LD_LIBRARY_PATH From aa63f2b436310aefcea7246ed08aa83e60169250 Mon Sep 17 00:00:00 2001 From: cpecar Date: Tue, 2 May 2023 16:50:43 -0400 Subject: [PATCH 06/10] Fixed errors in setting tensor dimension in onnx. Hard-coded input tensor names (need to check if onnx has some better workaround, or if I can fix these for all models) --- src/Kinematics.cxx | 85 +++++++++++++++++++++++++--------------------- src/Kinematics.h | 8 ++--- 2 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/Kinematics.cxx b/src/Kinematics.cxx index 259b8445..146d3752 100644 --- a/src/Kinematics.cxx +++ b/src/Kinematics.cxx @@ -16,14 +16,10 @@ Kinematics::Kinematics( { srand(time(NULL)); - // vectors initialized for ML predictions - //input_node_names.push_back("hfsin"); - //input_node_names.push_back("globalin"); - //output_node_names.push_back("pfnout"); - - dims = {60,7}; - dimsglobal = {10}; - + // dimension of tensors for ML pred + dims = {1,35,6}; + dimsglobal = {1,10}; + // set ion mass IonMass = ProtonMass(); @@ -204,43 +200,34 @@ void Kinematics::GetQWNu_electronic(){ }; void Kinematics::GetQWNu_ML(){ + Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING,"test"); Ort::SessionOptions session_options; session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED); Ort::AllocatorWithDefaultOptions allocator; Ort::Session ORTsession(env, modelname, session_options); - /* std::vector input_ptrs; - input_ptrs.push_back(ORTsession.GetInputNameAllocated(0,allocator)); - input_ptrs.push_back(ORTsession.GetInputNameAllocated(1,allocator)); - const char* input_node_names[] = {input_ptrs[0]->get(),input_ptrs[1]->get()}; - - std::vector output_ptrs; - output_ptrs.push_back(ORTsession.GetOutputNameAllocated(0,allocator)); - const char* output_node_names[] = {output_ptrs[0]->get()}; - */ std::vector input_node_names, output_node_names; - for (int i = 0; i < ORTsession.GetInputCount(); i++) { - input_node_names.push_back(ORTsession.GetInputNameAllocated(i, allocator).get()); + std::vector> input_shape; + /*for (int i = 0; i < ORTsession.GetInputCount(); i++) { + input_node_names.push_back(ORTsession.GetInputNameAllocated(i, allocator).get()); + input_shape.push_back(ORTsession.GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape()); + //std::cout << "input " << i << " " << input_node_names[i] << " shape: " + //<< input_shape[i][0] << " " << input_shape[i][1] << " " << input_shape[i][2] << endl; } - - output_node_names.push_back(ORTsession.GetOutputNameAllocated(0, allocator).get()); - + */ + input_node_names.push_back("input"); + input_node_names.push_back("num_global_features"); + + //output_node_names.push_back(ORTsession.GetOutputNameAllocated(0, allocator).get()); + output_node_names.push_back("activation_7"); auto memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault); std::vector ort_inputs; - ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, - input_tensor_values_hfs.data(),2, - dims.data(),dims.size() - )); - ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, - input_tensor_values_global.data(),1, - dimsglobal.data(),dimsglobal.size() - )); input_tensor_values_hfs.clear(); float pidadj = 0; - if(nHFS >= 2){ + if(nHFS >= 3){ for(int i = 0; i < nHFS; i++){ double pidsgn=(hfspid[i]/abs(hfspid[i])); if(abs(hfspid[i])==211) pidadj = 0.4*pidsgn; @@ -254,8 +241,24 @@ void Kinematics::GetQWNu_ML(){ input_tensor_values_hfs.push_back(hfspy[i]); input_tensor_values_hfs.push_back(hfspz[i]); input_tensor_values_hfs.push_back(hfsE[i]); - input_tensor_values_hfs.push_back(pidadj); + //input_tensor_values_hfs.push_back(pidadj); + } + // zero padding (fixed expected shape of input) + for(int i = nHFS; i < nPad; i++){ + input_tensor_values_hfs.push_back(0); + input_tensor_values_hfs.push_back(0); + input_tensor_values_hfs.push_back(0); + input_tensor_values_hfs.push_back(0); + input_tensor_values_hfs.push_back(0); + input_tensor_values_hfs.push_back(0); } + ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, + input_tensor_values_hfs.data(), + input_tensor_values_hfs.size(), + dims.data(), + dims.size() + )); + double Q2ele, Q2DA, Q2JB; double xele, xDA, xJB; TLorentzVector vecQEle; @@ -267,7 +270,7 @@ void Kinematics::GetQWNu_ML(){ this->CalculateDISbyDA(); Q2DA = Q2; xDA = x; - this->CalculateDISbyJB(); + this->CalculateDISbySigma(); Q2JB = Q2; xJB = x; if( Q2DA > 0 && Q2DA < 1e4){ @@ -306,17 +309,23 @@ void Kinematics::GetQWNu_ML(){ else{ input_tensor_values_global.push_back( -1*log10((float) (rand()) / (float) (RAND_MAX/1.0) ) ); } - input_tensor_values_global.push_back(vecQEle.Px()); - input_tensor_values_global.push_back(vecQEle.Py()); - input_tensor_values_global.push_back(vecQEle.Pz()); + input_tensor_values_global.push_back(vecQEle.Eta()); + input_tensor_values_global.push_back(vecQEle.Phi()); + input_tensor_values_global.push_back(vecQEle.Pt()); input_tensor_values_global.push_back(vecQEle.E()); - + ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, + input_tensor_values_global.data(), + input_tensor_values_global.size(), + dimsglobal.data(),dimsglobal.size() + )); + std::vector ort_outputs = ORTsession.Run(Ort::RunOptions{nullptr}, input_node_names.data(), ort_inputs.data(), ort_inputs.size(), - output_node_names.data(), + output_node_names.data(), 1); + float* nnvecq = ort_outputs[0].GetTensorMutableData(); vecQ.SetPxPyPzE(nnvecq[0],nnvecq[1],nnvecq[2],nnvecq[3]); diff --git a/src/Kinematics.h b/src/Kinematics.h index 1b3647c5..af0b80ee 100644 --- a/src/Kinematics.h +++ b/src/Kinematics.h @@ -291,15 +291,15 @@ class Kinematics // ONNX // TODO: make this variable that can be input at macro level - const char* modelname = "pfn_testEpic_000-2_vecQele_nHFS2_500_bs10k_bestValLoss"; - int nPad = 60; + const char* modelname = "pfn_epic22.11.2_d100_Q2_all_18x275_eleglobal_npartg3_bestValLoss.onnx"; + int nPad = 35; std::vector> input_node_dims; std::vector input_tensor_size; std::vector dims; std::vector dimsglobal; - std::vector input_tensor_values_hfs{60*7}; - std::vector input_tensor_values_global{10}; + std::vector input_tensor_values_hfs; + std::vector input_tensor_values_global; ClassDef(Kinematics,1); From c7106aec44e34ca664f977dfd71d84fe30062b70 Mon Sep 17 00:00:00 2001 From: cpecar Date: Wed, 3 May 2023 10:36:59 -0400 Subject: [PATCH 07/10] updated testml macro to reflect onnx changes --- macro/analysis_testml.C | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/macro/analysis_testml.C b/macro/analysis_testml.C index 184194f7..3fccf28d 100644 --- a/macro/analysis_testml.C +++ b/macro/analysis_testml.C @@ -2,26 +2,22 @@ // Copyright (C) 2023 Connor Pecar R__LOAD_LIBRARY(EpicAnalysis) -#include -namespace py = pybind11; // using ML prediction for vecQ, and writing out tree of HFS four-vectors // requires pybind includes above void analysis_testml( TString configFile="datarec/in.config", /* delphes tree(s) */ TString outfilePrefix="resolutions" /* output filename prefix*/ ) { - // object needed at start of script using pybind11 - py::scoped_interpreter guard{}; //outfilePrefix+="_DA"; // setup analysis ======================================== - AnalysisEcce *A = new AnalysisEcce( + AnalysisEpic *A = new AnalysisEpic( configFile, outfilePrefix ); - A->maxEvents = 10000; // use this to limit the number of events - A->writeSimpleTree = true; // write SimpleTree (for one bin) - A->writeHFSTree = true; // write HFSTree (for one bin) + A->maxEvents = 1000; // use this to limit the number of events + //A->writeSimpleTree = true; // write SimpleTree (for one bin) + //A->writeHFSTree = true; // write HFSTree (for one bin) A->SetReconMethod("ML"); // set reconstruction method A->AddFinalState("pipTrack"); // pion final state //A->AddFinalState("KpTrack"); // kaon final state From 932e1fd7e40ef3e54d9bff7bf4078f09d06974a6 Mon Sep 17 00:00:00 2001 From: cpecar Date: Wed, 3 May 2023 10:37:13 -0400 Subject: [PATCH 08/10] started script to install onnx --- deps/install_onnx.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/install_onnx.sh b/deps/install_onnx.sh index 1c0bc3eb..96050eee 100755 --- a/deps/install_onnx.sh +++ b/deps/install_onnx.sh @@ -11,5 +11,5 @@ source environ.sh echo "[+] downloading onnx runtime source" wget https://github.com/microsoft/onnxruntime/releases/download/v1.14.1/onnxruntime-linux-x64-1.14.1.tgz mv onnxruntime-linux-x64-1.14.1.tgz deps -tar -zxvf deps/onnxruntime-linux-x64-1.14.1.tgz - +tar -zxvf deps/onnxruntime-linux-x64-1.14.1.tgz -C deps/ +rm deps/onnxruntime-linux-x64-1.14.1.tgz From 477d424903e789e65b1660e6300171d07a630a6a Mon Sep 17 00:00:00 2001 From: cpecar Date: Wed, 3 May 2023 14:28:04 -0400 Subject: [PATCH 09/10] Now pass onnx file name through macro, rather than hard codin --- macro/analysis_testml.C | 6 ++++-- src/Analysis.cxx | 4 +++- src/Analysis.h | 6 ++++-- src/AnalysisEpic.cxx | 10 ++++++--- src/Kinematics.cxx | 45 +++++++++++++++++------------------------ src/Kinematics.h | 9 +++++---- 6 files changed, 42 insertions(+), 38 deletions(-) diff --git a/macro/analysis_testml.C b/macro/analysis_testml.C index 3fccf28d..378f9826 100644 --- a/macro/analysis_testml.C +++ b/macro/analysis_testml.C @@ -6,7 +6,8 @@ R__LOAD_LIBRARY(EpicAnalysis) // requires pybind includes above void analysis_testml( TString configFile="datarec/in.config", /* delphes tree(s) */ - TString outfilePrefix="resolutions" /* output filename prefix*/ + TString outfilePrefix="resolutions", /* output filename prefix*/ + TString onnxFileName="pfn_epic22.11.2_d500_Q2_1_10_18x275_eleglobal_npartg3_bestValLoss.onnx" ) { //outfilePrefix+="_DA"; // setup analysis ======================================== @@ -15,10 +16,11 @@ void analysis_testml( outfilePrefix ); - A->maxEvents = 1000; // use this to limit the number of events + //A->maxEvents = 1000; // use this to limit the number of events //A->writeSimpleTree = true; // write SimpleTree (for one bin) //A->writeHFSTree = true; // write HFSTree (for one bin) A->SetReconMethod("ML"); // set reconstruction method + A->SetModelName(onnxFileName); A->AddFinalState("pipTrack"); // pion final state //A->AddFinalState("KpTrack"); // kaon final state diff --git a/src/Analysis.cxx b/src/Analysis.cxx index f825258b..92a03cf5 100644 --- a/src/Analysis.cxx +++ b/src/Analysis.cxx @@ -320,7 +320,7 @@ void Analysis::Prepare() { // instantiate shared objects kin = std::make_shared(eleBeamEn,ionBeamEn,crossingAngle); - kinTrue = std::make_shared(eleBeamEn, ionBeamEn, crossingAngle); + kinTrue = std::make_shared(eleBeamEn, ionBeamEn, crossingAngle); #ifndef EXCLUDE_DELPHES kinJet = std::make_shared(eleBeamEn, ionBeamEn, crossingAngle); kinJetTrue = std::make_shared(eleBeamEn, ionBeamEn, crossingAngle); @@ -329,6 +329,8 @@ void Analysis::Prepare() { HFST = std::make_unique("hfstree",kin,kinTrue); PT = std::make_unique("ptree"); + kin->modelname = nnFile; + // if including jets, define a `jet` final state #ifndef EXCLUDE_DELPHES if(includeOutputSet["jets"]) { diff --git a/src/Analysis.h b/src/Analysis.h index ceb2139c..6bd5a659 100644 --- a/src/Analysis.h +++ b/src/Analysis.h @@ -70,7 +70,9 @@ class Analysis */ Bool_t useBreitJets; // if true, use Breit jets, if using finalState `jets` (requires centauro) // set kinematics reconstruction method; see constructor for available methods - void SetReconMethod(TString reconMethod_) { reconMethod=reconMethod_; }; + void SetReconMethod(TString reconMethod_) { reconMethod=reconMethod_; }; + // set ONNX file name + void SetModelName(TString onnxFile) { nnFile = onnxFile; }; // choose which output sets to include std::map includeOutputSet; // maximum number of errors to print @@ -153,7 +155,7 @@ class Analysis Double_t crossingAngle; // mrad Double_t totalCrossSection; TString reconMethod; - + TString nnFile; // event loop objects Long64_t ENT; Double_t eleP,maxEleP; diff --git a/src/AnalysisEpic.cxx b/src/AnalysisEpic.cxx index 662fbcdc..d1d5e798 100644 --- a/src/AnalysisEpic.cxx +++ b/src/AnalysisEpic.cxx @@ -13,7 +13,7 @@ void AnalysisEpic::Execute() { // setup Prepare(); - + // read EventEvaluator tree auto chain = std::make_unique("events"); for(Int_t idx=0; idxCalculateDIS(reconMethod))) continue; // reconstructed - if(!(kinTrue->CalculateDIS(reconMethod))) continue; // generated (truth) - + if( reconMethod == "ML"){ + if(!(kinTrue->CalculateDIS("ele"))) continue; // generated (truth) + } + else{ + if(!(kinTrue->CalculateDIS(reconMethod))) continue; + } // Get the weight for this event's Q2 auto Q2weightFactor = GetEventQ2Weight(kinTrue->Q2, inLookup[chain->GetTreeNumber()]); diff --git a/src/Kinematics.cxx b/src/Kinematics.cxx index 146d3752..832bc935 100644 --- a/src/Kinematics.cxx +++ b/src/Kinematics.cxx @@ -18,7 +18,7 @@ Kinematics::Kinematics( // dimension of tensors for ML pred dims = {1,35,6}; - dimsglobal = {1,10}; + dimsglobal = {1,12}; // set ion mass IonMass = ProtonMass(); @@ -200,7 +200,6 @@ void Kinematics::GetQWNu_electronic(){ }; void Kinematics::GetQWNu_ML(){ - Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING,"test"); Ort::SessionOptions session_options; session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED); @@ -209,13 +208,7 @@ void Kinematics::GetQWNu_ML(){ std::vector input_node_names, output_node_names; std::vector> input_shape; - /*for (int i = 0; i < ORTsession.GetInputCount(); i++) { - input_node_names.push_back(ORTsession.GetInputNameAllocated(i, allocator).get()); - input_shape.push_back(ORTsession.GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape()); - //std::cout << "input " << i << " " << input_node_names[i] << " shape: " - //<< input_shape[i][0] << " " << input_shape[i][1] << " " << input_shape[i][2] << endl; - } - */ + input_node_names.push_back("input"); input_node_names.push_back("num_global_features"); @@ -227,6 +220,7 @@ void Kinematics::GetQWNu_ML(){ input_tensor_values_hfs.clear(); float pidadj = 0; + if(nHFS >= 3){ for(int i = 0; i < nHFS; i++){ double pidsgn=(hfspid[i]/abs(hfspid[i])); @@ -258,7 +252,7 @@ void Kinematics::GetQWNu_ML(){ dims.data(), dims.size() )); - + double Q2ele, Q2DA, Q2JB; double xele, xDA, xJB; TLorentzVector vecQEle; @@ -273,19 +267,19 @@ void Kinematics::GetQWNu_ML(){ this->CalculateDISbySigma(); Q2JB = Q2; xJB = x; - if( Q2DA > 0 && Q2DA < 1e4){ + if( Q2DA > 0 && Q2DA < 1e6){ input_tensor_values_global.push_back(log10(Q2DA)); } else{ input_tensor_values_global.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); } - if( Q2ele > 0 && Q2ele < 1e4){ + if( Q2ele > 0 && Q2ele < 1e6){ input_tensor_values_global.push_back(log10(Q2ele)); } else{ input_tensor_values_global.push_back(log10((float) (rand()) / (float) (RAND_MAX/10000.0))); } - if( Q2JB > 0 && Q2JB < 1e4){ + if( Q2JB > 0 && Q2JB < 1e6){ input_tensor_values_global.push_back(log10(Q2JB)); } else{ @@ -309,26 +303,25 @@ void Kinematics::GetQWNu_ML(){ else{ input_tensor_values_global.push_back( -1*log10((float) (rand()) / (float) (RAND_MAX/1.0) ) ); } - input_tensor_values_global.push_back(vecQEle.Eta()); - input_tensor_values_global.push_back(vecQEle.Phi()); - input_tensor_values_global.push_back(vecQEle.Pt()); - input_tensor_values_global.push_back(vecQEle.E()); + input_tensor_values_global.push_back(vecElectron.Eta()); + input_tensor_values_global.push_back(vecElectron.Phi()); + input_tensor_values_global.push_back(vecElectron.Px()); + input_tensor_values_global.push_back(vecElectron.Py()); + input_tensor_values_global.push_back(vecElectron.Pz()); + input_tensor_values_global.push_back(vecElectron.E()); ort_inputs.push_back(Ort::Value::CreateTensor(memoryInfo, - input_tensor_values_global.data(), - input_tensor_values_global.size(), - dimsglobal.data(),dimsglobal.size() - )); - + input_tensor_values_global.data(), + input_tensor_values_global.size(), + dimsglobal.data(),dimsglobal.size() + )); std::vector ort_outputs = ORTsession.Run(Ort::RunOptions{nullptr}, - input_node_names.data(), + input_node_names.data(), ort_inputs.data(), ort_inputs.size(), output_node_names.data(), 1); - - float* nnvecq = ort_outputs[0].GetTensorMutableData(); + float* nnvecq = ort_outputs[0].GetTensorMutableData(); vecQ.SetPxPyPzE(nnvecq[0],nnvecq[1],nnvecq[2],nnvecq[3]); - } else{ this->CalculateDISbyElectron(); diff --git a/src/Kinematics.h b/src/Kinematics.h index af0b80ee..d866c078 100644 --- a/src/Kinematics.h +++ b/src/Kinematics.h @@ -239,7 +239,9 @@ class Kinematics enum mainFrame_enum {fLab, fHeadOn}; Int_t qComponentsMethod; enum qComponentsMethod_enum {qQuadratic, qHadronic, qElectronic}; - + + // onnx model name + const char* modelname; protected: // reconstruction methods @@ -289,9 +291,8 @@ class Kinematics // other TLorentzVector vecSpin, IvecSpin; - // ONNX - // TODO: make this variable that can be input at macro level - const char* modelname = "pfn_epic22.11.2_d100_Q2_all_18x275_eleglobal_npartg3_bestValLoss.onnx"; + // ONNX + //const char* modelname;// = "pfn_epic22.11.2_d100_Q2_all_18x275_eleglobal_npartg3_bestValLoss.onnx"; int nPad = 35; std::vector> input_node_dims; From c63797323cafd56cf1e4bc540c92453e12bbef65 Mon Sep 17 00:00:00 2001 From: cpecar Date: Thu, 4 May 2023 15:32:08 -0400 Subject: [PATCH 10/10] Make including onnx-runtime optional --- Makefile | 9 ++++++--- src/Kinematics.cxx | 16 ++++++++++------ src/Kinematics.h | 6 ++++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f64b7fc0..dd0b22bf 100644 --- a/Makefile +++ b/Makefile @@ -51,10 +51,13 @@ ifdef INCLUDE_CENTAURO FLAGS += -DINCLUDE_CENTAURO endif -# ONNX plugin: ML model predictions -DEP_INCLUDES += -I${ONNX_HOME}/include -DEP_INCLUDES += -L${ONNX_HOME}/lib -lonnxruntime +# ONNX plugin: ML model predictions +ifdef INCLUDE_ONNX + FLAGS += -DINCLUDE_ONNX + DEP_INCLUDES += -I${ONNX_HOME}/include + DEP_INCLUDES += -L${ONNX_HOME}/lib -lonnxruntime +endif # MSTWPDF DEP_INCLUDES += -I${MSTWPDF_HOME} diff --git a/src/Kinematics.cxx b/src/Kinematics.cxx index 832bc935..80e0d2cd 100644 --- a/src/Kinematics.cxx +++ b/src/Kinematics.cxx @@ -16,7 +16,7 @@ Kinematics::Kinematics( { srand(time(NULL)); - // dimension of tensors for ML pred + // dimension of tensors for ML predictions dims = {1,35,6}; dimsglobal = {1,12}; @@ -199,7 +199,10 @@ void Kinematics::GetQWNu_electronic(){ Nu = vecIonBeam.Dot(vecQ) / IonMass; }; + void Kinematics::GetQWNu_ML(){ + #ifdef INCLUDE_ONNX + Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING,"test"); Ort::SessionOptions session_options; session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED); @@ -212,7 +215,6 @@ void Kinematics::GetQWNu_ML(){ input_node_names.push_back("input"); input_node_names.push_back("num_global_features"); - //output_node_names.push_back(ORTsession.GetOutputNameAllocated(0, allocator).get()); output_node_names.push_back("activation_7"); auto memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault); @@ -221,7 +223,7 @@ void Kinematics::GetQWNu_ML(){ input_tensor_values_hfs.clear(); float pidadj = 0; - if(nHFS >= 3){ + if(nHFS >= 2){ for(int i = 0; i < nHFS; i++){ double pidsgn=(hfspid[i]/abs(hfspid[i])); if(abs(hfspid[i])==211) pidadj = 0.4*pidsgn; @@ -302,7 +304,7 @@ void Kinematics::GetQWNu_ML(){ } else{ input_tensor_values_global.push_back( -1*log10((float) (rand()) / (float) (RAND_MAX/1.0) ) ); - } + } input_tensor_values_global.push_back(vecElectron.Eta()); input_tensor_values_global.push_back(vecElectron.Phi()); input_tensor_values_global.push_back(vecElectron.Px()); @@ -321,7 +323,7 @@ void Kinematics::GetQWNu_ML(){ output_node_names.data(), 1); float* nnvecq = ort_outputs[0].GetTensorMutableData(); - vecQ.SetPxPyPzE(nnvecq[0],nnvecq[1],nnvecq[2],nnvecq[3]); + vecQ.SetPxPyPzE(nnvecq[0],nnvecq[1],nnvecq[2],nnvecq[3]); } else{ this->CalculateDISbyElectron(); @@ -329,7 +331,7 @@ void Kinematics::GetQWNu_ML(){ vecW = vecEleBeam + vecIonBeam - vecElectron; W = vecW.M(); Nu = vecIonBeam.Dot(vecQ) / IonMass; - + #endif } // ------------------------------------------------------ @@ -576,6 +578,8 @@ void Kinematics::AddToHFS(TLorentzVector p4_) { hfspy[nHFS] = p4.Py(); hfspz[nHFS] = p4.Pz(); hfsE[nHFS] = p4.E(); + hfseta[nHFS] = p4.Eta(); + hfsphi[nHFS] = p4.Phi(); new(ar[nHFS]) TLorentzVector(p4); nHFS++; diff --git a/src/Kinematics.h b/src/Kinematics.h index d866c078..d53a3c32 100644 --- a/src/Kinematics.h +++ b/src/Kinematics.h @@ -29,8 +29,11 @@ #ifndef EXCLUDE_DELPHES #include "classes/DelphesClasses.h" #endif -// onxx (for ML models prediction in c++) + +// onnx (for ML models prediction in c++) +#ifdef INCLUDE_ONNX #include +#endif using std::map; using std::cout; @@ -292,7 +295,6 @@ class Kinematics TLorentzVector vecSpin, IvecSpin; // ONNX - //const char* modelname;// = "pfn_epic22.11.2_d100_Q2_all_18x275_eleglobal_npartg3_bestValLoss.onnx"; int nPad = 35; std::vector> input_node_dims;