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

DeepTau - Do not call TF inference with empty grid #44455

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,16 @@ void L2TauNNProducer::fillPatatracks(tensorflow::Tensor& cellGridMatrix,
}

std::vector<float> L2TauNNProducer::getTauScore(const tensorflow::Tensor& cellGridMatrix) {
std::vector<tensorflow::Tensor> pred_tensor;
tensorflow::run(L2cacheData_->session, {{inputTensorName_, cellGridMatrix}}, {outputTensorName_}, &pred_tensor);
const int nTau = cellGridMatrix.shape().dim_size(0);
std::vector<float> pred_vector(nTau);
for (int tau_idx = 0; tau_idx < nTau; ++tau_idx) {
pred_vector[tau_idx] = pred_tensor[0].matrix<float>()(tau_idx, 0);
if (nTau > 0) {
// Only run the inference if there are taus to process
std::vector<tensorflow::Tensor> pred_tensor;
tensorflow::run(L2cacheData_->session, {{inputTensorName_, cellGridMatrix}}, {outputTensorName_}, &pred_tensor);
for (int tau_idx = 0; tau_idx < nTau; ++tau_idx) {
pred_vector[tau_idx] = pred_tensor[0].matrix<float>()(tau_idx, 0);
}
}

return pred_vector;
}

Expand Down
113 changes: 62 additions & 51 deletions RecoTauTag/RecoTau/plugins/DeepTauId.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ class DeepTauId : public DeepTauIdBase<DeepTauIdWrapper> {
{"outer_all_dropout_4/Identity"},
&pred_vector);
}

return pred_vector.at(0);
}

Expand All @@ -547,67 +548,77 @@ class DeepTauId : public DeepTauIdBase<DeepTauIdWrapper> {
bool is_inner) {
if (debug_level >= 2) {
std::cout << "<DeepTauId::createConvFeatures (is_inner = " << is_inner << ")>:" << std::endl;
std::cout << "number of valid cells = " << grid.num_valid_cells() << std::endl;
}

const size_t n_valid_cells = grid.num_valid_cells();
tensorflow::Tensor predTensor;
tensorflow::Tensor& convTensor = *convTensor_.at(is_inner);
eGammaTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::EgammaBlockInputs::NumberOfInputs});
muonTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::MuonBlockInputs::NumberOfInputs});
hadronsTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::HadronBlockInputs::NumberOfInputs});

eGammaTensor_[is_inner]->flat<float>().setZero();
muonTensor_[is_inner]->flat<float>().setZero();
hadronsTensor_[is_inner]->flat<float>().setZero();

unsigned idx = 0;
for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) {
for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) {
if (debug_level >= 2) {
std::cout << "processing ( eta = " << eta << ", phi = " << phi << " )" << std::endl;
}
const CellIndex cell_index{eta, phi};
const auto cell_iter = grid.find(cell_index);
if (cell_iter != grid.end()) {
//check if at least one input is there to
//avoid calling TF with empty grid #TODO understand why the grid is empty
if (n_valid_cells > 0) {
eGammaTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::EgammaBlockInputs::NumberOfInputs});
muonTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::MuonBlockInputs::NumberOfInputs});
hadronsTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
tensorflow::DT_FLOAT,
tensorflow::TensorShape{
(long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::HadronBlockInputs::NumberOfInputs});

eGammaTensor_[is_inner]->flat<float>().setZero();
muonTensor_[is_inner]->flat<float>().setZero();
hadronsTensor_[is_inner]->flat<float>().setZero();

unsigned idx = 0;
for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) {
for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) {
if (debug_level >= 2) {
std::cout << " creating inputs for ( eta = " << eta << ", phi = " << phi << " ): idx = " << idx
<< std::endl;
std::cout << "processing ( eta = " << eta << ", phi = " << phi << " )" << std::endl;
}
const Cell& cell = cell_iter->second;
createEgammaBlockInputs<CandidateCastType>(idx,
tau,
tau_index,
tau_ref,
pv,
rho,
electrons,
pfCands,
cell,
tau_funcs,
is_inner,
*eGammaTensor_[is_inner]);
createMuonBlockInputs<CandidateCastType>(
idx, tau, tau_index, tau_ref, pv, rho, muons, pfCands, cell, tau_funcs, is_inner, *muonTensor_[is_inner]);
createHadronsBlockInputs<CandidateCastType>(
idx, tau, tau_index, tau_ref, pv, rho, pfCands, cell, tau_funcs, is_inner, *hadronsTensor_[is_inner]);
idx += 1;
} else {
if (debug_level >= 2) {
std::cout << " skipping creation of inputs, because ( eta = " << eta << ", phi = " << phi
<< " ) is not in the grid !!" << std::endl;
const CellIndex cell_index{eta, phi};
const auto cell_iter = grid.find(cell_index);
if (cell_iter != grid.end()) {
if (debug_level >= 2) {
std::cout << " creating inputs for ( eta = " << eta << ", phi = " << phi << " ): idx = " << idx
<< std::endl;
}
const Cell& cell = cell_iter->second;
createEgammaBlockInputs<CandidateCastType>(idx,
tau,
tau_index,
tau_ref,
pv,
rho,
electrons,
pfCands,
cell,
tau_funcs,
is_inner,
*eGammaTensor_[is_inner]);
createMuonBlockInputs<CandidateCastType>(
idx, tau, tau_index, tau_ref, pv, rho, muons, pfCands, cell, tau_funcs, is_inner, *muonTensor_[is_inner]);
createHadronsBlockInputs<CandidateCastType>(
idx, tau, tau_index, tau_ref, pv, rho, pfCands, cell, tau_funcs, is_inner, *hadronsTensor_[is_inner]);
idx += 1;
} else {
if (debug_level >= 2) {
std::cout << " skipping creation of inputs, because ( eta = " << eta << ", phi = " << phi
<< " ) is not in the grid !!" << std::endl;
}
}
}
}
// Calling TF prediction only if n_valid_cells > 0
predTensor = getPartialPredictions(is_inner);
}

const auto predTensor = getPartialPredictions(is_inner);
idx = 0;
unsigned idx = 0;
for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) {
for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) {
const CellIndex cell_index{eta, phi};
Expand Down