o
    ^iLd                     @  s  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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 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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& dddZ'e(e)Z*ej+,e)dZ-G dd de.Z/G dd de/Z0G dd de/Z1G dd de/Z2G dd de2Z3G d d! d!e2Z4G d"d# d#e2Z5G d$d% d%e2Z6G d&d' d'e/Z7G d(d) d)e/Z8G d*d+ d+e/Z9G d,d- d-e/Z:G d.d/ d/e/Z;G d0d1 d1e;Z<G d2d3 d3e/Z=G d4d5 d5e=Z>G d6d7 d7e/Z?G d8d9 d9e=Z@G d:d; d;e=ZAG d<d= d=e=ZBG d>d? d?e=ZCG d@dA dAe@ZDG dBdC dCe
ZEG dDdE dEe=ZFG dFdG dGe/ZGG dHdI dIe=ZHG dJdK dKe/ZIG dLdM dMe/ZJG dNdO dOeKZLG dPdQ dQeKZMG dRdS dSe/ZNG dTdU dUe/ZOG dVdW dWeOZPG dXdY dYeOZQG dZd[ d[eQZRG d\d] d]eQZSG d^d_ d_eOZTG d`da daeOZUG dbdc dceOZVG ddde deeOZWG dfdg dgeOZXeYePeZeQe[eRe\eTe]eSe^eUe.eVe_eWe`eXi	ZaddkdlZbdddmddudvZcddydzZdejejfjgejejfjhejejfjiejejfjjejejfjkfZldddZmen ZoeodddddZpdddZqdddZred	ddddZsdddZteodddddZuG dd dZvddddZwdddZxdddZy	ddddZzdddZ{dddZ|dddZ}		ddddZ~		ddddZdS )    )annotationsN)autoEnum)	lru_cache)Path)extract_stack
format_excformat_listStackSummary)AnyNoReturnOptionalTYPE_CHECKING)get_file_path_2   )config)counters)	CompileId)DynamoTracerOutput)InstructionTranslatorBase)DynamoFrameType	case_namestrreturnc                 C  s   d|  dd S )NzhFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#_-)replacer    r   L/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/torch/_dynamo/exc.pyexportdb_error_message:   s   
r    graph_breaksc                      s   e Zd Zd fddZ  ZS )	TorchDynamoExceptionargsr   kwargsr   Nonec                   s   t  j|i | d | _d S N)super__init___torch_dynamo_tracer_outputselfr#   r$   	__class__r   r   r(   G   s   
zTorchDynamoException.__init__r#   r   r$   r   r   r%   __name__
__module____qualname__r(   __classcell__r   r   r,   r   r"   F       r"   c                   @     e Zd ZdS )InternalTorchDynamoErrorNr0   r1   r2   r   r   r   r   r6   L       r6   c                   @  r5   )ResumePrologueTracingErrorNr7   r   r   r   r   r9   P   r8   r9   c                      s.   e Zd ZU ded< ddd fd	d
Z  ZS )RestartAnalysisOptional[str]restart_reasonN)r<   r#   r   r   r%   c                  s   || _ t j|  d S r&   )r<   r'   r(   )r+   r<   r#   r,   r   r   r(   W   s   zRestartAnalysis.__init__)r#   r   r<   r;   r   r%   r0   r1   r2   __annotations__r(   r3   r   r   r,   r   r:   T   s   
 r:   c                   @  r5   )SpeculationRestartAnalysisNr7   r   r   r   r   r?   \   r8   r?   c                   @  r5   )UnspecializeRestartAnalysisNr7   r   r   r   r   r@   `   r8   r@   c                   @  r5   ) CompileCollectiveRestartAnalysisNr7   r   r   r   r   rA   d   r8   rA   c                   @  r5   )TensorifyScalarRestartAnalysisNr7   r   r   r   r   rB   h   r8   rB   c                   @  r5   )	SkipFrameNr7   r   r   r   r   rC   l   r8   rC   c                   @  r5   )TorchRuntimeErrorNr7   r   r   r   r   rD   p   r8   rD   c                         e Zd Zd fddZ  ZS )InvalidBackendnamer   r   r%   c                   s   t  d|d d S )NzInvalid backend: z=, see `torch._dynamo.list_backends()` for available backends.r'   r(   )r+   rG   r,   r   r   r(   u   s   
zInvalidBackend.__init__)rG   r   r   r%   r/   r   r   r,   r   rF   t   r4   rF   c                      s   e Zd Zd fddZ  ZS )ResetRequiredr   r%   c                   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(   |   s
   zResetRequired.__init__r   r%   r/   r   r   r,   r   rI   {   r4   rI   c                      s(   e Zd Zd fdd	ZdddZ  ZS )ShortenTracebackr#   r   first_useful_frameOptional[types.FrameType]r$   r   r%   c                  s   t  j|i | || _d S r&   )r'   r(   rO   )r+   rO   r#   r$   r,   r   r   r(      s   
zShortenTraceback.__init__typing.Selfc                 C  sZ   | j }| jd u s|d u stjr| S |j| jur(|j}|d us"J d|j| jus| |S )Nz#internal error, please report a bug)__traceback__rO   r   verbosetb_frametb_nextwith_traceback)r+   tbr   r   r   remove_dynamo_frames   s   
z%ShortenTraceback.remove_dynamo_frames)r#   r   rO   rP   r$   r   r   r%   )r   rQ   )r0   r1   r2   r(   rX   r3   r   r   r,   r   rN      s    rN   c                      s   e Zd Zd fd	d
Z  ZS )BackendCompilerFailed
backend_fnr   inner_exception	ExceptionrO   rP   r   r%   c                   sF   t |dd| _|| _d| jdt|j d| }t j||d d S )Nr0   ?zbackend=z	 raised:
z: )rO   )getattrbackend_namer[   typer0   r'   r(   )r+   rZ   r[   rO   msgr,   r   r   r(      s   zBackendCompilerFailed.__init__)rZ   r   r[   r\   rO   rP   r   r%   r/   r   r   r,   r   rY      r4   rY   c                      s:   e Zd Zddd fd	d
ZdddZddddZ  ZS )UnsupportedNr   ra   r   r   r;   r   r%   c                  s8   t  | tjj | _|| _d | _| 	  || _
d S r&   )r'   r(   torch_guardsTracingContextr   
real_stackra   categoryadd_to_statsr   )r+   ra   r   r,   r   r   r(      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   rg   r   ra   rL   r   r   r   remove_from_stats   s
   zUnsupported.remove_from_statsunimplementedrg   c                 C  s    || _ t| | j  d7  < d S )Nr   ri   )r+   rg   r   r   r   rh      s   zUnsupported.add_to_stats)ra   r   r   r;   r   r%   rM   )rk   )rg   r   r   r%   )r0   r1   r2   r(   rj   rh   r3   r   r   r,   r   rb      s    
rb   c                   @  r5   )$UnknownPropertiesDuringBackwardTraceNr7   r   r   r   r   rl      r8   rl   c                   @  r5   )RecompileErrorNr7   r   r   r   r   rm      r8   rm   c                      rE   )ArgsMismatchErrorra   r   r   r%   c                      t  | d S r&   rH   r+   ra   r,   r   r   r(         zArgsMismatchError.__init__ra   r   r   r%   r/   r   r   r,   r   rn      r4   rn   c                      rE   )AttributeMutationErrorra   r   r   r%   c                   ro   r&   rH   rp   r,   r   r   r(      rq   zAttributeMutationError.__init__rr   r/   r   r   r,   r   rs      r4   rs   c                      rE   )InfiniteGeneratorErrorra   r   r   r%   c                   ro   r&   rH   rp   r,   r   r   r(      rq   zInfiniteGeneratorError.__init__rr   r/   r   r   r,   r   rt      s    rt   c                      rE   )SideEffectsErrorra   r   r   r%   c                   ro   r&   rH   rp   r,   r   r   r(      rq   zSideEffectsError.__init__rr   r/   r   r   r,   r   ru      r4   ru   c                      s"   e Zd ZdZd fddZ  ZS )	CondOpArgsMismatchErrorz?
    Internal error from cond() due to arguments mismatch.
    ra   r   r   r%   c                   ro   r&   rH   rp   r,   r   r   r(      rq   z CondOpArgsMismatchError.__init__rr   )r0   r1   r2   __doc__r(   r3   r   r   r,   r   rv      s    rv   c                   @  s<   e Zd Ze Ze Ze Ze Ze Ze Z	e Z
e ZdS )UserErrorTypeN)r0   r1   r2   r   DYNAMIC_CONTROL_FLOWANTI_PATTERNSTANDARD_LIBRARYCONSTRAINT_VIOLATIONDYNAMIC_DIMINVALID_INPUTINVALID_OUTPUT*UNSUPPORTED_ALIASED_MUTATED_DYNAMIC_INPUTSr   r   r   r   rx      s    
rx   c                      s"   e Zd Z	dd fd
dZ  ZS )	UserErrorN
error_typerx   ra   r   r   r;   r   r%   c                   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   ra   r   r,   r   r   r(      s   


zUserError.__init__r&   )r   rx   ra   r   r   r;   r   r%   r/   r   r   r,   r   r      s    r   c                   @  r5   )SkipCodeRecursiveExceptionNr7   r   r   r   r   r     r8   r   c                   @  r5   )RecompileLimitExceededNr7   r   r   r   r   r     r8   r   c                   @  r5   )UnsafeScriptObjectErrorNr7   r   r   r   r   r   
  r8   r   c                      s    e Zd Zd
d fdd	Z  ZS )UncapturedHigherOrderOpErrorNra   r   rf   Optional[StackSummary]r   r%   c                   s6   t  | || _|d ur|| _d S tjj | _d S r&   )r'   r(   ra   rc   rd   re   r   rf   )r+   ra   rf   r,   r   r   r(     s   
z%UncapturedHigherOrderOpError.__init__r&   )ra   r   rf   r   r   r%   r/   r   r   r,   r   r     s    r   c                   @  r5   )IncorrectUsageNr7   r   r   r   r   r     r8   r   c                   @  r5   )FailOnRecompileLimitHitNr7   r   r   r   r   r      r8   r   c                   @  r5   )PackageErrorNr7   r   r   r   r   r   $  r8   r   c                   @  r5   )ObservedExceptionNr7   r   r   r   r   r   (      r   c                      s(   e Zd ZU ded< d
 fdd	Z  ZS )ObservedUserStopIterationzOptional[Any]valuer#   r   r$   r   r%   c                   s0   t  d t|dkr|d | _d S d | _d S )Nzunhandled `raise StopIteration`r   )r'   r(   lenr   r*   r,   r   r   r(   3  s   
z"ObservedUserStopIteration.__init__r.   r=   r   r   r,   r   r   -  s   
 r   c                   @  r5   )ObservedLookupErrorNr7   r   r   r   r   r   ;  r   r   c                   @  r5   )ObservedIndexErrorNr7   r   r   r   r   r   @  r   r   c                   @  r5   )ObservedKeyErrorNr7   r   r   r   r   r   E  r   r   c                   @  r5   )ObservedGeneratorExitNr7   r   r   r   r   r   J  r8   r   c                   @  r5   )ObservedAttributeErrorNr7   r   r   r   r   r   N  r   r   c                   @  r5   )ObservedRuntimeErrorNr7   r   r   r   r   r   S  r   r   c                   @  r5   )ObservedNotImplementedErrorNr7   r   r   r   r   r   X  r8   r   c                   @  r5   )ObservedTypeErrorNr7   r   r   r   r   r   \  r   r   exc_typetype[Exception]type[ObservedException]c                 C  s:   | t vrt| dt| }td| dtfi t | < t |  S )Nr0   ObservedError)observed_exception_mapr^   r   r`   r   )r   rG   r   r   r   get_dynamo_observed_exceptionn  s   r   )r#   r$   txr   r#   Optional[list[Any]]r$   Optional[dict[str, Any]]r   c                C  s:   ddl m} || ||pg |pi }|j| t| )Nr   )BuiltinVariable)	variablesr   call_functionexn_vt_stackset_current_exceptionr   )r   r   r#   r$   r   exception_vtr   r   r   raise_observed_exceptionw  s   r   r   r%   c                 C  s   | j   d S r&   )r   clear_current_exception)r   r   r   r   handle_observed_exception  s   r   er\   codetypes.CodeTypera   c                   sN   t | | tjjddd  fddd td  t| t|| d d S )Nartifactc                   S  
   dddS Ndynamo_graph_break_reasonstring)rG   encodingr   r   r   r   r   <lambda>     z,unimplemented_with_warning.<locals>.<lambda>c                         S r&   r   r   graph_break_msgr   r   r         metadata_fn
payload_fn%s)from_exc)	format_error_msg_verboserc   _loggingtrace_structuredgraph_breaks_logdebuglogwarningrk   )r   r   ra   r   r   r   unimplemented_with_warning  s   



r   )r   r   r   r;   c                C  s8   | t jddksJ |turt| |d|t| |d)NBREAKFr   )osenvironget_NOTHINGrb   )ra   r   r   r   r   r   rk     s   rk   gb_typecontextexplanationhints	list[str]c                   sL   t | | tjjddd  fddd td  t||||| dd d S )	Nr   c                   S  r   r   r   r   r   r   r   r     r   z/unimplemented_v2_with_warning.<locals>.<lambda>c                     r   r&   r   r   r   r   r   r     r   r   r   Tr   log_warning)r   rc   r   r   r   r   unimplemented_v2)r   r   r   r   r   r   r   r   r   unimplemented_v2_with_warning  s   

r   c                 C  sT   t |d }ddd |D }t |d }|  d| d| d| d}|S )N    r   c                 s  s$    | ]}d t |d  V  qdS )z  Hint: r   N)rJ   indentlstrip).0hintr   r   r   	<genexpr>  s    
z-format_graph_break_message.<locals>.<genexpr>z
  Explanation: z

  Developer debug context: )rJ   r   r   join)r   r   r   r   	hints_strra   r   r   r   format_graph_break_message  s   
r   )maxsizedict[str, Any]c            	   
   C  s   z*t t j} tdt| d}t|}t|}W d   n1 s$w   Y  W n t	yD } zt
d| i }W Y d}~nd}~ww i }| D ]\}}|D ]}|||d < qQqK|S )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).
     zgraph_break_registry.jsonNz%Error accessing the registry file: %sGb_type)r   __file__resolveparentr   r   openjsonloadr\   r   erroritems)	
script_dirregistry_pathfregistryr   mappingkventryr   r   r   _load_gb_type_to_gb_id_map  s(   

r   c                 C  s0   d}t  }| |v r| d||  d dS dS )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)r   r   )r   GRAPH_BREAK_SITE_URLgb_type_to_gb_id_mapr   r   r   get_gbid_documentation_link  s   r   Fr   r   boolc                C  sP   t | |||}t| }|r|d| 7 }|rt| |tur$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.
    z9
 For more details about this graph break, please visit: )r   r   r   r   r   rb   )r   r   r   r   r   r   ra   documentation_linkr   r   r   r   6  s   

r   c                   @  s*   e Zd ZdddZddd	Zdd
dZdS )KeyErrorMsgr   r   r   r%   c                 C  s
   || _ d S r&   )r   )r+   r   r   r   r   r(   Z     
zKeyErrorMsg.__init__r   c                 C  s
   t | jS r&   )r   r   rL   r   r   r   __str__]  r  zKeyErrorMsg.__str__c                 C  s   |   S r&   )r  rL   r   r   r   __repr__`  s   zKeyErrorMsg.__repr__N)r   r   r   r%   )r   r   )r0   r1   r2   r(   r  r  r   r   r   r   r   Y  s    

r   r   excexportc                 C  sT  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dkr|dnt| jd }t| trt|| f| jdd   | _d S || }|f| jdd   | _d S )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').
rf   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_enabledhasattrr  rS   r[   r  r	  r#   r   r   KeyErrorr   )r  ra   r  r
  rf   old_msgnew_msgr   r   r   augment_exc_messaged  sB   

 
"r  
compile_idr   #tuple[Optional[str], Optional[int]]c                 C  s0   d }d }| j d ur| j j}| j j}|| _||fS r&   )r  filenamelinenor  )r   r  r  r  r   r   r   get_exc_message  s   
r  r
   c                   C  s
   t t S r&   )filter_stackr   r   r   r   r   get_stack_above_dynamo  r  r  frameOptional[DynamoFrameType]r   c                 C  s<   t | dd }|d u rd S |d urt }nt }t|| S )Nrf   )r^   r  r
   	from_list)r  r  rf   stack_above_dynamor   r   r   r    s   r  stackc                 C  sV   t  }| D ]#}|jd u rqd|jv r |S d|jv s"|jr#d|jv r#q|| q|S )Nconvert_frame
eval_frameztorch._dynamo.optimize()r
   r  lineappend)r  
user_stackr  r   r   r   r    s   



r  rG   c                 C  s0   ddl m} t| d| }|r|dS d S )Nr   )TORCH_DYNAMO_RESUME_IN_PREFIXz_(\w+)_at_\d+)resume_executionr$  rematchgroup)rG   r$  r'  r   r   r   remove_resume_prefix  s
   
r)  c                 C  sZ   t  }| D ]%}|jdu rqt|j}|r%|r%|d j|kr%||d< ||_q|| q|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>
    Nr  )r
   r  r)  rG   r"  )r  	new_stackr  rG   r   r   r   collapse_resume_frames  s   

r+  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  r   r  r  ra   rf   r   r   r   r     s   

r   c                 C  s8   t jr
t| |||S d|j d|j d|j dt  S )Nr,  r   r-  z 
due to: 
)r   rS   r   r.  r/  r0  r   )r  r   r  r  r   r   r   format_error_msg  s   r1  )r   r   r   r   )r   r   r   r   )
r   r   r   r   r#   r   r$   r   r   r   )r   r   r   r%   )r   r\   r   r   ra   r   r   r   )ra   r   r   r   r   r;   r   r   )r   r\   r   r   r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   )r   r   r   r;   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   F)r  r\   ra   r   r  r   r   r%   )r   r\   r  r   r   r  )r   r
   r&   )r  r\   r  r  r   r   )r  r
   r   r
   )rG   r   r   r;   )NN)
r  r\   r   r   r  r;   r  r  r   r   )
__future__r   r   loggingr   r&  rJ   typingenumr   r   	functoolsr   pathlibr   r
  r   r   r	   r
   r   r   r   r   torch._guardsrc   torch._utils_internalr   r   r   utilsr   typesr   output_graphr   symbolic_convertr   r   r    	getLoggerr0   r   r   getArtifactLoggerr   RuntimeErrorr"   r6   r9   r:   r?   r@   rA   rB   rC   rD   rF   rI   rN   rY   rb   rl   rm   rn   rs   rt   ru   rv   rx   r   r   r   r   r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   StopIterationLookupError
IndexErrorGeneratorExitr  AttributeErrorNotImplementedError	TypeErrorr   r   r   r   _subclassesfake_tensorDataDependentOutputExceptionDynamicOutputShapeExceptionUnsupportedOperatorExceptionUnsupportedFakeTensorException$UnsupportedMutationAliasingException!exceptions_allowed_to_be_fallbackr   objectr   rk   r   r   r   r   r   r   r  r  r  r  r  r)  r+  r   r1  r   r   r   r   <module>   s    

	


	
	

#
/

!

	%