@@ -366,10 +366,15 @@ def _protobuf_convert(cls, k, v):
366366 raise TypeError (f"Could not convert { k } " )
367367
368368 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+ """
369375 super ().__init__ (obj = obj )
370376 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
373378
374379 if not isvar (attr ):
375380 opdef_sig , _ = op_def_lib .get_op_info (self .op )
@@ -601,7 +606,7 @@ def reify(self):
601606 #
602607 try :
603608 existing_op = ops .get_default_graph ().get_operation_by_name (self .name )
604- except KeyError :
609+ except ( KeyError , TypeError ) :
605610 #
606611 # There is no such `Operation`, so we attempt to create it
607612 #
@@ -613,7 +618,15 @@ def reify(self):
613618 # An `Operation` with this name exists, let's make sure it's
614619 # equivalent to this meta `Operation`
615620 #
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 :
617630 raise MetaReificationError (
618631 f"An Operation with the name { self .name } "
619632 " already exists in the graph and is not"
@@ -987,48 +1000,22 @@ def __api_call__(self, *args, **kwargs):
9871000
9881001 if not op_args_unreified :
9891002
990- res_var = None
991- # name = op_args.get("name", None)
9921003 #
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
9971005 #
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+ )
10301017
1031- res_var = metatize (tf_out )
1018+ res_var = metatize (tf_out )
10321019
10331020 if "names" in meta ._lvar_defaults_enabled :
10341021 # This should also reset the NodeDef's `obj`
@@ -1073,7 +1060,8 @@ def op_args_to_operation_inputs(self, apply_arguments):
10731060 node_attr = var ()
10741061
10751062 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 )
10771065 else :
10781066 op_name = var ()
10791067
0 commit comments