
    {Yhzu                      U d Z 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!m"Z" ddlm#Z#m$Z$ ddlm%Z% ddlm&Z& ddlm'Z'm(Z( dd	lm)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 dd
l2m3Z3m4Z4m5Z5m6Z6 ddl7Z7ddl8Z7ddl9Z7ddl:m;c m<Z= ddl7m>Z> ddl?m@Z@mAZAmBZBmCZC ddlDmEZE ddlFmGZGmHZH ddlImJZJmKZKmLZL ddlMmNZN ddlOmPZPmQZQmRZRmSZSmTZT ddlUmVZVmWZW ddlXmYZY ddlZm[Z[ ddl\m]Z]m^Z^ ddl_m`Z` ddlambZb ejc        rddldmeZemfZfmgZgmhZhmiZimjZj 	 ddlkZln# em$ r dZlY nw xY w	 ddlnZ7ddlompZq ddlImrZr ddlnmsZs ddltmuZu elrCeleljv        eljw        eljx        fZydezd<   eleqeljv        eqjv        eljw        eqjw        eljx        eqjx        iZ{ndZyi Z{dd l|m}Z}m~Z~mZ n# e$ r Y nw xY w e0d!          Ze7j        j        j        Ze7j        j        j        Ze7j        j        j        Z ej        ej!                  Zd"ezd#<   i Zd$ezd%<   d&Zd'Zd(e d)Z ej        e          Zi Zd*ezd+<    ej        e          Zd,ezd-<    ej                    Z G d. d/e
j                  Z G d0 d1          Zdd8Zdadd:Zdd;Zdadd>Zdd?Zdd@ZdAezdB<   dCezdD<   ddEZddFZ G dG dHe
j                  Z G dI dJ          Z ej                    Ze$	 	 	 	 	 	 	 	 ddd[            Ze/ddd_            Ze/	 dddb            ZdddcZej        ddd            Ze7j        e7j        e7j        fe7j        e7j        e7j        fe7j        e7j        e7j        fe7j        e7j        fe7j        e7j        fe7j        e7j        fe7j        e7j        e7j        fe7j        e7j        e7j        fe7j        e7j        e7j        fe7j        e7j        fi
Z G de df          Z eǦ            Zdg ZɐddhZdi Zdj Z̐ddkZ͐ddmZΐddpZϐddrZds Zdt Z G du dv          Ze/dd|            Ze/dd~            Ze/dd            Zd Zej        dk    r&ej        ej        ej        ej0        ej        ej        fZd Zd Zd Ze/dd            Ze/dd            ZߐddZe1ej        ej        ej        ej        f         Zdezd<   ddZddZej        ej        ej        ej        ej        ej        dZdddddddZddZd Zd Zd Zd Zd Z ej&        d          d             ZddZd ZddZddZdZej         G d d                      ZdZ ej        e          adezd<   ddZ ddZddZddZ eGe          Z eHe          ZddZddZddÄZ G dĄ dŦ          Z	da
dezd<   ddȄZddɄZe$	 	 dԐdd̄            Zej         G d̈́ dΦ                      Z G dτ deӦ          Z e            e_        dф ZddҜdӄZdԄ ZddׄZe$d؄             Zdل ZddڄZdۄ Zd܄ Zd݄ Z ej&        d          dd߄            Zd ZddZd Z e&d          d             Zeee eŐe!e" e#d          e$j%        e&j%        ej'        e7j'        e7j(        e7j)        e7j*        e7j+        e7j,        e7j        j-        j.        e7j/        j0        hZ1dezd<    e^            r*ddl2Z2e13                    e2j4        j(                   	 d Z5ej6        d             Z7ddZ8d Z9d Z:d Z;d Z<d Z=d Z>d Z? e#i @                                          ZAdezd<    e#i B                                          ZCdezd<    e#i D                                          ZEdezd<    e# e"            B                                          ZFdezd<    e# eGd                    ZHdezd<    e# eG eId                              ZJdezd<   eHjK        ZLeMjN        ZOePjN        ZQd  ejR        ePjS        B                                e"jS        B                                          D             ZTeUjN        ZVd eUjS        B                                D             ZWd eXjS        B                                D             ZYeXjZ        Z[d e!jS        B                                D             Z\d  Z]d Z^d Z_d Z`d Zad ZbecZddڐdZed ZfePjZ        Zgd	 Zhd
 Zid Zjd ZkddZldېdZmd ZnddZoddZpdZqddlrmsZs d ZtdܐdZudKddݐdZvd Zwd Zxd ZyddKddKd dKdKejz        dKdKf
dސd"Z{d# Z|ej$        d$             Z} eӦ            Z~ ej        eX          Zd%ezd&<   g Zd'ezd(<    eӦ            Zej6        d)             Zd* Zdߐd+Zd, Zd- Zdd.Z ej                    Zd/ Ze$d0             Zd1 Zd2 Zd3 Zdd4Zd5 Zdd8Zdd9Zd: Zdd=Zddd?Z G d@ dAe
j                  ZddCZddJZdK ZdL ZdMdNgZdOdPgZg dQZeez   ez   ZdR Z	 	 	 ddSZ	 	 	 ddTZdU ZdV Z G dW dX          ZdY Z G dZ d[          Z G d\ d]          Zd^ Zd_ Zd` Zda Zdb Zdc Zdd ZddfZej         G dg dh                      ZddkZddpZdq Zdr Zds Zdt Zdu ZddxZdy Zdz Zd{ Zej$        dd|            Zd} Zd~ Z G d de7j        j                  ZddZd Zd Z G d d          Z eæ            ZĐdezd<   dؐdZŐd Zej$        d             Zǐd ZȐd Zɐd Zʐd Zːd Z̐d Z͐d Zΐd Ze7j        d             Ze7j        d             Ze7j        d             ZӐd Zi ZՐdezd<   d Z֐d Z G d d          ZؐddZِdZdezd<   d Ze$dd            Ze$dd            ZݐddZe7j        dd            Ze7j        dd            ZddZdS (  a  
Utility functions and classes used throughout the TorchDynamo system.

This module contains a collection of helper utilities used by various parts of Dynamo for:
- Performance metrics collection and reporting
- Compilation timing and debugging
- Graph manipulation and tensor operations
- Runtime guards and checks
- Common data structure operations
- Testing and development tools

This is an internal module that provides shared functionality used across the Dynamo codebase.
    )annotationsN)CounterOrderedDict)AbstractContextManagercontextmanager)is_dataclass)	lru_cache)CodeTypeMethodWrapperType)	AnyCallablecastClassVarGenericOptionaloverloadTypeVarUnion)Literal	TypeAlias	TypeGuardTypeIs)fx)_instruction_counter_len_torch_function_stack_pop_torch_function_stack_push_on_torch_function_stack)enable_python_dispatcher)MetricsContextRuntimeMetricsContext)	CompileIdSourceTracingContext)is_sparse_compressed)justknobs_check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   )_get_flat_args)	Generator	ItemsViewIterableIteratorKeysView
ValuesView)detect_fake_mode
LazyString)configztuple[types.ModuleType, ...]NP_SUPPORTED_MODULES )
FakeTensoris_fakemaybe_get_fake_modeTz*collections.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 )ReInplaceTriggerr1         N)__name__
__module____qualname__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPSr>       e/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/_dynamo/utils.pyrJ   rJ      s        LLJJJrS   rJ   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!collections.defaultdict[str, int]_valuestriggerrJ   bytesintc                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_bytes_)rW   name)clsrX   rY   s      rT   add_missed_bytesz"ReinplaceCounters.add_missed_bytes   sA    A::K6667775@77777 :rS   countc                F    |dk    r| j         d| xx         |z  cc<   d S d S Nr   missed_tensors_)rW   )r_   rX   ra   s      rT   add_missed_opportunitiesz*ReinplaceCounters.add_missed_opportunities   s?    A::K3'33444=44444 :rS   c                8    | j                                          d S N)rW   clearr_   s    rT   rh   zReinplaceCounters.clear   s    rS   c                `    d}t           D ]#}|| j                            d| d          z  }$|S rc   )rJ   rW   getr_   sumrX   s      rT   get_total_missedz"ReinplaceCounters.get_total_missed   sB    ' 	C 	CG3;??#>W#>#>BBBCC
rS   c                j    d}t           D ](}|| j                            d|j         d          z  })|S r\   )rJ   rW   rk   r^   rl   s      rT   get_total_missed_bytesz(ReinplaceCounters.get_total_missed_bytes   sD    ' 	F 	FG3;??#A7<#A#A1EEECC
rS   c                D    | j         rt          dd| j                    d S d S )Ninductorreinplace_counters)rW   r)   ri   s    rT   logzReinplaceCounters.log   s6     ; 	J:';S[IIIII	J 	JrS   N)rX   rJ   rY   rZ   )rX   rJ   ra   rZ   )rM   rN   rO   collectionsdefaultdictrZ   rW   __annotations__classmethodr`   re   rh   rn   rp   rt   r>   rS   rT   rV   rV      s         1H1H1M1MGMMMM A A A [A
 > > > [>   [   [   [ J J [J J JrS   rV   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   r{   
c              3  f   K   | ],}d                      t          t          |                    V  -dS ), N)joinmapr~   ).0rows     rT   	<genexpr>ztabulate.<locals>.<genexpr>   sG       
 
),DIIc#smm$$
 
 
 
 
 
rS   )tabulateImportErrorr   	itertoolschain)ry   r{   r   s      rT   r   r      s    
  w 777 
 
 
yy 
 
09	40P0P
 
 
 
 
 	
 	
 	

s    =AANonec                     t           dz   a d S Nr1   )
curr_framer>   rS   rT   increment_framer      s    aJJJrS   c                 n    t                                            t                                           dad S Nr   )rH   rh   rF   r   r>   rS   rT   reset_frame_countr      s/    ""$$$""$$$JJJrS   cntrZ   c                    t           | z  a d S rg   )op_count)r   s    rT   increment_op_countr      s    OHHHrS   c                     i } t                                           D ]\  }}|dz  | |<   |                     dd          |                     dd          z   | d<   | S )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rH   itemsrk   )total_by_keyphasetimings      rT   calculate_time_spentr     sx    L17799 + +v$slU&2&6&6' '2A66'7L"# rS   c            	         t                      } d}|                                 D ]\  }}| d| dt          |d           }t          |           d S )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       rT   print_time_reportr     se    '))L
C"((** / /
U..s..U5!__..	#JJJJJrS   r   _METRICS_CONTEXTr    _RUNTIME_METRICS_CONTEXTc                     t           S rg   )r   r>   rS   rT   get_metrics_contextr   9  s    rS   c                     t           S rg   )r   r>   rS   rT   get_runtime_metrics_contextr   =  s    ##rS   c                      e Zd ZdZdZdZdZdS )CompileEventLogLevelz
    Enum that loosely corresponds with a "log level" of a given event.

    CHROMIUM_EVENT: Logs only to tlparse.
    COMPILE_EVENT: Logs to tlparse + PT2 Compile Events
    COMPILATION_METRIC: Logs to tlparse, PT2 Compile Events, and dynamo_compile
    r1   rK   rL   N)rM   rN   rO   __doc__CHROMIUMPT2_COMPILECOMPILATION_METRICr>   rS   rT   r   r   A  s,          HKrS   r   c                     e Zd ZdZedej        fd!d            Ze	 d"d#d            Ze	 d"d$d            Z	ed%d            Z
edej        fd&d            Zed'd            Zeej        fd(d            Zed)d            Zed)d            Zed"d*d            Ze	 d+d,d            Zed)d            Zed              ZdS )-CompileEventLoggera  
    Helper class for representing adding metadata(i.e. columns) to various compile events.
    Use CompileEventLogger to add event data to:
    - Chromium events
    - PT2 Compile Events
    - CompilationMetrics

    This should be used in conjunction with dynamo_timed() and metrics contexts, which create
    timed spans and events. CompileEventLogger uses three log levels (described in CompileEventLogLevel),
    where each log level logs to all sources below it in the hierarchy.

    Example usages:
    - I want to log to an existing chromium event within dynamo timed:
    with dynamo_timed("my_event"):
        CompileEventLogger.chromium("my_event", foo=bar)

    - I want to log my event to both chromium + pt2_compile_events:
    with dynamo_timed("my_event", log_pt2_compile_event=True):
        CompileEventLogger.pt2_compile("my_event", foo=bar)

    - I want to add information to dynamo events and dynamo_compile
        CompileEventLogger.compilation_metric(foo=bar)
    N
event_namer~   metadatarD   time_nsOptional[int]	log_levelr   c                    |t          j                    }t                      }|t          j        k    rd}n"|t          j        k    rd}nt          d          |                    | |||           d S )NFTzjCannot log instant event at COMPILATION_METRIC level. Please choose one of CHROMIUM_EVENT or COMPILE_EVENT)timer   get_chromium_event_loggerr   r   r   RuntimeErrorlog_instant_event)r   r   r   r   chromium_loglog_pt2_compile_events         rT   r   z$CompileEventLogger.log_instant_eventh  s     ?lnnG022,555$)!!.:::$(!!|   	&&+@	
 	
 	
 	
 	
rS   F	overwriteboolobjectc                :   t                      }|                                }|t          j        k    r |j        | fi | dS |t          j        k    r8|                                }| |vrt          d|            |j        | fi | dS |t          j        k    sJ |                                }| |k    rt          d          t                      }|
                                st          d          |                    ||            |j        | fi | dS )z
        Centralized API for adding data to various events
        Log an event to a toplevel "dynamo" event or metrics context
        depending on log level.
        zError: specified log level PT2_COMPILE, but the event %s is not logged to pt2_compile_events. Make sure the event is active and you passed log_pt2_compile_event=True to dynamo_timedzLog level is COMPILATION_METRIC, but event_name isn't the toplevel event. CompilationMetrics must be logged to the toplevel event. Consider using `log_toplevel_event_data` directly.z[No metrics context is in progress. Please only call this function within a metrics context.N)r   get_pt2_compile_substackr   r   add_event_datar   r   r   get_outermost_eventr   in_progressupdate)r   r   r   r   r   pt2_compile_substack	top_eventmetrics_contexts           rT   add_datazCompileEventLogger.add_data~  sv    122+DDFF,555'L'
??h?????.:::#/#H#H#J#J !555"A 	   (L'
??h????? 4 GGGGG$88::IY&&"B   233O"..00 "q  
 ""8Y777'L'
??h?????rS   c                    t                                                      }|t          d          t          j        || |fi | dS )zC
        Syntactic sugar for logging to the toplevel event
        NzWNo toplevel event active. Please only call this function within a dynamo_timed context.)r   r   r   r   r   )r   r   r   r   s       rT   add_toplevelzCompileEventLogger.add_toplevel  s[     .//CCEE	i   	#Iy)PPxPPPPPrS   r   r   rZ   c                   t                      }|t          j        k    s|t          j        k    r|                    | ||           dS |t          j        k    sJ |                                }| |k    rt          d          t                      }|	                                st          d          |                    ||           |                    | ||           dS )z:
        Increments an existing field, or adds it
        zLog level is COMPILATION_METRIC, but event_name isn't the toplevel event. CompilationMetrics must be logged to the toplevel event. Consider using `increment_toplevel` directly.hNo metrics context is in progress. Please only call this function within a metrics context/dynamo_timed.N)
r   r   r   r   	incrementr   r   r   r   r   r   r   r   r   r   r   r   s          rT   r   zCompileEventLogger.increment  s     122-6660<<<"":sE::::: 4 GGGGG$88::IY&&"}  
 233O"..00 "~   %%c5111"":sE:::::rS   r1   c                    t                      }|                                }|t          d          t                              ||| |           dS )zX
        Increments a value on the toplevel metric. By default, logs to metric.
        N_No toplevel event active. Please only call this function within a metrics context/dynamo_timed.)r   r   r   r   r   r   r   r   r   r   s        rT   increment_toplevelz%CompileEventLogger.increment_toplevel  s]     122 4466	q   	$$Y	3FFFFFrS   r   c                   t                      }|t          j        k    s|t          j        k    r|                    | ||           dS |t          j        k    sJ |                                }| |k    rt          d          t                      }|	                                st          d          |                    ||           |                    | ||           dS )zl
        Add metadata <value> to a set of values with key <key>. Creates a set if it doesn't exist.
        zLog level is COMPILATION_METRIC, but event_name isn't the toplevel event. CompilationMetrics must be logged to the toplevel event. Consider using `add_to_set_metric` directly.r   N)
r   r   r   r   
add_to_setr   r   r   r   r   r   s          rT   r   zCompileEventLogger.add_to_set  s     122-6660<<<##JU;;;;; 4 GGGGG$88::IY&&"|  
 233O"..00 "~   &&sE222##JU;;;;;rS   c                    t                      }|                                }|t          d          t                              ||| |           dS )z
        Same as add to set, just does it automatically to the toplevel event instead of having to explicitly name it.
        Defaults to COMPILATION_METRIC log level.
        Nr   )r   r   r   r   r   r   s        rT   add_to_set_toplevelz&CompileEventLogger.add_to_set_toplevel	  s]     122 4466	q   	%%iCGGGGGrS   c                D    t          j        | t          j        fddi| dS )z
        Add <metadata> to <event_name> in chromium. Each key/value of metadata will appear in the chromium trace.
        <event_name> should be the name of a timed event span passed to `dynamo_timed`.
        r   FN)r   r   r   r   r   r   s     rT   chromiumzCompileEventLogger.chromium  sA     	#,5	
 	
AF	
JR	
 	
 	
 	
 	
rS   c                D    t          j        | t          j        fddi| dS )a  
        Add <metadata> to <event_name> in chromium and PT2 Compile Events.
        Each key/value of metadata will appear in the chromium trace. Each kwarg name becomes
        a column in PT2 Compile Events, with the corresponding kwarg value.
        <event_name> should be the name of a timed event span passed to `dynamo_timed`,
        with log_to_pt2_compile_events=True.
        r   FN)r   r   r   r   r   s     rT   pt2_compilezCompileEventLogger.pt2_compile'  sA     	#,8	
 	
DI	
MU	
 	
 	
 	
 	
rS   c                @    t          j        t          j        | fi | dS )a6  
        Add <metadata> to the CompilationMetrics context. Also logs to PT2 Compile Events
        and chromium.
        Each key/value of metadata will appear in the chromium trace. Each kwarg name becomes
        a column in PT2 Compile Events and Dynamo Compile, with the corresponding kwarg value.
        N)r   r   r   r   )r   r   s     rT   compilation_metricz%CompileEventLogger.compilation_metric4  s8     	' 3Y	
 	
BJ	
 	
 	
 	
 	
rS   c                T    t                               | ||t          j                   dS )z
        Log an instant event to chromium logs with name <event_name> at time <time_ns>. The `args` field in
        Perfetto will point to metadata. <time_ns> should be a value obtained from time.time_ns().
        N)r   r   r   r   )r   r   r   s      rT   instantzCompileEventLogger.instant@  s3     	,,'+?+H	
 	
 	
 	
 	
rS   c                ^    t                      sdS t                      } |j        | fi | dS )z
        Adds to an existing pt2_compile event, but silently returns if the event doesn't exist
        or ChromiumEventLogger is not initialized.
        This function is syntactic sugar for chromium_event_logger().try_add_event_data.
        N)chromium_event_log_activer   try_add_event_data)r   r   r   s      rT   try_add_pt2_compilez&CompileEventLogger.try_add_pt2_compileL  sC     )** 	F022''
??h?????rS   c                ~    t                      sdS t                      }|                                sdS  | |i | dS )z
        Special function that quietly runs a given method, returning if CHROMIUM_EVENT_LOG is None or metrics context is not set
        N)r   r   r   )	method_fnargskwargsr   s       rT   try_zCompileEventLogger.try_X  sV    
 )** 	F-//**,, 	F	4"6"""""rS   )r   r~   r   rD   r   r   r   r   F)r   r~   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r~   r   r   r   r~   r   rZ   )r   r~   r   rZ   r   r   )r   r~   r   r   r   r~   r   r   )r   r~   r   r   r   r   )r   r~   r   r   )r   r   r   r   rg   )r   r~   r   rD   r   r   )rM   rN   rO   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   rS   rT   r   r   O  s        0  "&*>*G	
 
 
 
 \
*   +@ +@ +@ +@ \+@Z ;@Q Q Q Q \Q ; ; ; \;<  *>*QG G G G \G  < < < \<<  +?*QH H H H \H& 
 
 
 \
 

 

 

 \

 	
 	
 	
 	
 \	
 LP	
 	
 	
 	
 \	
 	@ 	@ 	@ \	@ 	# 	# \	# 	# 	#rS   r   Fr   
phase_nameOptional[str]r   r   r   Optional[dict[str, object]]dynamo_compile_column_us
compile_idOptional[CompileId]is_backwardOptional[bool]log_waitcounterwaitcounter_name_overrideGenerator[Any, None, None]c	           
   #  `	  K   |r|}	| }
n| }	d}
| t           vr
g t           | <   i }|r|                    |           |
r|                    d|
i           ||                    d|i           t                      }t          j                    }|                    |	||||           t          j                            |  d          g}|r=|r|n| }|	                    t          d|                                                      t          j        j                                        du}|r|                    d          sJ t!          t"          d          sdt"          _        t"          xj        d	z  c_        t"          j        d	k    rl|	                    t          d
                                                     |s6d}|	                    t          |                                                     	 t'          j                    5 }|D ]}|                    |           dV  ddd           n# 1 swxY w Y   t          j                    }||z
  }t           |          	                    |dz             |                    |	|i |||           |rt.          |	xx         |z  cc<   t"          xj        d	z  c_        t"          j        dk    }|dz  }|rVt1                      }|                                r0|                    ||           |r|                    d|           dS dS dS t7                      }|                    ||           |r"|d| d}|                    d||           dS dS dS # t          j                    }||z
  }t           |          	                    |dz             |                    |	|i |||           |rt.          |	xx         |z  cc<   t"          xj        d	z  c_        t"          j        dk    }|dz  }|rSt1                      }|                                r/|                    ||           |r|                    d|           w w w t7                      }|                    ||           |r |d| d}|                    d||           w w 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) Optionally log timings to pt2_compile_events.
    2) Optionally log timings to CompilationMetrics (dynamo_compile).
    3) Optionally log chromium events.
    4) Optionally increment a WaitCounter.
    5) Store 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".
    - compile_id: In the typical case, this parameter should not be needed. Use to
      supply the compile_id for those cases where we want to log a compile_id where
      it's not naturally available, e.g., for runtime autotuning.
    - is_backward: Specify forward/backward directly when not available in a
      CompileContext, e.g., during runtime autotuning.
      that support it.
    - log_waitcounter: If set, we'll log a waitcounter of the form "pytorch.dynamo_timed.{key}"
    Nfn_namer   z (dynamo_timed)zpytorch.wait_counter._usdepthr   r1   z#pytorch.wait_counter.dynamo_compilez.pytorch.wait_counter.compile_runtime_overheadsr     duration_usT)r   
is_runtime
is_forward)rF   r   r   r   r   log_event_starttorchprofilerrecord_functionappendr,   guard_guardsCompileContextcurrent_compile_idendswithhasattr_dynamo_timed_tlsr  
contextlib	ExitStackenter_contextlog_event_endrH   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r  event_metadatar   start_nscx_mgrswc_nameis_compile_time
runtime_wcstackcxend_nstime_spent_nsis_outer_eventr  r   runtime_contextextras                              rT   dynamo_timedr&  h  s     l  


***(* %N (h''' 4y'2333}k:;;;(A(C(CL|~~H  Hn.CZ  
 	&&#'>'>'>??!G  P/HQ++c|$EG$E$EFFLLNNOOOm2EEGGtSO A'0077777 ('22 	(&'#1$ "a''NN<(MNNTTVVWWW" AM
|J77==??@@@(Q!## 	u ( (##B''''EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 ) %,,]S-@AAA""H.CZ	
 	
 	
 $ 	Q %Z000MA000 ##q(##.49N'4/K Q"5"7"7"..00 N#--.FTTT% N'11-MMMMMN NN N #>"?"?))*BKPPP! Q&0&**5o E
 $--m[%PPPPP9	Q 	Q,Q Q9 ) %,,]S-@AAA""H.CZ	
 	
 	
 $ 	Q %Z000MA000 ##q(##.49N'4/K Q"5"7"7"..00 N#--.FTTT% N'11-MMMMNN #>"?"?))*BKPPP! Q&0&**5o E
 $--m[%PPPP9	Q,Qs1   9M: H7+M: 7H;;M: >H;?M: :D3R-reprLiteral['str']	aggregatec                    d S rg   r>   r'  r)  s     rT   compile_timesr,    s    ILrS   Literal['csv']tuple[list[str], list[object]]c                    d S rg   r>   r+  s     rT   r,  r,    s	     &)SrS   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 rg   r>   xs    rT   <lambda>zcompile_times.<locals>.<lambda>  s     rS   c                ~    r |t          |                     S d                    t          ||                     S )Nr   )rm   r   r   )valuesitem_fnr)  s     rT   fmt_fnzcompile_times.<locals>.fmt_fn  s=     	(73v;;'''yyWf--...rS   r~   c                F    g | ]}| t           |         d            fS )c                
    | dS )Nz.4fr>   r2  s    rT   r4  z*compile_times.<locals>.<listcomp>.<lambda>  s
    jj rS   r7  )rF   )r   kr8  s     rT   
<listcomp>z!compile_times.<locals>.<listcomp>  sG     
 
 
 /2<P<PQQQR
 
 
rS   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                ,    g | ]} |d            S )c                
    | dS )Nz.6fr>   r2  s    rT   r4  z*compile_times.<locals>.<listcomp>.<lambda>!  s
    1

 rS   r;  r>   )r   vr8  s     rT   r=  z!compile_times.<locals>.<listcomp>   s<     
 
 
 F122333
 
 
rS   N)rF   r   r6  listkeys)r'  r)  ry   r   r6  r{   r8  s    `    @rT   r,  r,    s      +{ / / / / / /
 u}}
 
 
 
-
 
 
 3x&BCCCC
	
 
 
 
-4466
 
 
 /4466774rS   c                 X    t                               t          dd                     d S )Nr~   Tr+  )rt   infor,  r>   rS   rT   dump_compile_timesrG  )  s'    HH]66677777rS   c                  &    e Zd ZdddZd ZddZdS )DuplicateWarningChecker   maxsizerZ   r}   r   c                <    || _         |                                  d S rg   )rK  reset)selfrK  s     rT   __init__z DuplicateWarningChecker.__init__=  s    

rS   c                ,    t                      | _        d S rg   )r   setrN  s    rT   rM  zDuplicateWarningChecker.resetA  s    ==rS   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)rQ  move_to_endr<   verboselenrK  popitemrN  r   s     rT   addzDuplicateWarningChecker.addD  s    $(??H  4 000> u !DHSMdh--$,..  e ,,, dh--$,..trS   N)rJ  )rK  rZ   r}   r   )r   rS  r}   r   )rM   rN   rO   rO  rM  r[  r>   rS   rT   rI  rI  <  sP            ! ! !	 	 	 	 	 	rS   rI  c                     t           j                            dd          dk    } | rt                      S t	          j                    S )NTORCH_COMPILE_DEBUG01)osenvironrk   add_file_handlerr  r  )compile_debugs    rT   setup_compile_debugrd  S  s@    JNN#8#>>#EM "!!!!!!rS   c                 8    t                                            d S rg   )graph_break_dup_warning_checkerrM  r>   rS   rT   reset_graph_break_dup_checkerrg  \  s    #))+++++rS   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 rg   removeHandlerlog_file_handlerloggers   rT   r4  z"add_file_handler.<locals>.<lambda>i  s    v334DEE rS   )r`  pathr   get_debug_dirmakedirsloggingFileHandler	getLogger
addHandlerr  r  callback)log_path	exitstackrp  rq  s     @@rT   rb  rb  `  s    w||MOO];;HK4((((*27<<++N+NOO//F
&'''$&&IEEEEEFFFrS   c                 0   t          j                    } t          j        tt	          j        t          j                  t          j        j        	                                D ]0
                               |                     fd           1| S | S )Nc                 .                                    S rg   rm  ro  s   rT   r4  z setup_log_file.<locals>.<lambda>s  s    v';';<L'M'M rS   )r  r  r<   log_file_nameru  rv  r
  _logging	_internalget_loggersrx  ry  )r{  rp  rq  s    @@rT   setup_log_filer  m  s    $&&I'".v/CDDn.::<< 	O 	OF.///MMMMMNNNNrS   c                n    t                       d|j         dt          |           j         d|j         dS )Nz/error_recordings/_z.rec)rs  co_nametyperM   co_firstlineno)exccodes     rT   gen_record_file_namer  y  sR    oo ? ?? ?S		"? ?%)%8? ? ? ?rS   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.Trj  wbz#Unable to write execution record %s)r`  rr  existsrt   warningrt  dirnameopendump	Exception	exception)r  exec_recordfs      rT   write_record_to_filer  ~  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   callr1   )nodesop)r  cns      rT   count_callsr    s2    	AW  QT>>FAHrS   r3  c                    | S rg   r>   r2  s    rT   identityr    s    HrS   c                `    	 t          |            dS # t          $ r Y dS t          $ r Y dS w xY wNTF)hash	TypeError
ValueErrorr2  s    rT   hashabler    sR    Qt   uu   uus    
-	--c                     d S rg   r>   r   r   s     rT   nothingr    s    DrS   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 rg   r6  refsrR  s    rT   rO  zExactWeakKeyDictionary.__init__  s    			rS   c                6    | j         t          |                   S rg   )r6  idrZ  s     rT   __getitem__z"ExactWeakKeyDictionary.__getitem__  s    {2c77##rS   Nc                R    | j                             t          |          |          S rg   )r6  rk   r  )rN  r   defaults      rT   rk   zExactWeakKeyDictionary.get  s    {r#ww000rS   c                .    t          |          | j        v S rg   )r  r6  rZ  s     rT   __contains__z#ExactWeakKeyDictionary.__contains__  s    #ww$+%%rS   c                     t          |           j        vr!t          j        | fd           j        <   | j        <   d S )Nc                .                                   S rg   )
_remove_id)refidxrN  s    rT   r4  z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>  s    $//#:N:N rS   )r  r  weakrefr  r6  )rN  r   r   r  s   `  @rT   __setitem__z"ExactWeakKeyDictionary.__setitem__  sR    ggdi$[.N.N.N.N.NOODIcN CrS   c                N    || j         v r| j         |= || j        v r
| j        |= d S d S rg   r  )rN  r  s     rT   r  z!ExactWeakKeyDictionary._remove_id  s;    $+C $)	# rS   c                j    | j                                          | j                                         d S rg   )r  rh   r6  rR  s    rT   rh   zExactWeakKeyDictionary.clear  s.    	rS   rg   )rM   rN   rO   r   rO  r  rk   r  r  r  rh   r>   rS   rT   r  r    s        ff  $ $ $1 1 1 1& & &! ! !      rS   r  objr   allowed_typestype[T]	TypeIs[T]c                    d S rg   r>   r  r  s     rT   istyper    s    >AcrS   )tuple[type[list[T]], type[tuple[T, ...]]]c                    d S rg   r>   r  s     rT   r  r    s	     rS   Iterable[type]c                    d S rg   r>   r  s     rT   r  r    s    @CrS   c                    t          |t          t          t          f          rt	          |           |v S t	          |           |u S )zisinstance() without subclasses)
isinstancetuplerC  rQ  r  r  s     rT   r  r    s=    -%s!344 *CyyM))99%%rS   rL      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    rT   	is_typingr    sE     7""z%9V'W'W"teV]++Fu/FFrS   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)
npr  int8int16int32int64uint8uint16uint32uint64r  s    rT   is_numpy_int_typer    sQ     uGHHHHIII		
  rS   c                v    t           sdS t          | t           j        t           j        t           j        f          S r  )r  r  float16float32float64r  s    rT   is_numpy_float_typer    s8     uJJJ	
  rS   r   Callable[..., T]*TypeGuard[functools._lru_cache_wrapper[T]]c                    d S rg   r>   r  s    rT   is_lru_cache_wrapped_functionr    s	     25rS   r   ,TypeGuard[functools._lru_cache_wrapper[Any]]c                    d S rg   r>   r  s    rT   r  r    s	     473rS   c                z    t          | t          j                  o!t          t	          j        | d                    S )N__wrapped__)r  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr  s    rT   r  r  %  s<     eY9:: {um44@ @ rS   r   
_FuncTypesMTypeIs[Union[_FuncTypes, torch._ops.OpOverloadPacket, torch._ops.OpOverload]]c                    t          |           p/t          | t          j        j        t          j        j        f          S rg   )r  r  r
  _opsOpOverloadPacket
OpOverloadr  s    rT   is_function_or_wrapperr  5  s9     u 
+UZ-BC" " rS   TypeIs[_FuncTypes]c                z    t          | t          j        t          j        t          j        t          j        f          S rg   )r  typesFunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper  s    rT   r  r  =  s6     %&'		
  rS   )__eq____ne____lt____le____gt____ge__z==z!=<z<=>z>=TypeIs[Union[types.GetSetDescriptorType, types.MethodDescriptorType, types.WrapperDescriptorType, types.MemberDescriptorType, types.MethodWrapperType]]c                    t          | t          j        t          j        t          j        t          j        t          j        f          S rg   )r  r   GetSetDescriptorTyper  r  MemberDescriptorTyper   r  s    rT   is_wrapper_or_member_descriptorr  _  s>      &&'&#	
  rS   c                ,    t          |           d         S r   ) unwrap_with_attr_name_if_wrapper)fns    rT   unwrap_if_wrapperr  {  s    +B//22rS   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     rT   r  r    sT     2 71"6KUSS #B(=rBB)			y=rS   c                H    t           sdS t          | t           j                  S r  )r  r  ndarrayr  s    rT   is_numpy_ndarrayr    s!     u%$$$rS   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	Parameterr<   traceable_tensor_subclasses_subclassesr?   r  )r  tensor_lists     rT   istensorr#    sO     	% 
	+% %K
 !2!= ??K#{###rS   c                ,    t          | t                    S rg   )r  r-   )mods    rT   is_lazy_moduler&    s    c?+++rS   rJ  c                     t          |   d S rg   )r   r   s    rT   
print_oncer)    s    	4LLLLrS   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 rg   r>   r2  s   rT   r  zmake_cell.<locals>.f  s    rS   Nr1   r   )__closure__rX  )valr  r3  s     @rT   	make_cellr.    sR    A     =$Q]););q)@)@)@)@=rS   c                8   	 t          d | D                       }d |                                D             }||fS # t          $ rR}ddlm} ddlm}  |dd ||   d |t          |                                            d	g |
           Y d }~d S d }~ww xY w)Nc              3  >   K   | ]}|                                 V  d S rg   as_proxyr   args     rT   r   z$proxy_args_kwargs.<locals>.<genexpr>  s*      ::c3<<>>::::::rS   c                >    i | ]\  }}||                                 S r>   r1  )r   r   r4  s      rT   
<dictcomp>z%proxy_args_kwargs.<locals>.<dictcomp>  s&    KKKSS\\^^KKKrS   r1   unimplemented_v2)typestrz&Failed to convert args/kwargs to proxyzcall_function args: r   z7Missing `as_proxy()` implementation for some arg/kwarg.gb_typecontextexplanationhintsfrom_exc)	r  r   NotImplementedErrorr  r8  variables.baser9  rC  r6  )r   r   
proxy_argsproxy_kwargser8  r9  s          rT   proxy_args_kwargsrE    s    
::T:::::
KKFLLNNKKK<'' 

 

 

))))))++++++<]77D>]]GGT&--//EZEZ<[]]Q	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	

s   := 
BABBrB  Optional[float]r   c                .    | d nt          | dz            S Nr  rZ   rB  s    rT   	to_int_msrK    s    944#a$h--/rS   c                .    | d nt          | dz            S )Ni@B rI  rJ  s    rT   	to_int_usrM    s    944#a)m"4"44rS   rL   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=<   e;Z<d>ed?<   d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ZFdIedJ<   dZGdedK<   dLZHdedM<   dZIdedN<   dZJdedO<   dZKdedP<   dZLdedQ<   dZMdedR<   dZNdedS<   dZOdedT<   dZPdedU<   dZQdedV<   dZRdedW<   dZSdedX<   dZTdedY<   dZUdedZ<   dZVded[<   dZWded\<   dZXded]<   eYdad`            ZZdS )bCompilationMetricsNr   r   	frame_keyr  co_filenamer   r  
cache_sizeaccumulated_cache_sizeguard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_countrF  
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_sr   has_guarded_coderemote_cache_time_saved_sstructured_logging_overhead_sconfig_suppress_errors config_inline_inbuilt_nn_modulesspecialize_floatdynamo_configr  num_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usr  !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 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_usrZ   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compile_time_autotune_time_usFr  
gc_time_ustensorify_float_attempttensorify_float_successtensorify_float_failureguard_latency_usrecompile_reasonnum_graph_breakstriton_kernel_compile_times_usir_countcudagraph_skip_reasonpython_version!pgo_put_remote_code_state_time_us!pgo_get_remote_code_state_time_usparam_numelparam_bytesparam_countmetricsrD   c                j   dd}dd}dd	}dd
} ||                     d                     ||                     d                     ||                     d                     ||                     d                     ||                     d                     ||                     d                     ||                     d                     ||                     d                     ||                     d                    d	}i ||} ||                     d                    |d<    ||                     d                    |d<    ||                     d                    |d<   |                     d          }|rt          |          nd|d<    | di |S )z
        Factory method to create a CompilationMetrics from a dict of fields.
        Includes the logic to add legacy fields and any pre-processing, e.g.,
        we transform some fields to comma-separated strings for scuba logging.
        metricr   r}   rF  c                    | | dz  nd S )Ng    .Ar>   r  s    rT   us_to_sz*CompilationMetrics.create.<locals>.us_to_s9  s    #)#56C<<4?rS   c                    | | dz  nd S rH  r>   r  s    rT   us_to_msz+CompilationMetrics.create.<locals>.us_to_ms<  s    %+%76T>>TArS   Optional[Any]r   c                    d	d| d S t          | t          t          f          sdS d                    fdt	          |           D                       S )
Nitemr   r}   r~   c                D    	 t          |           S # t          $ r Y dS w xY wN	<unknown>)r~   r  )r  s    rT   safe_strzFCompilationMetrics.create.<locals>.collection_to_str.<locals>.safe_str@  s5    't99$  ' ' '&;;'s    
r  ,c              3  .   K   | ]} |          V  d S rg   r>   )r   r  r  s     rT   r   zGCompilationMetrics.create.<locals>.collection_to_str.<locals>.<genexpr>L  s+      FFtHHTNNFFFFFFrS   )r  r   r}   r~   )r  rQ  rC  r   sorted)r  r  s    @rT   collection_to_strz4CompilationMetrics.create.<locals>.collection_to_str?  sk    ' ' ' ' ~tfsDk22 #"{88FFFFvf~~FFFFFFrS   c                p    | d S 	 t          j        t          |                     S # t          $ r Y dS w xY wr  )jsondumpsrC  r  r  s    rT   collection_to_json_strz9CompilationMetrics.create.<locals>.collection_to_json_strN  sI    ~t#z$v,,/// # # #"{{#s    ' 
55rp  rq  rr  rs  rt  ry  r{  r|  rz  )	rY  rZ  r[  r\  r]  rg  rn  ro  rh  r  r  r  r   N)r  r   r}   rF  )r  r   r}   r   )r  r  r}   r   r>   )rk   r~   )	r_   r  r  r  r  r  legacy_metricsall_metricsr   s	            rT   createzCompilationMetrics.create1  s_   	@ 	@ 	@ 	@	B 	B 	B 	B	G 	G 	G 	G	# 	# 	# 	# "''++o">">??+27?@@, , '.gEFF' ' (/wABB( (  'wJKK    *1>??* * 2:?@@2 2 2:?@@2 2 .5W<==. ./
 
8 4373 <M;LOO?@@<
 <
78 =N<MOO@AA=
 =
89 9O8NOO<==9
 9
45 !__\22
7A$KC
OOOtL!s!![!!!rS   )r  rD   )[rM   rN   rO   r   rw   rP  r  rQ  r  rR  rS  rT  rU  rV  rW  rX  rY  rZ  r[  r\  r]  r^  r_  r`  ra  rb  rc  rd  re  rf  rg  rh  ri  rj  rk  rl  r  rm  rn  ro  rp  r  rq  rr  rs  rt  ru  rv  rw  rx  ry  rz  r{  r|  r}  r~  r  r  r  LOG_FORMAT_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  rx   r  r>   rS   rT   rO  rO    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;?)????6:$::::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337!7777!&J&&&& $J$$$$.22222.22222266666(,,,,,&*****&*****48"8888"H""""+/////$(N((((7;%;;;;7;%;;;; "&K%%%% "&K%%%% "&K%%%%Q" Q" Q" [Q" Q" Q"rS   rO  @   maxlenz%collections.deque[CompilationMetrics]_compilation_metricsr  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 )a  
    These are the common fields in CompilationMetrics that existed before
    metrics_context, and aren't set by MetricsContext.set(). We add the subset
    of them that make sense in `dynamo`/toplevel events in PT2 Compile Events
    directly.

    If you're tempted to add to this list, consider using CompileEventLogger.compilation_metric()
    instead, which will automatically also add it to tlparse and PT2 Compile Events.
    TODO: Get rid of this function and replace it with CompileEventLogger directly instead.
    N)r   rP  r  rQ  r  rR  rS  rT  rU  rV  rW  rX  r^  r_  r`  ra  rb  rc  rd  re  rf  rl  )r   r   r   rP  r  rQ  r  rR  rS  rT  rU  rV  rW  rX  r^  r_  r`  ra  rb  rC  rc  rd  re  rf  rl  )r  event_loggerr   s      rT   #add_compilation_metrics_to_chromiumr    s    -..L1133J +	M'< 7M5'+-+M!"!; 7 * q2333!- "!"8999( Q.///%&%C+o;       rS   c                 n    dd}  | t          j                              }t          j        |d          S )NdrD   r}   c                L    h dfd|                                  D             S )N>   rW  base_dirrepro_afterrepro_levelTYPE_CHECKINGr~  debug_dir_rootrepro_tolerancerepro_forward_only_custom_ops_profile_save_config_ignorerepro_ignore_non_fpignore_logger_methodslog_compilation_metricssame_two_models_use_fp64r   reorderable_logging_functionsnontraceable_tensor_subclasses)_autograd_backward_strict_mode_banned_ops+inject_BUILD_SET_unimplemented_TESTING_ONLYc                l    i | ]0\  }}|v	|t          |t                    rt          |          n|1S r>   )r  rQ  r  )r   r   r   	blocklists      rT   r6  zJ_get_dynamo_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>  sM     
 
 
U)## *UC"8"8Ce###rS   )r   )r  r  s    @rT   clean_for_jsonz6_get_dynamo_config_for_logging.<locals>.clean_for_json  sG    
 
 
	.
 
 
 
ggii
 
 
 	
rS   T)	sort_keys)r  rD   r}   rD   )r<   get_config_copyr  r  )r  config_dicts     rT   _get_dynamo_config_for_loggingr    sE    
 
 
 
< !.!7!9!9::K:kT2222rS   c                     G d dt           j                  } t                      }d}t          j        j        r#t          j        j                                        nd}|	 |                                D ]V\  }}t          |t                    s|
                    |           t          |t                    rt          |          ||<   W|D ]}||= t          j        || d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)superr  r  )rN  o	__class__s     rT   r  zI_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializer.default  sB    8wwq))) 8 8 87778s    $ 
22)rM   rN   rO   r  __classcell__r  s   @rT   TypeSafeSerializerr    s8        	8 	8 	8 	8 	8 	8 	8 	8 	8rS   r  NT)r_   skipkeysr  z(Inductor Config is not JSON serializable)r  JSONEncoderrQ  r
  	_inductorr<   r  r   r  r~   r[  rC  r  r  )r  keys_to_scrubinductor_conf_strinductor_config_copyr   r-  s         rT   %_scrubbed_inductor_config_for_loggingr    sU   8 8 8 8 8T- 8 8 8 "eeM49O4JT..000PT  '	K06688 : :S!#s++ +!%%c***c3'' :04S		(-$ . .(-- $
$&	! ! !  	K 	K 	K J	K s   "BC. .C=<C=start_time_nsend_time_nsr  exc_typeOptional[type[BaseException]]	exc_valueOptional[BaseException]c                   t           j        j                                        r 	 ddlm} |}d}n# t          $ r d }d }Y nw xY wd }d }|                    d          }|s#t           j        j	        
                                }|| dz  |dz  |r|j        nd |rt          |          nd t          t           j                                                  t!                      t#                      t           j        j        t)                      rt*          j        nd||t.          j        d}	t0                              i |	|          t4                                         d}
j        d	u rd
|
z   }
j        du r|
dz   }
t           j                            |
fdd	|           t?                     t@          j!        rtE                     d S d S )Nr   )REMOTE_CACHE_VERSION_ManifoldCacher   r   )r   rp  r~  r^  r_  rz  rl  r  r  r  r  r  r  compilation_metricsFbwd_T_runtimec                 d    d t          j                                                   D             S )Nc                b    i | ],\  }}|t          |t                    rt          |          n|-S r>   )r  rQ  rC  )r   r<  rB  s      rT   r6  z@record_compilation_metrics.<locals>.<lambda>.<locals>.<dictcomp>C  sG     
 
 
1 *Q,,3tAwww!
 
 
rS   )dataclassesasdictr   )r  s   rT   r4  z,record_compilation_metrics.<locals>.<lambda>C  s9     
 
#*+>??EEGG
 
 
 rS   )record_logging_overheadr   )#r
  r  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cacher  ModuleNotFoundErrorrk   r  r  r  rO   r~   rM  r  get_structured_logging_overheadr  r  versioncudar.   triton__version__r  rO  r  r  r  r  r  trace_structuredr  r<   r  r'   )r  r  r  r  r  r  r  r  r   common_metricsr^   r  s              @rT   record_compilation_metricsr	    s4    ==?? $	9LLLLLL#7 4D11" 	9 	9 	9#' 48111	9 15-# \**J G]1DDFF
 !&$."d*.6@X**D)2<s9~~~*3N::<<+
 +
 899@BB*0:D&,," 41V+ N$ -334Q~4Q4QRR 3444 D%..}%--j 	N##	
 	
 	
 	
 !&  $   $ ((;<<< % 31222223 3s   
1 AA)on_exitnew_sizec                    t          t                    | k    r1t                                           t          t                    | k    1t          j        t          |           }|ad S )Nr  )rX  r  popleftru   deque)r  	new_deques     rT   set_compilation_metrics_limitr  _  s`    
"
#
#h
.
.$$&&& "
#
#h
.
.!"6xHHHI$rS   c                 8    t                                            d S rg   )r  rh   r>   rS   rT   clear_compilation_metricsr  g  s         rS   list[CompilationMetrics]c                 *    t          t                    S rg   )rC  r  r>   rS   rT   get_compilation_metricsr  l  s    $%%%rS   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Zd+dZ	 	 d,d-dZd.dZ	 d/d0d!Z	 d/d1d$Z	 	 d2d3d%ZdS )4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.
    r}   	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.
        r  )r  tlsr  rR  s    rT   	get_stackzChromiumEventLogger.get_stackw  s5    
 48W%% 	"8>!DHN8>!rS   r   c                B    |                                  }|r|d         ndS )zv
        Get the outermost event name (i.e. the longest running event)
        or None if the stack is empty.
        r   N)r  )rN  r  s     rT   r   z'ChromiumEventLogger.get_outermost_event  s&    
    *uQxxd*rS   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.
        r   )r  r  r   rR  s    rT   r   z,ChromiumEventLogger.get_pt2_compile_substack  s9    
 48344 	1800,.DH)800rS   rD   c                \    t          | j        d          si | j        _        | j        j        S )N
event_data)r  r  r  rR  s    rT   get_event_dataz"ChromiumEventLogger.get_event_data  s+    tx.. 	%"$DHx""rS   c                    t          j                    | _        t          t	          j                              | _        t                              d| j                   d S )Nz*ChromiumEventLogger initialized with id %s)		threadinglocalr  r~   uuiduuid4id_rt   rF  rR  s    rT   rO  zChromiumEventLogger.__init__  sG    ?$$ tz||$$ 	=txHHHHHrS   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  r   )rN  r   r   s      rT   r   z&ChromiumEventLogger.try_add_event_data  s?     T^^----FJ11&11111rS   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
        Event  not in q. Cannot add metadata to events that aren't in progress. Please make sure the event has started and hasn't ended.N)r  r   r'  r   r   )rN  r   r   r  s       rT   r   z"ChromiumEventLogger.add_event_data  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz":%%f-----rS   r   r   rZ   c                ,   ||                                  vr5t          dt          |           d|                                   d          |                                 }||vri ||<   |||         vrd||         |<   ||         |xx         |z  cc<   dS )zK
        Increment an integer event data field by the given amount
        r)  r*  r+  r   N)r  r   r'  r   rN  r   r   r   r  s        rT   r   zChromiumEventLogger.increment  s     T^^----Kj)) K K4>>3C3C K K K   ((**
Z''%'Jz"j,,,*+Jz"3':s###u,#####rS   r   c                Z   ||                                  vr5t          dt          |           d|                                   d          |                                 }||vri ||<   |||         vrt	                      ||         |<   ||         |                             |           dS )zR
        Add a value to a set within a event_name's metadata if it exists
        r)  r*  r+  N)r  r   r'  r   rQ  r[  r-  s        rT   r   zChromiumEventLogger.add_to_set  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz"j,,,*-%%Jz"3':s#''.....rS   FNr   r   r   r   r   r   c                d   |p"t           j        j                                        }t	          |          |d<   |                     ||d|           |                                                     |            | j        |fi | |r)| 	                                                    |           dS dS )a  
        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
        :param log_pt2_compile_event: If True, log to pt2_compile_events
        :param compile_id: Explicit compile_id (rather than using the current context)
        r   BN)
r
  r  r  r  r~   _log_timed_eventr  r  r   r   )rN  r   r   r   r   r   s         rT   r	  z#ChromiumEventLogger.log_event_start  s       T5=#?#R#R#T#T
!$Z		
 	
 	
 	
+++J33(333  	?))++22:>>>>>	? 	?rS   c                    |                                  }|                                 }|                                 |                                 |                                 }|                                 d S rg   )r  r   rh   r   )rN  r  substackr  s       rT   rM  zChromiumEventLogger.reset  sj       0022((**
rS   r  c                d   |p"t           j        j                                        }t	          |          |d<   |                                 }|v r|         }|= ni }|                    |           |                     |d|          }	fd}
|                                 }|vrt          
                    d           dS  |
|           |rJ|                                 } |
|           t          |	|| j        |           |                                 |                                 dS )a&  
        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
        :param start_time_ns: The start time timestamp in nanoseconds
        :param log_pt_compile_event: If True, log to pt2_compile_events
        :param compile_id: Explicit compile_id (rather than using the current context)
        r   Ec                    | d         k    r<t                               d           |                                  | d         k    :d S d S )Nz>ChromiumEventLogger: Detected overlapping events, fixing stack)rt   r  pop)r  r   s    rT   	pop_stackz4ChromiumEventLogger.log_event_end.<locals>.pop_stack6  sY    b	)) T   		 b	))))))rS   z7ChromiumEventLogger: Start event not in stack, ignoringN)r
  r  r  r  r~   r   r   r1  r  rt   r  r   r&   r&  r8  )rN  r   r   r   r  r   r   all_event_datar  eventr9  event_stackr   s    `           rT   r  z!ChromiumEventLogger.log_event_end  ss   &  T5=#?#R#R#T#T
!$Z ,,..''+J7Nz**Nh'''%%	
 
	 	 	 	 	 nn&& [(( KKQRRRF	+  	'#'#@#@#B#B I*+++'+TX}   !$$&&& 	rS   r   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^   tsr   phcattidpidchromium_eventc                      S rg   r>   r;  s   rT   r4  z6ChromiumEventLogger._log_timed_event.<locals>.<lambda>n      u rS   F
payload_fnsuppress_contextexpect_trace_id)r
  r  r  r(   )rN  r   r   r   r   r;  s        @rT   r1  z$ChromiumEventLogger._log_timed_eventX  sr     D.!	
 	
 	''$}}}"!	 	( 	
 	
 	
 	'u---rS   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?  r   r@  rA  rB  rC  srD  c                      S rg   r>   rF  s   rT   r4  z7ChromiumEventLogger.log_instant_event.<locals>.<lambda>  rG  rS   FTrH  )
r~   r
  r  r  r  r  r  r&   r   r&  )rN  r   r   r   r   r   r;  s         @rT   r   z%ChromiumEventLogger.log_instant_eventu  s     H5HHJJKK
!+D.!

 

 	''$}}}" 	 	( 	
 	
 	
 ! 	't4466'    	 	rS   )r}   r  r}   r   )r}   rD   )r   r~   r}   r   )r   r~   r   r~   r   rZ   )r   r~   r   r~   r   r   FN)r   r~   r   rZ   r   rD   r   r   r   r   r}   r   r}   r   rg   )r   r~   r   rZ   r   rD   r  rZ   r   r   r   r   r}   r   )
r   r~   r   rZ   r   r~   r   r=  r}   rD   r  )
r   r~   r   rZ   r   r=  r   r   r}   r   )rM   rN   rO   r   r  r   r   r   rO  r   r   r   r   r	  rM  r  r1  r   r>   rS   rT   r  r  p  sd        	" 	" 	" 	"+ + + +	1 	1 	1# # # #
I I I2 2 2 2. . . .(- - - -$/ / / /6 ',*.? ? ? ? ?<   " +/G G G G G\ .2    B .2&+( ( ( ( ( ( (rS   r  zOptional[ChromiumEventLogger]CHROMIUM_EVENT_LOGc                 :    t           t                      a t           S rg   )rT  r  r>   rS   rT   r   r     s    !022rS   c                     t           d uS rg   )rT  r>   rS   rT   r   r     s    T))rS   r   reset_event_log_on_exitc           	   #    K   t                      }t          j                    }|                    | |i |           	 dV  |                    | t          j                    i ||           |r|                                 dS dS # |                    | t          j                    i ||           |r|                                 w 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  rM  )r   rW  r   chromium_event_logchromium_start_times        rT   chromium_event_timedr[    s      344,..&&
	  '((LNN!	
 	
 	
 # 	'$$&&&&&	' 	' 	((LNN!	
 	
 	
 # 	'$$&&&&	's   B AC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 calledrD   scoper~   r^   c                X    t           t           xj        dz  c_        | j        | j        = d S r   )CleanupManagerra   r^  r^   rN  r   s     rT   __call__zCleanupHook.__call__  s.    %  A%  Jty!!!rS   c                b    || vsJ t           xj        dz  c_        || |<   t          | |          S r   )r`  ra   r]  )r^  r^   r-  s      rT   r  zCleanupHook.create  s@    5    !d5$'''rS   N)rM   rN   rO   r   rw   rb  r   r  r>   rS   rT   r]  r]    s[         66III" " " ( ( \( ( (rS   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 rg   )r6  r  r  )rN  r  hookr  s      rT   r  zCleanupManager._remove_id  sD    K$ 	 	DDFFFF3rS   )rM   rN   rO   ra   rw   r  r  r  s   @rT   r`  r`    sK         E&&&&                 rS   r`  c                    |                                                      | j                  }| j        r%| j        | j                                         |_        |S )z!Clone the tensor and its gradient)clonerequires_grad_requires_gradis_leafgrad)r3  ys     rT   clone_tensorro    sF    			  11Ay  QV'HrS   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 )Nrp  _dynamo_dynamic_indices)
r
  ri  rl  rj  rk  rm  clone_inputr  rt  copy)r3  rn  rq  s     rT   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%rS   xlaN)is_coalesced)layoutc              3  ,   K   | ]\  }}|d z
  |z  V  dS )r1   Nr>   )r   shapestrides      rT   r   zclone_input.<locals>.<genexpr>'  s@       
 
%2UFUQY& 
 
 
 
 
 
rS       )rq  devicerp  rt  )+r@   r
  no_gradr  r  rz  
sparse_coosparse_coo_tensor_indicesrW   r|  ry  r$   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorr6  rm   zipsizer}  is_quantizedempty_quantizedemptyrq  data_ptrelement_sizeas_strided_copy_ri  rl  rj  rk  rm  ru  r   r  rt  rv  )r3  rq  rw  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      rT   ru  ru    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 rg   )r  dictr   r  r  clone_inputsr
  r  ru  rC  rangerX  )example_inputsresr   r   rM  s        rT   r  r  D  s    Nt##>""))++ 	. 	.JC%'' .'..C!%66CCUCCC&u--C

~

C3s88__ ) )c!fel++ 	) Q((CFJrS   r-  torch.Tensorc                    	 |                                   d S # t          $ r=}ddlm} t	          j        ddt          |                     } |d|           |d }~ww xY w)Nr1   )	SkipFramez\(.*r  z(torch.compile cannot be run in context: )r  r   r  r  resubr'  )r-  rD  r  functorch_subclass_names       rT   skip_frame_if_in_functorch_moder  W  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rg   )r
  _C_DisableFuncTorchr  _python_dispatch_disable_current_modesri  randomget_rng_stater  r  is_availableset_rng_state)disable_functorchdisable_current_modes	rng_statecuda_rng_states       rT   preserve_rng_stater  d  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 rg   )	r  r
  jit_traceTopLevelTracedModule_scriptRecursiveScriptModuleScriptFunctionScriptModule)model0s    rT   is_jit_modelr  v  sD     I1I3I$I"		
  rS   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)	r  r
  r  tracer  scriptrt   r  error)modelr  rW  s      rT   torchscriptr    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rg   )r  getfiler  OSErrorr  s    rT   r  r    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_clsr  r  s    rT   is_namedtupler    s    T#YY'''rS   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-namedtuplerN   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  ,   K   | ]}|t           u|V  d S rg   )r   )r   ts     rT   r   z$is_namedtuple_cls.<locals>.<genexpr>  s,      KKA!7:J:Ja:J:J:J:JKKrS   c              3  R   K   | ]!}t          |          oj        |j        u V  "d S rg   )r  __new__)r   r  r_   s     rT   r   z$is_namedtuple_cls.<locals>.<genexpr>  sR       ! ! 	 *!,, 5K194! ! ! ! ! !rS   F)
issubclassr  getattrr  callable	__bases__anyr  )r_   modulebasess   `  rT   r  r    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stepr  c                      e Zd ZU ded<   dS )!namedtuple_fields.<locals>.MarkerrZ   indexNrM   rN   rO   rw   r>   rS   rT   Markerr    s         




rS   r  r  r   r  )r   )slicer  r  r  r  r  	dataclassrN   r   r  n_fieldsdirr  r  r  rX  r  rk   )r_   r  r  fieldsr^   s        rT   namedtuple_fieldsr    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0rS   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
  ri  )r   params     rT   r=  z%checkpoint_params.<locals>.<listcomp>  s;     
 
 
 ENEK$6$67
 
 
rS   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 rg   )r
  r  r  r  r  r  r  r  )r  r  original_valuer  r  saved_states      rT   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  ri  r  r  r  r  r   r   
parametersbuffers)gmr  r  r  r  s     @@@rT   checkpoint_paramsr    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
  r  r  synchronizer  gccollectmanual_seedr   perf_counterr  )r  r  timesr  t0r  r  t1s           rT   timedr    s    z   j,KMMMJLLL	d				B5\\  '				B27?rS   c           	         t          d t          j        ||                     d                    D                       S )Nc              3  $   K   | ]}|j         V  d S rg   )is_cudar   r3  s     rT   r   z check_is_cuda.<locals>.<genexpr>	  s$      WWQqyWWWWWWrS   T)allr   r   r  )r  r  s     rT   check_is_cudar  	  s8    WW)/."--PTBUBU"V"VWWWWWWrS   r~  c                   | dk    sJ d t          |           D             }t          |dd          |d d         z             }t          dd                    |           dd                    |           d          }d|  d	|_        |S )
Nr1   c                    g | ]}d | S rJ  r>   r   rM  s     rT   r=  z rot_n_helper.<locals>.<listcomp>	  s    &&&GGG&&&rS   r7  zlambda r  z: ()rot__helper)r  reversedevalr   rM   )r  varsrotatedr  s       rT   rot_n_helperr  	  s    q5555&&U1XX&&&DtBCCy49,--G	???388G+<+<???	@	@B####BKIrS   z	set[type]common_constant_typesc                >   t          | t          t          f          r"t          t	          t
          |                     S t          | t          j        t          t          j        t          j        t          j        f          pt          | t           t"          hz            S rg   )r  r  	frozensetr  r   is_safe_constantr  enumEnumr  r
  Sizer  _GenericAliasr   GenericAliasr  r  rJ  s    rT   r  r  2	  s    a%#$$ -3'++,,,	IJ 	
	 	  
	'
 
rS   c                 
    ddhS Nr   r1   r>   r>   rS   rT   common_constantsr  D	  s    
 	
		 rS   -TypeGuard[Union[torch.SymBool, torch.SymInt]]c                    t          | t          j        t          j        f          o.t          | j        t          j        j        j        j                   S rg   )	r  r
  SymBoolSymIntnodenestedr  
nested_intNestedIntNoder  s    rT   is_torch_symr%  N	  sH    eemU\:;; J
EL*5CE E A rS   c                   ddl m} t          j                    j         o|                                                                 p|                                                                ot          j	         p|                                
                                ot          j	         p]|                                                                ot          j	         p* ||          pt          d           o| t                      v S )Nr1   )is_from_defaultsz5pytorch/dynamo:enable_unspecialize_zero_one_plain_int)sourcer'  r#   rk   #force_unspec_int_unbacked_size_likeguard_sourceis_localis_specialized_nn_moduler<   allow_unspec_int_on_nn_module"is_unspecialized_builtin_nn_moduleis_unspecialized_nn_moduler%   r  )r   r(  r'  s      rT   is_int_specialization_caser0  T	  s   ((((((!##GG !!**,,, 	

 !!::<< 988	
 !!DDFF 988	
 !!<<>> 988	
  F##!	
(   WXXX ,)+++/rS   c                   ddl m}m}m} t	          | |          r|                                 so|                                 }|                                 }t          |          p3t          j
         o&t          |          t          u ot          ||           }|s| S t	          | |          r'|                    |                                           S | S )Nr1   )ConstantVariableLazyVariableTrackerSymNodeVariable)	variablesr2  r3  r4  r  is_realizedoriginal_sourcer  r%  r<   specialize_intr  rZ   r0  r  evaluate_expr)r4  r2  r3  r4  r(  r   is_symnode_vts          rT   specialize_symnoder;  s	  s    QQQQQQQQQQ #*++ COO4E4E  $$&&""$$$U++ 
%% >Us">.uf=== 	  	J#'' <&&s'8'8':':;;;JrS   c                x    ddl m} t          |           } t          | |          r|                                 S | S )Nr1   r2  )r5  r2  r;  r  as_python_constant)r4  r2  s     rT   guard_if_dynr?  	  sK    ++++++
S
!
!C#'(( (%%'''JrS   c                ~    t          d t          j        | |                                          D                       S )Nc              3  >   K   | ]}|                                 V  d S rg   )is_python_constantr  s     rT   r   z&check_constant_args.<locals>.<genexpr>	  s.      VV!q##%%VVVVVVrS   )r  r   r   r6  r  s     rT   check_constant_argsrC  	  s3    VVytV]]__/U/UVVVVVVrS   c                    ddl m} ddlm} d}t	          j        | |                                          D ]+}t          ||          r|dz  }t          ||          s dS ,|dk    S )Nr1   r=  UnspecializedPythonVariabler   F)variables.constantr2  variables.tensorrF  r   r   r6  r  )r   r   r2  rF  unspec_countr3  s         rT   check_unspec_python_argsrJ  	  s    444444======L_T6==??33  a455 	ALLA/00 	55	!rS   c                    ddl m} t          j        | |                                          D ])}|                                st          ||          s dS *dS )Nr1   rE  FT)rH  rF  r   r   r6  rB  r  )r   r   rF  r3  s       rT   check_unspec_or_constant_argsrL  	  sl     >=====_T6==??33  $$&& 	*Q8S*T*T 	554rS   c                    ddl m t          fdt          j        | |                                          D                       S )Nr1   )NumpyNdarrayVariablec              3  8   K   | ]}t          |          V  d S rg   )r  )r   r3  rN  s     rT   r   z+check_numpy_ndarray_args.<locals>.<genexpr>	  sB         	1*++     rS   )rH  rN  r  r   r   r6  )r   r   rN  s     @rT   check_numpy_ndarray_argsrP  	  sa    666666    v}}77     rS   ztype[KeysView[Any]]	dict_keysztype[ValuesView[Any]]dict_valuesztype[ItemsView[Any, Any]]
dict_itemsodict_valuesztype[Iterator[Any]]tuple_iteratorrange_iteratorc                0    h | ]}t          |          |S r>   r  r   methods     rT   	<setcomp>r[  	  s6       
  rS   c                0    h | ]}t          |          |S r>   rX  rY  s     rT   r[  r[  	  s%    RRRF&AQAQRRRRrS   c                0    h | ]}t          |          |S r>   rX  rY  s     rT   r[  r[  	  s%    PPP6x?O?OPPPPrS   c                0    h | ]}t          |          |S r>   rX  rY  s     rT   r[  r[  	  s%    NNN&Xf=M=MNvNNNrS   c                d    t          | t                    sJ t                              |           S rg   )r  r  rD  )r  s    rT   builtin_dict_keysr`  	  s(    a99Q<<rS   c                H    t           t                    sJ t           t          t          f          r                                 S t           t                    r  fdt          j                   D             S  fdt                                         D             S )Nc                >    g | ]}|t          j        |          fS r>   )r   r  r   r<  r  s     rT   r=  z'get_items_from_dict.<locals>.<listcomp>	  s+    TTTK+C334TTTrS   c                J    g | ]}|t                               |          f S r>   )r  r  rc  s     rT   r=  z'get_items_from_dict.<locals>.<listcomp>	  s.    FFF!D$$S!,,-FFFrS   )r  r  r  r   r   rD  r  s   `rT   get_items_from_dictre  	  s    c4     cD+&'' Gyy{{	C	%	% GTTTTk>Ns>S>STTTTFFFFtyy~~FFFFrS   c                l    t          |           }t          j        j                            |           |S rg   )
object_newr
  r  ModulerO  )r_   r  s     rT   nn_module_newri  	  s*    
S//C	HOS!!!JrS   c                B    t          j        t          j        | d          S r   )r  reduceoperatormul)its    rT   productro  	  s    HL"a000rS   c                L    |                                  \  }\  }}|||z            S rg   )
__reduce__)rn  r  r  r  r  s        rT   tuple_iterator_getitemrr  	  s'    }}Avuuu}rS   c                d    t          j                            t          j                  |           S rg   )r
  _dynamodisabler  r  ri   s    rT   dataclass_fieldsrv  	  s#    =  !344S999rS   tuple[int, int, int]c                t    |                                  \  }\  }}|j        |pdz   }|j        }|j        }|||fS r   )rq  r  r  r  )
range_iterr  	range_obj	maybe_idxr  r  r  s          rT   normalize_range_iterr|  	  sI    !+!6!6!8!8A|	Y Oy~A.E>D>D4rS   c                ,    |                      |          S rg   )as_subclass)r  r_   s     rT   to_subclassr  
  s    ==rS   c                    t           }t          | t                    rt          }t          t	          j        |                    |           ||dz                       S r   )r  r  r   nextr   islicerD  )r  r  
dict_classs      rT   dict_keys_getitemr  
  sK    J![!! ! 
	 !3!3QA>>???rS   c                L    | j         j        }| j        }|rdnd}| d| d| }|S )NLGz["z"].)r  rM   r^   )r   r#  r^   r-  r^  
local_names         rT   	enum_reprr  
  sD     ?#D
*C!CCcE++T++c++JrS   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#   rk   	fake_mode	shape_envr
  r   experimentalsymbolic_shapescompute_unbacked_bindings)r!  r  r  symbol_to_paths       rT   set_example_valuer  
  sq     "/DIo"$$.8I 8 0JJ}
 
	
8 *8	%&&&8 8rS   c                    |                                  j        j                            d          }t	          |          s/ddlm} ddlm}  |dt          |          dg |j
                   |S )Nr  r1   graph_break_hintsr7  z:Cannot check Tensor object identity without its fake valuez/TensorVariable is missing a fake example_value.r;  r<  r=  r>  )r2  r!  r  rk   r@   r  r  r  r8  r~   
DYNAMO_BUG)vtfake_tensorr  r8  s       rT   _get_fake_tensorr  /
  s    ++--$)--o>>K; 	
''''''))))))P$$I1%01		
 	
 	
 	
 rS   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 )Nr1   )BuiltinVariabler2  TensorVariableVariableTrackerc              3     K   | ]B}|                                 o)|                                                                k    V  Cd S rg   )rB  r>  )r   r3  searchs     rT   r   z iter_contains.<locals>.<genexpr>G
  sk       
 
    "" F$$&&&*C*C*E*EE
 
 
 
 
 
rS   FT)r5  r  r2  r  r  rB  r  r  r  r  rl  eqcall_functionor_)r   r  txcheck_tensor_identityr  r2  r  r  found_constmust_check_tensor_idfoundr3  checks    `           rT   iter_containsr  >
  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  } ''..LrS   r<  ?TypeIs[Union[torch.Tensor, torch.nn.Module, MethodWrapperType]]c                d    t          | t          j        t          j        j        t
          f          S )z4Returns whether it indexes dictionaries using its id)r  r
  r  r  rh  r   )r<  s    rT   	key_is_idr  g
  s"     a%,9JKLLLrS   c                >    d |                                  D             S )Nc                N    g | ]"}t          |          rt          |          n|#S r>   )r  r  )r   r<  s     rT   r=  zkey_to_id.<locals>.<listcomp>o
  s-    ???QYq\\(BqEEEq???rS   )rD  r  s    rT   	key_to_idr  n
  s    ??%**,,????rS   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 )Nr1   )is_builtin_callabler  c              3  :   K   | ]}t          |           V  dS r#  N
const_reprr   rO  r#  s     rT   r   zconst_repr.<locals>.<genexpr>v
  s0      DDQj%888DDDDDDrS   [](z,)r	  r  'r  c                R    | j         }|j        }|dk    r|j        S |dz   |j        z   S )Nbuiltins.)r  rN   rO   )r  klassr  s      rT   fullnamezconst_repr.<locals>.fullname
  s7    KE%F##))C<%"444rS   )trace_rulesr  r  rC  r  r   rX  r  r  r  replacerM   r  )r3  r#  r  
elems_reprr  s    `   rT   r  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rS   c               V    d                     fd| D                       }d|z   dz   S )Nr  c              3  :   K   | ]}t          |           V  dS r  r  r  s     rT   r   z!dict_keys_repr.<locals>.<genexpr>
  s0      GGq
1E222GGGGGGrS   r  r  r   )
const_keysr#  keys_strs    ` rT   dict_keys_reprr  
  s8    xxGGGGJGGGGGH>CrS   
__dict_key)UnsupportedFakeTensorExceptionc                D    | dt          |           d| j        j         S )Nr  _c)r  outputr   )r  rootr  s      rT   get_safe_global_namer  
  s+     66RWW66	 4666rS   r  c                    |D ]	}| |v r dS 
dS r  r>   )r  
containers	containers      rT   is_inr  
  s-      	944 5rS   )requires_suffixprefixc                   |st          | g|R  s| S t          j                    D ]}|  d| }t          |g|R  s|c S t          d          )zm
    Return a name that starts with `prefix` and is not in any of the
    `containers` (e.g., map, set).
    r  unreachable)r  r   ra   AssertionError)r  r  r  rM  	candidates        rT   get_unique_name_wrtr  
  s    
  5#=*#=#=#= _  OOOO	Y,,,, 		 
'
''rS   c                    	  |             S # t           $ rX}ddlm} d|j         d}t                              |            |dt          |j                  |g |           Y d }~d S d }~ww xY w)Nr1   r7  zEncountered exception (z!) during fake tensor propagation.z!Fake tensor propagation exceptionr:  )r  r  r8  reasonrt   r  r~   )r  rD  r8  msgs       rT   wrap_fake_exceptionr  
  s    
rtt) 
 
 
))))))SSSSC7MM	
 	
 	
 	
 	
 	
 	
 	
 	
 	

s   	 
A.AA))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 rg   )rv  deepcopyr  s   rT   r4  z)deepcopy_to_fake_tensor.<locals>.<lambda>
  s    4=+=+= rS   )r
  r!  r  FakeCopyModer  )r  r  s   ` rT   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)r  r  s     rT   rmser  
  s,     :ejcCi!8!899:::rS   g-C6?Tforce_max_multiplierc                D   	
  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    o1t          	
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          rt           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    rd})                                *                                s)                                *                                sZ )                                *                                r4t          j                            j                  ||          rdS tW                     '                                }tY          j)        |          rtF          $                    d           tW                    '                                } 
fd} |            }|||z  dz  z   k    }|sGrEtY          j)        |          r1tY          j)        |          rt
          j-        j.        j/        j0        sd}|s) 	d||1                                j        |
           |S rdS  	d           dS t           td          tf          t          d          t6          t
          j4        f          r)rdS  k    }|s 	d t                                 |S tk                     stm                     rir2tk                    s#tm                    s '                                 t                     t                    u o k    }|s 	d!            |S to                     r\t                     t                    u o=t          t          j8                   t          j8                  	
"          S t                     j        d#v r\t                     t                    u sJ t          	 
fd$ j9                                        D                       S tu          d%t                     j                   )&z-Check correctness to see if ref and res matchNztype mismatch r   zLength mismatchFc              3  V   	K   | ]#\  }}}t          |||
	           V  $dS )	log_error(use_larger_multiplier_for_smaller_tensorr  N)same)r   aibi	fp64_reficos_similarity	equal_nanexact_dtyper  ignore_non_fpr  relax_numpy_equalitytolr  s       rT   r   zsame.<locals>.<genexpr>
  sr       ,
 ,
 "B	 $#9a%9  ,
 ,
 ,
 ,
 ,
 ,
rS   QuestionAnsweringModelOutputr  zkeys mismatch z == )	r  r  r  r  r  r  r  r  r  zAccuracy failed for key name %sTzelements mismatch c                b    t          | t          j                  r| nt          j        |           S rg   )r  r
  r  tensorr  s    rT   	to_tensorzsame.<locals>.to_tensor:  s%    "1el33H11aHrS   c              3  .   K   | ]} |          V  d S rg   r>   )r   r-  r  s     rT   r   zsame.<locals>.<genexpr>=  s+      MMiinnMMMMMMrS   zdtype mismatch %s, %srp  )atolrtolr  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.c                 j   rdS j         t          j        t          j        fv rdnd} r                                dk    rd} nmr                                dk    rd} nP                                dk     s6j        dk    r%j        d	         j        d
         cxk    rdk    sn dk    rd} | S )N      $@g      @g       @
   i  g       @r     r7  r1   g{Gz?)rq  r
  r  bfloat16numelndimr|  )
multiplierr  fp64_refr  r  r  r  s    rT   get_multiplierzsame.<locals>.get_multiplier  s     , $#t  #yU]EN,KKKQT  @ ) ((B..%)

A ) ((C//%(

 ((4//HMMcimsy}.Q.Q.Q.QPQ.Q.Q.Q.Q(??
 &)
%%rS   r  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r  r  r  r  r  r  r  r  )MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsLongformerMaskedLMOutput	InstancesSquashedNormalBoxesNormalTanhTransformFooVariablec              3     K   | ]H}t          t          |          t          
|          t          |          	           V  IdS )r  N)r  r  )r   r   r  r  r  r  r  r  r  r  r  r  r  s     rT   r   zsame.<locals>.<genexpr>  s       
 
  S!!S!!#&&-#'%9+#9a  
 
 
 
 
 
rS   zunsupported type: );r  rC  r  ru   r  r
  r  ParameterListr  r  rX  r  r  rM   r  lossr  rQ  rD  r  r  floatr!  r?   	is_sparseto_denserq  r   allclosetor  flattenr  
functionalcosine_similarityrt   r  detachcpur  r  isnanr  r  mathr  r<   cppinject_relu_bug_TESTING_ONLYr  r~   rZ   r  r  r  r  	as_tensor__dict__r   )r  r  r  r  r  r  r  r  r  r  r  r  r<  rscore	loose_tol	ref_error	res_errorr  r  passes_testr  s   ````````````         @rT   r  r  
  s{
    dE;,eh.DejQ  \F #e[->?@@ 	
 	
4T#YY44c44	
 	
 	
 s88s3xxI'(((53xx3s88# 
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 &)c8%<%<,
 ,
 ,
 )
 )
 	
" 
c	=	=	= HHM 5]!5
 
 
 	
 
C		 pF#t$$$$$388::#chhjj//111CS__CC#chhjj//CC 211 

## 	 	AFFQK#1' +)="/'=e)=    	;Q???uu#$ t	C		 WF#s#####3xx3s88###%R#c((%R%RC%R%R###t	C%,.	/	/ S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V 	yCI%%	139ciHHHuyEJ&&   4NFFF--FFF--'    MIKLLL t	++--""5=11C++--""5=11C~c3SsdKKK  tH'99#st9TTEt||15<<>>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	!& !& !& !& !& !& !& !& !& !&F ,^--
'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ErS   c                n    | j                             d          d         }d| j         d| d| j         dS )N/r7  r  z' (r   r	  )rQ  splitr  r  )r  short_filenames     rT   format_func_infor;    sC    %++C004NGt|GGGG1DGGGGrS   c               #    K   t           j        } t          j        t           _        t           j        }t          j        t           _        	 d V  | t           _        |t           _        d S # | t           _        |t           _        w xY wrg   )r<   recompile_limitr  rK  accumulated_recompile_limit)priorprior_acc_limits     rT   disable_cache_limitrA    sm      "E [F8O),F&=!&-<*** "'-<*<<<<s   A& &B z'collections.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`  getpidrr  r   )root_dirdir_names     rT   _get_debug_dirrL  .  sm     	




!
!
*
*+A
B
B	C 	 bikk

		  7<<(+++rS   c                 8    t           j        } t          |           S rg   )r<   r  rL  )
debug_roots    rT   rs  rs  :  s    &J*%%%rS   c                    d| j         v r't          | j         d                   r| j         d         S |r5ddlm} ddlm}  |dt          |           d|  dg |j        	           d S d S )
Nr  r   r7  r1   r  z Missing FakeTensor example valuez,`FakeTensor` example value was required for z but not available.r  )r  r@   torch._dynamo.excr8  r  r  r~   r  )r!  requiredr8  r  s       rT   extract_fake_example_valuerR  ?  s    $)##	/0J(K(K#y))	 666666''''''6II`t```1%01		
 	
 	
 	
 	
 	
 trS   c                6    t          |           |j        u sJ | S rg   )rA   r  )rD  r  s     rT   ensure_graph_fakerT  Q  s"    q!!R\1111HrS   c                ^     d fd}t           j        j                            ||          S )Nr  torch.fx.Nodec                   | j         dk    rd| j        vrt          |           S | j         dk    rXd| j        vrO| j        j        j        v sJ j        j        | j                 }t          |t          j        j	                  sJ |S | j        d         }s*t          |t          j
                  rt          |          S |S )Nr  r  get_attr)r  r  get_fake_valuetargetr  
nn_modulesr  r
  r   GraphModuler  rT  )r  r  r   allow_non_graph_faker  s      rT   visitz)get_fake_values_from_nodes.<locals>.visitW  s    4?""af'D'D "!R)=>>>TZO16$A$A8ry33333%ah/Bb%("677777If_%# 	.
3(E(E 	.$S"---
rS   )r  rV  )r
  r   r!  map_arg)r  r  r]  r^  s   ` ` rT   get_fake_values_from_nodesr`  V  sC          " 8=  ...rS   c           	     b    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|          \  t          j        j        j        st          j        j        j        r,t          t'           i           |          }
d |
D             }ng }
i }d|	dk    rlt)                    dk    rYt+          d         t          j        j                  r4t1          d         j                  ft5          dd                   z   |	d	k    raj        j         j                 t=                    r&t?          d
          r                                t1          j                   j!        dv stE          d D                       rt5          d D                       	 j        5  tG                      5  tI           fd          }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   n# |$ r  tJ          $ r}|}|j&        |j&        }t+          |t          j'        j(        j)                  rp|j*        t          j+        j,        j-        j.        t          j+        j,        j/        j.        fv rdg}ndg} |dta          |j*                  d|j*         d|           nt+          |t          j'        j(        j1                  rpt          j        j        j2        s- |dta          |j*                  d|j*         ddg           n |dta          |j*                  d|j*         ddg           net+          |t          j'        j(        j3                  r|j*        }	d}t+          |	t          j4        j5                  rGt          j6        7                    |	j8        j!        |	j8        j9                  }||\  }}d| d| d| d} |d d!|j*         d| d"g           nt+          |t          j:        j;        j<        j=                  r  ||j>        ta          |          d#$          t+          ||          r ||j>        |j        d                   |t+          |t~                    r+d%ta          |          v r |d&d' j         d(| dg             |ta          |                    @                    |jA                  dd}~ww xY w|s9t          jC        t          jD        t          jF        t          )          |          }t          j        j        j        st          j        j        j        r!j        jH        I                     |
|           |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   )ValueRangeErrorr1   )TorchRuntimeErrorr8  Unsupported	UserErrorUserErrorTyper  c                V    i | ]&}t          |          t          |          |j        'S r>   )r@   r  r  r3  s     rT   r6  z"get_fake_value.<locals>.<dictcomp>  s=     !
 !
 !
&)!
sGGS\!
 !
 !
rS   Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  @   K   | ]}t          |t                    V  d S rg   )r  complex)r   as     rT   r   z!get_fake_value.<locals>.<genexpr>  sE       M M#$
1gM M M M M MrS   c              3     K   | ];}t          |t          j                  r|j        j        t          |          n|V  <d S rg   )r  r
  SymFloatr!  hintr!  r3  s     rT   r   z!get_fake_value.<locals>.<genexpr>  sc       
 
  #u~..368=3L #JJJ
 
 
 
 
 
rS   c                 4    t          j                   S rg   )run_noder  )r   r   nnmoduler!  r  s   rT   r4  z get_fake_value.<locals>.<lambda>  s    D$II rS   zkEnable tracing of data-dependent output operators with `torch._dynamo.config.capture_scalar_outputs = True`zConsider wrapping the operator into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html)zData dependent operatorz
Operator `zP` has a non-Tensor output whose value is dependent on the data of Tensor inputs.r  zDynamic shape operatorz.`'s output shape depends on input Tensor data.zmEnable tracing of dynamic shape operators with `torch._dynamo.config.capture_dynamic_output_shape_ops = True`z'Dynamic shape operator (no meta kernel)zA` does not have a meta kernel that supports dynamic output shapesz!Please report an issue to PyTorchr  z:It's possible that the support was implemented in module `z` and you may need to `import z`(z), otherwise z3Operator does not support running with fake tensorszunsupported operator: 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 when making fake tensor callz
TypeError z: )r  )Jtorch.utils._sympy.value_rangesrb  r  rc  r8  rd  re  rf  r  r  r@   r`  r   r   r
  rt  r<   use_graph_deduplicationtrack_nodes_for_deduplicationr2   rX  r  r  rh  r  r  r  r  r[  rZ  r&  r  _infer_parametersr^   r  r   r  r   	__cause__r!  r  DataDependentOutputExceptionfuncopsatenr  r  _local_scalar_denser~   DynamicOutputShapeException capture_dynamic_output_shape_opsUnsupportedOperatorExceptionr  r  r  _dispatch_pystub_schemaoverload_namer   r  r  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr  with_traceback__traceback__pytreetree_map_onlyr  r  partialrT  region_trackertrack_node_mutations)r!  r  r]  rb  rc  r8  rd  re  rf  r  flat_args_kwargsid_to_initial_versionret_valrD  causer>  import_suggestionmaybe_pystubr  ctxr  r   r   rv  s   ``                   @@@rT   rY  rY  k  s    @?????              
B $)##	/0J(K(K#y))-
TY$&: LD&
 	4#==# 6tR((*>
 
!
 !
-=!
 !
 !
  "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  
 
 	
 
 
 
 
hR\ 	 	355 	 	)IIIIIIII G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     aR aR aR ;"KE5$0M
 
 Z	
 z	#+	2:  K` 1EJI I I I     5$0L
 
 @	 ='H   4
OO gUZ g g gY	     ! E
OO zUZ z z z;	     5$0M
 
 *	 B ""ej344 
$x88JORZ%=     +".KFC/#)/ /IO/ // / / &
 M===( & & &		 	 	 	 	 58(8T
 
 	 )2E

5   
 // 	)M>q	JJPQQy)) 	jCJJ.F.F@;T[;;E;;	     A''66qGGTQCaRF   
 L)+,="EEEw
 

 	4
==
 		 55!	
 	
 	
 Nsa   $I +H4:HH4H!	!H4$H!	%H4(I 4H88I ;H8<I T$KTT$c                 .    t          t          dd           S )Nr   )r  _current_noder>   rS   rT   get_current_noder  2  s    ='4000rS   c              #     K   t                      }| t          _        	 d V  |t          _        d S # |t          _        w xY wrg   )r  r  r   )r!  olds     rT   set_current_noder  6  sI      


CM"!c!!!!s   0 >c           	       
 j         
t                    5  
fd}ddlm} 	 
dk    r j        i cddd           S 
dk    rnt          d         j                  sddlm}  |d	d
 |d          g             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 n# t          t          f$ rC}ddlm} g }	t          |t                    rdg}	 |dd
 ||          |	|           Y d}~nKd}~w|$ r  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 )Nz0Dynamo failed to run FX node with fake tensors: r   z(*z, **z): got )rZ  r'  )rD  r   r   r!  r  s    rT   make_error_messagez$run_node.<locals>.make_error_messageS  s@    p2ppppW[ppagpppq''rS   r1   )rd  r  Nrh  r   r7  z/Missing attribute when running call_method noder  zattribute not definedr  ri  rX  placeholderr  z;If the op is a PyTorch op, please file an issue to PyTorch.zGNotImplementedError/UnsupportedFakeTensorException when running FX noder:  )r  r  r  rd  rZ  r  r8  r  output_graphget_submoduler  r@  r  r  r  r   r  r  r  )tracerr!  r   r   rv  r  rd  r8  rD  r>  r  s    ```      @rT   ru  ru  @  s    
B	$		 7 7	 	 	 	 	 	 	 	 	%$$$$$-	_$$"t{D3F337 7 7 7 7 7 7 7 }$$tAw44 555555$$ Q "$6$67N$O$O 	    5wtAw44d122hI&II/7 7 7 7 7 7 7 70 }$$+++x00057 7 7 7 7 7 7 76 z!!*88EE97 7 7 7 7 7 7 7: }$$&$)3333y1?7 7 7 7 7 7 7 7: % $%CD 	 	 	------E!011 Q a..q11          	 	 	 	 	 	11!4455DD 	i7 7 7 7 7 7 7 7 7 7 7 7 7 7 7r 

s_   GD8
A'D8>D8$D8D87G8G	9FGG1G

GGG"%G"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.
    r1   )rc  c                $    t          |           S rg   )get_real_value)r  r  s    rT   r4  z get_real_value.<locals>.<lambda>  s    .F++ rS   r  graphargri  N)r  rc  real_value_cacher  r
  r   r!  r_  r   r   r  exampler  r[  rZ  r&  rv  r  ru  r   r~   r  r  )
r!  r  rc  cacher  r   r   	nn_module
real_valuerD  s
    `        rT   r  r    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)r  r  r  s     rT   stack_or_hintz7assert_no_fake_params_or_buffers.<locals>.stack_or_hint  s?    ! 	d`9N9Nq~9^9^```ccrS   zUnexpected fake buffer r   zUnexpected fake param )torch._subclasses.fake_tensorr  r@   named_buffersnamed_parameters)r  r@   r  r^   bufferr  r  s         @rT    assert_no_fake_params_or_buffersr    s   GGGGGGGGd d d d d ((** 
 
f76?? 	
 	
DdDD]]6-B-BDD	
 	
 	
 	
 **,, 
 
e75>> 	
 	
BTBBMM%,@,@BB	
 	
 	
 	

 
rS   c                $    | j          d| j         S )z9
    Returns the fully qualified name of the object.
    r  )rN   rO   r  s    rT   fqnr    s     n11s/111rS   c                6    t           j        j        j        r| S |S rg   )r
  rt  r<   assume_static_by_default)count1count2s     rT   ifdynstaticdefaultr    s    }4 rS   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   r  r  N)r  r`  listdirrr  r  r   r~   __file__r  	importlibimport_modulerM   )r%  r  s     rT   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rS   c                :    t          t          |                     S rg   )class_has_getattributer  r  s    rT   object_has_getattributer    s    !$u++...rS   c                F    t                               | d          }|||<   d S )Nr0  )r   __getattribute__)r  r^   r   r  s       rT    object_setattr_ignore_descriptorr    s$    Z00AAdGGGrS   r_   r  c                    	 t          t          j        | d          t          j                  rdS n# t
          $ r Y nw xY wdS )Nr  TF)r  r  r  r   r  AttributeErrorri   s    rT   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  r  r  r
  r  rh  r  )r   r  
getattr_fns      rT   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 )TensorStaticReasonrK   r
  r   N)rM   rN   rO   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYr>   rS   rT   r  r    s         IJrS   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    rT   tensor_static_reason_to_messager    sX    #---OO#...CC#666EE
3633
4
44rS   r  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.
    r1   ))is_from_unspecialized_param_buffer_sourceTrR  )r(  r  r*  r,  r.  r<   &force_nn_module_property_static_shapesr  r  r  r
  r  r  force_parameter_static_shapesr  r  )r  r  r  r  s       rT   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;rS   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>   )r  r^   rZ  r   r   )r   r  s     rT   r=  z<lazy_format_graph_tabular.<locals>.inner.<locals>.<listcomp>=  s:     
 
 
;<QT1618QVQX6
 
 
rS   )opcoder^   rZ  r   r   r   )
r   r   r~   r+   graphr  r*   forward__code__rQ  )r   
node_specs	graph_strr  r  s      rT   innerz(lazy_format_graph_tabular.<locals>.inner4  s    	))))))) 	 	 	~,Wb99::;  	
 
@B
 
 

 H N N N
 
 
	 "'2:+>+JIVVVs   
 *77r:   )r  r  r  s   `` rT   lazy_format_graph_tabularr  3  s8    W W W W W W" erS   c                l    |  d| d| d| dt          j        |                                            d
S )Nr   z line z 
r   )disBytecode)r  r^   r  line_nor  s        rT   format_bytecoder  H  sG    VVtVVhVVgVV#,t:L:L:P:P:R:RVVVVrS   _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 rg   )rX  r
  r  modulesr  _global_backward_hooks_global_backward_pre_hooksr>   rS   rT   nn_module_has_global_hooksr  W  s@     ux&=>> #:C C rS   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_namesr  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_namerg  s              rT   nn_module_get_all_hooksr  _  s      	'$$	'&& 
  7o 7""#5666 8 8""#6777 :""#8999I- # #^R00 	# 	#I#DT""""	# rS   c                F    t          | |||          }t          |          S )zL
    Helper function to check if a module has any hooks attached to it.
    )r  r  r  )r  r   )r%  r  r  r  r  s        rT   nnmodule_has_hooksr    s2     $/15	  E ;;rS   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 rg   )to_numpy_helperr   r  s     rT   r   z"to_numpy_helper.<locals>.<genexpr>  *      AAC?3//AAAAAArS   )r@   r  tnpr  r  r  r
  r  numpyr  rC  r  r  s    rT   r  r    s    u~~ %%% u|,,,	E5<	(	( {{{&&&	EE4=	)	) tE{{AA5AAAAAArS   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 rg   )numpy_to_tensorr  s     rT   r   z"numpy_to_tensor.<locals>.<genexpr>  r  rS   )
r  r  r  r
  r/  r  r  r  rC  r  r  s    rT   r!  r!    s    >>>%$$ &u%%%%%% |	EE4=	)	) tE{{AA5AAAAAArS   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_r  rM   )rN  r  s     rT   rO  z numpy_to_tensor_wrapper.__init__  s    "TV_4rS   r}   r~   c                "    d| j         j         dS )Nz<Wrapped function <original >>r'  rR  s    rT   __repr__z numpy_to_tensor_wrapper.__repr__  s    AdfoAAAArS   c                :     | j         |i |}t          |          S rg   )r  r!  rN  r   r   r   s       rT   rb  z numpy_to_tensor_wrapper.__call__  s&    dfd%f%%s###rS   Nr}   r~   )rM   rN   rO   rO  r*  rb  r>   rS   rT   r#  r#    sJ        5 5 5B B B B$ $ $ $ $rS   r#  c                   t          | t          j                  rt          | |          }t	          |          S t          | t
          j                  r1t          t          j        |           |          }t	          |          S d S rg   )r  r  r  r  r!  r
  r  )r  r^   r   s      rT   numpy_attr_wrapperr/    sx    #s{## $c4  s###	C	&	& $ck#&&--s###$ $rS   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.rZ  r~   c                2    || _         d| j         z   | _        d S r%  )rZ  rM   )rN  rZ  s     rT   rO  znumpy_method_wrapper.__init__  s    "T[0rS   r}   c                    d| j          dS )Nz<Wrapped method <original r)  )rZ  rR  s    rT   r*  znumpy_method_wrapper.__repr__  s    ;DK;;;;rS   c                    |d         }t          |t          j                  rt          j        |          }t          || j                  } ||dd          i |}t          |          S r  )r  r
  r  r  r  r  rZ  r!  )rN  r   r   r  method_callabler   s         rT   rb  znumpy_method_wrapper.__call__  si    1gc5<(( 	#+c""C!#t{33otABBx2622s###rS   N)rZ  r~   r-  rM   rN   rO   r   rO  r*  rb  r>   rS   rT   r1  r1    sQ        qq1 1 1 1< < < <$ $ $ $ $rS   r1  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 libraryr  Callable[..., Any]c                2    || _         d|j         | _        d S r%  )r  rM   )rN  r  s     rT   rO  znumpy_operator_wrapper.__init__  s    02;00rS   r}   r~   c                    d| j          dS )Nz<Wrapped operator <original r)  )rM   rR  s    rT   r*  znumpy_operator_wrapper.__repr__  s    ?dm????rS   c                T    |rJ d |D             } | j         | }t          |          S )Nc              3  v   K   | ]4}t          |t          j                  rt          j        |          n|V  5d S rg   )r  r
  r  r  r  r3  s     rT   r   z2numpy_operator_wrapper.__call__.<locals>.<genexpr>  sS       
 
KN
3 = =FCK3
 
 
 
 
 
rS   )r  r!  r,  s       rT   rb  znumpy_operator_wrapper.__call__  sI    
 
RV
 
 
 dgtns###rS   N)r  r9  r-  r6  r>   rS   rT   r8  r8    sU        [[1 1 1 1@ @ @ @$ $ $ $ $rS   r8  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)rq  r  rk  )r  r?   _has_symbolic_sizes_stridesr  r
  r   r  r!  r  	size_hintexprr}  empty_stridedrq  r  rk  zero_)r3  r  rO  r}  rn  s        rT   defakerD    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HrS   c                     | |i |S rg   r>   )r  r   r   s      rT   8_disable_side_effect_safety_checks_for_current_subtracerrF  
  s    2tvrS   c                0    dd l }| |j        j        j        u S r   )torch.utils.checkpointr  
checkpoint)r  r
  s     rT   is_utils_checkpointrJ    s!    !!!!%+(333rS   c                    ddl m} | |u S )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphrL  )r  rL  s     rT   is_invoke_subgraphrN    s     SSSSSS---rS   c                 H    ddl m}  |j        t          j        j        fi | S )Nr1    TorchHigherOrderOperatorVariable)variables.higher_order_opsrQ  maker
  _higher_order_opsinvoke_subgraph)optionsrQ  s     rT   build_invoke_subgraph_variablerW    sB    LLLLLL0+0/ 
  rS   c                     dd l mc m} ddlm} |j        }t          j        j        j	        r|j
        } |j        |fi | S )Nr   r1   rP  )torch._higher_order_ops.wraprT  wraprR  rQ  tag_activation_checkpointr
  
_functorchr<   functionalize_rng_opswrap_activation_checkpointrS  )rV  higher_order_opsrQ  activation_checkpoint_ops       rT   build_checkpoint_variablera  $  s    ;;;;;;;;;LLLLLL
 	2  4 O#3#N 0+0  
  rS   c                    ddl m} t          j        |           j        } |            }|dk    rn|dv r|rt                      }nd}|S )Nr1   )is_dynamo_supportedr*  )r  xpuF)
eval_framerc  r
  r  r  r.   )device_typerc  r  compile_supporteds       rT   is_compile_supportedrh  7  sk    //////<$$)D++--u}}		 	 %6	 &LL!rS   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)encoderX  decode)r~   ri  as_utf8s      rT   _fix_offsetro  I  s?     jj!!Gwww&&wy&AABBBrS   c                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )_AnchorsrZ   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetNr  r>   rS   rT   rq  rq  U  sD          rS   rq  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
    )rL      r   Nz(
z
)r1   r   c                0    t          |          |          S rg   )ro  )linenori  liness     rT   	normalizez-_extract_anchors_from_expr.<locals>.normalizey  s    5=&111rS   c                &   | t                    k     rL|t          |                    k    r3d}| dz  } | t                    k     r|t          |                    k    3| t                    k     r|t          |                    k     sJ | |fS r  rX  )r{  colr|  s     rT   next_valid_charz3_extract_anchors_from_expr.<locals>.next_valid_char~  s    s5zz!!cSv-?-?&?&?CaKF s5zz!!cSv-?-?&?&? E

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

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

""sSv-?-?'?'?'?'?s{rS   rK   z)\#z\#r  r  )r  r  astparseSyntaxErrorrX  bodyr9  r  Exprr   BinOpr   rZ   left
end_linenoend_col_offsetisspacerq  	SubscriptCallr  )rv  r  treer}  r   r  	statementrA  
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor|  r  s                  @@rT   _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r  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).
    )rL      )r  colno	end_colnor   r   r1   Nc                h    g | ].}t          j        j        |                                          /S r>   )	linecachegetlinerQ  rstrip)r   r{  r  s     rT   r=  z.get_instruction_source_311.<locals>.<listcomp>  sE     
 
 
 d.77>>@@
 
 
rS   r  r   ~c                :    g | ]}|                     d d          S )r  ^)r  r   markers     rT   r=  z.get_instruction_source_311.<locals>.<listcomp>3  s&    BBB6>>#s++BBBrS   c                ,    g | ]}t          |          S r>   rC  r  s     rT   r=  z.get_instruction_source_311.<locals>.<listcomp>6  s    +O+O+OVDLL+O+O+OrS   r  c                8    g | ]}d                      |          S )r  r  r  s     rT   r=  z.get_instruction_source_311.<locals>.<listcomp>P  s"    AAAv2776??AAArS   )r  r  r  FrameSummaryrQ  	positionsr{  r  r  
col_offsetr  r  r   
splitlinesr  textwrapdedentrX  indentr  r  r  ro  r  lstripr  r  rr  rs  rt  ru  )r  r  frame_summaryr  
orig_linesorig_lines_dedent
indent_lenr  
first_linestart_offset
end_offsetrv  markers	last_liner{  line
num_spacesanchorsmutable_markersr  rM  s   `                    rT   get_instruction_source_311r    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  r  r   s    rT   get_static_address_typer  \  s.    !U\"" =q5t<<<4rS   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 rg   )	r
  r  r3   	get_statedefault_generatorr  r  	set_stater  )r   getterssetterss      rT   is_rng_state_getter_or_setterr  c  ss     	$)
 G 	$)
 	G (g(((((rS   c                    t          | t          j                  o'| j        dk    o| j        j        t          j        j        u S )N__get__)	r  r   r   rM   __self____objclass__r
  r  _TensorBaser  s    rT   is_tensor_base_attr_getterr  t  s?    5%122 	@Ni'	@N'58+??rS   c                    	 t          j        t          j        |           }t	          |          t
          j        u S # t          $ r Y dS w xY wr  )r  r  r
  r  r  r   r  r  )r^   attrs     rT   is_tensor_getset_descriptorr  |  sQ    %elD99DzzU777   uus   9< 
A
	A
c                "    t          | d          S )N__torch_function__)r  r  s    rT   is_torch_function_objectr    s    5.///rS   r  ,torch._dynamo.variables.base.VariableTrackerc                r   ddl m} ddlm} |                                 s2t          | d          rt          |                                 d          rdd }t          | |          rt          | j	        dd           }n&t          | |          rt          | j
        dd           }|d t          j        j        fvS dS )Nr   )UserDefinedObjectVariable)TensorWithTFOverrideVariable
peek_valuer  F)torch._dynamo.variablesr  &torch._dynamo.variables.torch_functionr  r6  r  r  r  r  
class_typer   r
  r  _disabled_torch_function_impl)r  r  r  r  s       rT   has_torch_functionr    s     BAAAAASSSSSS 
~~ 
JL!!
J&-bmmoo?S&T&T
J b677 	A2=*>EEDD566 	A28%94@@DD%("HIII5rS   c                    d }d }t           j        j                                        x}r| |j        v r|j        |          }|j        }|                    | d||          S )NF)static_shapessymbolic_contextr(  )r
  r  r#   try_gettensor_to_contextr  from_tensor)r  r  r  r(  tracing_contexts        rT   to_fake_tensorr    sv    F-6>>@@@ 4111.@C%3F  	1A& !   rS   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    rT   is_frozen_dataclassr    st    #E*** 	.&u---	.	. E122	. E.99		.
 &-rS   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  r  r  )r  attrsr  s      rT   get_first_attrr    s^      & &3 	&3%%%%%	& CMMeMM
N
NNrS   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compilesr1   )r
  rt  r  rC   r  compile)gm_example_inputs_s     rT   inner_compilerzKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compiler  sE    #,-@A*MMMQRRMMM..sODDDrS   )backend	fullgraphdynamic)r
  r  )r  r  r  r  s     rT   compiler_fnz3maybe_enable_compiled_autograd.<locals>.compiler_fn  s9    E E E =Ni   rS   )r
  rt  r  _enable)should_enabler  r  r   r  s    ``  rT   maybe_enable_compiled_autogradr    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)rM   rN   rO   r>   rS   rT   Invalidr    s        rS   r  )r  r0   )r  s    rT   invalid_removeable_handler    sB        $    7799%%%rS   c                    t          | t          j        j                  s| S t          | t          j        j                  r| S | j                            | j                  }| j        |_        |S rg   )	r  r
  r  rh  r   r\  r  r  r0  )r%  proxys     rT   nn_module_proxyr    s^    c58?++ 
#ux+,, 
M!!#-00E\ENLrS   c                  $     e Zd Z fdZd Z xZS )	GmWrapperc                d    t                                                       || _        || _        d S rg   )r  rO  r  unflatten_fn)rN  r  r  r  s      rT   rO  zGmWrapper.__init__  s.    (rS   c                Z    t          |          } | j        |                     |           S rg   )rC  r  r  ra  s     rT   r  zGmWrapper.forward  s*    t**tw))$//00rS   )rM   rN   rO   rO  r  r  r  s   @rT   r  r    sG        ) ) ) ) )
1 1 1 1 1 1 1rS   r  r  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)r  r  rk   )r   rM  r!  s      rT   r=  z(flatten_graph_inputs.<locals>.<listcomp>  sH       At7m##	k5(I(I# 	
###rS   r   c                B    | d         t          | dd                    z   S r  r  r(  s    rT   
flatten_fnz(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qrr(^^++rS   c                0    | d          g| d          R S rg   r>   )	flat_argsboxed_inputs_counts    rT   r  z*flatten_graph_inputs.<locals>.unflatten_fn  s,    1112TY?Q?R?R5STTTrS   )treespecc                 n     |           }D ]}| |                                            |          S rg   )rh   )r   r  rM  compiled_fnr  inputs_idx_to_clears      rT   wrapperz%flatten_graph_inputs.<locals>.wrapper!  sH    Jt$$	 % 	 	AGMMOOOO {9%%%rS   )	enumerater  r  r
  rt  r  in_compiled_autograd_regionr  rC  rX  r  r  tree_flattenr  r  tree_unflattenarg_tree_leaves)r  inputs
compile_gmr  flat_inputsspecr  r  r  r  r  s          @@@@rT   flatten_graph_inputsr(    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+
& & & & & & & NrS   c                    t          | t          j        j                  rt	          | d          sg S | j                            dg           S )Nr  locals_to_steal)r  r
  r   r\  r  r  rk   )maybe_gms    rT   get_locals_to_stealr,  .  sH    h 455 WXv=V=V 	=.333rS   c                    || j         d<   d S )Nr*  r  )r  r*  s     rT   set_locals_to_stealr/  4  s    !0BGrS   c                      e Zd Zd ZddZdS )Litc                    || _         d S rg   rO  )rN  rO  s     rT   rO  zLit.__init__9  s    rS   r}   r~   c                    | j         S rg   r3  rR  s    rT   r*  zLit.__repr__<  s	    vrS   Nr-  )rM   rN   rO   rO  r*  r>   rS   rT   r1  r1  8  s7               rS   r1  zset[str]warn_once_cachec                    | t           v rd S t                               |            t          j        | |dz              d S )Nr1   )
stacklevel)r5  r[  warningswarn)r  r7  s     rT   	warn_oncer:  C  sG     oM#*q.111111rS   c                V    t          j        d          }|                    d|           S )Nz\x1B[@-_][0-?]*[ -/]*[@-~]r  )r  r  r  )textansi_escapes     rT   strip_color_from_stringr>  O  s&    *:;;K??2t$$$rS   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)r?  s    rT   +_disable_saved_tensors_hooks_during_tracingrB  U  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 rg   )r
  r  r<   freezingis_grad_enabledr>   rS   rT   is_parameter_freezingrF  _  s!    ?!*J53H3J3J/JJrS   c                 L    d t          t                                D             S )Nc                ,    g | ]}t          |          S r>   ) get_torch_function_mode_stack_atr  s     rT   r=  z1get_torch_function_mode_stack.<locals>.<listcomp>d  s.       01(++  rS   )r  r   r>   rS   rT   get_torch_function_mode_stackrJ  c  s2     5:;T;V;V5W5W   rS   c                t    | t                      k     r| dk    sJ t          j                            |           S r   )r   r
  r  _get_function_stack_at)inds    rT   rI  rI  i  s6    *,,,,8**3///rS   c                    t          t                                D ]}t                       | D ]}t          |           d S rg   )r  r   r   r   )r  r  modes      rT   set_torch_function_mode_stackrP  n  sY    ,..// $ $!#### , ,%d++++, ,rS   c                 ^    t          t                                D ]} t                       d S rg   )r  r   r   )r  s    rT   clear_torch_function_mode_stackrR  v  s9    ,..// $ $!####$ $rS   c                 "    t                       d S rg   )
breakpointr(  s    rT   _breakpoint_for_c_dynamorU  |  s    LLLLLrS   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 )NrK   r1   )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  	signaturerX  r  r  rW  r  r  )r   r  sigrW  s       rT   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
 
 	
 &%rS   c                    | j         t          j         t          j         fv o\| j        t          j        t          j        fv o=| j        t          j        t          j        fv o| j        t          j        t          j        fv S rg   )r   r  r   r6  rD  __iter__)user_clss    rT   #does_not_override_dict_iter_methodsr_    sl    4:{'899 	GO[-?@@	GMdi)9::	G $-1E!FF	rS   c                ,    |                      |          S rg   )r  r3  rM  s     rT   	call_sizerb    s    66!99rS   c                ,    |                      |          S rg   )r}  ra  s     rT   call_striderd    s    88A;;rS   c                *    |                                  S rg   )storage_offsetr2  s    rT   call_storage_offsetrg    s    rS   c                ,     ddg} fd|D             }|S )Nr  tagc                b    i | ]+}|j         v |t          j        j         |                   ,S r>   )r0  rv  )r   r   r  s     rT   r6  z(_extract_tensor_dict.<locals>.<dictcomp>  s>       ,/3!*CTCTTYqz#''CTCTCTrS   r>   )r  KEYS_TO_COPYtensor_dicts   `  rT   _extract_tensor_dictrm    s@    #L
   3?  K rS   z(dict[int, weakref.ReferenceType[object]]user_obj_id_to_weakrefc                F    t          |                      }|
J d            |S )NzUser object is no longer alive)rn  )obj_idr  s     rT   get_user_object_from_idrq    s'    
 
(
*
*C??<???JrS   c                \    t          |           }t          j        |           t          |<   d S rg   )r  r  r  rn  )r  rp  s     rT   store_user_object_weakrefrs    s(    WWF%,[%5%56"""rS   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   rZ   _counterr7  _idr}   r   c                n    | j         dz   | _         | j         dk    rt          j                    | _        d S d S r   )_depthr   r  rw  ri   s    rT   r  z#CompileTimeInstructionCounter.start  s5    Z!^
:??*022CGGG ?rS   c                    | j         dz
  | _         | j         dk    r0| xj        t          j        | j                  z  c_        d| _        d S d S )Nr1   r   r7  )ry  rv  r   endrw  ri   s    rT   r{  z!CompileTimeInstructionCounter.end  sJ    Z!^
:??LL04SW===LLCGGG ?rS   c                    d| _         d S r   rv  ri   s    rT   rh   z#CompileTimeInstructionCounter.clear  s    rS   c                    | j         S rg   r}  ri   s    rT   r   z#CompileTimeInstructionCounter.value  s
    |rS   c              #     K   	 t           j        r|                                  d V  t           j        r|                                  d S d S # t           j        r|                                  w w xY wrg   )r<   %record_compile_time_instruction_countr  r{  ri   s    rT   recordz$CompileTimeInstructionCounter.record  st      	; 		EEE; 					 v; 				s   $A #A/NrS  )r}   rZ   )rM   rN   rO   rv  rw   rw  ry  rx   r  r{  rh   r   r   r  r>   rS   rT   ru  ru    s         HCMMMMF3 3 3 [3
    [    [    [   ^ [  rS   ru  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     rT   set_feature_user    sO     ((** M++OWeLLLLLM MrS   )ddp_optimizerpython_reducer'python_reducer_without_compiled_forwardno_optimization_ddp_optimization_modec                     t           j        } t          | t                    r| rdnd}n7t          | t                    r| }nt          dt          |                     |t          v sJ d|            |S )Nr  r  z;Invalid dynamo config optimize_ddp type type(optimize_ddp)=z.Invalid dynamo config optimize_ddp value mode=)r<   optimize_ddpr  r   r~   r  r  r  )r  rO  s     rT   get_optimize_ddp_moder    s    &L,%% 
".E4E	L#	&	& 
LtL7I7ILL
 
 	
 ))));D;; *)) KrS   Generator[None, None, None]c               #    K   t           j        ot          j                    } | r^t          j        d          5  t          j                    5  dV  ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS dV  dS )ar  
    Disables torch.inference_mode for the compilation (still on at runtime).
    This simplifies the compile stack where we can assume that inference_mode
    will always be off.

    Since inference_mode is equivalent to no_grad + some optimizations (version
    counts etc), we turn on no_grad here. The other optimizations are not
    relevant to torch.compile.
    FN)r<   "fake_tensor_disable_inference_moder
  is_inference_mode_enabledinference_moder  )is_inference_mode_ons    rT   maybe_disable_inference_moder    s      	1We6U6W6W    ''	 	MOO	 	 EEE		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s5   A5AA5A!	!A5$A!	%A55A9<A9c               #     K   t           j        rBt          j        j                                        5  dV  ddd           dS # 1 swxY w Y   dS dV  dS )z
    Turns off tracking of inference_mode for fake tensor propagation. With this
    context manager, when a real tensor is converted to fake tensor, the fake
    tensor looses its inference-ness.
    N)r<   r  r
  r!  
meta_utils$disable_inference_mode_for_fake_propr>   rS   rT   *maybe_disable_inference_mode_for_fake_propr  -  s       0 )NNPP 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAr!  Optional[torch.fx.Node]c                .    | d u pd| j         v pd| j         v S )Nr  r-  r.  )r!  s    rT   is_node_meta_validr  ;  s$    4<M?di7M5DI;MMrS   AbstractContextManager[None]c                     t           j        j        j        r$t           j        j                            d          nt          j                    } | 	                                 | S )NzPregraph bytecode)
r
  autogradr  _is_profiler_enabledr  	_profiler_RecordFunctionFastr  nullcontext	__enter__cms    rT   record_pregraph_bytecode_enterr  ?  sR     >"7	&../BCCC#%% 
 LLNNNIrS   r  c                4    |                      d d d            d S rg   )__exit__r  s    rT   record_pregraph_bytecode_exitr  J  s    KKdD!!!!!rS   list[CodeType]c                 ,    ddl m}   | j                    S )Nr   r#   )torch._guardsr#   get_traced_coder  s    rT   r  r  Q  s%    ,,,,,,)>)+++rS   )ry   rz   r{   r|   r}   r~   rS  )r   rZ   r}   r   )r}   rG   )r}   r   )r}   r    )NFNNNNFN)r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r}   r   r   )r'  r(  r)  r   r}   r~   )r'  r-  r)  r   r}   r.  )r~   F)r)  r   r-  )r  r~   r}   r   )r  r  r}   rZ   )r3  rB   r}   rB   )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  rg   )rB  rF  r}   r   )r  rO  r}   r   rQ  )
r  rZ   r  rZ   r  rD   r  r  r  r  )r  rZ   r}   r   )r}   r  )r}   r  )r}   r   )FF)r   r~   rW  r   r   r   r}   r   )r-  r  )r}   r  )r1   )r   r   r}   r  )r}   rw  )r<  r   r}   r  )r  r   r}   r   )r  r~   r}   r~   )r  r   )T)r  r   )r%  r  )r   r   )r_   r  )r   r   r  r   )r  r  )r  r  r  r   r  r"   r}   r  )FFF)r~   r~   ri  rZ   r}   rZ   )rv  r~   r}   rw  )r  r  r  r  r}   r~   )r  r  r}   r   )TT)r  r  )r  r~   r  r   )r}   r  )r!  r  r}   r   )r}   r  )r  r  r}   r   )r}   r  (  r   
__future__r   atexitru   r  rv  r  rF  r  r  r  r  r  r  r   r  r  ru  r,  rl  r`  r  r  r  r"  r   r  r   r  r$  r8  r  r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r
  torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreer  _pytreer  r   torch._Cr   r   r   r   torch._dispatch.pythonr   torch._dynamo.metrics_contextr   r    r  r!   r"   r#   torch._subclasses.meta_utilsr$   torch._utils_internalr%   r&   r'   r(   r)   torch.fx._utilsr*   r+   torch.monitorr,   torch.nn.modules.lazyr-   torch.utils._tritonr.   r/   torch.utils.hooksr0   graph_utilsr2   r  collections.abcr3   r4   r5   r6   r7   r8   r  r  r  torch._loggingtorch._numpy_numpyr  r9   r;   r  r<   fftlinalgr  r=   rw   NP_TO_TNP_MODULEr  r?   r@   rA   r   rB   r  rh  r  unpatched_nn_module_getattrrb  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implrv   rC   rE   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgrw  rM   rt   rF   r!  rH   ra   timer_counterr  rJ   rV   r   r   r   r   r   r   r   r   r   r   r   r   r#  r  r&  r,  registerrG  FloatTensorr  DoubleTensorr  double
HalfTensorr  halfBFloat16Tensorr  
ByteTensorr  
CharTensorr  
LongTensorr  long	IntTensorr  rZ   ShortTensorr  short
BoolTensorr   tensortype_to_dtyperI  rf  rd  rg  rb  r  r  r  r  r  r  r  r  r  r  ParamSpecArgsParamSpecKwargs	ParamSpecTypeVarTupleTypeAliasTyper  r  r  r  r  r  r  r  r  r  r  r  r  neltlegtgecmp_name_to_op_mappingcmp_name_to_op_str_mappingr  r  r  r  r#  r&  r)  r.  rE  rK  rM  r  r  rO  !DEFAULT_COMPILATION_METRICS_LIMITr  r  r  r  r  r	  r   r   r  r  r  r  rT  r   r   r[  r]  r`  re  ro  ru  r  r  r  r  r  r  r  r  r  r  r  r  r  ro  r~   rY   r  Ellipsisr  NotImplementedr  rq  memory_formatrz  finfoiinfo	attention
SDPBackendr  _CudaDevicePropertiesr  r  r[  languager  r  r  r%  r0  r;  r?  rC  rJ  rL  rP  rD  rQ  r6  rR  r   rS  rT  iterrU  r  rV  __length_hint__tuple_iterator_lenr   r  rg  r  dict_newr   r0  dict_methodsr  	tuple_newtuple_methodsrC  list_methodsr  list_getitemstr_methodsr`  re  ri  ro  rr  rv  r  	iter_nextr|  r  dict_getitemr  r  r  r  r  r  r  r  r  GLOBAL_KEY_PREFIXtorch._subclassesr  r  r  r  r  r  r  r  r  r;  rA  orig_code_maprB  rC  seen_code_maprL  rs  rR  rT  r`  rY  r  r  r  ru  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r
  all_hook_namesr  r  r  r  r!  r#  r/  r1  r8  rD  rF  rJ  rN  rW  ra  rh  ro  rq  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r(  r,  r/  r1  rQ  r5  r:  r>  rB  rF  rJ  rI  rP  rR  rU  r[  r_  _disable_dynamorb  rd  rg  rm  rn  rq  rs  ru  r  r  r  r  r  r  r  r  r  r>   rS   rT   <module>r	     s     # " " " " "                



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



                , , , , , , , , = = = = = = = = $ $ $ $ $ $       - - - - - - - -
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 D C C C C C C C C C C C      , , , , $ $ $ $ $ $ $ $ $                  < ; ; ; ; ; O O O O O O O O ; ; ; ; ; ; ; ; ; ; = = = = = =              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  % : 7N{7N8 8     %'  & & & &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
 
 
 
 
                     > !       / / / /   $ $ $ $    49   S# S# S# S# S# S# S# S#l $IO%%   !%"',0.2&*"&!/3NQ NQ NQ NQ NQb 
 L L L L 
 L 
,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 
 A A A 
 A 
   

 
 C C C 
 C& & & w 	%!G G G  &   
5 5 5 
5
 
7 7 7 
7
    				!
           kkkkkk        83 3 3  % % %$ $ $, , , T     
 
 
$0 0 0 05 5 5 5   q" q" q" q" q" q" q" q"h %' ! ?Pk>O,? ? ?     
- - - -` 3  3  3  3F( ( ( (VK3 K3 K3 K3^ ">*DEEE 009STTT % % % %! ! ! !
& & & &m m m m m m m m`	 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  $        >  2  W W W
 
 
     "&bggii	 0 0 0 0%)T"))++%6%6 6 6 6 6(,RXXZZ(8(8
 8 8 8 8&*d;;==+?+?+A+A&B&B B B B B&*d4488nn 4 4 4 4&*d44a>>&:&: : : : :#3 ^
< !)/$-"6"6"8"8+:N:U:U:W:WXX   M	RRen&;&;&=&=RRRPPT]%9%9%;%;PPPNNCL$7$7$9$9NNN  G G G  1 1 1  
: : : 	      @ @ @  8 8 8$  & & & &RM M M M@ @ @   B       
 !  = < < < < <7 7 7    CH ( ( ( ( ( ( 
 
 
"? ? ?
; ; ; i-2!&mF mF mF mF mF`	H H H
 
= 
= 
= '&(( ;R+:QRV:W:W W W W W LN  M M M M '&(( , , ,& & &
   $  
/ / /*A A A AH  	!!1 1 1 " " "J J JZ$ $ $N
 
 
*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   <
 
 
  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+       "    . 
 
 
 
N N N N     " " " ", , , , , ,s%   E   E*)E*.A/G G&%G&