Skip to content

Commit

Permalink
Skipping input preparation for empty grid
Browse files Browse the repository at this point in the history
  • Loading branch information
valsdav committed Mar 19, 2024
1 parent 6bb9279 commit ba3ec51
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 68 deletions.
15 changes: 7 additions & 8 deletions RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,17 +731,16 @@ void L2TauNNProducer::fillPatatracks(tensorflow::Tensor& cellGridMatrix,
}

std::vector<float> L2TauNNProducer::getTauScore(const tensorflow::Tensor& cellGridMatrix) {
std::vector<tensorflow::Tensor> pred_tensor;
// Check for empty input
if (cellGridMatrix.NumElements() != 0) {
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
119 changes: 59 additions & 60 deletions RecoTauTag/RecoTau/plugins/DeepTauId.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,76 +550,75 @@ class DeepTauId : public DeepTauIdBase<DeepTauIdWrapper> {
std::cout << "<DeepTauId::createConvFeatures (is_inner = " << is_inner << ")>:" << std::endl;
std::cout << "number of valid cells = " << grid.num_valid_cells() << std::endl;
}
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();
const size_t n_valid_cells = grid.num_valid_cells();
tensorflow::Tensor predTensor;
tensorflow::Tensor& convTensor = *convTensor_.at(is_inner);

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;
}
}
}
}
}
tensorflow::Tensor predTensor;
//check if at least one input is there to
//avoid calling TF with empty grid #TODO understand why the grid is empty
if (idx != 0) {
// Calling TF prediction only if n_valid_cells > 0
predTensor = getPartialPredictions(is_inner);
} else {
if (debug_level >= 2) {
std::cout << " no valid cells found, skipped TF evaluation" << std::endl;
}
}

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

0 comments on commit ba3ec51

Please sign in to comment.