
    ɯwg8              	       V   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j4                  e      Zej:                  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j                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  fZIdCe7dDeJdEefdFZK eL       ZMeMddGdDeJdHedIeeJ   dEefdJZNdDeJdEdfdKZO G dL dM      ZPdXdNe7dDeJdOeQdEdfdPZRdCe7dQdRdEeeeJ   eeS   f   fdSZTdYdNe7dEee	   fdTZUdU ZV	 dZdNe7dEeJfdVZWdZdNe7dEeJfdWZXy)[    N)autoEnum)extract_stack
format_excformat_listStackSummary)AnycastNoReturnOptionalTupleTYPE_CHECKING   )config)counters)	CompileIdc                 ,    d| j                  dd      z   S )NzhFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#_-)replace	case_names    V/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_dynamo/exc.pyexportdb_error_messager      s"    	I


C
%	&    graph_breaksc                       e Zd Zy)TorchDynamoExceptionN__name__
__module____qualname__ r   r   r   r   !       r   r   c                       e Zd Zy)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)   c                ,    || _         t        |   |  y N)r)   super__init__)selfr)   args	__class__s      r   r-   zRestartAnalysis.__init__,   s    ,$r   returnN)r    r!   r"   str__annotations__r-   __classcell__r0   s   @r   r(   r(   )   s    -1    r   r(   c                       e Zd Zy)SpeculationRestartAnalysisNr   r#   r   r   r8   r8   1   r$   r   r8   c                       e Zd Zy)UnspecializeRestartAnalysisNr   r#   r   r   r:   r:   5   r$   r   r:   c                       e Zd Zy) CompileCollectiveRestartAnalysisNr   r#   r   r   r<   r<   9   r$   r   r<   c                       e Zd Zy)	SkipFrameNr   r#   r   r   r>   r>   =   r$   r   r>   c                       e Zd Zy)TorchRuntimeErrorNr   r#   r   r   r@   r@   A   r$   r   r@   c                         e Zd Zd fdZ xZS )InvalidBackendc                 ,    t         |   d|d       y )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.r,   r-   )r.   namer0   s     r   r-   zInvalidBackend.__init__F   s    x'de	
r   r1   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 )ResetRequiredc                 J    t         |   t        j                  d             y )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.   r0   s    r   r-   zResetRequired.__init__M   s!    OO	
r   r1   rF   r6   s   @r   rH   rH   L   s    
 
r   rH   c                         e Zd Zd fdZ xZS )BackendCompilerFailedc                     t        |dd      | _        || _        d| j                  dt        |      j                   d| }t
        |   |       y )Nr    ?zbackend=z	 raised:
z: )getattrbackend_nameinner_exceptiontyper    r,   r-   )r.   
backend_fnrR   msgr0   s       r   r-   zBackendCompilerFailed.__init__Y   sW    #J
C@.**-Z_8M8V8V7WWYZiYjkr   r1   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   c                    t         |   |       t        j                  j                  j                         | _        || _        d | _        | j                          || _
        y r+   )r,   r-   torch_guardsTracingContextr   
real_stackrU   categoryadd_to_statsr   )r.   rU   r   r0   s      r   r-   zUnsupported.__init__a   sK    --66DDF'+(1r   c                     | j                   J t        | j                      | j                  xx   dz  cc<   t        | j                      | j                     dk  rt        | j                      | j                  = y y )Nr   r   r]   r   rU   r.   s    r   remove_from_statszUnsupported.remove_from_statsi   sc    }}((()Q.)DMM"488,1'1 2r   c                 N    || _         t        |   | j                  xx   dz  cc<   y )Nr   r`   )r.   r]   s     r   r^   zUnsupported.add_to_statso   s"     488$)$r   r1   )unimplemented)r    r!   r"   r-   rb   r^   r5   r6   s   @r   rW   rW   `   s    )- 22*r   rW   c                       e Zd Zy)RecompileErrorNr   r#   r   r   rf   rf   t   r$   r   rf   c                         e Zd Zd fdZ xZS )ArgsMismatchErrorc                 $    t         |   |       y r+   rD   r.   rU   r0   s     r   r-   zArgsMismatchError.__init__y       r   r1   rF   r6   s   @r   rh   rh   x        r   rh   c                         e Zd Zd fdZ xZS )AttributeMutationErrorc                 $    t         |   |       y r+   rD   rj   s     r   r-   zAttributeMutationError.__init__~   rk   r   r1   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.
    c                 $    t         |   |       y r+   rD   rj   s     r   r-   z CondOpArgsMismatchError.__init__   rk   r   r1   )r    r!   r"   __doc__r-   r5   r6   s   @r   rq   rq      s     r   rq   c                   n    e Zd Z e       Z e       Z e       Z e       Z e       Z e       Z	 e       Z
y)UserErrorTypeN)r    r!   r"   r   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAINT_VIOLATIONDYNAMIC_DIMINVALID_INPUTINVALID_OUTPUTr#   r   r   ru   ru      s6    66Lv6&KFMVNr   ru   c                   *     e Zd Zddeddf fdZ xZS )	UserErrorN
error_typer2   c                     |<t        |t              sJ |j                  d      r|dz  }n|dz  }|t        |      z  }t        |   |       || _        || _        y)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   r0   s       r   r-   zUserError.__init__   sd      i---||C s
t))44C$r   r+   )r    r!   r"   ru   r-   r5   r6   s   @r   r~   r~      s    = $  r   r~   c                       e Zd Zy)SkipCodeRecursiveExceptionNr   r#   r   r   r   r      r$   r   r   c                       e Zd Zy)CacheLimitExceededNr   r#   r   r   r   r      r$   r   r   c                       e Zd Zy)UnsafeScriptObjectErrorNr   r#   r   r   r   r      r$   r   r   c                       e Zd Zy)UncapturedHigherOrderOpErrorNr   r#   r   r   r   r      r$   r   r   c                       e Zd Zy)IncorrectUsageNr   r#   r   r   r   r      r$   r   r   c                       e Zd Zy)ObservedExceptionNr   r#   r   r   r   r          r   r   c                   2     e Zd ZU ee   ed<   d fdZ xZS )ObservedUserStopIterationvaluec                 d    t         |   d       t        |      dkD  r|d   | _        y d | _        y )Nzunhandled `raise StopIteration`r   )r,   r-   lenr   )r.   r/   kwargsr0   s      r   r-   z"ObservedUserStopIteration.__init__   s.    :;t9q=aDJDJr   r1   )r    r!   r"   r   r	   r4   r-   r5   r6   s   @r   r   r      s    C= r   r   c                       e Zd Zy)ObservedKeyErrorNr   r#   r   r   r   r      r   r   r   c                       e Zd Zy)ObservedAttributeErrorNr   r#   r   r   r   r      r   r   r   c                     ddl m}  ||       j                  |g i       }|j                  j	                  |       t
        |    )Nr   )BuiltinVariable)	variablesr   call_functionexn_vt_stackappendobserved_exception_map)etxvtr   exception_vts        r   raise_observed_exceptionr      s=    * #1%33BB?LOO<(
 
##r   c                 8    | j                   j                          y r+   )r   pop)r   s    r   handle_observed_exceptionr      s    2 OOr   r   rU   r2   c                     t        | |      }t        j                  d|       t        j	                  |       t        ||        y )Nz%s)from_exc)format_error_msg_verbosegraph_breaks_logdebuglogwarningrd   )r   coderU   graph_break_msgs       r   unimplemented_with_warningr     s6     /q$7O41KK#"r   )r   r   r   r   c                    | t         j                  j                  dd      k7  sJ |t        urt	        | |      |t	        | |      )NBREAKFr   )osenvironget_NOTHINGrW   )rU   r   r   s      r   rd   rd   #  sE     "**..%0000x#3A
cY
//r   c                 v    t         d   | xx   dz  cc<   | t        j                  j                  dd      k7  sJ y )Nwarningsr   r   F)r   r   r   r   )rU   s    r   r   r   ,  s4    Z""**..%0000r   c                   ,    e Zd ZddZdefdZdefdZy)KeyErrorMsgr2   Nc                     || _         y 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                 "    | j                         S r+   )r   ra   s    r   __repr__zKeyErrorMsg.__repr__:  s    ||~r   r1   )r    r!   r"   r-   r3   r   r   r#   r   r   r   r   3  s     # r   r   excexportc                    dd l }d | _        t        |       }|>t        |      dkD  r0|d   | _        |ddj	                  |j                  |             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      rht        | j                  d      r5|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   | _        y ||z   }|f| j                  dd  z   | _        y )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_errorsr/   r3   r   KeyErrorr   )r   rU   r   r   r\   old_msgnew_msgs          r   augment_exc_messager   >  s   '+C$$J#j/A"5+5b>(%bggi.C.CJ.O&P%QRR##5F(G3C4G4G3H I++,E3 	3 >>gc<8\\s%&7_, 3&&7/0C0C0Q0Q/R S0030C0C0P0P/QQSUC /0C0C0Q0Q/R S_ _C
 !!&@	
 MQ&bC,<G#x #.0388AB<?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  sN     HF%%111:://66ALVr   c                 v    t        | dd       }|y g }|t        t                     }t        t        ||z         S )Nr\   )rP   filter_stackr   r
   r   )r   framer\   stack_above_dynamos       r   r   r   {  sI    lD1J  *-/:0:=>>r   c                     g }| D ]A  }d|j                   v r |S d|j                   v sd|j                  v r1|j                  |       C |S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r   liner   )stack
user_stackr   s      r   r   r     s`    J !enn,
 	 5>>)-F%**-T% ! r   c                     d|j                    d|j                   d|j                   d}|dz  }|t               z  }t	        | |      }|,|dz  }|dj                  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     a(8(8'9@S@S?TTVW  CCC:<CU+J	
 	rww{:.//txJr   c           	          t         j                  dz  }t        j                  rt	        | |||      }|S 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     so    
**q.C~~&sD/5I
 J t||nAd.>.>-? @=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   rd   r   r   boolr   intr   r   r   r   r   r#   r   r   <module>r	     sI   	   J J F F    '  g!>>33HnM 	< 		3 	 *  	 		/ 		 		$ 		, 	
) 
	
( 	
0 *& *(	) 	 
[ 
/ D  ,	!5 		3[ 		2 		#7 		Y 		, 	
 1 	( 	
	. 	 ,* $< 
!!>>	!!==	!!>>	!!@@	% !#) # # # 8 "*d0	006>sm001 1 1 .-Y .-S .- .-RV .-b		)	
8C=(3-'(	?	 ?(<2H ?<	 7;	0	) 	PS 	r   