
    AVh1                        d Z ddlZddlZddlmZ ddlmZ  ej                         xr  ej                  d      dk7  xs^  ej                  dd      dk7  xsF  ej                  dd      dk7  xs.  ej                  dd      dk7  xs  ej                  d	d      dk7  a
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd$dZd Zd Zd Z d  Z!d! Z"d" Z#d# Z$y)%zUtility functions for control flow.

This file is necessary to avoid cyclic dependencies between ops.py and
control_flow_ops.py.
    N)tf2)
tf_loggingTF_ENABLE_CONTROL_FLOW_V20TF_ENABLE_COND_V2TF_ENABLE_WHILE_V2TF_ENABLE_TENSOR_ARRAY_V2c                      da y)zHUse control flow v2.

  Do not use this symbol. This will be removed.
  TN)ENABLE_CONTROL_FLOW_V2     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/control_flow_util.pyenable_control_flow_v2r   %   s
      r   c                 H    t         xs | j                  xr t        | d       S )z:Returns whether control flow v2 should be used in `graph`.	_captured)r   building_functionhasattrgraphs    r   EnableControlFlowV2r   .   s+     
  
CAge[&A"ACr   c                     	 | j                  d      }|ry	 | j                         }t        |      d uS # t        $ r Y (w xY w)N_XlaCompileT)get_attr
ValueError_get_control_flow_contextGetContainingXLAContext)opxla_compilectxts      r   IsInXLAContextr    6   sP    	++m,K4{ 
	%	%	'$	 	&d	22 
 		s   4 	A A c                 <    | j                         }t        |      d uS N)r   r   )r   r   s     r   InXlaContextr#   @   s     		(	(	*$	 	&d	22r   c                 V    	 t        |       ry	 | j                  } # t        $ r Y yw xY w)NTF)r#   outer_graphAttributeErrorr   s    r   GraphOrParentsInXlaContextr'   E   s8    E4e 	  s    	((c                 <    | j                         }t        |      d uS r"   )r   GetContainingWhileContextr   r   s     r   IsInWhileLoopr+   N   s     		%	%	'$	"4	(	44r   c                 <    | j                         }t        |      d uS r"   )r   GetContainingCondContextr*   s     r   IsInCondr.   S   s     		%	%	'$	!$	't	33r   c                 B    | j                   dk(  xs | j                   dk(  S )z Return true if `op` is a Switch.Switch	RefSwitchtyper   s    r   IsSwitchr5   X   s    	H		6; 66r   c                 B    | j                   dk(  xs | j                   dk(  S )zReturn true if `op` is a Merge.MergeRefMerger2   r4   s    r   IsMerger9   ]       	G		4rww*44r   c                 B    | j                   dk(  xs | j                   dk(  S )z!Returns true if `op` is an Enter.EnterRefEnterr2   r4   s    r   IsLoopEnterr>   b   r:   r   c                 B    | j                   dk(  xs | j                   dk(  S )zReturn true if `op` is an Exit.ExitRefExitr2   r4   s    r   
IsLoopExitrB   g   s    	F		2bgg22r   c                 
   t        |       sy| j                  syd}| j                  D ]X  }|j                         D ]C  }|j                         }t	        |      r|j
                  }|xr |duxr |j                         }E Z |S )z4Return true if `op` is the Switch for a conditional.FTN)r5   outputs	consumersr   r>   outer_contextIsCondContext)r   is_cond_switchocr   s        r   IsCondSwitchrK   l   s    	"	
 .:: Aa[[] A((*d	Q!!% A4t+; ,@+/+=+=+? 	AA 
r   c                     t        |       sy| j                  syd}| j                  D ]1  }t        |j                        }|xr |duxr |j	                         }3 |S )z3Return true if `op` is the Merge for a conditional.FTN)r9   inputsGetOutputContextr   rG   )r   is_cond_mergeir   s       r   IsCondMergerQ      se    		
 -99 PaADD!D!Od$&6O4;M;M;OMP 
r   c                     t        |       r4| j                         }|duxr |j                         xr t        |        S y)z3Return true if `op` is the Switch for a while loop.NF)r5   r   IsWhileContextrK   r*   s     r   IsLoopSwitchrT      s@    b\'')DtN 3 3 5Nl2>N:NN	r   c                     t        |       r4| j                         }|duxr |j                         xr t        |        S y)z2Return true if `op` is the Merge for a while loop.NF)r9   r   rS   rQ   r*   s     r   IsLoopMergerV      s?    R['')DtM 3 3 5Mk"o:MM	r   c                 >    t        |       xr | j                  d      S )z'Return true iff op is a loop invariant.is_constant)r>   r   r4   s    r   IsLoopConstantEnterrY      s    	R	7R[[77r   c                     h d}| j                   }|j                  |v r(|j                  d   j                   }|j                  |v r(t        |      r|S dS )zCReturn the enter op if we can infer `value` to be a loop invariant.>   r0   Identityr1   RefIdentityr   N)r   r3   rM   rY   )valueid_opsr   s      r   GetLoopConstantEnterr_      sM    =&xx"
6	1B 	6"2&0D0r   c                 X    | j                         }|t        |       r|j                  }|S )z8Return the control flow context for the output of an op.)r   rB   rF   r*   s     r   rN   rN      s.    		%	%	'$ 
*R.D	+r   c                 T    | r&| j                         s| |k(  r| S | j                  } | r&y)a  Returns the first ancestor WhileContext of `ctxt`.

  Returns `ctxt` if `ctxt` is a WhileContext, or None if `ctxt` is not in a
  while loop.

  Args:
    ctxt: ControlFlowContext
    stop_ctxt: ControlFlowContext, optional. If provided, the search will end
      if it sees stop_ctxt.

  Returns:
    `ctxt` if `ctxt` is a WhileContext, the most nested WhileContext containing
    `ctxt`, or None if `ctxt` is not in a while loop.  If `stop_ctxt` is not
    `None`, this returns `ctxt` if it matches `stop_ctxt` in its traversal.
  N)rS   rF   )r   	stop_ctxts     r   r)   r)      s3      		 1$;D 	 
r   c                 J    | r!| j                         r| S | j                  } | r!y)a@  Returns the first ancestor XLAContext of `ctxt`.

  Returns `ctxt` if `ctxt` is a XLAContext, or None if `ctxt` is not in a
  while loop.

  Args:
    ctxt: ControlFlowContext

  Returns:
    `ctxt` if `ctxt` is a XLAContext, the most nested XLAContext containing
    `ctxt`, or None if `ctxt` is not in a while loop.
  N)IsXLAContextrF   r   s    r   r   r      s,     	4KD 	 
r   c                 J    | r!| j                         r| S | j                  } | r!y)a6  Returns the first ancestor CondContext of `ctxt`.

  Returns `ctxt` if `ctxt` is a CondContext, or None if `ctxt` is not in a cond.

  Args:
    ctxt: ControlFlowContext

  Returns:
    `ctxt` if `ctxt` is a CondContext, the most nested CondContext containing
    `ctxt`, or None if `ctxt` is not in a cond.
  N)rG   rF   re   s    r   r-   r-      s,     	D[D 	 
r   c                 4    | |ur| y| j                   } | |ury)z>Returns true if `maybe_containing_ctxt` is or contains `ctxt`.FT)rF   )r   maybe_containing_ctxts     r   IsContainingContextri      s-    ))|ED 	)) 
r   c                 6    t        | j                         |      S r"   )ri   r   r*   s     r   OpInContextrk      s    	R99;T	BBr   c                 .    t        | j                  |      S r"   )rk   r   )tensorr   s     r   TensorInContextrn     s    	VYY	%%r   c                    | j                         }t        |      }d}|sd}n/||u rd}n't        |      }t        |      }||d}t        |       rd}t	        |       rd}nt        ||      rd}n|j                  r#t        |j                  j                  |      rd}n|j                  r%|j                  j                  |j                  u rd}n|j                  r|j                  j                  |u rd}nZ|j                  rN|j                  j                  j                  r.|j                  j                  j                  j                  |u rd}|srd|j                   d| j                   d}n*d|j                   d| j                   d|j                   d}|}|d	| j                  d
|z  }|d|j                  d
z  }|d| j                  ddj                  t        j                  | j                              d|j                  ddj                  t        j                  |j                              d	z  }t        j                  |       t        |dz         y)an  Returns whether `input_op` can be used from `op`s context.

  Conceptually, only inputs from op's while context or any ancestor while
  context (including outside of any context) are valid. In practice, there are
  many other edge cases as well.

  Args:
    op: Operation
    input_op: Operation

  Raises:
    ValueError: if input_op is from an invalid context.
  FTNzCannot use 'z' as input to 'z,' because they are in different while loops.z' because 'z' is in a while loop.z

z while context: 
z

Traceback for z:
 z
Traceback for z See info log for more details.)r   rN   r)   r>   r5   ri   
grad_stateforward_context_outer_contextnamejoin	tracebackformat_listlogginginfor   )	r   input_opop_ctxt
input_ctxtvalid
while_ctxtinput_while_ctxt	error_msglog_msgs	            r   CheckInputFromValidContextr     sO    ((*')*
%	E*E*73J0<		! 	R	"	Z)9	: e



j33CC.0 e






/
/

)
)*
 e

%
%

%
%
5
5
C e

%
%



/
/
:
:



/
/
:
:
J
J

 e	rwwi @* * 
 rwwi @mm_13  GRWWjAAG8==:JKKG
..r||<=rwwy44X5G5GHIK KG LL
Y!BB
CC) 
r   c                 J    | j                         }|r|j                         }|S )z.Get the WhileContext to which this op belongs.)r   GetWhileContextr*   s     r   r   r   j  s&    		%	%	'$	!D	+r   r"   )%__doc__osrw   tensorflow.pythonr   tensorflow.python.platformr   ry   enabledgetenvr   r   r   r    r#   r'   r+   r.   r5   r9   r>   rB   rK   rQ   rT   rV   rY   r_   rN   r)   r   r-   ri   rk   rn   r   r   r   r   r   <module>r      s?    
  ! <&3;;= I$299%@ASHM#"))$?ELM $"))$7=DM $"))$8#>#E	M
 $"))$?EL  C33
5
4
7
5
5
3
*"8
1,&$C&aDHr   