o
    i"(                     @   s   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 e jdd Z	ddddZ
ddd	d
efddZdd ZG dd dZdd ZdS )    N)TracebackTypec                  c   s\   zd V  W d S  t y }  z| j}g }|d ur|jjj}|jjd}|dkr|d urtjdddd}|	| W d    n1 sDw   Y  |j}t
d|jd}|j|jjd	}t|jd
rl|j|jj|jjd}t||ji |jdtji}td ||j|j}	||	 n|| |j}|d usd }
t|D ]}|
|_|}
q| |
d } ~ ww )N__compile_source__z<string>wFz.py)modedeletesuffixz__inspect_currentframe()eval)co_nameco_linetable)r
   co_firstlineno__inspect_currentframe)	Exception__traceback__tb_framef_codeco_filename	f_globalsgettempfileNamedTemporaryFilewritecompilenamereplacer	   hasattrr
   r   r   f_localsinspectcurrentframer   tb_lasti	tb_linenoappendtb_nextreversedwith_traceback)exctbstackfilenamesourcefframecode
fake_framefake_tbr!    r.   X/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/utils/_traceback.pyreport_compile_source_on_error0   sR   

C
r0   basec                C   s\   |du rt jt jt}z
t j| |g}W n ty#   |  Y S w | t|d d S )ziShorten a source filepath, with the assumption that torch/ subdirectories don't need to be shown to user.N   )ospathdirname__file__
commonpath
ValueErrorlen)fnr2   prefixr.   r.   r/   shorten_filename   s   r=   F)r2   linereturnc                C   s:   d}|r
| j  d}| t| j|d d| j d| j S )z
    Format a FrameSummary in a short way, without printing full absolute path or code.

    The idea is the result fits on a single line.
     z  # r1   :z in )r>   r=   r'   linenor   )r*   r2   r>   
extra_liner.   r.   r/   format_frame   s   &rD   c                 C   s   t t| d S )zJFormat a TracebackType in a short way, printing only the inner-most frame.)rD   	traceback
extract_tbr%   r.   r.   r/   format_traceback_short   s   rI   c                   @   sd   e Zd ZddgZddddZddd	Zd
d Zdd ZeddddddZ	dd Z
edd ZdS )CapturedTracebackr%   skipr   r?   Nc                 C   s   || _ || _d S Nr%   rK   )selfr%   rK   r.   r.   r/   __init__   s   
zCapturedTraceback.__init__c                 C   s
   d | _ d S rL   rH   rN   r.   r.   r/   cleanup   s   
zCapturedTraceback.cleanupc                 C   s8   dd l }| jd u rt S t|jj| jgd | jS )Nr   )	torch._C._profilerr%   rF   StackSummary_extract_symbolized_tb_C	_profilersymbolize_tracebacksrK   )rN   torchr.   r.   r/   summary   s   
zCapturedTraceback.summaryc                 C   s   d d | j dfS )NrM   )rK   rP   r.   r.   r/   __getstate__   s
   zCapturedTraceback.__getstate__F)scriptcpprK   c                 C   sL   ddl }| s|r|dkrtdt|jjjd| |d| s|r!dS |d S )a  
        Like traceback.extract_stack(), but faster (approximately 20x faster); it
        is fast enough that you can unconditionally log stacks this way as part of
        normal execution.  It returns a torch._C._profiler.CapturedTraceback
        object that must be formatted specially with format_captured_tb.

        By default, this only reports Python backtraces (like extract_stack).  You
        can set the script/cpp kwargs to also turn on TorchScript/C++ trace
        reporting.
        r   Nzskip with script/cpp NYIT)pythonr[   r\   r3   )rR   AssertionErrorrJ   rU   rV   gather_traceback)r[   r\   rK   rX   r.   r.   r/   extract   s   
zCapturedTraceback.extractc                 C   s   t |  S )al  
        Formats a single torch._C._profiler.CapturedTraceback into a list of
        strings equivalent to the output of traceback.format_list.  Note that if
        pass it CapturedTraceback with C++ traces,  it is better not to use this
        function and use the batch formatting API format_captured_tbs to amortize
        the cost of symbolization
        )rF   format_listrY   rP   r.   r.   r/   format   s   zCapturedTraceback.formatc                    s   ddl }g }g }t D ]\}}|jdu r|g  q|d || q|jj fdd|D  |D ]}t | 	 ||< q6|S )z_
        Bulk version of CapturedTraceback.format.  Returns a list of list of strings.
        r   Nc                    s   g | ]} | j qS r.   rH   ).0itbsr.   r/   
<listcomp>   s    z0CapturedTraceback.format_all.<locals>.<listcomp>)
rR   	enumerater%   r    rU   rV   rW   rF   ra   rY   )rf   rX   rsdelayed_idxsrd   r%   r.   re   r/   
format_all   s   

zCapturedTraceback.format_all)r   )r?   N)__name__
__module____qualname__	__slots__rO   rQ   rY   rZ   staticmethodr`   rb   rk   r.   r.   r.   r/   rJ      s    
	
rJ   c              	   C   sB   t  }t| |d D ]}|t |d |d |d  q|S )z
    Given a symbolized traceback from symbolize_tracebacks, return a StackSummary object of
    pre-processed stack trace entries.
    Nr'   r>   r   )rF   rS   r"   r    FrameSummary)r%   rK   r&   r)   r.   r.   r/   rT      s   "rT   )
contextlibr   os.pathr4   r   rF   typesr   contextmanagerr0   r=   strrD   rI   rJ   rT   r.   r.   r.   r/   <module>   s   )
TY