CMS 3D CMS Logo

Classes | Typedefs | Enumerations | Functions
tensorflow Namespace Reference

Classes

class  NoThreadPool
 
struct  SessionCache
 
class  TBBThreadPool
 

Typedefs

typedef std::pair< std::string, Tensor > NamedTensor
 
typedef std::vector< NamedTensorNamedTensorList
 

Enumerations

enum  Backend {
  Backend::cpu, Backend::cuda, Backend::rocm, Backend::intel,
  Backend::best
}
 

Functions

bool closeSession (Session *&session)
 
bool closeSession (const Session *&session)
 
SessioncreateSession (SessionOptions &sessionOptions)
 
SessioncreateSession (Backend backend=Backend::cpu, int nThreads=1)
 
SessioncreateSession (const MetaGraphDef *metaGraphDef, const std::string &exportDir, SessionOptions &sessionOptions)
 
SessioncreateSession (const MetaGraphDef *metaGraphDef, const std::string &exportDir, Backend backend=Backend::cpu, int nThreads=1)
 
SessioncreateSession (const GraphDef *graphDef, SessionOptions &sessionOptions)
 
SessioncreateSession (const GraphDef *graphDef, Backend backend=Backend::cpu, int nThreads=1)
 
GraphDef * loadGraphDef (const std::string &pbFile)
 
MetaGraphDef * loadMetaGraph (const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
 
MetaGraphDef * loadMetaGraph (const std::string &exportDir, const std::string &tag=kSavedModelTagServe, Backend backend=Backend::cpu, int nThreads=1)
 
MetaGraphDef * loadMetaGraphDef (const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
 
MetaGraphDef * loadMetaGraphDef (const std::string &exportDir, const std::string &tag=kSavedModelTagServe, Backend backend=Backend::cpu, int nThreads=1)
 
void run (Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
 
void run (const Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
 
void run (Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, thread::ThreadPoolInterface *threadPool)
 
void run (const Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, thread::ThreadPoolInterface *threadPool)
 
void run (Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
 
void run (const Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
 
void run (Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
 
void run (const Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
 
void setBackend (SessionOptions &sessionOptions, Backend backend=Backend::cpu)
 
void setLogging (const std::string &level="3")
 
void setThreading (SessionOptions &sessionOptions, int nThreads=1)
 
void setThreading (SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool)
 

Typedef Documentation

◆ NamedTensor

typedef std::pair<std::string, Tensor> tensorflow::NamedTensor

Definition at line 30 of file TensorFlow.h.

◆ NamedTensorList

Definition at line 31 of file TensorFlow.h.

Enumeration Type Documentation

◆ Backend

enum tensorflow::Backend
strong
Enumerator
cpu 
cuda 
rocm 
intel 
best 

Definition at line 28 of file TensorFlow.h.

Function Documentation

◆ closeSession() [1/2]

bool tensorflow::closeSession ( Session *&  session)

Definition at line 261 of file TensorFlow.cc.

References mps_update::status.

Referenced by closeSession(), tensorflow::SessionCache::closeSession(), DTOccupancyTestML::dqmEndLuminosityBlock(), GEDPhotonProducer::endStream(), GsfElectronProducer::endStream(), L2TauNNProducer::globalEndJob(), BaseMVACache::~BaseMVACache(), reco::DeepSCGraphEvaluation::~DeepSCGraphEvaluation(), deep_tau::DeepTauCache::~DeepTauCache(), PtAssignmentEngineDxy::~PtAssignmentEngineDxy(), TfGraphDefWrapper::~TfGraphDefWrapper(), and TSGForOIDNN::~TSGForOIDNN().

261  {
262  if (session == nullptr) {
263  return true;
264  }
265 
266  // close and delete the session
267  Status status = session->Close();
268  delete session;
269 
270  // reset the pointer
271  session = nullptr;
272 
273  return status.ok();
274  }

◆ closeSession() [2/2]

bool tensorflow::closeSession ( const Session *&  session)

Definition at line 276 of file TensorFlow.cc.

References closeSession(), and alignCSCRings::s.

276  {
277  auto s = const_cast<Session*>(session);
278  bool state = closeSession(s);
279 
280  // reset the pointer
281  session = nullptr;
282 
283  return state;
284  }
bool closeSession(Session *&session)
Definition: TensorFlow.cc:261

◆ createSession() [1/6]

Session * tensorflow::createSession ( SessionOptions &  sessionOptions)

Definition at line 145 of file TensorFlow.cc.

References Exception, and mps_update::status.

Referenced by BaseMVACache::BaseMVACache(), PtAssignmentEngineDxy::configure(), createSession(), tensorflow::SessionCache::createSession(), deep_tau::DeepTauCache::DeepTauCache(), DTOccupancyTestML::dqmEndLuminosityBlock(), egammaTools::EgammaDNNHelper::getSessions(), HGCalConcentratorAutoEncoderImpl::HGCalConcentratorAutoEncoderImpl(), L2TauNNProducer::initializeGlobalCache(), reco::DeepSCGraphEvaluation::initTensorFlowGraphAndSession(), TfGraphDefProducer::produce(), and TSGForOIDNN::TSGForOIDNN().

145  {
146  // objects to create the session
147  Status status;
148 
149  // create a new, empty session
150  Session* session = nullptr;
151  status = NewSession(sessionOptions, &session);
152  if (!status.ok()) {
153  throw cms::Exception("InvalidSession") << "error while creating session: " << status.ToString();
154  }
155 
156  return session;
157  }

◆ createSession() [2/6]

Session * tensorflow::createSession ( Backend  backend = Backend::cpu,
int  nThreads = 1 
)

Definition at line 159 of file TensorFlow.cc.

References jetsAK4_CHS_cff::backend, createSession(), setBackend(), and setThreading().

159  {
160  // create session options and set thread options
161  SessionOptions sessionOptions;
162  setThreading(sessionOptions, nThreads);
163  setBackend(sessionOptions, backend);
164 
165  return createSession(sessionOptions);
166  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:145
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
Definition: TensorFlow.cc:17
void setBackend(SessionOptions &sessionOptions, Backend backend=Backend::cpu)
Definition: TensorFlow.cc:29

◆ createSession() [3/6]

Session * tensorflow::createSession ( const MetaGraphDef *  metaGraphDef,
const std::string &  exportDir,
SessionOptions &  sessionOptions 
)

Definition at line 168 of file TensorFlow.cc.

References createSession(), cms::soa::RestrictQualify::Default, Exception, convertSQLiteXML::ok, mps_update::status, and AlCaHLTBitMon_QueryRunRegistry::string.

170  {
171  // check for valid pointer
172  if (metaGraphDef == nullptr) {
173  throw cms::Exception("InvalidMetaGraphDef") << "error while creating session: metaGraphDef is nullptr";
174  }
175 
176  // check that the graph has nodes
177  if (metaGraphDef->graph_def().node_size() <= 0) {
178  throw cms::Exception("InvalidMetaGraphDef") << "error while creating session: graphDef has no nodes";
179  }
180 
181  Session* session = createSession(sessionOptions);
182 
183  // add the graph def from the meta graph
184  Status status;
185  status = session->Create(metaGraphDef->graph_def());
186  if (!status.ok()) {
187  throw cms::Exception("InvalidMetaGraphDef")
188  << "error while attaching metaGraphDef to session: " << status.ToString();
189  }
190 
191  // restore variables using the variable and index files in the export directory
192  // first, find names and paths
193  std::string varFileTensorName = metaGraphDef->saver_def().filename_tensor_name();
194  std::string restoreOpName = metaGraphDef->saver_def().restore_op_name();
195  std::string varDir = io::JoinPath(exportDir, kSavedModelVariablesDirectory);
196  std::string indexFile = io::JoinPath(varDir, MetaFilename(kSavedModelVariablesFilename));
197  std::string varFile = io::JoinPath(varDir, kSavedModelVariablesFilename);
198 
199  // when the index file is missing, there's nothing to do
200  if (!Env::Default()->FileExists(indexFile).ok()) {
201  return session;
202  }
203 
204  // create a tensor to store the variable file
205  Tensor varFileTensor(DT_STRING, TensorShape({}));
206  varFileTensor.scalar<tensorflow::tstring>()() = varFile;
207 
208  // run the restore op
209  status = session->Run({{varFileTensorName, varFileTensor}}, {}, {restoreOpName}, nullptr);
210  if (!status.ok()) {
211  throw cms::Exception("InvalidSession") << "error while restoring variables in session: " << status.ToString();
212  }
213 
214  return session;
215  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:145
constexpr bool Default
Definition: SoACommon.h:73

◆ createSession() [4/6]

Session * tensorflow::createSession ( const MetaGraphDef *  metaGraphDef,
const std::string &  exportDir,
Backend  backend = Backend::cpu,
int  nThreads = 1 
)

Definition at line 217 of file TensorFlow.cc.

References jetsAK4_CHS_cff::backend, createSession(), setBackend(), and setThreading().

217  {
218  // create session options and set thread options
219  SessionOptions sessionOptions;
220  setThreading(sessionOptions, nThreads);
221  setBackend(sessionOptions, backend);
222 
223  return createSession(metaGraphDef, exportDir, sessionOptions);
224  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:145
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
Definition: TensorFlow.cc:17
void setBackend(SessionOptions &sessionOptions, Backend backend=Backend::cpu)
Definition: TensorFlow.cc:29

◆ createSession() [5/6]

Session * tensorflow::createSession ( const GraphDef *  graphDef,
SessionOptions &  sessionOptions 
)

Definition at line 226 of file TensorFlow.cc.

References createSession(), Exception, and mps_update::status.

226  {
227  // check for valid pointer
228  if (graphDef == nullptr) {
229  throw cms::Exception("InvalidGraphDef") << "error while creating session: graphDef is nullptr";
230  }
231 
232  // check that the graph has nodes
233  if (graphDef->node_size() <= 0) {
234  throw cms::Exception("InvalidGraphDef") << "error while creating session: graphDef has no nodes";
235  }
236 
237  // create a new, empty session
238  Session* session = createSession(sessionOptions);
239 
240  // add the graph def
241  Status status;
242  status = session->Create(*graphDef);
243 
244  // check for success
245  if (!status.ok()) {
246  throw cms::Exception("InvalidSession") << "error while attaching graphDef to session: " << status.ToString();
247  }
248 
249  return session;
250  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:145

◆ createSession() [6/6]

Session * tensorflow::createSession ( const GraphDef *  graphDef,
Backend  backend = Backend::cpu,
int  nThreads = 1 
)

Definition at line 252 of file TensorFlow.cc.

References jetsAK4_CHS_cff::backend, createSession(), setBackend(), and setThreading().

252  {
253  // create session options and set thread options
254  SessionOptions sessionOptions;
255  setThreading(sessionOptions, nThreads);
256  setBackend(sessionOptions, backend);
257 
258  return createSession(graphDef, sessionOptions);
259  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:145
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
Definition: TensorFlow.cc:17
void setBackend(SessionOptions &sessionOptions, Backend backend=Backend::cpu)
Definition: TensorFlow.cc:29

◆ loadGraphDef()

GraphDef * tensorflow::loadGraphDef ( const std::string &  pbFile)

Definition at line 128 of file TensorFlow.cc.

References cms::soa::RestrictQualify::Default, Exception, and mps_update::status.

Referenced by BaseMVACache::BaseMVACache(), PtAssignmentEngineDxy::configure(), tensorflow::SessionCache::createSession(), deep_tau::DeepTauCache::DeepTauCache(), DTOccupancyTestML::dqmEndLuminosityBlock(), HGCalConcentratorAutoEncoderImpl::HGCalConcentratorAutoEncoderImpl(), L2TauNNProducer::initializeGlobalCache(), reco::DeepSCGraphEvaluation::initTensorFlowGraphAndSession(), egammaTools::EgammaDNNHelper::initTensorFlowGraphs(), TfGraphDefProducer::produce(), and TSGForOIDNN::TSGForOIDNN().

128  {
129  // objects to load the graph
130  Status status;
131 
132  // load it
133  GraphDef* graphDef = new GraphDef();
134  status = ReadBinaryProto(Env::Default(), pbFile, graphDef);
135 
136  // check for success
137  if (!status.ok()) {
138  throw cms::Exception("InvalidGraphDef")
139  << "error while loading graphDef from '" << pbFile << "': " << status.ToString();
140  }
141 
142  return graphDef;
143  }
constexpr bool Default
Definition: SoACommon.h:73

◆ loadMetaGraph() [1/2]

MetaGraphDef * tensorflow::loadMetaGraph ( const std::string &  exportDir,
const std::string &  tag,
SessionOptions &  sessionOptions 
)

Definition at line 105 of file TensorFlow.cc.

References loadMetaGraphDef(), and makeGlobalPositionRcd_cfg::tag.

105  {
106  edm::LogInfo("PhysicsTools/TensorFlow")
107  << "tensorflow::loadMetaGraph() is deprecated, use tensorflow::loadMetaGraphDef() instead";
108 
109  return loadMetaGraphDef(exportDir, tag, sessionOptions);
110  }
Log< level::Info, false > LogInfo
MetaGraphDef * loadMetaGraphDef(const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
Definition: TensorFlow.cc:88

◆ loadMetaGraph() [2/2]

MetaGraphDef * tensorflow::loadMetaGraph ( const std::string &  exportDir,
const std::string &  tag = kSavedModelTagServe,
Backend  backend = Backend::cpu,
int  nThreads = 1 
)

Definition at line 121 of file TensorFlow.cc.

References jetsAK4_CHS_cff::backend, loadMetaGraphDef(), and makeGlobalPositionRcd_cfg::tag.

121  {
122  edm::LogInfo("PhysicsTools/TensorFlow")
123  << "tensorflow::loadMetaGraph() is deprecated, use tensorflow::loadMetaGraphDef() instead";
124 
125  return loadMetaGraphDef(exportDir, tag, backend, nThreads);
126  }
Log< level::Info, false > LogInfo
MetaGraphDef * loadMetaGraphDef(const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
Definition: TensorFlow.cc:88

◆ loadMetaGraphDef() [1/2]

MetaGraphDef * tensorflow::loadMetaGraphDef ( const std::string &  exportDir,
const std::string &  tag,
SessionOptions &  sessionOptions 
)

Definition at line 88 of file TensorFlow.cc.

References Exception, mps_update::status, and makeGlobalPositionRcd_cfg::tag.

Referenced by loadMetaGraph(), and loadMetaGraphDef().

88  {
89  // objects to load the graph
90  Status status;
91  RunOptions runOptions;
92  SavedModelBundle bundle;
93 
94  // load the model
95  status = LoadSavedModel(sessionOptions, runOptions, exportDir, {tag}, &bundle);
96  if (!status.ok()) {
97  throw cms::Exception("InvalidMetaGraphDef")
98  << "error while loading metaGraphDef from '" << exportDir << "': " << status.ToString();
99  }
100 
101  // return a copy of the graph
102  return new MetaGraphDef(bundle.meta_graph_def);
103  }

◆ loadMetaGraphDef() [2/2]

MetaGraphDef * tensorflow::loadMetaGraphDef ( const std::string &  exportDir,
const std::string &  tag = kSavedModelTagServe,
Backend  backend = Backend::cpu,
int  nThreads = 1 
)

Definition at line 112 of file TensorFlow.cc.

References jetsAK4_CHS_cff::backend, loadMetaGraphDef(), setBackend(), setThreading(), and makeGlobalPositionRcd_cfg::tag.

112  {
113  // create session options and set thread options
114  SessionOptions sessionOptions;
115  setThreading(sessionOptions, nThreads);
116  setBackend(sessionOptions, backend);
117 
118  return loadMetaGraphDef(exportDir, tag, sessionOptions);
119  }
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
Definition: TensorFlow.cc:17
void setBackend(SessionOptions &sessionOptions, Backend backend=Backend::cpu)
Definition: TensorFlow.cc:29
MetaGraphDef * loadMetaGraphDef(const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
Definition: TensorFlow.cc:88

◆ run() [1/8]

void tensorflow::run ( Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const thread::ThreadPoolOptions &  threadPoolOptions 
)

Definition at line 286 of file TensorFlow.cc.

References Exception, PixelMapPlotter::inputs, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and mps_update::status.

Referenced by PtAssignmentEngineDxy::call_tensorflow_dxy(), MkFitOutputConverter::computeDNNs(), ticl::PatternRecognitionbyCLUE3D< TILES >::energyRegressionAndID(), ticl::PatternRecognitionbyCA< TILES >::energyRegressionAndID(), ticl::PatternRecognitionbyFastJet< TILES >::energyRegressionAndID(), TrackstersMergeProducerV3::energyRegressionAndID(), TrackstersMergeProducer::energyRegressionAndID(), egammaTools::EgammaDNNHelper::evaluate(), reco::DeepSCGraphEvaluation::evaluate(), TSGForOIDNN::evaluateClassifier(), TauNNId::EvaluateNN(), TSGForOIDNN::evaluateRegressor(), DeepTauId::getPartialPredictions(), DeepTauId::getPredictionsV2(), L2TauNNProducer::getTauScore(), DeepMETProducer::produce(), BaseMVAValueMapProducer< pat::Muon >::produce(), run(), DTOccupancyTestML::runOccupancyTest(), DeepCoreSeedGenerator::SeedEvaluation(), and HGCalConcentratorAutoEncoderImpl::select().

290  {
291  if (session == nullptr) {
292  throw cms::Exception("InvalidSession") << "cannot run empty session";
293  }
294 
295  // create empty run options
296  RunOptions runOptions;
297 
298  // run and check the status
299  Status status = session->Run(runOptions, inputs, outputNames, {}, outputs, nullptr, threadPoolOptions);
300  if (!status.ok()) {
301  throw cms::Exception("InvalidRun") << "error while running session: " << status.ToString();
302  }
303  }

◆ run() [2/8]

void tensorflow::run ( const Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const thread::ThreadPoolOptions &  threadPoolOptions 
)
inline

Definition at line 127 of file TensorFlow.h.

References PixelMapPlotter::inputs, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

131  {
132  // TF takes a non-const session in the run call which is, however, thread-safe and logically
133  // const, thus const_cast is consistent
134  run(const_cast<Session*>(session), inputs, outputNames, outputs, threadPoolOptions);
135  }
void run(const Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
Definition: TensorFlow.h:186

◆ run() [3/8]

void tensorflow::run ( Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
thread::ThreadPoolInterface *  threadPool 
)

Definition at line 305 of file TensorFlow.cc.

References PixelMapPlotter::inputs, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

309  {
310  // create thread pool options
311  thread::ThreadPoolOptions threadPoolOptions;
312  threadPoolOptions.inter_op_threadpool = threadPool;
313  threadPoolOptions.intra_op_threadpool = threadPool;
314 
315  // run
316  run(session, inputs, outputNames, outputs, threadPoolOptions);
317  }

◆ run() [4/8]

void tensorflow::run ( const Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
thread::ThreadPoolInterface *  threadPool 
)
inline

Definition at line 147 of file TensorFlow.h.

References PixelMapPlotter::inputs, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

151  {
152  // TF takes a non-const session in the run call which is, however, thread-safe and logically
153  // const, thus const_cast is consistent
154  run(const_cast<Session*>(session), inputs, outputNames, outputs, threadPool);
155  }
void run(const Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
Definition: TensorFlow.h:186

◆ run() [5/8]

void tensorflow::run ( Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const std::string &  threadPoolName = "no_threads" 
)

Definition at line 319 of file TensorFlow.cc.

References Exception, PixelMapPlotter::inputs, tensorflow::NoThreadPool::instance(), tensorflow::TBBThreadPool::instance(), jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

323  {
324  // lookup the thread pool and forward the call accordingly
325  if (threadPoolName == "no_threads") {
327  } else if (threadPoolName == "tbb") {
328  // the TBBTreadPool singleton should be already initialized before with a number of threads
330  } else if (threadPoolName == "tensorflow") {
331  run(session, inputs, outputNames, outputs, nullptr);
332  } else {
333  throw cms::Exception("UnknownThreadPool")
334  << "thread pool implementation'" << threadPoolName << "' unknown, use 'no_threads', 'tbb', or 'tensorflow'";
335  }
336  }
static PFTauRenderPlugin instance

◆ run() [6/8]

void tensorflow::run ( const Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const std::string &  threadPoolName = "no_threads" 
)
inline

Definition at line 167 of file TensorFlow.h.

References PixelMapPlotter::inputs, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

171  {
172  // TF takes a non-const session in the run call which is, however, thread-safe and logically
173  // const, thus const_cast is consistent
174  run(const_cast<Session*>(session), inputs, outputNames, outputs, threadPoolName);
175  }
void run(const Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
Definition: TensorFlow.h:186

◆ run() [7/8]

void tensorflow::run ( Session session,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const std::string &  threadPoolName = "no_threads" 
)

◆ run() [8/8]

void tensorflow::run ( const Session session,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs,
const std::string &  threadPoolName = "no_threads" 
)
inline

Definition at line 186 of file TensorFlow.h.

References jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

189  {
190  // TF takes a non-const session in the run call which is, however, thread-safe and logically
191  // const, thus const_cast is consistent
192  run(const_cast<Session*>(session), outputNames, outputs, threadPoolName);
193  }
void run(const Session *session, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const std::string &threadPoolName="no_threads")
Definition: TensorFlow.h:186

◆ setBackend()

void tensorflow::setBackend ( SessionOptions &  sessionOptions,
Backend  backend = Backend::cpu 
)

Definition at line 29 of file TensorFlow.cc.

References cms::Exception::addContext(), jetsAK4_CHS_cff::backend, best, cpu, cuda, intel, edm::ResourceInformation::nvidiaDriverVersion(), rocm, and edm::errors::UnavailableAccelerator.

Referenced by createSession(), deep_tau::DeepTauCache::DeepTauCache(), and loadMetaGraphDef().

29  {
30  /*
31  * The TensorFlow backend configures the available devices using options provided in the sessionOptions proto.
32  * // Options from https://github.com/tensorflow/tensorflow/blob/c53dab9fbc9de4ea8b1df59041a5ffd3987328c3/tensorflow/core/protobuf/config.proto
33  *
34  * If the device_count["GPU"] = 0 GPUs are not used.
35  * The visible_device_list configuration is used to map the `visible` devices (from CUDA_VISIBLE_DEVICES) to `virtual` devices.
36  * If Backend::cpu is request, the GPU device is disallowed by device_count configuration.
37  * If Backend::cuda is request:
38  * - if ResourceInformation shows an available Nvidia GPU device:
39  * the device is used with memory_growth configuration (not allocating all cuda memory at once).
40  * - if no device is present: an exception is raised.
41  */
42 
44  if (backend == Backend::cpu) {
45  // disable GPU usage
46  (*sessionOptions.config.mutable_device_count())["GPU"] = 0;
47  sessionOptions.config.mutable_gpu_options()->set_visible_device_list("");
48  }
49  // NVidia GPU
50  else if (backend == Backend::cuda) {
51  if (not ri->nvidiaDriverVersion().empty()) {
52  // Take only the first GPU in the CUDA_VISIBLE_DEVICE list
53  (*sessionOptions.config.mutable_device_count())["GPU"] = 1;
54  sessionOptions.config.mutable_gpu_options()->set_visible_device_list("0");
55  // Do not allocate all the memory on the GPU at the beginning.
56  sessionOptions.config.mutable_gpu_options()->set_allow_growth(true);
57  } else {
59  ex << "Cuda backend requested, but no NVIDIA GPU available in the job";
60  ex.addContext("Calling tensorflow::setBackend()");
61  throw ex;
62  }
63  }
64  // ROCm and Intel GPU are still not supported
65  else if ((backend == Backend::rocm) || (backend == Backend::intel)) {
67  ex << "ROCm/Intel GPU backend requested, but TF is not compiled yet for this platform";
68  ex.addContext("Calling tensorflow::setBackend()");
69  throw ex;
70  }
71  // Get NVidia GPU if possible or fallback to CPU
72  else if (backend == Backend::best) {
73  // Check if a Nvidia GPU is availabl
74  if (not ri->nvidiaDriverVersion().empty()) {
75  // Take only the first GPU in the CUDA_VISIBLE_DEVICE list
76  (*sessionOptions.config.mutable_device_count())["GPU"] = 1;
77  sessionOptions.config.mutable_gpu_options()->set_visible_device_list("0");
78  // Do not allocate all the memory on the GPU at the beginning.
79  sessionOptions.config.mutable_gpu_options()->set_allow_growth(true);
80  } else {
81  // Just CPU support
82  (*sessionOptions.config.mutable_device_count())["GPU"] = 0;
83  sessionOptions.config.mutable_gpu_options()->set_visible_device_list("");
84  }
85  }
86  }
virtual std::string const & nvidiaDriverVersion() const =0

◆ setLogging()

void tensorflow::setLogging ( const std::string &  level = "3")

◆ setThreading() [1/2]

void tensorflow::setThreading ( SessionOptions &  sessionOptions,
int  nThreads = 1 
)

Definition at line 17 of file TensorFlow.cc.

Referenced by createSession(), deep_tau::DeepTauCache::DeepTauCache(), loadMetaGraphDef(), and setThreading().

17  {
18  // set number of threads used for intra and inter operation communication
19  sessionOptions.config.set_intra_op_parallelism_threads(nThreads);
20  sessionOptions.config.set_inter_op_parallelism_threads(nThreads);
21  }

◆ setThreading() [2/2]

void tensorflow::setThreading ( SessionOptions &  sessionOptions,
int  nThreads,
const std::string &  singleThreadPool 
)

Definition at line 23 of file TensorFlow.cc.

References setThreading().

23  {
24  edm::LogInfo("PhysicsTools/TensorFlow") << "setting the thread pool via tensorflow::setThreading() is deprecated";
25 
26  setThreading(sessionOptions, nThreads);
27  }
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
Definition: TensorFlow.cc:17
Log< level::Info, false > LogInfo