Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ParT and UParT SONIC producer second pull request #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Configuration/ProcessModifiers/python/allSonicTriton_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from Configuration.ProcessModifiers.particleNetPTSonicTriton_cff import particleNetPTSonicTriton
from Configuration.ProcessModifiers.deepMETSonicTriton_cff import deepMETSonicTriton
from Configuration.ProcessModifiers.deepTauSonicTriton_cff import deepTauSonicTriton
from Configuration.ProcessModifiers.particleTransformerAK4SonicTriton_cff import particleTransformerAK4SonicTriton
from Configuration.ProcessModifiers.unifiedparticleTransformerAK4SonicTriton_cff import unifiedparticleTransformerAK4SonicTriton


# collect all SonicTriton-related process modifiers here
allSonicTriton = cms.ModifierChain(enableSonicTriton,deepMETSonicTriton,particleNetSonicTriton,deepTauSonicTriton)
allSonicTriton = cms.ModifierChain(enableSonicTriton,deepMETSonicTriton,particleNetSonicTriton,deepTauSonicTriton,particleTransformerAK4SonicTriton,unifiedparticleTransformerAK4SonicTriton)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import FWCore.ParameterSet.Config as cms

particleTransformerAK4SonicTriton = cms.Modifier()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import FWCore.ParameterSet.Config as cms

unifiedparticleTransformerAK4SonicTriton = cms.Modifier()
1 change: 1 addition & 0 deletions RecoBTag/ONNXRuntime/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<use name="PhysicsTools/ONNXRuntime"/>
<use name="DataFormats/BTauReco"/>
<export>
<lib name="1"/>
Expand Down
83 changes: 83 additions & 0 deletions RecoBTag/ONNXRuntime/interface/tensor_configs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RecoBTag_ONNXRuntime_tensor_configs_h
#define RecoBTag_ONNXRuntime_tensor_configs_h

#include <map>

namespace deepflavour {

constexpr unsigned n_features_global = 15;
Expand Down Expand Up @@ -28,4 +30,85 @@ namespace deepvertex {

} // namespace deepvertex

namespace parT {

enum InputFeatures {
kBegin=0,
kChargedCandidates=kBegin,
kNeutralCandidates=1,
kVertices=2,
kChargedCandidates4Vec=3,
kNeutralCandidates4Vec=4,
kVertices4Vec=5,
kEnd=6
};

constexpr unsigned n_cpf_accept = 25;
constexpr unsigned n_npf_accept = 25;
constexpr unsigned n_sv_accept = 5;

const std::map<InputFeatures, unsigned int> N_InputFeatures{

Choose a reason for hiding this comment

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

this should be nInputFeatures to follow the CMS naming rules: http://cms-sw.github.io/cms_coding_rules.html#2--naming-rules-1
(similarly for the other similar variables below)

{kChargedCandidates, 16},

Choose a reason for hiding this comment

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

these could actually be vectors rather than maps, since the enum is ordered and dense. that would speed up some of the operations in the producers.

{kNeutralCandidates, 8},
{kVertices, 14},
{kChargedCandidates4Vec, 4},
{kNeutralCandidates4Vec, 4},
{kVertices4Vec, 4}
};

const std::map<InputFeatures, unsigned int> N_AcceptedFeatures{
{kChargedCandidates, n_cpf_accept},
{kNeutralCandidates, n_npf_accept},
{kVertices, n_sv_accept},
{kChargedCandidates4Vec, n_cpf_accept},
{kNeutralCandidates4Vec, n_npf_accept},
{kVertices4Vec, n_sv_accept}
};

} // namespace parT


namespace UparT {

enum InputFeatures {
kBegin=0,
kChargedCandidates=kBegin,
kLostTracks = 1,
kNeutralCandidates=2,
kVertices=3,
kChargedCandidates4Vec=4,
kLostTracks4Vec=5,
kNeutralCandidates4Vec=6,
kVertices4Vec=7,
kEnd=8
};

constexpr unsigned n_cpf_accept = 29;
constexpr unsigned n_lt_accept = 5;
constexpr unsigned n_npf_accept = 25;
constexpr unsigned n_sv_accept = 5;

const std::map<InputFeatures, unsigned int> N_InputFeatures{
{kChargedCandidates, 25},
{kLostTracks, 18},
{kNeutralCandidates, 8},
{kVertices, 14},
{kChargedCandidates4Vec, 4},
{kLostTracks4Vec, 4},
{kNeutralCandidates4Vec, 4},
{kVertices4Vec, 4}
};

const std::map<InputFeatures, unsigned int> N_AcceptedFeatures{
{kChargedCandidates, n_cpf_accept},
{kLostTracks, n_lt_accept},
{kNeutralCandidates, n_npf_accept},
{kVertices, n_sv_accept},
{kChargedCandidates4Vec, n_cpf_accept},
{kLostTracks4Vec, n_lt_accept},
{kNeutralCandidates4Vec, n_npf_accept},
{kVertices4Vec, n_sv_accept}
};

} // namespace UparT
#endif
77 changes: 77 additions & 0 deletions RecoBTag/ONNXRuntime/interface/tensor_fillers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#define RecoBTag_ONNXRuntime_tensor_fillers_h

#include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h"
#include "DataFormats/BTauReco/interface/ParticleTransformerAK4Features.h"
#include "DataFormats/BTauReco/interface/UnifiedParticleTransformerAK4Features.h"
#include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
#include "RecoBTag/ONNXRuntime/interface/tensor_configs.h"

namespace btagbtvdeep {

Expand All @@ -19,6 +23,79 @@ namespace btagbtvdeep {

void neighbourTrack_tensor_filler(float*& ptr, const btagbtvdeep::TrackPairFeatures& neighbourTrack_features);

std::vector<float> inputs_parT(const btagbtvdeep::ChargedCandidateFeatures& c_pf_features, parT::InputFeatures ifeature);

std::vector<float> inputs_parT(const btagbtvdeep::NeutralCandidateFeatures& n_pf_features, parT::InputFeatures ifeature);

std::vector<float> inputs_parT(const btagbtvdeep::SecondaryVertexFeatures& sv_features, parT::InputFeatures ifeature);

std::vector<float> inputs_UparT(const btagbtvdeep::ChargedCandidateFeatures& c_pf_features, UparT::InputFeatures ifeature);

std::vector<float> inputs_UparT(const btagbtvdeep::LostTracksFeatures& lt_features, UparT::InputFeatures ifeature);

std::vector<float> inputs_UparT(const btagbtvdeep::NeutralCandidateFeatures& n_pf_features, UparT::InputFeatures ifeature);

std::vector<float> inputs_UparT(const btagbtvdeep::SecondaryVertexFeatures& sv_features, UparT::InputFeatures ifeature);

template<class parT_features>
void parT_tensor_filler(cms::Ort::FloatArrays& data, const parT::InputFeatures ifeature, const std::vector<parT_features>& features, const unsigned int max_n, const float*& start, unsigned offset) {

Choose a reason for hiding this comment

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

make sure to run these commands before submitting the official PR:

scram b code-checks
scram b code-format

float* ptr = nullptr;
for (std::size_t n = 0; n < max_n; n++) {
const auto& f = features.at(n);
ptr = &data[ifeature][offset + n * parT::N_InputFeatures.at(ifeature)];
start = ptr;
const std::vector<float>& inputs = inputs_parT(f, ifeature);
for (unsigned int i = 0; i < inputs.size(); i++) {
*ptr = inputs[i];
++ptr;
}
if (inputs.size() > 0) --ptr;
assert(start + parT::N_InputFeatures.at(ifeature) - 1 == ptr);
}
}

template<class parT_features>
void parT_tensor_filler(std::vector<float>& vdata, const parT::InputFeatures ifeature, const std::vector<parT_features>& features, const unsigned int target_n) {
unsigned int n = std::clamp((unsigned int)features.size(), (unsigned int)0, (unsigned int)parT::N_AcceptedFeatures.at(ifeature));
for (unsigned int count = 0; count < n; count++) {
const std::vector<float>& inputs = inputs_parT(features.at(count), ifeature);
vdata.insert(vdata.end(), inputs.begin(), inputs.end());
}
unsigned int n_features = parT::N_InputFeatures.at(ifeature);
if (n < target_n)
vdata.insert(vdata.end(), (target_n - n) * n_features, 0); // Add 0 to unfilled part as padding value
}

template<class UparT_features>
void UparT_tensor_filler(cms::Ort::FloatArrays& data, const UparT::InputFeatures ifeature, const std::vector<UparT_features>& features, const unsigned int max_n, const float*& start, unsigned offset) {
float* ptr = nullptr;
for (std::size_t n = 0; n < max_n; n++) {
const auto& f = features.at(n);
ptr = &data[ifeature][offset + n * UparT::N_InputFeatures.at(ifeature)];
start = ptr;
const std::vector<float>& inputs = inputs_UparT(f, ifeature);
for (unsigned int i = 0; i < inputs.size(); i++) {
*ptr = inputs[i];
++ptr;
}
if (inputs.size() > 0) --ptr;
assert(start + UparT::N_InputFeatures.at(ifeature) - 1 == ptr);
}
}

template<class UparT_features>
void UparT_tensor_filler(std::vector<float>& vdata, const UparT::InputFeatures ifeature, const std::vector<UparT_features>& features, const unsigned int target_n) {
unsigned int n = std::clamp((unsigned int)features.size(), (unsigned int)0, (unsigned int)UparT::N_AcceptedFeatures.at(ifeature));
for (unsigned int count = 0; count < n; count++) {
const std::vector<float>& inputs = inputs_UparT(features.at(count), ifeature);
vdata.insert(vdata.end(), inputs.begin(), inputs.end());
}
unsigned int n_features = UparT::N_InputFeatures.at(ifeature);
if (n < target_n)
vdata.insert(vdata.end(), (target_n - n) * n_features, 0); // Add 0 to unfilled part as padding value
}


} // namespace btagbtvdeep

#endif
Loading