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 d dlZddlmZ d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Z-G d+d, d,e(Z.G d-d. d.eZ/G d/d0 d0eZ0G d1d2 d2eZ1G d3d4 d4e2Z3G d5d6 d6eZ4ej5j6j7ej5j6j8ej5j6j9ej5j6j:fZ;d7e2d8e<d9efd:d;Z=e> Z?e?d<d8e<d=ed9efd>d?Z@d8e<d9dfd@dAZAG dBdC dCZBdRdFe2d8e<dGeCd9dfdHdIZDdSdFe2d9ee	 fdJdKZEdLdM ZF	dTdFe2d9e<fdNdOZGdTdFe2d9e<fdPdQZHdS )U    N)autoEnum)extract_stack
format_excformat_listStackSummary)AnycastNoReturnOptional   )config)countersc                 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/SoloSpeech/.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 fdd
Z  ZS )RestartAnalysisrestart_reasonN)r    c                   s   || _ t j|  d S N)r    super__init__)selfr    args	__class__r   r   r#   (   s   zRestartAnalysis.__init__)r   r   r   str__annotations__r#   __classcell__r   r   r&   r   r   %   s   
 r   c                   @   r   )SpeculationRestartAnalysisNr   r   r   r   r   r+   -   r   r+   c                   @   r   )UnspecializeRestartAnalysisNr   r   r   r   r   r,   1   r   r,   c                   @   r   )	SkipFrameNr   r   r   r   r   r-   5   r   r-   c                   @   r   )TorchRuntimeErrorNr   r   r   r   r   r.   9   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.r"   r#   )r$   namer&   r   r   r#   >   s   
zInvalidBackend.__init__r   r   r   r#   r*   r   r   r&   r   r0   =       r0   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#   E   s
   zResetRequired.__init__r3   r   r   r&   r   r5   D   r4   r5   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_fnr=   msgr&   r   r   r#   Q   s   zBackendCompilerFailed.__init__r3   r   r   r&   r   r9   P   r4   r9   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 r!   )
r"   r#   torch_guardsTracingContextr   
real_stackr@   categoryadd_to_statsr$   r@   r&   r   r   r#   Y   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@   r8   r   r   r   remove_from_stats`   s
   zUnsupported.remove_from_statsunimplementedc                 C   s    || _ t| | j  d7  < d S )Nr   rI   )r$   rF   r   r   r   rG   f   s   zUnsupported.add_to_stats)rK   )r   r   r   r#   rJ   rG   r*   r   r   r&   r   rA   X   s    rA   c                   @   r   )RecompileErrorNr   r   r   r   r   rL   k   r   rL   c                       r/   )ArgsMismatchErrorc                       t  | d S r!   r1   rH   r&   r   r   r#   p      zArgsMismatchError.__init__r3   r   r   r&   r   rM   o   r4   rM   c                       r/   )AttributeMutationErrorc                    rN   r!   r1   rH   r&   r   r   r#   u   rO   zAttributeMutationError.__init__r3   r   r   r&   r   rP   t   r4   rP   c                       s    e Zd ZdZ fddZ  ZS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    c                    rN   r!   r1   rH   r&   r   r   r#   ~   rO   z CondOpArgsMismatchError.__init__)r   r   r   __doc__r#   r*   r   r   r&   r   rQ   y   s    rQ   c                   @   s6   e Zd 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_OUTPUTr   r   r   r   rS      s    
rS   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. 
)
isinstancer(   endswithr   r"   r#   r\   message)r$   r\   r@   r   r&   r   r   r#      s   	


zUserError.__init__r!   )r   r   r   rS   r#   r*   r   r   r&   r   r[      s    r[   c                       s*   e Zd ZU ee ed<  fddZ  ZS )UserStopIterationvaluec                    s0   t  d t|dkr|d | _d S d | _d S )Nzunhandled `raise StopIteration`r   )r"   r#   lenrd   )r$   r%   kwargsr&   r   r   r#      s   
zUserStopIteration.__init__)r   r   r   r   r   r)   r#   r*   r   r   r&   r   rc      s   
 rc   c                   @   r   )UnsafeScriptObjectErrorNr   r   r   r   r   rg      r   rg   c                   @   r   )UncapturedHigherOrderOpErrorNr   r   r   r   r   rh      r   rh   c                   @   r   )IncorrectUsageNr   r   r   r   r   ri      r   ri   c                   @   r   )ObservedExceptionNr   r   r   r   r   rj      r   rj   er@   returnc                 C   s0   t | |}td| t| t|| d d S )Nz%sfrom_exc)format_error_msg_verbosegraph_breaks_logdebuglogwarningrK   )rk   coder@   graph_break_msgr   r   r   unimplemented_with_warning   s   

rv   rm   rn   c                C   s0   | t jddksJ |turt| |t| )NBREAKF)osenvironget_NOTHINGrA   )r@   rn   r   r   r   rK      s   
rK   c                 C   s.   t d |   d7  < | tjddksJ d S )Nwarningsr   rw   F)r   rx   ry   rz   )r@   r   r   r   rs      s   rs   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!   )rd   )r$   rd   r   r   r   r#         
zKeyErrorMsg.__init__c                 C   s
   t | jS r!   )r(   rd   r8   r   r   r   __str__   r~   zKeyErrorMsg.__str__rl   c                 C   s   |   S r!   )r   r8   r   r   r   __repr__   s   zKeyErrorMsg.__repr__N)r   r   r   r#   r   r(   r   r   r   r   r   r}      s    r}   r_   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').
rE   zI
Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information
r=   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.
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_stackre   joinr   r   replay_record_enabledhasattrr   verboser=   r   r   suppress_errorsr%   r(   r`   KeyErrorr}   )r   r@   r   r   rE   old_msgnew_msgr   r   r   augment_exc_message   sB   


 
"r   c                 C   s<   t | dd }|d u rd S g }|d urtt }tt|| S )NrE   )r;   filter_stackr   r	   r   )r   framerE   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()filenamelineappend)stack
user_stackr   r   r   r   r   A  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 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   rt   r   r   r@   rE   r   r   r   ro   M  s   

ro   c                 C   sJ   t jd }tjrt| |||}|S d|j d|j d|j dt  }|S )N   r   r^   r   z 
due to: 
)	rx   linesepr   r   ro   r   r   r   r   )r   rt   r   r   r@   r   r   r   format_error_msge  s   
r   )r_   Fr!   )NN)Irx   r6   enumr   r   r   r   r   r   r   typingr   r	   r
   r   torch._guardsrB   r   r   utilsr   r   logging	getLoggerr   rr   _logginggetArtifactLoggerrp   RuntimeErrorr   r   r   r+   r,   r-   r.   r0   r5   r9   rA   rL   rM   rP   rQ   rS   r[   rc   rg   rh   	Exceptionri   rj   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException!exceptions_allowed_to_be_fallbackr(   rv   objectr{   rK   rs   r}   boolr   r   r   ro   r   r   r   r   r   <module>   sl   
	
1
