
    קg8              	          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 d2e3e,          Z4 G d3 d4e!          Z5 G d5 d6e!          Z6 G d7 d8e7          Z8 G d9 d:e!          Z9 G d; d<e9          Z: G d= d>e9          Z; G d? d@e9          Z<e=e:e>e;e?e<iZ@dA ZAdB ZBejC        jD        jE        ejC        jD        jF        ejC        jD        jG        ejC        jD        jH        fZIdCe7dDeJdEefdFZK eL            ZMeMddGdDeJdHedIeeJ         dEefdJZNdDeJdEdfdKZO G dL dM          ZPdZdPe7dDeJdQeQdEdfdRZRdCe7dSdTdEeeeJ         eeS         f         fdUZTd[dPe7dEee	         fdVZUdW ZV	 d\dPe7dEeJfdXZWd\dPe7dEeJfdYZXdS )]    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    M/var/www/html/ai-engine/env/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 )	SkipFrameNr   r#   r   r   r>   r>   =   r$   r   r>   c                       e Zd ZdS )TorchRuntimeErrorNr   r#   r   r   r@   r@   A   r$   r   r@   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__F   s7    eeee	
 	
 	
 	
 	
r   r2   r    r!   r"   r.   r5   r6   s   @r   rB   rB   E   s=        
 
 
 
 
 
 
 
 
 
r   rB   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__M   s=    O 	
 	
 	
 	
 	
r   r2   rF   r6   s   @r   rH   rH   L   s=        
 
 
 
 
 
 
 
 
 
r   rH   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_fnrR   msgr1   s       r   r.   zBackendCompilerFailed.__init__Y   si    #J
C@@.k*kk_8M8M8VkkZikkr   r2   rF   r6   s   @r   rM   rM   X   s=                 r   rM   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_stackrU   categoryadd_to_statsr   )r/   rU   r   r1   s      r   r.   zUnsupported.__init__a   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   rU   r/   s    r   remove_from_statszUnsupported.remove_from_statsi   sm    }((()))Q.)))DM"48,11'111 21r   unimplementedc                 T    || _         t          |         | j        xx         dz  cc<   d S )Nr   r`   )r/   r]   s     r   r^   zUnsupported.add_to_statso   s2     48$$$)$$$$$r   r2   )rc   )r    r!   r"   r.   rb   r^   r5   r6   s   @r   rW   rW   `   sm        )- 2 2 2 2 2 2 2 22 2 2* * * * * * * *r   rW   c                       e Zd ZdS )RecompileErrorNr   r#   r   r   rf   rf   t   r$   r   rf   c                         e Zd Zd fdZ xZS )ArgsMismatchErrorr*   Nc                 J    t                                          |           d S r,   rD   r/   rU   r1   s     r   r.   zArgsMismatchError.__init__y   !    r   r2   rF   r6   s   @r   rh   rh   x   =                 r   rh   c                         e Zd Zd fdZ xZS )AttributeMutationErrorr*   Nc                 J    t                                          |           d S r,   rD   rj   s     r   r.   zAttributeMutationError.__init__~   rk   r   r2   rF   r6   s   @r   rn   rn   }   rl   r   rn   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,   rD   rj   s     r   r.   z CondOpArgsMismatchError.__init__   rk   r   r2   )r    r!   r"   __doc__r.   r5   r6   s   @r   rq   rq      sG                  r   rq   c                       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   ru   ru      s^        466466Ltvv466$&&KDFFMTVVNNNr   ru   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   rU   r   r1   s       r   r.   zUserError.__init__   s      i-----||C   s
t))444C$r   r,   )r    r!   r"   ru   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 )CacheLimitExceededNr   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 )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txvtr   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   rU   r*   c                     t          | |          }t                              d|           t                              |           t          ||            d S )Nz%s)from_exc)format_error_msg_verbosegraph_breaks_logdebuglogwarningrc   )r   coderU   graph_break_msgs       r   unimplemented_with_warningr     sT     /q$77O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_NOTHINGrW   )rU   r   r   s      r   rc   rc   #  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   )rU   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__4  s    


r   c                 *    t          | j                  S r,   )r3   r   ra   s    r   __str__zKeyErrorMsg.__str__7  s    4:r   c                 *    |                                  S r,   )r   ra   s    r   __repr__zKeyErrorMsg.__repr__:  s    ||~~r   r2   )r    r!   r"   r.   r3   r   r   r#   r   r   r   r   3  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
rR   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   verboserR   r   r   suppress_errorsr0   r3   r   KeyErrorr   )r   rU   r   r   r\   old_msgnew_msgs          r   augment_exc_messager   >  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   o  s?     HF%11:/6ALVr   c                     t          | dd           }|d S g }|t          t                                }t          t          ||z             S )Nr\   )rP   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   rU   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   rU   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)Yr   rJ   enumr   r   r   r   r   r   r   typingr	   r
   r   r   r   r   torch._guardsrY   r   r   utilsr   r   r   logging	getLoggerr    r   _logginggetArtifactLoggerr   RuntimeErrorr   r&   r(   r8   r:   r<   r>   r@   rB   rH   rM   rW   rf   rh   rn   rq   ru   r~   r   r   r   r   	Exceptionr   r   r   r   r   StopIterationr   AttributeErrorr   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException!exceptions_allowed_to_be_fallbackr3   r   objectr   rc   r   r   boolr   intr   r   r   r   r   r#   r   r   <module>r	     s   				          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   * * * * *& * * *(	 	 	 	 	) 	 	 	       
    [   
    /       D          ,	 	 	 	 	!5 	 	 		 	 	 	 	3[ 	 	 		 	 	 	 	2 	 	 		 	 	 	 	#7 	 	 		 	 	 	 	Y 	 	 		 	 	 	 	, 	 	 	
     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   