o
    h&                     @   sD  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 d dlZddlmZ ddlmZ ddlmZ e rDd 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.ej/j0j1ej/j0j2ej/j0j3ej/j0j4fZ5d-d. Z6d/e7fd0d1Z8d/e7fd2d3Z9G d4d5 d5Z:dCd8d9Z;dDd:ee	 fd;d<Z<d=d> Z=dEd?d@Z>dEdAdBZ?dS )F    N)autoEnum)extract_stack
format_excformat_listStackSummary)castOptional   )config)	is_fbcode)counters)exportdb_error_messagec                 C   s   dS )N  )	case_namer   r   G/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/exc.pyr      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                   @   r   )RestartAnalysisNr   r   r   r   r   r   $   r   r   c                   @   r   )	SkipFrameNr   r   r   r   r   r   (   r   r   c                   @   r   )TorchRuntimeErrorNr   r   r   r   r   r   ,   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#   1   s   
zInvalidBackend.__init__r   r   r   r#   __classcell__r   r   r&   r   r    0       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#   8   s
   zResetRequired.__init__r(   r   r   r&   r   r+   7   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$   
backend_fnr3   msgr&   r   r   r#   D   s   zBackendCompilerFailed.__init__r(   r   r   r&   r   r/   C   r*   r/   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#   torch_guardsTracingContextr   
real_stackr6   categoryadd_to_statsr$   r6   r&   r   r   r#   L   s
   zUnsupported.__init__c                 C   sB   t | j | j  d8  < t | j | j dkrt | j | j= d S d S )Nr
   r   )r   r=   r6   r.   r   r   r   remove_from_statsS   s   zUnsupported.remove_from_statsunimplementedc                 C   s    || _ t| | j  d7  < d S )Nr
   )r=   r   r6   )r$   r=   r   r   r   r>   X   s   zUnsupported.add_to_stats)rA   )r   r   r   r#   r@   r>   r)   r   r   r&   r   r7   K   s    r7   c                   @   r   )RecompileErrorNr   r   r   r   r   rB   ]   r   rB   c                       r   )ArgsMismatchErrorc                       t  | d S r8   r!   r?   r&   r   r   r#   b      zArgsMismatchError.__init__r(   r   r   r&   r   rC   a   r*   rC   c                       r   )AttributeMutationErrorc                    rD   r8   r!   r?   r&   r   r   r#   g   rE   zAttributeMutationError.__init__r(   r   r   r&   r   rF   f   r*   rF   c                       s    e Zd ZdZ fddZ  ZS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    c                    rD   r8   r!   r?   r&   r   r   r#   p   rE   z CondOpArgsMismatchError.__init__)r   r   r   __doc__r#   r)   r   r   r&   r   rG   k   s    rG   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   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAIN_VIOLATIONDYNAMIC_DIMINVALID_INPUTr   r   r   r   rI   t   s    
rI   c                       s$   e Zd Zddef fddZ  ZS )	UserErrorN
error_typec                    s>   |durt |tsJ |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strr   r"   r#   rQ   message)r$   rQ   r6   r   r&   r   r   r#   ~   s   	
zUserError.__init__r8   )r   r   r   rI   r#   r)   r   r   r&   r   rP   }   s    rP   c                   @   r   )IncorrectUsageNr   r   r   r   r   rU      r   rU   c                 C   s*   t | |}td| t| t|| )Nz%s)format_error_msg_verbosegraph_breaks_logdebuglogwarningrA   )ecoder6   graph_break_msgr   r   r   unimplemented_with_warning   s   


r^   r6   c                 C   s   | t jddksJ t| )NBREAKF)osenvirongetr7   r6   r   r   r   rA      s   rA   c                 C   s.   t d |   d7  < | tjddksJ d S )Nwarningsr
   r_   F)r   r`   ra   rb   rc   r   r   r   rZ      s   rZ   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 r8   )value)r$   rf   r   r   r   r#         
zKeyErrorMsg.__init__c                 C   s
   t | jS r8   )rS   rf   r.   r   r   r   __str__   rg   zKeyErrorMsg.__str__returnc                 C   s   |   S r8   )rh   r.   r   r   r   __repr__   s   zKeyErrorMsg.__repr__N)r   r   r   r#   rh   rS   rj   r   r   r   r   re      s    re   
Fc                 C   sN  dd l }t| }|d ur|dd|t|  7 }tjr0t| dr0|d| j d| j d7 }tjs<t| dr<|d	7 }t| d
rgt| j	drgt| j	dr]|d| j	j
 d| j	j d7 }n
|d| j	j
 d7 }tjsp|sp|d7 }t| jdkryd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').
r<   zI
Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information
r3   minifier_pathbuck_commandz
Minifier script written to zW. Run this buck command to find the smallest traced graph which reproduces this error: rk   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   replay_record_enabledhasattrrl   verboser3   rm   rn   suppress_errorslenargsrS   rR   KeyErrorre   )excr6   exportro   r<   old_msgnew_msgr   r   r   augment_exc_message   sB   

 
"r}   ri   c                 C   s<   t | dd }|d u rd S g }|d urtt }tt|| S )Nr<   )r1   filter_stackr   r   r   )ry   framer<   stack_above_dynamor   r   r   rp      s   
rp   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()filenamelineappend)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   line rk   z.========== TorchDynamo Stack Trace ==========
zY
========== The above exception occurred while processing the following code ==========

r   z
==========)co_nameco_filenameco_firstlinenor   rp   rq   r   )ry   r\   rl   r   r6   r<   r   r   r   rV     s   

rV   c              
   C   sN   t jd }tjrt| |||}|S d|j d|j d|j dtdd }|S )N   r   r   r   z 
due to: 
)limit)	r`   linesepr   rt   rV   r   r   r   r   )ry   r\   rl   r   r6   r   r   r   format_error_msg1  s   
r   )rk   Fr8   )NN)@r`   r,   enumr   r   ro   r   r   r   r   typingr   r	   torch._guardsr9   r   r   r   utilsr   torch.fb.exportdb.loggingr   logging	getLoggerr   rY   _logginggetArtifactLoggerrW   RuntimeErrorr   r   r   r   r   r    r+   r/   r7   rB   rC   rF   rG   rI   rP   	ExceptionrU   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException!exceptions_allowed_to_be_fallbackr^   rS   rA   rZ   re   r}   rp   r~   rV   r   r   r   r   r   <module>   sZ    
		
0
