
    wi:              	          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rd dlmZ d	 Zd dlZ ej        e          Zej                            ed
          Z G d de           Z! G d de!          Z" G d de!          Z# G d de#          Z$ G d de#          Z% G d de#          Z& G d de#          Z' G d de!          Z( G d de!          Z) G d de!          Z* G d d e!          Z+ G d! d"e!          Z, G d# d$e!          Z- G d% d&e!          Z. G d' d(e-          Z/ G d) d*e-          Z0 G d+ d,e/          Z1 G d- d.e          Z2 G d/ d0e-          Z3 G d1 d2e!          Z4 G d3 d4e-          Z5 G d5 d6e!          Z6 G d7 d8e!          Z7 G d9 d:e8          Z9 G d; d<e8          Z: G d= d>e!          Z; G d? d@e;          Z< G dA dBe;          Z= G dC dDe;          Z>e?e<e@e=eAe>iZBdE ZCdF ZDejE        jF        jG        ejE        jF        jH        ejE        jF        jI        ejE        jF        jJ        fZKdGe8dHeLdIefdJZM eN            ZOeOddKdHeLdLedMeeL         dIefdNZPdHeLdIdfdOZQ G dP dQ          ZRd^dTe8dHeLdUeSdIdfdVZTdGe8dWdXdIeeeL         eeU         f         fdYZVd_dTe8dIee	         fdZZWd[ ZX	 d`dTe8dIeLfd\ZYd`dTe8dIeLfd]ZZdS )a    N)autoEnum)extract_stack
format_excformat_listStackSummary)AnycastNoReturnOptionalTupleTYPE_CHECKING   )config)counters)	CompileIdc                 4    d|                      dd          z   S )NzhFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#_-)replace	case_names    a/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/torch/_dynamo/exc.pyexportdb_error_messager      s%    	I


C
%
%	&    graph_breaksc                       e Zd ZdS )TorchDynamoExceptionN__name__
__module____qualname__ r   r   r   r   !           Dr   r   c                       e Zd ZdS )InternalTorchDynamoErrorNr   r#   r   r   r&   r&   %   r$   r   r&   c                   2     e Zd ZU eed<   ddd fdZ xZS )RestartAnalysisrestart_reasonN)r)   returnc                B    || _          t                      j        |  d S N)r)   super__init__)selfr)   args	__class__s      r   r.   zRestartAnalysis.__init__,   s%    ,$r   r*   N)r    r!   r"   str__annotations__r.   __classcell__r1   s   @r   r(   r(   )   sX         -1                        r   r(   c                       e Zd ZdS )SpeculationRestartAnalysisNr   r#   r   r   r8   r8   1   r$   r   r8   c                       e Zd ZdS )UnspecializeRestartAnalysisNr   r#   r   r   r:   r:   5   r$   r   r:   c                       e Zd ZdS ) CompileCollectiveRestartAnalysisNr   r#   r   r   r<   r<   9   r$   r   r<   c                       e Zd ZdS )TensorifyScalarRestartAnalysisNr   r#   r   r   r>   r>   =   r$   r   r>   c                       e Zd ZdS )	SkipFrameNr   r#   r   r   r@   r@   A   r$   r   r@   c                       e Zd ZdS )TorchRuntimeErrorNr   r#   r   r   rB   rB   E   r$   r   rB   c                         e Zd Zd fdZ xZS )InvalidBackendr*   Nc                 R    t                                          d|d           d S )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.r-   r.   )r/   namer1   s     r   r.   zInvalidBackend.__init__J   s7    eeee	
 	
 	
 	
 	
r   r2   r    r!   r"   r.   r5   r6   s   @r   rD   rD   I   s=        
 
 
 
 
 
 
 
 
 
r   rD   c                         e Zd Zd fdZ xZS )ResetRequiredr*   Nc                 n    t                                          t          j        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/   r1   s    r   r.   zResetRequired.__init__Q   s=    O 	
 	
 	
 	
 	
r   r2   rH   r6   s   @r   rJ   rJ   P   s=        
 
 
 
 
 
 
 
 
 
r   rJ   c                         e Zd Zd fdZ xZS )BackendCompilerFailedr*   Nc                     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_fnrT   msgr1   s       r   r.   zBackendCompilerFailed.__init__]   si    #J
C@@.k*kk_8M8M8VkkZikkr   r2   rH   r6   s   @r   rO   rO   \   s=                 r   rO   c                   4     e Zd Zddd fdZd Zd	dZ xZS )
UnsupportedNr   r*   c                    t                                          |           t          j        j                                        | _        || _        d | _        | 	                                 || _
        d S r,   )r-   r.   torch_guardsTracingContextr   
real_stackrW   categoryadd_to_statsr   )r/   rW   r   r1   s      r   r.   zUnsupported.__init__e   s^    -6DDFF'+(1r   c                     | j         J t          | j                  | j        xx         dz  cc<   t          | j                  | j                 dk    rt          | j                  | j        = d S d S )Nr   r   r_   r   rW   r/   s    r   remove_from_statszUnsupported.remove_from_statsm   sm    }((()))Q.)))DM"48,11'111 21r   unimplementedc                 T    || _         t          |         | j        xx         dz  cc<   d S )Nr   rb   )r/   r_   s     r   r`   zUnsupported.add_to_statss   s2     48$$$)$$$$$r   r2   )re   )r    r!   r"   r.   rd   r`   r5   r6   s   @r   rY   rY   d   sm        )- 2 2 2 2 2 2 2 22 2 2* * * * * * * *r   rY   c                       e Zd ZdS )RecompileErrorNr   r#   r   r   rh   rh   x   r$   r   rh   c                         e Zd Zd fdZ xZS )ArgsMismatchErrorr*   Nc                 J    t                                          |           d S r,   rF   r/   rW   r1   s     r   r.   zArgsMismatchError.__init__}   !    r   r2   rH   r6   s   @r   rj   rj   |   =                 r   rj   c                         e Zd Zd fdZ xZS )AttributeMutationErrorr*   Nc                 J    t                                          |           d S r,   rF   rl   s     r   r.   zAttributeMutationError.__init__   rm   r   r2   rH   r6   s   @r   rp   rp      rn   r   rp   c                   $     e Zd ZdZd fdZ xZS )CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    r*   Nc                 J    t                                          |           d S r,   rF   rl   s     r   r.   z CondOpArgsMismatchError.__init__   rm   r   r2   )r    r!   r"   __doc__r.   r5   r6   s   @r   rs   rs      sG                  r   rs   c                       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   rw   rw      sl        466466Ltvv466$&&KDFFMTVVN15...r   rw   c                   *     e Zd Zddeddf fdZ xZS )	UserErrorN
error_typer*   c                     |It          |t                    sJ |                    d          r|dz  }n|dz  }|t          |          z  }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. 
)
isinstancer3   endswithr   r-   r.   r   message)r/   r   rW   r   r1   s       r   r.   zUserError.__init__   s      i-----||C   s
t))444C$r   r,   )r    r!   r"   rw   r.   r5   r6   s   @r   r   r      sP         = $          r   r   c                       e Zd ZdS )SkipCodeRecursiveExceptionNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )RecompileLimitExceededNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )UnsafeScriptObjectErrorNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )UncapturedHigherOrderOpErrorNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )IncorrectUsageNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )FailOnRecompileLimitHitNr   r#   r   r   r   r      r$   r   r   c                       e Zd ZdS )ObservedExceptionNr   r#   r   r   r   r              Dr   r   c                   8     e Zd ZU ee         ed<   d fdZ xZS )ObservedUserStopIterationvaluer*   Nc                     t                                          d           t          |          dk    r|d         | _        d S d | _        d S )Nzunhandled `raise StopIteration`r   )r-   r.   lenr   )r/   r0   kwargsr1   s      r   r.   z"ObservedUserStopIteration.__init__   sE    :;;;t99q==aDJJJDJJJr   r2   )r    r!   r"   r   r	   r4   r.   r5   r6   s   @r   r   r      sO         C=         r   r   c                       e Zd ZdS )ObservedKeyErrorNr   r#   r   r   r   r      r   r   r   c                       e Zd ZdS )ObservedAttributeErrorNr   r#   r   r   r   r      r   r   r   c                     ddl m}  ||                               |g i           }|j                            |           t
          |          )Nr   )BuiltinVariable)	variablesr   call_functionexn_vt_stackappendobserved_exception_map)etxr   exception_vts       r   raise_observed_exceptionr      sX    ****** #?1%%33BB??LO<(((
 
##r   c                 8    | j                                          d S r,   )r   pop)r   s    r   handle_observed_exceptionr      s    2 Or   r   rW   r*   c                     t          | |          t          j                            dd fd           t                              d           t                              |           t          ||            d S )Nartifactc                      dddS )Ndynamo_graph_break_reasonstring)rG   encodingr#   r#   r   r   <lambda>z,unimplemented_with_warning.<locals>.<lambda>)  s    / 
 
 r   c                       S r,   r#   )graph_break_msgs   r   r   z,unimplemented_with_warning.<locals>.<lambda>-  s    ? r   )metadata_fn
payload_fnz%s)from_exc)	format_error_msg_verboser[   _loggingtrace_structuredgraph_breaks_logdebuglogwarningre   )r   coderW   r   s      @r   unimplemented_with_warningr     s     /q$77O	N##
 
 +*** $    4111KK#""""""r   )r   r   r   r   c                    | t           j                            dd          k    sJ |t          urt	          | |          |t	          | |          )NBREAKFr   )osenvironget_NOTHINGrY   )rW   r   r   s      r   re   re   7  sX     "*..%000000x#333A
cY
/
/
//r   c                     t           d         | xx         dz  cc<   | t          j                            dd          k    sJ d S )Nwarningsr   r   F)r   r   r   r   )rW   s    r   r   r   @  sJ    Z""*..%00000000r   c                   .    e Zd ZddZdefdZdefdZdS )KeyErrorMsgr*   Nc                     || _         d S r,   )r   )r/   r   s     r   r.   zKeyErrorMsg.__init__H  s    


r   c                 *    t          | j                  S r,   )r3   r   rc   s    r   __str__zKeyErrorMsg.__str__K  s    4:r   c                 *    |                                  S r,   )r   rc   s    r   __repr__zKeyErrorMsg.__repr__N  s    ||~~r   r2   )r    r!   r"   r.   r3   r   r   r#   r   r   r   r   G  s^               #      r   r   r   Fexcexportc                    dd l }d | _        t          |           }|Nt          |          dk    r;|d         | _        |dd                    |                    |                     z  }t          j        r&t          | d          r|d| j	         d| j	         dz  }t          j
        st          | d	          r|d
z  }t          | d          r^t          | j        d          rIt          | j        d          r!|d| j        j         d| j        j         dz  }n|d| j        j         dz  }t          j        s|s|dz  }t          | j                  dk    rdnt!          | j        d                   }t#          | t$                    r*t'          ||z             f| j        dd          z   | _        d S ||z   }|f| j        dd          z   | _        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
rT   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_stackr   joinr   r   replay_record_enabledhasattrr   verboserT   r   r   suppress_errorsr0   r3   r   KeyErrorr   )r   rW   r   r   r^   old_msgnew_msgs          r   augment_exc_messager   R  sK   '+C$$$J#j//A"5"5+5b>(Rbggi.C.CJ.O.O&P&PRRR# 35F(G(G 3 3C4G 3 3+3 3 3 	3 > ]gc<88 ]\\s%&& 7_, ,  3&77 
	U0C0Q U U030C0PU U UCC _0C0Q _ _ _C
 ! 
& 
@	
 MMQ&&bbC,<,<G#x   -#..038ABB<?C-:,r   
compile_idr   c                 ^    d }d }| j         | j         j        }| j         j        }|| _        ||fS r,   )r   filenamelinenor   )r   r   r   r   s       r   get_exc_messager     s?     HF%11:/6ALVr   c                     t          | dd           }|d S g }|t          t                                }t          t          ||z             S )Nr^   )rR   filter_stackr   r
   r   )r   framer^   stack_above_dynamos       r   r   r     sV    lD11Jt  *-//::0:=>>>r   c                 z    g }| D ]5}d|j         v r n)d|j         v s	d|j        v r |                    |           6|S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r   liner   )stack
user_stackr   s      r   r   r     sb    J ! !en,,E5>))-F%*-T-T%    r   c                     d|j          d|j         d|j         d}|dz  }|t                      z  }t	          | |          }|4|dz  }|d                    t          |                    z  }|dz  }|dz  }|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   rW   r^   s         r   r   r     s     	XWW(8WW@SWWW  CCC:<<CU++J	
 	rww{:..///txJr   c           	          t           j        dz  }t          j        rt	          | |||          }n)d|j         d|j         d|j         dt                       }|S )N   r   r   r   z 
due to: 
)	r   linesepr   r   r   r   r   r   r   )r   r   r   r   rW   s        r   format_error_msgr    sx    
*q.C~ 8&sD/5II8t| 8 8d.> 8 88 8)38 8 Jr   )r   Fr,   )NN)[r   rL   enumr   r   r   r   r   r   r   typingr	   r
   r   r   r   r   torch._guardsr[   r   r   utilsr   r   r   logging	getLoggerr    r   r   getArtifactLoggerr   RuntimeErrorr   r&   r(   r8   r:   r<   r>   r@   rB   rD   rJ   rO   rY   rh   rj   rp   rs   rw   r   r   r   r   r   	Exceptionr   r   r   r   r   r   StopIterationr   AttributeErrorr   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException!exceptions_allowed_to_be_fallbackr3   r   objectr   re   r   r   boolr   intr   r   r   r   r  r#   r   r   <module>r     s5   				          J J J J J J J J J J J J F F F F F F F F F F F F F F F F                  (''''''    g!!>33HnMM 	 	 	 	 	< 	 	 		 	 	 	 	3 	 	 	         *      	 	 	 	 	 	 	 		 	 	 	 	/ 	 	 		 	 	 	 	 	 	 		 	 	 	 	_ 	 	 		 	 	 	 	$ 	 	 		 	 	 	 	, 	 	 	
 
 
 
 
) 
 
 
	
 	
 	
 	
 	
( 	
 	
 	
    0   * * * * *& * * *(	 	 	 	 	) 	 	 	       
    [   
    /   8 8 8 8 8D 8 8 8       ,	 	 	 	 	!5 	 	 		 	 	 	 	[ 	 	 		 	 	 	 	2 	 	 		 	 	 	 	#7 	 	 		 	 	 	 	Y 	 	 		 	 	 	 	i 	 	 		 	 	 	 	, 	 	 	
     1   	 	 	 	 	( 	 	 	
	 	 	 	 	. 	 	 	 ,* $ $ $  < 
!>	!=	!>	!@	% !#) # # # # # #, 688 "*d0 0 0	006>sm00 0 0 01 1 1 1 1 1       .- .-Y .-S .- .-RV .- .- .- .-b		)	
8C=(3-'(	 	 	 	? ?	 ?(<2H ? ? ? ?<	 	 	 7; 	   0	 	) 	PS 	 	 	 	 	 	r   