[XLA:Python] Raise an error if no backends are successfully initialized.
Prior to this change, this would fail when trying to access list(backends.values())[-1], and give no indication what the actual error was.
PiperOrigin-RevId: 275283415
Change-Id: I48a4aab0aea27794dcfdc7f8f9884f8369d4c031
diff --git a/tensorflow/compiler/xla/python/xla_client.py b/tensorflow/compiler/xla/python/xla_client.py
index 7f6b676..29b6278 100644
--- a/tensorflow/compiler/xla/python/xla_client.py
+++ b/tensorflow/compiler/xla/python/xla_client.py
@@ -25,6 +25,7 @@
import itertools
import os
+from absl import logging
import numpy as np
import six
@@ -213,12 +214,17 @@
_local_backends = collections.OrderedDict()
for name, factory in _local_backend_factories.items():
+ logging.vlog(2, "Initializing backend '%s'" % name)
try:
backend = factory()
except RuntimeError:
- # If the backend isn't built into the binary, or if it has no devices, we
- # expect a RuntimeError.
- continue
+ if name == 'cpu':
+ # We always expect CPU to initialize successfully.
+ raise
+ else:
+ # If the backend isn't built into the binary, or if it has no devices,
+ # we expect a RuntimeError.
+ continue
_local_backends[name] = backend
return _local_backends