CMS 3D CMS Logo

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

#include <DeepTauBase.h>

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 40 of file DeepTauBase.h.

Member Typedef Documentation

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

Definition at line 42 of file DeepTauBase.h.

Constructor & Destructor Documentation

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

Definition at line 133 of file DeepTauBase.cc.

References tensorflow::createSession(), Default, Exception, tensorflow::loadGraphDef(), AlcaSiPixelAliHarvester0T_cff::options, tensorflow::setThreading(), btagGenBb_cfi::Status, and AlCaHLTBitMon_QueryRunRegistry::string.

133  {
134  for (const auto& graph_entry : graph_names) {
135  tensorflow::SessionOptions options;
136  tensorflow::setThreading(options, 1, "no_threads");
137 
138  const std::string& entry_name = graph_entry.first;
139  const std::string& graph_file = graph_entry.second;
140  if (mem_mapped) {
141  memmappedEnv_[entry_name] = std::make_unique<tensorflow::MemmappedEnv>(tensorflow::Env::Default());
142  const tensorflow::Status mmap_status = memmappedEnv_.at(entry_name)->InitializeFromFile(graph_file);
143  if (!mmap_status.ok()) {
144  throw cms::Exception("DeepTauCache: unable to initalize memmapped environment for ")
145  << graph_file << ". \n"
146  << mmap_status.ToString();
147  }
148 
149  graphs_[entry_name] = std::make_unique<tensorflow::GraphDef>();
150  const tensorflow::Status load_graph_status =
151  ReadBinaryProto(memmappedEnv_.at(entry_name).get(),
152  tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
153  graphs_.at(entry_name).get());
154  if (!load_graph_status.ok())
155  throw cms::Exception("DeepTauCache: unable to load graph from ") << graph_file << ". \n"
156  << mmap_status.ToString();
157  options.config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(
158  ::tensorflow::OptimizerOptions::L0);
159  options.env = memmappedEnv_.at(entry_name).get();
160 
161  sessions_[entry_name] = tensorflow::createSession(graphs_.at(entry_name).get(), options);
162 
163  } else {
164  graphs_[entry_name].reset(tensorflow::loadGraphDef(graph_file));
165  sessions_[entry_name] = tensorflow::createSession(graphs_.at(entry_name).get(), options);
166  }
167  }
168  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
#define Default
Definition: vmac.h:110
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauBase.h:54
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:55
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:15
std::map< std::string, GraphPtr > graphs_
Definition: DeepTauBase.h:53
std::map< std::string, std::unique_ptr< tensorflow::MemmappedEnv > > memmappedEnv_
Definition: DeepTauBase.h:55
deep_tau::DeepTauCache::~DeepTauCache ( )

Definition at line 170 of file DeepTauBase.cc.

References tensorflow::closeSession().

170  {
171  for (auto& session_entry : sessions_)
172  tensorflow::closeSession(session_entry.second);
173  }
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauBase.h:54
bool closeSession(Session *&session)
Definition: TensorFlow.cc:161

Member Function Documentation

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

Definition at line 50 of file DeepTauBase.h.

References Skims_PA_cff::name.

50 { return *graphs_.at(name); }
std::map< std::string, GraphPtr > graphs_
Definition: DeepTauBase.h:53
tensorflow::Session& deep_tau::DeepTauCache::getSession ( const std::string &  name = "") const
inline

Definition at line 49 of file DeepTauBase.h.

References Skims_PA_cff::name.

49 { return *sessions_.at(name); }
std::map< std::string, tensorflow::Session * > sessions_
Definition: DeepTauBase.h:54

Member Data Documentation

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

Definition at line 53 of file DeepTauBase.h.

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

Definition at line 55 of file DeepTauBase.h.

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

Definition at line 54 of file DeepTauBase.h.