
    BVh.              
       l   U d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ dZdZg Z G d	 d
e      Ze edd      e edd      iZe edd      e edd      iZ G d de      Z e       Zeeef   ed<   dedee   fdZedfdedededee   fdZdee   dee   dee   fd Zdedee   fd!Zdedee   fd"Zd#edee   fd$Z d#edee   fd%Z! e
d&      Z" G d' d(e#      Z$d)edefd*Z% G d+ d,e	      Z& ejN                  e$e-      Z(e&ed.<    ejN                  e$e-      Z)e&ed/<   y)0a  Utilities for exporting TensorFlow symbols to the API.

Exporting a function or a class:

To export a function or a class use tf_export decorator. For e.g.:
```python
@tf_export('foo', 'bar.foo')
def foo(...):
  ...
```

If a function is assigned to a variable, you can export it by calling
tf_export explicitly. For e.g.:
```python
foo = get_foo(...)
tf_export('foo', 'bar.foo')(foo)
```


Exporting a constant
```python
foo = 1
tf_export('consts.foo').export_constant(__name__, 'foo')
```
    )SequenceN)Any
NamedTupleOptionalProtocolTypeVar)tf_decorator)
tf_inspectkeras
tensorflowc                   "    e Zd ZU eed<   eed<   y)_Attributesnames	constantsN)__name__
__module____qualname__str__annotations__     P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/util/tf_export.pyr   r   8   s    	*.r   r   _tf_api_names_tf_api_constants_keras_api_names_keras_api_constants_tf_api_names_v1_tf_api_constants_v1_keras_api_names_v1_keras_api_constants_v1c                       e Zd ZdZy)InvalidSymbolNameErrorzDRaised when trying to export symbol as an invalid or unallowed name.N)r   r   r   __doc__r   r   r   r"   r"   M   s    Lr   r"   _NAME_TO_SYMBOL_MAPPINGnamereturnc                 ,    t         j                  |       S N)r$   get)r%   s    r   get_symbol_from_namer*   T   s    	 	$	$T	**r   Fsymbolapi_nameadd_prefix_to_v1_namesc                 h   t        | d      syt        |   j                  }t        j                  |       \  }}||j
                  vryt        ||      }|j
                  j                  dg       }t        ||      }|r|S t        |   j                  }t        ||      }t        ||      }	|rd|	z  S |	S )a  Get canonical name for the API symbol.

  Example:
  ```python
  from tensorflow.python.util import tf_export
  cls = tf_export.get_symbol_from_name('keras.optimizers.Adam')

  # Gives `<class 'keras.optimizer_v2.adam.Adam'>`
  print(cls)

  # Gives `keras.optimizers.Adam`
  print(tf_export.get_canonical_name_for_symbol(cls, api_name='keras'))
  ```

  Args:
    symbol: API function or class.
    api_name: API name. Currently, only `tensorflow`.
    add_prefix_to_v1_names: Specifies whether a name available only in V1 should
      be prefixed with compat.v1.

  Returns:
    Canonical name for the API symbol (for e.g. initializers.zeros) if
    canonical name could be determined. Otherwise, returns None.
  __dict__N_tf_deprecated_api_namescompat.v1.%s)
hasattr	API_ATTRSr   r	   unwrapr/   getattrr)   get_canonical_nameAPI_ATTRS_V1)
r+   r,   r-   api_names_attr_undecorated_symbol	api_namesdeprecated_api_namescanonical_namev1_canonical_names
             r   get_canonical_name_for_symbolr?   X   s    : 
	$X&,,.&--f5!-666(.9)+4488 " &i1EF.  )//.(.9)(4HI---	r   r;   r<   c                 F    t        fd| D        d      }|r|S | r| d   S y)a  Get preferred endpoint name.

  Args:
    api_names: API names iterable.
    deprecated_api_names: Deprecated API names iterable.

  Returns:
    Returns one of the following in decreasing preference:
    - first non-deprecated endpoint
    - first endpoint
    - None
  c              3   ,   K   | ]  }|vs|  y wr(   r   ).0r%   r<   s     r   	<genexpr>z%get_canonical_name.<locals>.<genexpr>   s     FT1E%EtFs   	Nr   )next)r;   r<   non_deprecated_names    ` r   r6   r6      s5     F	F Q<	r   c                 &   g }t         t           j                  }t         t           j                  }t	        | d      s|S || j
                  v r|j                  t        | |             || j
                  v r|j                  t        | |             |S )zGet a list of TF 1.* names for this symbol.

  Args:
    symbol: symbol to get API names for.

  Returns:
    List of all API names for this symbol.
  r/   )r7   TENSORFLOW_API_NAMEr   KERAS_API_NAMEr2   r/   extendr5   )r+   names_v1tensorflow_api_attr_v1keras_api_attr_v1s       r   get_v1_namesrM      s     ('(;<BB">288		$Ov.OOGF$:;<&//)OOGF$567	/r   c                 &   g }t         t           j                  }t         t           j                  }t	        | d      s|S || j
                  v r|j                  t        | |             || j
                  v r|j                  t        | |             |S )zGet a list of TF 2.0 names for this symbol.

  Args:
    symbol: symbol to get API names for.

  Returns:
    List of all API names for this symbol.
  r/   )r3   rG   r   rH   r2   r/   rI   r5   )r+   names_v2tensorflow_api_attrkeras_api_attrs       r   get_v2_namesrR      s~     (!"56<<^,22.		$OFOO+OOGF$789v&OOGFN34	/r   modulec                     g }t         t           j                  }t        | |      r|j	                  t        | |             |S )zGet a list of TF 1.* constants in this module.

  Args:
    module: TensorFlow module.

  Returns:
    List of all API constants under the given module.
  )r7   rG   r   r2   rI   r5   )rS   constants_v1tensorflow_constants_attr_v1s      r   get_v1_constantsrW      sB     ,!-.A!B!L!LV12(DEF	r   c                     g }t         t           j                  }t        | |      r|j	                  t        | |             |S )zGet a list of TF 2.0 constants in this module.

  Args:
    module: TensorFlow module.

  Returns:
    List of all API constants under the given module.
  )r3   rG   r   r2   rI   r5   )rS   constants_v2tensorflow_constants_attrs      r   get_v2_constantsr[      sB     ,'(;<FFV./(ABC	r   Tc            	           e Zd ZU dZee   ed<   ee   ed<   eed<   edddded	ed
eee      de	fdZ
ddZdedefdZdededee   ddfdZdededdfdZy)
api_exportz6Provides ways to export symbols to the TensorFlow API._names	_names_v1	_api_nameNT)r,   v1allow_multiple_exportsargsr,   rb   rc   c                V    || _         ||n|| _        || _        | j                          y)an  Export under the names *args (first one is considered canonical).

    Args:
      *args: API names in dot delimited format.
      api_name: API you want to generate Currently, only `tensorflow`.
      v1: Names for the TensorFlow V1 API. If not set, we will use V2 API names
        both for TensorFlow V1 and V2 APIs.
      allow_multiple_exports: Deprecated.
    N)r_   r`   ra   _validate_symbol_names)selfr,   rb   rc   rd   s        r   __init__zapi_export.__init__   s+      DK>RtDNDN!r   r&   c                     t         j                        t         j                        z  } j                  t        k(  r.t
        D ]$  t        fd|D              st        dz         yt         fd|D              st        d      y)a  Validate you are exporting symbols under an allowed package.

    We need to ensure things exported by tf_export, etc.
    export symbols under disjoint top-level package names.

    For TensorFlow, we check that it does not export anything under subpackage
    names used by components (keras, etc.).

    For each component, we check that it exports everything under its own
    subpackage.

    Raises:
      InvalidSymbolNameError: If you try to export symbol under disallowed name.
    c              3   @   K   | ]  }|j                          y wr(   )
startswith)rB   n
subpackages     r   rC   z4api_export._validate_symbol_names.<locals>.<genexpr>&  s     BAq||J'Bs   z6@tf_export is not allowed to export symbols under %s.*c              3   T   K   | ]  }|j                  j                         ! y wr(   )rk   ra   )rB   rl   rg   s     r   rC   z4api_export._validate_symbol_names.<locals>.<genexpr>,  s     H!dnn-Hs   %(z8Can only export symbols under package name of component.N)	setr_   r`   ra   rG   SUBPACKAGE_NAMESPACESanyr"   all)rg   all_symbol_namesrm   s   ` @r   rf   z!api_export._validate_symbol_names  s     4;;'#dnn*==~~,,- *B1ABB&F  H7GHH$F
 	
 Ir   funcc                    t         | j                     j                  }t        | j                     j                  }t	        j
                  |      \  }}| j                  ||| j                         | j                  ||| j                         | j                  D ]  }|t        |<    | j                  D ]  }|t        d|z  <    |S )zCalls this decorator.

    Args:
      func: decorated symbol (function or class).

    Returns:
      The input function with _tf_api_names attribute set.
    r1   )
r3   ra   r   r7   r	   r4   set_attrr_   r`   r$   )rg   rt   r8   api_names_attr_v1r9   undecorated_funcr%   name_v1s           r   __call__zapi_export.__call__1  s     t~~.44N$T^^4::&--d3AMM"NDKK@MM"$5t~~F +&*d#+>> ?:>nw67? Kr   r8   r   c                     t        |||       y r(   )setattr)rg   rt   r8   r   s       r   rv   zapi_export.set_attrH  s     D.%(r   module_namer%   c                    t         j                  |   }t        | j                     j                  }t
        | j                     j                  }t        ||      st        ||g        t        ||      j                  | j                  |f       t        ||      st        ||g        t        ||      j                  | j                  |f       y)a  Store export information for constants/string literals.

    Export information is stored in the module where constants/string literals
    are defined.

    e.g.
    ```python
    foo = 1
    bar = 2
    tf_export("consts.foo").export_constant(__name__, 'foo')
    tf_export("consts.bar").export_constant(__name__, 'bar')
    ```

    Args:
      module_name: (string) Name of the module to store constant at.
      name: (string) Current constant name.
    N)sysmodulesr3   ra   r   r7   r2   r|   r5   appendr_   r`   )rg   r}   r%   rS   api_constants_attrapi_constants_attr_v1s         r   export_constantzapi_export.export_constantM  s    $ [[%F"4>>2<<(8BB6-.f("-F&'..T/BC601f+R0F)*114>>42HIr   )r&   N)r   r   r   r#   r   r   r   rG   r   boolrh   rf   r\   rz   r   rv   r   r   r   r   r^   r^      s    >
3-c].
 *$(%)"" " 8C=
!	"
 #",
:1  .))'*)3;C=))
J JC JD Jr   r^   fc                 l     t        j                          fd}t        j                   |      S )z3A wrapper that throws away all non-kwarg arguments.c                  x    | r0t        dj                  j                  j                               di |S )Nz`{f} only takes keyword args (possible keys: {kwargs}). Please pass these args as kwargs instead.)r   kwargsr   )	TypeErrorformatr   rd   )rd   r   r   	f_argspecs     r   wrapperzkwarg_only.<locals>.wrapperq  sB    66<f

9>> 7= 7  ;v;r   )decorator_argspec)r
   getfullargspecr	   make_decorator)r   r   r   s   ` @r   
kwarg_onlyr   m  s/    ''*) 
	$	$Q9	MMr   c            	       8    e Zd Zddddedeee      dedefdZy)	
ExportTypeNT)rb   rc   v2rb   rc   r&   c                     y r(   r   )rg   rb   rc   r   s       r   rz   zExportType.__call__  s     r   )	r   r   r   r   r   r   r   r^   rz   r   r   r   r   r   ~  s>    
 %)%)	 8C=
! #	
 r   r   )r,   	tf_exportkeras_export)*r#   collections.abcr   	functoolsr   typingr   r   r   r   r   tensorflow.python.utilr	   r
   rH   rG   rp   r   r3   r7   	Exceptionr"   dictr$   r   r   r*   r   r?   r6   rM   rR   rW   r[   r\   objectr^   r   r   partialr   r   r   r   r   <module>r      s  2 %  
 ? ? / -"   *  _6IJK 24JK	 2 K8	MY M +/& c3h 0+s +x} + (#(222 !2 c]	2j}4<SMc]2 # , # ,S Xc] "S Xc] " CLsJ sJlN# N# N"  *	)),	:  -9,,j r   