o
    "j-                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZmZ d dlZd dlmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZmZmZ dd	lmZm Z  g Z!e Z"G d
d dZ#dd Z$e$ a%dddZ&ej'dddZ(dd Z)dd Z*dS )    )annotationsN)AnyCallable)Layer   )convert_enumerateconvert_lenconvert_printconvert_rangeconvert_zip)TranslatorLogger)CONVERSION_OPTIONSStaticFunctionconvert_to_staticunwrap_decorators)
is_builtinis_paddle_funcc                   @  s"   e Zd ZdZdddZdd ZdS )	ConversionOptionsa  
    A container for conversion flags of a function in dynamic-to-static.

    Attributes:
        not_convert(bool): An attribute indicates that the function won't be converted in dynamic-to-static.

    NOTE(liym27): More attributes and methods can be added in this class.
    Fc                 C  s
   || _ d S N)not_convert)selfr    r   g/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/dy2static/convert_call_func.py__init__@   s   
zConversionOptions.__init__c                 C  sB   t |r|j}t |rt|t|  d S tdt|  d S )NzLOnly support @not_to_static to type(function) or type(method), but received )	inspectismethod__func__
isfunctionsetattrr   translator_loggerwarntype)r   funcr   r   r   attachC   s   

zConversionOptions.attachN)F)__name__
__module____qualname____doc__r   r#   r   r   r   r   r   6   s    
	r   c                  C  sB   t tttttttg} zddl}| 	| W | S  t
y    Y | S w )z!
    Return builtin modules.
    r   N)copycollectionsr   loggingnumpyospdbresixappendImportError)modulesr/   r   r   r   builtin_modulesO   s"   
r3   r2   list[types.ModuleType]c                 C  s    | D ]}|t vrt | qdS )z0
    Adds modules that ignore transcription
    N)BUILTIN_LIKELY_MODULESr0   )r2   moduler   r   r   add_ignore_modulej   s
   
r7   returnlist[Callable[..., Any]]c                   s(   t  ddd fdd  | S )Nr8   list[tuple[str, Any]]c                 S  s$   zt | W S  ty   g  Y S w r   )r   
getmembers	Exceptionr6   r   r   r   _try_get_membersx   s
   z.get_module_functions.<locals>._try_get_membersc                   sZ   | v rg S  |  g }| D ]\}}t|r|| t|r*| | q|S r   )addcallabler0   r   ismoduleextend)r6   resultsZ_member_namemember_get_module_functionsr>   visitedr   r   rF   ~   s   


z3get_module_functions.<locals>._get_module_functions)r8   r:   )setr=   r   rE   r   get_module_functionst   s   
rI   c                 C  sz   dd t D }|D ]}| |u rtdd|  d  dS q	ddlm} |g}t| |v r.d	S t| r;tdd
|  dS d	S )zJ
    Checks whether the func is supported by dygraph to static graph.
    c                 S  s   g | ]}t |D ]}|qqS r   )rI   ).0r6   r"   r   r   r   
<listcomp>   s    z"is_unsupported.<locals>.<listcomp>   zWhitelist: z@ is part of built-in module and does not have to be transformed.Tr   )
SequentialFzKWhitelist: %s is part of Paddle module and does not have to be transformed.)r5   r   log	paddle.nnrM   r!   r   )r"   Zbuiltin_functionsZ
builtin_fnrM   ZPADDLE_NEED_CONVERT_APISr   r   r   is_unsupported   s.   

rP   c              	   C  s4  t dd|  d}d}t| \}} t| td}|dur'|jr't dd|  | S t| dr.tS t| dr5tS t| dr<t	S t| d	rCt
S t| d
rJtS t| sRt| rT| S t| rud}t dd|  d| j d d d|  d  | S t| r| jdkr| S zZt| }t }|j D ]3}t|r|| qt|trt|\}}|| qt|rt|j| jdtr||  q| |v rt| }t| dd}nt d|  | }W n ty   d}Y n ty   d}Y nw t | rzt| }t| dd}W n| ty   d}Y nqw t!| drxt"| j#rxt!| drXt| t$rXzt| j%\}}	|	j&| j'd< t|	}	|	(| | _%W n tt)fyT   |rPdn|}Y nw | }n(z| j#j*}
t|
}| }W n tt)fyw   |rsdn|}Y n	w t+d|  d|du rt d|  | S |durt,-||}|S )a9  
    Converts a function call which needs to be transformed to static function.

    Args:
        func (callable): A callable function or method to convert.

    Returns:
        Callable: A converted function.

    Examples:
        .. code-block:: python

            >>> # doctest: +SKIP('`paddle.jit.to_static` can not run in xdoctest')
            >>> import paddle
            >>> from paddle.jit.dy2static import Call

            >>> paddle.enable_static()
            >>> def dyfunc(x):
            ...     if paddle.mean(x) < 0:
            ...         x_v = x - 1
            ...     else:
            ...         x_v = x + 1
            ...     return x_v
            ...
            >>> new_func = Call(dyfunc)
            >>> x = paddle.tensor.manipulation.fill_constant(shape=[3, 3], value=0, dtype='float64')
            >>> x_v = new_func(x)

            >>> exe = paddle.static.Executor(paddle.CPUPlace())
            >>> out = exe.run(fetch_list=[x_v])
            >>> print(out[0])
            [[1. 1. 1.]
             [1. 1. 1.]
             [1. 1. 1.]]

    r   z$Convert callable object: convert %s.NrL   zG%s is not converted when it is decorated by 'paddle.jit.not_to_static'.lenziprange	enumerateprint   z

*z
Your function:`zk` doesn't support to transform to static function because it is a generator function, it will be run as-is.
z<lambda>__self__zr%s doesn't have to be transformed to static function because it has been transformed before, it will be run as-is.	__class__forwardz	Callable z# can not be transformed at present.zO%s doesn't have to be transformed to static function, and it will be run as-is.).r   rN   r   getattrr   r   r   r   r   r
   r   r	   rP   r   isgeneratorfunctionr    r$   r   unwraprH   __globals__valuesr?   
isinstancer   isclass__dict__getstaticmethodr   AttributeErrorOSErrorr   hasattrr@   rZ   r   r[   r   Z_original_funcs__get__	TypeError__call__NotImplementedError	functoolspartial)r"   Z	func_selfZconverted_call_optionsZnumber_of_starsZ	_origfuncZglobal_functionsfnZforward_funcZ	call_funcr   r   r   convert_call   s   %














rr   )r2   r4   )r8   r9   )+
__future__r   r)   r(   rm   r   r*   r,   r-   r.   typestypingr   r   r+   rO   r   Zconvert_operatorsr   r   r	   r
   r   Zlogging_utilsr   Zprogram_translatorr   r   r   r   utilsr   r   __all__r   r   r3   r5   r7   	lru_cacherI   rP   rr   r   r   r   r   <module>   s6   

'