o
    i>f                     @  s  d dl m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m	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ d dlmZmZmZmZ d dlZd dlmZ d	d
lmZ d	dlmZ er{d dlZd dlm Z  d	dl!m"Z" d	dl#m$Z$ d	dlm%Z% dddZ&e'e(Z)ej*+e(dZ,G dd de-Z.G dd de.Z/G dd de.Z0G dd de.Z1G dd de1Z2G d d! d!e1Z3G d"d# d#e1Z4G d$d% d%e1Z5G d&d' d'e.Z6G d(d) d)e.Z7G d*d+ d+e.Z8G d,d- d-e.Z9G d.d/ d/e.Z:G d0d1 d1e:Z;G d2d3 d3e.Z<G d4d5 d5e<Z=G d6d7 d7e.Z>G d8d9 d9e<Z?G d:d; d;e<Z@G d<d= d=e<ZAG d>d? d?e<ZBG d@dA dAe?ZCG dBdC dCe	ZDG dDdE dEe<ZEG dFdG dGe.ZFG dHdI dIe<ZGG dJdK dKe.ZHG dLdM dMe.ZIG dNdO dOe.ZJG dPdQ dQeKZLG dRdS dSeKZMG dTdU dUe.ZNG dVdW dWe.ZOG dXdY dYeOZPG dZd[ d[eOZQG d\d] d]eQZRG d^d_ d_eQZSG d`da daeOZTG dbdc dceOZUG ddde deeOZVG dfdg dgeOZWG dhdi dieOZXeYePeZeQe[eRe\eTe]eSe^eUe-eVe_eWe`eXi	ZaddmdnZbdddoddwdxZcdd{d|ZdejejfjgejejfjhejejfjiejejfjjejejfjkfZldddZmdddZned	ddddZodddZpeq ZrerdddddZsG dd dZtddddZudddZvdddZw	ddddZxdddZydddZzdddZ{		ddddZ|dddZ}dddZ~dddZ		ddddZdS )    )annotationsN)autoEnum)	lru_cache)Path)extract_stack
format_excformat_listStackSummary)AnyNoReturnOptionalTYPE_CHECKING)get_file_path_2   )config)counters)	CompileId)DynamoTracerOutput)InstructionTranslatorBase)DynamoFrameType	case_namestrreturnc                 C  s   d|  dd S )NzhFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#_-)replace)r    r   S/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/_dynamo/exc.pyexportdb_error_message9   s   
r   graph_breaksc                         e Zd Zd fddZ  ZS )	TorchDynamoExceptionargsr   kwargsr   Nonec                   s   t  j|i | d | _d S N)super__init___torch_dynamo_tracer_outputselfr#   r$   	__class__r   r   r(   F   s   
zTorchDynamoException.__init__r#   r   r$   r   r   r%   __name__
__module____qualname__r(   __classcell__r   r   r,   r   r"   E       r"   c                   @     e Zd ZdS )InternalTorchDynamoErrorNr0   r1   r2   r   r   r   r   r6   K       r6   c                   @  r5   )ResumePrologueTracingErrorNr7   r   r   r   r   r9   O   r8   r9   c                      s.   e Zd ZU ded< ddd fd	d
Z  ZS )RestartAnalysisOptional[str]restart_reasonN)r<   r#   r   r   r%   c                  s   || _ t j|  d S r&   )r<   r'   r(   )r+   r<   r#   r,   r   r   r(   V   s   zRestartAnalysis.__init__)r#   r   r<   r;   r   r%   r0   r1   r2   __annotations__r(   r3   r   r   r,   r   r:   S   s   
 r:   c                   @  r5   )SpeculationRestartAnalysisNr7   r   r   r   r   r?   [   r8   r?   c                   @  r5   )UnspecializeRestartAnalysisNr7   r   r   r   r   r@   _   r8   r@   c                   @  r5   ) CompileCollectiveRestartAnalysisNr7   r   r   r   r   rA   c   r8   rA   c                   @  r5   )TensorifyScalarRestartAnalysisNr7   r   r   r   r   rB   g   r8   rB   c                   @  r5   )	SkipFrameNr7   r   r   r   r   rC   k   r8   rC   c                   @  r5   )TorchRuntimeErrorNr7   r   r   r   r   rD   o   r8   rD   c                      s   e Zd Zd fddZ  ZS )InvalidBackendnamer   r   r%   c                   s   t  d|d d S )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.)r'   r(   )r+   rF   r,   r   r   r(   t   s   
zInvalidBackend.__init__)rF   r   r   r%   r/   r   r   r,   r   rE   s   r4   rE   c                      s   e Zd Zd fddZ  ZS )ResetRequiredr   r%   c                   s   t  td d S )Nz
                Must call `torch._dynamo.reset()` before changing backends.  Detected two calls to
                `torch.compile()` with a different backend compiler arguments.
                )r'   r(   textwrapdedentr+   r,   r   r   r(   {   s
   zResetRequired.__init__r   r%   r/   r   r   r,   r   rG   z   r4   rG   c                      s(   e Zd Zd fdd	ZdddZ  ZS )ShortenTracebackr#   r   first_useful_frameOptional[types.FrameType]r$   r   r%   c                  s   t  j|i | || _d S r&   )r'   r(   rM   )r+   rM   r#   r$   r,   r   r   r(      s   
zShortenTraceback.__init__typing.Selfc                 C  sZ   | j }| jd u s|d u stjr| S |j| jur(|j}|d us"J d|j| jus| |S )Nz#internal error, please report a bug)__traceback__rM   r   verbosetb_frametb_nextwith_traceback)r+   tbr   r   r   remove_dynamo_frames   s   
z%ShortenTraceback.remove_dynamo_frames)r#   r   rM   rN   r$   r   r   r%   )r   rO   )r0   r1   r2   r(   rV   r3   r   r   r,   r   rL      s    rL   c                      s   e Zd Zd fd	d
Z  ZS )BackendCompilerFailed
backend_fnr   inner_exception	ExceptionrM   rN   r   r%   c                   sF   t |dd| _|| _d| jdt|j d| }t j||d d S )Nr0   ?zbackend=z	 raised:
z: )rM   )getattrbackend_namerY   typer0   r'   r(   )r+   rX   rY   rM   msgr,   r   r   r(      s   zBackendCompilerFailed.__init__)rX   r   rY   rZ   rM   rN   r   r%   r/   r   r   r,   r   rW      r4   rW   c                      s<   e Zd Zdddd fddZdddZddddZ  ZS )UnsupportedN)r   
real_stackr_   r   r   r;   ra   None | StackSummaryr   r%   c                  s@   t  | |stjj }|| _|| _d | _| 	  || _
d S r&   )r'   r(   torch_guardsTracingContextr   ra   r_   categoryadd_to_statsr   )r+   r_   r   ra   r,   r   r   r(      s   
zUnsupported.__init__c                 C  sP   | j d usJ t| j  | j  d8  < t| j  | j dkr&t| j  | j= d S d S )Nr   r   rf   r   r_   rJ   r   r   r   remove_from_stats   s
   zUnsupported.remove_from_statsunimplementedrf   c                 C  s    || _ t| | j  d7  < d S )Nr   rh   )r+   rf   r   r   r   rg      s   zUnsupported.add_to_stats)r_   r   r   r;   ra   rb   r   r%   rK   )rj   )rf   r   r   r%   )r0   r1   r2   r(   ri   rg   r3   r   r   r,   r   r`      s    
r`   c                   @  r5   )$UnknownPropertiesDuringBackwardTraceNr7   r   r   r   r   rk      r8   rk   c                   @  r5   )RecompileErrorNr7   r   r   r   r   rl      r8   rl   c                   @  r5   )ArgsMismatchErrorNr7   r   r   r   r   rm      r8   rm   c                   @  r5   )AttributeMutationErrorNr7   r   r   r   r   rn      r8   rn   c                   @  r5   )InfiniteGeneratorErrorNr7   r   r   r   r   ro          ro   c                   @  r5   )SideEffectsErrorNr7   r   r   r   r   rq      r8   rq   c                   @  s   e Zd ZdZdS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    N)r0   r1   r2   __doc__r   r   r   r   rr      s    rr   c                   @  s<   e Zd Ze Ze Ze Ze Ze Ze Z	e Z
e ZdS )UserErrorTypeN)r0   r1   r2   r   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAINT_VIOLATIONDYNAMIC_DIMINVALID_INPUTINVALID_OUTPUT*UNSUPPORTED_ALIASED_MUTATED_DYNAMIC_INPUTSr   r   r   r   rt      s    
rt   c                      s"   e Zd Z	dd fd
dZ  ZS )	UserErrorN
error_typert   r_   r   r   r;   r   r%   c                   sZ   |durt |tsJ |dr|d7 }n|d7 }|t|7 }t | || _|| _dS )aG  
        Type of errors that would be valid in Eager, but not supported in TorchDynamo.
        The error message should tell user about next actions.

        error_type: Type of user error
        msg: Actionable error message
        case_name: (Optional) Unique name (snake case) for the usage example in exportdb.
        N. 
)
isinstancer   endswithr   r'   r(   r~   message)r+   r~   r_   r   r,   r   r   r(      s   


zUserError.__init__r&   )r~   rt   r_   r   r   r;   r   r%   r/   r   r   r,   r   r}      s    r}   c                   @  r5   )SkipCodeRecursiveExceptionNr7   r   r   r   r   r     r8   r   c                   @  r5   )RecompileLimitExceededNr7   r   r   r   r   r     r8   r   c                   @  s   e Zd ZdddZdS )StepUnsupportedr   r%   c                 C  s   t jj | _d S r&   )rc   rd   re   r   ra   rJ   r   r   r   r(     s   zStepUnsupported.__init__NrK   )r0   r1   r2   r(   r   r   r   r   r     s    r   c                   @  r5   )UnsafeScriptObjectErrorNr7   r   r   r   r   r     r8   r   c                      s    e Zd Zd
d fdd	Z  ZS )UncapturedHigherOrderOpErrorNr_   r   ra   Optional[StackSummary]r   r%   c                   s6   t  | || _|d ur|| _d S tjj | _d S r&   )r'   r(   r_   rc   rd   re   r   ra   )r+   r_   ra   r,   r   r   r(     s   
z%UncapturedHigherOrderOpError.__init__r&   )r_   r   ra   r   r   r%   r/   r   r   r,   r   r     s    r   c                   @  r5   )IncorrectUsageNr7   r   r   r   r   r     r8   r   c                   @  r5   )FailOnRecompileLimitHitNr7   r   r   r   r   r   &  r8   r   c                   @  r5   )PackageErrorNr7   r   r   r   r   r   *  r8   r   c                      r!   )	ObservedExceptionr#   r   r$   r   r%   c                   s$   t  j|i | tjj | _d S r&   )r'   r(   rc   rd   re   r   ra   r*   r,   r   r   r(   0  s   zObservedException.__init__r.   r/   r   r   r,   r   r   .  s    r   c                      s(   e Zd ZU ded< d
 fdd	Z  ZS )ObservedUserStopIterationzOptional[Any]valuer#   r   r$   r   r%   c                   s0   t  d t|dkr|d | _d S d | _d S )Nzunhandled `raise StopIteration`r   )r'   r(   lenr   r*   r,   r   r   r(   ;  s   
z"ObservedUserStopIteration.__init__r.   r=   r   r   r,   r   r   5  s   
 r   c                   @  r5   )ObservedLookupErrorNr7   r   r   r   r   r   C  rp   r   c                   @  r5   )ObservedIndexErrorNr7   r   r   r   r   r   H  rp   r   c                   @  r5   )ObservedKeyErrorNr7   r   r   r   r   r   M  rp   r   c                   @  r5   )ObservedGeneratorExitNr7   r   r   r   r   r   R  r8   r   c                   @  r5   )ObservedAttributeErrorNr7   r   r   r   r   r   V  rp   r   c                   @  r5   )ObservedRuntimeErrorNr7   r   r   r   r   r   [  rp   r   c                   @  r5   )ObservedNotImplementedErrorNr7   r   r   r   r   r   `  r8   r   c                   @  r5   )ObservedTypeErrorNr7   r   r   r   r   r   d  rp   r   exc_typetype[Exception]type[ObservedException]c                 C  s:   | t vrt| dt| }td| dtfi t | < t |  S )Nr0   ObservedError)observed_exception_mapr\   r   r^   r   )r   rF   r   r   r   get_dynamo_observed_exceptionv  s   r   )r#   r$   txr   r#   Optional[list[Any]]r$   Optional[dict[str, Any]]r   c                C  sJ   ddl m} || ||pg |pi }|j| t| }|r#|| |)Nr   )BuiltinVariable)	variablesr   call_functionexn_vt_stackset_current_exceptionr   )r   r   r#   r$   r   exception_vt
raised_excr   r   r   raise_observed_exception  s   r   r   r%   c                 C  s   | j   d S r&   )r   clear_current_exception)r   r   r   r   handle_observed_exception  s   r   erZ   codetypes.CodeTypegb_typecontextexplanationhints	list[str]c                  sP   t | | tjjddd  fddd td  t}|||||| dd d S )	Nartifactc                   S  s
   dddS )Ndynamo_graph_break_reasonstring)rF   encodingr   r   r   r   r   <lambda>  s   z,unimplemented_with_warning.<locals>.<lambda>c                     s    S r&   r   r   graph_break_msgr   r   r     s    )metadata_fn
payload_fnz%sT)r   r   r   r   from_exclog_warning)format_error_msg_verboserc   _loggingtrace_structuredgraph_breaks_logdebugrj   )r   r   r   r   r   r   _unimplementedr   r   r   unimplemented_with_warning  s    


r   c                 C  sT   t |d }ddd |D }t |d }|  d| d| d| d}|S )N    r   c                 s  s$    | ]}d t |d  V  qdS )z  Hint: r   N)rH   indentlstrip).0hintr   r   r   	<genexpr>  s    
z-format_graph_break_message.<locals>.<genexpr>z
  Explanation: z

  Developer debug context: )rH   r   r   join)r   r   r   r   	hints_strr_   r   r   r   format_graph_break_message  s   
r   )maxsizedict[str, Any]c                  C  s   z*t t j} tdt| d}t|}t|}W d   n1 s$w   Y  W n t	y:   t
d i }Y nw i }| D ]\}}|D ]}|||d < qGqA|S )z
    Loads the gb_type to gb_id map from the graph break registry from JSON file with caching.

    Includes historical gb_type (mapping behavior of duplicate gb_types with different gb_ids is undefined).
     zgraph_break_registry.jsonNz!Error accessing the registry fileGb_type)r   __file__resolveparentr   r   openjsonloadrZ   log	exceptionitems)
script_dirregistry_pathfregistrymappingkventryr   r   r   _load_gb_type_to_gb_id_map  s&   


r   r;   c                 C  s0   d}t  }| |v r| d||  d dS dS )z
    Retrieves the GBID documentation link for a given graph break type.

    Args:
        gb_type: The graph break type to look up.

    Returns:
        A string containing the documentation URL if found, otherwise None.
    z;https://meta-pytorch.github.io/compile-graph-break-site/gb/gbGBz.htmlN)r   r   )r   GRAPH_BREAK_SITE_URLgb_type_to_gb_id_mapr   r   r   get_gbid_documentation_link  s   r   F)r   r   r   r   boolc           	      C  sh   t | |||}t| }|r|d| 7 }|rt| |tur0d}t|dr)|j}t||d|t|)a  
    Called within dynamo to cause a graph break.
    Args:
        gb_type: Context-free graph break type. It should be a short string without any
                 information specific to the tracing context (i.e. no dynamically-generated strings)
        context: Developer context for the graph break. It can contain tracing context/dynamic strings.
        explanation: User-facing context-dependent explanation for the graph break. Can be dynamic.
        hints: List of user-facing hints for the graph break.
    z9
 For more details about this graph break, please visit: Nra   )ra   )r   r   r   warning_NOTHINGhasattrra   r`   )	r   r   r   r   r   r   r_   documentation_linkpast_real_stackr   r   r   rj   +  s   

rj   c                   @  s*   e Zd ZdddZddd	Zdd
dZdS )KeyErrorMsgr   r   r   r%   c                 C  s
   || _ d S r&   )r   )r+   r   r   r   r   r(   R     
zKeyErrorMsg.__init__r   c                 C  s
   t | jS r&   )r   r   rJ   r   r   r   __str__U  r   zKeyErrorMsg.__str__c                 C  s   |   S r&   )r   rJ   r   r   r   __repr__X  s   zKeyErrorMsg.__repr__N)r   r   r   r%   )r   r   )r0   r1   r2   r(   r   r   r   r   r   r   r   Q  s    

r   r   excr_   exportc                 C  sT  dd l }d | _t| }|d ur't|dkr'|d | _|dd|| 7 }tjr<t| dr<|d| j	 d| j	 d7 }tj
sHt| d	rH|d
7 }t| drst| jdrst| jdri|d| jj d| jj d7 }n
|d| jj d7 }t| jdkr|dnt| jd }t| trt|| f| jdd   | _d S || }|f| jdd   | _d S )Nr   z
from user code:
 r   record_filenamez!
Last frame execution written to zD. To run only this frame while debugging, run torch._dynamo.replay('z').
ra   z
Set TORCHDYNAMO_VERBOSE=1 for the internal stack trace (please do this especially if you're reporting a bug to PyTorch). For even more developer context, set TORCH_LOGS="+dynamo"
rY   minifier_pathbuck_commandz
Minifier script written to zW. Run this buck command to find the smallest traced graph which reproduces this error: r   zQ. Run this script to find the smallest traced graph which reproduces this error.
r   )	tracebackinnermost_user_frame_summaryget_real_stackr   r   r	   r   replay_record_enabledr   r   rQ   rY   r   r   r#   r   r   KeyErrorr   )r   r_   r   r   ra   old_msgnew_msgr   r   r   augment_exc_message\  sB   

 
"r  
compile_idr   #tuple[Optional[str], Optional[int]]c                 C  s0   d }d }| j d ur| j j}| j j}|| _||fS r&   )r   filenamelinenor  )r   r  r  r  r   r   r   get_exc_message  s   
r  r
   c                   C  s
   t t S r&   )filter_stackr   r   r   r   r   get_stack_above_dynamo  r   r
  frameOptional[DynamoFrameType]r   c                 C  s<   t | dd }|d u rd S |d urt }nt }t|| S )Nra   )r\   r
  r
   	from_list)r   r  ra   stack_above_dynamor   r   r   r     s   r   stackc                 C  sV   t  }| D ]#}|jd u rqd|jv r |S d|jv s"|jr#d|jv r#q|| q|S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r
   r  lineappend)r  
user_stackr  r   r   r   r	    s   



r	  rF   c                 C  s0   ddl m} t| d| }|r|dS d S )Nr   )TORCH_DYNAMO_RESUME_IN_PREFIXz_(\w+)_at_\d+)resume_executionr  rematchgroup)rF   r  r  r   r   r   remove_resume_prefix  s
   
r  c                 C  sZ   t  }| D ]%}|jdu rqt|j}|r%|r%|d j|kr%||d< ||_q|| q|S )a1  
    When we graph break, we create a resume function and make a regular Python call
    to it, which gets intercepted by Dynamo. This behavior is normally shown in the
    traceback, which can be confusing to a user. So we can filter out resume frames
    for better traceback clarity.

    Example:
    File "..." line 3, in f
        <line 3>
    File "..." line 5, in torch_dynamo_resume_in_f_at_80
        <line 5>
    File "..." line 10, in torch_dynamo_resume_in_f_at_120
        <line 10>

    becomes
    File "..." line 10, in f
        <line 10>
    Nr   )r
   r  r  rF   r  )r  	new_stackr  rF   r   r   r   collapse_resume_frames  s   

r  r   c                 C  sp   d|j  d|j d|j d}|d7 }|t 7 }t| |}|d ur6|d7 }|dt|7 }|d7 }|d7 }|S )	NWON'T CONVERT r    line r   z.========== TorchDynamo Stack Trace ==========
zY
========== The above exception occurred while processing the following code ==========

r   z
==========)co_nameco_filenameco_firstlinenor   r   r   r	   )r   r   r   r  r_   ra   r   r   r   r     s   

r   c                 C  s.   t | dd dt | dd dt | dd dS )	Nr  z	<unknown>z (r   r  r!  r   ))r\   )r   r   r   r   format_frame_info  s   

r#  Optional[types.CodeType]reasonc                 C  s*   | d urt | }d| d| S d| S )Nz6torch.compile intentionally decided to skip the frame z! and fall back to eager.
Reason: zVtorch.compile intentionally decided to skip the frame and fall back to eager.
Reason: r#  )r   r%  
frame_infor   r   r   format_skip_frame_message  s   r(  frame_summaryc                 C  s   t | }d| d| S )NzxSkipping frame because there is a graph break in a for/while loop
torch.compile intentionally decided to skip the frame zd and fall back to eager.
Reason: Skipping frame because there is a graph break in a for/while loop.
r&  )r   r)  r'  r   r   r   format_loop_skip_frame_message(  s   r*  c                 C  s8   t jr
t| |||S d|j d|j d|j dt  S )Nr  r   r  z 
due to: 
)r   rQ   r   r  r   r!  r   )r   r   r   r  r   r   r   format_error_msg2  s   r+  )r   r   r   r   )r   r   r   r   )
r   r   r   r   r#   r   r$   r   r   r   )r   r   r   r%   )r   rZ   r   r   r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   )r   r   r   r;   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   F)r   rZ   r_   r   r   r   r   r%   )r   rZ   r  r   r   r  )r   r
   r&   )r   rZ   r  r  r   r   )r  r
   r   r
   )rF   r   r   r;   )NN)
r   rZ   r   r   r   r;   r  r  r   r   )r   r   r   r   )r   r$  r%  r   r   r   )r   r   r)  r   r   r   )
__future__r   r   loggingr  rH   typingenumr   r   	functoolsr   pathlibr   r   r   r   r	   r
   r   r   r   r   torch._guardsrc   torch._utils_internalr   r   r   utilsr   typesr   output_graphr   symbolic_convertr   r   r   	getLoggerr0   r   r   getArtifactLoggerr   RuntimeErrorr"   r6   r9   r:   r?   r@   rA   rB   rC   rD   rE   rG   rL   rW   r`   rk   rl   rm   rn   ro   rq   rr   rt   r}   r   r   r   r   r   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r   StopIterationLookupError
IndexErrorGeneratorExitr   AttributeErrorNotImplementedError	TypeErrorr   r   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException$UnsupportedMutationAliasingException!exceptions_allowed_to_be_fallbackr   r   r   r   objectr   rj   r   r  r  r
  r   r	  r  r  r   r#  r(  r*  r+  r   r   r   r   <module>   s    




	
&
	&
/

!

	%


