
    ϑi                        S SK Jr  S SKJrJr  S SKJr  S SKrSSKJ	r	  SSK
Jr  SSKJrJr  \" S	5      r\" S
5      rSS jrg)    )annotations)CallableTypeVar)	ParamSpecN   eval_frame_callback)SotStepProfilerGuard)InfoCollectorStepInfoManagerPRc                   ^ ^^ [         R                  R                  5       (       d  [        S5      eTR	                  SS5        U4S jmSUU 4S jjnU$ )am  
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("Could 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())

zxSOT is only supported when running in PIR mode. Please set the environment variable FLAGS_enable_pir_api=1 to enable it.trainingTc                   > [        U 40 TD6$ )Nr   )framekwargss    X/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/jit/sot/translate.pycallback$symbolic_translate.<locals>.callback_   s    "53F33    c                   > [        TS5      (       d   S5       e[        5       R                  TR                  5         [	        5          [        5       R                  5         [        R                  R                  R                  T5         T" U 0 UD6n [        R                  R                  R                  S 5        [        5       R                  5         UsS S S 5        sS S S 5        $ ! [         a  nUeS nAff = f! [        R                  R                  R                  S 5        f = f! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)N__code__z1Target function doesn't have code for simulating.)hasattrr   
step_guardr   r
   r   clear_step_infopaddle	frameworkcoreset_eval_frame	Exceptionprint_step_report)argsr   outser   fns       r   impl symbolic_translate.<locals>.implb   s    r:&& 	
?	
& ))"++68L8NO++-!!00:;4*6*   %%44T:O--/ 9O8N66
     %%44T: 9O8N666sU   EAD+C)AD+	E)
C:3C55C::C==+D((D++
D9	5E
E)r#   zP.argsr   zP.kwargsreturnr   )r   r   use_pir_apiRuntimeError
setdefault)r&   r   r'   r   s   `` @r   symbolic_translater-   "   sU    j ''))3
 	

 j$'4 " Kr   )r&   Callable[P, R]r)   r.   )
__future__r   typingr   r   typing_extensionsr   r   opcode_translatorr	   profilerr
   utilsr   r   r   r   r-    r   r   <module>r6      s8    # $ '  2 *
 cNCLQr   