CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Attributes
deep_tau::DeepTauCache Class Reference

Public Types

using GraphPtr = std::shared_ptr< tensorflow::GraphDef >
 

Public Member Functions

 DeepTauCache (const std::map< std::string, std::string > &graph_names, bool mem_mapped)
 
const tensorflow::GraphDef & getGraph (const std::string &name="") const
 
tensorflow::Session & getSession (const std::string &name="") const
 
 ~DeepTauCache ()
 

Private Attributes

std::map< std::string, GraphPtrgraphs_
 
std::map< std::string, std::unique_ptr< tensorflow::MemmappedEnv > > memmappedEnv_
 
std::map< std::string, tensorflow::Session * > sessions_
 

Detailed Description

Definition at line 15 of file DeepTauId.cc.

Member Typedef Documentation

◆ GraphPtr

using deep_tau::DeepTauCache::GraphPtr = std::shared_ptr<tensorflow::GraphDef>

Definition at line 17 of file DeepTauId.cc.

Constructor & Destructor Documentation

◆ DeepTauCache()

deep_tau::DeepTauCache::DeepTauCache ( const std::map< std::string, std::string > &  graph_names,
bool  mem_mapped 
)
inline

Definition at line 19 of file DeepTauId.cc.

References tensorflow::cpu, tensorflow::createSession(), cms::soa::RestrictQualify::Default, Exception, HLT_2024v14_cff::graph_file, graphs_, tensorflow::loadGraphDef(), HLT_2024v14_cff::mem_mapped, memmappedEnv_, AlcaSiPixelAliHarvester0T_cff::options, sessions_, and AlCaHLTBitMon_QueryRunRegistry::string.

19  {
20  for (const auto& graph_entry : graph_names) {
21  // Backend : to be parametrized from the python config
23 
24  const std::string& entry_name = graph_entry.first;
25  const std::string& graph_file = graph_entry.second;
26  if (mem_mapped) {
27  memmappedEnv_[entry_name] = std::make_unique<tensorflow::MemmappedEnv>(tensorflow::Env::Default());
28  const tensorflow::Status mmap_status = memmappedEnv_.at(entry_name)->InitializeFromFile(graph_file);
29  if (!mmap_status.ok()) {
30  throw cms::Exception("DeepTauCache: unable to initalize memmapped environment for ")
31  << graph_file << ". \n"
32  << mmap_status.ToString();
33  }
34 
35  graphs_[entry_name] = std::make_unique<tensorflow::GraphDef>();
36  const tensorflow::Status load_graph_status =
37  ReadBinaryProto(memmappedEnv_.at(entry_name).get(),
38  tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
39  graphs_.at(entry_name).get());
40  if (!load_graph_status.ok())
41  throw cms::Exception("DeepTauCache: unable to load graph from ") << graph_file << ". \n"
42  << load_graph_status.ToString();
43 
44  options.getSessionOptions().config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(
45  ::tensorflow::OptimizerOptions::L0);
46  options.getSessionOptions().env = memmappedEnv_.at(entry_name).get();
47 
48  sessions_[entry_name] = tensorflow::createSession(graphs_.at(entry_name).get(), options);
49 
50  } else {
51  graphs_[entry_name].reset(tensorflow::loadGraphDef(graph_file));
52  sessions_[entry_name] = tensorflow::createSession(graphs_.at(entry_name).get(), options);
53  }
54  }
55  };
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauId.cc:68
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:119
Session * createSession()
Definition: TensorFlow.cc:136
constexpr bool Default
Definition: SoACommon.h:75
std::map< std::string, GraphPtr > graphs_
Definition: DeepTauId.cc:67
std::map< std::string, std::unique_ptr< tensorflow::MemmappedEnv > > memmappedEnv_
Definition: DeepTauId.cc:69

◆ ~DeepTauCache()

deep_tau::DeepTauCache::~DeepTauCache ( )
inline

Definition at line 56 of file DeepTauId.cc.

References tensorflow::closeSession(), and sessions_.

56  {
57  for (auto& session_entry : sessions_)
58  tensorflow::closeSession(session_entry.second);
59  }
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauId.cc:68
bool closeSession(Session *&session)
Definition: TensorFlow.cc:233

Member Function Documentation

◆ getGraph()

const tensorflow::GraphDef& deep_tau::DeepTauCache::getGraph ( const std::string &  name = "") const
inline

Definition at line 64 of file DeepTauId.cc.

References graphs_, and Skims_PA_cff::name.

64 { return *graphs_.at(name); }
std::map< std::string, GraphPtr > graphs_
Definition: DeepTauId.cc:67

◆ getSession()

tensorflow::Session& deep_tau::DeepTauCache::getSession ( const std::string &  name = "") const
inline

Definition at line 63 of file DeepTauId.cc.

References Skims_PA_cff::name, and sessions_.

Referenced by DeepTauId::getPartialPredictions(), and DeepTauId::getPredictionsV2().

63 { return *sessions_.at(name); }
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauId.cc:68

Member Data Documentation

◆ graphs_

std::map<std::string, GraphPtr> deep_tau::DeepTauCache::graphs_
private

Definition at line 67 of file DeepTauId.cc.

Referenced by DeepTauCache(), and getGraph().

◆ memmappedEnv_

std::map<std::string, std::unique_ptr<tensorflow::MemmappedEnv> > deep_tau::DeepTauCache::memmappedEnv_
private

Definition at line 69 of file DeepTauId.cc.

Referenced by DeepTauCache().

◆ sessions_

std::map<std::string, tensorflow::Session*> deep_tau::DeepTauCache::sessions_
private

Definition at line 68 of file DeepTauId.cc.

Referenced by DeepTauCache(), getSession(), and ~DeepTauCache().