
    BVh                     r    d 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 Z	d Z
d	 ZddZd ZddZd Zy
)zjUtility functions for control flow.

This file is copied from tensorflow/python/ops/control_flow_util.py.
    )
smart_cond)tensor)tensor_util)cond)	variablesc                 <    | j                         }t        |      d uS N)_get_control_flow_contextGetContainingXLAContext)graphctxts     _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/utils/control_flow_util.pyInXlaContextr      s     		(	(	*$	 	&d	22    c                 V    	 t        |       ry	 | j                  } # t        $ r Y yw xY w)NTF)r   outer_graphAttributeError)r   s    r   GraphOrParentsInXlaContextr       s8    E4e 	  s    	((c                 <    | j                         }t        |      d uS r	   )r
   GetContainingWhileContext)opr   s     r   IsInWhileLoopr   )   s     		%	%	'$	"4	(	44r   Nc                 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)IsWhileContextouter_context)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)IsXLAContextr   )r   s    r   r   r   D   s,     	4KD 	 
r   c                     t        | t        j                        rt        j                  | |||      S t	        j
                  | |||      S )a  Return either `true_fn()` if predicate `pred` is true else `false_fn()`.

  If `pred` is a bool or has a constant value, we return either `true_fn()`
  or `false_fn()`, otherwise we use `tf.cond` to dynamically route to both.

  Args:
    pred: A scalar determining whether to return the result of `true_fn` or
      `false_fn`.
    true_fn: The callable to be performed if pred is true.
    false_fn: The callable to be performed if pred is false.
    name: Optional name prefix when using `tf.cond`.

  Returns:
    Tensors returned by the call to either `true_fn` or `false_fn`.

  Raises:
    TypeError: If `true_fn` or `false_fn` is not callable.
  )true_fnfalse_fnname)
isinstancer   Variabler   smart_moduler   )predr    r!   r"   s       r   r   r   W   sJ    & i(()99gt= =		 	 
GhT
; ;r   c                    t        | t        j                        rt        j                  |       S | dv rt        |       S t        | t
              r| S t        | t        j                        ryt        dt        |       z        )a  Return the bool value for `pred`, or None if `pred` had a dynamic value.

  Args:
    pred: A scalar, either a Python bool or a TensorFlow boolean variable
      or tensor, or the Python integer 1 or 0.

  Returns:
    True or False if `pred` has a constant boolean value, None otherwise.

  Raises:
    TypeError: If `pred` is not a Variable, Tensor or bool, or Python
      integer 1 or 0.
  >   r      NzG`pred` must be a Tensor, or a Python bool, or 1 or 0. Found instead: %s)
r#   r   Tensorr   constant_valueboolr   r$   	TypeErrortype)r&   s    r   r*   r*   q   st     fmm$%%d++	V^:dKi(() &(,T
3 	4 4r   r	   )NNN)__doc__tensorflow.python.frameworkr   r%   r   r   tensorflow.python.opsr   r   r   r   r   r   r   r*    r   r   <module>r2      s?   
 C . 3 & +3
5
,&;44r   