
    BVh."                        d Z ddlm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$                  e	j&                  e	j(                  e	j*                  e	j,                  fZ G d dej0                        Z G d dej4                  ej6                        Z G d dej:                  e      Z G d dej>                  e      Z  G d de!      Z"d Z#d Z$ ejJ                  ee$       y)z-An extension type that represents WeakTensor.    )OptionalN)context)composite_tensor_gradient)dtypes)errors)extension_type)opstensor)tensor_conversion_registry)corec                       e Zd ZdZd Zd Zy)WeakTensorGradientz'CompositeTensorGradient for WeakTensor.c                     |j                   S Nr
   )selfweak_tensors     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/weak_tensor.pyget_gradient_componentsz*WeakTensorGradient.get_gradient_components,   s        c                 :    |j                   j                  |g      S r   )
_type_spec_from_components)r   r   component_gradss      r   replace_gradient_componentsz.WeakTensorGradient.replace_gradient_components/   s    !!22O3DEEr   N)__name__
__module____qualname____doc__r   r    r   r   r   r   )   s    /Fr   r   c                      e Zd ZU dZdZ ej
                  ed<   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eej,                     dee   fdZd Zd Zd Zed        Zed        Zed        Z ed        Z! e"       Z#y)
WeakTensora  A weakly typed Tensor.

  A simple wrapper class that contains a normal Tensor.

  A "weak" type means that its dtype is temporarily inferred by the system,
  and could defer to other dtypes.

  i.g. weak f64 + f16 => f16

  This information is used for auto dtype conversion.
  ztf.WeakTensorr   c                     | j                   j                  t        vr)t        | j                   j                   dt         d      y )Nz3 not allowed as a weak type. The allowed types are .)r   dtype_ALLOWED_WEAK_DTYPES	TypeErrorr   s    r   __validate__zWeakTensor.__validate__D   sJ    {{ 44[[  33G2HK  5r   c                 &    | j                  d      S )NFis_repr_format_weak_tensorr(   s    r   __str__zWeakTensor.__str__K   s    ##E#22r   c                 &    | j                  d      S )NTr+   r-   r(   s    r   __repr__zWeakTensor.__repr__N   s    ##D#11r   c                     |r| j                   j                         n| j                   j                         }|t        |      dz
     }|j	                  |      }|d | dz   |z   S )N   z, weak=True)r   r1   r/   lenrfind)r   r,   
tensor_strclosing_char
last_indexs        r   r.   zWeakTensor._format_weak_tensorQ   s_    +2%%'8K8K8MJc*o12L!!,/Jkz"]2\AAr   c                 4    t        | j                  g|i |S r   )getattrr   )r   argskwargss      r   __getattr__zWeakTensor.__getattr__W   s     4;;0000r   c                 2    t        j                  | d      )Na   is not allowed. You can attempt the following resolutions to the problem: If you are running in Graph mode, use Eager execution mode or decorate this function with @tf.function. If you are using AutoGraph, you can try decorating this function with @tf.function. If that does not work, then you may be using an unsupported feature or your source code may not be visible to AutoGraph. See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code for more information.)r   OperatorNotAllowedInGraphError)r   tasks     r   	_disallowzWeakTensor._disallow\   s$    

/
/& ! 	!	 	r   c                 &    | j                  d       y )Nz)Iterating over a symbolic `tf.WeakTensor`)rA   r(   s    r   _disallow_iterationzWeakTensor._disallow_iterationh   s    NN>?r   c                     | j                   j                  .| j                   j                  D cg c]  }|j                   c}S y c c}w r   )shapendimsdimsvalue)r   dims     r   _shape_as_listzWeakTensor._shape_as_listk   s7    zz##'::??3Ccii33 4s   Ac                     t        j                         s| j                          | j                  j	                         }t        | |      S r   )r   executing_eagerlyrC   r   _get_first_dim_WeakTensorIterator)r   	first_dims     r   __iter__zWeakTensor.__iter__q   s9    $$&
 **,ItY//r   c                 6    | j                   j                         S r   )r   __hash__r(   s    r   rR   zWeakTensor.__hash__w       ;;!!r   c                     | S r   r    r(   s    r   __copy__zWeakTensor.__copy__z   s    Kr   c                 6    | j                   j                         S r   )r   __len__r(   s    r   rW   zWeakTensor.__len__~       ;;  r   c                 6    | j                   j                         S r   )r   __bool__r(   s    r   rZ   zWeakTensor.__bool__   rS   r   Nr%   namec                 <    | j                   j                  ||      S )N)r%   r[   )r   __tf_tensor__)r   r%   r[   s      r   r]   zWeakTensor.__tf_tensor__   s     ;;$$5t$<<r   c                     ~| S r   r    )r   memos     r   __deepcopy__zWeakTensor.__deepcopy__   s
    Kr   c                     | j                   S )z.Converts this 'WeakTensor' into a 'tf.Tensor'.r
   r(   s    r   	to_tensorzWeakTensor.to_tensor       ;;r   c                     | j                   S )z"Convert `self` to a graph element.r
   r(   s    r   _as_graph_elementzWeakTensor._as_graph_element   rc   r   c                     t        |t        j                        rt        |      S t        |t        j                        rt        |      S t        j                  dddt        |       d      )aW  Converts a 'tf.Tensor' into a 'WeakTensor'.

    This should be the standard way of creating a WeakTensor instead
    of directly calling the WeakTensor constructor.

    Args:
      tensor: The `tf.Tensor` that should be converted into a 'WeakTensor'.

    Returns:
      A `EagerWeakTensor` or 'GraphWeakTensor' that holds the `tensor`.
    NzHWeakTensor can only be constructed from tf.Tensor or tf.WeakTensor, but z was given.)	
isinstancer   ValueEagerWeakTensorSymbolGraphWeakTensorr   InvalidArgumentErrortype)clsr   s     r   from_tensorzWeakTensor.from_tensor   sd     &$**%V$$&$++&V$$

%
%	V~[	* r   c                 .    | j                   j                  S r   )r   r%   r(   s    r   r%   zWeakTensor.dtype       ;;r   c                 .    | j                   j                  S r   )r   rE   r(   s    r   rE   zWeakTensor.shape   rq   r   c                      y)NTr    r(   s    r   is_tensor_likezWeakTensor.is_tensor_like   s    r   )NN)$r   r   r   r   
tensor_libTensor__annotations__r)   r/   r1   r.   r=   rA   rC   rJ   rP   rR   rU   rW   rZ   r   r   DTypestrr]   r`   rb   re   classmethodro   propertyr%   rE   rt   r   __composite_gradient__r    r   r   r"   r"   3   s    
 (


32B1

@0"!" IM=FLL)=8@=

  2       ./r   r"   c                   @    e Zd ZdZdZ d Zd Zd Zd Zd Zd Z	dd
Z
y	)ri   zA weakly typed Eager Tensor.ztf.EagerWeakTensorc                     t        | j                  t        j                        st	        d      | j                  j                         S )zJCopy of the contents of this EagerWeakTensor into a NumPy array or scalar.z3WeakTensor.numpy() is only supported in eager mode.)rg   r   r	   EagerTensor
ValueErrornumpyr(   s    r   r   zEagerWeakTensor.numpy   s3    dkk3??3LMM;;r   c                 6    | j                   j                         S r   )r   __complex__r(   s    r   r   zEagerWeakTensor.__complex__   s    ;;""$$r   c                 6    | j                   j                         S r   )r   __int__r(   s    r   r   zEagerWeakTensor.__int__   rX   r   c                 6    | j                   j                         S r   )r   	__float__r(   s    r   r   zEagerWeakTensor.__float__       ;;  ""r   c                 6    | j                   j                         S r   )r   	__index__r(   s    r   r   zEagerWeakTensor.__index__   r   r   c                 >    | j                   j                  |       dS )Nz weakly typed)r   
__format__)r   format_specs     r   r   zEagerWeakTensor.__format__   s    kk$$[12-@@r   Nc                 ^    t        j                  | j                  j                  |            S r   )nparrayr   	__array__)r   r%   s     r   r   zEagerWeakTensor.__array__   s"    
 88DKK))%011r   r   )r   r   r   r   r   r   r   r   r   r   r   r    r   r   ri   ri      s.    $!(%!##A2r   ri   c                       e Zd ZdZdZ y)rk   zA weakly typed Graph Tensor.ztf.GraphWeakTensorN)r   r   r   r   r    r   r   rk   rk      s
    $!(r   rk   c                   *    e Zd ZdZg dZd Zd Zd Zy)rN   zHIterates over the leading dim of a WeakTensor. Performs no error checks._weak_tensor_index_limitc                 .    || _         d| _        || _        y )Nr   r   )r   r   dim0s      r   __init__z_WeakTensorIterator.__init__   s    #DDKDKr   c                     | S r   r    r(   s    r   rP   z_WeakTensorIterator.__iter__   s    Kr   c                     | j                   | j                  k(  rt        t        j	                  | j
                  j                  | j                            }| xj                   dz  c_         |S )Nr3   )r   r   StopIterationr"   ro   r   r   )r   results     r   __next__z_WeakTensorIterator.__next__   sP    {{dkk!##T%6%6%=%=dkk%JLFKK1KMr   N)r   r   r   r   	__slots__r   rP   r   r    r   r   rN   rN      s    P2)
r   rN   c                 l    |rt         j                  |       S t        | t               r| j                  S | S r   )r"   ro   rg   r   )tto_weaks     r    convert_to_weak_tensor_or_tensorr      s.    !!!$$:88O	
(r   c                 <    t        | t              r| j                  S y r   )rg   r"   r   )r   s    r   weak_tensor_conversion_functionr     s    :88O r   )&r   typingr   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   r	   r   ru   r   tensorflow.python.typesr   int32int64float32float64
complex128r&   CompositeTensorGradientr   BatchableExtensionTyperv   r"   rh   ri   rj   rk   objectrN   r   r   #register_tensor_conversion_functionr    r   r   <module>r      s    4   + A . . 6 + < B ( LL
LL
NN
NN
 F2JJ FI066 I0^ 2djj*  2F"dkk: "& *
 ?  > >/r   