diff --git a/PhysicsTools/TensorFlow/src/TensorFlow.cc b/PhysicsTools/TensorFlow/src/TensorFlow.cc
index fcb09e2e9c449..786166ef407a9 100644
--- a/PhysicsTools/TensorFlow/src/TensorFlow.cc
+++ b/PhysicsTools/TensorFlow/src/TensorFlow.cc
@@ -42,6 +42,15 @@ namespace tensorflow {
// NVidia GPU
else if (backend == Backend::cuda) {
if (not ri->nvidiaDriverVersion().empty()) {
+ // Check if one GPU device is visible to TF
+ // If not, an exception is raised --> this can happen in case of driver version mismatch
+ // or missing CUDA support in TF compilation
+ if ((*_options.config.mutable_device_count())["GPU"] == 0) {
+ edm::Exception ex(edm::errors::UnavailableAccelerator);
+ ex << "Cuda backend requested, NVIDIA GPU visible to cmssw, but not visible to TensorFlow in the job";
+ ex.addContext("Calling tensorflow::setBackend()");
+ throw ex;
+ }
// Take only the first GPU in the CUDA_VISIBLE_DEVICE list
(*_options.config.mutable_device_count())["GPU"] = 1;
_options.config.mutable_gpu_options()->set_visible_device_list("0");
diff --git a/PhysicsTools/TensorFlow/test/BuildFile.xml b/PhysicsTools/TensorFlow/test/BuildFile.xml
index 03ca557c61619..c51046ba68ac9 100644
--- a/PhysicsTools/TensorFlow/test/BuildFile.xml
+++ b/PhysicsTools/TensorFlow/test/BuildFile.xml
@@ -5,7 +5,7 @@
-
+
@@ -29,7 +29,7 @@
-
+
@@ -52,7 +52,7 @@
-
+
@@ -75,7 +75,8 @@
-
+
+
@@ -98,7 +99,7 @@
-
+
@@ -121,7 +122,8 @@
-
+
+
@@ -145,7 +147,7 @@
-
+