
    wi                      U 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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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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Zd dlZ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%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z;m<Z< d dl=Z=d dl>Z=d dl?Z=d dl@mAc mBZC d d	l=mDZD d d
lEmFZFmGZGmHZHmIZI d dlJmKZK d dlLmMZM d dlNmOZOmPZP d dlQmRZR d dlSmTZTmUZUmVZVmWZW d dlXmYZYmZZZ d dl[m\Z\ d dl]m^Z^ d dl_m`Z`maZa d dlbmcZc 	 d dldZen# ef$ r dZeY nw xY w	 d dlgZ=d dlhmiZj d dlNmkZk d dlgmlZl ddlmmnZn eerCeeeejo        eejp        eejq        fZrdesd<   eeejeejo        ejjo        eejp        ejjp        eejq        ejjq        iZtndZri Ztd dlumvZvmwZwmxZx n# ey$ r Y nw xY w e7d          Zze=j{        j|        j}        Z~e=j{        j|        j        Ze=j{        j|        j        Z ej        ej(                  Zdesd<   i Zd esd!<   d"Zd#Zd$e d%Z ej        e          Zi Zd&esd'<    ej        e          Zd(esd)<    ej                    Z G d* d+e	j                  Z G d, d-          Zdgd4Zd adhd6Zdhd7Zd adid:Zdjd;Zdhd<Zd=esd><   dkd?Ze 	 	 	 	 	 dldmdK            Ze3dndodO            Ze3	 dndpdR            ZdqdrdSZej        dhdT            Ze=j        e=j        e=j        fe=j        e=j        e=j        fe=j        e=j        e=j        fe=j        e=j        fe=j        e=j        fe=j        e=j        fe=j        e=j        e=j        fe=j        e=j        e=j        fe=j        e=j        e=j        fe=j        e=j        fi
Z G dU dV          Z e            ZdW ZdhdXZdY ZdZ Zdsd[Zdtd]Zdud`ZÐdvdbZdc Zdd Z G de df          Ze3dwdl            Ze3dxdn            Ze3dydp            Zdq Zej        drk    r&ej        ej        ej        ej7        ej        ej        fZds Zdt Zdu Zdv Zdw Zdx Zdy Zdz Zd{ Zd| Zd} Zd~ Z e
j"        d          d             ZܐdzdZd Zސd{dZߐd{dZdZej         G d d                      ZdZ ej        e䬌          adesd<   d|dZd}dZd~dZ eMe鬚          ZddZdhdZddZ G d d          Zdadesd<   ddZe 	 	 ddd            Zej         G d d                      Z G d deǦ          Z e            e_        d ZdddZd ZddZe d             Zd ZdndZd Zd Zd Z e
j"        d          dd            Zd Z ddZd Z e"d          d             Zeeeeee ed          ej	        e
j	        ej        e=j        e=j        e=j        e=j        e=j        e=j        e=j{        j        j        e=j        j        hZdesd<    ea            r*d dlZe                    ej        j                   	 d ZdÄ ZdĄ Zdń ZdƄ ZdǄ ZdȄ Z  ei !                                          Z"desd<    ei #                                          Z$desd<    e ej%                    #                                          Z&desd<    e e'd                    Z(desd<    e e' e)d                               Z*desd<   e(j+        Z,e-j.        Z/dф Z0d҄ Z1dӄ Z2e3Z4ddՄZ5dք Z6dׄ Z7d؄ Z8dل Z9dڄ Z:dndۄZ;d܄ Z<d݄ Z=dsdބZ>dsd߄Z?dZ@d dlAmBZB d ZCd ZDd ZEd ZFdd@dd@dd@d@ejG        d@f	dZHd ZIej         d             ZJ eǦ            ZK ej        eL          ZMdesd<   g ZNdesd<    eǦ            ZO e
j"        d          d             ZPd ZQddZRd ZSd ZTdndZU ejV                    ZWd ZXe d             ZYd ZZd Z[d Z\ddZ]d Z^ddZ_ddZ`ddZadnddZb G d de	j                  Zcdd
ZdddZed Zfd ZgddgZhddgZig dZjeheiz   ejz   Zkd Zl	 	 	 ddZm	 	 	 ddZnd Zod Zp G d d          Zqd  Zr G d! d"          Zs G d# d$          Ztd% Zud& Zvd' Zwd( Zxd) Zyd* Zzdd,Z{ej         G d- d.                      Z|dd1Z}dd6Z~d7 Zd8 Zd9 Zd: Zdd=Zd> Zd? Zd@ Zej         ddA            ZdB ZdC Z G dD dEe=j{        j|                  ZddHZdI ZdJ Z G dK dL          Z e            ZdMesdN<   ddOZdP Zej         dQ             ZdR ZdS ZdT ZdU ZdV ZdW ZdX ZdY ZdZ Zd[ Zd\ Zd] Zi Zd^esd_<   d` Zda Z G db dc          ZddfZdS (      )annotationsN)contextmanager)is_dataclass)	lru_cache)MethodWrapperType)AnyCallablecastClassVarCounterDefaultDictDequeDict	GeneratorGenericIterableIteratorKeysViewListOptionaloverloadSetTupleTypeTypeVarUnion
ValuesView)LiteralTypeIs)fx)_instruction_counter_len_torch_function_stack_pop_torch_function_stack_push_on_torch_function_stack)enable_python_dispatcher)MetricsContext)SourceTracingContext)is_sparse_compressed)log_chromium_event_internallog_compilation_eventrecord_chromium_event_internalsignpost_event)_format_graph_codelazy_format_graph_code)_WaitCounter)LazyModuleMixin)
has_tritonhas_triton_package)RemovableHandle)detect_fake_mode
LazyString   )configzTuple[types.ModuleType, ...]NP_SUPPORTED_MODULES )
FakeTensoris_fakemaybe_get_fake_modeTzDefaultDict[str, Counter[str]]countersDict[str, Any]optimus_scuba_logzAhttps://pytorch.org/docs/main/torch.compiler_troubleshooting.htmlz;https://pytorch.org/docs/main/torch.compiler_nn_module.htmlzSee z& for more information and limitations.zDict[str, List[float]]compilation_time_metricsDict[str, float]cumulative_time_spent_nsc                      e Zd ZdZdZdZdS )ReInplaceTriggerr8         N)__name__
__module____qualname__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPSr;       c/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/torch/_dynamo/utils.pyrG   rG      s        LLJJJrP   rG   c                      e Zd ZU  ej        e          Zded<   edd            Z	edd	            Z
ed
             Zed             Zed             Zed             ZdS )ReinplaceCounterszDefaultDict[str, int]_valuestriggerrG   bytesintc                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_bytes_)rT   name)clsrU   rV   s      rQ   add_missed_bytesz"ReinplaceCounters.add_missed_bytes   sA    A::K6667775@77777 :rP   countc                F    |dk    r| j         d| xx         |z  cc<   d S d S Nr   missed_tensors_)rT   )r\   rU   r^   s      rQ   add_missed_opportunitiesz*ReinplaceCounters.add_missed_opportunities   s?    A::K3'33444=44444 :rP   c                8    | j                                          d S N)rT   clearr\   s    rQ   re   zReinplaceCounters.clear   s    rP   c                `    d}t           D ]#}|| j                            d| d          z  }$|S r`   )rG   rT   getr\   sumrU   s      rQ   get_total_missedz"ReinplaceCounters.get_total_missed   sB    ' 	C 	CG3;??#>W#>#>BBBCC
rP   c                j    d}t           D ](}|| j                            d|j         d          z  })|S rY   )rG   rT   rh   r[   ri   s      rQ   get_total_missed_bytesz(ReinplaceCounters.get_total_missed_bytes   sD    ' 	F 	FG3;??#A7<#A#A1EEECC
rP   c                D    | j         rt          dd| j                    d S d S )Ninductorreinplace_counters)rT   r-   rf   s    rQ   logzReinplaceCounters.log   s6     ; 	J:';S[IIIII	J 	JrP   N)rU   rG   rV   rW   )rU   rG   r^   rW   )rJ   rK   rL   collectionsdefaultdictrW   rT   __annotations__classmethodr]   rb   re   rk   rm   rq   r;   rP   rQ   rS   rS      s         %<[%<S%A%AGAAAA A A A [A
 > > > [>   [   [   [ J J [J J JrP   rS   rows3Union[List[Tuple[str, object]], List[List[object]]]headers!Union[Tuple[str, ...], List[str]]returnstrc                    	 dd l }|                     | |          S # t          $ r6 d                    d t          j        |g|           D                       cY S w xY w)Nr   rx   
c              3  f   K   | ],}d                      t          t          |                    V  -dS ), N)joinmapr{   ).0rows     rQ   	<genexpr>ztabulate.<locals>.<genexpr>   sG       
 
),DIIc#smm$$
 
 
 
 
 
rP   )tabulateImportErrorr   	itertoolschain)rv   rx   r   s      rQ   r   r      s    
  w 777 
 
 
yy 
 
09	40P0P
 
 
 
 
 	
 	
 	

s    =AANonec                     t           dz   a d S Nr8   )
curr_framer;   rP   rQ   increment_framer      s    aJJJrP   c                 n    t                                            t                                           dad S Nr   )rE   re   rC   r   r;   rP   rQ   reset_frame_countr      s/    ""$$$""$$$JJJrP   cntrW   c                    t           | z  a d S rd   )op_count)r   s    rQ   increment_op_countr      s    OHHHrP   c                     i } t                                           D ]\  }}|dz  | |<   |                     dd          |                     dd          z   | d<   | S )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rE   itemsrh   )total_by_keyphasetimings      rQ   calculate_time_spentr      sx    L17799 + +v$slU&2&6&6' '2A66'7L"# rP   c            	         t                      } d}|                                 D ]\  }}| d| dt          |d           }t          |           d S )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       rQ   print_time_reportr     se    '))L
C"((** / /
U..s..U5!__..	#JJJJJrP   r&   _METRICS_CONTEXTc                     t           S rd   )r   r;   rP   rQ   get_metrics_contextr   %  s    rP   Fr   
phase_nameOptional[str]log_pt2_compile_eventboolmetadataOptional[Dict[str, object]]dynamo_compile_column_uslog_waitcounterGenerator[Any, None, None]c           	   #    K   ||                     d          sJ |r|}| }n| }d}| t          vr
g t          | <   i }|r|                    |           |r|                    d|i           t                      }	t	          j                    }
|	                    ||
||           	 t          j        	                    |  d          5  |rAt          d|                                            5  dV  ddd           n# 1 swxY w Y   ndV  ddd           n# 1 swxY w Y   t	          j                    }||
z
  }t          |                              |dz             |	                    ||i |
|           |rRt                      }|                                r|                    ||dz             t"          |xx         |z  cc<   dS dS # t	          j                    }||
z
  }t          |                              |dz             |	                    ||i |
|           |rPt                      }|                                r|                    ||dz             t"          |xx         |z  cc<   w xY w)a  
    dynamo_timed is a context manager
    By wrapping a function in dynamo_timed, we can get a few things:

    1) Log timings to pt2_compile_events.
    2) Log timings to CompilationMetrics (dynamo_compile).
    3) Chromium events.
    4) Storing a record in compilation_time_metrics
       For example:

        def _foo(...):
            with dynamo_timed("_foo"):
                ...

        Would show up as an entry in our timing dict:
        OrderedDict([('_foo', [0.083690, 0.23949, 3.1425e-05])])
        This is extremely useful for granular debugging.

    Although it is tempting to use dynamo_timed as a decorator, please do not.
    In its decorator form it makes cProfile traces less useful as dynamo_timed
    suddenly becomes a bottleneck for lots of function calls (as only one parent
    pointer is recorded).

    Params:
    - key: key into compile_time_metrics. If phase_name is not provided, this is
      also the event name used for pt2_compile_events logs and chromium events.
    - phase_name: Optional override for the event name.
    - log_pt2_compile_event: Whether to log a pt2 compile event internally.
    - metadata: Extra metadata to put in pt2_compile_events.
    - dynamo_compile_column_us: If provided, updates the specified CompilationMetrics
      field to be logged to dyname_compile column. We expect all columns to be _us;
      therefore, the field name must end with "_us".
    - log_waitcounter: If set, we'll log a waitcounter of the form "pytorch.dynamo_timed.{key}"
    N_usfn_namez (dynamo_timed)zpytorch.dynamo_timed.r     )endswithrC   updateget_chromium_event_loggertimetime_nslog_event_starttorchprofilerrecord_functionr0   guardappendlog_event_endr   in_progress	incrementrE   )r   r   r   r   r   r   
event_namer   event_metadatachromium_logstart_nsend_nstime_spent_nsmetrics_contexts                 rQ   dynamo_timedr   )  s     Z  +'0077777 


***(* %N (h''' 4y'2333(A(C(CL|~~H  Hn.C  B^++s,C,C,CDD 	 	 !"?#"?"?@@FFHH  EEE               	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ) %,,]S-@AAA""H.C	
 	
 	
 $ 	B133O**,, )),mt.C   %Z000MA00000	B 	B ) %,,]S-@AAA""H.C	
 	
 	
 $ 	B133O**,, )),mt.C   %Z000MA0000000sU   &"G 'D/D 4D D	DD	DG D  G #D $G B(I9reprLiteral['str']	aggregatec                    d S rd   r;   r   r   s     rQ   compile_timesr         CrP   Literal['csv']Tuple[List[str], List[object]]c                    d S rd   r;   r   s     rQ   r   r     	     CrP   c                &   d ffd	| dk    r+fdt           D             }d}|t          |d          z  }|S | dk    rOfd	t                                           D             }t          t                                                     }||fS d
S )a  
    Get metrics about torchdynamo frontend/backend compilation times.

    Accumulates information from functions tagged with `dynamo_timed`.

    repr='str' returns a printable string for user interaction, and 'csv'
    returns headers, rows which can be logged for output

    aggregate causes values from multiple compilations (e.g. split graphs)
    to be accumulated into one value.  If false, expect more than one value
    per metric.
    c                    | S rd   r;   xs    rQ   <lambda>zcompile_times.<locals>.<lambda>  s     rP   c                ~    r |t          |                     S d                    t          ||                     S )Nr   )rj   r   r   )valuesitem_fnr   s     rQ   fmt_fnzcompile_times.<locals>.fmt_fn  s=     	(73v;;'''yyWf--...rP   r{   c                F    g | ]}| t           |         d            fS )c                
    | dS )Nz.4fr;   r   s    rQ   r   z*compile_times.<locals>.<listcomp>.<lambda>  s
    jj rP   r   )rC   )r   kr   s     rQ   
<listcomp>z!compile_times.<locals>.<listcomp>  sG     
 
 
 /2<P<PQQQR
 
 
rP   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r}   csvc                ,    g | ]} |d            S )c                
    | dS )Nz.6fr;   r   s    rQ   r   z*compile_times.<locals>.<listcomp>.<lambda>  s
    1

 rP   r   r;   )r   vr   s     rQ   r   z!compile_times.<locals>.<listcomp>  s<     
 
 
 F122333
 
 
rP   N)rC   r   r   listkeys)r   r   rv   r   r   rx   r   s    `    @rQ   r   r     s      +{ / / / / / /
 u}}
 
 
 
-
 
 
 3x&BCCCC
	
 
 
 
-4466
 
 
 /4466774rP   c                 X    t                               t          dd                     d S )Nr{   Tr   )rq   infor   r;   rP   rQ   dump_compile_timesr     s'    HH]66677777rP   c                  &    e Zd ZdddZd ZddZdS )DuplicateWarningChecker   maxsizerW   rz   r   c                <    || _         |                                  d S rd   )r   reset)selfr   s     rQ   __init__z DuplicateWarningChecker.__init__  s    

rP   c                6    t          j                    | _        d S rd   )rr   OrderedDictsetr   s    rQ   r   zDuplicateWarningChecker.reset  s    *,,rP   r   !Union[str, Tuple[object, object]]r   c                ,   || j         v r+| j                             |d           t          j        sdS n_d | j         |<   t	          | j                   | j        k    r8| j                             d           t	          | j                   | j        k    8dS )NT)lastF)r   move_to_endr9   verboselenr   popitemr   r   s     rQ   addzDuplicateWarningChecker.add  s    $(??H  4 000> u !DHSMdh--$,..  e ,,, dh--$,..trP   N)r   )r   rW   rz   r   )r   r   rz   r   )rJ   rK   rL   r   r   r  r;   rP   rQ   r   r     sP            - - -	 	 	 	 	 	rP   r   c                     t           j                            dd          dk    } | rt                      S t	          j                    S )NTORCH_COMPILE_DEBUG01)osenvironrh   add_file_handler
contextlib	ExitStack)compile_debugs    rQ   setup_compile_debugr    s@    JNN#8#>>#EM "!!!!!!rP   c                 8    t                                            d S rd   )graph_break_dup_warning_checkerr   r;   rP   rQ   reset_graph_break_dup_checkerr    s    #))+++++rP   c                    t           j                            t                      d          } t          j        | d           t          j        t           j                            | d                    t          j        d                                         t          j
                    }|                    fd           |S )NtorchdynamoTexist_okz	debug.logztorch._dynamoc                 .                                    S rd   removeHandlerlog_file_handlerloggers   rQ   r   z"add_file_handler.<locals>.<lambda>  s    v334DEE rP   )r  pathr   get_debug_dirmakedirsloggingFileHandler	getLogger
addHandlerr  r  callback)log_path	exitstackr  r  s     @@rQ   r  r    s    w||MOO];;HK4((((*27<<++N+NOO//F
&'''$&&IEEEEEFFFrP   c                 0   t          j                    } t          j        tt	          j        t          j                  t          j        j        	                                D ]0
                               |                     fd           1| S | S )Nc                 .                                    S rd   r  r  s   rQ   r   z setup_log_file.<locals>.<lambda>  s    v';';<L'M'M rP   )r  r  r9   log_file_namer#  r$  r   _logging	_internalget_loggersr&  r'  )r)  r  r  s    @@rQ   setup_log_filer0     s    $&&I'".v/CDDn.::<< 	O 	OF.///MMMMMNNNNrP   c                n    t                       d|j         dt          |           j         d|j         dS )Nz/error_recordings/_z.rec)r!  co_nametyperJ   co_firstlineno)exccodes     rQ   gen_record_file_namer8    sR    oo ? ?? ?S		"? ?%)%8? ? ? ?rP   filenamec                   	 t           j                            |           rt                              d|            d S t          j        t           j                            |           d           t          | d          5 }|                    |           d d d            d S # 1 swxY w Y   d S # t          $ r t          
                    d|            Y d S w xY w)Nz9Unable to write execution record %s; file already exists.Tr  wbz#Unable to write execution record %s)r  r   existsrq   warningr"  dirnameopendump	Exception	exception)r9  exec_recordfs      rQ   write_record_to_filerE    s*   
G7>>(## 	$KKKX     K11DAAAAh%% $  ###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ G G G;XFFFFFFGs;   :B1 AB1 B$B1 $B((B1 +B(,B1 1%CCgfx.Graphc                :    d}| j         D ]}d|j        v r|dz  }|S )Nr   callr8   )nodesop)rF  cns      rQ   count_callsrN    s2    	AW  QT>>FAHrP   r   c                    | S rd   r;   r   s    rQ   identityrP  '  s    HrP   c                `    	 t          |            dS # t          $ r Y dS t          $ r Y dS w xY w)NTF)hash	TypeError
ValueErrorr   s    rQ   hashablerU  +  sR    Qt   uu   uus    
-	--c                     d S rd   r;   argskwargss     rQ   nothingrZ  6  s    DrP   c                  >    e Zd ZdZd Zd Zd
dZd Zd Zd Z	d	 Z
dS )ExactWeakKeyDictionaryz\Similar to weakref.WeakKeyDictionary, but use `is`/`id` rather than `==` to compare equalityc                "    i | _         i | _        d S rd   r   refsr   s    rQ   r   zExactWeakKeyDictionary.__init__=  s    			rP   c                6    | j         t          |                   S rd   )r   idr  s     rQ   __getitem__z"ExactWeakKeyDictionary.__getitem__A  s    {2c77##rP   Nc                R    | j                             t          |          |          S rd   )r   rh   ra  )r   r   defaults      rQ   rh   zExactWeakKeyDictionary.getD  s    {r#ww000rP   c                .    t          |          | j        v S rd   )ra  r   r  s     rQ   __contains__z#ExactWeakKeyDictionary.__contains__G  s    #ww$+%%rP   c                     t          |           j        vr!t          j        | fd           j        <   | j        <   d S )Nc                .                                   S rd   )
_remove_id)refidxr   s    rQ   r   z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>M  s    $//#:N:N rP   )ra  r_  weakrefrj  r   )r   r   r   rk  s   `  @rQ   __setitem__z"ExactWeakKeyDictionary.__setitem__J  sR    ggdi$[.N.N.N.N.NOODIcN CrP   c                N    || j         v r| j         |= || j        v r
| j        |= d S d S rd   r^  )r   rk  s     rQ   ri  z!ExactWeakKeyDictionary._remove_idP  s;    $+C $)	# rP   c                j    | j                                          | j                                         d S rd   )r_  re   r   r   s    rQ   re   zExactWeakKeyDictionary.clearV  s.    	rP   rd   )rJ   rK   rL   __doc__r   rb  rh   rf  rm  ri  re   r;   rP   rQ   r\  r\  :  s        ff  $ $ $1 1 1 1& & &! ! !      rP   r\  objobjectallowed_typesType[T]	TypeIs[T]c                    d S rd   r;   rq  rs  s     rQ   istyperx  [  r   rP   )Tuple[Type[List[T]], Type[Tuple[T, ...]]]c                    d S rd   r;   rw  s     rQ   rx  rx  `  r   rP   Iterable[type]c                    d S rd   r;   rw  s     rQ   rx  rx  g  r   rP   c                    t          |t          t          t          f          rt	          |           |v S t	          |           |u S )zisinstance() without subclasses)
isinstancetupler   r   r4  rw  s     rQ   rx  rx  l  s=    -%s!344 *CyyM))99%%rP   rI      c                    t           j        dk    rt          | t                    rdS t          | t          j                  p| t          j        u S )Nr  T)sysversion_infor~  _builtin_final_typing_classestyping_Finalr   r   s    rQ   	is_typingr    sE     7""z%9V'W'W"teV]++Fu/FFrP   c                    t           sdS t          | t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j	        f          S NF)
nprx  int8int16int32int64uint8uint16uint32uint64r  s    rQ   is_numpy_int_typer    sQ     uGHHHHIII		
  rP   c                v    t           sdS t          | t           j        t           j        t           j        f          S r  )r  rx  float16float32float64r  s    rQ   is_numpy_float_typer    s8     uJJJ	
  rP   c                z    t          | t          j                  o!t          t	          j        | d                    S )N__wrapped__)r~  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr  s    rQ   is_lru_cache_wrapped_functionr    s:    eY9:: {um44@ @ rP   c                    t          |           p/t          | t          j        j        t          j        j        f          S rd   )r  r~  r   _opsOpOverloadPacket
OpOverloadr  s    rQ   is_function_or_wrapperr    s7    u 
+UZ-BC" " rP   c                z    t          | t          j        t          j        t          j        t          j        f          S rd   )r~  typesFunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper  s    rQ   r  r    s4    %&'		
  rP   c                    t          | t          j        t          j        t          j        t          j        t          j        f          S rd   )r~  r  GetSetDescriptorTyper  r  MemberDescriptorTyper   r  s    rQ   is_wrapper_or_member_descriptorr    s<     &&'&#	
  rP   c                ,    t          |           d         S r   ) unwrap_with_attr_name_if_wrapper)fns    rQ   unwrap_if_wrapperr    s    +B//22rP   c                    t          |           r/t          j        | dd          rt          j        | d|           } d}nd }| |fS )N_torchdynamo_inlineF)r  r  r  )r  	attr_names     rQ   r  r    sT     2 71"6KUSS #B(=rBB)			y=rP   c                H    t           sdS t          | t           j                  S r  )r  rx  ndarrayr  s    rQ   is_numpy_ndarrayr    s!     u%$$$rP   c                    t           j        t           j        j        gt          j        R }|t           j        j        fz   }t          | |          S )zCheck of obj is a tensor)	r   Tensornn	Parameterr9   traceable_tensor_subclasses_subclassesr<   rx  )rq  tensor_lists     rQ   istensorr    sO     	% 
	+% %K
 !2!= ??K#{###rP   c                ,    t          | t                    S rd   )r~  r1   )mods    rQ   is_lazy_moduler    s    c?+++rP   r   c                     t          |   d S rd   )r   rX  s    rQ   
print_oncer    s    	4LLLLrP   c                n    | fd}|j         t          |j                   dk    sJ |j         d         S )zNSome black magic to create a cell object that usually only exists in a closurec                      S rd   r;   r   s   rQ   rD  zmake_cell.<locals>.f  s    rP   Nr8   r   )__closure__r  )valrD  r   s     @rQ   	make_cellr    sR    A     =$Q]););q)@)@)@@=rP   c                2   	 t          d | D                       }d |                                D             }||fS # t          $ rO}ddlm} ddlm}  |d ||   d |t          |                                            |           Y d }~d S d }~ww xY w)	Nc              3  >   K   | ]}|                                 V  d S rd   as_proxyr   args     rQ   r   z$proxy_args_kwargs.<locals>.<genexpr>  s*      ::c3<<>>::::::rP   c                >    i | ]\  }}||                                 S r;   r  )r   r   r  s      rQ   
<dictcomp>z%proxy_args_kwargs.<locals>.<dictcomp>  s&    KKKSS\\^^KKKrP   r8   unimplemented)typestrzcall_function args: r   from_exc)	r  r   NotImplementedErrorr6  r  variables.baser  r   r   )rX  rY  
proxy_argsproxy_kwargser  r  s          rQ   proxy_args_kwargsr    s    
::T:::::
KKFLLNNKKK<'' 
 
 
&&&&&&++++++U77D>UUGGT&--//=R=R4SUU	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	
s   := 
BABBr   Optional[float]Optional[int]c                .    | d nt          | dz            S Nr   rW   r   s    rQ   	to_int_msr    s    944#a$h--/rP   c                .    | d nt          | dz            S )Ni@B r  r  s    rQ   	to_int_usr  #  s    944#a)m"4"44rP   rI   c                     e Zd ZU dZded<   dZded<   dZded<   dZded<   dZded<   dZ	ded	<   dZ
ded
<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded <   dZded!<   dZded"<   dZ ded#<   dZ!ded$<   dZ"ded%<   dZ#ded&<   dZ$ded'<   dZ%ded(<   dZ&ded)<   dZ'ded*<   dZ(ded+<   dZ)ded,<   dZ*ded-<   dZ+ded.<   dZ,ded/<   dZ-ded0<   dZ.ded1<   dZ/ded2<   dZ0ded3<   dZ1ded4<   dZ2ded5<   dZ3ded6<   dZ4ded7<   dZ5ded8<   dZ6ded9<   dZ7ded:<   dZ8ded;<   dZ9ded<<   dZ:ded=<   dZ;ded><   e<Z=d?ed@<   dZ>dedA<   dZ?dedB<   dZ@dedC<   dZAdedD<   dZBdedE<   dZCdedF<   dZDdedG<   dZEdedH<   dZFdedI<   dZGdJedK<   dS )LCompilationMetricsNr   
compile_id	frame_keyr3  co_filenamer  r5  
cache_sizeaccumulated_cache_sizeguard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_countr  
start_timeentire_frame_compile_time_sbackend_compile_time_sinductor_compile_time_scode_gen_time_s	fail_typefail_reasonfail_user_frame_filenamefail_user_frame_linenozOptional[Set[str]]non_compliant_opscompliant_custom_opsrestart_reasonsdynamo_time_before_restart_szOptional[bool]has_guarded_coderemote_cache_time_saved_sstructured_logging_overhead_sconfig_suppress_errors config_inline_inbuilt_nn_modulesspecialize_floatdynamo_config
is_forwardnum_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usduration_us!dynamo_cumulative_compile_time_us'aot_autograd_cumulative_compile_time_us#inductor_cumulative_compile_time_us,inductor_code_gen_cumulative_compile_time_ustriton_compile_time_usruntime_cudagraphify_time_usruntime_triton_autotune_time_us%dynamo_compile_time_before_restart_uscuda_synchronize_time_us distributed_ephemeral_timeout_usstructured_logging_overhead_us!remote_fx_graph_cache_get_time_us!remote_fx_graph_cache_put_time_us#backward_cumulative_compile_time_usend_time_uspre_grad_pass_time_uspost_grad_pass_time_usjoint_graph_pass_time_usrW   log_format_versioninductor_configremote_cache_version"inductor_fx_remote_cache_hit_count#inductor_fx_remote_cache_miss_count%inductor_fx_remote_cache_backend_type!inductor_fx_remote_cache_hit_keys"inductor_fx_remote_cache_miss_keyscuda_versiontriton_versionzOptional[dict[str, bool]]feature_usage)HrJ   rK   rL   r  rt   r  r3  r  r5  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"  LOG_FORMAT_VERSIONr#  r$  r%  r&  r'  r(  r)  r*  r+  r,  r-  r;   rP   rQ   r  r  ,  s         $J$$$$#I####!G!!!!!%K%%%%$(N(((( $J$$$$,00000!%K%%%%+/////$(N((((&*****'+++++"&J&&&&377777.22222/33333'+O++++#I####!%K%%%%.22222,00000,00000/33333*.O....48 8888 (,++++15555559!9999-111117;$;;;;'+++++#'M''''!%J%%%%(,,,,,7;%;;;;7;%;;;;#'M''''!%K%%%%7;%;;;;=A+AAAA9='====BF0FFFF,0000026 666659#9999;?)????.222226:$::::48"88887;%;;;;7;%;;;;9='====!%K%%%%+/////,00000.2222200000%)O))))*.....8<&<<<<9='====;?)????7;%;;;;8<&<<<<"&L&&&&$(N((((/3M333333rP   r  @   maxlenzDeque[CompilationMetrics]_compilation_metricsrL  c                   t                      }|                                }|sd S |                    || j        | j        | j        | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j        | j        | j        | j        t'          | j                  nd | j        t'          | j                  nd | j        t'          | j                  nd | j        | j        | j                   d S )N)r   r  r3  r  r5  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r
  )r   get_topadd_event_datar  r3  r  r5  r  r  r  r  r  r  r  r  r  r  r  r   r   r  r  r  r  r
  )rL  event_loggerr   s      rQ   #add_compilation_metrics_to_chromiumr7  ~  s   ,..L%%''J +	M'< 7M5'+-+M!"!; 7 * q2333!- "!"8999( Q.///%&%C+o;       rP   c                 0    G d dt           j                  } d}t                      }d}t          j        j        r#t          j        j                                        nd}|	 |                                D ]k\  }}t          |t                    rt          j        ||          r|                    |           t          |t                    rt          |          ||<   l|D ]}||= t          j        || d          }n# t          $ r d}Y nw xY w|S )zN
    Method to parse and scrub uninteresting configs from inductor config
    c                       e Zd Z fdZ xZS )A_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializerc                j    	 t                                          |          S # t          $ r Y dS w xY w)NzValue is not JSON serializable)superrd  rA  )r   o	__class__s     rQ   rd  zI_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializer.default  sB    8wwq))) 8 8 87778s    $ 
22)rJ   rK   rL   rd  __classcell__r>  s   @rQ   TypeSafeSerializerr:    s8        	8 	8 	8 	8 	8 	8 	8 	8 	8rP   rA  zV((^TYPE_CHECKING$)|(.*_progress$)|(.*TESTING.*)|(.*(rocm|halide).*)|(^trace\..*)|(^_))NT)r\   skipkeysz(Inductor Config is not JSON serializable)jsonJSONEncoderr   r   	_inductorr9   get_config_copyr   r~  r{   researchr  r   dumpsrA  )rA  configs_to_scrub_rekeys_to_scrubinductor_conf_strinductor_config_copyr   r  s          rQ   %_scrubbed_inductor_config_for_loggingrN    sl   8 8 8 8 8T- 8 8 8 tEEM49O4JT..000PT  '	K06688 : :S!#s++ +ry9Lc/R/R +!%%c***c3'' :04S		(-$ . .(-- $
$*<t! ! !  	K 	K 	K J	K s   $BD DDstart_time_nsend_time_nsmetricsexc_typeOptional[Type[BaseException]]	exc_valueOptional[BaseException]c                d   fd}fd}dfd}t           j                                        }t           j        j                                        rH	 dd	lm}	m}
 |
}|		                                }t          |          j        }n# t          $ r d }d }Y nw xY wd }d }t          t           j        j                                                  | d
z  |d
z  || z
  d
z  |r|j        nd |rt          |          nd t%          |          t'                      t           j        j        t-                      rt.          j        nd |d           |d          ||d}| dz   |d           |d           |d           |d           |d           |d           |d          |d	}t3          di i ||t4                                         j        rd}nd}t           j                            |fdd           t=                     t>          j         rtC                     d S d S )Nc                B                         | d           }||dz  nd S )Ng    .Arh   fieldmetricrQ  s     rQ   us_to_sz+record_compilation_metrics.<locals>.us_to_s  s)    UD))%1v||t;rP   c                B                         | d           }||dz  nd S r  rX  rY  s     rQ   us_to_msz,record_compilation_metrics.<locals>.us_to_ms  s)    UD))!'!3v~~=rP   rZ  r{   rz   r   c                    dd                     | d           }|d S | = t          |t                    st          |t                    sd S d                    fd|D                       S )	Nitemr   rz   r{   c                `    	 t          |           S # t          $ r t          d           cY S w xY wrd   )r{   rA  )r`  s    rQ   safe_strzPrecord_compilation_metrics.<locals>._convert_collection_to_str.<locals>.safe_str  s@    !4yy  ! ! !4yy   !s    --,c              3  .   K   | ]} |          V  d S rd   r;   )r   r`  rb  s     rQ   r   zQrecord_compilation_metrics.<locals>._convert_collection_to_str.<locals>.<genexpr>  s+      ::4::::::rP   )r`  r   rz   r{   )rh   r~  r   r   r   )rZ  r[  rb  rQ  s     @rQ   _convert_collection_to_strz>record_compilation_metrics.<locals>._convert_collection_to_str  s    	! 	! 	! 	! UD))>4 EN&#&& 	z&$/G/G 	4xx::::6::::::rP   r   )FbRemoteFxGraphCacheREMOTE_CACHE_VERSIONr    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  compilation_metricsbwd_compilation_metricsc                 d    d t          j                                                   D             S )Nc                b    i | ],\  }}|t          |t                    rt          |          n|-S r;   )r~  r   r   )r   r   r   s      rQ   r  z@record_compilation_metrics.<locals>.<lambda>.<locals>.<dictcomp>2  sG     
 
 
1 *Q,,3tAwww!
 
 
rP   )dataclassesasdictr   )ri  s   rQ   r   z,record_compilation_metrics.<locals>.<lambda>2  s9     
 
#*+>??EEGG
 
 
 rP   F)record_logging_overhead)rZ  r{   rz   r   r;   )"r   r-  get_structured_logging_overheadrE  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cacherf  rg  get_remote_backendr4  rJ   ModuleNotFoundErrorr{   _guardsCompileContextcurrent_compile_idrL   r  rN  versioncudar2   triton__version__r  r2  r   r  trace_structuredr7  r9   log_compilation_metricsr+   )rO  rP  rQ  rR  rT  r\  r^  re  r  rf  rg  r%  backendr(  common_metricslegacy_metricsr[   ri  s     `              @rQ   record_compilation_metricsr    s=   < < < < <> > > > >; ; ; ; ; ;" %*N$R$R$T$T!==?? $	9       
 $8 *==??G48MM4J11" 	9 	9 	9#' 48111	9 15-# %-6IIKKLL&$."d*#m3<.6@X**D)2<s9~~~*34Q*R*R@BB*0:D&,,"-G-G/.
 .
 /I.H0/
 /
 !51V% N0 $c)'.w/R'S'S")'*S"T"T#*7+P#Q#Q"7#QRR%,W-O%P%P-5X/.
 .
 .6X/.
 .
 *G N  -  
9^
9~
9
9   3444% )$(	N##	
 	
 	
 	
 !& $    ((;<<< % 31222223 3s   2B BB)on_exitnew_sizec                    t          t                    | k    r1t                                           t          t                    | k    1t          j        t          |           }|ad S )Nr0  )r  r2  popleftrr   deque)r  	new_deques     rQ   set_compilation_metrics_limitr  I  s`    
"
#
#h
.
.$$&&& "
#
#h
.
.!"6xHHHI$rP   c                 8    t                                            d S rd   )r2  re   r;   rP   rQ   clear_compilation_metricsr  Q  s         rP   List[CompilationMetrics]c                 *    t          t                    S rd   )r   r2  r;   rP   rQ   get_compilation_metricsr  V  s    $%%%rP   c                  |    e Zd ZdZddZd dZd Zd!d	Zd
 Zd"dZ	d"dZ
	 d#d$dZd%dZd&dZ	 d'd(dZ	 	 d)d*dZdS )+ChromiumEventLoggera  Logs chromium events to structured logs. tlparse will concatenate these into a perfetto UI link.

    See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.yr4qxyxotyw for
    a specification of the Chromium Event JSON format.
    rz   	List[str]c                t    t          | j        d          r| j        j        S g | j        _        | j        j        S )z]
        The main event stack, with every chromium event.
        Logged to tlparse.
        stack)hasattrtlsr  r   s    rQ   	get_stackzChromiumEventLogger.get_stacka  s5    
 48W%% 	"8>!DHN8>!rP   r   c                B    |                                  }|r|d         ndS )zG
        Get the top event name or None if the stack is empty.
        N)r  )r   r  s     rQ   r4  zChromiumEventLogger.get_topl  s&       !+uRyyt+rP   c                t    t          | j        d          r| j        j        S g | j        _        | j        j        S )zq
        A smaller subset of the main stack that gets used to log
        PT2 Compile Events internally.
        pt2_compile_substack)r  r  r  r   s    rQ   get_pt2_compile_substackz,ChromiumEventLogger.get_pt2_compile_substacks  s9    
 48344 	1800,.DH)800rP   rA   c                \    t          | j        d          si | j        _        | j        j        S )N
event_data)r  r  r  r   s    rQ   get_event_dataz"ChromiumEventLogger.get_event_data~  s+    tx.. 	%"$DHx""rP   c                    t          j                    | _        t          t	          j                              | _        t                              d| j                   d S )Nz*ChromiumEventLogger initialized with id %s)		threadinglocalr  r{   uuiduuid4id_rq   r   r   s    rQ   r   zChromiumEventLogger.__init__  sG    ?$$ tz||$$ 	=txHHHHHrP   r   r{   r   c                R    ||                                  vrdS  | j        |fi | dS )zd
        Same as add_event_data, but will silently not log if the event isn't in the stack.
        N)r  r5  )r   r   rY  s      rQ   try_add_event_dataz&ChromiumEventLogger.try_add_event_data  s?     T^^----FJ11&11111rP   c                   ||                                  vr5t          dt          |           d|                                   d          |                                 }||vri ||<   ||                             |           dS )zz
        Adds additional metadata info to an in-progress event
        This metadata is recorded in the END event
        zEvent z not in zq. Cannot add metadata to events that aren't in progress. Please make sure the event has started and hasn't ended.N)r  RuntimeErrorr   r  r   )r   r   rY  r  s       rQ   r5  z"ChromiumEventLogger.add_event_data  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz":%%f-----rP   Fr   rW   r   r   r   c                `   t          t          j        j                                                  }||d<   |                     ||d|           |                                                     |            | j        |fi | |r)| 	                                                    |           dS dS )z
        Logs the start of a single event.
        :param str event_name Name of event to appear in trace
        :param time_ns Timestamp in nanoseconds
        :param metadata: Any extra metadata associated with this event
        r  BN)
r{   r   rv  rw  rx  _log_timed_eventr  r   r5  r  )r   r   r   r   r   r  s         rQ   r   z#ChromiumEventLogger.log_event_start  s     5HHJJKK
!+		
 	
 	
 	
+++J33(333  	?))++22:>>>>>	? 	?rP   c                    |                                  }|                                 }|                                 |                                 |                                 }|                                 d S rd   )r  r  re   r  )r   r  substackr  s       rQ   r   zChromiumEventLogger.reset  sj       0022((**
rP   rO  c                `   t          t          j        j                                                  }||d<   |                                 }|v r|         }|= ni }|                    |           |                     |d|          }	fd}
|                                 }|vrt          
                    d           dS  |
|           |rJ|                                 } |
|           t          |	|| j        |           |                                 |                                 dS )aA  
        Logs the end of a single event. This function should only be
        called after log_event_start with the same event_name.
        :param event_name: Name of event to appear in trace
        :param time_ns: Timestamp in nanoseconds
        :param metadata: Any extra metadata associated with this event
        r  Ec                    | d         k    r<t                               d           |                                  | d         k    :d S d S )Nr  z>ChromiumEventLogger: Detected overlapping events, fixing stack)rq   r=  pop)r  r   s    rQ   	pop_stackz4ChromiumEventLogger.log_event_end.<locals>.pop_stack  sY    b	)) T   		 b	))))))rP   z7ChromiumEventLogger: Start event not in stack, ignoringN)r{   r   rv  rw  rx  r  r   r  r  rq   r=  r  r*   r  r  )r   r   r   r   rO  r   r  all_event_datar   eventr  event_stackr  s    `           rQ   r   z!ChromiumEventLogger.log_event_end  sp    5HHJJKK
!+ ,,..''+J7Nz**Nh'''%%	
 
	 	 	 	 	 nn&& [(( KKQRRRF	+  	'#'#@#@#B#B I*+++'+TX}   !$$&&& 	rP   Nr   Optional[Dict[str, Any]]c                    ||dz  ||ddddt           j                            dfddd           t                     S )	za
        Logs a timed event in chromium format. See log_event_start, log_event_end, etc.
        r   r   r   )r[   tsrX  phcattidpidchromium_eventc                      S rd   r;   r  s   rQ   r   z6ChromiumEventLogger._log_timed_event.<locals>.<lambda>(      u rP   F
payload_fnsuppress_contextexpect_trace_id)r   r-  r}  r,   )r   r   r   r   r   r  s        @rQ   r  z$ChromiumEventLogger._log_timed_event  sr     D.!	
 	
 	''$}}}"!	 	( 	
 	
 	
 	'u---rP   c           	     <   |i }t          t          j        j                                                  }||d<   ||dz  |ddddddt          j                            d	fd
dd           |r+t          |                                 | j	        |           dS dS )aZ  
        Log an instant event with no associated duration.
        :param str event_name: Name of event to appear in trace
        :param int time_ns Timestamp in nanoseconds
        :param Optional[Dict[str, Any]] metadata: Any extra metadata associated with this event
        :param str cname optional color for the arrow in the trace
        Nr  r   ir   r   p)r[   r  rX  r  r  r  r  sr  c                      S rd   r;   r  s   rQ   r   z7ChromiumEventLogger.log_instant_event.<locals>.<lambda>O  r  rP   FTr  )
r{   r   rv  rw  rx  r-  r}  r*   r  r  )r   r   r   r   r   r  r  s         @rQ   log_instant_eventz%ChromiumEventLogger.log_instant_event/  s     H5HHJJKK
!+D.!

 

 	''$}}}" 	 	( 	
 	
 	
 ! 	't4466'    	 	rP   )rz   r  rz   r   )rz   rA   )r   r{   rz   r   F)
r   r{   r   rW   r   rA   r   r   rz   r   rz   r   )r   r{   r   rW   r   rA   rO  rW   r   r   rz   r   rd   )
r   r{   r   rW   r   r{   r   r  rz   rA   r  )
r   r{   r   rW   r   r  r   r   rz   r   )rJ   rK   rL   rp  r  r4  r  r  r   r  r5  r   r   r   r  r  r;   rP   rQ   r  r  Z  s.        	" 	" 	" 	", , , ,	1 	1 	1# # # #
I I I2 2 2 2. . . .2 ',? ? ? ? ?6   C C C CT .2    B .2&+( ( ( ( ( ( (rP   r  zOptional[ChromiumEventLogger]CHROMIUM_EVENT_LOGc                 :    t           t                      a t           S rd   )r  r  r;   rP   rQ   r   r   ]  s    !022rP   r   reset_event_logc           	   #  d  K   t                      }|r|                                 t          j                    }|                    | |i |           	 dV  |                    | t          j                    i ||           dS # |                    | t          j                    i ||           w xY w)z
    Context manager that creates a chromium start and end event. Chromium event
    logging is integrated with dynamo_timed, so you probably want to use that
    instead. Use this context manager only if you want to avoid dynamo_timed.
    N)r   r   r   r   r   r   )r   r  r   chromium_event_logchromium_start_times        rQ   chromium_event_timedr  d  s       344 #  """,..&&
	  	
((LNN!	
 	
 	
 	
 	
((LNN!	
 	
 	
 	
s   B ,B/c                  D    e Zd ZU dZded<   ded<   d Zed             ZdS )	CleanupHookz,Remove a global variable when hook is calledrA   scoper{   r[   c                X    t           t           xj        dz  c_        | j        | j        = d S r   )CleanupManagerr^   r  r[   r   rX  s     rQ   __call__zCleanupHook.__call__  s.    %  A%  Jty!!!rP   c                b    || vsJ t           xj        dz  c_        || |<   t          | |          S r   )r  r^   r  )r  r[   r  s      rQ   createzCleanupHook.create  s@    5    !d5$'''rP   N)rJ   rK   rL   rp  rt   r  staticmethodr  r;   rP   rQ   r  r    s[         66III" " " ( ( \( ( (rP   r  c                  .     e Zd ZU dZded<    fdZ xZS )r  r   zClassVar[CleanupManager]instancec                ~    | j         |         D ]} |             t                                          |           d S rd   )r   r<  ri  )r   rk  hookr>  s      rQ   ri  zCleanupManager._remove_id  sD    K$ 	 	DDFFFF3rP   )rJ   rK   rL   r^   rt   ri  r?  r@  s   @rQ   r  r    sK         E&&&&                 rP   r  c                    |                                                      | j                  }| j        r%| j        | j                                         |_        |S )z!Clone the tensor and its gradient)clonerequires_grad_requires_gradis_leafgrad)r   ys     rQ   clone_tensorr    sF    			  11Ay  QV'HrP   dtypec          	     F   t          |           r| S fd}t          j                    5  | j        j        dk    r ||           cddd           S | j        t          j        u rqt          j         ||                                            || 	                                          | j
        |                                           cddd           S t          |           r| j        t          j        t          j        hv r)|                                 }|                                 }n(|                                 }|                                 }t          j         ||           ||           ||                                           | j
        | j                  cddd           S t+          d t-          |                                 |                                           D                       }| j        rt          j        |dz   f|           }n&t          j        |dz   p| j        | j                  }|                                 |                                z
  dz  |                                 z  }|                    |                                 |                                 |           	 |                     | !                                           | j"        r|#                    | j$                   | j"        r"| j%        tM          | j%        	          |_%        n'# tN          $ r  ||           cY cddd           S w xY wtQ          | d
          r| j)        *                                |_)        |cddd           S # 1 swxY w Y   dS )zcopy while preserving stridesc                    t          j        |           }| j        r|                    | j                   | j        r"| j        t          | j                  |_        t          | d          r| j        	                                |_        |S )Nr  _dynamo_dynamic_indices)
r   r  r  r  r  r  clone_inputr  r  copy)r   r  r  s     rQ   torch_clonez clone_input.<locals>.torch_clone  s    KNN9 	.Q_---9 	6+ u555AF1/00 	I()(A(F(F(H(HA%rP   xlaN)is_coalesced)layoutc              3  ,   K   | ]\  }}|d z
  |z  V  dS )r8   Nr;   )r   shapestrides      rQ   r   zclone_input.<locals>.<genexpr>  s@       
 
%2UFUQY& 
 
 
 
 
 
rP       )r  devicer  r  )+r=   r   no_gradr  r4  r  
sparse_coosparse_coo_tensor_indicesrT   r  r  r)   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorr   rj   zipsizer  is_quantizedempty_quantizedemptyr  data_ptrelement_sizeas_strided_copy_r  r  r  r  r  r  r  r  r  r  )r   r  r  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      rQ   r  r    s8    qzz      
 6 68=E!!;q>>6 6 6 6 6 6 6 6 8u'''*AJJLL))AIIKK((^^--	  6 6 6 6 6 6 6 6 "!$$ 	xE,e.>???%&^^%5%5" !%&^^%5%5" !1.//M**AHHJJ''x  )6 6 6 6 6 6 6 68  
 
69!&&((AHHJJ6O6O
 
 
 
 
 > 	*K",<+>BBFF[b (8  F ZZ\\FOO---3^^ 	16688QXXZZ1BCCC
	"LL###y 7%%ao666y ?QV/)!&>>> 	" 	" 	" ;q>>!!g6 6 6 6 6 6 6 6^	"
 1/00 	N-.-F-K-K-M-MF*m6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6sK   NA7NCN2DNA1L32N3MNM2NNNc                *   t          |           t          u rt          |           }|                                D ]p\  }}t          |t                    rt          |          ||<   -t          |t          j                  sJ t          |                      t          |          ||<   q|S t          |           }t          t          |                    D ]:}t          ||         t          j                  rt          ||                   ||<   ;|S rd   )r4  dictr   r~  r  clone_inputsr   r  r  r   ranger  )example_inputsresr   r   r  s        rQ   r  r    s    Nt##>""))++ 	. 	.JC%'' .'..C!%66CCUCC6&u--C

~

C3s88__ ) )c!fel++ 	) Q((CFJrP   r  torch.Tensorc                    	 |                                   d S # t          $ r=}ddlm} t	          j        ddt          |                     } |d|           |d }~ww xY w)Nr8   )	SkipFramez\(.*rh  z(torch.compile cannot be run in context: )r  r  r6  r  rG  subr   )r  r  r  functorch_subclass_names       rQ   skip_frame_if_in_functorch_moder    s    	   """""" #%&"d3ii"@"@iP7NPP
 
	s    
A8AAc               #  l  K   t           j        j        } t           j        j        j        } |            5   |             5  t          j        t           j                                                  }t          |           t           j
                                        r0t          j        t           j
                                                  }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   	 d V  t           j        j                                        5  t           j                            |           t           j
                                        rt           j
                            |           d d d            d S # 1 swxY w Y   d S # t           j        j                                        5  t           j                            |           t           j
                                        rt           j
                            |           d d d            w # 1 swxY w Y   w xY wrd   )r   _C_DisableFuncTorchrq  _python_dispatch_disable_current_modesr  randomget_rng_stater  rz  is_availableset_rng_state)disable_functorchdisable_current_modes	rng_statecuda_rng_states       rQ   preserve_rng_stater-    s     2!K8O			 	  E E"3"3"5"5 E EK : : < <==	'	222:""$$ 	E"[)A)A)C)CDDN	E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E
9[)@@BB 	9 	9L&&y111z&&(( 9
((888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9U[)@@BB 	9 	9L&&y111z&&(( 9
((888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9sy   C0BCC0C	C0 C	!C00C47C4<F #AFFF$H3>AH'H3'H++H3.H+/H3c                    t          | t          j        j        j        t          j        j        j        t          j        j        t          j        j        f          S rd   )	r~  r   jit_traceTopLevelTracedModule_scriptRecursiveScriptModuleScriptFunctionScriptModule)model0s    rQ   is_jit_modelr7  +  sB    I1I3I$I"		
  rP   c                \   t          |           r| S 	 t          j                            | |          S # t          $ rl 	 t          j                            |           cY S # t          $ r: |rt                              d           nt                              d           Y nw xY wY nw xY wd S )Nz	jit errorz0Both torch.jit.trace and torch.jit.script failed)	r7  r   r/  tracerA  scriptrq   rB  error)modelr  r  s      rQ   torchscriptr=  7  s    E 	Nyun555 N N N	N9##E***** 	N 	N 	N Nk****		LMMM		NN 4s3   3 
B)AB)AB# B)"B##B)(B)c                \    	 t          j        |           S # t          t          f$ r Y d S w xY wrd   )r  getfilerS  OSErrorrq  s    rQ   r?  r?  I  s>    s###w   tts    ++c                :    t          t          |                     S )zLTest if an object is a namedtuple or a torch.return_types.* quasi-namedtuple)is_namedtuple_clsr4  rA  s    rQ   is_namedtuplerD  P  s    T#YY'''rP   c                    	 t           t                    rt           dd          }|dv rdS t          t           dd          t                    rit	          t           dd                    rKt          d  j        D                       }|t          fk    rdS |rt           fd|D                       rdS n# t          $ r Y nw xY wd	S )
zhTest if an object is a namedtuple or a (torch.return_types|torch.autograd.forward_ad).* quasi-namedtuplerK   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  ,   K   | ]}|t           u|V  d S rd   )r   )r   ts     rQ   r   z$is_namedtuple_cls.<locals>.<genexpr>`  s,      KKA!7:J:Ja:J:J:J:JKKrP   c              3  R   K   | ]!}t          |          oj        |j        u V  "d S rd   )rC  __new__)r   rJ  r\   s     rQ   r   z$is_namedtuple_cls.<locals>.<genexpr>d  sR       ! ! 	 *!,, 5K194! ! ! ! ! !rP   F)
issubclassr  getattrr~  callable	__bases__anyrS  )r\   modulebasess   `  rQ   rC  rC  U  s   c5!! 	 S,55FLLLt'#y$77??  HWd++E E   KKKKKKKUH$$4 	 S ! ! ! ! #! ! !   	   4   5s   *B= A,B= B= =
C
	C
Tuple[str, ...]c                H   | t           u rdS t          | t                    sJ t          | d          r| j        S t
          j         G d d                      }| j        dk    sJ  | t          |t          | j
                                      }i }t          |          D ]D}|d         dk    r6t          t          ||          |          rt          ||          j        ||<   Et          |          | j
        k    sJ t          t!          ||j                            S )	zIGet the fields of a namedtuple or a torch.return_types.* quasi-namedtuple)startstopsteprG  c                      e Zd ZU ded<   dS )!namedtuple_fields.<locals>.MarkerrW   indexNrJ   rK   rL   rt   r;   rP   rQ   MarkerrZ  ~  s         




rP   r]  rF  r   r2  )r   )slicerM  r  r  rG  rm  	dataclassrK   r   r  n_fieldsdirr~  rN  r[  r  sortedrh   )r\   r]  rq  fieldsr[   s        rQ   namedtuple_fieldsrd  s  s>    e||((c5!!!!!sI {        >11111
#c&%--..
/
/CFC 4 47c>>jd););VDD>"3--3F4Lv;;#,&&&&FJ///000rP   c                   t          j                    5  t          j        t           j                                                  t           j                                        r0t          j        t           j                                                  d t          j        | 	                                | 
                                          D             d d d            n# 1 swxY w Y   fd}|S )Nc                F    g | ]}||j         t          j        |          fS r;   )_versionr   r  )r   params     rQ   r   z%checkpoint_params.<locals>.<listcomp>  s;     
 
 
 ENEK$6$67
 
 
rP   c                 l   t          j                    5  t           j                                       t           j                                        rt           j                                       D ]&\  } }}| j        |k    r|                     |           '	 d d d            d S # 1 swxY w Y   d S rd   )r   r  r%  r(  rz  r'  rg  r  )rh  ry  original_valuer,  r+  saved_states      rQ   restorez"checkpoint_params.<locals>.restore  s    ]__ 	0 	0L&&y111z&&(( 9
((8882= 0 0.w>W,,KK///0		0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   BB))B-0B-)r   r  r  r%  r&  rz  r'  r   r   
parametersbuffers)gmrl  r,  r+  rk  s     @@@rQ   checkpoint_paramsrp    s   	 
 
K : : < <==	:""$$ 	E"[)A)A)C)CDDN
 
""**,,GG
 
 
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0 0 0 0 0 0 0 Ns   CC%%C),C)c                n   t           j                                        rt           j        j        }nt          } |             t          j                     t          j        d           t          j	                    }t          |          D ]} | | } |             t          j	                    }|||z
  fS )Ni9  )r   rz  r'  synchronizerZ  gccollectmanual_seedr   perf_counterr  )r<  r  timesrr  t0r2  r  t1s           rQ   timedrz    s    z   j,KMMMJLLL	d				B5\\  '				B27?rP   c           	         t          d t          j        ||                     d                    D                       S )Nc              3  $   K   | ]}|j         V  d S rd   )is_cudar   r   s     rQ   r   z check_is_cuda.<locals>.<genexpr>  s$      WWQqyWWWWWWrP   T)allr   r   rm  )ro  r  s     rQ   check_is_cudar    s8    WW)/."--PTBUBU"V"VWWWWWWrP   r  c                   | dk    sJ d t          |           D             }t          |dd          |d d         z             }t          dd                    |           dd                    |           d          }d|  d	|_        |S )
Nr8   c                    g | ]}d | S r  r;   r   r  s     rQ   r   z rot_n_helper.<locals>.<listcomp>  s    &&&GGG&&&rP   r  zlambda rc  z: ()rot__helper)r  reversedevalr   rJ   )rM  varsrotatedr  s       rQ   rot_n_helperr    s    q5555&&U1XX&&&DtBCCy49,--G	???388G+<+<???	@	@B####BKIrP   z	Set[type]common_constant_typesc                   t          | t          t          f          r"t          t	          t
          |                     S t          | t          j        t          t          j        f          pt          | t          t          hz            S rd   )rx  r  	frozensetr  r   is_safe_constantr~  enumEnumr4  r   Sizer  r^  r  s    rQ   r  r    sm    a%#$$ -3'++,,,a$)T5:677 6	'< < rP   c                    ddl m}m} t          | |          r'|                    |                                           S | S )Nr8   )ConstantVariableSymNodeVariable)	variablesr  r  r~  r  evaluate_expr)r  r  r  s      rQ   specialize_symnoder    sS    <<<<<<<< #'' <&&s'8'8':':;;;JrP   c                x    ddl m} t          |           } t          | |          r|                                 S | S )Nr8   r  )r  r  r  r~  as_python_constant)r  r  s     rQ   guard_if_dynr    sK    ++++++
S
!
!C#'(( (%%'''JrP   c                ~    t          d t          j        | |                                          D                       S )Nc              3  >   K   | ]}|                                 V  d S rd   )is_python_constantr~  s     rQ   r   z&check_constant_args.<locals>.<genexpr>  s.      VV!q##%%VVVVVVrP   )r  r   r   r   rW  s     rQ   check_constant_argsr    s3    VVytV]]__/U/UVVVVVVrP   c                    ddl m} ddlm} d}t	          j        | |                                          D ]+}t          ||          r|dz  }t          ||          s dS ,|dk    S )Nr8   r  UnspecializedPythonVariabler   F)variables.constantr  variables.tensorr  r   r   r   r~  )rX  rY  r  r  unspec_countr   s         rQ   check_unspec_python_argsr    s    444444======L_T6==??33  a455 	ALLA/00 	55	!rP   c                    ddl m} t          j        | |                                          D ])}|                                st          ||          s dS *dS )Nr8   r  FT)r  r  r   r   r   r  r~  )rX  rY  r  r   s       rQ   check_unspec_or_constant_argsr    sl     >=====_T6==??33  $$&& 	*Q8S*T*T 	554rP   c                    ddl m t          fdt          j        | |                                          D                       S )Nr8   )NumpyNdarrayVariablec              3  8   K   | ]}t          |          V  d S rd   )r~  )r   r   r  s     rQ   r   z+check_numpy_ndarray_args.<locals>.<genexpr>"  sB         	1*++     rP   )r  r  rQ  r   r   r   )rX  rY  r  s     @rQ   check_numpy_ndarray_argsr    sa    666666    v}}77     rP   zType[KeysView[Any]]	dict_keyszType[ValuesView[Any]]dict_valuesodict_valueszType[Iterator[Any]]tuple_iteratorrange_iteratorc                l    t          |           }t          j        j                            |           |S rd   )
object_newr   r  Moduler   )r\   rq  s     rQ   nn_module_newr  1  s*    
S//C	HOS!!!JrP   c                B    t          j        t          j        | d          S r   )r  reduceoperatormul)its    rQ   productr  7  s    HL"a000rP   c                L    |                                  \  }\  }}|||z            S rd   )
__reduce__)r  r[  r2  rq  rV  s        rQ   tuple_iterator_getitemr  ;  s'    }}Avuuu}rP   Tuple[int, int, int]c                t    |                                  \  }\  }}|j        |pdz   }|j        }|j        }|||fS r   )r  rV  rW  rX  )
range_iterr2  	range_obj	maybe_idxrV  rW  rX  s          rQ   normalize_range_iterr  C  sI    !+!6!6!8!8A|	Y Oy~A.E>D>D4rP   c                ,    |                      |          S rd   )as_subclass)rJ  r\   s     rQ   to_subclassr  M  s    ==rP   c                h    t          t          j        t          |           ||dz                       S r   )nextr   isliceiter)drM  s     rQ   dict_keys_getitemr  Q  s)    	 a!QU33444rP   c                L    | j         j        }| j        }|rdnd}| d| d| }|S )NLGz["z"].)r>  rJ   r[   )r   r  r[   r  r  
local_names         rQ   	enum_reprr  U  sD     ?#D
*C!CCcE++T++c++JrP   c                    || j         d<   t          j                    j        j        }t
          j        j        j        	                    ||          x}r|| j         d<   d S d S )Nexample_valueunbacked_bindings)
metar(   rh   	fake_mode	shape_envr   r    experimentalsymbolic_shapescompute_unbacked_bindings)noder  r  symbol_to_paths       rQ   set_example_valuer  _  so     "/DIo"$$.8I.>XX=  ~ 8 *8	%&&&8 8rP   c                    |                                  j        j                            d          }t	          |          sddlm}  |d           |S )Nr  r8   r  z:Cannot check Tensor object identity without its fake value)r  r  r  rh   r=   r6  r  )vtfake_tensorr  s      rQ   _get_fake_tensorr  n  s]    ++--$)--o>>K; T&&&&&&RSSSrP   c                ^   ddl m}m}m}m}                                 r0t          fd| D                       }|                    |          S d}	|r!t          |          rd}	t                    d }
| D ]}|	r9t          ||          r(t          |          u r|                    d          c S = |t          j                                      ||gi           }|
|}
n |t          j                                      |||
gi           }
|
|                    d          }
|
S )Nr8   )BuiltinVariabler  TensorVariableVariableTrackerc              3     K   | ]B}|                                 o)|                                                                k    V  Cd S rd   )r  r  )r   r   rH  s     rQ   r   z iter_contains.<locals>.<genexpr>  sk       
 
    "" F$$&&&*C*C*E*EE
 
 
 
 
 
rP   FT)r  r  r  r  r  r  rQ  r  r~  r  r  eqcall_functionor_)r   rH  txcheck_tensor_identityr  r  r  r  found_constmust_check_tensor_idfoundr   checks    `           rQ   iter_containsr  w  s                 "" 4 
 
 
 
 
 
 
 
 

  &&{333  *FN!C!C *#!&))'+E   	!^,, 9-a0000+22488888#OHK00>>rAv;PRSSE}'55CC  } ''..LrP   c                d    t          | t          j        t          j        j        t
          f          S )z4Returns whether it indexes dictionaries using its id)r~  r   r  r  r  r   )r   s    rQ   	key_is_idr    s     a%,9JKLLLrP   c                >    d |                                  D             S )Nc                N    g | ]"}t          |          rt          |          n|#S r;   )r  ra  )r   r   s     rQ   r   zkey_to_id.<locals>.<listcomp>  s-    ???QYq\\(BqEEEq???rP   )r   r  s    rQ   	key_to_idr    s    ??%**,,????rP   c                  ddl m} t          | t          t          f          rrd                    fd| D                       }t          | t                    rd| dS t          | t                    sJ t          |           dk    rd| dS d| d	S t          | t          j                  r%t          | 
          
                    dd          S  ||           r| j        S t          | t                    rd } ||           S | S )Nr8   )is_builtin_callablerc  c              3  :   K   | ]}t          |           V  dS r  N
const_reprr   r  r  s     rQ   r   zconst_repr.<locals>.<genexpr>  s0      DDQj%888DDDDDDrP   [](z,)r  r  'rh  c                R    | j         }|j        }|dk    r|j        S |dz   |j        z   S )Nbuiltins.)r>  rK   rL   )r=  klassrR  s      rQ   fullnamezconst_repr.<locals>.fullname  s7    KE%F##))C<%"444rP   )trace_rulesr  r~  r   r  r   r  r  r  r  replacerJ   r4  )r   r  r  
elems_reprr  s    `   rQ   r  r    sG   000000!dE]## XXDDDD!DDDDD
a 	)$z$$$$a'''''1vv{{):))))(:((((	Aty	!	!  %(((00b999		Q		 z	At		 	5 	5 	5 x{{xrP   c               V    d                     fd| D                       }d|z   dz   S )Nrc  c              3  :   K   | ]}t          |           V  dS r  r  r  s     rQ   r   z!dict_keys_repr.<locals>.<genexpr>  s0      GGq
1E222GGGGGGrP   r   r  r   )
const_keysr  keys_strs    ` rQ   dict_keys_reprr    s8    xxGGGGJGGGGGH>CrP   
__dict_key)UnsupportedFakeTensorExceptionc                D    | dt          |           d| j        j         S )Nr2  _c)ra  outputr  )r  rootrq  s      rQ   get_safe_global_namer    s+     66RWW66	 4666rP   c                    	  |             S # t           $ rC}ddlm} d|j         d}t                              |            |||           Y d }~d S d }~ww xY w)Nr8   r  zUnsupported: z with fake tensor propagation.r  )r  r6  r  reasonrq   r=  )r  r  r  msgs       rQ   wrap_fake_exceptionr    s    'rtt) ' ' '&&&&&&FahFFFCcA&&&&&&&&&&'s   	 
A8AAc                     t           j        j                            |          5  t	           fd          cd d d            S # 1 swxY w Y   d S )Nc                 ,    t          j                   S rd   )r  deepcopyrA  s   rQ   r   z)deepcopy_to_fake_tensor.<locals>.<lambda>  s    4=+=+= rP   )r   r  r  FakeCopyModer  )rq  r  s   ` rQ   deepcopy_to_fake_tensorr!    s    			&	3	3I	>	> ? ?"#=#=#=#=>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   AA	A	c                x    t          j        t          j        t          j        | |z
                                S )z+
    Calculate root mean squared error
    )r   sqrtmeansquare)rj  r  s     rQ   rmser&    s,     :ejcCi!8!899:::rP   g-C6?Tc                   	
  t           t          t          t          j        t
          j        j        t
          j        f          rt          t          t          t          j        f          s*J dt                      dt                                 t                     t                    k    r 	d           dS t                     t                    k    o0t          	
fdt                     D                       S t                     j        dk    r)t           j        j        j        	
          S t           t                     r
t          t                     sJ t#                                                     t#                                                    k    sNJ d	t#                                                      d
t#                                                                 t'                                                     D ]=}t           |         |         |         	
          s 	d|            dS >dS t           t"                    rct          t"                    sJ t#                     t#                    k    s*J dt#                      d
t#                                 dS t           t
          j        t*          f          r:t           t
          j        j                  rJ t          t
          j        j                  rJ d fd fD             \    j        r1j        sJ                                                                   t          t
          j                  s*J dt                      dt                                 r j        j        k    r 	d j        j                   dS  j        t
          j        k    rhrdS t          j                             t
          j                                      t
          j                            }|s 	d           |S r                                                     t
          j                                                                        t
          j                   t          j         d          rdS t
          j        j!        "                     dd          }|dk     rQtF          $                    d|%                                &                                '                                           |dk    S s                     j                   t          j                   rdS j        t
          j(        k    rXd})                                *                                s)                                *                                sZ )                                *                                r4t          j                            j                  ||          rdS tW                     '                                }tY          j)        |          rtF          $                    d           tW                    '                                }j        t
          j-        t
          j.        fv rdnd}
r!/                                dk    r	dk    rd}ns
r!/                                d k    r	dk    rd!}nP/                                d"k     s6 j0        d#k    r% j1        d$          j1        d%         cxk    rd&k    sn d'k    rd}|||z  dz  z   k    }|sGrEtY          j)        |          r1tY          j)        |          rt
          j2        j3        j4        j5        sd}|s) 	d(||6                                j        |
           |S rdS  	d)           dS t           tn          tp          t          d          t6          t
          j9        f          r)rdS  k    }|s 	d*t                                 |S tu                     stw                     rir2tu                    s#tw                    s '                                 t                     t                    u o k    }|s 	d+            |S ty                     r\t                     t                    u o=t          t          j=                   t          j=                  	
          S t                     j        d,v r\t                     t                    u sJ t          	 
fd- j>                                        D                       S t          d.t                     j                   )/z-Check correctness to see if ref and res matchNztype mismatch r   zLength mismatchFc              3  T   K   | ]"\  }}}t          |||
	           V  #dS )	log_error(use_larger_multiplier_for_smaller_tensorN)same)r   aibi	fp64_reficos_similarity	equal_nanexact_dtypeignore_non_fpr*  relax_numpy_equalitytolr+  s       rQ   r   zsame.<locals>.<genexpr>  so       ,
 ,
 "B	 $#9a  ,
 ,
 ,
 ,
 ,
 ,
rP   QuestionAnsweringModelOutputr)  zkeys mismatch z == r0  r5  r1  r2  r4  r3  r*  r+  zAccuracy failed for key name %sTzelements mismatch c                b    t          | t          j                  r| nt          j        |           S rd   )r~  r   r  tensorrJ  s    rQ   	to_tensorzsame.<locals>.to_tensorP  s%    "1el33H11aHrP   c              3  .   K   | ]} |          V  d S rd   r;   )r   r  r;  s     rQ   r   zsame.<locals>.<genexpr>S  s+      MMiinnMMMMMMrP   zdtype mismatch %s, %sr  )atolrtolr1  z+Accuracy failed: uint8 tensor did not matchr   gư>)dimepsgGz?zSimilarity score=%sg{Gz?z=Found nan in reference. Consider running in higher precision.g      @g       @
   g      $@i  g      @r      r  r8   g{Gz?zRMSE (res-fp64): %.5f, (ref-fp64): %.5f and shape=%s. res.dtype: %s, multiplier: %f, tol: %f, use_larger_multiplier_for_smaller_tensor: %dz+Accuracy failed: allclose not within tol=%szAccuracy failed (%s): %s != %sz!Accuracy failed (numpy): %s != %s)MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsLongformerMaskedLMOutput	InstancesSquashedNormalBoxesNormalTanhTransformFooVariablec              3     K   | ]H}t          t          |          t          
|          t          |          	           V  IdS )r7  N)r,  rN  )r   r   r0  r1  r2  fp64_refr3  r*  rj  r4  r  r5  r+  s     rQ   r   zsame.<locals>.<genexpr>	  s       
 
  S!!S!!#&&-#'%9+#9a  
 
 
 
 
 
rP   zunsupported type: )@r~  r   r  rr   r  r   r  ParameterListr  r4  r  r  r  rJ   r,  lossr  r   r   rb  r  floatr  r<   	is_sparseto_denser  r   allclosetor  flattenr  
functionalcosine_similarityrq   r=  cpudetachr`  r  isnanrQ  r&  mathr  bfloat16numelndimr  rE  r9   cppinject_relu_bug_TESTING_ONLYr  r{   rW   r  r  r  r  	as_tensor__dict__r  )rj  r  rP  r0  r5  r1  r2  r4  r3  r*  r+  r   rscore	loose_tol	ref_error	res_error
multiplierpasses_testr;  s   ```````````        @rQ   r,  r,    s    dE;,eh.DejQ  PF ${01
 
 	4 	43DII33S		33	4 	4 
 s88s3xxI'(((53xx3s88# 
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 &)c8%<%<,
 ,
 ,
 )
 )
 	
  
c	=	=	= HHM 5]
 
 
 	
 
C		 fF#t$$$$$388::#HHJJ#
 #
 
 
 
BC

OOBBSXXZZBB
 
 
 

## 	 	AFFQK#1' +)="/'=e   	;Q???uu!" t	C		 NF#s#####3xx3s88###%R#c((%R%RC%R%R###t	C%,.	/	/ JFc5#4#?@@@@@c5#4#?@@@@@	I 	I 	I NMMMc88LMMMS(= 	!=  =,,..C,,..C#u|,,VV.VtCyy.V.V4PS99.V.VVV, 	yCI%%	139ciHHHuyEJ&&   4NFFF--FFF--'    MIKLLL k	++--""5=11C++--""5=11C~c3SsdKKK  tH'99#st9TTEt||1599;;3E3E3G3G3L3L3N3NOOOD=  (ffSY'' ~c3SsiPPP t ~.. %	 ((,,.. IIKKOO--  		))   #)44&&"+  	   4 3//4466	:i(( KKW   !3//4466	 9(GGGCCS  < %NN$$**sh!%JJ= %NN$$++x!$JJNN$$t++A#)B-39R=*M*M*M*MA*M*M*M*Mh
 "%J'J,BS4Z,OP#	'!	' 
9--	' 
9--		' "O26S	' #'K" II!!

	"@
 
 
 #" tICSIII5	C#sDJJelC	D	D =F 	43J 	MI6S		3LLL	3		 6F#6s#;#; 6F 	c""	&9#&>&>	 ((**C#YY$s))#5#* 	EI93DDD	#		 -FS		T#YY& 
DOC  OC  )#!5'5]-
 -
 -
 	
 
c	   
 
 CyyDII%%%% 
 
 
 
 
 
 
 
 
 
 
 
 
 
 |((**
 
 
 
 
 	
" DS		0BDDEEErP   c                n    | j                             d          d         }d| j         d| d| j         dS )N/r  r  z' (r   r  )r  splitr3  r5  )r7  short_filenames     rQ   format_func_inforq  	  sC    %++C004NGt|GGGG1DGGGGrP   c               #    K   t           j        } t          j        t           _        t           j        }t          j        t           _        	 d V  | t           _        |t           _        d S # | t           _        |t           _        w xY wrd   )r9   cache_size_limitr  r   accumulated_cache_size_limit)priorprior_acc_limits     rQ   disable_cache_limitrw  	  sm      #E!kF9O*-+F'>"'.=+++ #(.=+====s   A& &B zDefaultDict[Any, List[Any]]guard_failuresz3List[torch._dynamo.output_graph.GraphCompileReason]graph_break_reasonsc                    dt           j                                                             d          z   dz   t          t	          j                              z   }t          j                            | |          S )Nrun_z%Y_%m_%d_%H_%M_%S_%fz-pid_)datetimenowstrftimer{   r  getpidr   r   )root_dirdir_names     rQ   _get_debug_dirr  ;	  sm     	




!
!
*
*+A
B
B	C 	 bikk

		  7<<(+++rP   c                 8    t           j        } t          |           S rd   )r9   debug_dir_rootr  )
debug_roots    rQ   r!  r!  G	  s    &J*%%%rP   c                    d| j         v r't          | j         d                   r| j         d         S |rddlm}  |d           d S d S )Nr  r   r  z9`FakeTensor` example value was required but not available)r  r=   torch._dynamo.excr  )r  requiredr  s      rQ   extract_fake_example_valuer  L	  sf    $)##	/0J(K(K#y))	 333333QRRRRRtrP   c                6    t          |           |j        u sJ | S rd   )r>   r  )r  r  s     rQ   ensure_graph_faker  W	  s"    q!!R\1111HrP   c                ^     d fd}t           j        j                            ||          S )NrM  torch.fx.Nodec                    | j         dk    rd| j        vrt          |           S | j        d         }s*t          |t          j                  rt          |          S |S )Nr  r  )rK  r  get_fake_valuer~  r   r  r  )rM  r   allow_non_graph_faker  s     rQ   visitz)get_fake_values_from_nodes.<locals>.visit]	  sm    4?""af'D'D "!R)=>>>f_%# 	.
3(E(E 	.$S"---
rP   )rM  r  )r   r    r  map_arg)r  rJ  r  r  s   ` ` rQ   get_fake_values_from_nodesr  \	  sC    	 	 	 	 	 	 	 8=  ...rP   c           	         ddl m} ddlm}m}m}m}m}  j        }	d j	        v r't           j	        d                   r j	        d         S t           j         j        f|          \  d|	dk    rlt                    dk    rYt          d         t           j        j                  r4t'          d         j                  ft+          dd                   z   |	dk    raj        j         j                 t3                    r&t5          d	          r                               t'          j                   j        d
v st;          d D                       rt+          d D                       	 j        5  t=                      5  t?           fd          }
ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   nV# |$ r  t@          $ rC}|}|j!        |j!        }t          |t           j"        j#        j$                  r |d|j%         d           nt          |t           j"        j#        j&                  rBt           j'        j(        j)        s |d|j%         d           ns |d|j%         d           n]t          |t           j"        j#        j*                  r|j%        }	d}t          |	t           j+        j,                  rGt           j-        .                    |	j/        j        |	j/        j0                  }||\  }}d| d| d| d} |d|j%         d| d           nt          |t           j1        j2        j3        j4                  r  ||j5        tm          |          d          t          ||          r ||j5        |j        d                   |t          |tn                    r'dtm          |          v r |d j         d|             |tm          |                    8                    |j9                  dd}~ww xY w|s9tu          j;        t           j<        t{          j>        t~                     |
          }|
S )!az  
    Run the computation represented by `node` using fake tensors and return the result.

    allow_non_graph_fake: whether to allow the return result to be:
        1. non-fake or 2. fake that is not created by this instance of Dynamo.
        If `True`, you must be prepared to deal with such return values, ideally
        by further wrapping them as this graph's fakes.
    r   )ValueRangeErrorr8   )TorchRuntimeErrorr  Unsupported	UserErrorUserErrorTyper  Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  @   K   | ]}t          |t                    V  d S rd   )r~  complex)r   as     rQ   r   z!get_fake_value.<locals>.<genexpr>	  sE       M M#$
1gM M M M M MrP   c              3     K   | ];}t          |t          j                  r|j        j        t          |          n|V  <d S rd   )r~  r   SymFloatr  hintrS  r  s     rQ   r   z!get_fake_value.<locals>.<genexpr>	  sc       
 
  #u~..368=3L #JJJ
 
 
 
 
 
rP   c                 4    t          j                   S rd   )run_noder  )rX  rY  nnmoduler  r  s   rQ   r   z get_fake_value.<locals>.<lambda>	  s    D$II rP   zdata dependent operator: zC; to enable, set torch._dynamo.config.capture_scalar_outputs = Truezdynamic shape operator: zM; to enable, set torch._dynamo.config.capture_dynamic_output_shape_ops = Truezm; Operator does not have a meta kernel that supports dynamic output shapes, please report an issue to PyTorchrh  z:It's possible that the support was implemented in module `z` and you may need to `import z`(z), otherwise zunsupported operator: z (z~see https://docs.google.com/document/d/1GgvOe7C8_NVOMLOCwDaYV1mXXyHMXY7ExoewHqooxrs/edit#heading=h.64r4npvq0w0 for how to fix)constrain_as_size_example)	case_nameargumentz
TypeError z: )r  )@torch.utils._sympy.value_rangesr  r6  r  r  r  r  r  rK  r  r=   r  rX  rY  r  r~  r   r  r  r!  r  r  r  
nn_modulestargetr  r  _infer_parametersr[   rQ  r%   r  r  	__cause__r  r  DataDependentOutputExceptionfuncDynamicOutputShapeException_dynamor9    capture_dynamic_output_shape_opsUnsupportedOperatorExceptionr  r  r!  _dispatch_pystub_schemaoverload_namer    r  r  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr{   rS  with_traceback__traceback__pytreetree_map_onlyr  r  partialr  )r  r  r  r  r  r  r  r  r  rK  ret_valr  causeimport_suggestionmaybe_pystubrR  ctxr2  rX  rY  r  s   ``                @@@rQ   r  r  k	  sJ    @?????              
B $)##	/0J(K(K#y))-
TY$&: LD& H	]s4yy1}}DGUX_1U1U}'Q>>@5abb??R	]9'4(## 	7:L(M(M 	7
 &&x666 +8R\BByEEE M M(,M M M J JE  
 
 	
 
 
 
 
CR\ 	 	355 	 	)IIIIIIII G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     <R <R <R ;"KE5$0M
 
 5	? MTEJ T T T    5$0L
 
 .	? ='H 
buz b b b   
 8uz 8 8 8   
 5$0M
 
  	? B ""ej344 
$x88JORZ%=     +".KFC/#)/ /IO/ // / / &
 M# # #7H # # #   
 58(8T
 
 	? )2E

5   
 // 	?)M>q	JJPQQy)) 	?jCJJ.F.FM=t{==e==>>>A''66qGGTQy<R|   
 L)+,="EEEw
 
 Nsa   G% GG5GG	GG		GG% GG% G G% %P85H>P33P8c                 .    t          t          dd           S )Nr   )rN  _current_noder;   rP   rQ   get_current_noder  	  s    ='4000rP   c              #     K   t                      }| t          _        	 d V  |t          _        d S # |t          _        w xY wrd   )r  r  r   )r  olds     rQ   set_current_noder  	  sI      


CM"!c!!!!s   0 >c                .   j         t                    5  fd}	 dk    r j        i cddd           S dk    r5 t          d         j                  dd         i cddd           S dk    r|J  |i cddd           S dk    r+| j                            j                  cddd           S d	k    r$d
j        v sJ j        d
         cddd           S nx# t          t          f$ r&}ddl	m
}  | ||          |           Y d}~nFd}~wt          $ r6}t           ||                                        |j                  |d}~ww xY wddd           n# 1 swxY w Y   t                    )a  
    Runs a given node, with the given args and kwargs.

    Behavior is dictated by a node's op.

    run_node is useful for extracting real values out of nodes.
    See get_real_value for more info on common usage.

    Note: The tracer arg is only used for 'get_attr' ops
    Note: The nnmodule arg is only used for 'call_module' ops

    Nodes that are not call_function, call_method, call_module, or get_attr will
    raise an AssertionError.
    c           	     L    d dj          d d d	t          |           z   S )NzFailed running r   z(*z, **z):
)r  r{   )r  rX  rY  r  rK  s    rQ   make_error_messagez$run_node.<locals>.make_error_message
  s=    OROO$+OOOO6OOORUVWRXRXXXrP   r  Nr  r   r8   r  get_attrplaceholderr  r  r  )rK  r  r  rN  output_graphget_submoduler  r  r  r6  r  rA  r  r  r  AssertionError)	tracerr  rX  rY  r  r  r  r  rK  s	    ```    @rQ   r  r  
  s    
B	$		  	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y	_$$"t{D3F33        }$$4wtAw44d122hI&II        }$$+++x000        z!!*88EE        }$$&$)3333y1#        % $%CD 	= 	= 	=******M,,Q//!<<<<<<<<< 	 	 	11!4455DD 	1              : 

s^   	E=C9.C9?C9$C9C98E=9E.
D+&E=+E.81E))E..E==FFc                J   ddl m} j        }| |v r||          S | j        }t          j        j                            | j        | j	        ffd          \  }}|dk    rd| j
        v r| j
        d         j        S |dk    rDj        j        | j                 }t          |          st!          j        |          }n ||i | nd}	 t%          | |||          }||| <   nC# t&          $ r6}	 |t)          |	                                        |	j                  dd}	~	ww xY w|S )z
    Run the actual computation represented by `node` and return the result.
    This will execute any dependent nodes in the graph as well.
    r8   )r  c                $    t          |           S rd   )get_real_value)rM  r  s    rQ   r   z get_real_value.<locals>.<lambda>A
  s    .F++ rP   r  graphargr  N)r6  r  real_value_cacherK  r   r    r  r  rX  rY  r  exampler  r  r  r  r  r  r  r  r{   r  r  )
r  r  r  cacherK  rX  rY  	nn_module
real_valuer  s
    `        rQ   r  r  3
  sl   
 '&&&&&#Eu}}T{	B8=((	DK ++++ LD&
 
]zTY66y$,,	]'24;?	i(( 	'i00II It&v&&&&	RfdD&)DD
 d R R RA''66qGGTQRs   C   
D *1DD c                   ddl mm} fd}|                                 D ])\  }} ||          rJ d| d ||                       *|                                 D ])\  }} ||          rJ d| d ||                       *d S )Nr   )FakeTensorConfigr=   c                N    j         rdd l}d |j        | j                   S dS )Nr   z"FAKE TENSOR CREATION TRACEBACK: 
 zNEnable TORCH_FAKE_TENSOR_DEBUG=1 to get creation stack traces on fake tensors.)debug	tracebackformat_list_debug_trace)rJ  r  r  s     rQ   stack_or_hintz7assert_no_fake_params_or_buffers.<locals>.stack_or_hint]
  s?    ! 	d`9N9Nq~9^9^```ccrP   zUnexpected fake buffer r   zUnexpected fake param )torch._subclasses.fake_tensorr  r=   named_buffersnamed_parameters)ro  r=   r  r[   bufferrh  r  s         @rQ    assert_no_fake_params_or_buffersr  Z
  s/   GGGGGGGGd d d d d ((** D Df7
 
 	D 	DCTCCMM&,A,ACC	D 	D 
 	D **,, B Be7
 
 	B 	BADAA==+?+?AA	B 	B 
 	BB BrP   r   c                $    | j          d| j         S )z9
    Returns the fully qualified name of the object.
    r  )rK   rL   rA  s    rQ   fqnr  o
  s     n11s/111rP   c                6    t           j        j        j        r| S |S rd   )r   r  r9   assume_static_by_default)count1count2s     rQ   ifdynstaticdefaultr  v
  s    }4 rP   r  types.ModuleTypec           
     F   t          t          j        t          j                            t          t          | j                                                D ]I}|                    d          r2|d         dk    r&t          j
        | j         d|dd                     JdS )z@
    Ensure all the files in a given submodule are imported
    z.pyr   r2  r  N)rb  r  listdirr   r>  r
   r{   __file__r   	importlibimport_modulerJ   )r  r9  s     rQ   import_submoduler  }
  s     2:bgood36M6M&N&NOOPP G GU## 	Gs(:(:#s|$E$Ehssm$E$EFFFG GrP   r   c                :    t          t          |                     S rd   )class_has_getattributer4  r  s    rQ   object_has_getattributer  
  s    !$u++...rP   r\   r4  c                    	 t          t          j        | d          t          j                  rdS n# t
          $ r Y nw xY wdS )N__getattribute__TF)r~  r  r  r  r  AttributeErrorrf   s    rQ   r  r  
  sc    "3(:;;
 
 	 4		
    5s   -2 
??ignore_nn_module_getattrc                    	 t          j        t          |           d          }n# t          $ r d }Y nw xY w|r|t          j        j        j        u rd }|S )N__getattr__)r  r  r4  r  r   r  r  r  )r   r   
getattr_fns      rQ   get_custom_getattrr  
  si    +DKKGG

   


 J%(/2M$M$M
s   "% 44c                      e Zd ZdZdZdZdS )TensorStaticReasonrH   rB  r   N)rJ   rK   rL   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYr;   rP   rQ   r  r  
  s         IJrP   r  r  c                    | t           j        k    rdS | t           j        k    rdS | t           j        k    rdS t	          d|            )Nz>mark_dynamic on parameter, parameters are always static today.z2mark_dynamic on a non tensor, how did this happen?z4tensor is static because it is nn module associated.zIllegal reason )r  r  r  r	  r  )r  s    rQ   tensor_static_reason_to_messager  
  sX    #---OO#...CC#666EE
3633
4
44rP   r9  Union[torch.Tensor, Any]	is_tensortensor_sourcer'   )Tuple[bool, Optional[TensorStaticReason]]c                   ddl m} |                                                                s&|                                                                rt
          j        rdt          j        fS t          |           t          j        j        u s ||          rt
          j        rdt          j        fS |sdt          j        fS dS )a@  
    Given a tensor, source, and is_tensor flag, determine if a shape should be static.

    Args:
    tensor - the real tensor to evaluate, parameters force a static shape.
    is_tensor - internal dynamo check, essentially "is_tensor": target_cls is TensorVariable,
    tensors not in a TensorVariable for whatever reason are forced static.

    Returns a tuple, where the first element is the bool of whether or not this tensor should have a static shape.
    The second element is a TensorStaticReason, useful for passing to tensor_static_reason_to_message if needed.
    r8   ))is_from_unspecialized_param_buffer_sourceT)FN)sourcer  guard_sourceis_specialized_nn_module"is_unspecialized_builtin_nn_moduler9   &force_nn_module_property_static_shapesr  r	  r4  r   r  r  force_parameter_static_shapesr  r  )r9  r  r  r  s       rQ   tensor_always_has_static_shaper  
  s      BAAAAA 	""$$==??;%%''JJLL; 
7; '::: 	V***44]CC 	+

. 	+ '111 3'222;rP   c                0      fd}t          |          S )Nc                     	 ddl m }  n0# t          $ r# dt          t                              z   cY S w xY wd j        j        D             } | |g d          }t          j        j        j	        |          S )Nr   )r   zkTabulate module missing, please install tabulate to log the graph in tabular format, logging code instead:
c                N    g | ]"}|j         |j        |j        |j        |j        g#S r;   )rK  r[   r  rX  rY  )r   rM  s     rQ   r   z<lazy_format_graph_tabular.<locals>.inner.<locals>.<listcomp>
  s:     
 
 
;<QT1618QVQX6
 
 
rP   )opcoder[   r  rX  rY  r}   )
r   r   r{   r/   graphrJ  r.   forward__code__r  )r   
node_specs	graph_strr   ro  s      rQ   innerz(lazy_format_graph_tabular.<locals>.inner
  s    	))))))) 	 	 	~,Wb99::;  	
 
@B
 
 

 H N N N
 
 
	 "'2:+>+JIVVVs   
 *77r6   )r   ro  r"  s   `` rQ   lazy_format_graph_tabularr#  
  s8    W W W W W W" erP   c                l    |  d| d| d| dt          j        |                                            d
S )Nr   z line z 
r~   )disBytecode)prefixr[   r9  line_nor7  s        rQ   format_bytecoder)  
  sG    VVtVVhVVgVV#,t:L:L:P:P:R:RVVVVrP   _forward_pre_hooks_forward_hooks_backward_pre_hooks_backward_hooks)_state_dict_pre_hooks_state_dict_hooks_load_state_dict_pre_hooks_load_state_dict_post_hooksc                     t          t          j        j        j        j                  p't          t          j        j        j        j                  S rd   )r  r   r  modulesrR  _global_backward_hooks_global_backward_pre_hooksr;   rP   rQ   nn_module_has_global_hooksr6  
  s@     ux&=>> #:C C rP   c                @   g }| o| o| }|s|r|                     t                     |s|r|                     t                     |r|                     t                     g }|D ]5}t	          | |g           }|D ]}	||	         }
|                    |
            6|S )z
    Sometimes its useful to differentiate between types of hooks such as forward/backward/pre
    hooks executed during module.__call__, and state_dict hooks which are executed separately.
    )extendforward_hook_namesbackward_hook_namesstate_dict_hook_namesrN  r   )r  check_forward_hookscheck_backward_hookscheck_state_dict_hookshook_dicts_to_checkcheck_all_hooks	all_hookshook_dict_namehooks	hook_namer  s              rQ   nn_module_get_all_hooksrE  
  s      	'$$	'&& 
  7o 7""#5666 8 8""#6777 :""#8999I- # #^R00 	# 	#I#DT""""	# rP   c                F    t          | |||          }t          |          S )zL
    Helper function to check if a module has any hooks attached to it.
    )r<  r=  r>  )rE  r   )r  r<  r=  r>  rC  s        rQ   nnmodule_has_hooksrG     s2     $/15	  E ;;rP   c                `   t          |           r| S t          | t          j                  rt	          | j                  S t          | t          j                  r|                     d          S t          | t          t          f          r" t          |           d | D                       S | S )z0Convert tensor and tnp.ndarray to numpy.ndarray.T)forcec              3  4   K   | ]}t          |          V  d S rd   )to_numpy_helperr   rq  s     rQ   r   z"to_numpy_helper.<locals>.<genexpr>;  *      AAC?3//AAAAAArP   )r=   r~  tnpr  rK  r9  r   r  numpyr  r   r4  r  s    rQ   rK  rK  2  s    u~~ %%% u|,,,	E5<	(	( {{{&&&	EE4=	)	) tE{{AA5AAAAAArP   c                2   t           J t          | t           j                  rt          j        |           S t          | t
          j                  r| j        S t          | t          t          f          r" t          |           d | D                       S | S )zeConvert tnp.ndarray to tensor, leave other types intact. If a list/tuple, loop through it to convert.Nc              3  4   K   | ]}t          |          V  d S rd   )numpy_to_tensorrL  s     rQ   r   z"numpy_to_tensor.<locals>.<genexpr>H  rM  rP   )
r  r~  r  r   rd  rN  r9  r  r   r4  r  s    rQ   rR  rR  @  s    >>>%$$ &u%%%%%% |	EE4=	)	) tE{{AA5AAAAAArP   c                  "    e Zd Zd ZddZd ZdS )numpy_to_tensor_wrapperc                <    || _         d| j         j        z   | _        d S Nwrapped_rD  rJ   )r   rD  s     rQ   r   z numpy_to_tensor_wrapper.__init__N  s    "TV_4rP   rz   r{   c                "    d| j         j         dS )Nz<Wrapped function <original >>rX  r   s    rQ   __repr__z numpy_to_tensor_wrapper.__repr__R  s    AdfoAAAArP   c                :     | j         |i |}t          |          S rd   )rD  rR  r   rX  rY  r   s       rQ   r  z numpy_to_tensor_wrapper.__call__U  s&    dfd%f%%s###rP   Nrz   r{   )rJ   rK   rL   r   r[  r  r;   rP   rQ   rT  rT  M  sJ        5 5 5B B B B$ $ $ $ $rP   rT  c                   t          | t          j                  rt          | |          }t	          |          S t          | t
          j                  r1t          t          j        |           |          }t	          |          S d S rd   )r~  rN  r  rN  rR  r   r  )rq  r[   r   s      rQ   numpy_attr_wrapperr`  Z  sx    #s{## $c4  s###	C	&	& $ck#&&--s###$ $rP   c                  (    e Zd ZdZd	dZd
dZd ZdS )numpy_method_wrapperzgConvert obj from torch.Tensor to tnp.ndarray and call method. Then convert result back to torch.Tensor.methodr{   c                2    || _         d| j         z   | _        d S rV  )rc  rJ   )r   rc  s     rQ   r   znumpy_method_wrapper.__init__f  s    "T[0rP   rz   c                    d| j          dS )Nz<Wrapped method <original rZ  )rc  r   s    rQ   r[  znumpy_method_wrapper.__repr__j  s    ;DK;;;;rP   c                    |d         }t          |t          j                  rt          j        |          }t          || j                  } ||dd          i |}t          |          S Nr   r8   )r~  r   r  rN  r  rN  rc  rR  )r   rX  rY  rq  method_callabler   s         rQ   r  znumpy_method_wrapper.__call__m  si    1gc5<(( 	#+c""C!#t{33otABBx2622s###rP   N)rc  r{   r^  rJ   rK   rL   rp  r   r[  r  r;   rP   rQ   rb  rb  c  sQ        qq1 1 1 1< < < <$ $ $ $ $rP   rb  c                  (    e Zd ZdZd
dZddZd Zd	S )numpy_operator_wrapperzQImplements dunder methods for tnp.ndarray via functions from the operator libraryrK  Callable[..., Any]c                2    || _         d|j         | _        d S rV  )rK  rJ   )r   rK  s     rQ   r   znumpy_operator_wrapper.__init__y  s    02;00rP   rz   r{   c                    d| j          dS )Nz<Wrapped operator <original rZ  )rJ   r   s    rQ   r[  znumpy_operator_wrapper.__repr__}  s    ?dm????rP   c                T    |rJ d |D             } | j         | }t          |          S )Nc              3  v   K   | ]4}t          |t          j                  rt          j        |          n|V  5d S rd   )r~  r   r  rN  r  r  s     rQ   r   z2numpy_operator_wrapper.__call__.<locals>.<genexpr>  sS       
 
KN
3 = =FCK3
 
 
 
 
 
rP   )rK  rR  r]  s       rQ   r  znumpy_operator_wrapper.__call__  sH    z
 
RV
 
 
 dgtns###rP   N)rK  rl  r^  ri  r;   rP   rQ   rk  rk  v  sU        [[1 1 1 1@ @ @ @$ $ $ $ $rP   rk  c                "   t          | t                    s| S | j        rg }|                                 D ]n}t          |t          j                  r=|                    |j        j        	                    |j        j
                             Y|                    |           og }|                                 D ]n}t          |t          j                  r=|                    |j        j        	                    |j        j
                             Y|                    |           on(|                                 }|                                 }t	          j        ||| j        | j        | j                  }|                                 |S )N)r  r  r  )r~  r<   _has_symbolic_sizes_stridesr  r   SymIntr   r  r  	size_hintexprr  empty_stridedr  r  r  zero_)r   r  r  r  r  s        rQ   defakerx    se   a$$  	$  	 	A!U\** AF,66qv{CCDDDDA 	! 	!A!U\** !af.88EEFFFFa    		! vvxxgxo	 	 	A GGIIIHrP   c                0    dd l }| |j        j        j        u S r   )torch.utils.checkpointrq  
checkpoint)rq  r   s     rQ   is_utils_checkpointr|    s!    !!!!%+(333rP   c                    ddl m} | |u S )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphr~  )rq  r~  s     rQ   is_invoke_subgraphr    s     SSSSSS---rP   c                 H    ddl m}  |j        t          j        j        fi | S )Nr8    TorchHigherOrderOperatorVariable)variables.higher_order_opsr  maker   _higher_order_opsinvoke_subgraph)optionsr  s     rQ   build_invoke_subgraph_variabler    sB    LLLLLL0+0/ 
  rP   c                     dd l mc m} ddlm} |j        }t          j        j        j	        r|j
        } |j        |fi | S )Nr   r8   r  )torch._higher_order_ops.wrapr  wrapr  r  tag_activation_checkpointr   
_functorchr9   functionalize_rng_opswrap_activation_checkpointr  )r  higher_order_opsr  activation_checkpoint_ops       rQ   build_checkpoint_variabler    s    ;;;;;;;;;LLLLLL
 	2  4 O#3#N 0+0  
  rP   c                f    ddl m}  |            }| dk    rn| dk    r|rt                      }nd}|S )Nr8   )is_dynamo_supportedr[  rz  F)
eval_framer  r2   )device_typer  compile_supporteds      rQ   is_compile_supportedr    s[    //////++--e			#4	&LL!rP   offsetc                    |                      d          }t          |d|                             dd                    S )z
    Convert byte offset `offset` of `str` into character offset.
    Byte offset is used for 3.11+ instruction column data.
    Takes things like unicode characters into consideration.

    Unchanged from CPython implementation.
    zutf-8Nr
  )errors)encoder  decode)r{   r  as_utf8s      rQ   _fix_offsetr    s?     jj!!Gwww&&wy&AABBBrP   c                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )_AnchorsrW   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetNr\  r;   rP   rQ   r  r    sD          rP   r  segmentOptional[_Anchors]c                   t           j        dk    sJ ddl}	 |                    d| z   dz             }n# t          $ r Y dS w xY wt          |j                  dk    rdS |                     d          fd}fd	fd
}fd}|j        d         }t          ||j	                  r|j
        }t          ||j                  rt          t          |j        j                  dz
  } |||j        j                  }	 ||	          \  }}	|         |	         x}
                                s|
dv rI|
dv r |||	          \  }}	n |||	          \  }}	|         |	         x}
                                E|
dv I|	dz   }|t          |                   k     r+|         |         x}
                                s	|
dvr|dz  }t%          ||	||          S t          ||j                  rt          t          |j
        j                  dz
  } |||j
        j                  } ||          \  }}|         |         dk    r! |||          \  }}|         |         dk    !t          t          |j                  dz
  } |||j                  }t%          ||||          S t          ||j                  rt          t          |j        j                  dz
  } |||j        j                  } ||          \  }}|         |         dk    r! |||          \  }}|         |         dk    !t          t          |j                  dz
  } |||j                  }t%          ||||          S dS )a  
    Given source code `segment` corresponding to a bytecode
    instruction, determine:
        - for binary ops, the location of the binary op
        - for indexing, the location of the brackets.
    `segment` is expected to be a valid Python expression
    )rI      r   Nz(
z
)r8   r~   c                0    t          |          |          S rd   )r  )linenor  liness     rQ   	normalizez-_extract_anchors_from_expr.<locals>.normalize  s    5=&111rP   c                &   | t                    k     rL|t          |                    k    r3d}| dz  } | t                    k     r|t          |                    k    3| t                    k     r|t          |                    k     sJ | |fS rg  r  )r  colr  s     rQ   next_valid_charz3_extract_anchors_from_expr.<locals>.next_valid_char  s    s5zz!!cSv-?-?&?&?CaKF s5zz!!cSv-?-?&?&? E

""sSv-?-?'?'?'??s{rP   c                    |dz  } | |          \  } }| t                    k     r|t          |                    k     sJ | |fS r   r  r  r  r  r  s     rQ   r   z-_extract_anchors_from_expr.<locals>.increment!  sX    q%ofc22E

""sSv-?-?'?'?'??s{rP   c                    d}| dz  }  | |          \  } }| t                    k     r|t          |                    k     sJ | |fS rg  r  r  s     rQ   nextlinez,_extract_anchors_from_expr.<locals>.nextline(  s]    !%ofc22E

""sSv-?-?'?'?'??s{rP   rH   z)\#z\#r   r  )r  r  astparseSyntaxErrorr  bodyro  r~  Exprr   BinOpr
   rW   left
end_linenoend_col_offsetisspacer  	SubscriptCallr  )r  r  treer  r   r  	statementru  
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor  r  s                  @@rQ   _extract_anchors_from_exprr    sH    w&&&&JJJ yy5011   tt 49~~tMM$E2 2 2 2 2
               	!I)SX&& >LdCI&& <	L c49#7881<Ji
DI,DEEG"1/*g"F"FJ z*7332<<>> I",,;;*2(:w*G*G'J*3)J*H*H'J	 z*7332<<>> I",,  !ICj 12222$Z0;;DDFF 3eOOQ	 JYGGGcm,, 	L sDJ$9::Q>K ydj.GHHH$3OK$J$J!K$X.#55(1	+x(H(H%X $X.#55  T_559L!	,0CDDIK<KKKch'' 	L
 sDI$899A=K ydi.FGGH$3OK$J$J!K$X.#55(1	+x(H(H%X $X.#55  T_559L!	,0CDDIK<KKK4s   6 
AAr7  types.CodeTypeinstdis.Instructionc                Z    t           j        dk    rft          j         j        |j        j         j        |j        j        |j        j	        |j        j
                  }t          j        |g          d         }d                    |                                dd                   } fdt          |j        j        |j        j        dz             D             }t          j        d                    |                                                    }t#          |d                   t#          |d                   z
  }|d         d|         }t          j        t          j        |          |          }|S |j        J |j        j        dS t'          j         j        |j        j                                                  }|j        j        |S |j        j	        |j        j
        |S t-          ||j        j	                  }	d}
d}g }|j        j        |j        j        k    rGt-          ||j        j
                  }
||	|
         }|                    d	|	z  d
|
|	z
  z  z              n||	d         dz   }|                    d	|	z  d
t#          |          |	z
  z  z              t'          j         j        |j        j                                                  }t-          ||j        j
                  }
t          |j        j        dz   |j        j                  D ]}t'          j         j        |                                          }||dz   z  }t#          |          t#          |                                          z
  }|                    d	|z  d
t#          |          |z
  z  z              ||d|
         z  }t#          |          t#          |                                          z
  }|                    d	|z  d
|
|z
  z  z              d}	 t3          |          }n# t4          $ r Y nw xY w|d |D             }nd |D             }|j        dk    r|xj        |	z  c_        |j        dk    r|xj        |	z  c_        t          t#          |                    D ]}t          t#          ||                             D ]e}||j        k     r||j        k    r||j        k     r%||j        k    r||j        k    r<||j        k    rH||         |         d
k    rd||         |<   fd |D             }d}t          t#          |                    D ]O}|t'          j         j        |j        j        |z                                             dz   z  }|||         dz   z  }P|S )a  
    Python 3.11+ only. Returns lines of source code (from code object `code`)
    corresponding to `inst`'s location data, and underlines relevant code to `inst`.

    Example: CALL on `g`:
    f(g(
      ^^
        h(x)))
        ^^^^^

    We need our own implementation in < 3.13 since `format_frame_summary` in
    Python's `traceback` module doesn't handle multi-line expressions
    (and their anchor extraction code is not completely correct).
    )rI      )r  colno	end_colnor   r~   r8   Nc                h    g | ].}t          j        j        |                                          /S r;   )	linecachegetliner  rstrip)r   r  r7  s     rQ   r   z.get_instruction_source_311.<locals>.<listcomp>  sE     
 
 
 d.77>>@@
 
 
rP   rh  r   ~c                :    g | ]}|                     d d          S )r  ^)r
  r   markers     rQ   r   z.get_instruction_source_311.<locals>.<listcomp>  s&    BBB6>>#s++BBBrP   c                ,    g | ]}t          |          S r;   r   r  s     rQ   r   z.get_instruction_source_311.<locals>.<listcomp>  s    +O+O+OVDLL+O+O+OrP   r  c                8    g | ]}d                      |          S )rh  r  r  s     rQ   r   z.get_instruction_source_311.<locals>.<listcomp>  s"    AAAv2776??AAArP   )r  r  r  FrameSummaryr  	positionsr  r3  r  
col_offsetr  r  r   
splitlinesr  textwrapdedentr  indentr  r  r  r  r   lstripr  r  r  r  r  r  )r7  r  frame_summaryr  
orig_linesorig_lines_dedent
indent_lenr  
first_linestart_offset
end_offsetr  markers	last_liner  line
num_spacesanchorsmutable_markersr  r  s   `                    rQ   get_instruction_source_311r  s  s    7""!.N!L~0.+n3
 
 
 &77:6,,..qrr233
 
 
 
 5t~7PST7TUU
 
 

 %ODIIj,A,ABBMMOOA''#.?.B*C*CC
A{
{+!8!8&AA>%%%~$r "4#3T^5JKKRRTTJ~ (~ (DN,I,Q z4>+DEEL JGG ~ DN$999 T^-JKK
\*45s\)C:3L,MMNNNN\]]+d2s\)C3z??\3Q,RRSSS%dn7
 

&(( 	 !DN,IJJ
DN1A5t~7PQQ 	N 	NF$T%5v>>EEGGDtd{"GTS%7%77JNN3+cSYY5K.LLMMMM9[j[))^^c)*:*:*<*<&=&==
sZ'#j1H*IIJJJ"&G,W55    BB'BBB ,P+Ow+O+O+O "a''##|3##%**&&,6&& CLL)) 	7 	7FS!899:: 7 7G333W444w?V9V9Vg888w999G666"6*3/36636OF+C07  BAAAAF3w<<   $ $d.0E0IJJQQSS	
 	'!*t##Ms   P) )
P65P6c                \    t          | t          j                  rt          | dd           S d S )N_dynamo_static_input_type)r~  r   r  rN  r:  s    rQ   get_static_address_typer    s.    !U\"" =q5t<<<4rP   c                   t           j        j        j        t           j        j        t           j        t           j        j        f}t           j        j        j        t           j        j        t           j        t           j        j        f}| g ||R v S rd   )	r   r!  r   	get_statedefault_generatorr&  rz  	set_stater(  )r   getterssetterss      rQ   is_rng_state_getter_or_setterr    ss     	$)
 G 	$)
 	G (g(((((rP   c                    t          | t          j                  o'| j        dk    o| j        j        t          j        j        u S )N__get__)	r~  r  r   rJ   __self____objclass__r   r!  _TensorBaser  s    rQ   is_tensor_base_attr_getterr    s?    5%122 	@Ni'	@N'58+??rP   c                "    t          | d          S )N__torch_function__)r  r  s    rQ   is_torch_function_objectr    s    5.///rP   r  ,torch._dynamo.variables.base.VariableTrackerc                   ddl m} ddlm} |                                 s2t          | d          rYt          |                                 d          r7t          | |          rdS t          | |          ot          | j        d          S dS )Nr   )UserDefinedObjectVariable)TensorWithTFOverrideVariable
peek_valuer  TF)	torch._dynamo.variablesr  &torch._dynamo.variables.torch_functionr  is_realizedr  r  r~  r   )r  r  r  s      rQ   has_torch_functionr    s    AAAAAASSSSSS 
~~ 
L!!
&-bmmoo?S&T&T
 b677 	4"788 
WH*>
 >
 	
 5rP   c                    d }d }t           j        j                                        x}r| |j        v r|j        |          }|j        }|                    | d||          S )NF)static_shapessymbolic_contextr  )r   rv  r(   try_gettensor_to_contextr  from_tensor)rJ  r  r  r  tracing_contexts        rQ   to_fake_tensorr  5  sv    F-6>>@@@ 4111.@C%3F  	1A& !   rP   c                    t          |            oOt          |            o?t          |           o0t          | d          o t          | j        d          o| j        j        S )N__dataclass_params__frozen)r  r  r   r  r  r  r  s    rQ   is_frozen_dataclassr  C  st    #E*** 	.&u---	.	. E122	. E.99		.
 &-rP   c                x    |D ]$}t          | |          rt          | |          c S %t          |  d|           )zX
    Return the first available attribute or throw an exception if none is present.
    z% does not has any of the attributes: )r  rN  r  )rq  attrsattrs      rQ   get_first_attrr!  N  s^      & &3 	&3%%%%%	& CMMeMM
N
NNrP   c              #     K   | sd V  d S fd}t           j        j                            |          5 }|V  d d d            d S # 1 swxY w Y   d S )Nc                :    d }t          j        | |          S )Nc                    t           j        j        j        d         dxx         dz  cc<   t           j                            | |          S )Ncompiled_autogradcompilesr8   )r   r  rq  r@   rE  compile)gm_example_inputs_s     rQ   inner_compilerzKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compiler`  sE    #,-@A*MMMQRRMMM..sODDDrP   )r  	fullgraphdynamic)r   r'  )ro  r*  r,  r+  s     rQ   compiler_fnz3maybe_enable_compiled_autograd.<locals>.compiler_fn_  s9    E E E =Ni   rP   )r   r  r%  _enable)should_enabler+  r,  r-  r  s    ``  rQ   maybe_enable_compiled_autogradr0  Y  s       	 	 	 	 	 	 ],44[AA 	SIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A		AAc                 V     G d dt                     } t           |                       S )Nc                      e Zd ZdS )*invalid_removeable_handle.<locals>.InvalidN)rJ   rK   rL   r;   rP   rQ   Invalidr3  n  s        rP   r4  )r  r4   )r4  s    rQ   invalid_removeable_handler5  l  sB        $    7799%%%rP   c                    t          | t          j        j                  s| S t          | t          j        j                  r| S | j                            | j                  }| j        |_        |S rd   )	r~  r   r  r  r    GraphModuler>  rL  re  )r  proxys     rQ   nn_module_proxyr9  x  s^    c58?++ 
#ux+,, 
M!!#-00E\ENLrP   c                  $     e Zd Z fdZd Z xZS )	GmWrapperc                d    t                                                       || _        || _        d S rd   )r<  r   ro  unflatten_fn)r   ro  r=  r>  s      rQ   r   zGmWrapper.__init__  s.    (rP   c                Z    t          |          } | j        |                     |           S rd   )r   ro  r=  r  s     rQ   r  zGmWrapper.forward  s*    t**tw))$//00rP   )rJ   rK   rL   r   r  r?  r@  s   @rQ   r;  r;    sG        ) ) ) ) )
1 1 1 1 1 1 1rP   r;  ro  torch.fx.GraphModulec                  	
 d t          | j        j                  D             
t          j        j        j        rg
dgk    sJ t          |d         t                    sJ t          |d                   d 	fd} |t          | |           	|                    n]t          j        |          \  }}t          j        t          j        |          } |t          | |          |          t          j        		
fd}|S )z
    Mutate inputs so that they are flat and wrap gm such that it
    accepts those inputs.  This is needed for graphs that take
    bumpy inputs.
    c                d    g | ]-\  }}|j         d k    |j                            dd          +|.S )r  	steal_argF)rK  r  rh   )r   r  r  s      rQ   r   z(flatten_graph_inputs.<locals>.<listcomp>  sH       At7m##	k5(I(I# 	
###rP   r   c                B    | d         t          | dd                    z   S rg  r  r  s    rQ   
flatten_fnz(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qrr(^^++rP   c                0    | d          g| d          R S rd   r;   )	flat_argsboxed_inputs_counts    rQ   r=  z*flatten_graph_inputs.<locals>.unflatten_fn  s,    1112TY?Q?R?R5STTTrP   )treespecc                 n     |           }D ]}| |                                            |          S rd   )re   )rX  rF  r  compiled_fnrD  inputs_idx_to_clears      rQ   wrapperz%flatten_graph_inputs.<locals>.wrapper  sH    Jt$$	 % 	 	AGMMOOOO {9%%%rP   )	enumerater  rJ  r   r  r%  in_compiled_autograd_regionr~  r   r  r;  r  tree_flattenr  r  tree_unflattenarg_tree_leaves)ro  inputs
compile_gmr=  flat_inputsspecrL  rG  rJ  rD  rK  s          @@@@rQ   flatten_graph_inputsrV    sT     00   }&B , #qc))))&)T***** ^^	, 	, 	,	U 	U 	U 	U 	U !j2|!<!<jj>P>PQQ #/77T ()>NNN j2|!<!<kJJ+
& & & & & & & NrP   c                    t          | t          j        j                  rt	          | d          sg S | j                            dg           S )Nr  locals_to_steal)r~  r   r    r7  r  r  rh   )maybe_gms    rQ   get_locals_to_stealrZ    sH    h 455 WXv=V=V 	=.333rP   c                    || j         d<   d S )NrX  )r  )ro  rX  s     rQ   set_locals_to_stealr\    s    !0BGrP   c                      e Zd Zd ZddZdS )Litc                    || _         d S rd   r  )r   r  s     rQ   r   zLit.__init__  s    rP   rz   r{   c                    | j         S rd   r`  r   s    rQ   r[  zLit.__repr__  s	    vrP   Nr^  )rJ   rK   rL   r   r[  r;   rP   rQ   r^  r^    s7               rP   r^  zSet[str]warn_once_cachec                    | t           v rd S t                               |            t          j        | |dz              d S )Nr8   )
stacklevel)rb  r  warningswarn)r  rd  s     rQ   	warn_oncerg    sG     oM#*q.111111rP   c                V    t          j        d          }|                    d|           S )Nz\x1B[@-_][0-?]*[ -/]*[@-~]rh  )rG  r'  r  )textansi_escapes     rQ   strip_color_from_stringrk    s&    *:;;K??2t$$$rP   c               #     K   	 t           j        j                            d          } d V  t           j        j                            |            d S # t           j        j                            |            w xY w)NT)r   r!  	_autograd _saved_tensors_hooks_set_tracing)ru  s    rQ   +_disable_saved_tensors_hooks_during_tracingro    sk      C"CCDII;;EBBBBB;;EBBBBs   (A &A8c                 V    t           j        j        j        ot          j                     S rd   )r   rE  r9   freezingis_grad_enabledr;   rP   rQ   is_parameter_freezingrs    s!    ?!*J53H3J3J/JJrP   c                 L    d t          t                                D             S )Nc                ,    g | ]}t          |          S r;   ) get_torch_function_mode_stack_atr  s     rQ   r   z1get_torch_function_mode_stack.<locals>.<listcomp>  s.       01(++  rP   )r  r"   r;   rP   rQ   get_torch_function_mode_stackrw    s2     5:;T;V;V5W5W   rP   c                t    | t                      k     r| dk    sJ t          j                            |           S r   )r"   r   r!  _get_function_stack_at)inds    rQ   rv  rv    s7    *,,,,98**3///rP   c                    t          t                                D ]}t                       | D ]}t          |           d S rd   )r  r"   r#   r$   )r  r  modes      rQ   set_torch_function_mode_stackr}    sY    ,..// $ $!#### , ,%d++++, ,rP   c                 ^    t          t                                D ]} t                       d S rd   )r  r"   r#   )r  s    rQ   clear_torch_function_mode_stackr    s9    ,..// $ $!####$ $rP   c                 "    t                       d S rd   )
breakpointr  s    rQ   _breakpoint_for_c_dynamor    s    LLLLLrP   c                    | j         }t          j        |          }t          |j                  dk    rddlm}  |d          |j        | j        k    rddlm}  |d          d S )NrH   r8   )InternalTorchDynamoErrorz[Tensor subclass method __metadata_guard__ must take exactly two subclass metadata argumentsz?Tensor subclass method __metadata_guard__ must be a classmethod)	__metadata_guard__r  	signaturer  rm  r6  r  r  r>  )r   r  sigr  s       rQ   verify_guard_fn_signaturer    s    		!B

B

C
3>a111111&&i
 
 	
 
{eo%%111111&&M
 
 	
 &%rP   c                "   | j         t          j         t          j        j         fv ok| j        t          j        t          j        j        fv oG| j        t          j        t          j        j        fv o#| j        t          j        t          j        j        fv S rd   )r   r  rr   r   r   r   __iter__)user_clss    rQ   #does_not_override_dict_iter_methodsr     sx    4:{'>'DEE 	SO[-D-KLL	SMdi)@)EFF	S $-1H1Q!RR	rP   c                l    t           j                            d          d             } || |          S )NT	recursivec                ,    |                      |          S rd   )r  r   r  s     rQ   r  zcall_size.<locals>.fn.  s    vvayyrP   r   r  disabler   r  r  s      rQ   	call_sizer  -  s@    
]T**  +* 2a88OrP   c                l    t           j                            d          d             } || |          S )NTr  c                ,    |                      |          S rd   )r  r  s     rQ   r  zcall_stride.<locals>.fn6  s    xx{{rP   r  r  s      rQ   call_strider  5  s@    
]T**  +* 2a88OrP   c                j    t           j                            d          d             } ||           S )NTr  c                *    |                                  S rd   )storage_offsetr   s    rQ   r  zcall_storage_offset.<locals>.fn>  s    !!!rP   r  )r   r  s     rQ   call_storage_offsetr  =  s>    
]T**" " +*" 2a55LrP   c                ,     ddg} fd|D             }|S )Nr  tagc                b    i | ]+}|j         v |t          j        j         |                   ,S r;   )re  r  )r   r   rJ  s     rQ   r  z(_extract_tensor_dict.<locals>.<dictcomp>M  s>       ,/3!*CTCTTYqz#''CTCTCTrP   r;   )rJ  KEYS_TO_COPYtensor_dicts   `  rQ   _extract_tensor_dictr  G  s@    #L
   3?  K rP   z(Dict[int, weakref.ReferenceType[object]]user_obj_id_to_weakrefc                F    t          |                      }|
J d            |S )NzUser object is no longer alive)r  )obj_idrq  s     rQ   get_user_object_from_idr  Z  s'    
 
(
*
*C??<???JrP   c                \    t          |           }t          j        |           t          |<   d S rd   )ra  rl  rj  r  )rq  r  s     rQ   store_user_object_weakrefr  `  s(    WWF%,[%5%56"""rP   c                      e Zd ZU dZded<   dZded<   dZedd            Zedd	            Z	edd
            Z
edd            Zeed                         ZdS )CompileTimeInstructionCounterr   rW   _counterr  _idrz   r   c                n    | j         dz   | _         | j         dk    rt          j                    | _        d S d S r   )_depthr!   rV  r  rf   s    rQ   rV  z#CompileTimeInstructionCounter.startj  s5    Z!^
:??*022CGGG ?rP   c                    | j         dz
  | _         | j         dk    r0| xj        t          j        | j                  z  c_        d| _        d S d S )Nr8   r   r  )r  r  r!   endr  rf   s    rQ   r  z!CompileTimeInstructionCounter.endp  sJ    Z!^
:??LL04SW===LLCGGG ?rP   c                    d| _         d S r   r  rf   s    rQ   re   z#CompileTimeInstructionCounter.clearw  s    rP   c                    | j         S rd   r  rf   s    rQ   r   z#CompileTimeInstructionCounter.value{  s
    |rP   c              #     K   	 t           j        r|                                  d V  t           j        r|                                  d S d S # t           j        r|                                  w w xY wrd   )r9   %record_compile_time_instruction_countrV  r  rf   s    rQ   recordz$CompileTimeInstructionCounter.record  st      	; 		EEE; 					 v; 				s   $A #A/Nr  )rz   rW   )rJ   rK   rL   r  rt   r  r  ru   rV  r  re   r   r   r  r;   rP   rQ   r  r  e  s         HCMMMMF3 3 3 [3
    [    [    [   ^ [  rP   r  featureusagec                    t                                                      r%t                                          d| |           dS dS )zQ
    Records whether we are using a feature
    Generally a feature is a JK.
    r-  N)r   r   set_key_value)r  r  s     rQ   set_feature_user    sO     ((** M++OWeLLLLLM MrP   )rv   rw   rx   ry   rz   r{   r  )r   rW   rz   r   )rz   rD   )rz   r&   )NFNNF)r   r{   r   r   r   r   r   r   r   r   r   r   rz   r   r  )r   r   r   r   rz   r{   )r   r   r   r   rz   r   )r{   F)r   r   r^  )r9  r{   rz   r   )rF  rG  rz   rW   )r   r?   rz   r?   )rq  rr  rs  rt  rz   ru  )rq  rr  rs  ry  rz   ru  )rq  rr  rs  r{  rz   r   rd   )r   r  rz   r  )rL  r  rz   r   r  )
rO  rW   rP  rW   rQ  rA   rR  rS  rT  rU  )r  rW   rz   r   )rz   r  )rz   r  )FF)r   r{   r  r   r   r   rz   r   )r  r  )rz   rT  )r8   )rz   r  )T)rq  r   )r  r  )r   r   )r\   r4  )r   r   r   r   )r  r  )r9  r  r  r   r  r'   rz   r  )FFF)r{   r{   r  rW   rz   rW   )r  r{   rz   r  )r7  r  r  r  rz   r{   )r  r	  rz   r   )TT)ro  r?  )r  r{   r  r   (  
__future__r   atexitrr   r  r  rm  r|  r%  r  r  rs  r  r  r   rC  r  r#  r^  r  r  rG  r  r  r  r   r  r  r  r  re  rl  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   typing_extensionsr   r   r   torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreerq  _pytreer  r    torch._Cr!   r"   r#   r$   torch._dispatch.pythonr%   torch._dynamo.metrics_contextr&   torch._guardsr'   r(   torch._subclasses.meta_utilsr)   torch._utils_internalr*   r+   r,   r-   torch.fx._utilsr.   r/   torch.monitorr0   torch.nn.modules.lazyr1   torch.utils._tritonr2   r3   torch.utils.hooksr4   rO  r  ru  torch._loggingtorch._numpy_numpyrN  r5   r7   rh  r9   fftlinalgr%  r:   rt   NP_TO_TNP_MODULEr  r<   r=   r>   r   r?   r  r  r  unpatched_nn_module_getattrr  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implrs   r@   rB   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgr%  rJ   rq   rC   rS  rE   r^   timer_counterr  rG   rS   r   r   r   r   r   r   r   r   r   r   r   registerr   FloatTensorr  DoubleTensorr  double
HalfTensorr  halfBFloat16Tensorr_  
ByteTensorr  
CharTensorr  
LongTensorr  long	IntTensorr  rW   ShortTensorr  short
BoolTensorr   tensortype_to_dtyper   r  r  r  r  r0  r8  rE  rN  rP  rU  rZ  r\  rx  r  ParamSpecArgsParamSpecKwargs	ParamSpecTypeVarTupleTypeAliasTyper  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.  r_  r  !DEFAULT_COMPILATION_METRICS_LIMITr  r2  r7  rN  r  r   r  r  r  r  r  r   r  r  r  r  r  r  r  r  r-  r7  r=  r?  rD  rC  rd  rp  rz  r  r  r  r{   rV   r4  Ellipsisr>  NotImplementedCodeTyper  r  memory_formatr  finfoiinfo	attention
SDPBackendrz  _CudaDevicePropertiesr  r{  r  languager  r  r  r  r  r  r  r   r  r   r  r   r  r  r  r  r  __length_hint__tuple_iterator_lenrr  rL  r  r  r  r  r  	iter_nextr  r  r  r  r  r  r  r  r  r  r  GLOBAL_KEY_PREFIXtorch._subclassesr  r  r  r!  r&  r;  r,  rq  rw  orig_code_mapr   rx  ry  seen_code_mapr  r!  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r)  r9  r:  r;  all_hook_namesr6  rE  rG  rK  rR  rT  r`  rb  rk  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r!  r0  r5  r9  r;  rV  rZ  r\  r^  r   rb  rg  rk  ro  rs  rw  rv  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r;   rP   rQ   <module>r     s   " " " " " " "                



      				                  				 				 



                % % % % % % $ $ $ $ $ $       # # # # # #                                               0 . - - - - - - -      , , , , $ $ $ $ $ $ $ $ $                  < ; ; ; ; ; 8 8 8 8 8 8 0 0 0 0 0 0 0 0 = = = = = =            G F F F F F F F & & & & & & 1 1 1 1 1 1 > > > > > > > > - - - - - -   	BBB	......)))))) 
 FII	>
 	
 	
 	
 	
 FCGIszIsz	
  "VVVVVVVVVVV 	 	 	D	 GCLL#ho9  8?3  % : +B;+B;CV+W+W W W W W$&  & & & &G  Q V.VVV g!! 46  5 5 5 5
 .E[-DU-K-K  K K K K	!!    ty   %J %J %J %J %J %J %J %JP
 
 
 
 
                     > !           !%"',0.2!^B ^B ^B ^B ^BB 
    
 
,1    
" " " " "J 8 8 8 8
 
u{3	5	u}ej1	5>+	u{n	uzm	u{EJ/	Oek59-	U[1	uzm        ( #:"9";"; " " ", , , ,
 
 
	 	 	? ? ? ?
G G G G        	 	 	       B 
   
 
   
 
   
& & & w 	%!G G G  &      	 	 	  $3 3 3  % % %$ $ $, , , T     
 
 
0 0 0 05 5 5 5   F4 F4 F4 F4 F4 F4 F4 F4R %' ! 3D+2C,3 3 3     
# # # #L& & & &Ru3 u3 u3 u3r ">*DEEE % % % %! ! ! !
& & & &} } } } } } } }@ 59  8 8 8 8     ""'
 
 
 
 
@ ( ( ( ( ( ( ( ((         +       ).**    ! G G G G GT  &
 
 
 
 9 9 9"	 	 	   $  ( ( (
  < Q1 1 1 12  ,   "X X X 2   		DJJ	N	L	K		L	K	K	H!	J$'$     ,  5MMMfo3444      W W W
 
 
     "&bggii	 0 0 0 0%)T"))++%6%6 6 6 6 6&*d+B;+B+D+D+K+K+M+M&N&N N N N N&*d4488nn 4 4 4 4&*d44a>>&:&: : : : :#3 ^
  1 1 1  
 	     5 5 5  8 8 8  & & & &RM M M
@ @ @   B       
 !  = < < < < <7 7 7' ' '? ? ?
; ; ; i-2`F `F `F `FF	H H H
 
> 
> 
> '&(( /Fk.Ed.K.K K K K K LN  M M M M '&(( T, , ,& & &
     
/ / /C C C CL  	!!1 1 1 " " ". . .b$ $ $NB B B*2 2 2 2  G G G G/ / / /	 	 	 	           5 5 5 5   D  *W W W +,<= ,.?@     $&99<QQ    	   F  	   $  
 
 

$ 
$ 
$ 
$ 
$ 
$ 
$ 
$$ $ $$ $ $ $ $ $ $ $&$ $ $ $ $ $ $ $(  @4 4 4. . .    &
 
 
"	C 	C 	C 	C        v v v vrA A A AH  ) ) )"  0 0 0   4
 
 
  O O O    $& & &  1 1 1 1 1 1 1 1, , , ,^4 4 41 1 1         CEE ! ! ! !	2 	2 	2 	2% % % C C CK K K  0 0 0
, , ,$ $ $  
 
 
"        
 
 
  DF  E E E E  6 6 6
# # # # # # # #LM M M M M Ms%    E EEA/G G
G