CMS 3D CMS Logo

Classes | Typedefs | Functions | Variables
tensorflow Namespace Reference

Classes

class  NTSession
 
class  NTSessionFactory
 
class  NTSessionRegistrar
 
class  TBBSession
 
class  TBBSessionFactory
 
class  TBBSessionRegistrar
 

Typedefs

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

Functions

bool closeSession (Session *&session)
 
SessioncreateSession (SessionOptions &sessionOptions)
 
SessioncreateSession (int nThreads=1)
 
SessioncreateSession (MetaGraphDef *metaGraph, const std::string &exportDir, SessionOptions &sessionOptions)
 
SessioncreateSession (MetaGraphDef *metaGraph, const std::string &exportDir, int nThreads=1)
 
SessioncreateSession (GraphDef *graphDef, SessionOptions &sessionOptions)
 
SessioncreateSession (GraphDef *graphDef, 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, int nThreads=1)
 
void run (Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
 
void run (Session *session, const std::vector< std::string > &inputNames, const std::vector< Tensor > &inputTensors, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
 
void run (Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs)
 
void run (Session *session, const std::vector< std::string > &inputNames, const std::vector< Tensor > &inputTensors, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs)
 
void setLogging (const std::string &level="3")
 
void setThreading (SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
 

Variables

static NTSessionRegistrar registrar
 
static TBBSessionRegistrar registrar
 

Typedef Documentation

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

Definition at line 24 of file TensorFlow.h.

Definition at line 25 of file TensorFlow.h.

Function Documentation

bool tensorflow::closeSession ( Session *&  session)

Definition at line 161 of file TensorFlow.cc.

References btagGenBb_cfi::Status, and mps_update::status.

Referenced by DTOccupancyTestML::dqmEndLuminosityBlock(), DeepDoubleXTFJetTagsProducer::~DeepDoubleXTFJetTagsProducer(), DeepFlavourTFJetTagsProducer::~DeepFlavourTFJetTagsProducer(), deep_tau::DeepTauCache::~DeepTauCache(), and DeepVertexTFJetTagsProducer::~DeepVertexTFJetTagsProducer().

161  {
162  if (session == nullptr) {
163  return true;
164  }
165 
166  // close and delete the session
167  Status status = session->Close();
168  delete session;
169 
170  // reset the pointer
171  session = nullptr;
172 
173  return status.ok();
174  }
Session * tensorflow::createSession ( SessionOptions &  sessionOptions)

Definition at line 71 of file TensorFlow.cc.

References Exception, btagGenBb_cfi::Status, and mps_update::status.

Referenced by BaseMVAValueMapProducer< pat::Jet >::BaseMVAValueMapProducer(), createSession(), DeepDoubleXTFJetTagsProducer::DeepDoubleXTFJetTagsProducer(), DeepFlavourTFJetTagsProducer::DeepFlavourTFJetTagsProducer(), deep_tau::DeepTauCache::DeepTauCache(), DeepVertexTFJetTagsProducer::DeepVertexTFJetTagsProducer(), DTOccupancyTestML::dqmEndLuminosityBlock(), and ticl::PatternRecognitionbyCA::PatternRecognitionbyCA().

71  {
72  // objects to create the session
73  Status status;
74 
75  // create a new, empty session
76  Session* session = nullptr;
77  status = NewSession(sessionOptions, &session);
78  if (!status.ok()) {
79  throw cms::Exception("InvalidSession") << "error while creating session: " << status.ToString();
80  }
81 
82  return session;
83  }
Session * tensorflow::createSession ( int  nThreads = 1)

Definition at line 85 of file TensorFlow.cc.

References createSession(), and setThreading().

85  {
86  // create session options and set thread options
87  SessionOptions sessionOptions;
88  setThreading(sessionOptions, nThreads);
89 
90  return createSession(sessionOptions);
91  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:15
Session * tensorflow::createSession ( MetaGraphDef *  metaGraph,
const std::string &  exportDir,
SessionOptions &  sessionOptions 
)

Definition at line 93 of file TensorFlow.cc.

References createSession(), Default, Exception, convertSQLiteXML::ok, btagGenBb_cfi::Status, mps_update::status, and AlCaHLTBitMon_QueryRunRegistry::string.

93  {
94  Session* session = createSession(sessionOptions);
95 
96  // add the graph def from the meta graph
97  Status status;
98  status = session->Create(metaGraph->graph_def());
99  if (!status.ok()) {
100  throw cms::Exception("InvalidSession") << "error while attaching meta graph to session: " << status.ToString();
101  }
102 
103  // restore variables using the variable and index files in the export directory
104  // first, find names and paths
105  std::string varFileTensorName = metaGraph->saver_def().filename_tensor_name();
106  std::string restoreOpName = metaGraph->saver_def().restore_op_name();
107  std::string varDir = io::JoinPath(exportDir, kSavedModelVariablesDirectory);
108  std::string indexFile = io::JoinPath(varDir, MetaFilename(kSavedModelVariablesFilename));
109  std::string varFile = io::JoinPath(varDir, kSavedModelVariablesFilename);
110 
111  // when the index file is missing, there's nothing to do
112  if (!Env::Default()->FileExists(indexFile).ok()) {
113  return session;
114  }
115 
116  // create a tensor to store the variable file
117  Tensor varFileTensor(DT_STRING, TensorShape({}));
118  varFileTensor.scalar<std::string>()() = varFile;
119 
120  // run the restore op
121  status = session->Run({{varFileTensorName, varFileTensor}}, {}, {restoreOpName}, nullptr);
122  if (!status.ok()) {
123  throw cms::Exception("InvalidSession") << "error while restoring variables in session: " << status.ToString();
124  }
125 
126  return session;
127  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
#define Default
Definition: vmac.h:110
Session * tensorflow::createSession ( MetaGraphDef *  metaGraph,
const std::string &  exportDir,
int  nThreads = 1 
)

Definition at line 129 of file TensorFlow.cc.

References createSession(), and setThreading().

129  {
130  // create session options and set thread options
131  SessionOptions sessionOptions;
132  setThreading(sessionOptions, nThreads);
133 
134  return createSession(metaGraph, exportDir, sessionOptions);
135  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:15
Session * tensorflow::createSession ( GraphDef *  graphDef,
SessionOptions &  sessionOptions 
)

Definition at line 137 of file TensorFlow.cc.

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

137  {
138  // create a new, empty session
139  Session* session = createSession(sessionOptions);
140 
141  // add the graph def
142  Status status;
143  status = session->Create(*graphDef);
144 
145  // check for success
146  if (!status.ok()) {
147  throw cms::Exception("InvalidSession") << "error while attaching graph def to session: " << status.ToString();
148  }
149 
150  return session;
151  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
Session * tensorflow::createSession ( GraphDef *  graphDef,
int  nThreads = 1 
)

Definition at line 153 of file TensorFlow.cc.

References createSession(), and setThreading().

153  {
154  // create session options and set thread options
155  SessionOptions sessionOptions;
156  setThreading(sessionOptions, nThreads);
157 
158  return createSession(graphDef, sessionOptions);
159  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:71
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:15
GraphDef * tensorflow::loadGraphDef ( const std::string &  pbFile)

Definition at line 55 of file TensorFlow.cc.

References Default, Exception, btagGenBb_cfi::Status, and mps_update::status.

Referenced by BaseMVAValueMapProducer< pat::Jet >::BaseMVAValueMapProducer(), deep_tau::DeepTauCache::DeepTauCache(), DTOccupancyTestML::dqmEndLuminosityBlock(), TrackstersProducer::initializeGlobalCache(), DeepFlavourTFJetTagsProducer::initializeGlobalCache(), DeepVertexTFJetTagsProducer::initializeGlobalCache(), and DeepDoubleXTFJetTagsProducer::initializeGlobalCache().

55  {
56  // objects to load the graph
57  Status status;
58 
59  // load it
60  GraphDef* graphDef = new GraphDef();
61  status = ReadBinaryProto(Env::Default(), pbFile, graphDef);
62 
63  // check for success
64  if (!status.ok()) {
65  throw cms::Exception("InvalidGraphDef") << "error while loading graph def: " << status.ToString();
66  }
67 
68  return graphDef;
69  }
#define Default
Definition: vmac.h:110
MetaGraphDef * tensorflow::loadMetaGraph ( const std::string &  exportDir,
const std::string &  tag,
SessionOptions &  sessionOptions 
)

Definition at line 31 of file TensorFlow.cc.

References Exception, btagGenBb_cfi::Status, and mps_update::status.

Referenced by loadMetaGraph().

31  {
32  // objects to load the graph
33  Status status;
34  RunOptions runOptions;
35  SavedModelBundle bundle;
36 
37  // load the model
38  status = LoadSavedModel(sessionOptions, runOptions, exportDir, {tag}, &bundle);
39  if (!status.ok()) {
40  throw cms::Exception("InvalidMetaGraph") << "error while loading meta graph: " << status.ToString();
41  }
42 
43  // return a copy of the graph
44  return new MetaGraphDef(bundle.meta_graph_def);
45  }
MetaGraphDef * tensorflow::loadMetaGraph ( const std::string &  exportDir,
const std::string &  tag = kSavedModelTagServe,
int  nThreads = 1 
)

Definition at line 47 of file TensorFlow.cc.

References loadMetaGraph(), and setThreading().

47  {
48  // create session options and set thread options
49  SessionOptions sessionOptions;
50  setThreading(sessionOptions, nThreads);
51 
52  return loadMetaGraph(exportDir, tag, sessionOptions);
53  }
MetaGraphDef * loadMetaGraph(const std::string &exportDir, const std::string &tag, SessionOptions &sessionOptions)
Definition: TensorFlow.cc:31
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
Definition: TensorFlow.cc:15
void tensorflow::run ( Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
const std::vector< std::string > &  targetNodes,
std::vector< Tensor > *  outputs 
)

Definition at line 176 of file TensorFlow.cc.

References Exception, btagGenBb_cfi::Status, and mps_update::status.

Referenced by ticl::PatternRecognitionbyCA::energyRegressionAndID(), DeepTauId::getPartialPredictions(), DPFIsolation::getPredictions(), DeepTauId::getPredictionsV1(), DeepTauId::getPredictionsV2(), DeepDoubleXTFJetTagsProducer::produce(), DeepFlavourTFJetTagsProducer::produce(), DeepVertexTFJetTagsProducer::produce(), BaseMVAValueMapProducer< T >::produce(), run(), and DTOccupancyTestML::runOccupancyTest().

180  {
181  if (session == nullptr) {
182  throw cms::Exception("InvalidSession") << "cannot run empty session";
183  }
184 
185  // run and check the status
186  Status status = session->Run(inputs, outputNames, targetNodes, outputs);
187  if (!status.ok()) {
188  throw cms::Exception("InvalidRun") << "error while running session: " << status.ToString();
189  }
190  }
void tensorflow::run ( Session session,
const std::vector< std::string > &  inputNames,
const std::vector< Tensor > &  inputTensors,
const std::vector< std::string > &  outputNames,
const std::vector< std::string > &  targetNodes,
std::vector< Tensor > *  outputs 
)

Definition at line 192 of file TensorFlow.cc.

References Exception, mps_fire::i, PixelMapPlotter::inputs, and run().

197  {
198  if (inputNames.size() != inputTensors.size()) {
199  throw cms::Exception("InvalidInput") << "numbers of input names and tensors not equal";
200  }
201 
203  for (size_t i = 0; i < inputNames.size(); i++) {
204  inputs.push_back(NamedTensor(inputNames[i], inputTensors[i]));
205  }
206 
207  run(session, inputs, outputNames, targetNodes, outputs);
208  }
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:25
std::pair< std::string, Tensor > NamedTensor
Definition: TensorFlow.h:24
void tensorflow::run ( Session session,
const NamedTensorList inputs,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs 
)
void tensorflow::run ( Session session,
const std::vector< std::string > &  inputNames,
const std::vector< Tensor > &  inputTensors,
const std::vector< std::string > &  outputNames,
std::vector< Tensor > *  outputs 
)

Definition at line 217 of file TensorFlow.cc.

References PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and run().

221  {
222  run(session, inputNames, inputTensors, outputNames, {}, outputs);
223  }
void tensorflow::setLogging ( const std::string &  level = "3")
void tensorflow::setThreading ( SessionOptions &  sessionOptions,
int  nThreads,
const std::string &  singleThreadPool = "no_threads" 
)

Definition at line 15 of file TensorFlow.cc.

References Exception, and jets_cff::singleThreadPool.

Referenced by BaseMVAValueMapProducer< pat::Jet >::BaseMVAValueMapProducer(), createSession(), DeepDoubleXTFJetTagsProducer::DeepDoubleXTFJetTagsProducer(), DeepFlavourTFJetTagsProducer::DeepFlavourTFJetTagsProducer(), deep_tau::DeepTauCache::DeepTauCache(), DeepVertexTFJetTagsProducer::DeepVertexTFJetTagsProducer(), and loadMetaGraph().

15  {
16  // set number of threads used for intra and inter operation communication
17  sessionOptions.config.set_intra_op_parallelism_threads(nThreads);
18  sessionOptions.config.set_inter_op_parallelism_threads(nThreads);
19 
20  // when exactly one thread is requested use a custom thread pool
21  if (nThreads == 1 && !singleThreadPool.empty()) {
22  // check for known thread pools
23  if (singleThreadPool != "no_threads" && singleThreadPool != "tbb") {
24  throw cms::Exception("UnknownThreadPool")
25  << "thread pool '" << singleThreadPool << "' unknown, use 'no_threads' or 'tbb'";
26  }
27  sessionOptions.target = singleThreadPool;
28  }
29  }
singleThreadPool
Definition: jets_cff.py:309

Variable Documentation

NTSessionRegistrar tensorflow::registrar
static

Definition at line 169 of file NTSession.cc.

TBBSessionRegistrar tensorflow::registrar
static

Definition at line 171 of file TBBSession.cc.