o
    I&ih*                  	   @   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 d dlZddlmZ ddlmZ ddlmZ e rFd d	lmZ n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Z.G d-d. d.e)Z/G d/d0 d0eZ0G d1d2 d2e1Z2ej3j4j5ej3j4j6ej3j4j7ej3j4j8fZ9d3e1d4e:d5efd6d7Z;d4e:d5efd8d9Z<d4e:d5dfd:d;Z=G d<d= d=Z>dLd@e1d4e:dAe?d5dfdBdCZ@dMd@e1d5ee	 fdDdEZAdFdG ZB	dNd@e1d5e:fdHdIZCdNd@e1d5e:fdJdKZDdS )O    N)autoEnum)extract_stack
format_excformat_listStackSummary)castNoReturnOptional   )config)	is_fbcode)counters)exportdb_error_messagec                 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   <C:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/exc.pyr      s   
r   Z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                   @   r   )RestartAnalysisNr   r   r   r   r   r   (   r   r   c                   @   r   )SpeculationRestartAnalysisNr   r   r   r   r   r   ,   r   r   c                   @   r   )UnspecializeRestartAnalysisNr   r   r   r   r   r    0   r   r    c                   @   r   )	SkipFrameNr   r   r   r   r   r!   4   r   r!   c                   @   r   )TorchRuntimeErrorNr   r   r   r   r   r"   8   r   r"   c                          e Zd Z fddZ  ZS )InvalidBackendc                    s   t  d|d d S )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.super__init__)selfname	__class__r   r   r'   =   s   
zInvalidBackend.__init__r   r   r   r'   __classcell__r   r   r*   r   r$   <       r$   c                       r#   )ResetRequiredc                    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'   D   s
   zResetRequired.__init__r,   r   r   r*   r   r/   C   r.   r/   c                       r#   )BackendCompilerFailedc                    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(   Z
backend_fnr7   msgr*   r   r   r'   P   s   zBackendCompilerFailed.__init__r,   r   r   r*   r   r3   O   r.   r3   c                       s.   e Zd Z fddZdd ZdddZ  ZS )	Unsupportedc                    s2   t  | tjj | _|| _d | _| 	  d S N)
r&   r'   torchZ_guardsZTracingContextr   
real_stackr9   categoryadd_to_statsr(   r9   r*   r   r   r'   X   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   r>   r   r9   r2   r   r   r   remove_from_stats_   s
   zUnsupported.remove_from_statsunimplementedc                 C   s    || _ t| | j  d7  < d S )Nr   rA   )r(   r>   r   r   r   r?   e   s   zUnsupported.add_to_stats)rC   )r   r   r   r'   rB   r?   r-   r   r   r*   r   r:   W   s    r:   c                   @   r   )RecompileErrorNr   r   r   r   r   rD   j   r   rD   c                       r#   )ArgsMismatchErrorc                       t  | d S r;   r%   r@   r*   r   r   r'   o      zArgsMismatchError.__init__r,   r   r   r*   r   rE   n   r.   rE   c                       r#   )AttributeMutationErrorc                    rF   r;   r%   r@   r*   r   r   r'   t   rG   zAttributeMutationError.__init__r,   r   r   r*   r   rH   s   r.   rH   c                       s    e Zd ZdZ fddZ  ZS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    c                    rF   r;   r%   r@   r*   r   r   r'   }   rG   z CondOpArgsMismatchError.__init__)r   r   r   __doc__r'   r-   r   r   r*   r   rI   x   s    rI   c                   @   s0   e Zd Ze Ze Ze Ze Ze Ze Z	dS )UserErrorTypeN)
r   r   r   r   ZDYNAMIC_CONTROL_FLOWZANTI_PATTERNZSTANDARD_LIBRARYZCONSTRAINT_VIOLATIONZDYNAMIC_DIMZINVALID_INPUTr   r   r   r   rK      s    
rK   c                       s$   e Zd Zddef fddZ  ZS )	UserErrorN
error_typec                    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. 
)
isinstancestrendswithr   r&   r'   rM   message)r(   rM   r9   r   r*   r   r   r'      s   	


zUserError.__init__r;   )r   r   r   rK   r'   r-   r   r   r*   r   rL      s    rL   c                   @   r   )UncapturedHigherOrderOpErrorNr   r   r   r   r   rU      r   rU   c                   @   r   )IncorrectUsageNr   r   r   r   r   rV      r   rV   er9   returnc                 C   s*   t | |}td| t| t|| )Nz%s)format_error_msg_verbosegraph_breaks_logdebuglogwarningrC   )rW   coder9   Zgraph_break_msgr   r   r   unimplemented_with_warning   s   


r_   c                 C   s   | t jddksJ t| )NBREAKF)osenvirongetr:   r9   r   r   r   rC      s   rC   c                 C   s.   t d |   d7  < | tjddksJ d S )Nwarningsr   r`   F)r   ra   rb   rc   rd   r   r   r   r]      s   r]   c                   @   s*   e Zd Zdd Zdd ZdefddZdS )	KeyErrorMsgc                 C   s
   || _ d S r;   )value)r(   rg   r   r   r   r'         
zKeyErrorMsg.__init__c                 C   s
   t | jS r;   )rR   rg   r2   r   r   r   __str__   rh   zKeyErrorMsg.__str__rX   c                 C   s   |   S r;   )ri   r2   r   r   r   __repr__   s   zKeyErrorMsg.__repr__N)r   r   r   r'   ri   rR   rj   r   r   r   r   rf      s    rf   rP   Fexcexportc                 C   sJ  dd l }t| }|d ur|dd|| 7 }tjr.t| dr.|d| j d| j d7 }tjs:t| dr:|d	7 }t| d
ret| j	dret| j	dr[|d| j	j
 d| j	j d7 }n
|d| j	j
 d7 }tjsn|sn|d7 }t| jdkrwd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').
r=   zI
Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information
r7   minifier_pathbuck_commandz
Minifier script written to zW. Run this buck command to find the smallest traced graph which reproduces this error: rP   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get_real_stackjoinr   r   Zreplay_record_enabledhasattrrn   verboser7   ro   rp   Zsuppress_errorslenargsrR   rQ   KeyErrorrf   )rk   r9   rl   rq   r=   Zold_msgZnew_msgr   r   r   augment_exc_message   s>   

 
"ry   c                 C   s<   t | dd }|d u rd S g }|d urtt }tt|| S )Nr=   )r5   filter_stackr   r   r   )rk   framer=   Zstack_above_dynamor   r   r   rr     s   
rr   c                 C   sB   g }| D ]}d|j v r |S d|j v sd|jv rq|| q|S )NZconvert_frameZ
eval_frameztorch._dynamo.optimize()filenamelineappend)stackZ
user_stackr{   r   r   r   rz   "  s   
rz   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 rO    line rP   z.========== TorchDynamo Stack Trace ==========
zY
========== The above exception occurred while processing the following code ==========

rm   z
==========)co_nameco_filenameco_firstlinenor   rr   rs   r   )rk   r^   rn   r{   r9   r=   r   r   r   rY   .  s   

rY   c                 C   sJ   t jd }tjrt| |||}|S d|j d|j d|j dt  }|S )N   r   rO   r   z 
due to: 
)	ra   linesepr   ru   rY   r   r   r   r   )rk   r^   rn   r{   r9   r   r   r   format_error_msgF  s   
r   )rP   Fr;   )NN)Era   r0   enumr   r   rq   r   r   r   r   typingr   r	   r
   Ztorch._guardsr<   rm   r   r   utilsr   Ztorch.fb.exportdb.loggingr   logging	getLoggerr   r\   Z_loggingZgetArtifactLoggerrZ   RuntimeErrorr   r   r   r   r    r!   r"   r$   r/   r3   r:   rD   rE   rH   rI   rK   rL   rU   	ExceptionrV   Z_subclassesZfake_tensorZDataDependentOutputExceptionZDynamicOutputShapeExceptionZUnsupportedOperatorExceptionZUnsupportedFakeTensorExceptionZ!exceptions_allowed_to_be_fallbackrR   r_   rC   r]   rf   boolry   rr   rz   rY   r   r   r   r   r   <module>   sj    
		.
