o
    "Õjñ  ã                   @  sZ   d dl mZ ddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZ G dd„ dƒZd	S )
é    )Úannotationsé   )Úlogé   )ÚCompileSIRCache)ÚApiStatementÚCallStatementÚLayerStatementÚMethodStatementÚStatementIRÚStatementIRFactoryÚSymbolc                   @  sv   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSymbolicTraceContextza
    SymbolicTraceContext is a context manager, which is used to record the symbolic trace.

    c                 C  s   |   ¡  d S ©N)Úreset©Úself© r   úi/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/sot/symbolic/symbolic_context.pyÚ__init__$   s   zSymbolicTraceContext.__init__c                 C  s   t ƒ | _| j ¡ g| _dS )z$
        Reset the context.
        N)r   Ústatement_factoryÚcreateÚ	sir_stackr   r   r   r   r   '   s   zSymbolicTraceContext.resetc                 C  s
   | j d S )zh
        The top SIR of sir_stack.

        Returns:
            StatementIR: the top of stack.
        éÿÿÿÿ)r   r   r   r   r   ÚTOS1   s   
	zSymbolicTraceContext.TOSc                 C  ó   t ||||ƒ}| j |¡ dS )z2
        Call a SIR, which is a subgraph.
        N)r   r   Úadd_statement)r   ZsirnameÚinputsÚoutputsÚstacksÚstmtr   r   r   Úcall_SIR<   s   zSymbolicTraceContext.call_SIRc                 C  s.   t |ƒsJ dƒ‚t||||ƒ}| j |¡ dS )z$
        Call a paddle api.
        z#call_API must receive a paddle api.N)Úcallabler   r   r   )r   Úapir   r   r   r    r   r   r   Úcall_APID   s   zSymbolicTraceContext.call_APIc                 C  sJ   t |tƒs	J dƒ‚t |d d tƒsJ dƒ‚t||||ƒ}| j |¡ dS )zN
        Call a method of a api. The API here can be python or Paddle
        z)call_METHOD must method api name. string.r   z8call_METHOD must first augument must be Symbol Variable.N)Ú
isinstanceÚstrr   r
   r   r   )r   Úmethod_namer   r   r   r    r   r   r   Úcall_METHODM   s   ÿþÿþz SymbolicTraceContext.call_METHODc                 C  r   )z(
        Call a layer of a api.
        N)r	   r   r   )r   Úlayerr   r   r   r    r   r   r   Ú
call_LAYERZ   s   zSymbolicTraceContext.call_LAYERÚnamer&   c                 C  s
   | j | S )zŸ
        Get a SIR from statement_factory.

        Args:
            name (str): the name of SIR.

        Returns:
            StatementIR: the SIR.
        )r   )r   r+   r   r   r   Úget_sira   s   

zSymbolicTraceContext.get_sirc                 C  s    | j  ¡  | j  | j ¡ ¡ dS )z 
        Reset the TOS.
        N)r   ÚpopÚappendr   r   r   r   r   r   Ú	reset_TOSm   s   
zSymbolicTraceContext.reset_TOSc                 C  s&   | j  ¡  | j  |¡ | j |¡ dS )zl
        Use deepcopyed sir to replace the TOS.
        This function will update statment_factory.
        N)r   r-   r.   r   Úupdate)r   Zsirr   r   r   Úreplace_TOSt   s   
z SymbolicTraceContext.replace_TOSc                 C  s$   dd„ }t dƒ}g |_g |_||fS )z¢
        Return a dummy function, which will return an empty list.

        Args:
            ret_vals (list[Symbol]): the return values of the function.
        c                  _  s   g S r   r   )ÚargsÚkwargsr   r   r   Ú
dummy_func…   s   z;SymbolicTraceContext.compile_do_nothing.<locals>.dummy_funcr4   )r   r   r   )r   Úret_valsr4   Zdummy_stmt_irr   r   r   Úcompile_do_nothing}   s
   z'SymbolicTraceContext.compile_do_nothingc                 K  sd   | j }t|jƒdkr|  |¡S | ¡ |_||_tddƒ td| j dƒ tƒ | |j	fi |¤Ž}||fS )zk
        start compile and return the python function, which must can be to_static without errors.
        r   r   z&start subgraph compile and execution.
Ú
)
r   ÚlenZ
statementsr6   Zanalyse_inputsr   r   r   r   r+   )r   r5   r3   Zcur_sirZstatic_funcr   r   r   Ú
compile_fnŽ   s   


zSymbolicTraceContext.compile_fnN)r+   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   r!   r$   r(   r*   r,   r/   r1   r6   r9   r   r   r   r   r      s    


	
	r   N)Ú
__future__r   Úutilsr   Zcompile_cacher   Zstatement_irr   r   r	   r
   r   r   r   r   r   r   r   r   Ú<module>   s
   $