o
    hL(                     @   s   d dl mZ d dlmZmZ d dlZd dlZd dlZd dlZd dl	Z
ejdd ZddddZdd	d
ddZdd ZG dd dZdd ZdS )    )TracebackType)ListOptionalNc                  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#    r0   L/var/www/html/ai/venv/lib/python3.10/site-packages/torch/utils/_traceback.pyreport_compile_source_on_error.   sX   

F
r2   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 )z
    Shorten a source filepath, under the assumption that anything under torch/
    directory is "obvious" and doesn't need to be shown to user.
    N   )ospathdirname__file__
commonpath
ValueErrorlen)fnr4   prefixr0   r0   r1   shorten_filename   s   r?   F)r4   linec                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  # r3   :z in )r@   r?   r)   linenor   )r,   r4   r@   
extra_liner0   r0   r1   format_frame   s   &rE   c                 C   s   t t| d S )zT
    Format a TracebackType in a short way, printing only the inner-most frame.
    )rE   	traceback
extract_tbr'   r0   r0   r1   format_traceback_short   s   rJ   c                   @   s`   e Zd ZddgZdddZ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   c                 C   s   || _ || _d S Nr'   rL   )selfr'   rL   r0   r0   r1   __init__   s   
zCapturedTraceback.__init__c                 C   s
   d | _ d S rM   rI   rO   r0   r0   r1   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'   rG   StackSummary_extract_symbolized_tb_C	_profilersymbolize_tracebacksrL   )rO   torchr0   r0   r1   summary   s   
zCapturedTraceback.summaryc                 C   s   d d | j dfS )NrN   )rL   rQ   r0   r0   r1   __getstate__   s   zCapturedTraceback.__getstate__F)scriptcpprL   c                 C   sL   ddl }| s|r|dksJ 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]   r5   )rS   rK   rV   rW   gather_traceback)r\   r]   rL   rY   r0   r0   r1   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
        )rG   format_listrZ   rQ   r0   r0   r1   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 }t||D ]\}}t	 | 
 ||< q9|S )z_
        Bulk version of CapturedTraceback.format.  Returns a list of list of strings.
        r   Nc                    s   g | ]} | j qS r0   rI   ).0itbsr0   r1   
<listcomp>   s    z0CapturedTraceback.format_all.<locals>.<listcomp>)rS   	enumerater'   r"   rV   rW   rX   ziprG   ra   rZ   )rf   rY   rsdelayed_idxsrd   r'   stbsstbr0   re   r1   
format_all   s   

zCapturedTraceback.format_allN)r   )__name__
__module____qualname__	__slots__rP   rR   rZ   r[   staticmethodr`   rb   rn   r0   r0   r0   r1   rK      s    

rK   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   )rG   rT   r$   r"   FrameSummary)r'   rL   r(   r+   r0   r0   r1   rU      s   "rU   )typesr   typingr   r   r   rG   
contextlibr   os.pathr6   contextmanagerr2   r?   rE   rJ   rK   rU   r0   r0   r0   r1   <module>   s    '
V
V