o
    ߗi:                  	   @   s  d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	 d dl
mZmZmZmZmZmZ d dlZddlmZ ddlmZ erDd dlmZ d	d
 Zd dl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!Z#G dd de#Z$G dd de#Z%G dd de#Z&G dd de#Z'G dd de!Z(G dd de!Z)G dd de!Z*G d d! d!e!Z+G d"d# d#e!Z,G d$d% d%e!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/eZ2G d0d1 d1e-Z3G d2d3 d3e!Z4G d4d5 d5e-Z5G d6d7 d7e!Z6G d8d9 d9e!Z7G d:d; d;e8Z9G d<d= d=e8Z:G d>d? d?e!Z;G d@dA dAe;Z<G dBdC dCe;Z=G dDdE dEe;Z>e?e<e@e=eAe>iZBdFdG ZCdHdI ZDejEjFjGejEjFjHejEjFjIejEjFjJfZKdJe8dKeLdLefdMdNZMeN ZOeOddOdKeLdPedQeeL dLefdRdSZPdKeLdLdfdTdUZQG dVdW dWZRdjdZe8dKeLd[eSdLdfd\d]ZTdJe8d^d_dLeeeL eeU f fd`daZVdkdZe8dLee	 fdbdcZWddde ZX	dldZe8dLeLfdfdgZYdldZe8dLeLfdhdiZZdS )m    N)autoEnum)extract_stack
format_excformat_listStackSummary)AnycastNoReturnOptionalTupleTYPE_CHECKING   )config)counters)	CompileIdc                 C   s   d|  dd S )NzhFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#_-)replace	case_name r   O/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/exc.pyexportdb_error_message   s   
r   graph_breaksc                   @      e Zd ZdS )TorchDynamoExceptionN__name__
__module____qualname__r   r   r   r   r   !       r   c                   @   r   )InternalTorchDynamoErrorNr   r   r   r   r   r"   %   r!   r"   c                       s.   e Zd ZU eed< ddd fddZ  ZS )RestartAnalysisrestart_reasonN)r$   returnc                   s   || _ t j|  d S N)r$   super__init__)selfr$   args	__class__r   r   r(   ,   s   zRestartAnalysis.__init__r%   N)r   r   r    str__annotations__r(   __classcell__r   r   r+   r   r#   )   s   
 r#   c                   @   r   )SpeculationRestartAnalysisNr   r   r   r   r   r1   1   r!   r1   c                   @   r   )UnspecializeRestartAnalysisNr   r   r   r   r   r2   5   r!   r2   c                   @   r   ) CompileCollectiveRestartAnalysisNr   r   r   r   r   r3   9   r!   r3   c                   @   r   )TensorifyScalarRestartAnalysisNr   r   r   r   r   r4   =   r!   r4   c                   @   r   )	SkipFrameNr   r   r   r   r   r5   A   r!   r5   c                   @   r   )TorchRuntimeErrorNr   r   r   r   r   r6   E   r!   r6   c                          e Zd Zd fddZ  ZS )InvalidBackendr%   Nc                    s   t  d|d d S )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.r'   r(   )r)   namer+   r   r   r(   J   s   
zInvalidBackend.__init__r-   r   r   r    r(   r0   r   r   r+   r   r8   I       r8   c                       r7   )ResetRequiredr%   Nc                    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(   Q   s
   zResetRequired.__init__r-   r;   r   r   r+   r   r=   P   r<   r=   c                       r7   )BackendCompilerFailedr%   Nc                    sB   t |dd| _|| _d| jdt|j d| }t | d S )Nr   ?zbackend=z	 raised:
z: )getattrbackend_nameinner_exceptiontyper   r'   r(   )r)   
backend_fnrE   msgr+   r   r   r(   ]   s   zBackendCompilerFailed.__init__r-   r;   r   r   r+   r   rA   \   r<   rA   c                       s6   e Zd Zddd fddZdd Zdd	d
Z  ZS )UnsupportedNr   r%   c                   s8   t  | tjj | _|| _d | _| 	  || _
d S r&   )r'   r(   torch_guardsTracingContextr   
real_stackrH   categoryadd_to_statsr   )r)   rH   r   r+   r   r   r(   e   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   rN   r   rH   r@   r   r   r   remove_from_statsm   s
   zUnsupported.remove_from_statsunimplementedc                 C   s    || _ t| | j  d7  < d S )Nr   rP   )r)   rN   r   r   r   rO   s   s   zUnsupported.add_to_statsr-   )rR   )r   r   r    r(   rQ   rO   r0   r   r   r+   r   rI   d   s    rI   c                   @   r   )RecompileErrorNr   r   r   r   r   rS   x   r!   rS   c                       r7   )ArgsMismatchErrorr%   Nc                       t  | d S r&   r9   r)   rH   r+   r   r   r(   }      zArgsMismatchError.__init__r-   r;   r   r   r+   r   rT   |   r<   rT   c                       r7   )AttributeMutationErrorr%   Nc                    rU   r&   r9   rV   r+   r   r   r(      rW   zAttributeMutationError.__init__r-   r;   r   r   r+   r   rX      r<   rX   c                       s"   e Zd ZdZd fddZ  ZS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    r%   Nc                    rU   r&   r9   rV   r+   r   r   r(      rW   z CondOpArgsMismatchError.__init__r-   )r   r   r    __doc__r(   r0   r   r   r+   r   rY      s    rY   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)r   r   r    r   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAINT_VIOLATIONDYNAMIC_DIMINVALID_INPUTINVALID_OUTPUT*UNSUPPORTED_ALIASED_MUTATED_DYNAMIC_INPUTSr   r   r   r   r[      s    
r[   c                       s(   e Zd Zddeddf fddZ  ZS )	UserErrorN
error_typer%   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(   re   message)r)   re   rH   r   r+   r   r   r(      s   	


zUserError.__init__r&   )r   r   r    r[   r(   r0   r   r   r+   r   rd      s     rd   c                   @   r   )SkipCodeRecursiveExceptionNr   r   r   r   r   rl      r!   rl   c                   @   r   )RecompileLimitExceededNr   r   r   r   r   rm      r!   rm   c                   @   r   )UnsafeScriptObjectErrorNr   r   r   r   r   rn      r!   rn   c                   @   r   )UncapturedHigherOrderOpErrorNr   r   r   r   r   ro      r!   ro   c                   @   r   )IncorrectUsageNr   r   r   r   r   rp      r!   rp   c                   @   r   )FailOnRecompileLimitHitNr   r   r   r   r   rq      r!   rq   c                   @   r   )ObservedExceptionNr   r   r   r   r   rr          rr   c                       s,   e Zd ZU ee ed< d fddZ  ZS )ObservedUserStopIterationvaluer%   Nc                    s0   t  d t|dkr|d | _d S d | _d S )Nzunhandled `raise StopIteration`r   )r'   r(   lenru   )r)   r*   kwargsr+   r   r   r(      s   
z"ObservedUserStopIteration.__init__r-   )r   r   r    r   r   r/   r(   r0   r   r   r+   r   rt      s   
 rt   c                   @   r   )ObservedKeyErrorNr   r   r   r   r   rx      rs   rx   c                   @   r   )ObservedAttributeErrorNr   r   r   r   r   ry      rs   ry   c                 C   s2   ddl m} || |g i }|j| t|  )Nr   )BuiltinVariable)	variablesrz   call_functionexn_vt_stackappendobserved_exception_map)etxrz   exception_vtr   r   r   raise_observed_exception   s   r   c                 C   s   | j   d S r&   )r}   pop)r   r   r   r   handle_observed_exception   s   r   r   rH   r%   c                    sN   t | | tjjddd  fddd td  t| t|| d d S )Nartifactc                   S   s
   dddS )Ndynamo_graph_break_reasonstring)r:   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%s)from_exc)	format_error_msg_verboserJ   _loggingtrace_structuredgraph_breaks_logdebuglogwarningrR   )r   coderH   r   r   r   unimplemented_with_warning  s   


r   )r   r   r   r   c                C   s8   | t jddksJ |turt| |d|t| |d)NBREAKFr   )osenvironget_NOTHINGrI   )rH   r   r   r   r   r   rR   7  s   rR   c                 C   s.   t d |   d7  < | tjddksJ d S )Nwarningsr   r   F)r   r   r   r   )rH   r   r   r   r   @  s   r   c                   @   s2   e Zd Zd	ddZdefddZdefddZdS )
KeyErrorMsgr%   Nc                 C   s
   || _ d S r&   )ru   )r)   ru   r   r   r   r(   H     
zKeyErrorMsg.__init__c                 C   s
   t | jS r&   )r.   ru   r@   r   r   r   __str__K  r   zKeyErrorMsg.__str__c                 C   s   |   S r&   )r   r@   r   r   r   __repr__N  s   zKeyErrorMsg.__repr__r-   )r   r   r    r(   r.   r   r   r   r   r   r   r   G  s    
r   rh   Fexcexportc                 C   sf  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s||s||d7 }t| jdkrdnt| jd }t| trt|| f| jdd   | _d S || }|f| jdd   | _d S )Nr   z
from user code:
  record_filenamez!
Last frame execution written to zD. To run only this frame while debugging, run torch._dynamo.replay('z').
rM   zI
Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information
rE   minifier_pathbuck_commandz
Minifier script written to zW. Run this buck command to find the smallest traced graph which reproduces this error: rh   zQ. Run this script to find the smallest traced graph which reproduces this error.
z

You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True
r   )	tracebackinnermost_user_frame_summaryget_real_stackrv   joinr   r   replay_record_enabledhasattrr   verboserE   r   r   suppress_errorsr*   r.   ri   KeyErrorr   )r   rH   r   r   rM   old_msgnew_msgr   r   r   augment_exc_messageR  sB   


 
"r   
compile_idr   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   c                 C   s<   t | dd }|d u rd S g }|d urtt }tt|| S )NrM   )rC   filter_stackr   r	   r   )r   framerM   stack_above_dynamor   r   r   r     s   
r   c                 C   sB   g }| D ]}d|j v r |S d|j v sd|jv rq|| q|S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r   liner~   )stack
user_stackr   r   r   r   r     s   
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 rg    line rh   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   rH   rM   r   r   r   r     s   

r   c                 C   sJ   t jd }tjrt| |||}|S d|j d|j d|j dt  }|S )N   r   rg   r   z 
due to: 
)	r   linesepr   r   r   r   r   r   r   )r   r   r   r   rH   r   r   r   format_error_msg  s   
r   )rh   Fr&   )NN)[r   r>   enumr   r   r   r   r   r   r   typingr   r	   r
   r   r   r   torch._guardsrJ   r   r   utilsr   r   r   logging	getLoggerr   r   r   getArtifactLoggerr   RuntimeErrorr   r"   r#   r1   r2   r3   r4   r5   r6   r8   r=   rA   rI   rS   rT   rX   rY   r[   rd   rl   rm   rn   ro   	Exceptionrp   rq   rr   rt   rx   ry   StopIterationr   AttributeErrorr   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException!exceptions_allowed_to_be_fallbackr.   r   objectr   rR   r   r   boolr   intr   r   r   r   r   r   r   r   r   <module>   s    
	

	1

