
    9j_t                       d dl mZ 	 d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ d dlmZmZmZ d dlZd dlmZ d	d
lmZ d	dlmZ er$d dlZd dl m!Z! d dlm"Z" d	dl#m$Z$ d	dl%m&Z& d	dlm'Z'm(Z( dzdZ) ejT                  e+      Z,ejZ                  j]                  e+d      Z/ G d de0      Z1 G d de1      Z2 G d de1      Z3 G d de1      Z4 G d de4      Z5 G d de4      Z6 G d d e4      Z7 G d! d"e4      Z8 G d# d$e4      Z9 G d% d&e4      Z: G d' d(e1      Z; G d) d*e1      Z< G d+ d,e1      Z= G d- d.e1      Z> G d/ d0e1      Z? G d1 d2e?      Z@ G d3 d4e1      ZA G d5 d6e1      ZB G d7 d8e1      ZC G d9 d:e1      ZD G d; d<e1      ZE G d= d>e	      ZF G d? d@e1      ZG G dA dBe1      ZH G dC dDe1      ZI G dE dFe1      ZJ G dG dHeK      ZL G dI dJe1      ZM G dK dLe1      ZN G dM dNeN      ZO G dO dPeN      ZP G dQ dReP      ZQ G dS dTeP      ZR G dU dVeN      ZS G dW dXeN      ZT G dY dZeN      ZU G d[ d\eN      ZV G d] d^eN      ZWeXeOeYePeZeQe[eSe\eRe]eTe0eUe^eVe_eWi	Z`d{d_Zaddd`	 	 	 	 	 	 	 	 	 d|daZbd}dbZcd~dcZdej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  fZl	 	 	 	 	 	 	 	 	 	 	 	 	 	 dddZm	 	 	 	 	 	 	 	 	 	 ddeZn ed	f      ddg       ZoddhZp eq       Zrerdididj	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddkZs G dl dm      ZtddnZudddoZv	 	 	 	 	 	 ddpZwddqZx	 d	 	 	 	 	 ddrZyddsZzddtZ{dduZ|	 	 d	 	 	 	 	 	 	 	 	 ddvZ}ddwZ~ddxZ	 	 d	 	 	 	 	 	 	 	 	 ddyZy)    )annotationsN)autoEnum)	lru_cache)Path)extract_stack
format_excformat_listFrameSummaryStackSummary)AnyNoReturnTYPE_CHECKING)get_file_path_2   )config)counters)VariableTracker)	CompileId)DynamoTracerOutput)InstructionTranslatorBase)DynamoFrameTypeFrameExecStrategyc                ,    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    Q/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_dynamo/exc.pyexportdb_error_messager    :   s"    	I


C
%	&    graph_breaksc                  $     e Zd ZdZd fdZ xZS )TorchDynamoExceptiona6  Base exception class for all TorchDynamo-specific exceptions.

    Attributes:
        _torch_dynamo_tracer_output: Optional tracer output attached to the exception
        frame_exec_strategy: Optional frame execution strategy to control how convert_frame
            should handle this exception. When set, convert_frame will use this strategy
            instead of the default behavior. This allows exceptions to signal specific
            execution strategies (e.g., SKIP, RUN_ONLY) without requiring separate
            exception types for control flow.
    c                @    t        |   |i | d | _        d | _        y N)super__init___torch_dynamo_tracer_outputframe_exec_strategy)selfargskwargs	__class__s      r   r(   zTorchDynamoException.__init__R   s%    $)&)FJ(=A r!   )r,   r   r-   r   returnNone)__name__
__module____qualname____doc__r(   __classcell__r.   s   @r   r$   r$   F   s    	B Br!   r$   c                      e Zd Zy)InternalTorchDynamoErrorNr1   r2   r3    r!   r   r8   r8   X       r!   r8   c                      e Zd Zy)ResumePrologueTracingErrorNr9   r:   r!   r   r=   r=   \   r;   r!   r=   c                  2     e Zd ZU ded<   ddd fdZ xZS )RestartAnalysis
str | Nonerestart_reasonN)rA   c               ,    || _         t        |   |  y r&   )rA   r'   r(   )r+   rA   r,   r.   s      r   r(   zRestartAnalysis.__init__c   s    ,$r!   )r,   r   rA   r@   r/   r0   r1   r2   r3   __annotations__r(   r5   r6   s   @r   r?   r?   `   s    @D    r!   r?   c                      e Zd Zy)SpeculationRestartAnalysisNr9   r:   r!   r   rF   rF   h   r;   r!   rF   c                      e Zd ZdZy)AutogradGradRestartAnalysiszRaised when autograd.grad consumed grad_fns that are returned.

    On restart, autograd.grad will graph break instead of being traced.
    Nr1   r2   r3   r4   r:   r!   r   rH   rH   l   s    r!   rH   c                      e Zd ZdZy)RequiresGradRestartAnalysiszRaised when a source-less requires_grad_() intermediate leaks as output.

    On restart, requires_grad_() will graph break instead of being traced,
    preserving partial acceleration for code before the call.
    NrI   r:   r!   r   rK   rK   s   s    r!   rK   c                      e Zd Zy)UnspecializeRestartAnalysisNr9   r:   r!   r   rM   rM   {   r;   r!   rM   c                      e Zd Zy) CompileCollectiveRestartAnalysisNr9   r:   r!   r   rO   rO      r;   r!   rO   c                      e Zd Zy)TensorifyScalarRestartAnalysisNr9   r:   r!   r   rQ   rQ      r;   r!   rQ   c                      e Zd Zy)	SkipFrameNr9   r:   r!   r   rS   rS      r;   r!   rS   c                  "     e Zd Zdd fdZ xZS )TorchRuntimeErrorc                    t         |   |       || _        ||| _        y t        j                  j
                  j                         | _        y r&   r'   r(   msgtorch_guardsTracingContextr   
real_stackr+   rX   r\   r.   s      r   r(   zTorchRuntimeError.__init__   J     %  	 --;;= 	r!   r&   rX   strr\   StackSummary | Noner/   r0   r1   r2   r3   r(   r5   r6   s   @r   rU   rU          
 
r!   rU   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+   namer.   s     r   r(   zInvalidBackend.__init__   s    x'de	
r!   )rg   r`   r/   r0   rb   r6   s   @r   re   re      s    
 
r!   re   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+   r.   s    r   r(   zResetRequired.__init__   s!    OO	
r!   r/   r0   rb   r6   s   @r   ri   ri      s    
 
r!   ri   c                  8     e Zd Z	 	 	 	 	 	 	 	 d fdZddZ xZS )ShortenTracebackc               2    t        |   |i | || _        y r&   )r'   r(   first_useful_frame)r+   rq   r,   r-   r.   s       r   r(   zShortenTraceback.__init__   s     	$)&)"4r!   c                   | j                   }| j                  |t        j                  r| S |j                  | j                  ur.|j
                  }|J d       |j                  | j                  ur.| j                  |      S )Nz#internal error, please report a bug)__traceback__rq   r   verbosetb_frametb_nextwith_traceback)r+   tbs     r   remove_dynamo_framesz%ShortenTraceback.remove_dynamo_frames   sz    ""*bjFNNKkk!8!88B>H#HH> kk!8!88 ""2&&r!   )r,   r   rq   types.FrameType | Noner-   r   r/   r0   )r/   ztyping.Self)r1   r2   r3   r(   ry   r5   r6   s   @r   ro   ro      s-    55.D5PS5	5'r!   ro   c                  0     e Zd Z	 	 	 	 	 	 	 	 d fdZ xZS )BackendCompilerFailedc                    t        |dd      | _        || _        d| j                  dt        |      j                   d| }t
        |   ||       y )Nr1   ?zbackend=z	 raised:
z: )rq   )getattrbackend_nameinner_exceptiontyper1   r'   r(   )r+   
backend_fnr   rq   rX   r.   s        r   r(   zBackendCompilerFailed.__init__   s_     $J
C@.**-Z_8M8V8V7WWYZiYjk1CDr!   )r   r   r   	Exceptionrq   rz   r/   r0   rb   r6   s   @r   r|   r|      s9    	E	E #	E 3		E
 
	E 	Er!   r|   c                  V     e Zd Z	 	 dddd	 	 	 	 	 	 	 	 	 	 	 d fdZddZd	d
dZ xZS )UnsupportedN)r   r\   c                   t         |   |       |s(t        j                  j                  j                         }|| _        || _        || _        d | _	        | j                          || _        d| _        y NF)r'   r(   rY   rZ   r[   r   r\   rX   
skip_framecategoryadd_to_statsgb_typelogged)r+   rX   r   r   r   r\   r.   s         r   r(   zUnsupported.__init__   sc     	55CCEJ$$$(#*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   rX   r+   s    r   remove_from_statszUnsupported.remove_from_stats   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_stats   s"     488$)$r!   ) F)rX   r`   r   r`   r   boolr   r@   r\   ra   r/   r0   rm   )unimplemented)r   r`   r/   r0   )r1   r2   r3   r(   r   r   r5   r6   s   @r   r   r      sf    
   !%*. 	
   ( 
*2* *r!   r   c                      e Zd Zy)$UnknownPropertiesDuringBackwardTraceNr9   r:   r!   r   r   r      r;   r!   r   c                      e Zd Zy)RecompileErrorNr9   r:   r!   r   r   r      r;   r!   r   c                      e Zd Zy)InfiniteGeneratorErrorNr9   r:   r!   r   r   r          r!   r   c                      e Zd ZdZy)CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    NrI   r:   r!   r   r   r      s    r!   r   c                  |    e Zd Z e       Z e       Z e       Z e       Z e       Z e       Z	 e       Z
 e       Zy)UserErrorTypeN)r1   r2   r3   r   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAINT_VIOLATIONDYNAMIC_DIMINVALID_INPUTINVALID_OUTPUT*UNSUPPORTED_ALIASED_MUTATED_DYNAMIC_INPUTSr:   r!   r   r   r     s>    66Lv6&KFMVN15.r!   r   c                  2     e Zd Z	 d	 	 	 	 	 	 	 d fdZ xZS )	UserErrorc                @   |<t        |t              sJ |j                  d      r|dz  }n|dz  }|t        |      z  }t        |   |       t        j                  j                  j                         | _
        d| _        d| _        || _        || _        || _        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. 
F)
isinstancer`   endswithr    r'   r(   rY   rZ   r[   r   r\   r   r   
error_typerX   message)r+   r   rX   r   r.   s       r   r(   zUserError.__init__  s      i---||C s
t))44C--66DDF$r!   r&   )r   r   rX   r`   r   r@   r/   r0   rb   r6   s   @r   r   r     s0    KO'.1>H	 r!   r   c                  "     e Zd Zdd fdZ xZS )StepUnsupportedc                    t         |   |       || _        |s(t        j                  j
                  j                         }|| _        d| _        y r   )	r'   r(   rX   rY   rZ   r[   r   r\   r   r]   s      r   r(   zStepUnsupported.__init__-  s@    55CCEJ$r!   r&   r_   rb   r6   s   @r   r   r   ,  s     r!   r   c                      e Zd Zy)UnsafeScriptObjectErrorNr9   r:   r!   r   r   r   6  r;   r!   r   c                  "     e Zd Zdd fdZ xZS )UncapturedHigherOrderOpErrorc                    t         |   |       || _        ||| _        y t        j                  j
                  j                         | _        y r&   rW   r]   s      r   r(   z%UncapturedHigherOrderOpError.__init__;  r^   r!   r&   r_   rb   r6   s   @r   r   r   :  rc   r!   r   c                      e Zd Zy)FailOnRecompileLimitHitNr9   r:   r!   r   r   r   H  r;   r!   r   c                      e Zd Zy)PackageErrorNr9   r:   r!   r   r   r   L  r;   r!   r   c                  4     e Zd Zdd	 	 	 	 	 	 	 d fdZ xZS )ObservedExceptionNr\   c                   t        |   |i | ||| _        y t        j                  j                  j                         | _        y r&   )r'   r(   rY   rZ   r[   r   r\   r+   r\   r,   r-   r.   s       r   r(   zObservedException.__init__R  sJ     	$)&) %  	 --;;= 	r!   r,   r   r\   ra   r-   r   r/   r0   rb   r6   s   @r   r   r   P  s3     =A

&9
LO
	
 
r!   r   c                  @     e Zd ZU ded<   dd	 	 	 	 	 	 	 d fdZ xZS )ObservedUserStopIterationz
Any | NonevalueNr   c               h    t         |   d|       t        |      dkD  r|d   | _        y d | _        y )Nzunhandled `raise StopIteration`r   r   )r'   r(   lenr   r   s       r   r(   z"ObservedUserStopIteration.__init__c  s5     	:zRt9q=aDJDJr!   r   rC   r6   s   @r   r   r   ]  s9    
 =A&9LO	 r!   r   c                      e Zd Zy)ObservedLookupErrorNr9   r:   r!   r   r   r   m  r   r!   r   c                      e Zd Zy)ObservedIndexErrorNr9   r:   r!   r   r   r   r  r   r!   r   c                      e Zd Zy)ObservedKeyErrorNr9   r:   r!   r   r   r   w  r   r!   r   c                      e Zd Zy)ObservedGeneratorExitNr9   r:   r!   r   r   r   |  r;   r!   r   c                      e Zd Zy)ObservedAttributeErrorNr9   r:   r!   r   r   r     r   r!   r   c                      e Zd Zy)ObservedRuntimeErrorNr9   r:   r!   r   r   r     r   r!   r   c                      e Zd Zy)ObservedNotImplementedErrorNr9   r:   r!   r   r   r     r;   r!   r   c                      e Zd Zy)ObservedTypeErrorNr9   r:   r!   r   r   r     r   r!   r   c                    | t         vr3t        | dt        |             }t        d| dt        fi       t         | <   t         |    S )Nr1   ObservedError)observed_exception_mapr   r`   r   r   )exc_typerg   s     r   get_dynamo_observed_exceptionr     sN    --xS];+/tfE"%6$8",
x( "(++r!   )r,   r-   c               z   ddl m} ddlm} |r2|D cg c]&  }t	        |t
              r|j                  ||      n|( }}ng }|j                  ||       j                  |||xs i       }t	        ||      sJ |j                  |       |j                  j                  |       t        |       }	|r |	| |	c c}w )Nr   )ExceptionVals)SourcelessBuilder)symbolic_convertr   variables.builderr   r   r`   createcall_function_attach_traceback_to_exceptionexn_vt_stackset_current_exceptionr   )
r   txr,   r-   r   r   argargs_exception_vt
raised_excs
             r   raise_observed_exceptionr     s     04 
 2<C1E$$R-3N
 

 (* %++B9GG
E6<RL lM222%%l3OO)),7.x8J%  
'
s   +B8c                *    t        t        | |g       y)z:Raise a TypeError as an observed exception during tracing.)r,   N)r   	TypeError)r   rX   s     r   raise_type_errorr     s    Y#7r!   c                8    | j                   j                          y r&   )r   clear_current_exception)r   s    r   handle_observed_exceptionr     s    2 OO++-r!   c                   t        | |      t        j                  j                  dd fd       t        j                  d       t        } |||||| d       y )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%sT)r   contextexplanationhintsfrom_exclog_warning)format_error_msg_verboserY   _loggingtrace_structuredgraph_breaks_logdebugr   )ecoder   r  r  r  _unimplementedr   s          @r   unimplemented_with_warningr    sf      /q$7O	NN##
 + $  41"Nr!   c                
   t        j                  |d      j                         }dj                  d |D              }t        j                  |d      j                         }|  d| d| d| }t	        |       }|r|d| z  }|S )N    r   c              3  j   K   | ]+  }d t        j                  |d      j                         z    - yw)z  Hint: r  N)rk   indentlstrip).0hints     r   	<genexpr>z-format_graph_break_message.<locals>.<genexpr>#  s.      @D
X__T6299;;s   13z
  Explanation: z

  Developer debug context: z:

 For more details about this graph break, please visit: )rk   r  r  joinget_gbid_documentation_link)r   r  r  r  	hints_strrX   documentation_links          r   format_graph_break_messager    s     //+v6==?K		 HM I oogv.557G 		 
} 
 #9(C 5W=MN`MabbJr!   )maxsizec                    	 t        t              j                         j                  } t	        dt        |       d      }t        |      5 }t        j                  |      }ddd       i }j                         D ]  \  }}|D ]
  }|||d   <     |S # 1 sw Y   4xY w# t        $ r t        j                  d       i }Y Yw xY w)z
    Loads the gb_type to gb_id map from the graph break registry from JSON file with caching.

    Includes historical gb_type (mapping behavior of duplicate gb_types with different gb_ids is undefined).
    r   zgraph_break_registry.jsonNz!Error accessing the registry fileGb_type)r   __file__resolveparentr   r`   openjsonloadr   log	exceptionitems)
script_dirregistry_pathfregistrymappingkventrys           r   _load_gb_type_to_gb_id_mapr0  6  s    
(^++-44
'J!<
 -  	$Ayy|H	$ G  *1 	*E()GE)$%	** N	$ 	$ 9:s*   AB 
B B BB  CCc                X    d}t               }| |v r| d||    j                  d       dS y)z
    Retrieves the GBID documentation link for a given graph break type.

    Args:
        gb_type: The graph break type to look up.

    Returns:
        A string containing the documentation URL if found, otherwise None.
    z;https://meta-pytorch.github.io/compile-graph-break-site/gb/gbGBz.htmlN)r0  r  )r   GRAPH_BREAK_SITE_URLgb_type_to_gb_id_maps      r   r  r  Q  sO     	F  67&&#$B';G'D'K'KD'Q&RRWX	
 r!   F)r  r  r   c                *   t        | |||      }|rt        j                  |       |t        urZd}t	        |d      r|j
                  }t        |t              r |j                   d| }t        || ||      t        || ||      |t        || |      )a  
    Called within dynamo to cause a graph break.
    Args:
        gb_type: Context-free graph break type. It should be a short string without any
                 information specific to the tracing context (i.e. no dynamically-generated strings)
        context: Developer context for the graph break. It can contain tracing context/dynamic strings.
        explanation: User-facing context-dependent explanation for the graph break. Can be dynamic.
        hints: List of user-facing hints for the graph break.
    Nr\   zJ

*** While handling this graph break, another graph break occurred: ***

r   )	r  r%  warning_NOTHINGhasattrr\   r   r   rX   )	r   r  r  r  r  r  r   rX   past_real_stacks	            r   r   r   l  s    ( %Wg{E
JCCx8\*&11Oh,\\N"pqtpuvCc7J?SS*
	 c7J
//r!   c                  $    e Zd ZddZddZddZy)KeyErrorMsgc                    || _         y r&   )r   )r+   r   s     r   r(   zKeyErrorMsg.__init__  s	    
r!   c                ,    t        | j                        S r&   )r`   r   r   s    r   __str__zKeyErrorMsg.__str__  s    4::r!   c                "    | j                         S r&   )r?  r   s    r   __repr__zKeyErrorMsg.__repr__  s    ||~r!   N)r   r   r/   r0   )r/   r`   )r1   r2   r3   r(   r?  rA  r:   r!   r   r<  r<    s    r!   r<  c                |    t        | d      r/|j                  d      }|d    d| j                   |d    |d    }|S )N	_hop_namez
  Explanation:r   z
  Higher Order Operator: r      )r9  	partitionrC  )excrX   liness      r   !augment_exc_message_with_hop_namerH    sQ     sK 01Qxj3CMM?58*USTXJW 	 Jr!   c                   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                        dk(  rdnt        | j                  d         }t!        | |      }t#        | t$              r%t'        ||z         f| j                  dd  z   | _        y ||z   }|f| j                  dd  z   | _        y )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\   z
Set TORCHDYNAMO_VERBOSE=1 for the internal stack trace (please do this especially if you're reporting a bug to PyTorch). For even more developer context, set TORCH_LOGS="+dynamo"
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.
r   )	tracebackinnermost_user_frame_summaryget_real_stackr   r  r
   r   replay_record_enabledr9  rK  rt   r   rL  rM  r,   r`   rH  r   KeyErrorr<  )rF  rX   exportrN  r\   old_msgnew_msgs          r   augment_exc_messagerV    s   '+C$$J#j/A"5+5b>(%bggi.C.CJ.O&P%QRR##5F(G01D1D0E F++,E3	

 >>gc<8J	
 s%&7_, 3&&7/0C0C0Q0Q/R S0030C0C0P0P/QQSUC /0C0C0Q0Q/R S_ _C
 MQ&bC,<G/W=G#x #.0388AB<?C-:,r!   c                    d }d }| j                   ,| j                   j                  }| j                   j                  }|| _        ||fS r&   )rO  filenamelineno
compile_id)r  rZ  rX  rY  s       r   get_exc_messager[    sN     HF%%111:://66ALVr!   c                 (    t        t                     S r&   )filter_stackr   r:   r!   r   get_stack_above_dynamor^    s    ((r!   c                    t        | dd       }|y |t               }n
t               }t        j                  ||z         S )Nr\   )r   r^  r   	from_list)rF  framer\   stack_above_dynamos       r   rP  rP    sM     lD1J  45)^!!"4z"ABBr!   c                    t               }| D ]Z  }|j                  d|j                  v r |S d|j                  v s|j                  rd|j                  v rJ|j                  |       \ |S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r   rX  lineappend)stack
user_stackra  s      r   r]  r]    sv    J 	!>>!enn,  5>>)JJ4

B% 	! r!   c                j    ddl m} t        j                  | d|       }|r|j	                  d      S | S )Nr   )TORCH_DYNAMO_RESUME_IN_PREFIXz_(\w+)_at_\d+)resume_executionrk  rematchgroup)rg   rk  rn  s      r   remove_resume_prefixrp    s4    ?HH56oFME{{1~Kr!   c                    t               }| D ]_  }|j                  t        |j                        }|r!|r|d   j                  |k(  r||d<   ||_        H||_        |j	                  |       a |S )a1  
    When we graph break, we create a resume function and make a regular Python call
    to it, which gets intercepted by Dynamo. This behavior is normally shown in the
    traceback, which can be confusing to a user. So we can filter out resume frames
    for better traceback clarity.

    Example:
    File "..." line 3, in f
        <line 3>
    File "..." line 5, in torch_dynamo_resume_in_f_at_80
        <line 5>
    File "..." line 10, in torch_dynamo_resume_in_f_at_120
        <line 10>

    becomes
    File "..." line 10, in f
        <line 10>
    rJ  )r   rX  rp  rg   rg  )rh  	new_stackra  rg   s       r   collapse_resume_framesrs  (  sx    ( I 	$>>!#EJJ/)B-"4"4"<!IbMEJEJU#	$ 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	   rP  r  r
   )rF  r  rK  ra  rX   r\   s         r   r  r  K  s     a(8(8'9@S@S?TTVW  CCC:<CU+J	
 	rww{:.//txJr!   c           	     Z    t        | dd       dt        | dd       dt        | dd       dS )	Nrw  z	<unknown>z (rx  rv  ry  r   ))r   )r  s    r   format_frame_infor|  f  sF    4K0
1 2D-56 7.231	6r!   c                6    | t        |       }d| d| S d| S )Nz6torch.compile intentionally decided to skip the frame z! and fall back to eager.
Reason: zVtorch.compile intentionally decided to skip the frame and fall back to eager.
Reason: )r|  )r  reason
frame_infos      r   format_skip_frame_messager  n  sB    &t,
DZL Qh 	
h 	
r!   c           	         t         j                  rt        | |||      S d|j                   d|j                   d|j
                   dt                S )Nru  r   rv  z 
due to: 
)r   rt   r  rw  rx  ry  r	   )rF  r  rK  ra  s       r   format_error_msgr  |  sW     ~~'T?EJJDLL>4+;+;*< ==8 8r!   )r   r`   r/   r`   )r   type[Exception]r/   ztype[ObservedException])
r   r  r   r   r,   z(list[VariableTracker] | list[str] | Noner-   z!dict[str, VariableTracker] | Noner/   r   )r   r   rX   r`   r/   r   )r   r   r/   r0   )r  r   r  types.CodeTyper   r`   r  r`   r  r`   r  	list[str]r/   r   )
r   r`   r  r`   r  r`   r  r  r/   r`   )r/   zdict[str, Any])r   r`   r/   r@   )r   r`   r  r`   r  r`   r  r  r  r   r  r   r   r   r/   r   )rF  r   rX   r`   r/   r`   )r   F)rF  r   rX   r`   rS  r   r/   r0   )r  r   rZ  r   r/   ztuple[str | None, int | None])r/   r   r&   )rF  r   ra  DynamoFrameType | Noner/   ra   )rh  r   r/   r   )rg   r`   r/   r`   )rh  z!StackSummary | list[FrameSummary]r/   r   )NN)
rF  r   r  r  rK  r@   ra  r  r/   r`   )r  r  r/   r`   )r  ztypes.CodeType | Noner~  r`   r/   r`   )
__future__r   r#  loggingrm  rk   typingenumr   r   	functoolsr   pathlibr   rN  r   r	   r
   r   r   r   r   r   torch._guardsrY   torch._utils_internalr   r   r   utilsr   typestorch._dynamo.variablesr   r   output_graphr   r   r   r   r   r    	getLoggerr1   r%  r  getArtifactLoggerr	  RuntimeErrorr$   r8   r=   r?   rF   rH   rK   rM   rO   rQ   rS   rU   re   ri   ro   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   StopIterationLookupError
IndexErrorGeneratorExitrR  AttributeErrorNotImplementedErrorr   r   r   r   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException$UnsupportedMutationAliasingException!exceptions_allowed_to_be_fallbackr  r  r0  r  objectr8  r   r<  rH  rV  r[  r^  rP  r]  rp  rs  r  r|  r  r  r:   r!   r   <module>r     s]   "2   	      X X / /  1   7'0;9 g!>>33HnM B< B$	3 		!5 	 *  	 	/ / 	/ 		 		_ 		$ 	
, 

) 
	
( 	
'+ '"
E, 
E$*& *B	+? 		) 		1 	
2 8D 8$ :* 	2 	
#7 
	i 		' 	

, 

 1  	+ 	
	, 	
	* 	
	- 		. 	
	, 	
	"3 		) 	 ,$"(*&4 
 , 6:04! 3	
 . B8
.< 
!!>>	!!==	!!>>	!!@@	!!FF% !##
# 	#
 # # # #L  	
 	4 1 40 8 %0%0 %0 	%0
 %0 %0 %0 %0 %0T 	.-b		'	"	)
 59C	C1CCB  L #'$(		
   "	
 	6
" #'$(		8		8
	8  	8 "		8
 		8r!   