Skip to content

Commit 7ade4c1

Browse files
pkchmn-robot
authored andcommitted
Internal change
PiperOrigin-RevId: 339487033
1 parent 7a741f5 commit 7ade4c1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

morph_net/framework/tpu_util.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
"""Utility functions for handling TPU graphs."""
22

3-
from __future__ import absolute_import
4-
from __future__ import division
5-
from __future__ import print_function
6-
73
import contextlib2
84
import tensorflow.compat.v1 as tf
95

@@ -33,12 +29,19 @@ def is_on_cpu():
3329

3430

3531
def get_variable_name(read_variable_op):
32+
"""Obtains the name of the variable corresponding to ReadVariableOp."""
3633
assert read_variable_op.type == 'ReadVariableOp'
3734
op = read_variable_op
38-
while op.type != 'VarHandleOp':
35+
# Depending on whether we're on TPU or CPU, and whether control flow v2 is
36+
# enabled, the graph will have different structure. This loop is written to
37+
# support all known cases.
38+
while True:
39+
if op.type == 'VarHandleOp':
40+
return op.name
41+
if op.type == 'Placeholder':
42+
return op.name.split('/ReadVariableOp/')[1]
3943
assert len(op.inputs) == 1
4044
op = op.inputs[0].op
41-
return op.name
4245

4346

4447
def maybe_convert_to_variable(tensor):

0 commit comments

Comments
 (0)