o
    "j                     @  s   d dl mZ d dlZd dlmZ d dlZd dlmZ 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 d
dlmZ erLd
dlmZ dd Zdd ZG dd dZeG dd deZdS )    )annotationsN)TYPE_CHECKING)	amp_state)convert_dtype)_dygraph_tracer   )
EventGuard)CacheGraphLogger	SingletonStepInfoManagerloglog_domap_if   )compile_sir)SymbolicTraceContextc                  C  s>   t  } | jd ur| j} | j}tjj| | jd us	d S d S N)inspectcurrentframef_backf_codepaddleZ	frameworkcoreZsot_set_with_graph)framecode r   f/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/sot/symbolic/compile_cache.pytrace_back_frames)   s   
r   c                 C  s   | D ]}d|_ qd S )N )name)Zoutput_tensorsZoutput_tensorr   r   r   clear_eager_tensor_name1   s   r!   c                   @  s*   e Zd ZdZdddZdd Zdd	 Zd
S )FallbackWrapperzW
    Used to store and call static graph methods generated by paddle.jit.to_static
    is_trainingboolc                 C  s"   || _ d | _d | _|| _|| _d S r   )compiled_fnpartial_programconcrete_programSIRr#   )selfr%   r(   r#   r   r   r   __init__;   s
   
zFallbackWrapper.__init__c                   s   t  }|du r||fS t }|j s!|j s!|j s!||fS t|d  tdd  d t||f fdddd d	d d
S )z<Prepare inputs for amp, cast float16 into float32 if needed.Ndtype   z[AMP] Cast z into float32
c                   s   t | tjot| j kS r   )
isinstancer   ZTensorr   r+   xZ	amp_dtyper   r   <lambda>S   s    z1FallbackWrapper.amp_cast_inputs.<locals>.<lambda>c                 S  s   |  tjS r   )castr   Zfloat32r.   r   r   r   r1   U       c                 S  s   | S r   r   r.   r   r   r   r1   V   s    )predZtrue_fnZfalse_fn)	r   r   Z_expected_placeZis_gpu_placeZis_xpu_placeZis_custom_placer   r   r   )r)   argskwargsZcurrent_amp_stateZtracerr   r0   r   amp_cast_inputsB   s&   
zFallbackWrapper.amp_cast_inputsc              	     s8  t djj  t jrt  tdfdd  \ td fdd jd u rXt d j	j
 i \__jj_W d    n1 sSw   Y  t d jj i }W d    n1 spw   Y  t| td	fd
d tddd  |W  d    S 1 sw   Y  d S )NzFallbackWrapper: r   c                     s   t d jS )Nz"[FallbackWrapper] start run SIR: 
)printr(   r   r)   r   r   r1   `   r3   z*FallbackWrapper.__call__.<locals>.<lambda>   c                     s   t jj i d jS )Nr   )r8   r%   get_concrete_programZtrain_programr   r5   r6   r)   r   r   r1   e   s    z%FallbackWrapper: get_concrete_programz)FallbackWrapper: sot call partial_programr   c                     s   t   jjS r   )r
   Zadd_subgraphr'   Zmain_programr   r9   r   r   r1   x   s    c                   S  s   t dS )Nz'[CompileCache] run sir forward success.)r8   r   r   r   r   r1   ~   s    )r   r(   r    r   Zneed_back_tracer   r   r7   r&   r%   r;   r'   r#   trainingZsot_callr!   )r)   r5   r6   Zoutputsr   r<   r   __call__Y   sD   




$zFallbackWrapper.__call__N)r#   r$   )__name__
__module____qualname____doc__r*   r7   r>   r   r   r   r   r"   6   s
    
r"   c                      s4   e Zd ZdZ fddZddd	Zdd
dZ  ZS )CompileSIRCachez,
    Cache the compiled function of SIR
    c                   s   t  jdd d S )NF)Zweak)superr*   r9   	__class__r   r   r*      s   zCompileSIRCache.__init__contextr   sir_namestrc                 K  s"   | |}tt||d f}|S )a  
        generate a hash key for a SIR

        Args:
            context: The context to compile
            sir_name: The name of the sir to compile
            build_strategy: The build strategy to compile

        Returns:
            The hash key of the SIR
        r=   )get_sirhashrI   )r)   rG   rH   r6   ZsirZhash_keyr   r   r   key_fn   s   
zCompileSIRCache.key_fnc                 K  sF   | dd}| dd}ttjjt||||dd|||d dS )a  
        Generate static graph function

        Args:
            context: The context to compile
            sir_name: The name of the sir to compile
            build_strategy: The build strategy to compile

        Returns:
            The static graph function
        build_strategyNbackendT)rM   rN   Z
full_graphr=   )r#   )getr"   r   ZjitZ	to_staticr   rJ   )r)   rG   rH   r6   rM   rN   r   r   r   value_fn   s   zCompileSIRCache.value_fn)rG   r   rH   rI   )r?   r@   rA   rB   r*   rL   rP   __classcell__r   r   rE   r   rC      s
    
rC   )
__future__r   r   typingr   r   Zpaddle.amp.auto_castr   Zpaddle.base.data_feederr   Zpaddle.frameworkr   Zprofilerr   utilsr	   r
   r   r   r   r   r   interpreterr   Zsymbolic_contextr   r   r!   r"   rC   r   r   r   r   <module>   s"   $	M