Skip to content

Commit dc71eee

Browse files
annarevnfelt
authored andcommitted
Update TensorBoard to import pywrap_tensorflow from tensorflow.python (#2096)
* Update TensorBoard to import pywrap_tensorflow from tensorflow.python instead of tensorflow.compat.v1. We are removing pywrap_tensorflow from TF API including tf.compat.v1. Importing from tensorflow.python is a temporary solution until a better API for reading records is available for TensorBoard to use. * Change call to NewCheckpointReader to tf.train.load_checkpoint instead. Also, added TODO
1 parent 3d8bd7a commit dc71eee

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

tensorboard/backend/event_processing/event_file_loader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import inspect
2222

2323
from tensorboard.compat import tf
24+
from tensorboard.compat import _pywrap_tensorflow
2425
from tensorboard.compat.proto import event_pb2
2526
from tensorboard.util import platform_util
2627
from tensorboard.util import tb_logging
@@ -38,7 +39,7 @@ def __init__(self, file_path):
3839
file_path = platform_util.readahead_file_path(file_path)
3940
logger.debug('Opening a record reader pointing at %s', file_path)
4041
with tf.errors.raise_exception_on_not_ok_status() as status:
41-
self._reader = tf.compat.v1.pywrap_tensorflow.PyRecordReader_New(
42+
self._reader = _pywrap_tensorflow.PyRecordReader_New(
4243
tf.compat.as_bytes(file_path), 0, tf.compat.as_bytes(''), status)
4344
# Store it for logging purposes.
4445
self._file_path = file_path

tensorboard/compat/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,34 @@ def tf2():
7272
# As a fallback, try `tensorflow.compat.v2` if it's defined.
7373
return tf.compat.v2
7474
raise ImportError('cannot import tensorflow 2.0 API')
75+
76+
77+
# TODO(https://github.com/tensorflow/tensorboard/issues/1711): remove this
78+
@_lazy.lazy_load('tensorboard.compat._pywrap_tensorflow')
79+
def _pywrap_tensorflow():
80+
"""Provide pywrap_tensorflow access in TensorBoard.
81+
82+
pywrap_tensorflow cannot be accessed from tf.python.pywrap_tensorflow
83+
and needs to be imported using
84+
`from tensorflow.python import pywrap_tensorflow`. Therefore, we provide
85+
a separate accessor function for it here.
86+
87+
NOTE: pywrap_tensorflow is not part of TensorFlow API and this
88+
dependency will go away soon.
89+
90+
Returns:
91+
pywrap_tensorflow import, if available.
92+
93+
Raises:
94+
ImportError: if we couldn't import pywrap_tensorflow.
95+
"""
96+
try:
97+
from tensorboard.compat import notf # pylint: disable=g-import-not-at-top
98+
except ImportError:
99+
try:
100+
from tensorflow.python import pywrap_tensorflow # pylint: disable=g-import-not-at-top
101+
return pywrap_tensorflow
102+
except ImportError:
103+
pass
104+
from tensorboard.compat.tensorflow_stub import pywrap_tensorflow # pylint: disable=g-import-not-at-top
105+
return pywrap_tensorflow

tensorboard/compat/tensorflow_stub/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,5 @@
3535
from . import pywrap_tensorflow # noqa
3636
from . import tensor_shape # noqa
3737

38-
# Set pywrap_tensorflow on v1 and avoid cycles on some imports
39-
compat.v1.pywrap_tensorflow = pywrap_tensorflow
40-
4138
# Set a fake __version__ to help distinguish this as our own stub API.
4239
__version__ = 'stub'

tensorboard/plugins/debugger/debugger_plugin_testlib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import portpicker # pylint: disable=import-error
2727
import tensorflow as tf
28+
from tensorflow.python import pywrap_tensorflow
2829
from werkzeug import wrappers
2930
from werkzeug import test as werkzeug_test
3031

@@ -60,7 +61,7 @@ def setUp(self):
6061
self.log_dir = self.get_temp_dir()
6162
file_prefix = tf.compat.as_bytes(
6263
os.path.join(self.log_dir, 'events.debugger'))
63-
writer = tf.compat.v1.pywrap_tensorflow.EventsWriter(file_prefix)
64+
writer = pywrap_tensorflow.EventsWriter(file_prefix)
6465
device_name = '/job:localhost/replica:0/task:0/cpu:0'
6566
writer.WriteEvent(
6667
self._CreateEventWithDebugNumericSummary(
@@ -106,7 +107,7 @@ def setUp(self):
106107
os.mkdir(run_foo_directory)
107108
file_prefix = tf.compat.as_bytes(
108109
os.path.join(run_foo_directory, 'events.debugger'))
109-
writer = tf.compat.v1.pywrap_tensorflow.EventsWriter(file_prefix)
110+
writer = pywrap_tensorflow.EventsWriter(file_prefix)
110111
writer.WriteEvent(
111112
self._CreateEventWithDebugNumericSummary(
112113
device_name=device_name,

tensorboard/plugins/debugger/events_writer_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import time
2828

2929
import tensorflow as tf
30+
from tensorflow.python import pywrap_tensorflow
3031
from tensorboard.util import tb_logging
3132

3233
logger = tb_logging.get_logger()
@@ -208,7 +209,7 @@ def _create_events_writer(self, directory):
208209
os.path.join(directory, DEBUGGER_EVENTS_FILE_STARTING_TEXT),
209210
time.time(), self._events_file_count)
210211
logger.info("Creating events file %s", file_path)
211-
return tf.compat.v1.pywrap_tensorflow.EventsWriter(tf.compat.as_bytes(file_path))
212+
return pywrap_tensorflow.EventsWriter(tf.compat.as_bytes(file_path))
212213

213214
def _fetch_events_files_on_disk(self):
214215
"""Obtains the names of debugger-related events files within the directory.

tensorboard/plugins/projector/projector_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from tensorboard.backend.http_util import Respond
3434
from tensorboard.compat import tf
35+
from tensorboard.compat import _pywrap_tensorflow
3536
from tensorboard.plugins import base_plugin
3637
from tensorboard.plugins.projector.projector_config_pb2 import ProjectorConfig
3738
from tensorboard.util import tb_logging
@@ -423,8 +424,7 @@ def _get_reader_for_run(self, run):
423424
reader = None
424425
if config.model_checkpoint_path and _using_tf():
425426
try:
426-
reader = tf.compat.v1.pywrap_tensorflow.NewCheckpointReader(
427-
config.model_checkpoint_path)
427+
reader = tf.train.load_checkpoint(config.model_checkpoint_path)
428428
except Exception: # pylint: disable=broad-except
429429
logger.warn('Failed reading "%s"', config.model_checkpoint_path)
430430
self.readers[run] = reader

0 commit comments

Comments
 (0)