@@ -366,10 +366,15 @@ def _protobuf_convert(cls, k, v):
366
366
raise TypeError (f"Could not convert { k } " )
367
367
368
368
def __init__ (self , op , name , attr , obj = None ):
369
+ """Create a TF meta NodeDef.
370
+
371
+ XXX: Meta NodeDefs with `name == None` have a special meaning;
372
+ their names are uniquely generated. We still consider them equal
373
+ (when every other property is equal, of course).
374
+ """
369
375
super ().__init__ (obj = obj )
370
376
self .op = metatize (op )
371
- assert name is not None
372
- self .name = name if isvar (name ) else str (name )
377
+ self .name = name if isvar (name ) else name
373
378
374
379
if not isvar (attr ):
375
380
opdef_sig , _ = op_def_lib .get_op_info (self .op )
@@ -601,7 +606,7 @@ def reify(self):
601
606
#
602
607
try :
603
608
existing_op = ops .get_default_graph ().get_operation_by_name (self .name )
604
- except KeyError :
609
+ except ( KeyError , TypeError ) :
605
610
#
606
611
# There is no such `Operation`, so we attempt to create it
607
612
#
@@ -613,7 +618,15 @@ def reify(self):
613
618
# An `Operation` with this name exists, let's make sure it's
614
619
# equivalent to this meta `Operation`
615
620
#
616
- if self != mt (existing_op ):
621
+ existing_op_mt = mt (existing_op )
622
+
623
+ # # Since we can't exactly reproduce all NodeDef.attr information
624
+ # # (e.g. dtypes), we need to remove any unnecessary NodeDef.attr
625
+ # # fields from comparisons with same-named nodes in the graph.
626
+ # if op_attrs.keys() != node_attr.keys():
627
+ # existing_op_mt.node_def.attr = node_attr
628
+
629
+ if self != existing_op_mt :
617
630
raise MetaReificationError (
618
631
f"An Operation with the name { self .name } "
619
632
" already exists in the graph and is not"
@@ -987,48 +1000,22 @@ def __api_call__(self, *args, **kwargs):
987
1000
988
1001
if not op_args_unreified :
989
1002
990
- res_var = None
991
- # name = op_args.get("name", None)
992
1003
#
993
- # if name is not None:
994
- # #
995
- # # An operation with this name might already exist in the graph
996
- # #
1004
+ # We create the `Operation` in the graph
997
1005
#
998
- # from tensorflow.python.framework import ops
999
- #
1000
- # try:
1001
- # this_op = ops.get_default_graph().get_operation_by_name(name)
1002
- # except KeyError:
1003
- # pass
1004
- # else:
1005
- # # TODO: Make sure the existing `Operation` matches our arguments
1006
- # assert this_op.type == self.op_def.obj.name
1007
- #
1008
- # this_op = mt(this_op)
1009
- # op_inputs, op_node_def = self.op_args_to_operation_inputs(op_args)
1010
- # assert op_inputs == this_op.inputs
1011
- # assert op_node_def == this_op.node_def
1012
- # res_var = this_op.default_output
1013
-
1014
- if res_var is None :
1015
- #
1016
- # We create the `Operation` in the graph
1017
- #
1018
-
1019
- tf_out = self ._apply_func (** op_args )
1020
-
1021
- # Ensure that the original meta objects will be available
1022
- # for use in the `metatize` that follows
1023
- tf_metatize_cache .update (
1024
- {
1025
- k : v
1026
- for k , v in zip (op_args .values (), apply_arguments .values ())
1027
- if isinstance (k , tf .Tensor )
1028
- }
1029
- )
1006
+ tf_out = self ._apply_func (** op_args )
1007
+
1008
+ # Ensure that the original meta objects will be available
1009
+ # for use in the `metatize` that follows
1010
+ tf_metatize_cache .update (
1011
+ {
1012
+ k : v
1013
+ for k , v in zip (op_args .values (), apply_arguments .values ())
1014
+ if isinstance (k , tf .Tensor )
1015
+ }
1016
+ )
1030
1017
1031
- res_var = metatize (tf_out )
1018
+ res_var = metatize (tf_out )
1032
1019
1033
1020
if "names" in meta ._lvar_defaults_enabled :
1034
1021
# This should also reset the NodeDef's `obj`
@@ -1073,7 +1060,8 @@ def op_args_to_operation_inputs(self, apply_arguments):
1073
1060
node_attr = var ()
1074
1061
1075
1062
if "names" not in meta ._lvar_defaults_enabled :
1076
- op_name = apply_arguments .get ("name" , op_def_tf .name ) or op_def_tf .name
1063
+ # default_name = ops.get_default_graph().unique_name(op_def_tf.name, mark_as_used=False)
1064
+ op_name = apply_arguments .get ("name" , None )
1077
1065
else :
1078
1066
op_name = var ()
1079
1067
0 commit comments