From 8a546bca778cfd4a8e8c3c8577c32c6d7aec3ac8 Mon Sep 17 00:00:00 2001 From: Davide Valsecchi Date: Mon, 27 Feb 2023 10:38:39 +0100 Subject: [PATCH] Improved options struct for TF session --- .../TensorFlow/interface/TensorFlow.h | 87 ++++++------- PhysicsTools/TensorFlow/src/TensorFlow.cc | 123 +++++++----------- .../TensorFlow/test/testConstSession.cc | 4 +- .../TensorFlow/test/testConstSessionCUDA.cc | 4 +- .../TensorFlow/test/testGraphLoading.cc | 6 +- .../TensorFlow/test/testGraphLoadingCUDA.cc | 5 +- .../TensorFlow/test/testHelloWorld.cc | 6 +- .../TensorFlow/test/testHelloWorldCUDA.cc | 6 +- .../TensorFlow/test/testMetaGraphLoading.cc | 8 +- .../test/testMetaGraphLoadingCUDA.cc | 7 +- .../TensorFlow/test/testSessionCache.cc | 5 +- .../TensorFlow/test/testSessionCacheCUDA.cc | 6 +- .../TensorFlow/test/testThreadPools.cc | 7 +- .../TensorFlow/test/testThreadPoolsCUDA.cc | 6 +- .../TensorFlow/test/testVisibleDevices.cc | 6 +- .../TensorFlow/test/testVisibleDevicesCUDA.cc | 10 +- RecoTauTag/RecoTau/src/DeepTauBase.cc | 10 +- 17 files changed, 141 insertions(+), 165 deletions(-) diff --git a/PhysicsTools/TensorFlow/interface/TensorFlow.h b/PhysicsTools/TensorFlow/interface/TensorFlow.h index 371c49d4aad04..a61539c00413d 100644 --- a/PhysicsTools/TensorFlow/interface/TensorFlow.h +++ b/PhysicsTools/TensorFlow/interface/TensorFlow.h @@ -30,82 +30,75 @@ namespace tensorflow { typedef std::pair NamedTensor; typedef std::vector NamedTensorList; + struct Options { + int _nThreads; + Backend _backend; + SessionOptions _options; + + Options(Backend backend) : _nThreads{1}, _backend{backend} { + setThreading(_nThreads); + setBackend(_backend); + }; + + Options() : _nThreads{1}, _backend{Backend::cpu} { + setThreading(_nThreads); + setBackend(_backend); + }; + + // updates the config of sessionOptions so that it uses nThreads + void setThreading(int nThreads = 1); + + // Set the backend option cpu/cuda + // The gpu memory is set to "allow_growth" to avoid TF getting all the CUDA memory at once. + void setBackend(Backend backend = Backend::cpu); + + SessionOptions& getSessionOptions() { return _options; }; + int getNThreads() const { return _nThreads; }; + Backend getBackend() const { return _backend; }; + }; + // set the tensorflow log level void setLogging(const std::string& level = "3"); - // updates the config of sessionOptions so that it uses nThreads - void setThreading(SessionOptions& sessionOptions, int nThreads = 1); - - // deprecated - // updates the config of sessionOptions so that it uses nThreads, prints a deprecation warning - // since the threading configuration is done per run() call as of 2.1 - void setThreading(SessionOptions& sessionOptions, int nThreads, const std::string& singleThreadPool); - - // Set the backend option cpu/cuda - // The gpu memory is set to "allow_growth" to avoid TF getting all the CUDA memory at once. - void setBackend(SessionOptions& sessionOptions, Backend backend = Backend::cpu); - // loads a meta graph definition saved at exportDir using the SavedModel interface for a tag and - // predefined sessionOptions + // predefined options // transfers ownership - MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag, SessionOptions& sessionOptions); - - // deprecated in favor of loadMetaGraphDef - MetaGraphDef* loadMetaGraph(const std::string& exportDir, const std::string& tag, SessionOptions& sessionOptions); + MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag = kSavedModelTagServe); // loads a meta graph definition saved at exportDir using the SavedModel interface for a tag and - // nThreads + // user provided options // transfers ownership - MetaGraphDef* loadMetaGraphDef(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, Options& options); // deprecated in favor of loadMetaGraphDef - MetaGraphDef* loadMetaGraph(const std::string& exportDir, - const std::string& tag = kSavedModelTagServe, - Backend backend = Backend::cpu, - int nThreads = 1); + MetaGraphDef* loadMetaGraph(const std::string& exportDir, const std::string& tag, Options& Options); // loads a graph definition saved as a protobuf file at pbFile // transfers ownership GraphDef* loadGraphDef(const std::string& pbFile); - // return a new, empty session using predefined sessionOptions - // transfers ownership - Session* createSession(SessionOptions& sessionOptions); + // return a new, empty session using the predefined options + Session* createSession(); - // return a new, empty session with nThreads and selected backend + // return a new, empty session using user provided options // transfers ownership - Session* createSession(Backend backend = Backend::cpu, int nThreads = 1); + Session* createSession(Options& options); // return a new session that will contain an already loaded meta graph whose exportDir must be // given in order to load and initialize the variables, sessionOptions are predefined // an error is thrown when metaGraphDef is a nullptr or when the graph has no nodes // transfers ownership - Session* createSession(const MetaGraphDef* metaGraphDef, - const std::string& exportDir, - SessionOptions& sessionOptions); - - // return a new session that will contain an already loaded meta graph whose exportDir must be given - // in order to load and initialize the variables, threading options are inferred from nThreads - // an error is thrown when metaGraphDef is a nullptr or when the graph has no nodes - // transfers ownership - Session* createSession(const MetaGraphDef* metaGraphDef, - const std::string& exportDir, - Backend backend = Backend::cpu, - int nThreads = 1); + Session* createSession(const MetaGraphDef* metaGraphDef, const std::string& exportDir, Options& options); // return a new session that will contain an already loaded graph def, sessionOptions are predefined // an error is thrown when graphDef is a nullptr or when the graph has no nodes // transfers ownership - Session* createSession(const GraphDef* graphDef, SessionOptions& sessionOptions); + Session* createSession(const GraphDef* graphDef); - // return a new session that will contain an already loaded graph def, threading options are - // inferred from nThreads + // return a new session that will contain an already loaded graph def, sessionOptions are user defined // an error is thrown when graphDef is a nullptr or when the graph has no nodes // transfers ownership - Session* createSession(const GraphDef* graphDef, Backend backend = Backend::cpu, int nThreads = 1); + Session* createSession(const GraphDef* graphDef, Options& options); // closes a session, calls its destructor, resets the pointer, and returns true on success bool closeSession(Session*& session); diff --git a/PhysicsTools/TensorFlow/src/TensorFlow.cc b/PhysicsTools/TensorFlow/src/TensorFlow.cc index 3dffc4c90ab39..fcb09e2e9c449 100644 --- a/PhysicsTools/TensorFlow/src/TensorFlow.cc +++ b/PhysicsTools/TensorFlow/src/TensorFlow.cc @@ -12,21 +12,14 @@ namespace tensorflow { - void setLogging(const std::string& level) { setenv("TF_CPP_MIN_LOG_LEVEL", level.c_str(), 0); } - - void setThreading(SessionOptions& sessionOptions, int nThreads) { + void Options::setThreading(int nThreads) { + _nThreads = nThreads; // set number of threads used for intra and inter operation communication - sessionOptions.config.set_intra_op_parallelism_threads(nThreads); - sessionOptions.config.set_inter_op_parallelism_threads(nThreads); - } - - void setThreading(SessionOptions& sessionOptions, int nThreads, const std::string& singleThreadPool) { - edm::LogInfo("PhysicsTools/TensorFlow") << "setting the thread pool via tensorflow::setThreading() is deprecated"; - - setThreading(sessionOptions, nThreads); + _options.config.set_intra_op_parallelism_threads(nThreads); + _options.config.set_inter_op_parallelism_threads(nThreads); } - void setBackend(SessionOptions& sessionOptions, Backend backend) { + void Options::setBackend(Backend backend) { /* * The TensorFlow backend configures the available devices using options provided in the sessionOptions proto. * // Options from https://github.com/tensorflow/tensorflow/blob/c53dab9fbc9de4ea8b1df59041a5ffd3987328c3/tensorflow/core/protobuf/config.proto @@ -43,17 +36,17 @@ namespace tensorflow { edm::Service ri; if (backend == Backend::cpu) { // disable GPU usage - (*sessionOptions.config.mutable_device_count())["GPU"] = 0; - sessionOptions.config.mutable_gpu_options()->set_visible_device_list(""); + (*_options.config.mutable_device_count())["GPU"] = 0; + _options.config.mutable_gpu_options()->set_visible_device_list(""); } // NVidia GPU else if (backend == Backend::cuda) { if (not ri->nvidiaDriverVersion().empty()) { // Take only the first GPU in the CUDA_VISIBLE_DEVICE list - (*sessionOptions.config.mutable_device_count())["GPU"] = 1; - sessionOptions.config.mutable_gpu_options()->set_visible_device_list("0"); + (*_options.config.mutable_device_count())["GPU"] = 1; + _options.config.mutable_gpu_options()->set_visible_device_list("0"); // Do not allocate all the memory on the GPU at the beginning. - sessionOptions.config.mutable_gpu_options()->set_allow_growth(true); + _options.config.mutable_gpu_options()->set_allow_growth(true); } else { edm::Exception ex(edm::errors::UnavailableAccelerator); ex << "Cuda backend requested, but no NVIDIA GPU available in the job"; @@ -73,26 +66,41 @@ namespace tensorflow { // Check if a Nvidia GPU is availabl if (not ri->nvidiaDriverVersion().empty()) { // Take only the first GPU in the CUDA_VISIBLE_DEVICE list - (*sessionOptions.config.mutable_device_count())["GPU"] = 1; - sessionOptions.config.mutable_gpu_options()->set_visible_device_list("0"); + (*_options.config.mutable_device_count())["GPU"] = 1; + _options.config.mutable_gpu_options()->set_visible_device_list("0"); // Do not allocate all the memory on the GPU at the beginning. - sessionOptions.config.mutable_gpu_options()->set_allow_growth(true); + _options.config.mutable_gpu_options()->set_allow_growth(true); } else { // Just CPU support - (*sessionOptions.config.mutable_device_count())["GPU"] = 0; - sessionOptions.config.mutable_gpu_options()->set_visible_device_list(""); + (*_options.config.mutable_device_count())["GPU"] = 0; + _options.config.mutable_gpu_options()->set_visible_device_list(""); } } } - MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag, SessionOptions& sessionOptions) { + void setLogging(const std::string& level) { + /* + * 0 = all messages are logged (default behavior) + * 1 = INFO messages are not printed + * 2 = INFO and WARNING messages are not printed + * 3 = INFO, WARNING, and ERROR messages are not printed + */ + setenv("TF_CPP_MIN_LOG_LEVEL", level.c_str(), 0); + } + + MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag) { + Options default_options{}; + return loadMetaGraphDef(exportDir, tag, default_options); + } + + MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag, Options& options) { // objects to load the graph Status status; RunOptions runOptions; SavedModelBundle bundle; // load the model - status = LoadSavedModel(sessionOptions, runOptions, exportDir, {tag}, &bundle); + status = LoadSavedModel(options.getSessionOptions(), runOptions, exportDir, {tag}, &bundle); if (!status.ok()) { throw cms::Exception("InvalidMetaGraphDef") << "error while loading metaGraphDef from '" << exportDir << "': " << status.ToString(); @@ -102,27 +110,11 @@ namespace tensorflow { return new MetaGraphDef(bundle.meta_graph_def); } - MetaGraphDef* loadMetaGraph(const std::string& exportDir, const std::string& tag, SessionOptions& sessionOptions) { + MetaGraphDef* loadMetaGraph(const std::string& exportDir, const std::string& tag, Options& options) { edm::LogInfo("PhysicsTools/TensorFlow") << "tensorflow::loadMetaGraph() is deprecated, use tensorflow::loadMetaGraphDef() instead"; - return loadMetaGraphDef(exportDir, tag, sessionOptions); - } - - MetaGraphDef* loadMetaGraphDef(const std::string& exportDir, const std::string& tag, Backend backend, int nThreads) { - // create session options and set thread options - SessionOptions sessionOptions; - setThreading(sessionOptions, nThreads); - setBackend(sessionOptions, backend); - - return loadMetaGraphDef(exportDir, tag, sessionOptions); - } - - MetaGraphDef* loadMetaGraph(const std::string& exportDir, const std::string& tag, Backend backend, int nThreads) { - edm::LogInfo("PhysicsTools/TensorFlow") - << "tensorflow::loadMetaGraph() is deprecated, use tensorflow::loadMetaGraphDef() instead"; - - return loadMetaGraphDef(exportDir, tag, backend, nThreads); + return loadMetaGraphDef(exportDir, tag, options); } GraphDef* loadGraphDef(const std::string& pbFile) { @@ -142,13 +134,18 @@ namespace tensorflow { return graphDef; } - Session* createSession(SessionOptions& sessionOptions) { + Session* createSession() { + Options default_options{}; + return createSession(default_options); + } + + Session* createSession(Options& options) { // objects to create the session Status status; // create a new, empty session Session* session = nullptr; - status = NewSession(sessionOptions, &session); + status = NewSession(options.getSessionOptions(), &session); if (!status.ok()) { throw cms::Exception("InvalidSession") << "error while creating session: " << status.ToString(); } @@ -156,18 +153,7 @@ namespace tensorflow { return session; } - Session* createSession(Backend backend, int nThreads) { - // create session options and set thread options - SessionOptions sessionOptions; - setThreading(sessionOptions, nThreads); - setBackend(sessionOptions, backend); - - return createSession(sessionOptions); - } - - Session* createSession(const MetaGraphDef* metaGraphDef, - const std::string& exportDir, - SessionOptions& sessionOptions) { + Session* createSession(const MetaGraphDef* metaGraphDef, const std::string& exportDir, Options& options) { // check for valid pointer if (metaGraphDef == nullptr) { throw cms::Exception("InvalidMetaGraphDef") << "error while creating session: metaGraphDef is nullptr"; @@ -178,7 +164,7 @@ namespace tensorflow { throw cms::Exception("InvalidMetaGraphDef") << "error while creating session: graphDef has no nodes"; } - Session* session = createSession(sessionOptions); + Session* session = createSession(options); // add the graph def from the meta graph Status status; @@ -214,16 +200,12 @@ namespace tensorflow { return session; } - Session* createSession(const MetaGraphDef* metaGraphDef, const std::string& exportDir, Backend backend, int nThreads) { - // create session options and set thread options - SessionOptions sessionOptions; - setThreading(sessionOptions, nThreads); - setBackend(sessionOptions, backend); - - return createSession(metaGraphDef, exportDir, sessionOptions); + Session* createSession(const GraphDef* graphDef) { + Options default_options{}; + return createSession(graphDef, default_options); } - Session* createSession(const GraphDef* graphDef, SessionOptions& sessionOptions) { + Session* createSession(const GraphDef* graphDef, Options& options) { // check for valid pointer if (graphDef == nullptr) { throw cms::Exception("InvalidGraphDef") << "error while creating session: graphDef is nullptr"; @@ -235,7 +217,7 @@ namespace tensorflow { } // create a new, empty session - Session* session = createSession(sessionOptions); + Session* session = createSession(options); // add the graph def Status status; @@ -249,15 +231,6 @@ namespace tensorflow { return session; } - Session* createSession(const GraphDef* graphDef, Backend backend, int nThreads) { - // create session options and set thread options - SessionOptions sessionOptions; - setThreading(sessionOptions, nThreads); - setBackend(sessionOptions, backend); - - return createSession(graphDef, sessionOptions); - } - bool closeSession(Session*& session) { if (session == nullptr) { return true; diff --git a/PhysicsTools/TensorFlow/test/testConstSession.cc b/PhysicsTools/TensorFlow/test/testConstSession.cc index 7fdf8e36c823b..6e75c3155d010 100644 --- a/PhysicsTools/TensorFlow/test/testConstSession.cc +++ b/PhysicsTools/TensorFlow/test/testConstSession.cc @@ -33,12 +33,12 @@ void testConstSession::test() { tensorflow::Backend backend = tensorflow::Backend::cpu; // load the graph - tensorflow::setLogging(); + tensorflow::Options options{backend}; tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - const tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + const tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // example evaluation diff --git a/PhysicsTools/TensorFlow/test/testConstSessionCUDA.cc b/PhysicsTools/TensorFlow/test/testConstSessionCUDA.cc index 2b6b039e0fb04..aa34e457c0a50 100644 --- a/PhysicsTools/TensorFlow/test/testConstSessionCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testConstSessionCUDA.cc @@ -55,11 +55,13 @@ process.add_(cms.Service('CUDAService')) // load the graph std::string pbFile = dataPath_ + "/constantgraph.pb"; tensorflow::setLogging(); + tensorflow::Options options{backend}; + tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - const tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + const tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // example evaluation diff --git a/PhysicsTools/TensorFlow/test/testGraphLoading.cc b/PhysicsTools/TensorFlow/test/testGraphLoading.cc index 95047ec7c04aa..537f2e42207f8 100644 --- a/PhysicsTools/TensorFlow/test/testGraphLoading.cc +++ b/PhysicsTools/TensorFlow/test/testGraphLoading.cc @@ -33,16 +33,16 @@ void testGraphLoading::test() { tensorflow::Backend backend = tensorflow::Backend::cpu; // load the graph - tensorflow::setLogging(); + tensorflow::Options options{backend}; tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, options), cms::Exception); // example evaluation tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10}); diff --git a/PhysicsTools/TensorFlow/test/testGraphLoadingCUDA.cc b/PhysicsTools/TensorFlow/test/testGraphLoadingCUDA.cc index 80b33a504a475..8de6395343d91 100644 --- a/PhysicsTools/TensorFlow/test/testGraphLoadingCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testGraphLoadingCUDA.cc @@ -55,15 +55,16 @@ process.add_(cms.Service('CUDAService')) // load the graph std::string pbFile = dataPath_ + "/constantgraph.pb"; tensorflow::setLogging(); + tensorflow::Options options{backend}; tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, options), cms::Exception); // example evaluation tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10}); diff --git a/PhysicsTools/TensorFlow/test/testHelloWorld.cc b/PhysicsTools/TensorFlow/test/testHelloWorld.cc index bf1161b3d5c60..8a0a16cb4afea 100644 --- a/PhysicsTools/TensorFlow/test/testHelloWorld.cc +++ b/PhysicsTools/TensorFlow/test/testHelloWorld.cc @@ -36,13 +36,13 @@ void testHelloWorld::test() { // object to load and run the graph / session tensorflow::Status status; - tensorflow::SessionOptions sessionOptions; - tensorflow::setBackend(sessionOptions, backend); + tensorflow::Options options{backend}; + tensorflow::setLogging(); tensorflow::RunOptions runOptions; tensorflow::SavedModelBundle bundle; // load everything - status = tensorflow::LoadSavedModel(sessionOptions, runOptions, modelDir, {"serve"}, &bundle); + status = tensorflow::LoadSavedModel(options.getSessionOptions(), runOptions, modelDir, {"serve"}, &bundle); if (!status.ok()) { std::cout << status.ToString() << std::endl; return; diff --git a/PhysicsTools/TensorFlow/test/testHelloWorldCUDA.cc b/PhysicsTools/TensorFlow/test/testHelloWorldCUDA.cc index 5d5fe53302601..4824359a4f544 100644 --- a/PhysicsTools/TensorFlow/test/testHelloWorldCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testHelloWorldCUDA.cc @@ -56,14 +56,14 @@ process.add_(cms.Service('CUDAService')) // object to load and run the graph / session tensorflow::Status status; - tensorflow::SessionOptions sessionOptions; - tensorflow::setBackend(sessionOptions, backend); + tensorflow::Options options{backend}; + tensorflow::setLogging("0"); tensorflow::RunOptions runOptions; tensorflow::SavedModelBundle bundle; // load everything std::string modelDir = dataPath_ + "/simplegraph"; - status = tensorflow::LoadSavedModel(sessionOptions, runOptions, modelDir, {"serve"}, &bundle); + status = tensorflow::LoadSavedModel(options.getSessionOptions(), runOptions, modelDir, {"serve"}, &bundle); if (!status.ok()) { std::cout << status.ToString() << std::endl; return; diff --git a/PhysicsTools/TensorFlow/test/testMetaGraphLoading.cc b/PhysicsTools/TensorFlow/test/testMetaGraphLoading.cc index ac9b7406b4a43..dc24ed7253ae6 100644 --- a/PhysicsTools/TensorFlow/test/testMetaGraphLoading.cc +++ b/PhysicsTools/TensorFlow/test/testMetaGraphLoading.cc @@ -33,20 +33,20 @@ void testMetaGraphLoading::test() { tensorflow::Backend backend = tensorflow::Backend::cpu; // load the graph - tensorflow::setLogging(); + tensorflow::Options options{backend}; tensorflow::MetaGraphDef* metaGraphDef = tensorflow::loadMetaGraphDef(exportDir); CPPUNIT_ASSERT(metaGraphDef != nullptr); // create a new, empty session - tensorflow::Session* session1 = tensorflow::createSession(backend); + tensorflow::Session* session1 = tensorflow::createSession(options); CPPUNIT_ASSERT(session1 != nullptr); // create a new session, using the meta graph - tensorflow::Session* session2 = tensorflow::createSession(metaGraphDef, exportDir, backend); + tensorflow::Session* session2 = tensorflow::createSession(metaGraphDef, exportDir, options); CPPUNIT_ASSERT(session2 != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, exportDir, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, exportDir, options), cms::Exception); // example evaluation tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10}); diff --git a/PhysicsTools/TensorFlow/test/testMetaGraphLoadingCUDA.cc b/PhysicsTools/TensorFlow/test/testMetaGraphLoadingCUDA.cc index 7fc7a5290a5f2..c550b55918f46 100644 --- a/PhysicsTools/TensorFlow/test/testMetaGraphLoadingCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testMetaGraphLoadingCUDA.cc @@ -55,19 +55,20 @@ process.add_(cms.Service('CUDAService')) // load the graph std::string exportDir = dataPath_ + "/simplegraph"; tensorflow::setLogging(); + tensorflow::Options options{backend}; tensorflow::MetaGraphDef* metaGraphDef = tensorflow::loadMetaGraphDef(exportDir); CPPUNIT_ASSERT(metaGraphDef != nullptr); // create a new, empty session - tensorflow::Session* session1 = tensorflow::createSession(backend); + tensorflow::Session* session1 = tensorflow::createSession(options); CPPUNIT_ASSERT(session1 != nullptr); // create a new session, using the meta graph - tensorflow::Session* session2 = tensorflow::createSession(metaGraphDef, exportDir, backend); + tensorflow::Session* session2 = tensorflow::createSession(metaGraphDef, exportDir, options); CPPUNIT_ASSERT(session2 != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, exportDir, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, exportDir, options), cms::Exception); // example evaluation tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10}); diff --git a/PhysicsTools/TensorFlow/test/testSessionCache.cc b/PhysicsTools/TensorFlow/test/testSessionCache.cc index 9cec510b4cea8..0383663eb69c3 100644 --- a/PhysicsTools/TensorFlow/test/testSessionCache.cc +++ b/PhysicsTools/TensorFlow/test/testSessionCache.cc @@ -30,11 +30,10 @@ void testSessionCache::test() { std::cout << "Testing CPU backend" << std::endl; tensorflow::Backend backend = tensorflow::Backend::cpu; - - tensorflow::setLogging(); + tensorflow::Options options{backend}; // load the graph and the session - tensorflow::SessionCache cache(pbFile, backend); + tensorflow::SessionCache cache(pbFile, options); CPPUNIT_ASSERT(cache.graph.load() != nullptr); CPPUNIT_ASSERT(cache.session.load() != nullptr); diff --git a/PhysicsTools/TensorFlow/test/testSessionCacheCUDA.cc b/PhysicsTools/TensorFlow/test/testSessionCacheCUDA.cc index 334c27501e422..651eed4aadc19 100644 --- a/PhysicsTools/TensorFlow/test/testSessionCacheCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testSessionCacheCUDA.cc @@ -54,7 +54,11 @@ process.add_(cms.Service('CUDAService')) // load the graph and the session std::string pbFile = dataPath_ + "/constantgraph.pb"; tensorflow::setLogging(); - tensorflow::SessionCache cache(pbFile, backend); + tensorflow::Options options{backend}; + + // load the graph and the session + tensorflow::SessionCache cache(pbFile, options); + CPPUNIT_ASSERT(cache.graph.load() != nullptr); CPPUNIT_ASSERT(cache.session.load() != nullptr); diff --git a/PhysicsTools/TensorFlow/test/testThreadPools.cc b/PhysicsTools/TensorFlow/test/testThreadPools.cc index 6c07b1d3c78ef..9e47b77c68f7b 100644 --- a/PhysicsTools/TensorFlow/test/testThreadPools.cc +++ b/PhysicsTools/TensorFlow/test/testThreadPools.cc @@ -31,18 +31,19 @@ void testGraphLoading::test() { std::cout << "Testing CPU backend" << std::endl; tensorflow::Backend backend = tensorflow::Backend::cpu; + tensorflow::Options options{backend}; // initialize the TBB threadpool int nThreads = 4; tensorflow::TBBThreadPool::instance(nThreads); + options.setThreading(nThreads); // load the graph - tensorflow::setLogging(); tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // prepare inputs @@ -69,7 +70,7 @@ void testGraphLoading::test() { CPPUNIT_ASSERT(outputs[0].matrix()(0, 0) == 46.); // tensorflow defaut pool using a new session - tensorflow::Session* session2 = tensorflow::createSession(graphDef, backend, nThreads); + tensorflow::Session* session2 = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); outputs.clear(); tensorflow::run(session2, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "tensorflow"); diff --git a/PhysicsTools/TensorFlow/test/testThreadPoolsCUDA.cc b/PhysicsTools/TensorFlow/test/testThreadPoolsCUDA.cc index 13e8278f2b83e..5c89aebfb407b 100644 --- a/PhysicsTools/TensorFlow/test/testThreadPoolsCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testThreadPoolsCUDA.cc @@ -51,10 +51,12 @@ process.add_(cms.Service('CUDAService')) std::cout << "Testing CUDA backend" << std::endl; tensorflow::Backend backend = tensorflow::Backend::cuda; + tensorflow::Options options{backend}; // initialize the TBB threadpool int nThreads = 4; tensorflow::TBBThreadPool::instance(nThreads); + options.setThreading(nThreads); // load the graph std::string pbFile = dataPath_ + "/constantgraph.pb"; @@ -63,7 +65,7 @@ process.add_(cms.Service('CUDAService')) CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // prepare inputs @@ -90,7 +92,7 @@ process.add_(cms.Service('CUDAService')) CPPUNIT_ASSERT(outputs[0].matrix()(0, 0) == 46.); // tensorflow defaut pool using a new session - tensorflow::Session* session2 = tensorflow::createSession(graphDef, backend, nThreads); + tensorflow::Session* session2 = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); outputs.clear(); tensorflow::run(session2, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "tensorflow"); diff --git a/PhysicsTools/TensorFlow/test/testVisibleDevices.cc b/PhysicsTools/TensorFlow/test/testVisibleDevices.cc index e1c6a33191925..02524df01cd9f 100644 --- a/PhysicsTools/TensorFlow/test/testVisibleDevices.cc +++ b/PhysicsTools/TensorFlow/test/testVisibleDevices.cc @@ -32,18 +32,18 @@ std::string testVisibleDevices::pyScript() const { return "createconstantgraph.p void testVisibleDevices::test() { std::string pbFile = dataPath_ + "/constantgraph.pb"; tensorflow::Backend backend = tensorflow::Backend::cpu; + tensorflow::Options options{backend}; // load the graph - tensorflow::setLogging(); tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, options), cms::Exception); std::vector response; tensorflow::Status status = session->ListDevices(&response); diff --git a/PhysicsTools/TensorFlow/test/testVisibleDevicesCUDA.cc b/PhysicsTools/TensorFlow/test/testVisibleDevicesCUDA.cc index b52a58118ef09..29f424f6cee0f 100644 --- a/PhysicsTools/TensorFlow/test/testVisibleDevicesCUDA.cc +++ b/PhysicsTools/TensorFlow/test/testVisibleDevicesCUDA.cc @@ -54,6 +54,8 @@ process.add_(cms.Service('CUDAService')) std::cout << "Testing CUDA backend" << std::endl; tensorflow::Backend backend = tensorflow::Backend::cuda; + tensorflow::Options options{backend}; + tensorflow::setLogging("0"); // load the graph std::string pbFile = dataPath_ + "/constantgraph.pb"; @@ -62,11 +64,11 @@ process.add_(cms.Service('CUDAService')) CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef - tensorflow::Session* session = tensorflow::createSession(graphDef, backend); + tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // check for exception - CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, backend), cms::Exception); + CPPUNIT_ASSERT_THROW(tensorflow::createSession(nullptr, options), cms::Exception); std::vector response; tensorflow::Status status = session->ListDevices(&response); @@ -74,11 +76,11 @@ process.add_(cms.Service('CUDAService')) // If a single device is found, we assume that it's the CPU. // You can check that name if you want to make sure that this is the case - std::cout << "Available devices: " << response.size() << std::endl; - CPPUNIT_ASSERT(response.size() == 2); for (unsigned int i = 0; i < response.size(); ++i) { std::cout << i << " " << response[i].name() << " type: " << response[i].device_type() << std::endl; } + std::cout << "Available devices: " << response.size() << std::endl; + CPPUNIT_ASSERT(response.size() == 2); // cleanup CPPUNIT_ASSERT(tensorflow::closeSession(session)); diff --git a/RecoTauTag/RecoTau/src/DeepTauBase.cc b/RecoTauTag/RecoTau/src/DeepTauBase.cc index 2af38735a9c30..7d8962cad47f0 100644 --- a/RecoTauTag/RecoTau/src/DeepTauBase.cc +++ b/RecoTauTag/RecoTau/src/DeepTauBase.cc @@ -214,10 +214,8 @@ namespace deep_tau { DeepTauCache::DeepTauCache(const std::map& graph_names, bool mem_mapped) { for (const auto& graph_entry : graph_names) { - tensorflow::SessionOptions options; - tensorflow::setThreading(options, 1); - // To be parametrized from the python config - tensorflow::setBackend(options, tensorflow::Backend::cpu); + // Backend : to be parametrized from the python config + tensorflow::Options options{tensorflow::Backend::cpu}; const std::string& entry_name = graph_entry.first; const std::string& graph_file = graph_entry.second; @@ -239,9 +237,9 @@ namespace deep_tau { throw cms::Exception("DeepTauCache: unable to load graph from ") << graph_file << ". \n" << load_graph_status.ToString(); - options.config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level( + options.getSessionOptions().config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level( ::tensorflow::OptimizerOptions::L0); - options.env = memmappedEnv_.at(entry_name).get(); + options.getSessionOptions().env = memmappedEnv_.at(entry_name).get(); sessions_[entry_name] = tensorflow::createSession(graphs_.at(entry_name).get(), options);