B
    ѻd6                 @   sD  d Z ddlZddlZddlZddlmZ ddlmZ dZdZdZ	egZ
edd	d
gZe	eddeeddeeddiZe	eddeeddeeddiZG dd deZG dd deZe Zdd Ze	dfddZd d! Zd"d# Zd$d% Zd&d' Zd(d) ZG d*d+ d+eZd,d- Zejee	d.Zejeed.Z ejeed.Z!dS )/a  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')
```
    N)tf_decorator)
tf_inspect	estimatorkeras
tensorflowZExportedApiAttributesnames	constantsZ_tf_api_namesZ_tf_api_constantsZ_estimator_api_namesZ_estimator_api_constantsZ_keras_api_namesZ_keras_api_constantsZ_tf_api_names_v1Z_tf_api_constants_v1Z_estimator_api_names_v1Z_estimator_api_constants_v1Z_keras_api_names_v1Z_keras_api_constants_v1c               @   s   e Zd ZdZdS )SymbolAlreadyExposedErrorzBRaised when adding API names to symbol that already has API names.N)__name__
__module____qualname____doc__ r   r   R/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/util/tf_export.pyr	   T   s   r	   c               @   s   e Zd ZdZdS )InvalidSymbolNameErrorzDRaised when trying to export symbol as an invalid or unallowed name.N)r
   r   r   r   r   r   r   r   r   Y   s   r   c             C   s
   t | S )N)_NAME_TO_SYMBOL_MAPPINGget)namer   r   r   get_symbol_from_name`   s    r   Fc       
      C   s   t | dsdS t| j}t| \}}||jkr4dS t||}|jdg }t||}|r^|S t	| j}t||}t||}	|rd|	 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 (tensorflow or estimator).
    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__NZ_tf_deprecated_api_nameszcompat.v1.%s)
hasattr	API_ATTRSr   r   unwrapr   getattrr   get_canonical_nameAPI_ATTRS_V1)
symbolapi_nameZadd_prefix_to_v1_namesapi_names_attr_Zundecorated_symbol	api_namesdeprecated_api_namescanonical_nameZv1_canonical_namer   r   r   get_canonical_name_for_symbold   s$    







r#   c                s0   t  fdd| D d}|r |S | r,| d S dS )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   s   | ]}| kr|V  qd S )Nr   ).0r   )r!   r   r   	<genexpr>   s    z%get_canonical_name.<locals>.<genexpr>Nr   )next)r    r!   Znon_deprecated_namer   )r!   r   r      s    r   c             C   s   g }t t j}t t j}t t j}t| ds0|S || jkrJ|t| | || jkrd|t| | || jkr~|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 including TensorFlow and
    Estimator names.
  r   )	r   TENSORFLOW_API_NAMEr   ESTIMATOR_API_NAMEKERAS_API_NAMEr   r   extendr   )r   Znames_v1Ztensorflow_api_attr_v1Zestimator_api_attr_v1Zkeras_api_attr_v1r   r   r   get_v1_names   s    







r+   c             C   s   g }t t j}t t j}t t j}t| ds0|S || jkrJ|t| | || jkrd|t| | || jkr~|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 including TensorFlow and
    Estimator names.
  r   )	r   r'   r   r(   r)   r   r   r*   r   )r   Znames_v2Ztensorflow_api_attrZestimator_api_attrZkeras_api_attrr   r   r   get_v2_names   s    







r,   c             C   sP   g }t t j}t t j}t| |r2|t| | t| |rL|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 including TensorFlow and
    Estimator constants.
  )r   r'   r   r(   r   r*   r   )moduleZconstants_v1Ztensorflow_constants_attr_v1Zestimator_constants_attr_v1r   r   r   get_v1_constants   s    




r.   c             C   sP   g }t t j}t t j}t| |r2|t| | t| |rL|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 including TensorFlow and
    Estimator constants.
  )r   r'   r   r(   r   r*   r   )r-   Zconstants_v2Ztensorflow_constants_attrZestimator_constants_attrr   r   r   get_v2_constants   s    




r/   c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
api_exportz6Provides ways to export symbols to the TensorFlow API.c             O   sZ   || _ |d|| _d|kr$td|dt| _|dg | _|dd| _|   dS )	a  Export under the names *args (first one is considered canonical).

    Args:
      *args: API names in dot delimited format.
      **kwargs: Optional keyed arguments.
        v1: Names for the TensorFlow V1 API. If not set, we will use V2 API
          names both for TensorFlow V1 and V2 APIs.
        overrides: List of symbols that this is overriding
          (those overrided api exports will be removed). Note: passing overrides
          has no effect on exporting a constant.
        api_name: Name of the API you want to generate (e.g. `tensorflow` or
          `estimator`). Default is `tensorflow`.
        allow_multiple_exports: Allow symbol to be exported multiple time under
          different names.
    v1v2z{You passed a "v2" argument to tf_export. This is not what you want. Pass v2 names directly as positional arguments instead.r   	overridesZallow_multiple_exportsFN)	_namesr   	_names_v1
ValueErrorr'   	_api_name
_overrides_allow_multiple_exports_validate_symbol_names)selfargskwargsr   r   r   __init__  s    zapi_export.__init__c                sr   t  jt  jB } jtkrPxNtD ]&tfdd|D r$td q$W nt fdd|D sntddS )a  Validate you are exporting symbols under an allowed package.

    We need to ensure things exported by tf_export, estimator_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 (estimator, 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   s   | ]}|  V  qd S )N)
startswith)r$   n)
subpackager   r   r%   ;  s    z4api_export._validate_symbol_names.<locals>.<genexpr>z6@tf_export is not allowed to export symbols under %s.*c             3   s   | ]}|  jV  qd S )N)r?   r7   )r$   r@   )r;   r   r   r%   @  s    z}Can only export symbols under package name of component. e.g. tensorflow_estimator must export all symbols under tf.estimatorN)	setr4   r5   r7   r'   SUBPACKAGE_NAMESPACESanyr   all)r;   Zall_symbol_namesr   )r;   rA   r   r:   )  s    

z!api_export._validate_symbol_namesc       
      C   s   t | j j}t| j j}x0| jD ]&}t|\}}t|| t|| q W t|\}}| ||| j	 | ||| j
 x| j	D ]}|t|< qW x| j
D ]}	|td|	 < qW |S )a-  Calls this decorator.

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

    Returns:
      The input function with _tf_api_names attribute set.

    Raises:
      SymbolAlreadyExposedError: Raised when a symbol already has API names
        and kwarg `allow_multiple_exports` not set.
    zcompat.v1.%s)r   r7   r   r   r8   r   r   delattrset_attrr4   r5   r   )
r;   funcr   Zapi_names_attr_v1fr   Zundecorated_fZundecorated_funcr   Zname_v1r   r   r   __call__F  s    
zapi_export.__call__c             C   s8   ||j kr(| js(td|jt||f t||| d S )Nz#Symbol %s is already exposed as %s.)r   r9   r	   r
   r   setattr)r;   rH   r   r   r   r   r   rG   e  s    
zapi_export.set_attrc             C   s~   t j| }t| j j}t| j j}t||s8t||g  t||	| j
|f t||sdt||g  t||	| j|f dS )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modulesr   r7   r   r   r   rK   r   appendr4   r5   )r;   module_namer   r-   Zapi_constants_attrZapi_constants_attr_v1r   r   r   export_constantp  s    




zapi_export.export_constantN)	r
   r   r   r   r>   r:   rJ   rG   rP   r   r   r   r   r0   
  s   r0   c                s(   t   fdd}tj |dS )z3A wrapper that throws away all non-kwarg arguments.c                 s$   | rt dj jjd f |S )Nz`{f} only takes keyword args (possible keys: {kwargs}). Please pass these args as kwargs instead.)rI   r=   )	TypeErrorformatr
   r<   )r<   r=   )rI   	f_argspecr   r   wrapper  s
    zkwarg_only.<locals>.wrapper)Zdecorator_argspec)r   
getargspecr   make_decorator)rI   rT   r   )rI   rS   r   
kwarg_only  s    
rW   )r   )"r   collections	functoolsrL   tensorflow.python.utilr   r   r(   r)   r'   rC   
namedtupleZ_Attributesr   r   	Exceptionr	   r   dictr   r   r#   r   r+   r,   r.   r/   objectr0   rW   partial	tf_exportZestimator_exportZkeras_exportr   r   r   r   <module>'   s\   0 	