Skip to content

Commit

Permalink
Merge pull request cms-sw#11 from wpmccormack/CMSSW_12_0_0_pre5_halfR…
Browse files Browse the repository at this point in the history
…agged2

Adding pseudo-ragged batching to particlenet
  • Loading branch information
kpedro88 authored Sep 1, 2021
2 parents 28564a2 + c1a914b commit fadbcda
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 24 deletions.
11 changes: 11 additions & 0 deletions RecoBTag/FeatureTools/interface/deep_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ namespace btagbtvdeep {
float min = 0,
float max = -1);

int center_norm_pad_halfRagged(const std::vector<float> &input,
float center,
float scale,
unsigned target_length,
std::vector<float> &datavec,
int startval,
float pad_value = 0,
float replace_inf_value = 0,
float min = 0,
float max = -1);

void ParticleNetConstructor(const edm::ParameterSet &Config_,
bool doExtra,
std::vector<std::string> &input_names_,
Expand Down
22 changes: 22 additions & 0 deletions RecoBTag/FeatureTools/src/deep_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ namespace btagbtvdeep {
return target_length;
}

int center_norm_pad_halfRagged(const std::vector<float> &input,
float center,
float norm_factor,
unsigned target_length,
std::vector<float> &datavec,
int startval,
float pad_value,
float replace_inf_value,
float min,
float max) {
// do variable shifting/scaling/padding/clipping in one go

assert(min <= pad_value && pad_value <= max);

for (unsigned i = 0; i < input.size(); ++i) {
datavec.push_back(std::clamp((catch_infs(input[i], replace_inf_value) - center) * norm_factor, min, max));
}
datavec.insert(datavec.end(), target_length - input.size(), pad_value);

return target_length;
}

void ParticleNetConstructor(const edm::ParameterSet &Config_,
bool doExtra,
std::vector<std::string> &input_names_,
Expand Down
79 changes: 59 additions & 20 deletions RecoBTag/ONNXRuntime/plugins/ParticleNetSonicJetTagsProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ParticleNetSonicJetTagsProducer : public TritonEDProducer<> {
std::vector<unsigned> input_sizes_; // total length of each input vector
std::unordered_map<std::string, PreprocessParams> prep_info_map_; // preprocessing info for each input group
bool debug_ = false;
bool skippedInference_ = false;
constexpr static unsigned numParticleGroups_ = 3;
};

ParticleNetSonicJetTagsProducer::ParticleNetSonicJetTagsProducer(const edm::ParameterSet &iConfig)
Expand Down Expand Up @@ -128,10 +130,39 @@ void ParticleNetSonicJetTagsProducer::acquire(edm::Event const &iEvent, edm::Eve
edm::Handle<TagInfoCollection> tag_infos;
iEvent.getByToken(src_, tag_infos);
client_->setBatchSize(tag_infos->size());
skippedInference_ = false;
if (!tag_infos->empty()) {
unsigned int maxParticles = 0;
unsigned int maxVertices = 0;
for (unsigned jet_n = 0; jet_n < tag_infos->size(); ++jet_n) {
maxParticles = std::max(maxParticles,
static_cast<unsigned int>(((*tag_infos)[jet_n]).features().get("pfcand_etarel").size()));
maxVertices =
std::max(maxVertices, static_cast<unsigned int>(((*tag_infos)[jet_n]).features().get("sv_etarel").size()));
}
if (maxParticles == 0 && maxVertices == 0) {
client_->setBatchSize(0);
skippedInference_ = true;
return;
}
unsigned int minPartFromJSON = prep_info_map_.at(input_names_[0]).min_length;
unsigned int maxPartFromJSON = prep_info_map_.at(input_names_[0]).max_length;
unsigned int minVertFromJSON = prep_info_map_.at(input_names_[3]).min_length;
unsigned int maxVertFromJSON = prep_info_map_.at(input_names_[3]).max_length;
maxParticles = std::clamp(maxParticles, minPartFromJSON, maxPartFromJSON);
maxVertices = std::clamp(maxVertices, minVertFromJSON, maxVertFromJSON);

for (unsigned igroup = 0; igroup < input_names_.size(); ++igroup) {
const auto &group_name = input_names_[igroup];
auto &input = iInput.at(group_name);
unsigned target;
if (igroup < numParticleGroups_) {
input.setShape(1, maxParticles);
target = maxParticles;
} else {
input.setShape(1, maxVertices);
target = maxVertices;
}
auto tdata = input.allocate<float>(true);
for (unsigned jet_n = 0; jet_n < tag_infos->size(); ++jet_n) {
const auto &taginfo = (*tag_infos)[jet_n];
Expand All @@ -143,17 +174,16 @@ void ParticleNetSonicJetTagsProducer::acquire(edm::Event const &iEvent, edm::Eve
const auto &varname = prep_params.var_names[i];
const auto &raw_value = taginfo.features().get(varname);
const auto &info = prep_params.info(varname);
int insize = center_norm_pad(raw_value,
info.center,
info.norm_factor,
prep_params.min_length,
prep_params.max_length,
vdata,
curr_pos,
info.pad,
info.replace_inf_value,
info.lower_bound,
info.upper_bound);
int insize = center_norm_pad_halfRagged(raw_value,
info.center,
info.norm_factor,
target,
vdata,
curr_pos,
info.pad,
info.replace_inf_value,
info.lower_bound,
info.upper_bound);
curr_pos += insize;
if (i == 0 && (!input_shapes_.empty())) {
input_shapes_[igroup][2] = insize;
Expand Down Expand Up @@ -195,18 +225,27 @@ void ParticleNetSonicJetTagsProducer::produce(edm::Event &iEvent,
}

if (!tag_infos->empty()) {
const auto &output1 = iOutput.begin()->second;
const auto &outputs_from_server = output1.fromServer<float>();
if (!skippedInference_) {
const auto &output1 = iOutput.begin()->second;
const auto &outputs_from_server = output1.fromServer<float>();

for (unsigned jet_n = 0; jet_n < tag_infos->size(); ++jet_n) {
const auto &taginfo = (*tag_infos)[jet_n];
const auto &jet_ref = tag_infos->at(jet_n).jet();
for (unsigned jet_n = 0; jet_n < tag_infos->size(); ++jet_n) {
const auto &taginfo = (*tag_infos)[jet_n];
const auto &jet_ref = tag_infos->at(jet_n).jet();

if (!taginfo.features().empty()) {
for (std::size_t flav_n = 0; flav_n < flav_names_.size(); flav_n++) {
(*(output_tags[flav_n]))[jet_ref] = outputs_from_server[jet_n][flav_n];
if (!taginfo.features().empty()) {
for (std::size_t flav_n = 0; flav_n < flav_names_.size(); flav_n++) {
(*(output_tags[flav_n]))[jet_ref] = outputs_from_server[jet_n][flav_n];
}
} else {
for (std::size_t flav_n = 0; flav_n < flav_names_.size(); flav_n++) {
(*(output_tags[flav_n]))[jet_ref] = 0.;
}
}
} else {
}
} else {
for (unsigned jet_n = 0; jet_n < tag_infos->size(); ++jet_n) {
const auto &jet_ref = tag_infos->at(jet_n).jet();
for (std::size_t flav_n = 0; flav_n < flav_names_.size(); flav_n++) {
(*(output_tags[flav_n]))[jet_ref] = 0.;
}
Expand Down
2 changes: 1 addition & 1 deletion RecoBTag/ONNXRuntime/python/pfParticleNetAK4_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

particleNetSonicTriton.toReplaceWith(pfParticleNetAK4JetTags, _particleNetSonicJetTagsProducer.clone(
src = 'pfParticleNetAK4TagInfos',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK4/CHS/V00/preprocess_noragged.json',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK4/CHS/V00/preprocess.json',
Client = cms.PSet(
timeout = cms.untracked.uint32(300),
mode = cms.string("Async"),
Expand Down
6 changes: 3 additions & 3 deletions RecoBTag/ONNXRuntime/python/pfParticleNet_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

particleNetSonicTriton.toReplaceWith(pfParticleNetJetTags, _particleNetSonicJetTagsProducer.clone(
src = 'pfParticleNetTagInfos',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/General/V01/preprocess_noragged.json',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/General/V01/preprocess.json',
Client = cms.PSet(
timeout = cms.untracked.uint32(300),
mode = cms.string("Async"),
Expand All @@ -47,7 +47,7 @@

particleNetSonicTriton.toReplaceWith(pfMassDecorrelatedParticleNetJetTags, _particleNetSonicJetTagsProducer.clone(
src = 'pfParticleNetTagInfos',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/MD-2prong/V01/preprocess_noragged.json',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/MD-2prong/V01/preprocess.json',
Client = cms.PSet(
timeout = cms.untracked.uint32(300),
modelName = cms.string("particlenet_AK8_MD-2prong"),
Expand All @@ -69,7 +69,7 @@

particleNetSonicTriton.toReplaceWith(pfParticleNetMassRegressionJetTags, _particleNetSonicJetTagsProducer.clone(
src = 'pfParticleNetTagInfos',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/MassRegression/V01/preprocess_noragged.json',
preprocess_json = 'RecoBTag/Combined/data/ParticleNetAK8/MassRegression/V01/preprocess.json',
Client = cms.PSet(
timeout = cms.untracked.uint32(300),
modelName = cms.string("particlenet_AK8_MassRegression"),
Expand Down

0 comments on commit fadbcda

Please sign in to comment.