a
    SicA                     @   sV   d Z ddlm  mZ dd Zdd Zdd Zdd	d
Zdd Z	dddZ
dd ZdS )zjUtility functions for control flow.

This file is copied from tensorflow/python/ops/control_flow_util.py.
    Nc                 C   s   |   }t|d uS N)_get_control_flow_contextGetContainingXLAContext)graphctxt r   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/utils/control_flow_util.pyInXlaContext   s    r	   c                 C   s2   t | rdS z
| j} W q  ty*   Y dS 0 q d S )NTF)r	   outer_graphAttributeError)r   r   r   r   GraphOrParentsInXlaContext   s    
r   c                 C   s   |   }t|d uS r   )r   GetContainingWhileContext)opr   r   r   r   IsInWhileLoop&   s    r   c                 C   s$   | r |   s| |kr| S | j} q dS )a1  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_ctxtr   r   r   r   +   s
    r   c                 C   s   | r|   r| S | j} q dS )aP  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   r   r   r   r   C   s
    r   c                 C   s4   t | tjrtj| |||dS tjjj| |||d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)
isinstancetfVariablecond__internal__
smart_cond)predr   r   r   r   r   r   r   W   s
    r   c                 C   sZ   t | tjrt| S | dv r&t| S t | tr4| S t | tjrDdS tdt|  dS )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      Nz@`pred` must be a Tensor, or a Python bool, or 1 or 0. Received: )r   r   Tensorget_static_valueboolr   	TypeErrortype)r   r   r   r   constant_valueq   s    

r$   )N)NNN)__doc__tensorflow.compat.v2compatv2r   r	   r   r   r   r   r   r$   r   r   r   r   <module>   s   


