o
    Ơi+&                     @   s  U 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	Z
d dlmZ d dlmZ e
jeef Ze
jee
jg ef f ZedZed ed< edZed ed< edZee ed< ed	Zee ed	< eeZeG d
d dZe
jegef Z 	d+dddefddZ!dd Z"dd Z#dedefddZ$d,ddZ%d-ddZ&d.d d!Z'G d"d dZ(G d#d$ d$Z)G d%d dZ*G d&d' d'e*Z+d(ede
j,eef fd)d*Z-dS )/    N)
ContextVar)	dataclass_CURRENT_WRITERDiagnosticsWriter_CURRENT_COLLECTORDiagnosticsCollector!_SUBSEQUENT_COLLECT_SUPPRESSED_BY_IS_IN_COLLECT_CONTEXTc                   @   s   e Zd ZU eje ed< dS )CollectionConditionContext	exceptionN)__name__
__module____qualname__tOptional	Exception__annotations__ r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/fx/diagnostics.pyr
       s   
 r
   T	conditionCollectionConditionsupress_subsequent_collectc                 C      t  | |S )z'See `DiagnosticsCollector.collect_when`)get_current_collectorcollect_when)r   r   r   r   r   r   (   s   r   c                   C      t t S N)r   CollectionConditionsalwaysr   r   r   r   collect/      r   c                   C   r   r   )r   r   	when_failr   r   r   r   collect_when_fail3   r    r"   	file_nametextc                 C   r   r   )get_current_writerwrite)r#   r$   r   r   r   r&   7      r&   returnc                  C   s"   t d} | st } t |  | S )zrGet the writer for current execution context.

    Lazily instantiates and registers one if not already done.
    N)r   getr   set)current_writerr   r   r   r%   ;   s
   

r%   c                  C   s"   t d } | st } t |  | S r   )r   r)   r   r*   )current_collectorr   r   r   r   G   s
   

r   	collectorc                 C   s   t |  d S r   )r   r*   )r-   r   r   r   set_current_collectorO   r'   r.   c                   @   s^   e Zd ZU eed< dd ZdedefddZdefd	d
ZdefddZ	dede
fddZdS )r   	_root_dirc                 C   s*   t jdd| _d| _td| j  d S )Nzfx2trt.)prefix z.Initializing DiagnosticsWriter with root_dir: )tempfilemkdtempr/   _data_LOGGERinfoselfr   r   r   __init__W   s   zDiagnosticsWriter.__init__r#   datac              
   C   s   || _ tds
dS z3t|\}}|r|d}nt|tr$|d}nt|tr,|}n	tdt	| | 
|| W dS  tyX } ztd|  W Y d}~dS d}~ww )z=
        TODO: Can be disabled by regex on file_name
        FNzutf-8zUnknown data type: zError writing diagnostics: )r4   r	   r)   _res_or_errencode
isinstancestrbytes	TypeErrortype_writer   r5   warning)r8   r#   r:   reserrto_writeer   r   r   r&   \   s"   


zDiagnosticsWriter.writer(   c                 C      | j S r   )r/   r7   r   r   r   root_dirv      zDiagnosticsWriter.root_dirc                 C   rH   r   )r4   r7   r   r   r   r:   y   rJ   zDiagnosticsWriter.datarF   c                 C   sj   t t d }| d| }tj|  |}t|d}|| W d    d S 1 s.w   Y  d S )Ni  .wb)inttimeospathjoinrI   openr&   )r8   r#   rF   tsfnfr   r   r   rB   |   s   "zDiagnosticsWriter._writeN)r   r   r   r>   r   r9   WriteObjr&   rI   r:   r?   rB   r   r   r   r   r   S   s   
 c                   @   s   e Zd ZedddZedddZedd	d
ZedddZedddZedddZ	e	dde
deddfddZedefddZdS )r   
conditionsr   r(   c                        fddS )Nc                       t  fddD S )Nc                 3       | ]}| V  qd S r   r   .0condctxr   r   	<genexpr>       z=CollectionConditions.any.<locals>.<lambda>.<locals>.<genexpr>)anyr^   rW   r^   r   <lambda>       z*CollectionConditions.any.<locals>.<lambda>r   clsrW   r   rc   r   rb         zCollectionConditions.anyc                    rX   )Nc                    rY   )Nc                 3   rZ   r   r   r[   r^   r   r   r`      ra   z=CollectionConditions.all.<locals>.<lambda>.<locals>.<genexpr>)allr^   rc   r^   r   rd      re   z*CollectionConditions.all.<locals>.<lambda>r   rf   r   rc   r   ri      rh   zCollectionConditions.allr   c                    rX   )Nc                    s
    |  S r   r   r^   r   r   r   rd         
 z+CollectionConditions.not_.<locals>.<lambda>r   )rg   r   r   rj   r   not_   rh   zCollectionConditions.not_c                 C      dd S )zAlways collectc                 S      dS )NTr   r^   r   r   r   rd          z-CollectionConditions.always.<locals>.<lambda>r   rg   r   r   r   r         zCollectionConditions.alwaysc                 C   rm   )zNever collectc                 S   rn   )NFr   r^   r   r   r   rd      ro   z,CollectionConditions.never.<locals>.<lambda>r   rp   r   r   r   never   rq   zCollectionConditions.neverc                 C   rm   )zCollect when failedc                 S   s
   | j d uS r   )r   r^   r   r   r   rd      rk   z0CollectionConditions.when_fail.<locals>.<lambda>r   rp   r   r   r   r!      s   zCollectionConditions.when_failF	func_namematch_prefixc                    s   dt dtf fdd}|S )Nr_   r(   c                    sB   t  }|D ]}r|d  r dS q|d  kr dS qdS )N   TF)inspectstack
startswith)r_   framesframers   rt   r   r   _when_called_by_function   s   zNCollectionConditions.when_called_by_function.<locals>._when_called_by_function)r
   bool)rg   rs   rt   r|   r   r{   r   when_called_by_function   s   z,CollectionConditions.when_called_by_functionc                 C   s   t t jdddS )Ntest_T)rt   )r   rl   r~   rp   r   r   r   when_not_in_tests   s   z&CollectionConditions.when_not_in_testsN)rW   r   r(   r   )r   r   r(   r   )r(   r   )F)r   r   r   classmethodrb   ri   rl   r   rr   r!   r>   r}   r~   r   r   r   r   r   r   r      s0    r   c                   @   sP   e Zd Zej	ddddefddZdefdd	Ze	d
e
dedefddZdS )r   Tr   r   r   c           	      c   sl   t  }td}d}|r!|r||krt }n	|}t| d}td}d}zRzdV  W n ty= } z|} d}~ww W |rFtd | |t	|rtz| 
  W n tys } ztd| dt   W Y d}~nd}~ww t| dS |rtd | |t	|rz| 
  W n ty } ztd| dt   W Y d}~nd}~ww t| w )aU  
        Context manager to collect diagnostics when the enclosed code completes
        and *any* of the given condition is met.

        Args:
            condition:
                the condition only when met should the collection be done
            supress_subsequent_collect:
                When true, suppress any collections registered by this function
                call. This is to ensure duplicate collections registered across
                the callstack by different components. In this case, only the
                outermost component will collect.

                When false, always collect (subject to given condition) regardless
                of earlier collection registration's suppression.

        Returns:
            a context manager that handles the collection when its enclosed
            code finished run.
        NFTz@Error while collecting diagnostics (THIS EXCEPTION IS HANDLED):

)objectr   r)   r   rr   r*   r	   r   _test_conditionr
   r   r5   rC   	traceback
format_excreset)	r8   r   r   this_collection_handlesuppressed_byreset_suppressed_byis_in_collect_context_tokr   rG   r   r   r   r      sh   






z!DiagnosticsCollector.collect_whenr(   c                 C   rn   )z4Collect the diagnostics. Overridable in sub-classes.r1   r   r7   r   r   r   r      s   zDiagnosticsCollector.collectr]   r_   c              
   C   sB   z||W S  t y  } ztd|  W Y d }~dS d }~ww )NzError while testing condition: F)r   r5   rC   )rg   r]   r_   rG   r   r   r   r      s   
z$DiagnosticsCollector._test_conditionNT)r   r   r   
contextlibcontextmanagerr}   r   r>   r   r   r   r
   r   r   r   r   r   r      s"    :c                   @   sL   e Zd ZU eed< dZeed< defddZdefdd	Zde	fd
dZ
dS )ZipDiagnosticsCollectorrB   r1   _last_zip_path_for_testwriterc                 C   s
   || _ d S r   )rB   )r8   r   r   r   r   r9   
     
z ZipDiagnosticsCollector.__init__r(   c              	   C   sD   t  \}}zt|d| j }|| _|W t| S t| w )Nzip)	r2   mkstempshutilmake_archiverB   rI   r   rO   remove)r8   _fpzip_pathr   r   r   r     s   zZipDiagnosticsCollector.collectc                 C   s
   | j  S r   )rB   r:   r7   r   r   r   r:     r   zZipDiagnosticsCollector.dataN)r   r   r   r   r   r   r>   r9   r   rV   r:   r   r   r   r   r     s   
 	r   r:   c              
   C   s~   t | ttfr| dfS t| stdt|  z|  dfW S  ty> } ztd|  dt|fW  Y d }~S d }~ww )Nr1   zBdata must be a callable that returns actual data towrite, but got zError getting data to write: )	r=   r>   r?   callabler@   rA   r   r5   rC   )r:   rG   r   r   r   r;     s   r;   r   )r(   r   )r(   r   )r-   r   ).r   rv   loggingrO   os.pathr   r2   rN   r   typingr   contextvarsr   dataclassesr   Unionr>   r?   TWriteCallablerV   r   r   r   r   r   r	   r}   	getLoggerr   r5   r
   r   r   r   r"   r&   r%   r   r.   r   r   r   r   Tupler;   r   r   r   r   <module>   sV   
 




35K 