o
    "j                     @  sz   d dl mZ d dlmZmZmZ d dlZddlmZ ddl	m
Z
mZmZmZ er6d dlmZ edZed	ZdddZdS )    )annotations)TYPE_CHECKINGCallableTypeVarN   eval_frame_callback)GraphLoggerStepInfoManager	StepStatelog_do)	ParamSpecPRfnCallable[P, R]returnc                   sL    dd fdd d fdddfdddfdd}|S )aa  
    This function is the entry point of PaddleSOT. It sets eval_frame_callback before input
    function to achieve Opcode-level translation. The translation process depends on the
    simulation execution, in which information will be collected, especially the network
    code. After the simulation execution is completed, the network code will be compiled
    into a static graph Program to improve performance.

    Args:
        fn: The input function.

    Returns:
        Callable, The wrapped function.

    Examples:
        >>> # doctest: +SKIP("Cound not get source code of function foo."")
        >>> import paddle
        >>> import numpy as np
        >>> from sot.translate import symbolic_translate
        >>> def foo(cond: paddle.Tensor, x: paddle.Tensor):
        ...     x += 1
        ...     if cond:
        ...         x += 1
        ...     else:
        ...         x -= 1
        ...     return x
        >>> symbolic_translate_foo = symbolic_translate(foo)
        >>> # For the true branch, the output is 2.
        >>> cond = paddle.to_tensor(True)
        >>> x = paddle.to_tensor(0)
        >>> dygraph_out = foo(cond, x)
        >>> symbolic_translate_out = symbolic_translate_foo(cond, x)
        >>> dygraph_out
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        2)
        >>> symbolic_translate_out
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        2)
        >>> np.testing.assert_allclose(
        ...     dygraph_out.numpy(), symbolic_translate_out.numpy()
        ... )
        >>> # For the false branch, the output is 0.
        >>> cond = paddle.to_tensor(False)
        >>> dygraph_out = foo(cond, x)
        >>> symbolic_translate_out = symbolic_translate_foo(cond, x)
        >>> dygraph_out
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        0)
        >>> symbolic_translate_out
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        0)
        >>> np.testing.assert_allclose(
        ...     dygraph_out.numpy(), symbolic_translate_out.numpy()
        ... )

    ZtrainingTc                   s   t | fi  S Nr   )frame)kwargs Y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/sot/translate.pycallbackZ   s   z$symbolic_translate.<locals>.callbackargsP.argsr   P.kwargsr   r   c               
     s   t ds	J dt   t   tjj  z!z	| i |}W n t	y2 } z|d }~ww W tjjd  ntjjd  w t
ddd  |S )N__code__z1Target function doesn't have code for simulating.r   c                   S  s
   t   S r   )r	   Z
print_infor   r   r   r   <lambda>k   s   
 z6symbolic_translate.<locals>.impl_sot.<locals>.<lambda>)hasattrr
   Zsot_stepr	   clearpaddleZ	frameworkcoreZset_eval_frame	Exceptionr   )r   r   outse)r   r   r   r   impl_sot]   s$   

 z$symbolic_translate.<locals>.impl_sotc                    s    | i |}|S r   r   )r   r   r#   )r   r   r   impl_dynamicn   s   z(symbolic_translate.<locals>.impl_dynamicc                    s   t   jM t  j}|tjkr| i |W  d    S |tjkr2| i |W  d    S |tjkrLt  jg| R i |W  d    S W d    d S 1 sWw   Y  d S r   )	r
   Z
step_guardr   Zcurrent_stater   ZRUN_SOTZRUN_DYNZCOLLECT_INFOZcollect_info)r   r   state)r   r&   r%   r   r   implr   s&   


"z symbolic_translate.<locals>.implN)r   r   r   r   r   r   )
setdefault)r   r   r(   r   )r   r   r&   r%   r   r   symbolic_translate   s   9r*   )r   r   r   r   )
__future__r   typingr   r   r   r    Zopcode_translatorr   utilsr	   r
   r   r   Ztyping_extensionsr   r   r   r*   r   r   r   r   <module>   s   