Skip to content

Commit 5efb51d

Browse files
zoyahavtfx-copybara
authored andcommitted
Cleanup: add exception chaining to graph_tools errors.
PiperOrigin-RevId: 465369075
1 parent 9cbe5c2 commit 5efb51d

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

tensorflow_transform/graph_tools.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,31 +179,27 @@ def wrapper(self, tensor_or_op):
179179
try:
180180
return func(self, tensor_or_op)
181181
except _UnexpectedPlaceholderError as e:
182-
if e.func_graph_name:
183-
raise ValueError(
184-
'The tensor_or_op {} depended on a placeholder ({}) that is part '
185-
'of a tf.function graph ({}), this is not supported. This may be a '
186-
'result of calling a tf.Transform analyzer in a tf.function'
187-
''.format(tensor_or_op, e.tensor, e.func_graph_name))
188-
else:
189-
raise ValueError(
190-
'The tensor_or_op {} depended on a placeholder ({}) that was not '
191-
'in the input_signature. This may have be caused by manually '
192-
'adding a placeholder to the graph'.format(tensor_or_op, e.tensor))
182+
context = (f' tf.function name: `{e.func_graph_name}`'
183+
if e.func_graph_name else '')
184+
raise ValueError(
185+
'The tensor_or_op {} depended on a placeholder ({}) that was not '
186+
'in the input_signature. This may have be caused by manually '
187+
'adding a placeholder to the graph.{}'.format(tensor_or_op, e.tensor,
188+
context)) from e
193189
except _UnexpectedTableError as e:
194190
if e.func_graph_name:
195191
raise ValueError(
196192
'The tensor_or_op {} depended on an initializable table ({}) that '
197193
'is part of a tf.function graph ({}), this is not supported. This'
198194
' may be a result of initializing a table in a tf.function'
199-
''.format(tensor_or_op, e.op, e.func_graph_name))
195+
''.format(tensor_or_op, e.op, e.func_graph_name)) from e
200196
else:
201197
raise ValueError(
202198
'The tensor_or_op {} depended on an initializable table ({}) that '
203199
'was not tracked by the graph analysis. This may be caused by '
204200
'adding an initializable table without adding its initializer to '
205201
'the collection tf.GraphKeys.TABLE_INITIALIZERS'.format(
206-
tensor_or_op, e.op))
202+
tensor_or_op, e.op)) from e
207203

208204
return wrapper
209205

@@ -687,15 +683,15 @@ def _get_table_init_op_source_info(self, table_init_op, graph_analyzer,
687683
raise ValueError(
688684
'The table initializer {} depended on a placeholder ({}). Note '
689685
'placeholders will not be fed during table initialization'.format(
690-
table_init_op, e.tensor))
686+
table_init_op, e.tensor)) from e
691687
except _UnexpectedTableError as e:
692688
if e.func_graph_name:
693689
raise e
694690
raise ValueError(
695691
'The table initializer {} depended on an initializable table ({}). '
696692
'Note tables are initialized in one pass so a table initializer '
697693
'cannot depend on the output of an initializeable table'.format(
698-
table_init_op, e.op))
694+
table_init_op, e.op)) from e
699695
return _SourceInfo(ready, path)
700696

701697
@property

tensorflow_transform/graph_tools_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,7 @@ def testInitializableGraphAnalyzerConstructorRaises(
636636
(_create_graph_with_y_function_of_x, [], {}, 'y',
637637
'may have be caused by manually adding a placeholder to the graph'),
638638
(_create_graph_with_placeholder_in_tf_function, ['x'], {}, 'z',
639-
r'that is part of a tf.function graph \(foo\), this is not supported. ' # pylint: disable=implicit-str-concat
640-
'This may be a result of calling a tf.Transform analyzer in a '
641-
'tf.function'),
639+
'manually adding a placeholder to the graph. tf.function name: `foo`'),
642640
(_create_graph_with_y_function_of_x_and_untracked_table, ['x'], {
643641
'filename': True
644642
}, 'y', 'may be caused by adding an initializable table without'),

0 commit comments

Comments
 (0)