
    BVh8                     .   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ e	j                   Zd Zd Z ej&                  dg d      Z ej&                  dddg      Zd Zd Z edg       	 	 	 	 	 	 dd       Zy)zUtility to lift subgraphs.    N)
func_graph)ops)tensor)	array_ops)op_selector)resource_variable_ops)compat)object_identity)	tf_exportc                 R    t        | t        j                        r| j                  S | S N)
isinstance
tensor_libTensoropop_or_tensors    U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/eager/lift_to_graph.py_as_operationr   "   s"    j//0??	    c                 ^    t        d t        j                  t        |             D              S )Nc              3   |   K   | ]4  }t        |      j                  d k(  xr t        |      j                    6 yw)ConstN)r   typecontrol_inputs).0is     r   	<genexpr>z#_constant_inputs.<locals>.<genexpr>)   sE      M 1""h. 5"1%4445 Ms   :<)allr   graph_inputsr   r   s    r   _constant_inputsr!   (   s0    	 M!..}\/JKM 
M Mr   _InputMutation	copied_opinput_indexold_graph_tensor_ControlMutationr$   old_graph_opc                    g }g }g }t        | j                        D ]s  \  }}|j                  |d      }	|	It        j                  d|j
                  |j                        }	|j                  t        d||             |j                  |	       u g }
| j                  D ]D  }|j                  |d      }||j                  t        d|             4|
j                  |       F t        j                  |
      5  t        j                  | j                        5  |j                  j                  | j                  d      }|<t!        j"                  |j$                        |j                  vr|j'                  |       |j)                  | j                  || j*                  D cg c]  }|j                   c}| j,                  j.                  j1                         D ci c]*  \  }}|j3                  d      s|j3                  d      s||, c}}| j$                        }ddd       ddd       || <   t        | j*                        D ]  \  }}|j*                  |   ||<    |D cg c]  }|j5                  |	       c}|D cg c]  }|j5                  |	       c}fS c c}w c c}}w # 1 sw Y   xY w# 1 sw Y   xY wc c}w c c}w )
a  Copy an op directly to a given graph.

  Generally `op`'s inputs should already have been copied. If this is not the
  case, for example with v1 while_loops, then `_copy_non_source` inserts
  placeholders for the unavailable Tensors and returns a list of required
  mutations.

  Args:
    op: The op to be copied.
    graph: The destination graph.
    op_map: A dict mapping ops and tensors in the old graph to the new one.
    base_graph: The graph we're copying from, for any necessary functions.
  Returns:
    A tuple of (required_inputs, required_control_inputs):
      required_inputs:
        A list of `_InputMutation` tuples containing inputs to `copied_op` which
        must be updated once `old_graph_tensor` has been copied.
      required_control_inputs:
        A list of `_ControlMutation` tuples containing control inputs to
        `copied_op` which must be added once `old_graph_op` has been copied.
  Nunused_control_flow_input)nameshapedtyper#   )r$   r(   _class_tpu_replicate)op_typeinputsdtypesattrsr+   )r$   )	enumerater1   getr   placeholderr,   r-   appendr"   r   r'   r   control_dependenciesdevice
_functionsr   r	   as_strr+   add_to_graph	create_opoutputsnode_defattritems
startswith_replace)r   graphop_map
base_graphinput_mutationscontrol_mutationscopied_inputsr%   original_inputcopied_inputcopied_control_inputsoriginal_control_inputcopied_control_inputfxkeyvaluer$   r   omutations                        r   _copy_non_sourcerU   <   s   , /-%.ryy%9 '!k>::nd3L ***$$$$&l 
4%0*8:;
 &'"  " 1 1 9!::&<dC#
T(>@A ""#789  56 

2998M !!"''40A}qvv.e6F6FFnnU !#,A,)+)9)9)?)?)A
%3>>(+/0 J

 WW   	I $ &*

# %da!!!$F1I% +, y1 , -. y1 .
/ / -
   ,,.sO   < K B
J4&J)9)J4"/J.J4%K -KK)J44J=	9K  K	c                    |r0| |v r,|j                  ||    | j                  j                        }nU| j                  j                  dk(  rt	        |       r| j                  j
                  d   }t        |j                  |||      \  }}	|s|	rt        dj                  |            t        j                  | j                  j                        5  t        j                  ||   | j                  | j                  j                        }ddd       nrt        j                  | j                  j                        5  t        j                  | j                  | j                  | j                  j                        }ddd       t!        j"                  |       }
|
j$                  rt!        j&                  |
d	
       || <   |j                  || j                  <   y# 1 sw Y   axY w# 1 sw Y   mxY w)a  Create a source in a graph based on a Tensor from a different graph.

  This function creates a placeholder analog of `s` in a graph with the
  following behavior:

  1) If s is a captured Tensor or Variable and handle_captures is set to True,
     simply capture it in the new graph as well.

  2) If s is a PlaceholderWithDefault whose default is a constant, preserve
     said default in the new graph.

  3) When applicable, copy resource variable metadata from `s` to the newly
     created placeholder.

  Args:
    s: The source of interest.
    graph: The destination graph.
    op_map: A dict mapping ops and tensors in the old graph to the new one.
    handle_captures: A boolean indicating whether to re-capture s in the new
      graph or simply create a vanilla placeholder.
    inverse_captures: A dict mapping s back to the Tensor or Variable that it
      captures.
    base_graph: The graph being copied from.
  )r+   PlaceholderWithDefaultr   r   rD   rE   rF   z4Could not copy source node {} because it has inputs.)inputr,   r+   N)r-   r,   r+   T)
graph_mode)capturer   r+   r   r!   r1   rU   AssertionErrorformatr   r9   r   placeholder_with_defaultr,   r6   r-   r   get_resource_handle_datashape_and_type_set_handle_shapes_and_types)srD   rE   handle_capturesinverse_capturesrF   copied_placeholderdefault_valueunavailable_inputsunavailable_control_inputsbase_handles              r   _copy_sourcerj      s   4 ..'7':Kttyy,,1A!1DDDKKNM5E5622 7
@6- " " 
ADDKK	  F$==}%QWW14499FF F 
ADDKK	  8$00qwwQTTYY88 &>>qA+66
 !&) $&&&,%F F8 8s   :G ?AG, G),G5z__internal__.lift_to_graph)v1c                   ! g }g }	| D ]?  }
t        |
t        j                        r|j                  |
       /|	j                  |
       A |xs |	d   j                  }|xs t        j                         }t        j                  |xs g       }t        d |D              }t        j                  t              }|	D ],  }|j                  t        j                  ||||||             . g }t        g       !|	D cg c]  }|t        |         st        |       }}t        |      }|r|r|j                         }|!v r!j!                  |       |j                  |       t        j"                  |      D ]Q  }|j$                  dk(  r|j!                  |       t'        !fd||   D              s<||vsA|j                  |       S |r|j)                  !       |r#|j                  t+        t-        |                   |r|j/                  d        g }t        j                         }g }t        |t0        j2                        rAt        |t0        j2                        r'|j4                  }|D ]
  \  }}|||<    |j6                  }|j9                         5  |D ]  }|||<   	 t               }|D ]D  }||v s|j;                  |       |j!                  |j<                         t?        ||||||       F |D ].  }|j!                  |j<                         t?        ||||||       0 g }g }tA        |      D ]?  }||v s||v rtC        ||||	      \  }}|jE                  |       |jE                  |       A |jG                         5  |D ]5  } | jH                  jK                  | jL                  || jN                            7 |D ]D  } | jP                  j$                  dk(  r| jH                  jS                  || jP                            F 	 d
d
d
       |cd
d
d
       S c c}w # 1 sw Y   xY w# 1 sw Y   y
xY w)a  Copies the tensor and all its inputs recursively to the outer graph.

  Args:
    tensors: The Tensors to lift.
    graph: The graph to lift to.
    sources: Optional sequence of nodes to start from. If omitted the whole
      subgraph which feeds into `init_tensor` is lifted.
    disallowed_placeholders: An optional set of ops which may not appear in the
      lifted graph. Defaults to all placeholders.
    add_sources: A boolean indicating whether placeholders which are not in
      sources should be allowed.
    handle_captures: A boolean indicating whether to re-capture s in the new
      graph or simply create a vanilla placeholder.
    base_graph: The graph from which to lift ops. This will be inferred if not
      specified.
    op_map: A map contains all the existing nodes that have been lifted to the
      destination graph, so they won't be lifted and copied again.

  Returns:
    A mapping from ops in the current default graph to ops in `graph`.

  Raises:
    UnliftableError: If a placeholder blocks lifting.
  r   c              3   4   K   | ]  }|j                     y wr   r   )r   rP   s     r   r   z lift_to_graph.<locals>.<genexpr>   s     *QADD*s   )init_tensorsourcesdisallowed_placeholdersvisited_ops
op_outputsadd_sourcesTPUReplicateMetadatac              3   &   K   | ]  }|v  
 y wr    )r   rP   
marked_opss     r   r   z lift_to_graph.<locals>.<genexpr>  s     9AZ9s   c                 D    t        t        j                  |             dk(  S )Nr   )lenr   r    rn   s    r   <lambda>zlift_to_graph.<locals>.<lambda>(  s    3{'?'?'C#D#I r   )rQ   )rb   rD   rE   rc   rd   rF   rX   N)*r   r   ResourceVariabler7   rD   r
   ObjectIdentityDictionaryObjectIdentitySetsetcollectionsdefaultdictupdater   map_subgraphr   popaddr    r   r   difference_updatenextitersortr   	FuncGraphcapturesinternal_captures
as_defaultremover   rj   reversedrU   extend_mutation_lockr$   _update_inputr%   r&   r(   _add_control_input)"tensorsrD   rp   rq   rt   rc   rF   rE   variable_init_tensorsinit_tensorsr   rr   rs   ro   ops_to_copytops_to_visitunvisited_opsr   inpr   rd   r   external_captureinternal_capturer   
source_opsrb   rG   rH   new_input_mutationsnew_control_mutationsrT   rx   s"                                    @r   lift_to_graphr      s   B , "f&/@@A""6*&!	"
 2\!_22*?_==?& --gm<'*'**+&&s+* " "kNN;++ 7! "" +2w*,8 7q#M!$45  " 7, 7l#-
b	z	nnR))"- ## 88--
#9C99w


c
"#  ##J/ $tM234+ 	8 IK ($==?Z112
,,-""H.6 <**+;'(<"44
  4" fQiJ 
#	
gqqtt+-!	#	
#  !nnQTT)+!! O{# 6	z	R6\3CuV
4D0001456 
			 	M% E(((  &)B)B"C	EE ( M(   %%)??
--fX5J5J.KLM		M i4 4a7p	M 	MQ4 4s2   ; P* P;?CP;BP/	P;/P8	4P;;Q)NNFFNN)__doc__r   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r   r   tensorflow.python.utilr	   r
    tensorflow.python.util.tf_exportr   UnliftableErrorr   r!   
namedtupler"   r'   rU   rj   r   rw   r   r   <module>r      s     !  2 + < + - 7 ) 2 6 --M (''46 *;)).!# 
P/f9'x 'B/ *.#"'!a 0ar   