forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 1
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 sonic-ize particle transformer to CMSSW_14_1_0_pre0 #15
Open
y19y19
wants to merge
9
commits into
fastmachinelearning:master
Choose a base branch
from
y19y19:CMSSW_14_1_0_pre0_testParT
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fcb4d88
add SONIC-ize particleTransformer
y19y19 7fabe9f
add particle transformer modofier
y19y19 0301af1
move common input for particle transformer with/without sonic to inte…
y19y19 a5b07c8
respond to code comments
y19y19 35d7a18
remove InputIndexes and loop through enum
y19y19 2b6404d
code patch
y19y19 28d81de
remove unnecessary comments
y19y19 7a4032e
resolve comments to make variables inline and use array instead of map
y19y19 df4fe14
update format
y19y19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Configuration/ProcessModifiers/python/particleTransformerAK4SonicTriton_cff.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
particleTransformerAK4SonicTriton = cms.Modifier() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
#include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" | ||
|
||
#include "RecoBTag/ONNXRuntime/interface/tensor_fillers.h" | ||
#include "RecoBTag/ONNXRuntime/interface/tensor_configs.h" | ||
|
||
using namespace cms::Ort; | ||
|
||
class ParticleTransformerAK4ONNXJetTagsProducer : public edm::stream::EDProducer<edm::GlobalCache<ONNXRuntime>> { | ||
|
@@ -27,7 +30,7 @@ class ParticleTransformerAK4ONNXJetTagsProducer : public edm::stream::EDProducer | |
|
||
static std::unique_ptr<ONNXRuntime> initializeGlobalCache(const edm::ParameterSet&); | ||
static void globalEndJob(const ONNXRuntime*); | ||
|
||
private: | ||
typedef std::vector<reco::ParticleTransformerAK4TagInfo> TagInfoCollection; | ||
typedef reco::JetTagCollection JetTagCollection; | ||
|
@@ -41,24 +44,9 @@ class ParticleTransformerAK4ONNXJetTagsProducer : public edm::stream::EDProducer | |
std::vector<std::string> flav_names_; | ||
std::vector<std::string> input_names_; | ||
std::vector<std::string> output_names_; | ||
|
||
enum InputIndexes { | ||
kChargedCandidates = 0, | ||
kNeutralCandidates = 1, | ||
kVertices = 2, | ||
kChargedCandidates4Vec = 3, | ||
kNeutralCandidates4Vec = 4, | ||
kVertices4Vec = 5 | ||
}; | ||
unsigned n_cpf_; | ||
constexpr static unsigned n_features_cpf_ = 16; | ||
constexpr static unsigned n_pairwise_features_cpf_ = 4; | ||
unsigned n_npf_; | ||
constexpr static unsigned n_features_npf_ = 8; | ||
constexpr static unsigned n_pairwise_features_npf_ = 4; | ||
unsigned n_sv_; | ||
constexpr static unsigned n_features_sv_ = 14; | ||
constexpr static unsigned n_pairwise_features_sv_ = 4; | ||
unsigned int n_cpf_; | ||
unsigned int n_npf_; | ||
unsigned int n_sv_; | ||
std::vector<unsigned> input_sizes_; | ||
std::vector<std::vector<int64_t>> input_shapes_; // shapes of each input group (-1 for dynamic axis) | ||
|
||
|
@@ -84,7 +72,7 @@ void ParticleTransformerAK4ONNXJetTagsProducer::fillDescriptions(edm::Configurat | |
desc.add<edm::InputTag>("src", edm::InputTag("pfParticleTransformerAK4TagInfos")); | ||
desc.add<std::vector<std::string>>("input_names", {"input_1", "input_2", "input_3", "input_4", "input_5", "input_6"}); | ||
desc.add<edm::FileInPath>("model_path", | ||
edm::FileInPath("RecoBTag/Combined/data/RobustParTAK4/PUPPI/V00/RobustParTAK4.onnx")); | ||
edm::FileInPath("RecoBTag/Combined/data/RobustParTAK4/PUPPI/V00/modelfile/model.onnx")); | ||
desc.add<std::vector<std::string>>("output_names", {"softmax"}); | ||
desc.add<std::vector<std::string>>( | ||
"flav_names", std::vector<std::string>{"probb", "probbb", "problepb", "probc", "probuds", "probg"}); | ||
|
@@ -124,12 +112,12 @@ void ParticleTransformerAK4ONNXJetTagsProducer::produce(edm::Event& iEvent, cons | |
get_input_sizes(taginfo); | ||
|
||
// run prediction with dynamic batch size per event | ||
input_shapes_ = {{(int64_t)1, (int64_t)n_cpf_, (int64_t)n_features_cpf_}, | ||
{(int64_t)1, (int64_t)n_npf_, (int64_t)n_features_npf_}, | ||
{(int64_t)1, (int64_t)n_sv_, (int64_t)n_features_sv_}, | ||
{(int64_t)1, (int64_t)n_cpf_, (int64_t)n_pairwise_features_cpf_}, | ||
{(int64_t)1, (int64_t)n_npf_, (int64_t)n_pairwise_features_npf_}, | ||
{(int64_t)1, (int64_t)n_sv_, (int64_t)n_pairwise_features_sv_}}; | ||
input_shapes_ = {{(int64_t)1, (int64_t)n_cpf_, (int64_t)parT::N_InputFeatures.at(parT::kChargedCandidates)}, | ||
{(int64_t)1, (int64_t)n_npf_, (int64_t)parT::N_InputFeatures.at(parT::kNeutralCandidates)}, | ||
{(int64_t)1, (int64_t)n_sv_, (int64_t)parT::N_InputFeatures.at(parT::kVertices)}, | ||
{(int64_t)1, (int64_t)n_cpf_, (int64_t)parT::N_InputFeatures.at(parT::kChargedCandidates4Vec)}, | ||
{(int64_t)1, (int64_t)n_npf_, (int64_t)parT::N_InputFeatures.at(parT::kNeutralCandidates4Vec)}, | ||
{(int64_t)1, (int64_t)n_sv_, (int64_t)parT::N_InputFeatures.at(parT::kVertices4Vec)}}; | ||
|
||
outputs = globalCache()->run(input_names_, data_, input_shapes_, output_names_, 1)[0]; | ||
assert(outputs.size() == flav_names_.size()); | ||
|
@@ -151,24 +139,17 @@ void ParticleTransformerAK4ONNXJetTagsProducer::get_input_sizes( | |
const reco::FeaturesTagInfo<btagbtvdeep::ParticleTransformerAK4Features> taginfo) { | ||
const auto& features = taginfo.features(); | ||
|
||
unsigned int n_cpf = features.c_pf_features.size(); | ||
unsigned int n_npf = features.n_pf_features.size(); | ||
unsigned int n_vtx = features.sv_features.size(); | ||
|
||
n_cpf_ = std::max((unsigned int)1, n_cpf); | ||
n_npf_ = std::max((unsigned int)1, n_npf); | ||
n_sv_ = std::max((unsigned int)1, n_vtx); | ||
n_cpf_ = std::clamp((unsigned int)features.c_pf_features.size(), (unsigned int)1, (unsigned int)parT::n_cpf_accept); | ||
n_npf_ = std::clamp((unsigned int)features.n_pf_features.size(), (unsigned int)1, (unsigned int)parT::n_npf_accept); | ||
n_sv_ = std::clamp((unsigned int)features.sv_features.size(), (unsigned int)1, (unsigned int)parT::n_sv_accept); | ||
|
||
n_cpf_ = std::min((unsigned int)25, n_cpf_); | ||
n_npf_ = std::min((unsigned int)25, n_npf_); | ||
n_sv_ = std::min((unsigned int)5, n_sv_); | ||
input_sizes_ = { | ||
n_cpf_ * n_features_cpf_, | ||
n_npf_ * n_features_npf_, | ||
n_sv_ * n_features_sv_, | ||
n_cpf_ * n_pairwise_features_cpf_, | ||
n_npf_ * n_pairwise_features_npf_, | ||
n_sv_ * n_pairwise_features_sv_, | ||
n_cpf_ * parT::N_InputFeatures.at(parT::kChargedCandidates), | ||
n_npf_ * parT::N_InputFeatures.at(parT::kNeutralCandidates), | ||
n_sv_ * parT::N_InputFeatures.at(parT::kVertices), | ||
n_cpf_ * parT::N_InputFeatures.at(parT::kChargedCandidates4Vec), | ||
n_npf_ * parT::N_InputFeatures.at(parT::kNeutralCandidates4Vec), | ||
n_sv_ * parT::N_InputFeatures.at(parT::kVertices4Vec), | ||
}; | ||
// init data storage | ||
data_.clear(); | ||
|
@@ -180,116 +161,26 @@ void ParticleTransformerAK4ONNXJetTagsProducer::get_input_sizes( | |
} | ||
|
||
void ParticleTransformerAK4ONNXJetTagsProducer::make_inputs(btagbtvdeep::ParticleTransformerAK4Features features) { | ||
float* ptr = nullptr; | ||
//float* ptr = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete commented-out code |
||
const float* start = nullptr; | ||
unsigned offset = 0; | ||
|
||
// c_pf candidates | ||
auto max_c_pf_n = std::min(features.c_pf_features.size(), (std::size_t)n_cpf_); | ||
for (std::size_t c_pf_n = 0; c_pf_n < max_c_pf_n; c_pf_n++) { | ||
const auto& c_pf_features = features.c_pf_features.at(c_pf_n); | ||
ptr = &data_[kChargedCandidates][offset + c_pf_n * n_features_cpf_]; | ||
start = ptr; | ||
*ptr = c_pf_features.btagPf_trackEtaRel; | ||
*(++ptr) = c_pf_features.btagPf_trackPtRel; | ||
*(++ptr) = c_pf_features.btagPf_trackPPar; | ||
*(++ptr) = c_pf_features.btagPf_trackDeltaR; | ||
*(++ptr) = c_pf_features.btagPf_trackPParRatio; | ||
*(++ptr) = c_pf_features.btagPf_trackSip2dVal; | ||
*(++ptr) = c_pf_features.btagPf_trackSip2dSig; | ||
*(++ptr) = c_pf_features.btagPf_trackSip3dVal; | ||
*(++ptr) = c_pf_features.btagPf_trackSip3dSig; | ||
*(++ptr) = c_pf_features.btagPf_trackJetDistVal; | ||
*(++ptr) = c_pf_features.ptrel; | ||
*(++ptr) = c_pf_features.drminsv; | ||
*(++ptr) = c_pf_features.vtx_ass; | ||
*(++ptr) = c_pf_features.puppiw; | ||
*(++ptr) = c_pf_features.chi2; | ||
*(++ptr) = c_pf_features.quality; | ||
assert(start + n_features_cpf_ - 1 == ptr); | ||
} | ||
|
||
// n_pf candidates | ||
auto max_n_pf_n = std::min(features.n_pf_features.size(), (std::size_t)n_npf_); | ||
for (std::size_t n_pf_n = 0; n_pf_n < max_n_pf_n; n_pf_n++) { | ||
const auto& n_pf_features = features.n_pf_features.at(n_pf_n); | ||
ptr = &data_[kNeutralCandidates][offset + n_pf_n * n_features_npf_]; | ||
start = ptr; | ||
*ptr = n_pf_features.ptrel; | ||
*(++ptr) = n_pf_features.etarel; | ||
*(++ptr) = n_pf_features.phirel; | ||
*(++ptr) = n_pf_features.deltaR; | ||
*(++ptr) = n_pf_features.isGamma; | ||
*(++ptr) = n_pf_features.hadFrac; | ||
*(++ptr) = n_pf_features.drminsv; | ||
*(++ptr) = n_pf_features.puppiw; | ||
assert(start + n_features_npf_ - 1 == ptr); | ||
} | ||
|
||
// sv candidates | ||
auto max_sv_n = std::min(features.sv_features.size(), (std::size_t)n_sv_); | ||
for (std::size_t sv_n = 0; sv_n < max_sv_n; sv_n++) { | ||
const auto& sv_features = features.sv_features.at(sv_n); | ||
ptr = &data_[kVertices][offset + sv_n * n_features_sv_]; | ||
start = ptr; | ||
*ptr = sv_features.pt; | ||
*(++ptr) = sv_features.deltaR; | ||
*(++ptr) = sv_features.mass; | ||
*(++ptr) = sv_features.etarel; | ||
*(++ptr) = sv_features.phirel; | ||
*(++ptr) = sv_features.ntracks; | ||
*(++ptr) = sv_features.chi2; | ||
*(++ptr) = sv_features.normchi2; | ||
*(++ptr) = sv_features.dxy; | ||
*(++ptr) = sv_features.dxysig; | ||
*(++ptr) = sv_features.d3d; | ||
*(++ptr) = sv_features.d3dsig; | ||
*(++ptr) = sv_features.costhetasvpv; | ||
*(++ptr) = sv_features.enratio; | ||
assert(start + n_features_sv_ - 1 == ptr); | ||
} | ||
|
||
// c_pf candidates | ||
parT_tensor_filler(data_, parT::kChargedCandidates, features.c_pf_features, max_c_pf_n, start, offset); | ||
// n_pf candidates | ||
parT_tensor_filler(data_, parT::kNeutralCandidates, features.n_pf_features, max_n_pf_n, start, offset); | ||
// sv candidates | ||
parT_tensor_filler(data_, parT::kVertices, features.sv_features, max_sv_n, start, offset); | ||
// cpf pairwise features (4-vectors) | ||
auto max_cpf_n = std::min(features.c_pf_features.size(), (std::size_t)n_cpf_); | ||
for (std::size_t cpf_n = 0; cpf_n < max_cpf_n; cpf_n++) { | ||
const auto& cpf_pairwise_features = features.c_pf_features.at(cpf_n); | ||
ptr = &data_[kChargedCandidates4Vec][offset + cpf_n * n_pairwise_features_cpf_]; | ||
start = ptr; | ||
*ptr = cpf_pairwise_features.px; | ||
*(++ptr) = cpf_pairwise_features.py; | ||
*(++ptr) = cpf_pairwise_features.pz; | ||
*(++ptr) = cpf_pairwise_features.e; | ||
|
||
assert(start + n_pairwise_features_cpf_ - 1 == ptr); | ||
} | ||
|
||
parT_tensor_filler(data_, parT::kChargedCandidates4Vec, features.c_pf_features, max_c_pf_n, start, offset); | ||
// npf pairwise features (4-vectors) | ||
auto max_npf_n = std::min(features.n_pf_features.size(), (std::size_t)n_npf_); | ||
for (std::size_t npf_n = 0; npf_n < max_npf_n; npf_n++) { | ||
const auto& npf_pairwise_features = features.n_pf_features.at(npf_n); | ||
ptr = &data_[kNeutralCandidates4Vec][offset + npf_n * n_pairwise_features_npf_]; | ||
start = ptr; | ||
*ptr = npf_pairwise_features.px; | ||
*(++ptr) = npf_pairwise_features.py; | ||
*(++ptr) = npf_pairwise_features.pz; | ||
*(++ptr) = npf_pairwise_features.e; | ||
|
||
assert(start + n_pairwise_features_npf_ - 1 == ptr); | ||
} | ||
|
||
parT_tensor_filler(data_, parT::kNeutralCandidates4Vec, features.n_pf_features, max_n_pf_n, start, offset); | ||
// sv pairwise features (4-vectors) | ||
auto max_sv_N = std::min(features.sv_features.size(), (std::size_t)n_sv_); | ||
for (std::size_t sv_N = 0; sv_N < max_sv_N; sv_N++) { | ||
const auto& sv_pairwise_features = features.sv_features.at(sv_N); | ||
ptr = &data_[kVertices4Vec][offset + sv_N * n_pairwise_features_sv_]; | ||
start = ptr; | ||
*ptr = sv_pairwise_features.px; | ||
*(++ptr) = sv_pairwise_features.py; | ||
*(++ptr) = sv_pairwise_features.pz; | ||
*(++ptr) = sv_pairwise_features.e; | ||
|
||
assert(start + n_pairwise_features_sv_ - 1 == ptr); | ||
} | ||
parT_tensor_filler(data_, parT::kVertices4Vec, features.sv_features, max_sv_n, start, offset); | ||
} | ||
|
||
//define this as a plug-in | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as a method to loop through all the enum values, this is somewhat redundant (requires adding the same information in two places: the enum definition and this map). I checked with core software and they recommend something like the following:
then you can just loop from
kBegin
tokEnd
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay that will help...