
    Bj?                   $   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/m0Z0m1Z1m2Z2 dd
l3m4Z4m5Z5 ddl6Z6ddl7Z6ddl8Z6ddl9m:c m;Z< ddl6m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZD ddlEmFZFmGZG ddlHmIZImJZJmKZK ddlLmMZM ddlNmOZOmPZPmQZQmRZRmSZS ddlTmUZUmVZV ddlWmXZX ddlYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z`maZa ddlbmcZc ddldmeZe ejf        r>ddlgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZq ddlrmsZs ddltmuZu ddlvmwZwmxZx ddlymzZz dd l{m|Z| dd!l}m~Z~ 	 ddlZn# e$ r dZY nw xY w	 ddlZ6ddlmZ dd"lHmZ dd#lmZ dd$lmZ erCeej        ej        ej        fZd%ed&<   eeej        ej        ej        ej        ej        ej        iZnd'Zi Zdd(lmZmZmZ n# e$ r Y nw xY w e2d)          Z e2d*          Z e2d+          Z e2d,          Z e2d-          Z e2d.          Z e2d/          Z e2d0          Z e2d1          Z e2d2          Z e2d3          Z e2d4          Z e4d5          Ze6j        j        j        Ze6j        j        j        Ze6j        j        j        Z ej        ej!                  Zd6ed7<   i Zd8ed9<   d:Zd;Zd<e d=Z ej        e          Zi Zd>ed?<    ej        e          Zd@edA<    ej                    Z G dB dCe
j                  Z G dD dE          ZddLZdaddNZddOZdadPedQ<   ddTZddUZddVZdaÐddYZĐddZZŐdd[Z ej                    ZȐdd]Zɐdd_Z G d` dae
j                  Z G db dc          Z ej                    Ze$ddf            Ze$	 	 	 	 	 	 	 	 dddv            Ze/dddz            Ze/	 ddd}            Z	 dddZej        dd            Ze6j        e6j        e6j        fe6j        e6j        e6j        fe6j        e6j        e6j        fe6j        e6j        fe6j        e6j        fe6j        e6j        fe6j        e6j        e6j        fe6j        e6j        e6j        fe6j        e6j        e6j        fe6j        e6j        fi
Z G d d          Z e            ZddZddZ ej        dej                  Z G d dej                  ZddZddZddZddZddZddZddZddZ G d d          Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/dd            Ze/ddĄ            ZddńZ e             Zded<   ej        dk    r+ej        ej        ej4        ej2        ej        ej        fZdd΄Zej        dk    reej        fz  ZddфZ	dd҄Z
ddӄZe/ddք            Ze/dd؄            ZddلZej        ej        z  ej        z  ej        z  Zded<   dd݄Zdd߄Zej        ej        ej        ej        ej        ej        dZdddddddZddZddZddZddZddZ ddZ! ej&        d          dd            Z"dƐddZ#ddZ$ddZ%ddZ&dZ'ej(         G d d                       Z)dZ* ej+        e*          a,ded<   dʐdZ-dːdZ.dːdZ/dːd	Z0d̐dZ1d͐dZ2ddZ3dΐdZ4 G d d          Z5da6ded<   dϐdZ7dАdZ8e$	 	 dѐdҐd            Z9ej(         G d  d!                      Z: G d" d#e          Z; e;            e;_<        dӐd%Z=dԐd)Z>dՐd*Z?dd+d֐d.Z@e/dאd2            ZAe/dؐd5            ZAdِd6ZAdڐd7ZBe$dېd9            ZCdܐd<ZDddݐd>ZEdސd?ZFdÐd@ZGdߐdBZH ej&        d          ddE            ZIddHZJ	 dddLZKddMZL e&dN          ddQ            ZMeeeNeeOeP eQd          eRjS        eTjS        ej'        e6jU        e6jV        e6jW        e6jX        e6jY        e6jZ        e6j        j[        j\        e6j]        j^        e6j:        j;        j_        e6j:        j;        j`        e6j:        j;        ja        hZbdRedS<    ea            r*ddlcZcebd                    ecje        jV                   	 ddTZfejg        ddV            ZhddXZiddZZjdd\Zkdd]Zldd_Zmdd`ZnddaZoddbZp eQi q                                          Zrdcedd<    eQi s                                          Ztdeedf<    eQi u                                          Zvdgedh<    eQ e"            s                                          Zwdeedi<    eQ exd'                    Zydjedk<    eQ ex ezd                              Z{djedl<   eyj|        Z}e~j        Zej        Zdm  ej        ej        s                                e"j        s                                          D             Zdn ej        s                                D             Zdo ej        s                                D             Ze j        Zdp e j        s                                D             Zdq ej        s                                D             Zej        Zdr eOj        s                                D             Zds  eQe
j                  j        s                                D             Z e2dt          Z e2du          ZddyZdd{Zdd}Zdd~ZddZddZddZeZddZddZddZej        Ze6j=        j        dd            ZddZddZddZddZddZ	 	 dddZ	 dddZddZddZd dZddZdZddl}mZ ddZddZdgdddZddZddZddńZdddǄZddgddgddgdgej        dgdgdgdfd	d؄Zd
dلZej$        dېdڄ            Z e            Z ej        e          Zded<   g Zded<    e            Zejg        dd            ZddZdddZddZddZÐddZĐddZ	 dddZ	 dddZ ej                    ZȐddZe$dd            ZʐddZːddZ̐ddZ͐ddZΐddZϐdd ZАddZѐddZҐddZ	 dddZ G d de
j                  ZՐdd
Z֐d dZאd!dZؐd"dZg dZڐddgZg dZܐeڐez   ez   ZݐdАdZ	 	 	 d#d$dZ	 	 	 d#d%dZd&d Zd&d!Z G d" d#e,eef                   Zd'd$Z G d% d&          Z G d' d(e,eef                   Zd(d)Zd)d-ZdÐd.ZdÐd/Zd*d1Zd*d2Zd+d5Zd,d7Zej(         G d8 d9                      Zd-d<Zd.d@Zd/dAZddBZddCZd0dDZddEZd1dFZd2dGZddHZd3dJZej$        	 d4d5dN            Zd6dPZd7dQZ G dR dSe6j        j                  Zd8dVZd9dXZ d:dZZ G d[ d\          Z e            Zd]ed^<   dd;d`Zd<dbZej$        dېdc            ZdАddZd=deZd>dgZ	d?diZ
ddjZd@dnZdŐdoZdAdpZdBdrZe6j        dCdt            Ze6j        dCdu            Ze6j        dDdv            ZdEdwZdFdyZdGd{Z G d| d}          Z G d~ de          ZdHdZdZdDed<   ddZe$dېd            Ze$dېd            ZdIdZdgadАdZ dJdZ!e6j        dKd            Z"e6j        dLd            Z#dMdZ$dNdZ%dOdZ&dS (P  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castClassVarGenericLiteralNoReturnoverload	TypeAlias	TypeGuardTypeVar)	ParamSpec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)
OrderedSet)is_traceable_wrapper_subclass)
has_tritonhas_triton_package)RemovableHandle   )_get_flat_args)
Callable	Container	Generator	ItemsViewIterableIteratorKeysViewMappingSequence
ValuesView)Instruction)ExecutionRecord)InstructionTranslatorInstructionTranslatorBaseVariableTracker)DeviceLikeType)FakeTensorMode)detect_fake_mode
LazyStringconfigztuple[types.ModuleType, ...]NP_SUPPORTED_MODULES )
FakeTensoris_fakemaybe_get_fake_modeTT1T2T3T4T5T6T7T8T9T10R_Pz*collections.defaultdict[str, Counter[str]]countersdict[str, Any]optimus_scuba_logzihttps://docs.pytorch.org/docs/main/user_guide/torch_compiler/compile/programming_model.recompilation.htmlzZhttps://docs.pytorch.org/docs/main/user_guide/torch_compiler/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 )ReInplaceTriggerr2         N)__name__
__module____qualname__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPSrL       V/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/torch/_dynamo/utils.pyrd   rd      s        LLJJJrm   rd   c                      e Zd ZU  ej        e          Zded<   edd	            Z	edd            Z
edd            Zedd            Zedd            Zedd            ZdS )ReinplaceCountersz!collections.defaultdict[str, int]_valuestriggerrd   bytesintreturnNonec                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_bytes_rq   name)clsrr   rs   s      rn   add_missed_bytesz"ReinplaceCounters.add_missed_bytes   sA    A::K6667775@77777 :rm   countc                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_tensors_rz   )r|   rr   r~   s      rn   add_missed_opportunitiesz*ReinplaceCounters.add_missed_opportunities   sA    A::K8',88999UB99999 :rm   c                8    | j                                          d S N)rq   clearr|   s    rn   r   zReinplaceCounters.clear   s    rm   c                j    d}t           D ](}|| j                            d|j         d          z  })|S r   rd   rq   getr{   r|   sumrr   s      rn   get_total_missedz"ReinplaceCounters.get_total_missed   sD    ' 	H 	HG3;??#CW\#C#CQGGGCC
rm   c                j    d}t           D ](}|| j                            d|j         d          z  })|S rx   r   r   s      rn   get_total_missed_bytesz(ReinplaceCounters.get_total_missed_bytes   sD    ' 	F 	FG3;??#A7<#A#A1EEECC
rm   c                D    | j         rt          dd| j                    d S d S )Ninductorreinplace_counters)rq   r(   r   s    rn   logzReinplaceCounters.log   s6     ; 	J:';S[IIIII	J 	Jrm   N)rr   rd   rs   rt   ru   rv   )rr   rd   r~   rt   ru   rv   ru   rv   ru   rt   )rg   rh   ri   collectionsdefaultdictrt   rq   __annotations__classmethodr}   r   r   r   r   r   rL   rm   rn   rp   rp      s        1H1H1M1MGMMMM A A A [A
 C C C [C    [    [    [ J J J [J J Jrm   rp   rows'list[tuple[str, Any]] | list[list[Any]]headerstuple[str, ...] | list[str]ru   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     rn   	<genexpr>ztabulate.<locals>.<genexpr>   sG       
 
),DIIc#smm$$
 
 
 
 
 
rm   )tabulateImportErrorr   	itertoolschain)r   r   r   s      rn   r   r      s    
  w 777 
 
 
yy 
 
09	40P0P
 
 
 
 
 	
 	
 	

s    =AArv   c                     t           dz   a d S Nr2   )
curr_framerL   rm   rn   increment_framer   	  s    aJJJrm   c                 n    t                                            t                                           dad S Nr   )rb   r   r`   r   rL   rm   rn   reset_frame_countr     s/    ""$$$""$$$JJJrm   list[Callable[[], str]] | None_recompile_user_contextshookCallable[[], str]c                L    t           g a t                               |            dS )aj  
    Register a hook to be called when a recompile is triggered. The hook
    should return a string describing user contexts that are not available
    to the compiler, such as the current training epoch. This is useful for
    debugging and data analysis for recompile. For data retention purposes,
    the user context string is capped at 256 characters.
    N)r   append)r   s    rn   (register_hook_for_recompile_user_contextr     s*      '#% ##D)))))rm   c                     t           S r   r   rL   rm   rn   #get_hook_for_recompile_user_contextr   '  s    ##rm   c                 
    da dS )z@Clear any registered recompile user-context hooks (test helper).Nr   rL   rm   rn   reset_recompile_user_contextsr   +  s      $rm   cntrt   c                    t           | z  a d S r   )op_count)r   s    rn   increment_op_countr   4  s    OHHHrm   c                     i } t                                           D ]\  }}|dz  | |<   |                     dd          |                     dd          z   | d<   | S )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rb   itemsr   )total_by_keyphasetimings      rn   calculate_time_spentr   ;  sx    L17799 + +v$slU&2&6&6' '2A66'7L"# rm   c            	         t                      } d}|                                 D ]\  }}| d| dt          |d           }t          |           d S )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       rn   print_time_reportr   K  se    '))L
C"((** / /
U..s..U5!__..	#JJJJJrm   r   c                     t          t          d          st          t                    t          _        t          j        S )Nmetrics_contexton_exit)hasattr_metrics_context_tlsr   record_compilation_metricsr   rL   rm   rn   get_metrics_contextr   m  s<    '):;; 
/=.0
 0
 0
,  //rm   r   c                     t          t          d          st          t                    t          _        t          j        S )Nruntime_metrics_contextr   )r   r   r   r   r   rL   rm   rn   get_runtime_metrics_contextr   u  s<    ')BCC 
7L.8
 8
 8
4  77rm   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
    r2   re   rf   N)rg   rh   ri   __doc__CHROMIUMPT2_COMPILECOMPILATION_METRICrL   rm   rn   r   r   }  s,          HKrm   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d0d            Zeej        fd1d            Zed2d            Zed2d            Zed2d            Zed+d3d             Ze	 d4d5d!            Zed2d"            Zed6d)            ZdS )7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   metadatar^   time_ns
int | None	log_levelr   ru   rv   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         rn   r   z$CompileEventLogger.log_instant_event  s     ?lnnG022,555$)!!.:::$(!!|   	&&+@	
 	
 	
 	
 	
rm   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_eventr   s           rn   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?????rm   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       rn   add_toplevelzCompileEventLogger.add_toplevel  s[     .//CCEE	i   	#Iy)PPxPPPPPrm   r   r   rt   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          rn   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:::::rm   r2   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        rn   increment_toplevelz%CompileEventLogger.increment_toplevel  s]     122 4466	q   	$$Y	3FFFFFrm   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          rn   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;;;;;rm   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        rn   add_to_set_toplevelz&CompileEventLogger.add_to_set_toplevelE  s]     122 4466	q   	%%iCGGGGGrm   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     rn   chromiumzCompileEventLogger.chromiumY  sA     	#,5	
 	
AF	
JR	
 	
 	
 	
 	
rm   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     rn   pt2_compilezCompileEventLogger.pt2_compilec  sA     	#,8	
 	
DI	
MU	
 	
 	
 	
 	
rm   c                &   t           j        j        j        rx|rxd                    d |                                D                       }t           j        j                            |  d|           5  	 ddd           dS # 1 swxY w Y   dS dS dS )aM  
        Add record function data to the profiler event.

        This emits profiler event data so compilation events show up in stack profilers
        like the PyTorch profiler.

        Args:
            event_name: Name of the event to record
            **metadata: Additional metadata to attach to the record function
        r   c              3  *   K   | ]\  }}| d | V  dS )=NrL   r   kvs      rn   r   z>CompileEventLogger.add_record_function_data.<locals>.<genexpr>}  s0      $M$MDAqZZAZZ$M$M$M$M$M$Mrm   z_data: N)torchautogradprofiler_is_profiler_enabledr   r   record_function)r   r   metadata_strs      rn   add_record_function_dataz+CompileEventLogger.add_record_function_datap  s     >"7 	H 	99$M$MHNN<L<L$M$M$MMML(8844l44                     	 	 	 	s   3BB	B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     rn   compilation_metricz%CompileEventLogger.compilation_metric  s8     	' 3Y	
 	
BJ	
 	
 	
 	
 	
rm   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      rn   instantzCompileEventLogger.instant  s3     	,,'+?+H	
 	
 	
 	
 	
rm   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      rn   try_add_pt2_compilez&CompileEventLogger.try_add_pt2_compile  sC     )** 	F022''
??h?????rm   	method_fnCallable[_P, Any]args_P.argskwargs	_P.kwargsc                ~    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   )r*  r,  r.  r   s       rn   try_zCompileEventLogger.try_  sV    
 )** 	F-//**,, 	F	4"6"""""rm   )
r   r   r   r^   r   r   r   r   ru   rv   F)
r   r   r   r   r   r   r   r   ru   rv   )r   r   r   r   r   r   ru   rv   )
r   r   r   r   r   r   r   rt   ru   rv   )r   r   r   rt   r   r   ru   rv   )
r   r   r   r   r   r   r   r   ru   rv   )r   r   r   r   r   r   ru   rv   )r   r   r   r   ru   rv   )r   r   r   r   ru   rv   r   )r   r   r   r^   r   r   ru   rv   )r*  r+  r,  r-  r.  r/  ru   rv   )rg   rh   ri   r   staticmethodr   r   r   r  r  r  r   r  r  r  r  r  r!  r#  r%  r)  r1  rL   rm   rn   r   r     sC        0  #*>*G	
 
 
 
 \
*   +@ +@ +@ +@ \+@Z ;@Q Q Q Q \Q ; ; ; \;<  *>*QG G G G \G  < < < \<<  +?*QH H H H \H& 
 
 
 \
 

 

 

 \

    \$ 	
 	
 	
 	
 \	
 IM	
 	
 	
 	
 \	
 	@ 	@ 	@ \	@ 	# 	# 	# \	# 	# 	#rm   r   r{   Generator[Any, None, None]c              #  "  K   t           j        j        j        rrt           j        j                            |           }|                                 	 dV  |                    ddd           dS # |                    ddd           w xY wdV  dS )a  
    A context manager for compile-time profiling that uses _RecordFunctionFast
    for lower overhead than torch.profiler.record_function.

    This is intended for use during compilation (dynamo, inductor, etc.) where
    we want profiling support but with minimal overhead. Moreover, we do not
    want the record_function call inside torch.compile to be dispatched.

    Args:
        name: The name of the record function event that will appear in profiles.
    N)	r  r  r  r  _C	_profiler_RecordFunctionFast	__enter____exit__)r{   rfs     rn   compile_time_record_functionr<    s       ~3 X33D99
	*EEEKKdD)))))BKKdD))))s   A/ /BFr   
phase_name
str | Noner   r   r   dict[str, object] | Nonedynamo_compile_column_us
compile_idCompileId | Noneis_backwardbool | Nonelog_waitcounterwaitcounter_name_overridec	           
   #  .	  K   |r|}	| }
n| }	d}
| t           vr
g t           | <   t           |          }i }|r|                    |           |
r|                    d|
i           ||                    d|i           t                      }t          j                    }|                    |	||||           t          |  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
  }|                    |dz             |                    |	|i |||           |rt,          |	xx         |z  cc<   t           xj        d	z  c_        t           j        dk    }|dz  }|rVt/                      }|                                r0|                    ||           |r|                    d|           dS dS dS t5                      }|                    ||           |r"|d| d}|                    d||           dS dS dS # t          j                    }||z
  }|                    |dz             |                    |	|i |||           |rt,          |	xx         |z  cc<   t           xj        d	z  c_        t           j        dk    }|dz  }|rSt/                      }|                                r/|                    ||           |r|                    d|           w w w t5                      }|                    ||           |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_namerC  z (dynamo_timed)zpytorch.wait_counter._usdepthr   r2   z#pytorch.wait_counter.dynamo_compilez.pytorch.wait_counter.compile_runtime_overheadsr     duration_usT)rA  
is_runtime
is_forward)r`   r   r   r   r   log_event_startr<  r   r+   guardr  _guardsCompileContextcurrent_compile_idendswithr   _dynamo_timed_tlsrJ  
contextlib	ExitStackenter_contextlog_event_endrb   r   r   r  r   )r   r=  r   r   r@  rA  rC  rE  rF  r   rH  metricsevent_metadatar   start_nscx_mgrswc_nameis_compile_time
runtime_wcstackcxend_nstime_spent_nsis_outer_eventrL  r   runtime_contextextras                               rn   dynamo_timedrh    s     l  


***(* %&s+GN (h''' 4y'2333}k:;;;(A(C(CL|~~H  Hn.CZ   ">>U>U>U!V!V WG 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*+++""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*+++""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   6M, 	H4(M, 4H88M, ;H8<M, ,D(RreprLiteral['str']	aggregatec                    d S r   rL   ri  rk  s     rn   compile_timesrn  `  s    ILrm   Literal['csv']tuple[list[str], list[object]]c                    d S r   rL   rm  s     rn   rn  rn  d  s	     &)Srm   (str | None | tuple[list[str], list[str]]c                0   t           fd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.
    valueslist[float]item_fnCallable[[float], str]ru   r   c                ~    r |t          |                     S d                    t          ||                     S )Nr   )r   r   r   )rt  rv  rk  s     rn   fmt_fnzcompile_times.<locals>.fmt_fn{  s=     	(73v;;'''yyWf--...rm   c                F    g | ]}| t           |         d            fS )c                
    | dS )Nz.4frL   xs    rn   <lambda>z*compile_times.<locals>.<listcomp>.<lambda>  s
    jj rm   rv  )r`   )r   r  ry  s     rn   
<listcomp>z!compile_times.<locals>.<listcomp>  sG     
 
 
 /2<P<PQQQR
 
 
rm   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                ,    g | ]} |d            S )c                
    | dS )Nz.6frL   r|  s    rn   r~  z*compile_times.<locals>.<listcomp>.<lambda>  s
    1

 rm   r  rL   )r   r  ry  s     rn   r  z!compile_times.<locals>.<listcomp>  s<     
 
 
 F122333
 
 
rm   N)rt  ru  rv  rw  ru   r   )r   r`   r   rt  listkeys)ri  rk  r   r   rt  r   ry  s    `    @rn   rn  rn  k  s      GJ / / / / / / /
 u}}
 
 
 
-
 
 
 3x&BCCCC
	
 
 
 
-4466
 
 
 /4466774rm   c                 X    t                               t          dd                     d S )Nr   Trm  )r   inforn  rL   rm   rn   dump_compile_timesr    s'    HH]66677777rm   c                  (    e Zd ZdddZddZddZdS )DuplicateWarningChecker   maxsizert   ru   rv   c                <    || _         |                                  d S r   )r  reset)selfr  s     rn   __init__z DuplicateWarningChecker.__init__  s    

rm   c                ,    t                      | _        d S r   )r   setr  s    rn   r  zDuplicateWarningChecker.reset  s    *5--rm   r   str | tuple[object, object]r   c                ,   || j         v r+| j                             |d           t          j        sdS n_d | j         |<   t	          | j                   | j        k    r8| j                             d           t	          | j                   | j        k    8dS )NT)lastF)r  move_to_endrJ   verboselenr  popitemr  r   s     rn   addzDuplicateWarningChecker.add  s    $(??H  4 000> u !DHSMdh--$,..  e ,,, dh--$,..trm   N)r  )r  rt   ru   rv   r   )r   r  ru   r   )rg   rh   ri   r  r  r  rL   rm   rn   r  r    sU            8 8 8 8	 	 	 	 	 	rm   r  contextlib.ExitStackc                     t           j                            dd          dk    } | rt                      S t	          j                    S )NTORCH_COMPILE_DEBUG01)osenvironr   add_file_handlerrV  rW  )compile_debugs    rn   setup_compile_debugr    s@    JNN#8#>>#EM "!!!!!!rm   c                 8    t                                            d S r   )graph_break_dup_warning_checkerr  rL   rm   rn   reset_graph_break_dup_checkerr    s    #))+++++rm   z
    \x1B            # ESC
    \[              # [
    [0-?]*          # Parameter bytes
    [ -/]*          # Intermediate bytes
    [@-~]           # Final byte
    c                  $     e Zd ZdZd fdZ xZS )StripAnsiFormatterz0Logging formatter that strips ANSI escape codes.recordlogging.LogRecordru   r   c                |    t                                          |          }t                              d|          S )N )superformatANSI_ESCAPE_PATTERNsub)r  r  msg	__class__s      rn   r  zStripAnsiFormatter.format  s.    ggnnV$$"&&r3///rm   )r  r  ru   r   )rg   rh   ri   r   r  __classcell__r  s   @rn   r  r    sC        ::0 0 0 0 0 0 0 0 0 0rm   r  c                    t           j                            t                      d          } t          j        | d           t          j        t           j                            | d                                        t          d                     t          j	        d          
                               t          j                    }|                    fd           |S )NtorchdynamoTexist_okz	debug.logz%(message)sztorch._dynamoc                 .                                    S r   removeHandlerlog_file_handlerloggers   rn   r~  z"add_file_handler.<locals>.<lambda>  s    v334DEE rm   )r  pathr   get_debug_dirmakedirsloggingFileHandlersetFormatterr  	getLogger
addHandlerrV  rW  callback)log_path	exitstackr  r  s     @@rn   r  r    s    w||MOO];;HK4((((*27<<++N+NOO!!"4]"C"CDDD//F
&'''$&&IEEEEEFFFrm   c                 0   t          j                    } t          j        tt	          j        t          j                  t          j        j        	                                D ]0
                               |                     fd           1| S | S )Nc                 .                                    S r   r  r  s   rn   r~  z setup_log_file.<locals>.<lambda>  s    v';';<L'M'M rm   )rV  rW  rJ   log_file_namer  r  r  _logging	_internalget_loggersr  r  )r  r  r  s    @@rn   setup_log_filer    s    $&&I'".v/CDDn.::<< 	O 	OF.///MMMMMNNNNrm   exc	Exceptioncoder
   c                n    t                       d|j         dt          |           j         d|j         dS )Nz/error_recordings/_z.rec)r  co_nametyperg   co_firstlineno)r  r  s     rn   gen_record_file_namer    sR    oo ? ?? ?S		"? ?%)%8? ? ? ?rm   filenameexec_recordr?   c                   	 t           j                            |           rt                              d|            d S t          j        t           j                            |           d           t          | d          5 }|                    |           d d d            d S # 1 swxY w Y   d S # t          $ r t          
                    d|            Y d S w xY w)Nz9Unable to write execution record %s; file already exists.Tr  wbz#Unable to write execution record %s)r  r  existsr   warningr  dirnameopendumpr  	exception)r  r  fs      rn   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   callr2   )nodesop)r  cns      rn   count_callsr    s2    	AW  QT>>FAHrm   r}  c                    | S r   rL   r|  s    rn   identityr    s    Hrm   r   c                `    	 t          |            dS # t          $ r Y dS t          $ r Y dS w xY wNTF)hash	TypeError
ValueErrorr|  s    rn   hashabler    sR    Qt   uu   uus    
-	--r,  r.  c                     d S r   rL   r,  r.  s     rn   nothingr  "  s    Drm   c                  L    e Zd ZdZddZddZddd
ZddZddZddZ	ddZ
dS )ExactWeakKeyDictionaryz\Similar to weakref.WeakKeyDictionary, but use `is`/`id` rather than `==` to compare equalityru   rv   c                "    i | _         i | _        d S r   rt  refsr  s    rn   r  zExactWeakKeyDictionary.__init__)  s    &(;=			rm   r   r   c                6    | j         t          |                   S r   )rt  idr  s     rn   __getitem__z"ExactWeakKeyDictionary.__getitem__-  s    {2c77##rm   Ndefaultc                R    | j                             t          |          |          S r   )rt  r   r  )r  r   r  s      rn   r   zExactWeakKeyDictionary.get0  s    {r#ww000rm   r   c                .    t          |          | j        v S r   )r  rt  r  s     rn   __contains__z#ExactWeakKeyDictionary.__contains__3  s    #ww$+%%rm   r   c                     t          |           j        vr!t          j        | fd           j        <   | j        <   d S )Nc                .                                   S r   )
_remove_id)refidxr  s    rn   r~  z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>9  s    $//#:N:N rm   )r  r  weakrefr  rt  )r  r   r   r  s   `  @rn   __setitem__z"ExactWeakKeyDictionary.__setitem__6  sR    ggdi$[.N.N.N.N.NOODIcN Crm   r  rt   c                N    || j         v r| j         |= || j        v r
| j        |= d S d S r   r  )r  r  s     rn   r  z!ExactWeakKeyDictionary._remove_id<  s;    $+C $)	# rm   c                j    | j                                          | j                                         d S r   )r  r   rt  r  s    rn   r   zExactWeakKeyDictionary.clearB  s.    	rm   r   )r   r   ru   r   r   )r   r   r  r   ru   r   )r   r   ru   r   )r   r   r   r   ru   rv   r  rt   ru   rv   )rg   rh   ri   r   r  r  r   r	  r  r  r   rL   rm   rn   r  r  &  s        ff> > > >$ $ $ $1 1 1 1 1& & & &! ! ! !        rm   r  objr   allowed_typestype[T]	TypeIs[T]c                    d S r   rL   r  r  s     rn   istyper  G  s    >Acrm   )tuple[type[list[T]], type[tuple[T, ...]]]c                    d S r   rL   r  s     rn   r  r  K  s	     rm   tuple[type[T1], type[T2]]TypeIs[T1 | T2]c                    d S r   rL   r  s     rn   r  r  R  s	     crm   #tuple[type[T1], type[T2], type[T3]]TypeIs[T1 | T2 | T3]c                    d S r   rL   r  s     rn   r  r  X  s	     3rm   -tuple[type[T1], type[T2], type[T3], type[T4]]TypeIs[T1 | T2 | T3 | T4]c                    d S r   rL   r  s     rn   r  r  ^  s	     !$rm   7tuple[type[T1], type[T2], type[T3], type[T4], type[T5]]TypeIs[T1 | T2 | T3 | T4 | T5]c                    d S r   rL   r  s     rn   r  r  d  s	     &)Srm   Atuple[type[T1], type[T2], type[T3], type[T4], type[T5], type[T6]]#TypeIs[T1 | T2 | T3 | T4 | T5 | T6]c                    d S r   rL   r  s     rn   r  r  j  s	     +.#rm   Ktuple[type[T1], type[T2], type[T3], type[T4], type[T5], type[T6], type[T7]](TypeIs[T1 | T2 | T3 | T4 | T5 | T6 | T7]c                    d S r   rL   r  s     rn   r  r  q  s	     03srm   Utuple[type[T1], type[T2], type[T3], type[T4], type[T5], type[T6], type[T7], type[T8]]-TypeIs[T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8]c                    d S r   rL   r  s     rn   r  r  z  s	     58Crm   _tuple[type[T1], type[T2], type[T3], type[T4], type[T5], type[T6], type[T7], type[T8], type[T9]]2TypeIs[T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9]c                    d S r   rL   r  s     rn   r  r    s	     :=rm   jtuple[type[T1], type[T2], type[T3], type[T4], type[T5], type[T6], type[T7], type[T8], type[T9], type[T10]]8TypeIs[T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10]c                    d S r   rL   r  s     rn   r  r    s     @Csrm   )tuple[type, ...] | list[type] | set[type]c                    d S r   rL   r  s     rn   r  r    s	     3rm   c                    t          |t          t          t          f          rt	          |           |v S t	          |           |u S )zisinstance() without subclasses)
isinstancetupler  r  r  r  s     rn   r  r    s=    -%s!344 *CyyM))99%%rm   ztuple[Any, ...]_builtin_final_typing_classesrf      inputs collections.abc.Sequence[object]modeltorch.fx.GraphModulelist[torch.device | None]c                    t          j        |           d         d t          |j        j                  D             z   }t          t          d |D                                 }d |D             d gz   S )Nr   c                :    g | ]}d |j         v |j         d          S )valmeta)r   nodes     rn   r  z&get_inputs_devices.<locals>.<listcomp>  s2     3 3 3!@R@R	%@R@R@Rrm   c                <    g | ]}t          |d           |j        S device)r   rM  r   is     rn   r  z&get_inputs_devices.<locals>.<listcomp>  s)    IIIGAx4H4HIAHIIIrm   c                \    g | ])}t          |t          j                  r|j        d k    '|*S rH  )r;  r  rM  r  rN  s     rn   r  z&get_inputs_devices.<locals>.<listcomp>  s?       z!U\::?@v?O?O?O?O?Orm   )pytreetree_flattenr  graphr  r-   )r@  rB  
all_inputsdevicess       rn   get_inputs_devicesrV    s     $V,,Q/ 3 3%)%+*;%<%<3 3 3 J *.IIjIIIJJ* *G   	 rm   )rf      r   c                    t           j        dk    rt          | t                    rdS t          | t          j        t          j        f          p| t          j        u p| t          j	        u S )Nr>  T)
sysversion_infor;  r=  types	UnionTypetyping_Finalr   Unionr   s    rn   	is_typingra    s`     7""z%9V'W'W"t55?FM:;; 	!FN"	!FL rm   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    rn   is_numpy_int_typerm    sQ     uGHHHHIII		
  rm   c                v    t           sdS t          | t           j        t           j        t           j        f          S rc  )rd  r  float16float32float64r`  s    rn   is_numpy_float_typerr    s8     uJJJ	
  rm   Callable[..., T]*TypeGuard[functools._lru_cache_wrapper[T]]c                    d S r   rL   r`  s    rn   is_lru_cache_wrapped_functionrv  	  s	     25rm   ,TypeGuard[functools._lru_cache_wrapper[Any]]c                    d S r   rL   r`  s    rn   rv  rv    s	     473rm   c                z    t          | t          j                  o!t          t	          j        | d                    S )N__wrapped__)r;  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr`  s    rn   rv  rv    s<     eY9:: {um44@ @ rm   r   
_FuncTypesHTypeIs[_FuncTypes | torch._ops.OpOverloadPacket | torch._ops.OpOverload]c                    t          |           p/t          | t          j        j        t          j        j        f          S r   )r}  r;  r  _opsOpOverloadPacket
OpOverloadr`  s    rn   is_function_or_wrapperr  %  s9     u 
+UZ-BC" " rm   TypeIs[_FuncTypes]c                z    t          | t          j        t          j        t          j        t          j        f          S r   )r;  r[  FunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper`  s    rn   r}  r}  -  s6     %&'		
  rm   )__eq____ne____lt____le____gt____ge__z==z!=<z<=>z>=TypeIs[types.GetSetDescriptorType | types.MethodDescriptorType | types.WrapperDescriptorType | types.MemberDescriptorType | types.MethodWrapperType]c                    t          | t          j        t          j        t          j        t          j        t          j        f          S r   )r;  r[  GetSetDescriptorTyper  r  MemberDescriptorTyper   r`  s    rn   is_wrapper_or_member_descriptorr  O  s>      &&'&#	
  rm   fnc                ,    t          |           d         S r   ) unwrap_with_attr_name_if_wrapper)r  s    rn   unwrap_if_wrapperr  i  s    +B//22rm   tuple[Any, str | None]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     rn   r  r  m  sT     2 71"6KUSS #B(=rBB)			y=rm   TypeGuard[np.ndarray]c                H    t           sdS t          | t           j                  S rc  )rd  r  ndarrayr`  s    rn   is_numpy_ndarrayr  x  s!     u%$$$rm   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	ParameterrJ   traceable_tensor_subclasses_subclassesrM   r  )r  tensor_lists     rn   istensorr    sO     	% 
	+% %K
 !2!= ??K#{###rm   modc                ,    t          | t                    S r   )r;  r,   )r  s    rn   is_lazy_moduler    s    c?+++rm   r  c                     t          |   d S r   )r   r,  s    rn   
print_oncer    s    	4LLLLrm   rG  types.CellTypec                p    | d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 closureru   r   c                      S r   rL   r|  s   rn   r  zmake_cell.<locals>.f  s    rm   Nr2   r   )ru   r   )__closure__r  )rG  r  r}  s     @rn   	make_cellr    sW    A      =$Q]););q)@)@)@@=rm   &tuple[tuple[Any, ...], dict[str, Any]]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 r   as_proxyr   args     rn   r   z$proxy_args_kwargs.<locals>.<genexpr>  s*      ::c3<<>>::::::rm   c                >    i | ]\  }}||                                 S rL   r  )r   r   r  s      rn   
<dictcomp>z%proxy_args_kwargs.<locals>.<dictcomp>  s&    KKKSS\\^^KKKrm   r2   unimplemented)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  r  variables.baser  r  rt  )r,  r.  
proxy_argsproxy_kwargser  r  s          rn   proxy_args_kwargsr    s    
::T:::::
KKFLLNNKKK<'' 

 

 

&&&&&&++++++<]77D>]]GGT&--//EZEZ<[]]Q	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	

s   := 
BABBr  float | Noner   c                .    | d nt          | dz            S NrK  rt   r  s    rn   	to_int_msr    s    944#a$h--/rm   c                .    | d nt          | dz            S )Ni@B r  r  s    rn   	to_int_usr    s    944#a)m"4"44rm   rf   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d0<   dZ-ded1<   dZ.ded2<   dZ/ded3<   dZ0ded4<   dZ1ded5<   dZ2ded6<   dZ3ded7<   dZ4ded8<   dZ5ded9<   dZ6ded:<   dZ7ded;<   dZ8ded<<   dZ9ded=<   dZ:ded><   dZ;ded?<   dZ<ded@<   dZ=dedA<   dZ>dedB<   e?Z@dCedD<   dZAdedE<   dZBdedF<   dGZCdedH<   dGZDdedI<   dZEdedJ<   dZFdedK<   dZGdedL<   dGZHdedM<   dGZIdedN<   dGZJdedO<   dGZKdedP<   dGZLdedQ<   dGZMdedR<   dZNdedS<   dZOdedT<   dZPdUedV<   dZQdedW<   dXZRd#edY<   dZSdedZ<   dZTd#ed[<   dZUd#ed\<   dZVded]<   dZWded^<   dZXded_<   dZYded`<   dZZdeda<   dZ[dedb<   dZ\dedc<   dZ]dedd<   dZ^dede<   dZ_dedf<   dZ`dedg<   dZadedh<   dZbdedi<   dZcdedj<   dXZdd#edk<   dZededl<   dZfdedm<   egdrdq            ZhdS )sCompilationMetricsNr>  rA  	frame_keyr  co_filenamer   r  
cache_sizeaccumulated_cache_sizeguard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_countr  
start_timeentire_frame_compile_time_sbackend_compile_time_sinductor_compile_time_scode_gen_time_s	fail_typefail_reasonfail_user_frame_filenamefail_user_frame_linenozset[str] | Nonenon_compliant_opscompliant_custom_opsrestart_reasonsdynamo_time_before_restart_szlist[str] | Nonestack_traceexception_stack_tracegraph_node_shapesrD  has_guarded_coderemote_cache_time_saved_sstructured_logging_overhead_sconfig_suppress_errors config_inline_inbuilt_nn_modulesspecialize_floatdynamo_configcompiler_configrN  num_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usrL  !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_usrt   log_format_versioninductor_configremote_cache_versionr   "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!inductor_fx_local_cache_hit_count"inductor_fx_local_cache_miss_count"aotautograd_remote_cache_hit_count#aotautograd_remote_cache_miss_count!aotautograd_local_cache_hit_count"aotautograd_local_cache_miss_countcuda_versiontriton_versionzdict[str, bool] | Nonefeature_usagecompile_time_autotune_time_usFrM  
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recompile_user_contexts#inline_inbuilt_nn_modules_candidatepytorch_versioninductor_provenancerZ  r^   ru   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   ru   r  c                    | | dz  nd S )Ng    .ArL   r8  s    rn   us_to_sz*CompilationMetrics.create.<locals>.us_to_s5  s    #)#56C<<4?rm   c                    | | dz  nd S r  rL   r:  s    rn   us_to_msz+CompilationMetrics.create.<locals>.us_to_ms8  s    %+%76T>>TArm   
Any | Noner>  c                    d	d| d S t          | t          t          f          sdS d                    fdt	          |           D                       S )
Nitemr   ru   r   c                D    	 t          |           S # t          $ r Y dS w xY wN	<unknown>)r   r  )r@  s    rn   safe_strzFCompilationMetrics.create.<locals>.collection_to_str.<locals>.safe_str<  s5    't99$  ' ' '&;;'s    
rC  ,c              3  .   K   | ]} |          V  d S r   rL   )r   r@  rD  s     rn   r   zGCompilationMetrics.create.<locals>.collection_to_str.<locals>.<genexpr>H  s+      FFtHHTNNFFFFFFrm   )r@  r   ru   r   )r;  r  r  r   sorted)r8  rD  s    @rn   collection_to_strz4CompilationMetrics.create.<locals>.collection_to_str;  sk    ' ' ' ' ~tfsDk22 #"{88FFFFvf~~FFFFFFrm   c                p    | d S 	 t          j        t          |                     S # t          $ r Y dS w xY wrB  )jsondumpsr  r  r:  s    rn   collection_to_json_strz9CompilationMetrics.create.<locals>.collection_to_json_strJ  sI    ~t#z$v,,/// # # #"{{#s    ' 
55r  r   r  r  r  r  r
  r  r	  )	r  r  r  r  r  r  r  r  r  r  r  r*  rA  N)r8  r   ru   r  )r8  r   ru   r   )r8  r>  ru   r>  rL   )r   r   )	r|   rZ  r;  r=  rH  rL  legacy_metricsall_metricsrA  s	            rn   createzCompilationMetrics.create-  sa   	@ 	@ 	@ 	@	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!![!!!rm   )rZ  r^   ru   r  )irg   rh   ri   rA  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rN  r  r  r  r  rL  r   r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  r  r  LOG_FORMAT_VERSIONr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r"  rM  r#  r$  r%  r&  r'  r(  r)  r*  r+  r,  r-  r.  r/  r0  r1  r2  r3  r4  r5  r6  r   rO  rL   rm   rn   r  r    s        !J!!!! I    G"K""""!%N%%%%!J!!!!)-----"K""""(,,,,,!%N%%%%#'''''$(((((#J####044444+/////,00000$(O(((( I    "K""""+/////)-----)-----,00000'+O++++15 5555$(K((((.22222$((((( %)((((.2222226!6666*.....48$8888$((((( $M$$$$"&O&&&&"J""""%)))))48%888848%8888 $M$$$$"K""""48%8888:>+>>>>6:'::::?C0CCCC)-----/3 333326#66668<)<<<<37$777715"555548%888848%88886:'::::"K""""(,,,,,)-----+/////00000"&O&&&&'+++++56&666667'77778<)<<<<48%888859&999945%555556&666656&666667'777745%555556&6666#L####!%N%%%%,0M000004!4444#J####!J!!!!+/////+//////33333%)))))#'''''#'''''15"5555H(,,,,,!%N%%%%48%888848%8888 #K"""" #K"""" #K""""/333337<'<<<<"&O&&&&+/////R" R" R" [R" R" R"rm   r  @   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   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r  event_loggerr   s      rn   #add_compilation_metrics_to_chromiumrW    s    -..L1133J +	M'< 7M5'+-+M!"!; 7 *+)<)HD$%%%d -.,B,ND'(((TX ()'8'DD"###$%&%C+o;       rm   c                 n    dd}  | t          j                              }t          j        |d          S )Ndr^   ru   c                L    h dfd|                                  D             S )N>   r  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ignore_logging_functions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 rL   r;  r  rG  r   r   r   	blocklists      rn   r  zJ_get_dynamo_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>  M     
 
 
U)## *UC"8"8Ce###rm   r   rY  rp  s    @rn   clean_for_jsonz6_get_dynamo_config_for_logging.<locals>.clean_for_json  sG    
 
 
	0
 
 
 
ggii
 
 
 	
rm   T	sort_keysrY  r^   ru   r^   )rJ   get_config_copyrJ  rK  )rt  config_dicts     rn   _get_dynamo_config_for_loggingrz    sE    
 
 
 
> !.!7!9!9::K:kT2222rm   c                     dd} t           j        j        sd S 	 t           j        j                                        }n# t          t
          f$ r Y dS w xY w | |          }t          j        |d          S )	NrY  r^   ru   c                J    dhfd|                                  D             S )Nr^  c                l    i | ]0\  }}|v	|t          |t                    rt          |          n|1S rL   rn  ro  s      rn   r  zH_compiler_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>  rq  rm   rr  rs  s    @rn   rt  z4_compiler_config_for_logging.<locals>.clean_for_json  s@    
	
 
 
 
ggii
 
 
 	
rm   z!Compiler Config cannot be pickledTru  rw  )r  compilerrJ   rx  r  AttributeErrorrJ  rK  )rt  compiler_config_copyry  s      rn   _compiler_config_for_loggingr    s    	
 	
 	
 	
 >  t3$~4DDFF~& 3 3 32223 !.!566K:kT2222s   #= AAc                 R    G d dt           j                  } t                      }d}d}t          j        j        rJ	 t          j        j                                        }n%# t          t          t          t          f$ r d}Y nw xY w|	 |                                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d fdZ xZS )A_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializeror   ru   c                j    	 t                                          |          S # t          $ r Y dS w xY w)NzValue is not JSON serializable)r  r  r  )r  r  r  s     rn   r  zI_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializer.default  sB    8wwq))) 8 8 87778s    $ 
22)r  r   ru   r   )rg   rh   ri   r  r  r  s   @rn   TypeSafeSerializerr    s=        	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rm   r  Nz!Inductor Config cannot be pickledT)r|   skipkeysrv  z(Inductor Config is not JSON serializable)rJ  JSONEncoderr  r  	_inductorrJ   rx  r  r  r   AssertionErrorr   r;  r   r  r  rK  r  )r  keys_to_scrubinductor_conf_strinductor_config_copyr   rG  s         rn   %_scrubbed_inductor_config_for_loggingr    s   8 8 8 8 8T- 8 8 8 "eeM D	D#(?#9#I#I#K#K  ><H 	D 	D 	D C	D '	K06688 : :S!#s++ +!%%c***c3'' :04S		(-$ . .(-- $
$&	! ! !  	K 	K 	K J	K s$   #A! !BB	BD D$#D$start_time_nsend_time_nsrZ  exc_typetype[BaseException] | None	exc_valueBaseException | Nonec                ,   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	        
                                }i d|d| dz  d|dz  d|r|j        nd d	|rt          |          nd d
t          t           j                                                  dt!                      dt"          j        dddt'                      dt)                      dt           j        j        dt/                      rt0          j        ndd|d|dt4          j        dt           j        }	t6                              i |	|          t:                                         d}
j        du rd|
z   }
j         du r|
dz   }
t           j        !                    |
fdd|           tE                     t"          j#        rtI                     d S d S )Nr   )REMOTE_CACHE_VERSION_ManifoldCacherA  r  rK  r  r  r  r	  r  r  r  Tr  r  r  r   r  r  r  r-  r5  compilation_metricsFbwd__runtimec                 d    d t          j                                                   D             S )Nc                b    i | ],\  }}|t          |t                    rt          |          n|-S rL   )r;  r  r  r  s      rn   r  z@record_compilation_metrics.<locals>.<lambda>.<locals>.<dictcomp>b  sG     
 
 
1 *Q,,3tAwww!
 
 
rm   )dataclassesasdictr   )r  s   rn   r~  z,record_compilation_metrics.<locals>.<lambda>b  s9     
 
#*+>??EEGG
 
 
 rm   )record_logging_overheadrA  )%r  r  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cacher  ModuleNotFoundErrorr   rQ  rR  rS  ri   r   r  r  get_structured_logging_overheadrz  rJ   suppress_errorsr  r  versioncudar/   triton__version__rY  r  rO  rT  r   rN  rM  trace_structuredrW  rf  r&   )r  r  rZ  r  r  r  r  r  rA  common_metricsr{   r  s              @rn   r   r   '  s    ==?? $	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
j$. 	{d* 	h@X**D	
 	<s9~~~ 	))N::<<+
 +
 	799 	!&"8 	+D 	@BB 	799 	* 	
D&,,"  	 4!" 	01V#$ 	#+%& 	5,'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new_sizec                    t          t                    | k    r1t                                           t          t                    | k    1t          j        t          |           }|ad S )NrR  )r  rT  popleftr   deque)r  	new_deques     rn   set_compilation_metrics_limitr  y  s`    
"
#
#h
.
.$$&&& "
#
#h
.
.!"6xHHHI$rm   c                 8    t                                            d S r   )rT  r   rL   rm   rn   clear_compilation_metricsr    s         rm   list[CompilationMetrics]c                 *    t          t                    S r   )r  rT  rL   rm   rn   get_compilation_metricsr    s    $%%%rm   c                      e Zd ZdZd)dZd*dZd)dZd+d	Zd,dZd-dZ	d.dZ
d.dZd/dZd0dZ	 	 d1d2d!Zd-d"Z	 d3d4d$Z	 d3d5d'Z	 	 d6d7d(ZdS )8ChromiumEventLoggera\  Logs chromium events to structured logs. tlparse will concatenate these into a perfetto UI link.

    Also emits RecordFunction calls to torch.profiler when enabled.

    See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.yr4qxyxotyw for
    a specification of the Chromium Event JSON format.
    ru   	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.
        ra  )r   tlsra  r  s    rn   	get_stackzChromiumEventLogger.get_stack  s5    
 48W%% 	"8>!DHN8>!rm   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  )r  ra  s     rn   r   z'ChromiumEventLogger.get_outermost_event  s&    
    *uQxxd*rm   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   r  s    rn   r   z,ChromiumEventLogger.get_pt2_compile_substack  s9    
 48344 	1800,.DH)800rm   r^   c                \    t          | j        d          si | j        _        | j        j        S )N
event_data)r   r  r  r  s    rn   get_event_dataz"ChromiumEventLogger.get_event_data  s+    tx.. 	%"$DHx""rm   'dict[str, AbstractContextManager[None]]c                \    t          | j        d          si | j        _        | j        j        S )Nrecord_functions)r   r  r  r  s    rn   get_record_functionsz(ChromiumEventLogger.get_record_functions  s,    tx!344 	+(*DH%x((rm   rv   c                    t          j                    | _        ddlm} |j        r#|j         dt          j                     | _        n%t          t          j                              | _        t                              d| j                   d S )Nr2   rI   -z*ChromiumEventLogger initialized with id %s)	threadinglocalr  r  rJ   pt2_compile_id_prefixuuiduuid4id_r   r   r  )r  rJ   s     rn   r  zChromiumEventLogger.__init__  s    ?$$ ' 	) 6GGGGDHH4:<<((DH 	=txHHHHHrm   r   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   )r  r   r.  s      rn   r(  z&ChromiumEventLogger.try_add_event_data  s?     T^^----FJ11&11111rm   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   ri  r  r   )r  r   r.  r  s       rn   r   z"ChromiumEventLogger.add_event_data  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz":%%f-----rm   r   r   rt   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   ri  r  r  r   r   r   r  s        rn   r  zChromiumEventLogger.increment  s     T^^----Kj)) K K4>>3C3C K K K   ((**
Z''%'Jz"j,,,*+Jz"3':s###u,#####rm   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   ri  r  r  r  r  s        rn   r  zChromiumEventLogger.add_to_set  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz"j,,,*-%%Jz"3':s#''.....rm   FNr   r   r   r   rA  rB  c                Z   |p"t           j        j                                        }t	          |          |d<   |                     ||d|           |                                                     |            | j        |fi | |r'| 	                                                    |           t           j
        j        j        ret           j        j                            |          }|                                 ||                                 |<   |rt%          j        |fi | dS 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)
        rA  BN)r  rQ  rR  rS  r   _log_timed_eventr  r   r   r   r  r  r  r6  r7  r8  r9  r  r   r!  )r  r   r   r   r   rA  r;  s          rn   rO  z#ChromiumEventLogger.log_event_start  sF      T5=#?#R#R#T#T
!$Z		
 	
 	
 	
+++J33(333  	?))++22:>>> >"7 	T#77
CCBLLNNN68D%%''
3  T";JSS(SSSSS	T 	TT Trm   c                   |                                  }|                                 }|                                 |                                 |                                 }|                                 |                                 }|rD|                                D ]}|                    d d d            |                                 d S d S r   )r  r   r   r  r  rt  r:  )r  ra  substackr  r  r;  s         rn   r  zChromiumEventLogger.reset6  s       0022((**
4466 	%&--// . .D$----""$$$$$	% 	%rm   r  c                   |p"t           j        j                                        }t	          |          |d<   |                                 }|v r|         }|= ni }|                    |           |                     |d|          }	d
fd}
|                                 }|vrt          
                    d           d	S  |
|           |rJ|                                 } |
|           t          |	|| j        |           |                                 |                                 |                                 }|v r.|                              }|                    d	d	d	           d	S 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)
        rA  Era  r  ru   rv   c                    | d         k    r<t                               d           |                                  | d         k    :d S d S )Nz>ChromiumEventLogger: Detected overlapping events, fixing stack)r   r  pop)ra  r   s    rn   	pop_stackz4ChromiumEventLogger.log_event_end.<locals>.pop_stackn  sY    b	)) T   		 b	))))))rm   z7ChromiumEventLogger: Start event not in stack, ignoringN)ra  r  ru   rv   )r  rQ  rR  rS  r   r  r   r  r  r   r  r   r%   r  r  r  r:  )r  r   r   r   r  r   rA  all_event_datar[  eventr  event_stackr   r  r;  s    `             rn   rY  z!ChromiumEventLogger.log_event_endF  s   &  T5=#?#R#R#T#T
!$Z ,,..''+J7Nz**  Nh'''%%	
 
	 	 	 	 	 	 nn&& [(( KKQRRRF	+  	'#'#@#@#B#B I*+++'+TX}   !$$&&& 	  4466)))!%%j11BKKdD))))) *)rm   r   dict[str, Any] | Nonec                    ||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.
        rK  rh  r   )r{   tsr,  phcattidpidchromium_eventc                      S r   rL   r  s   rn   r~  z6ChromiumEventLogger._log_timed_event.<locals>.<lambda>      u rm   F
payload_fnsuppress_contextexpect_trace_id)r  r  r  r'   )r  r   r   r   r   r  s        @rn   r  z$ChromiumEventLogger._log_timed_event  sr     D.!	
 	
 	''$}}}"!	 	( 	
 	
 	
 	'u---rm   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
        NrA  rK  rO  rh  r   p)r{   r  r,  r  r  r  r  sr  c                      S r   rL   r  s   rn   r~  z7ChromiumEventLogger.log_instant_event.<locals>.<lambda>  r  rm   FTr  )
r   r  rQ  rR  rS  r  r  r%   r   r  )r  r   r   r   r   rA  r  s         @rn   r   z%ChromiumEventLogger.log_instant_event  s     H5HHJJKK
!+D.!

 

 	''$}}}" 	 	( 	
 	
 	
 ! 	't4466'    	 	rm   )ru   r  ru   r>  )ru   r^   )ru   r  r   )r   r   r.  r   ru   rv   )r   r   r   r   r   rt   ru   rv   )r   r   r   r   r   r   ru   rv   FN)r   r   r   rt   r   r^   r   r   rA  rB  ru   rv   r   )r   r   r   rt   r   r^   r  rt   r   r   rA  rB  ru   rv   )
r   r   r   rt   r   r   r   r  ru   r^   rc  )
r   r   r   rt   r   r  r   r   ru   rv   )rg   rh   ri   r   r  r   r   r  r  r  r(  r   r  r  rO  r  rY  r  r   rL   rm   rn   r  r    s        	" 	" 	" 	"+ + + +	1 	1 	1 	1# # # #
) ) ) )
I I I I2 2 2 2. . . .(- - - -$/ / / /6 ','+&T &T &T &T &TP% % % %. (,N* N* N* N* N*j +/    B +/&+( ( ( ( ( ( (rm   r  zChromiumEventLogger | NoneCHROMIUM_EVENT_LOGc                 :    t           t                      a t           S r   )r  r  rL   rm   rn   r   r     s    !022rm   c                     t           d uS r   )r  rL   rm   rn   r'  r'    s    T))rm   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   rO  rY  r  )r   r  r   chromium_event_logchromium_start_times        rn   chromium_event_timedr    s      344,..&&
	  '((LNN!	
 	
 	
 # 	'$$&&&&&	' 	' 	((LNN!	
 	
 	
 # 	'$$&&&&	's   B ACc                  H    e Zd ZU dZded<   ded<   dd
Zedd            ZdS )CleanupHookz,Remove a global variable when hook is calledr^   scoper   r{   r,  r   ru   rv   c                X    t           t           xj        dz  c_        | j        | j        = d S r   )CleanupManagerr~   r  r{   r  r,  s     rn   __call__zCleanupHook.__call__	  s.    %  A%  Jty!!!rm   rG  c                b    || vsJ t           xj        dz  c_        || |<   t          | |          S r   )r  r~   r  )r  r{   rG  s      rn   rO  zCleanupHook.create	  s@    5    !d5$'''rm   Nr,  r   ru   rv   )r  r^   r{   r   rG  r   ru   r  )rg   rh   ri   r   r   r  r3  rO  rL   rm   rn   r  r  	  se         66III" " " " ( ( ( \( ( (rm   r  c                  0     e Zd ZU dZded<   d	 fdZ xZS )
r  r   zClassVar[CleanupManager]instancer  rt   ru   rv   c                ~    | j         |         D ]} |             t                                          |           d S r   )rt  r  r  )r  r  r   r  s      rn   r  zCleanupManager._remove_id'	  sD    K$ 	 	DDFFFF3rm   r  )rg   rh   ri   r~   r   r  r  r  s   @rn   r  r  #	  sP         E&&&&                   rm   r  torch.Tensorc                    |                                                      | j                  }| j        r%| j        | j                                         |_        |S )z!Clone the tensor and its gradient)clonerequires_grad_requires_gradis_leafgrad)r}  ys     rn   clone_tensorr  0	  sF    			  11Ay  QV'Hrm   srcdstattrc                    t          | |          r3t          ||t          | |                                                     dS t          ||          rt	          ||           dS dS )z]Copy a single dynamo attribute from src to dst, or remove it from dst if src doesn't have it.N)r   setattrgetattrcopydelattr)r  r  r  s      rn   _copy_dynamo_attrr  8	  st    sD T73--224455555	d		 T rm   c                    t          | |d           t          | |d           t          | |d           t          | |d           t          | |d           t          | |d           dS )a
  
    Copy dynamo-specific tensor attributes from src to dst.
    These attributes are used for dynamic shape marking and must be preserved
    when cloning or casting tensors. If src doesn't have an attribute but dst does,
    the attribute is removed from dst.
    _dynamo_dynamic_indices_dynamo_unbacked_indices_dynamo_hint_overrides_dynamo_shape_ids_dynamo_strict_unbacked_indices_dynamo_weak_dynamic_indicesN)r  )r  r  s     rn   copy_dynamo_tensor_attributesr'  @	  s|     c3 9:::c3 :;;;c3 8999c3 3444c3 ABBBc3 >?????rm   dtyper)  torch.dtype | Nonec          	     X   t          |           r| S d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+          |           r ||           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&        tO          | j&                  |_&        n'# tP          $ r  ||           cY cddd           S w xY wtS          | |           |cddd           S # 1 swxY w Y   dS )zcopy while preserving stridesr}  r  ru   c                    t          j        |           }| j        r|                    | j                   | j        r"| j        t          | j                  |_        t          | |           |S )Nr(  )r  r  r  r  r  r  clone_inputr'  )r}  r  r)  s     rn   torch_clonez clone_input.<locals>.torch_cloneV	  sl    KNN9 	.Q_---9 	6+ u555AF%a+++rm   xlaN)is_coalesced)layoutc              3  ,   K   | ]\  }}|d z
  |z  V  dS )r2   NrL   )r   shapestrides      rn   r   zclone_input.<locals>.<genexpr>	  s@       
 
%2UFUQY& 
 
 
 
 
 
rm       )r)  rM  r(  r}  r  ru   r  )*rN   r  no_gradrM  r  r1  
sparse_coosparse_coo_tensor_indicesrq   r3  r0  r#   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorrt  r.   r   zipsizer4  is_quantizedempty_quantizedemptyr)  data_ptrelement_sizeas_strided_copy_r  r  r  r  r  r-  r   r'  )r}  r)  r.  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      rn   r-  r-  O	  sj    qzz       
 9 98=E!!;q>>9 9 9 9 9 9 9 9 8u'''*AJJLL))AIIKK((^^--	  9 9 9 9 9 9 9 9 "!$$ 	"xE,e.>???%&^^%5%5" !%&^^%5%5" !1.//M**AHHJJ''x  )9 9 9 9 9 9 9 96 +1-- 	" ;q>>=9 9 9 9 9 9 9 9@  
 
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>>!!o9 9 9 9 9 9 9 9f	"
 	&a000s9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9sQ   NA7NCN3NDN(A1MNM>/N=M>>NN#&N#example_inputsdict[str, T | tuple[T, ...]]dict[str, list[T]]c                    d S r   rL   rP  s    rn   clone_inputsrU  	  s	     rm   Sequence[T]list[T]c                    d S r   rL   rT  s    rn   rU  rU  	  s    :=#rm   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 r   )r  dictr   r;  r<  rU  r  r  r-  r  ranger  )rP  resr   r   rO  s        rn   rU  rU  	  s    Nt##>""))++ 	. 	.JC%'' .'..C!%66CCUCC6&u--C

~

C3s88__ ) )c!fel++ 	) Q((CFJrm   c           	         	 |                                   d S # t          $ rI}ddlm} t	          j        ddt          |                     } |ddd| dg |d	           Y d }~d S d }~ww xY w)
Nr2   r  z\(.*r  z(skip frame due to being in functorh modez(torch.compile cannot be run in context: z. Skipping frame.T)r  r  r  r  r  
skip_frame)rG  r   r  r  rer  ri  )rG  r  r  functorch_subclass_names       rn   skip_frame_if_in_functorch_modera  	  s    
 
 
 
&&&&&& #%&"d3ii"@"@>mCZmmm	
 	
 	
 	
 	
 	
 	
 	
 	
 	

s    
A+>A&&A+Generator[None, None, None]c               #    K   t           j        j        } t           j        j        j        } |            5   |             5  t          j        t           j                                                  }t          |           t           j
                                        r0t          j        t           j
                                                  }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
                            |           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
                            |           t           j                                        rt           j                            |           d d d            w # 1 swxY w Y   w xY wr   )r  r6  _DisableFuncTorchr  _python_dispatch_disable_current_modesr  randomget_rng_statera  r  is_availablexpuset_rng_state)disable_functorchdisable_current_modes	rng_statecuda_rng_statexpu_rng_states        rn   preserve_rng_staterq  	  su     2!K8O			 	  C C"3"3"5"5 C CK : : < <==	'	222:""$$ 	E"[)A)A)C)CDDN9!!## 	C!K	(?(?(A(ABBMC C C C C C C C C C C C C C C C C C C C C C C C C C C C C C7[)@@BB 	7 	7L&&y111z&&(( 9
((888y%%'' 7	''666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7U[)@@BB 	7 	7L&&y111z&&(( 9
((888y%%'' 7	''666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7sy   D>CD'D>'D+	+D>.D+	/D>>EE
H% 1BHHH%$K;	BK/#K;/K33K;6K37K;model0TypeIs[torch.jit._trace.TopLevelTracedModule | torch.jit._script.RecursiveScriptModule | torch.jit.ScriptFunction[Any, Any] | torch.jit.ScriptModule]c                    t          | t          j        j        j        t          j        j        j        t          j        j        t          j        j        f          S r   )	r;  r  jit_traceTopLevelTracedModule_scriptRecursiveScriptModuleScriptFunctionScriptModule)rr  s    rn   is_jit_modelr|  	  sD     I1I3I$I"		
  rm   r  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  ru  tracer  scriptr   r  error)rB  rP  r  s      rn   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r   )r~  getfiler  OSErrorr  s    rn   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    rn   is_namedtupler  
  s    T#YY'''rm   r|   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-namedtuplerh   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  ,   K   | ]}|t           u|V  d S r   )r   )r   ts     rn   r   z$is_namedtuple_cls.<locals>.<genexpr>
  s,      KKA!7:J:Ja:J:J:J:JKKrm   c              3  R   K   | ]!}t          |          oj        |j        u V  "d S r   )r  __new__)r   r  r|   s     rn   r   z$is_namedtuple_cls.<locals>.<genexpr>!
  sR       ! ! 	 *!,, 5K194! ! ! ! ! !rm   F)
issubclassr<  r  r;  callable	__bases__anyr  )r|   modulebasess   `  rn   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
r  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>.Markerrt   indexNrg   rh   ri   r   rL   rm   rn   Markerr  ;
  s         




rm   r  r  r   r  )r   )slicer  r<  r   r  r  	dataclassrh   r   r[  n_fieldsdirr;  r  r  r  rG  r   )r|   r  r  fieldsr{   s        rn   namedtuple_fieldsr  0
  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0rm   gmCallable[[], None]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   dfd}|S )Nc                F    g | ]}||j         t          j        |          fS rL   )_versionr  r  )r   params     rn   r  z%checkpoint_params.<locals>.<listcomp>O
  s=     
 
 
  ENEK$6$67
 
 
rm   ru   rv   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 r   )r  r7  rg  rk  r  ri  r  rJ  )r  r  original_valuero  rn  saved_states      rn   restorez"checkpoint_params.<locals>.restoreU
  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  r7  r  rg  rh  r  ri  r   r   
parametersbuffers)r  r  ro  rn  r  s     @@@rn   checkpoint_paramsr  J
  s"   	 
 
K : : < <==	:""$$ 	E"[)A)A)C)CDDN
 
 #"**,,GG
 
 
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0 0 0 0 0 0 0 0 Ns   CC%%C),C)Iterable[Any]timestuple[Any, float]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  ri  synchronizer  gccollectmanual_seedr   perf_counterr[  )rB  rP  r  r  t0r  rN  t1s           rn   timedr  a
  s     z   j,KMMMJLLL	d				B5\\  '				B27?rm   c           	         t          d t          j        ||                     d                    D                       S )Nc              3  $   K   | ]}|j         V  d S r   )is_cudar   r}  s     rn   r   z check_is_cuda.<locals>.<genexpr>u
  s$      WWQqyWWWWWWrm   T)allr   r   r  )r  rP  s     rn   check_is_cudar  t
  s8    WW)/."--PTBUBU"V"VWWWWWWrm   r5  r  Callable[..., Any]c                   | dk    sJ d t          |           D             }t          |dd          |d d         z             }t          dd                    |           dd                    |           d          }d|  d	|_        |S )
Nr2   c                    g | ]}d | S r  rL   rN  s     rn   r  z rot_n_helper.<locals>.<listcomp>{
  s    &&&GGG&&&rm   r  zlambda rE  z: ()rot__helper)r[  reversedevalr   rg   )r  varsrotatedr  s       rn   rot_n_helperr  x
  s    q5555&&U1XX&&&DtBCCy49,--G	???388G+<+<???	@	@B####BKIrm   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 r   )r  r<  	frozensetr  r   is_safe_constantr;  enumEnumr  r  Sizer]  _GenericAliasr[  GenericAliasr  r  r  s    rn   r  r  
  s    a%#$$ -3'++,,,	IJ 	
	 	  
	'
 
rm   set[int]c                 
    ddhS Nr   r2   rL   rL   rm   rn   common_constantsr  
  s    
 	
		 rm   'TypeGuard[torch.SymBool | torch.SymInt]c                    t          | t          j        t          j        f          o.t          | j        t          j        j        j        j                   S r   )	r;  r  SymBoolSymIntrJ  nestedr  
nested_intNestedIntNoder`  s    rn   is_torch_symr  
  sH    eemU\:;; J
EL*5CE E A rm   sourcec                   ddl m} t          j                    j         o|j                                         p|j                                        ot          j	         pv|j        
                                ot          j	         pP|j                                        ot          j	         p* ||          pt          d           o| t                      v S )Nr2   )is_from_defaultsz5pytorch/dynamo:enable_unspecialize_zero_one_plain_int)r  r  r"   r   #force_unspec_int_unbacked_size_likeguard_sourceis_localis_specialized_nn_modulerJ   allow_unspec_int_on_nn_module"is_unspecialized_builtin_nn_moduleis_unspecialized_nn_moduler$   r  )r   r  r  s      rn   is_int_specialization_caser  
  s    ((((((!##GG ((*** 	

 88:: 988	
 BBDD 988	
 ::<< 988	
  F##!	
(   WXXX ,)+++/rm   r  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 )Nr2   )ConstantVariableLazyVariableTrackerSymNodeVariable)	variablesr  r  r  r;  is_realizedoriginal_sourcer  r  rJ   specialize_intr  rt   r  rO  evaluate_expr)r  r  r  r  r  r   is_symnode_vts          rn   specialize_symnoder  
  s    QQQQQQQQQQ #*++ COO4E4E  $$&&""$$$U++ 
%% >Us">.uf=== 	  	J#'' <&&s'8'8':':;;;Jrm   c                    ddl m} t          |           } t          | |          r(|                                 r|                                 S | S )Nr2   rB   )r  rC   r  r;  is_python_constantas_python_constant)r  rC   s     rn   guard_if_dynr    s]    ******
S
!
!C#'' (C,B,B,D,D (%%'''Jrm   Mapping[Any, Any]c                ~    t          d t          j        | |                                          D                       S )Nc              3  >   K   | ]}|                                 V  d S r   )r  r  s     rn   r   z&check_constant_args.<locals>.<genexpr>  s.      VV!q##%%VVVVVVrm   )r  r   r   rt  r  s     rn   check_constant_argsr    s3    VVytV]]__/U/UVVVVVVrm   c                    ddl m} ddlm} d}t	          j        | |                                          D ]?}t          ||          r|dz  }t          ||          r|                                s dS @|dk    S )Nr2   rB   UnspecializedPythonVariabler   F)	r  rC   variables.tensorr  r   r   rt  r;  r  )r,  r.  rC   r  unspec_countr}  s         rn   check_unspec_python_argsr	    s    ******======L_T6==??33  a455 	ALLQ00 	Q5I5I5K5K 	55	!rm   c                    ddl m} t          j        | |                                          D ])}|                                st          ||          s dS *dS )Nr2   r  FT)r  r  r   r   rt  r  r;  )r,  r.  r  r}  s       rn   check_unspec_or_constant_argsr    sl    
 >=====_T6==??33  $$&& 	*Q8S*T*T 	554rm   c                    ddl m t          fdt          j        | |                                          D                       S )Nr2   )NumpyNdarrayVariablec              3  8   K   | ]}t          |          V  d S r   )r;  )r   r}  r  s     rn   r   z+check_numpy_ndarray_args.<locals>.<genexpr>/  sB         	1*++     rm   )r  r  r  r   r   rt  )r,  r.  r  s     @rn   check_numpy_ndarray_argsr  ,  sa    666666    v}}77     rm   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 rL   r  r   methods     rn   	<setcomp>r  @  s6       
  rm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  E  %    NNN&Xf=M=MNvNNNrm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  F  s5       8H8H
  rm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  K  s%    RRRF&AQAQRRRRrm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  L  s%    PPP6x?O?OPPPPrm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  O  r  rm   c                0    h | ]}t          |          |S rL   r  r  s     rn   r  r  R  s5       hv>N>N
  rm   KVrY  
dict[K, V]KeysView[K]c                d    t          | t                    sJ t                              |           S r   )r;  rZ  r  )rY  s    rn   builtin_dict_keysr'  Z  s(    a99Q<<rm   Iterable[tuple[K, V | Any]]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 rL   )r   r  r   r  r  s     rn   r  z'get_items_from_dict.<locals>.<listcomp>f  s+    TTTK+C334TTTrm   c                J    g | ]}|t                               |          f S rL   )rZ  r  r+  s     rn   r  z'get_items_from_dict.<locals>.<listcomp>h  s.    FFF!D$$S!,,-FFFrm   )r;  rZ  r  r   r   r  r  s   `rn   get_items_from_dictr-  `  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Frm    Iterable[tuple[int, K, V | Any]]c                    t          |           }t          | t                    r?d t          t                              |                     D             fd|D             S d t          |          D             S )a  Enumerate dict items yielding (dict_keys_position, key, value).

    For OrderedDicts where move_to_end/prepend has been used, the OrderedDict
    iteration order can differ from dict.keys() order.  We iterate in
    OrderedDict order (correct execution semantics) but return each key's
    dict.keys() position so that ConstDictKeySource indices stay consistent
    with PyDict_Next / C++ DictGuardManager.
    c                    i | ]\  }}||	S rL   rL   )r   rO  r  s      rn   r  z6enumerate_items_with_dict_position.<locals>.<dictcomp>x  s    AAAtq!aAAArm   c              3  4   K   | ]\  }}|         ||fV  d S r   rL   )r   r  r  
key_to_poss      rn   r   z5enumerate_items_with_dict_position.<locals>.<genexpr>y  s2      88$!QA1%888888rm   c              3  ,   K   | ]\  }\  }}|||fV  d S r   rL   )r   rO  r  r  s       rn   r   z5enumerate_items_with_dict_position.<locals>.<genexpr>z  s0      88)!VaQ1I888888rm   )r-  r;  r   	enumeraterZ  r  )r  r   r2  s     @rn   "enumerate_items_with_dict_positionr5  k  s      $$E#{## 9AAy3'@'@AAA
8888%888888y'7'78888rm   c                l    t          |           }t          j        j                            |           |S r   )
object_newr  r  Moduler  )r|   r  s     rn   nn_module_newr9  }  s*    
S//C	HOS!!!Jrm   itIterable[T]c                B    t          j        t          j        | d          S r   )r{  reduceoperatormul)r:  s    rn   productr@    s    HL"a000rm   r  c                L    |                                  \  }\  }}|||z            S r   )
__reduce__)r:  r  r  r  r  s        rn   tuple_iterator_getitemrC    s'    }}Avuuu}rm   c                d    t          j                            t          j                  |           S r   )r  _dynamodisabler  r  r   s    rn   dataclass_fieldsrG    s#    =  !344S999rm   
count_iterIterator[Any]tuple[Any, Any]c                ^   	 |                                  \  }}n# t          $ r dd l}t          |           }|                    d          r|                    d          st          t          fcY S 	 |                    d|dd          d          }n'# t          t          f$ r t          t          fcY cY S w xY wt          |t                    rdt          |          cxk    rd	k    sn t          t          fcY S Y nw xY wt          |          dk    r
|d         dfS |d         |d         fS )
Nr   zcount(r  (   r  ,)r2   re   )rB  r  astri  
startswithrT  NotImplementedliteral_evalSyntaxErrorr  r;  r<  r  )rH  r  r,  rO  
count_reprs        rn   normalize_count_iterrU    su   4''))44 4 4 4 	


*%%
$$X.. 	4j6I6I#6N6N 	4"N3333	4##$<
1R4(8$<$<$<==DDZ( 	4 	4 	4"N333333	4$&& 	4a3t99.A.A.A.A.A.A.A.A"N3333 /B.A4 4yyA~~Q|GT!Ws5    AC?2!BC?B83C?7B88AC?>C?
range_itertuple[int, int, int]c                    |                                  \  }\  }}|j        |pd|j        z  z   }|j        }|j        }|||fS r   )rB  r  r  r  )rV  r  	range_obj	maybe_idxr  r  r  s          rn   normalize_range_iterr[    sP    !+!6!6!8!8A|	Y Oy~A??E>D>D4rm   r  c                ,    |                      |          S r   )as_subclass)r  r|   s     rn   to_subclassr^    s    ==rm   dict[Any, Any]c                ~    t          t          j        t                              |           ||dz                       S r   )nextr   islicerZ  r  )rY  r  s     rn   dict_keys_getitemrc    s/    
 	 1q!a%88999rm   r  set[T]c                ,    t          |           |         S r   r  )r  r  s     rn   set_getitemrg    s    771:rm   r  c                L    | j         j        }| j        }|rdnd}| d| d| }|S )NLGz["z"].)r  rg   r{   )r   r  r{   rG  r  
local_names         rn   	enum_reprrl    sD     ?#D
*C!CCcE++T++c++Jrm   rJ  torch.fx.Nodeexample_valuec                    || j         d<   t          j                    j        }|J |j        }t
          j        j        j        	                    ||          x}r|| j         d<   d S d S )Nrn  unbacked_bindings)
rI  r"   r   	fake_mode	shape_envr  r   experimentalsymbolic_shapescompute_unbacked_bindings)rJ  rn  rq  rr  symbol_to_paths        rn   set_example_valuerw    s     "/DIo"$$.I   #I 8 0JJ}
 
	
8 *8	%&&&8 8rm   vtrC   c                    |                                  j        j                            d          }t	          |          s/ddlm} ddlm}  |dt          |          dg |j
                   |S )Nrn  r2   graph_break_hintsr  z:Cannot check Tensor object identity without its fake valuez/TensorVariable is missing a fake example_value.r  r  r  r  )r  rJ  rI  r   rN   r  r{  r  r  r   
DYNAMO_BUG)rx  fake_tensorr{  r  s       rn   _get_fake_tensorr    s    ++--$)--o>>K; 	
''''''&&&&&&P$$I1%01		
 	
 	
 	
 rm   r  seq_lenc                |    |                      |          \  }}}t          d||z
  ||dk    rdndz
  z   |z            S )Nr   r2   r  )indicesmax)r  r  r  r  r  s        rn   slice_lengthr    sI    		'**E4q4%<4q11b#ABtKLLLrm   r  txrA   expectactualc                P    ddl m} d| d| d| } |t          | |g           d S )Nr   )raise_observed_exceptionz3wrong number of arguments or keyword arguments for z() call.
  Expect: z
  Actual: r  )torch._dynamo.excr  r  )r  r{   r  r  r  msg_strs         rn   raise_args_mismatchr    sw     ;:::::	d 	 		 		 	  
Y     rm   r   searchr@   check_tensor_identityc                   ddl m}                                 r0t          fd| D                       }|                    |          S d}|r%                                rd}t                    d }| D ]}|r=|                                r(t          |          u r|                    d          c S Addlm}	 |	                    |t          j
                                      ||gi           }
||
}|	                    |t          j                                      ||
|gi           }||                    d          }|S )Nr2   )r  c              3     K   | ]B}|                                 o)|                                                                k    V  Cd S r   )r  r  )r   r}  r  s     rn   r   z iter_contains.<locals>.<genexpr>  sk       
 
    "" F$$&&&*C*C*E*EE
 
 
 
 
 
rm   FTr   )SourcelessBuilder)r  r  r  r  rO  	is_tensorr  torch._dynamo.variables.builderr  r>  eqcall_functionor_)r   r  r  r  r  found_constmust_check_tensor_idfoundr}  r  checks    `         rn   iter_containsr    s    ,+++++  "" 4 
 
 
 
 
 
 
 
 

  &&{333  *!1!1!3!3 *#!&))$(E   	{{}} 9-a0000+22488888IIIIII%,,R==KKQK E })00X\BBPP  } ''..Lrm   r  :TypeIs[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  r8  r   )r  s    rn   	key_is_idr  @  s"     a%,9JKLLLrm   	list[Any]c                    d | D             S )Nc                N    g | ]"}t          |          rt          |          n|#S rL   )r  r  )r   r  s     rn   r  zkey_to_id.<locals>.<listcomp>H  s-    888QYq\\(BqEEEq888rm   rL   r`  s    rn   	key_to_idr  G  s    88%8888rm   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d} ||           S | S )Nr2   )is_builtin_callablerE  c              3  :   K   | ]}t          |           V  dS r  N
const_reprr   r  r  s     rn   r   zconst_repr.<locals>.<genexpr>O  s0      DDQj%888DDDDDDrm   []rL  rN  r  r  'r  r  r   ru   r   c                R    | j         }|j        }|dk    r|j        S |dz   |j        z   S )Nbuiltins.)r  rh   ri   )r  klassr  s      rn   fullnamezconst_repr.<locals>.fullname`  s7    KE%F##))C<%"444rm   )r  r   ru   r   )trace_rulesr  r;  r  r<  r   r  r  r  rl  replacerg   r  )r}  r  r  
elems_reprr  s    `   rn   r  r  K  sL   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 	5 x{{xrm   
const_keysc               V    d                     fd| D                       }d|z   dz   S )NrE  c              3  :   K   | ]}t          |           V  dS r  r  r  s     rn   r   z!dict_keys_repr.<locals>.<genexpr>m  s0      GGq
1E222GGGGGGrm   r  r  r   )r  r  keys_strs    ` rn   dict_keys_reprr  l  s8    xxGGGGJGGGGGH>Crm   
__dict_key)UnsupportedFakeTensorExceptionrootc                D    | dt          |           d| j        j         S )Nr  _c)r  outputrA  )r  r  r  s      rn   get_safe_global_namer  w  s+     66RWW66	 4666rm   r@  
containersContainer[T]c                    |D ]	}| |v r dS 
dS r  rL   )r@  r  	containers      rn   is_inr    s-      	944 5rm   )requires_suffixprefixr  c                   |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   r~   r  )r  r  r  rO  	candidates        rn   get_unique_name_wrtr    s      5#=*#=#=#= _  OOOO	Y,,,, 		 
'
''rm   Callable[[], Any]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)Nr2   r  zEncountered exception (z!) during fake tensor propagation.z!Fake tensor propagation exceptionr  )r  r  r  reasonr   r  r   )r  r  r  r  s       rn   wrap_fake_exceptionr    s    
rtt) 
 
 
&&&&&&SSSSC7MM	
 	
 	
 	
 	
 	
 	
 	
 	
 	

s   	 
A.AA))A.rq  ,torch._subclasses.fake_tensor.FakeTensorModec                     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 r   )r  deepcopyr  s   rn   r~  z)deepcopy_to_fake_tensor.<locals>.<lambda>  s    4=+=+= rm   )r  r  r~  FakeCopyModer  )r  rq  s   ` rn   deepcopy_to_fake_tensorr    s     
		&	3	3I	>	> ? ?"#=#=#=#=>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   AA	A	r  r\  c                x    t          j        t          j        t          j        | |z
                                S )z+
    Calculate root mean squared error
    )r  sqrtmeansquare)r  r\  s     rn   rmser    s,     :ejcCi!8!899:::rm   	equal_nanc                (    t          | |d|          S )Ng        )tolr  same)r  r\  r  s      rn   bitwise_samer    s%    	   rm   g-C6?TGz?fp64_refcos_similarityr  floatexact_dtyperelax_numpy_equalityignore_non_fp	log_errorCallable[..., None](use_larger_multiplier_for_smaller_tensorforce_max_multiplieruse_iou_for_booliou_thresholdc                   	
  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    o3t          	
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 Ad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,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    rrdS rP z                                                                  } z                                                                  }|dk    rt7          |dk              S ||z                                  }|k     r 	d|t=          |                                          t=          |                                          t=                                                                                     t=                                                                                    t           j                             dS 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     rQtN          (                    d|)                                *                                                                           t7          |dk              S s !                    j                   t          j                    rdS j        t
          j+        k    rd},                                -                                s,                                -                                sZ ,                                -                                r4t          j         !                    j                  ||          rdS t]                                                     }t_          j,        |          rtN          (                    d            t]                                                    }d- 
fd"} |            }|||z  d#z  z   k    }|sGrEt_          j,        |          r1t_          j,        |          rt
          j0        j1        j2        j3        sd}|s) 	d$||4                                j        |
           |S rdS  	d%           dS t           tj          t<          t          d          t6          t
          j6        f          r)rdS  k    }|s 	d&t                                 |S to                     stq                     rir2to                    s#tq                    s                                  t                     t                    u o k    }|s 	d'            |S ts                     r\t                     t                    u o=t          t          j:                   t          j:                  	
(          S t                     j        d)v rJt                     t                    u sJ t          	 
fd* j;        D                       S ty          d+t                     j                   ).z-Check correctness to see if ref and res matchNztype mismatch r   zLength mismatchFc              3  Z   K   | ]%\  }}}t          |||
	           V  &dS )r  r  r  r  r  Nr  )r   aibi	fp64_refir  r  r  r  r  r  r  r  r  r  r  s       rn   r   zsame.<locals>.<genexpr>  sx       ,
 ,
" "B	! $#9a%9!1+  ,
 ,
 ,
 ,
 ,
 ,
rm   QuestionAnsweringModelOutputr  zkeys mismatch z == )r  r  r  r  r  r  r  r  r  r  r  zAccuracy failed for key name %sTzelements mismatch r  r   ru   r  c                b    t          | t          j                  r| nt          j        |           S r   )r;  r  r  tensorr  s    rn   	to_tensorzsame.<locals>.to_tensor(  s%    "1el33H11aHrm   c              3  .   K   | ]} |          V  d S r   rL   )r   rG  r  s     rn   r   zsame.<locals>.<genexpr>+  s+      MMiinnMMMMMMrm   zdtype mismatch %s, %sr   z^IoU accuracy failed: %.4f < %.2f (intersection=%d, union=%d, ref_sum=%d, res_sum=%d, shape=%s)r(  )atolrtolr  z+Accuracy failed: uint8 tensor did not matchgư>)dimepsr  zSimilarity score=%sg{Gz?z=Found nan in reference. Consider running in higher precision.r  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       @rK     r  r2   g{Gz?)r)  r  ro  bfloat16numelndimr3  )
multiplierr  r  r  r\  r  r  s    rn   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(??
 &)
%%rm   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     rn   r   zsame.<locals>.<genexpr>  s       
 
  S!!S!!#&&-#'%9+#9a  
 
 
 
 
 
rm   zunsupported type: )r  r   ru   r  )ru   r  )=r;  r  r<  r   r  r  r  ParameterListr  r  r  r  rB  rg   r  lossrZ  r  r  rG  r  r  r  rM   	is_sparseto_denser)  r   r   r@  rt   r3  allclosetori  flattenrp  
functionalcosine_similarityr   r  detachcpurq  isnanr  r  mathr  rJ   cppinject_relu_bug_TESTING_ONLYrC  r   rM  rm  rr  r  	as_tensor__dict__r   )r  r\  r  r  r  r  r  r  r  r  r  r  r  r  r  intersectionunioniourscore	loose_tol	ref_error	res_errorr	  r  passes_testr  s   ``````````````            @rn   r  r    s   " dE;,eh.DejQ  yF #e[->?@@ 	
 	
4T#YY44c44	
 	
@ s88s3xxI'(((53xx3s88# 
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
" &)c8%<%<#,
 ,
 ,
 )
 )
 	
& 
c	=	=	= HHM 5]!5-'
 
 
 	
  
C		 IF#t$$$$$388::#chhjj//111CS__CC#chhjj//CC 211 

## 	 	AFFQK#1' +)="/'=e)=%5"/  $ 	;Q???uu'( t	C		 nF#s#####3xx3s88###%R#c((%R%RC%R%R###t	C%,.	/	/ jFc5#4#?@@@@@c5#4#?@@@@@	I 	I 	I 	I NMMMc88LMMMS(= 	!=  =,,..C,,..C#u|,,VV.VtCyy.V.V4PS99.V.VVV, (	yCI%%	139ciHHHuyEJ&&   4#   %(#I??#4#4#:#:#<#<L 3YOO--3355Ezz#LA$5666'%/5577C]**!	|) 1 1 3 344

--		 0 011		 0 011 OO	 	 	  %u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&&& (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Erm   c                n    | j                             d          d         }d| j         d| d| j         dS )N/r  r  z' (r   r  )r  splitr  r  )r  short_filenames     rn   format_func_infor5    sC    %++C004NGt|GGGG1DGGGGrm   c               #    K   t           j        } t          j        t           _        t           j        }t          j        t           _        	 d V  | t           _        |t           _        d S # | t           _        |t           _        w xY wr   )rJ   recompile_limitrY  r  accumulated_recompile_limit)priorprior_acc_limits     rn   disable_cache_limitr;    sm      "E [F8O),F&=!&-<*** "'-<*<<<<s   A& &B z'collections.defaultdict[Any, list[Any]]guard_failuresz3list[torch._dynamo.output_graph.GraphCompileReason]graph_break_reasonsroot_dirc                    dt           j                                                             d          z   dz   t          t	          j                              z   }t          j                            | |          S )Nrun_z%Y_%m_%d_%H_%M_%S_%fz-pid_)datetimenowstrftimer   r  getpidr  r   )r>  dir_names     rn   _get_debug_dirrF  5  sm     	




!
!
*
*+A
B
B	C 	 bikk

		  7<<(+++rm   c                 8    t           j        } t          |           S r   )rJ   r_  rF  )
debug_roots    rn   r  r  A  s    &J*%%%rm   requiredc                    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 )
Nrn  r   r  r2   rz  z Missing FakeTensor example valuez,`FakeTensor` example value was required for z but not available.r|  )rI  rN   r  r  r  r{  r   r}  )rJ  rI  r  r{  s       rn   extract_fake_example_valuerK  F  s    $)##	/0J(K(K#y))	 333333''''''6II`t```1%01		
 	
 	
 	
 	
 	
 trm   r  c                6    t          |           |j        u sJ | S r   )rO   rq  )r  r  s     rn   ensure_graph_fakerM  X  s"    q!!R\1111Hrm   r  allow_non_graph_fakec                ^     d fd}t           j        j                            ||          S )Nr  rm  ru   r   c                   | 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  rn  get_attr)r  rI  get_fake_valuetargetr  
nn_modulesr;  r  r   GraphModuler  rM  )r  r  r   rN  r  s      rn   visitz)get_fake_values_from_nodes.<locals>.visit`  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"---
rm   )r  rm  ru   r   )r  r   rJ  map_arg)r  r  rN  rV  s   ` ` rn   get_fake_values_from_nodesrX  ]  sC          " 8=  ...rm   r  FakeTensorMode | Nonec                    ddl }ddlm || S d}|j        J |j        }|j        d
fd	} |j        |||           } | S )z
    Replace symbolic size expressions (like 's0', 's94') in error messages
    with their concrete runtime values for better readability.

    Example: "size (s94)" -> "size (s94: hint= 10)" if s94's value is 10.
    r   N)Integerz
\(s(\d+)\)matchr   ru   r   c                   d|                      d           t          fd                                D             d           }t          |t          f          rd dt          |           dS |                      d          S )Nr  r2   c              3  8   K   | ]\  }}|j         k    |V  d S r   )r{   )r   r  r  sym_names      rn   r   zGget_concrete_sizes_from_symints.<locals>.replace_sym.<locals>.<genexpr>  s3      KK41a(8J8JQ8J8J8J8JKKrm   rL  z	: hint = r  r   )groupra  r   r;  rt   r   )r\  rG  r_  r[  backed_var_to_vals     @rn   replace_symz4get_concrete_sizes_from_symints.<locals>.replace_sym  s    'u{{1~~''KKKK,2244KKK
 
 cC>** 	65x55#c((5555{{1~~rm   )r\  r   ru   r   )r_  sympy.core.numbersr[  rr  ra  r  )r  rq  r_  patternrr  rb  r[  ra  s         @@rn   get_concrete_sizes_from_symintsre  t  s     III******
G***#I!3       "&+s
+
+CJrm   gb_fnCallable[[], NoReturn]r   c           	         ddl m}m} 	  |              nP# |$ rH} |t          |          t	          |dd                     }|                    |j                  d d }~ww xY wt          d          )Nr2   )TorchRuntimeErrorUnsupported
real_stackzshould be unreachable)r  ri  rj  r   r  with_traceback__traceback__r  )rf  ri  rj  r  r  s        rn   (_wrap_graph_break_with_torch_runtime_errrn    s    33333333< < < <A<(F(FGG  11t;< 0
1
11s   
 A"AAA"c                   t          j                    }	 t          | ||          |j        j        xj        t          j                    |z
  z  c_        S # |j        j        xj        t          j                    |z
  z  c_        w xY wr   )r   r   _get_fake_value_implr  bytecode_tracing_timingsget_fake_value_ns)rJ  r  rN  _t0s       rn   rR  rR    s~    
 ,..CU#D".BCC
	*<<QT@TT<<<	*<<QT@TT<<<<<<s   A 0Bc           	         ddl m} ddlm ddl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j        }
|
J |d	k    rgt-                    dk    rTt/          d         t          j        j                  r/t5          d         |
          ft7          dd                   z   |d
k    r\j        j         j                 t?                    r&tA          d          r!                               t5          |
           j"        dv stG          d D                       rt7          d D                       	 |
5  tI                      5  tK           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  tL          $ r}||j'        |j'        t/          t          j(        j)        j*                  rqj+        t          j,        j-        j.        j/        t          j,        j-        j0        j/        fv rdg}ndg} dtc          j+                  dj+         d|           nt/          t          j(        j)        j2                  rrt          j        j        j3        s. dtc          j+                  dj+         ddg           n dtc          j+                  dj+         ddg           not/          t          j(        j)        j4                  rj+        }d}t/          |t          j5        j6                  rGt          j7        8                    |j9        j"        |j9        j:                  }||\  }}d| d| d| d } d!d"j+         d| d#g           nt/          t          j;        j<        j=        j>                  r! ||j?        tc                    d$%          t/          |          r ||j?        |j        d                   |t/          t                    r3d&tc                    v r" d'd( j         d) dg jA                   t          tc          |          |
          t          fd*           t          d+          dd}~ww xY w|s9t          jF        t          jG        t          jI        t          ,          |          }t          j        j        j        st          j        j        j        r!j        jK        L                     ||	           |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   )ValueRangeErrorr2   rz  )r  rj  	UserErrorUserErrorTypern  c                V    i | ]&}t          |          t          |          |j        'S rL   )rN   r  r  r  s     rn   r  z(_get_fake_value_impl.<locals>.<dictcomp>  s=     !
 !
 !
&)!
sGGS\!
 !
 !
rm   Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  @   K   | ]}t          |t                    V  d S r   )r;  complex)r   as     rn   r   z'_get_fake_value_impl.<locals>.<genexpr>  sE       M M#$
1gM M M M M Mrm   c              3     K   | ];}t          |t          j                  r|j        j        t          |          n|V  <d S r   )r;  r  SymFloatrJ  hintr  r  s     rn   r   z'_get_fake_value_impl.<locals>.<genexpr>  sc       
 
  c5>227:x}7P c


	
 
 
 
 
 
rm   c                 4    t          j                   S r   )run_noder  )r,  r.  nnmodulerJ  r  s   rn   r~  z&_get_fake_value_impl.<locals>.<lambda>  s    D$II rm   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: c                 2     ddg j                    S )Nz)RuntimeError when making fake tensor callr  r  
USER_ERROR)causer{  r  r  s   rn   r~  z&_get_fake_value_impl.<locals>.<lambda>g  s0    MMC5)45   rm   zshould not reachable)r  )Mtorch.utils._sympy.value_rangesru  r  r{  r  r  rj  rv  rw  r  rI  rN   rX  r,  r.  r  rE  rJ   use_graph_deduplicationtrack_nodes_for_deduplicationr3   rq  r  r;  r  r8  r  r<  r  rT  rS  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  r6  _dispatch_pystub_schemaoverload_namer   rs  rt  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr  r  re  rn  r  rQ  tree_map_onlyr  r{  partialrM  region_trackertrack_node_mutations)rJ  r  rN  ru  rj  rv  rw  r  flat_args_kwargsid_to_initial_versionrq  ret_valr  r  import_suggestionmaybe_pystubr  ctxr  r,  r  r{  r.  r  r  r  s   ``                 @@@@@@@rn   rp  rp    s    @?????######IIIIIIIIIIII	B $)##	/0J(K(K#y))-
TY$&: LD&
 	4#==# 6tR((*>
 
!
 !
-=!
 !
 !

  "HI   	]s4yy1}}DGUX_1U1U}'Q;;=d122hO	]9'4(## 	7:L(M(M 	7
 &&x666 +8Y??yEEE M M(,M M M J JE  
 
 
 
 
 
 
v? 	 	022 	 	)IIIIIIII G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     o? o? o? ;"KE5$0M
 
 _	
 z	#+	2:  K` M1EJI I I I     5$0L
 
 D	 ='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M===( & & &
 
 
 
 
 
 58(8T
 
 	 )2E

5   	
 // 		)M>q	JJPQQy)) 	jCJJ.F.FM@;T[;;E;;5)45    .c!ffi@@0      	
 	
 	
 344$>_o?b   
 L)+,="EEEw
 

 	4
==
 		 55!	
 	
 	
 Nsa   -I /H8>H!H8!H%	%H8(H%	)H8,I 8H<<I ?H< I UK,UUtorch.fx.Node | Nonec                 .    t          t          dd           S )Nr   )r  _current_noderL   rm   rn   get_current_noder    s    ='4000rm   c              #     K   t                      }| t          _        	 d V  |t          _        d S # |t          _        w xY wr   )r  r  r   )rJ  olds     rn   set_current_noder    sI      


CM"!c!!!!s   0 >tracerr  c           	     .   j         t                    5  dfd}ddlm} 	 dk    r j        i cd	d	d	           S d
k    r{t          d         j                  s+ddlm} ddlm}  |dd |d          g |j	                    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$ rT}	ddlm} ddlm} g |j	        }
t!          |	t                    r|
ddgz  }
 |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.
    r  r   ru   r   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 )rS  ri  )r  r,  r.  rJ  r  s    rn   make_error_messagez$run_node.<locals>.make_error_message  s@    p2ppppW[ppagpppq''rm   r2   )rj  r  Nry  r   rz  r  z/Missing attribute when running call_method noder  zattribute not definedr|  rz  rQ  placeholderrn  zsIf the op is a custom op, did you implement a fake tensor implementation? (e.g. with `@my_custom_op.register_fake`)z;If the op is a PyTorch op, please file an issue to PyTorch.zGNotImplementedError/UnsupportedFakeTensorException when running FX noder  )r  r   ru   r   )r  r  r  rj  rS  r   r  r{  r  r  r  output_graphget_submodulerI  r  r  r;  r  r   rl  rm  r  )r  rJ  r,  r.  r  r  rj  r{  r  r  r  r  s    ```       @rn   r  r    s   " 
B	$		 ; ;	 	 	 	 	 	 	 	 	 	%$$$$$1	_$$"t{D3F33; ; ; ; ; ; ; ; }$$tAw44 	333333222222!M Q "$6$67N$O$O= 1 <=	    5wtAw44d122hI&II1; ; ; ; ; ; ; ;2 }$$+++x0007; ; ; ; ; ; ; ;8 z!!*88EE;; ; ; ; ; ; ; ;< }$$&$)3333y1A; ; ; ; ; ; ; ;< % $%CD 	 	 	++++++******3'23E!011 @Q  Ma..q11          	 	 	 	 	 	11!4455DD 	q; ; ; ; ; ; ; ; ; ; ; ; ; ; ;z 

s`   G=EA4EE*$EEG=G.A
F&!G=&G.81G))G..G==HHc                F  	
 ddl m
 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)          | |||          }||| <   n8# t*          $ r+}|	t-          	
fd	           t/          d
          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.
    r2   rz  r  c                $    t          |           S r   )get_real_value)r  r  s    rn   r~  z get_real_value.<locals>.<lambda>  s    .F++ rm   r  graphargrz  Nc                 2     dddg j                    S )Nz7RuntimeError when trying to get real value from fx.Noder  r  r  )exnr{  r  s   rn   r~  z get_real_value.<locals>.<lambda>  s0    MMQ5)45   rm   zshould not be reachable)r  r{  r  r  real_value_cacher  r  r   rJ  rW  r,  r.  rI  exampler  rT  rS  r  r  r  r  r   rn  r  )rJ  r  cacher  r,  r.  	nn_module
real_valuer  r  r{  r  s    `       @@@rn   r  r    s   
 $#####""""""#Eu}}T{	B8=((	DK ++++ LD&
 
]zTY66y$,,	]'24;?	i(( 	'i00II It&v&&&&	BfdD&)DD
 d B B B0     	
 	
 	
 677TAB s   C) )
D3&DDtorch.nn.Modulec                   ddl mm} dfd}|                                 D ])\  }} ||          rJ d| d	 ||                       *|                                 D ])\  }} ||          rJ d
| d	 ||                       *d S )Nr   )FakeTensorConfigrN   r  r   ru   r   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     rn   stack_or_hintz7assert_no_fake_params_or_buffers.<locals>.stack_or_hint  s?    ! 	d`9N9Nq~9^9^```ccrm   zUnexpected fake buffer r   zUnexpected fake param )r  r   ru   r   )torch._subclasses.fake_tensorr  rN   named_buffersnamed_parameters)r  rN   r  r{   bufferr  r  s         @rn    assert_no_fake_params_or_buffersr    s   GGGGGGGGd d d d d d ((** 
 
f76?? 	
 	
DdDD]]6-B-BDD	
 	
" 	
 **,, 
 
e75>> 	
 	
BTBBMM%,@,@BB	
 	
! 	

 
rm   c                $    | j          d| j         S )z9
    Returns the fully qualified name of the object.
    r  )rh   ri   r  s    rn   fqnr  .  s     n11s/111rm   count1count2c                6    t           j        j        j        r| S |S r   )r  rE  rJ   assume_static_by_default)r  r  s     rn   ifdynstaticdefaultr  5  s    }4 rm   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)rG  r  listdirr  r  r   r   __file__rT  	importlibimport_modulerg   )r  r  s     rn   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rm   c                :    t          t          |                     S r   )class_has_getattributer  r`  s    rn   object_has_getattributer  E  s    !$u++...rm   c                F    t                               | d          }|||<   d S )Nr'  )r   __getattribute__)r  r{   r   rY  s       rn    object_setattr_ignore_descriptorr  I  s$    Z00AAdGGGrm   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  r  r   s    rn   r  r  O  sc    "3(:;;
 
 	 4		
    5s   -2 
??ignore_nn_module_getattrr>  c                    	 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  r8  r  )r   r  
getattr_fns      rn   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 )TensorStaticReasonre   r  r   N)rg   rh   ri   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYrL   rm   rn   r  r  h  s         IJrm   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    rn   tensor_static_reason_to_messager  n  sX    #---OO#...CC#666EE
3633
4
44rm   r  torch.Tensor | Anyr  tensor_sourcer!   &tuple[bool, TensorStaticReason | None]c                T   ddl m} |j                                        s|j                                        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.
    r2   ))is_from_unspecialized_param_buffer_sourceTr  )r  r  r  r  r  rJ   &force_nn_module_property_static_shapesr  r  r  r  r  r  force_parameter_static_shapesr  r  )r  r  r  r  s       rn   tensor_always_has_static_shaper  x  s      BAAAAA 	";;==;%HHJJ; 
7; '::: 	V***44]CC 	+

. 	+ '111 3'222;rm   rH  c                2     d fd}t          |          S )Nru   r   c                     	 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 rL   )r  r{   rS  r,  r.  )r   r  s     rn   r  z<lazy_format_graph_tabular.<locals>.inner.<locals>.<listcomp>  s:     
 
 
;<QT1618QVQX6
 
 
rm   )opcoder{   rS  r,  r.  r   )
r   r   r   r*   rS  r  r)   forward__code__r  )r   
node_specs	graph_strrH  r  s      rn   innerz(lazy_format_graph_tabular.<locals>.inner  s    	))))))) 	 	 	~,Wb99::;  	
 
@B
 
 

 H N N N
 
 
	 "'2:+>+JIVVVs   
 *77ru   r   rG   )rH  r  r  s   `` rn   lazy_format_graph_tabularr    s>    W W W W W W W" erm   line_noc                l    |  d| d| d| dt          j        |                                            d
S )Nr   z line z 
r   )disBytecode)r  r{   r  r  r  s        rn   format_bytecoder    sI     VVtVVhVVgVV#,t:L:L:P:P:R:RVVVVrm   )_forward_pre_hooks_forward_pre_hooks_with_kwargs_forward_hooks_with_kwargs_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          t          j        j        j        j                  p't          t          j        j        j        j                            S r   )r   r  r  r  modulesr  _global_backward_hooks_global_backward_pre_hooksrL   rm   rn   nn_module_has_global_hooksr    sH     EH#:;; 	Cux&ABB  rm   check_forward_hookscheck_backward_hookscheck_state_dict_hooksc                @   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  r  r  r  hook_dicts_to_checkcheck_all_hooks	all_hookshook_dict_namehooks	hook_namer   s              rn   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""""	# rm   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        rn   nnmodule_has_hooksr+    s2     $/15	  E ;;rm   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 r   )to_numpy_helperr   r  s     rn   r   z"to_numpy_helper.<locals>.<genexpr>
  *      AAC?3//AAAAAArm   )rN   r;  tnpr  r/  r  r  r  numpyr<  r  r  r`  s    rn   r/  r/    s    u~~ %%% u|,,,	E5<	(	( {{{&&&	EE4=	)	) tE{{AA5AAAAAArm   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 r   )numpy_to_tensorr0  s     rn   r   z"numpy_to_tensor.<locals>.<genexpr>  r1  rm   )
rd  r;  r  r  r&  r2  r  r<  r  r  r`  s    rn   r6  r6    s    >>>%$$ &u%%%%%% |	EE4=	)	) tE{{AA5AAAAAArm   c                  &    e Zd ZddZddZddZdS )numpy_to_tensor_wrapperr  Callable[_P, R]ru   rv   c                <    || _         d| j         j        z   | _        d S Nwrapped_r  rg   )r  r  s     rn   r  z numpy_to_tensor_wrapper.__init__  s    "TV_4rm   r   c                "    d| j         j         dS )Nz<Wrapped function <original >>r=  r  s    rn   __repr__z numpy_to_tensor_wrapper.__repr__!  s    AdfoAAAArm   r,  r-  r.  r/  r   c                :     | j         |i |}t          |          S r   )r  r6  r  r,  r.  r   s       rn   r  z numpy_to_tensor_wrapper.__call__$  s&    dfd%f%%s###rm   N)r  r9  ru   rv   r  r,  r-  r.  r/  ru   r   )rg   rh   ri   r  r@  r  rL   rm   rn   r8  r8    sT        5 5 5 5B B B B$ $ $ $ $ $rm   r8  c                   t          | t          j                  rt          | |          }t	          |          S t          | t
          j                  r1t          t          j        |           |          }t	          |          S d S r   )r;  r2  r  r  r6  r  r  )r  r{   r   s      rn   numpy_attr_wrapperrE  )  sx    #s{## $c4  s###	C	&	& $ck#&&--s###$ $rm   c                  *    e Zd ZdZddZddZddZdS )numpy_method_wrapperzgConvert obj from torch.Tensor to tnp.ndarray and call method. Then convert result back to torch.Tensor.r  r   ru   rv   c                2    || _         d| j         z   | _        d S r;  )r  rg   )r  r  s     rn   r  znumpy_method_wrapper.__init__5  s    "T[0rm   c                    d| j          dS )Nz<Wrapped method <original r?  )r  r  s    rn   r@  znumpy_method_wrapper.__repr__9  s    ;DK;;;;rm   r,  r   r.  c                    |d         }t          |t          j                  rt          j        |          }t          || j                  } ||dd          i |}t          |          S r  )r;  r  r  r2  r  r  r  r6  )r  r,  r.  r  method_callabler   s         rn   r  znumpy_method_wrapper.__call__<  si    1gc5<(( 	#+c""C!#t{33otABBx2622s###rm   N)r  r   ru   rv   r  )r,  r   r.  r   ru   r   rg   rh   ri   r   r  r@  r  rL   rm   rn   rG  rG  2  sV        qq1 1 1 1< < < <$ $ $ $ $ $rm   rG  c                  *    e Zd ZdZddZddZddZdS )numpy_operator_wrapperzQImplements dunder methods for tnp.ndarray via functions from the operator libraryr  r  ru   rv   c                2    || _         d|j         | _        d S r;  )r  rg   )r  r  s     rn   r  znumpy_operator_wrapper.__init__H  s    02;00rm   r   c                    d| j          dS )Nz<Wrapped operator <original r?  )rg   r  s    rn   r@  znumpy_operator_wrapper.__repr__L  s    ?dm????rm   r,  r-  r.  r/  r   c                T    |rJ d |D             } | j         | }t          |          S )Nc              3  v   K   | ]4}t          |t          j                  rt          j        |          n|V  5d S r   )r;  r  r  r2  r  r  s     rn   r   z2numpy_operator_wrapper.__call__.<locals>.<genexpr>S  sS       
 
KN
3 = =FCK3
 
 
 
 
 
rm   )r  r6  rB  s       rn   r  znumpy_operator_wrapper.__call__O  sH    z
 
RV
 
 
 dgtns###rm   N)r  r  ru   rv   r  rC  rL  rL   rm   rn   rN  rN  E  sZ        [[1 1 1 1@ @ @ @$ $ $ $ $ $rm   rN  c                   t          | t                    s| S | j        r=d |                                 D             }d |                                 D             }n(|                                 }|                                 }t          j        ||| j        | j        | j	                  }|
                                 |S )Nc                `    g | ]+}t           j        j        j                            |          ,S rL   r  r   rs  rt  optimization_hintr   r  s     rn   r  zdefake.<locals>.<listcomp>c  s;     
 
 
KLEH!1CCAFF
 
 
rm   c                `    g | ]+}t           j        j        j                            |          ,S rL   rU  rW  s     rn   r  zdefake.<locals>.<listcomp>f  s>     
 
 
 H!1CCAFF
 
 
rm   )r)  rM  r  )r;  rM   _has_symbolic_sizes_stridesrC  r4  r  empty_stridedr)  rM  r  zero_)r}  rC  r4  r  s       rn   defaker\  Z  s    a$$  	$ 
 
PQPVPVPXPX
 
 

 
XXZZ
 
 

 vvxxgxo	 	 	A GGIIIHrm   r9  r-  r/  c                     | |i |S r   rL   )r  r,  r.  s      rn   8_disable_side_effect_safety_checks_for_current_subtracerr^  x  s     2tvrm   c                0    dd l }| |j        j        j        u S r   )torch.utils.checkpointr  
checkpoint)r  r  s     rn   is_utils_checkpointrb  ~  s!    !!!!%+(333rm   c                    ddl m} | |u S )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphrd  )r  rd  s     rn   is_invoke_subgraphrf    s     SSSSSS---rm   optionsc                 H    ddl m}  |j        t          j        j        fi | S )Nr2    TorchHigherOrderOperatorVariable)variables.higher_order_opsrj  maker  _higher_order_opsinvoke_subgraph)rg  rj  s     rn   build_invoke_subgraph_variablero    sB    LLLLLL0+0/ 
  rm   c                     dd l mc m} ddlm} |j        }t          j        j        j	        r|j
        } |j        |fi | S )Nr   r2   ri  )torch._higher_order_ops.wraprm  wraprk  rj  tag_activation_checkpointr  
_functorchrJ   functionalize_rng_opswrap_activation_checkpointrl  )rg  higher_order_opsrj  activation_checkpoint_ops       rn   build_checkpoint_variablery    s    ;;;;;;;;;LLLLLL
 	2  4 O#3#N 0+0  
  rm   device_typerD   c                    ddl m} t          j        |           j        } |            }|dk    rn|dv r|rt                      }nd}|S )Nr2   )is_dynamo_supportedr!  )r  rj  mtiaF)
eval_framer|  r  rM  r  r/   )rz  r|  r  compile_supporteds       rn   is_compile_supportedr    sl    //////<$$)D++--u}}	(	(	(->	(&LL!rm   offsetc                    |                      d          }t          |d|                             dd                    S )z
    Convert byte offset `offset` of `str` into character offset.
    Byte offset is used for 3.11+ instruction column data.
    Takes things like unicode characters into consideration.

    Unchanged from CPython implementation.
    zutf-8Nr  )errors)encoder  decode)r   r  as_utf8s      rn   _fix_offsetr    s?     jj!!Gwww&&wy&AABBBrm   c                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )_Anchorsrt   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetNr  rL   rm   rn   r  r    sD          rm   r  segment_Anchors | Nonec                   t           j        dk    sJ ddl}d}	 |                    d| z   dz             }n# t          $ r Y dS w xY w|J t          |j                  dk    rdS |                     d          dfd}dfddfd}dfd}|j        d         }t          ||j	                  r|j
        }t          ||j                  rt          t          |j        j                  dz
  }|j        j        J  |||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        J  |||j
        j                  } ||          \  }}|         |         dk    r! |||          \  }}|         |         dk    !t          t          |j                  dz
  }|j        J  |||j                  }t%          ||||          S t          ||j                  rt          t          |j        j                  dz
  }|j        j        J  |||j        j                  } ||          \  }}|         |         dk    r! |||          \  }}|         |         dk    !t          t          |j                  dz
  }|j        J  |||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
    )rf      r   Nz(
z
)r2   r   linenort   r  ru   c                0    t          |          |          S r   )r  )r  r  liness     rn   	normalizez-_extract_anchors_from_expr.<locals>.normalize  s    5=&111rm   coltuple[int, int]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  r  )r  r  r  s     rn   next_valid_charz3_extract_anchors_from_expr.<locals>.next_valid_char  s    s5zz!!cSv-?-?&?&?CaKF s5zz!!cSv-?-?&?&? E

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

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

""sSv-?-?'?'?'??s{rm   re   z)\#z\#r  rL  )r  rt   r  rt   ru   rt   )r  rt   r  rt   ru   r  )rY  rZ  rO  parserS  r  bodyr3  r;  Exprr   BinOpr   rt   left
end_linenoend_col_offsetisspacer  	SubscriptCallr  )r  rO  treer  r  r  	statementexpr
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor  r  s                  @@rn   _extract_anchors_from_exprr    s    w&&&&JJJD yy5011   tt
49~~tMM$E2 2 2 2 2 2
                  	!I)SX&& CLdCI&& A	L c49#7881<J9+777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:,888 ydj.GHHH$3OK$J$J!K$X.#55(1	+x(H(H%X $X.#55  T_559L&222!	,0CDDIK<KKKch'' 	L
 sDI$899A=K9+777 ydi.FGGH$3OK$J$J!K$X.#55(1	+x(H(H%X $X.#55  T_559L&222!	,0CDDIK<KKK4s   8 
AAtypes.CodeTypeinstr>   c                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 t'          |d          r|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}	 t5          |          }n# t6          $ 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).
    )rf      )r  colno	end_colnor   r   r2   Nc                h    g | ].}t          j        j        |                                          /S rL   )	linecachegetliner  rstrip)r   r  r  s     rn   r  z.get_instruction_source_311.<locals>.<listcomp>l  sE     
 
 
 d.77>>@@
 
 
rm   	positionsr  r   ~c                :    g | ]}|                     d d          S )r  ^)r  r   markers     rn   r  z.get_instruction_source_311.<locals>.<listcomp>  s&    BBB6>>#s++BBBrm   c                ,    g | ]}t          |          S rL   rf  r  s     rn   r  z.get_instruction_source_311.<locals>.<listcomp>  s    +O+O+OVDLL+O+O+Orm   r  c                8    g | ]}d                      |          S )r  r  r  s     rn   r  z.get_instruction_source_311.<locals>.<listcomp>  s"    AAAv2776??AAArm   ) rY  rZ  r  FrameSummaryr  r  r  r  r  
col_offsetr  r  r   
splitlinesr[  textwrapdedentr  indentr   r  r  r  r  r   lstripr  r  r  r  r  r  )r  r  frame_summaryrN  
orig_linesorig_lines_dedent
indent_lenr  
first_linestart_offset
end_offsetr  markers	last_liner  line
num_spacesanchorsmutable_markersr  rO  s   `                    rn   get_instruction_source_311r  O  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4%%D$.*D*DD~$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   )P9 9
QQc                \    t          | t          j                  rt          | dd           S d S )N_dynamo_static_input_type)r;  r  r  r  r  s    rn   get_static_address_typer    s.    !U\"" =q5t<<<4rm   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 r   )	r  r6  r6   	get_statedefault_generatorrh  r  	set_staterk  )r   getterssetterss      rn   is_rng_state_getter_or_setterr    ss     	$)
 G 	$)
 	G (g(((((rm   c                    t          | t          j                  o<| j        dk    o1t	          | j        d          o| j        j        t          j        j	        u S )N__get____objclass__)
r;  r[  r   rg   r   __self__r  r  r6  _TensorBaser`  s    rn   is_tensor_base_attr_getterr    sV    5%122 	@Ni'	@ENN33	@ N'58+??	rm   c                    	 t          j        t          j        |           }t	          |          t
          j        u S # t          $ r Y dS w xY wrc  )r~  r  r  r  r  r[  r  r  )r{   r  s     rn   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    rn   is_torch_function_objectr    s    5.///rm   c                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  r  r   r  r;  r  
class_typer   r  r6  _disabled_torch_function_impl)rx  r  r  r  s       rn   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5rm   c                    d }d }t           j        j                                        x}r| |j        v r|j        |          }|j        }|                    | d||          S )NF)static_shapessymbolic_contextr  )r  rQ  r"   try_gettensor_to_contextr  from_tensor)r  rq  r  r  tracing_contexts        rn   to_fake_tensorr     sx     F-6>>@@@ 4111.@C%3F  	1A& !   rm   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    rn   is_frozen_dataclassr  .  st    #E*** 	.&u---	.	. E122	. E.99		.
 &-rm   attrsc                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  r  r  s      rn   get_first_attrr  9  s^      & &3 	&3%%%%%	& CMMeMM
N
NNrm   should_enable	fullgraphdynamicc              #     K   | sd V  d S dfd}t           j        j                            |          5 }|V  d d d            d S # 1 swxY w Y   d S )Nr  r   ru   c                <    dd}t          j        | |          S )Ngm_r   example_inputs_ru   c                    t           j        j        j        d         dxx         dz  cc<   t           j                            | |          S )Ncompiled_autogradcompilesr2   )r  rE  r  r]   r  compile)r  r  s     rn   inner_compilerzKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compilerM  sE    #,-@A*MMMQRRMMM..sODDDrm   )backendr	  r
  )r  r   r  r   ru   r   )r  r  )r  r  r
  r	  s     rn   compiler_fnz3maybe_enable_compiled_autograd.<locals>.compiler_fnL  s?    E E E E =Ni   rm   )r  r   ru   r   )r  rE  r  _enable)r  r	  r
  r  r  s    ``  rn   maybe_enable_compiled_autogradr  D  s        	 	 	 	 	 	 	 ],44[AA 	SIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A

AAr1   c                 V     G d dt                     } t           |                       S )Nc                      e Zd ZdS )*invalid_removeable_handle.<locals>.InvalidN)rg   rh   ri   rL   rm   rn   Invalidr  [  s        rm   r  )rZ  r1   )r  s    rn   invalid_removeable_handler  Y  sB        $    7799%%%rm   c                    t          | t          j        j                  s| S t          | t          j        j                  r| S | j                            | j                  }| j        |_        |S r   )	r;  r  r  r8  r   rU  r  r  r'  )r  proxys     rn   nn_module_proxyr  e  s^    c58?++ 
#ux+,, 
M!!#-00E\ENLrm   c                  (     e Zd Zd fdZdd
Z xZS )	GmWrapperr  rC  unflatten_fnCallable[[list[Any]], Any]ru   rv   c                d    t                                                       || _        || _        d S r   )r  r  r  r"  )r  r  r"  r  s      rn   r  zGmWrapper.__init__q  s0     	(rm   r,  r   c                Z    t          |          } | j        |                     |           S r   )r  r  r"  r  s     rn   r   zGmWrapper.forwardx  s*    t**tw))$//00rm   )r  rC  r"  r#  ru   rv   r,  r   ru   r   )rg   rh   ri   r  r   r  r  s   @rn   r!  r!  p  sQ        ) ) ) ) ) )1 1 1 1 1 1 1 1rm   r!  
compile_gmCallable[[Any, Any], Any]c                  	
 d t          | j        j                  D             
t          j        j        j        ri
dgk    sJ t          |d         t                    sJ t          |d                   dd	dfd} |t          | |           	|                    n]t          j        |          \  }}t          j        t          j        |	          } |t          | |          |          t          j        	d	
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  rI  r   )r   rO  rJ  s      rn   r  z(flatten_graph_inputs.<locals>.<listcomp>  sH       At7m##	k5(I(I# 	
###rm   r   r,  r   ru   c                B    | d         t          | dd                    z   S r  rf  r  s    rn   
flatten_fnz(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qrr(^^++rm   	flat_argsc                0    | d          g| d          R S r   rL   )r.  boxed_inputs_counts    rn   r"  z*flatten_graph_inputs.<locals>.unflatten_fn  s,    1112TY?Q?R?R5STTTrm   )treespecc                 n     |           }D ]}| |                                            |          S r   )r   )r,  r.  rO  compiled_fnr-  inputs_idx_to_clears      rn   wrapperz%flatten_graph_inputs.<locals>.wrapper  sH    Jt$$	 % 	 	AGMMOOOO {9%%%rm   r&  )r.  r   ru   r   )r4  rS  r  r  rE  r  in_compiled_autograd_regionr;  r  r  r!  rQ  rR  r{  r  tree_unflattenarg_tree_leaves)r  r@  r'  r"  flat_inputsspecr5  r0  r3  r-  r4  s          @@@@rn   flatten_graph_inputsr;  ~  sd     00   }&B , #qc))))&)T***** ^^	, 	, 	, 	,	U 	U 	U 	U 	U 	U !j2|!<!<jj>P>PQQ #/77T ()>NNN j2|!<!<kJJ+
& & & & & & & & Nrm   maybe_gmc                    t          | t          j        j                  rt	          | d          sg S | j                            dg           S )NrI  locals_to_steal)r;  r  r   rU  r   rI  r   )r<  s    rn   get_locals_to_stealr?    sH    h 455 WXv=V=V 	=.333rm   r>  c                    || j         d<   d S )Nr>  rH  )r  r>  s     rn   set_locals_to_stealrA    s    !0BGrm   c                      e Zd ZddZd	dZdS )
Litr  r   ru   rv   c                    || _         d S r   r  )r  r  s     rn   r  zLit.__init__  s    rm   c                    | j         S r   rE  r  s    rn   r@  zLit.__repr__  s	    vrm   N)r  r   ru   rv   r  )rg   rh   ri   r  r@  rL   rm   rn   rC  rC    s<                rm   rC  zset[str]warn_once_cache
stacklevelc                    | t           v rd S t                               |            t          j        | |dz              d S )Nr2   )rH  )rG  r  warningswarn)r  rH  s     rn   	warn_oncerL    sG     oM#*q.111111rm   textc                V    t          j        d          }|                    d|           S )Nz\x1B[@-_][0-?]*[ -/]*[@-~]r  )r_  r  r  )rM  ansi_escapes     rn   strip_color_from_stringrP    s&    *:;;K??2t$$$rm   c               #     K   	 t           j        j                            d          } d V  t           j        j                            |            d S # t           j        j                            |            w xY w)NT)r  r6  	_autograd _saved_tensors_hooks_set_tracing)r9  s    rn   +_disable_saved_tensors_hooks_during_tracingrT    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 r   )r  r  rJ   freezingis_grad_enabledrL   rm   rn   is_parameter_freezingrX    s!    ?!*J53H3J3J/JJrm   c                 L    d t          t                                D             S )Nc                ,    g | ]}t          |          S rL   ) get_torch_function_mode_stack_atrN  s     rn   r  z1get_torch_function_mode_stack.<locals>.<listcomp>  s.       01(++  rm   )r[  r   rL   rm   rn   get_torch_function_mode_stackr\    s2     5:;T;V;V5W5W   rm   indc                t    | t                      k     r| dk    sJ t          j                            |           S r   )r   r  r6  _get_function_stack_at)r]  s    rn   r[  r[    s7    *,,,,98**3///rm   ra  c                    t          t                                D ]}t                       | D ]}t          |           d S r   )r[  r   r   r   )ra  r  modes      rn   set_torch_function_mode_stackrb    sY    ,..// $ $!#### , ,%d++++, ,rm   c                 ^    t          t                                D ]} t                       d S r   )r[  r   r   )r  s    rn   clear_torch_function_mode_stackrd    s9    ,..// $ $!####$ $rm   rM  torch.devicetorch.Streamc                @    t           j                            |           S r   )r  acceleratorcurrent_streamrL  s    rn   get_current_streamrj    s    ++F333rm   c                 "    t                       d S r   )
breakpointr  s    rn   _breakpoint_for_c_dynamorm    s    LLLLLrm   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 )Nre   r2   )InternalTorchDynamoErrorz[Tensor subclass method __metadata_guard__ must take exactly two subclass metadata argumentsz?Tensor subclass method __metadata_guard__ must be a classmethod)	__metadata_guard__r~  	signaturer  r  r  ro  r  r  )r   r  sigro  s       rn   verify_guard_fn_signaturers    s    		!B

B

C
3>a111111&&i
 
 	
 
{eo%%111111&&M
 
 	
 &%rm   user_clsc                    | 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 r   )r   rZ  r   rt  r  __iter__)rt  s    rn   #does_not_override_dict_iter_methodsrw    sl    4:{'899 	GO[-?@@	GMdi)9::	G $-1E!FF	rm   rO  c                ,    |                      |          S r   )rC  r}  rO  s     rn   	call_sizerz  "  s    66!99rm   c                ,    |                      |          S r   )r4  ry  s     rn   call_strider|  '  s    88A;;rm   c                *    |                                  S r   )storage_offsetr|  s    rn   call_storage_offsetr  ,  s    rm   c                ,     ddg} fd|D             }|S )Nr  tagc                b    i | ]+}|j         v |t          j        j         |                   ,S rL   )r'  r  )r   r   r  s     rn   r  z(_extract_tensor_dict.<locals>.<dictcomp>9  s>       ,/3!*CTCTTYqz#''CTCTCTrm   rL   )r  KEYS_TO_COPYtensor_dicts   `  rn   _extract_tensor_dictr  3  s@    #L
   3?  K rm   
tuple[Any]c                .    t          j        j        | i |S r   )r  r6  Streamr  s     rn   build_streamr  @  s    8?D+F+++rm   torch.Eventc                .    t          j        j        | i |S r   )r  r6  Eventr  s     rn   build_eventr  D  s    8>4*6***rm   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d                        ZdS )CompileTimeInstructionCounterr   rt   _counterr  _idru   rv   c                n    | j         dz   | _         | j         dk    rt          j                    | _        d S d S r   )_depthr   r  r  r   s    rn   r  z#CompileTimeInstructionCounter.startM  s5    Z!^
:??*022CGGG ?rm   c                    | j         dz
  | _         | j         dk    r0| xj        t          j        | j                  z  c_        d| _        d S d S )Nr2   r   r  )r  r  r   endr  r   s    rn   r  z!CompileTimeInstructionCounter.endS  sJ    Z!^
:??LL04SW===LLCGGG ?rm   c                    d| _         d S r   r  r   s    rn   r   z#CompileTimeInstructionCounter.clearZ  s    rm   c                    | j         S r   r  r   s    rn   r   z#CompileTimeInstructionCounter.value^  s
    |rm   rb  c              #     K   	 t           j        r|                                  d V  t           j        r|                                  d S d S # t           j        r|                                  w w xY wr   )rJ   %record_compile_time_instruction_countr  r  r   s    rn   r  z$CompileTimeInstructionCounter.recordb  st      	; 		EEE; 					 v; 				s   $A #A/Nr   r   ru   rb  )rg   rh   ri   r  r   r  r  r   r  r  r   r   r   r  rL   rm   rn   r  r  H  s         HCMMMMF3 3 3 [3
    [    [    [    ^ [  rm   r  c                        e Zd Zd fdZ xZS )CompileCounterIntotherr   ru   c                `    t          t                                          |                    S r   )r  r  __add__)r  r  r  s     rn   r  zCompileCounterInt.__add__o  s!     !7!7888rm   )r  r   ru   r  )rg   rh   ri   r  r  r  s   @rn   r  r  n  s=        9 9 9 9 9 9 9 9 9 9rm   r  featureusagec                    t                                                      r%t                                          d| |           dS dS )zQ
    Records whether we are using a feature
    Generally a feature is a JK.
    r!  N)r   r   set_key_value)r  r  s     rn   set_feature_user  s  sO     ((** M++OWeLLLLLM Mrm   )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=)rJ   optimize_ddpr;  r   r   r  r  r  )r  ra  s     rn   get_optimize_ddp_moder    s    &L,%% 
".E4E	L#	&	& 
LtL7I7ILL
 
 	
 ))));D;; *)) Krm   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)rJ   "fake_tensor_disable_inference_moder  is_inference_mode_enabledinference_moder7  )is_inference_mode_ons    rn   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)rJ   r  r  r  
meta_utils$disable_inference_mode_for_fake_proprL   rm   rn   *maybe_disable_inference_mode_for_fake_propr    s       0 )NNPP 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAc                .    | d u pd| j         v pd| j         v S )Nrn  rG  rH  )rJ  s    rn   is_node_meta_validr    s$    4<M?di7M5DI;MMrm   c                     t           S r   _error_on_graph_breakrL   rm   rn   _get_error_on_graph_breakr    s      rm   c                
    | a d S r   r  r`  s    rn   _set_error_on_graph_breakr    s    !rm   AbstractContextManager[None]c                     t           j        j        j        r$t           j        j                            d          nt          j                    } | 	                                 | S )NzPregraph bytecode)
r  r  r  r  r6  r7  r8  rV  nullcontextr9  cms    rn   record_pregraph_bytecode_enterr    sR     >"7	&../BCCC#%% 
 LLNNNIrm   r  c                4    |                      d d d            d S r   )r:  r  s    rn   record_pregraph_bytecode_exitr    s    KKdD!!!!!rm   list[CodeType] | Nonec                 ,    ddl m}   | j                    S )Nr   r"   )torch._guardsr"   get_traced_coder  s    rn   r  r    s%    ,,,,,,)>)+++rm   c                    ddl m} ddlm} t	          |           j                            dd          }|r0 |ddt	          |            d| d	| d
dg|j                   d S d S )Nr2   rz  r  __hash__Fz,User-defined object with overridden __hash__zhashing object of type=z and variable tracker zFound a user-defined object z4 with overridden __hash__ when attempting to hash itzMDynamo does not support hashing user-defined objects with overridden __hash__r|  )r  r{  r  r  r  r'  r   SUPPORTABLE)r  rx  r{  r  is_overriddens        rn   raise_on_overridden_hashr    s    ######""""""II&**:u==M 	
BSd3iiSSrSSorooo_".		
 	
 	
 	
 	
 	
	
 	
rm   r  Callable[..., VariableTracker]c                J     t                    s
J d            d fd}|S )Nz!Expect f to be a python callable.r,  rC   r.  ru   c                     ddl m} ddlm}  |            5   |                              | |          cd d d            S # 1 swxY w Y   d S )Nr   )_force_inline)UserFunctionVariable)torch._dynamo.trace_rulesr  !torch._dynamo.variables.functionsr  r  )r,  r.  r  r  r  r  s       rn   inline_callz"_make_inlined.<locals>.inline_call  s     	<;;;;;JJJJJJ]__ 	K 	K''**88T6JJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	Ks    AA	A	)r,  rC   r.  rC   ru   rC   r  )r  r  r  s   `` rn   _make_inlinedr    sR     A;;;;;;;;K K K K K K K rm   )r   r   r   r   ru   r   r   )r   r   ru   rv   )ru   r   )r   rt   ru   rv   )ru   ra   )ru   r   )ru   r   )r{   r   ru   r4  )NFNNNNFN)r   r   r=  r>  r   r   r   r?  r@  r>  rA  rB  rC  rD  rE  r   rF  r>  ru   r4  r2  )ri  rj  rk  r   ru   r   )ri  ro  rk  r   ru   rp  )r   F)ri  r   rk  r   ru   rr  )ru   r  )r  r  r  r
   ru   r   )r  r   r  r?   ru   rv   )r  r  ru   rt   )r}  rP   ru   rP   )r}  r   ru   r   )r,  r   r.  r   ru   rv   )r  r   r  r  ru   r  )r  r   r  r  ru   r  )r  r   r  r  ru   r  )r  r   r  r   ru   r!  )r  r   r  r#  ru   r$  )r  r   r  r&  ru   r'  )r  r   r  r)  ru   r*  )r  r   r  r,  ru   r-  )r  r   r  r/  ru   r0  )r  r   r  r2  ru   r3  )r  r   r  r5  ru   r6  )r  r   r  r8  ru   r   )r  r   r  r   ru   r   )r@  rA  rB  rC  ru   rD  )r   r   ru   r   )r   rs  ru   rt  )r   r   ru   rw  )r   r   ru   r  )r   r   ru   r  )r   r   ru   r  )r  r   ru   r   )r  r   ru   r  )r   r   ru   r  )r  r   ru   r   )r  r   ru   r   r
  r   )rG  r   ru   r  )r,  r   r.  r   ru   r  )r  r  ru   r   )r  r  ru   rv   r  )r  rt   r  rt   rZ  r^   r  r  r  r  ru   rv   )r  rt   ru   rv   )ru   r  )ru   r  )ru   r   )FF)r   r   r  r   r   r   ru   r4  r6  )r  r  r  r  r  r   ru   rv   )r  r  r  r  ru   rv   )r}  r  r)  r*  ru   r  )rP  rQ  ru   rR  )rP  rV  ru   rW  )rP  r   ru   r   )rG  r  ru   rv   r  )rr  r   ru   rs  )rB  r   rP  r   r  r   ru   r   )r  r   ru   r>  )r|   r   ru   r   )r|   r  ru   r  )r  rC  ru   r  )r2   )rB  r   rP  r  r  rt   ru   r  )r  rC  rP  r  ru   r   )r  rt   ru   r  )r  r   ru   r   )ru   r  )r   r   ru   r  )r   r   r  r   ru   r   )r  r   ru   r   )r,  r  r.  r   ru   r   )rY  r$  ru   r%  )r  r$  ru   r(  )r  r$  ru   r.  )r|   r   ru   r   )r:  r;  ru   rt   )r:  r   r  rt   ru   r   )rH  rI  ru   rJ  )rV  r   ru   rW  )r  r   r|   r  ru   r   )rY  r_  r  rt   ru   r   )r  rd  r  rt   ru   rP   )r   r   r  r   ru   r   )rJ  rm  rn  r   ru   rv   )rx  rC   ru   r   )r  r  r  rt   ru   rt   )r  r  )
r  rA   r{   r   r  r   r  r   ru   rv   )
r   r  r  r   r  r@   r  r   ru   r   )r  r   ru   r  )r   r   ru   r  )r}  r   r  r   ru   r   )r  r   r  r   ru   r   )r  rA   r  r   r  r   ru   r   )r@  rP   r  r  ru   r   )r  r   r  r   r  r   ru   r   )r  r  ru   r   )r  r   rq  r  ru   r   )r  r  r\  r  ru   r  )r  r   r\  r   r  r   ru   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  ru   r   )r  r
   ru   r   )r>  r   ru   r   r  )T)rJ  rm  rI  r   ru   r   )r  r   r  rA   ru   r   )r  rA   r  r   rN  r   ru   r   )r  r   rq  rY  ru   r   )rf  rg  ru   r   )rJ  rm  r  rA   rN  r   ru   r   )ru   r  )rJ  rm  ru   rb  )r  r   rJ  rm  r,  r   r.  r   r  r   ru   r   )rJ  rm  r  r   ru   r   )r  r  ru   rv   )r  r   ru   r   )r  r   r  r   ru   r   )r  r  ru   rv   )r  r   r{   r   r   r   ru   rv   )r|   r  ru   r   )r   r   r  r   ru   r>  )r  r  ru   r   )r  r  r  r   r  r!   ru   r  )rH  r   r  rC  ru   r   )r  r   r{   r   r  r   r  rt   r  r   ru   r   )FFF)
r  r  r  r   r  r   r  r   ru   r  )
r  r  r  r   r  r   r  r   ru   r   )r   r   ru   r   )r  r   r{   r   ru   r   )r}  r   ru   r   )r  r9  r,  r-  r.  r/  ru   r[   )rg  r   ru   r   )rz  rD   ru   r   )r   r   r  rt   ru   rt   )r  r   ru   r  )r  r  r  r>   ru   r   )r  r   ru   r   )r{   r   ru   r   )rx  rC   ru   r   )r  r  rq  r  ru   r   )r  r   r  r   ru   r   )TT)r  r   r	  r   r
  r   ru   r4  )ru   r1   )r  r   ru   r   )r  rC  r@  r   r'  r(  ru   r  )r<  r   ru   r  )r  rC  r>  r  ru   rv   )r  r   rH  rt   ru   rv   )rM  r   ru   r   )ru   r  )r]  rt   ru   r   )ra  r  ru   rv   )rM  re  ru   rf  )r   r   ru   rv   )rt  r   ru   r   )r}  r   rO  rt   ru   rt   )r}  r   ru   rt   )r  r  ru   r^   )r,  r  r.  r_  ru   rf  )r,  r  r.  r_  ru   r  )r  r   r  r   ru   rv   )rJ  r  ru   r   )r   r   ru   rv   )ru   r  )r  r  ru   rv   )ru   r  )r  r   rx  rC   ru   rv   )r  r@   r  r  ru   r  ('  r   
__future__r   atexitr   rV  r  r  rA  r	  r  r{  r  r  r~  r   rJ  r  r  r#  r>  r  r_  rY  r  r  r   r  r[  r]  r  rJ  r  r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r  torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreer  _pytreerQ  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._ordered_setr-   torch.utils._python_dispatchr.   torch.utils._tritonr/   r0   torch.utils.hooksr1   graph_utilsr3   r^  collections.abcr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   %torch._dynamo.bytecode_transformationr>   torch._dynamo.replay_recordr?   torch._dynamo.symbolic_convertr@   rA   torch._dynamo.variables.baserC   torch._prims_commonrD   torch._subclassesrE   r3  rd  r  torch._loggingtorch._numpy_numpyr2  rF   rH   r  rJ   fftlinalgrg  rK   r   NP_TO_TNP_MODULEr  rM   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r  r8  r  unpatched_nn_module_getattrr  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implr   r]   r_   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgr  rg   r   r`   r  rb   r~   timer_counterr  rd   rp   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   rU  r<  rh  rn  registerr  FloatTensorrp  DoubleTensorrq  double
HalfTensorro  halfBFloat16Tensorr  
ByteTensorri  
CharTensorre  
LongTensorrh  long	IntTensorrg  rt   ShortTensorrf  short
BoolTensorr   tensortype_to_dtyper  r  r  r  r  VERBOSEr  	Formatterr  r  r  r  r  r  r  r  r  r  r  r<  r=  rZ  ParamSpecArgsParamSpecKwargsTypeVarTupleTypeAliasTyperV  r_  ra  rm  rr  rv  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  r  r  r  rP  r  r  !DEFAULT_COMPILATION_METRICS_LIMITr  rT  rW  rz  r  r  r   r  r  r  r  r  r   r'  r  r  r  r  r  r  r'  r-  rU  ra  rq  r|  r  r  r  r  r  r  r  r  r  r  r   rs   r  Ellipsisr  rQ  rM  r)  memory_formatr1  finfoiinfo	attention
SDPBackendr  _CudaDevicePropertiesSequenceKey
MappingKey
GetAttrKeyr  r  r  languager  r  r  r  r  r  r  r  r	  r  r  r  r  rt  r  r   r  r  iterr  r[  r  __length_hint__tuple_iterator_lenr   r  r7  rZ  dict_newr   r'  dict_methodsr  set_methodsr  frozenset_methods	tuple_newtuple_methodsr  list_methodsr  list_getitemstr_methodsenum_type_methodsr"  r#  r'  r-  r5  r9  r@  rC  rG  ra  	iter_nextrU  r[  r^  dict_getitemrr  rc  rg  rl  rw  r  r  r  r  r  r  r  r  GLOBAL_KEY_PREFIXr  r  r  r  r  r  r  r  r  r  r5  r;  orig_code_mapr<  r=  seen_code_maprF  r  rK  rM  rX  re  rn  rR  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r"  all_hook_namesr  r)  r+  r/  r6  r8  rE  rG  rN  r\  r^  rb  rf  ro  ry  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r!  r;  r?  rA  rC  rG  rL  rP  rT  rX  r\  r[  rb  rd  rj  rm  rs  rw  _disable_dynamorz  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rL   rm   rn   <module>rA	     s;'     # " " " " "                



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



                , , , , , , , , = = = = = = = = $ $ $ $ $ $       - - - - - - - -                        0 / / / / / / /      , , , , $ $ $ $ $ $ $ $ $                  < ; ; ; ; ; O O O O O O O O ; ; ; ; ; ; ; ; ; ; = = = = = =              G F F F F F F F & & & & & & 1 1 1 1 1 1 / / / / / / F F F F F F > > > > > > > > - - - - - - ' ' ' ' ' ' 
 1                        BAAAAA;;;;;;        =<<<<<222222000000   	BBB	......)))))) 
 FII	>
 	
 	
 	
 	
 FCGIszIsz	
  " VVVVVVVVVVV 	 	 	D	 GCLLWT]]WT]]WT]]WT]]WT]]WT]]WT]]WT]]WT]]gennGCLLYt__#ho9  8?3  % : 7N{7N8 8     %'  & & & & B o 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
 
 
 
 
           <@  ? ? ? ?* * * *$ $ $ $$ $ $ $           > 'y(( 0 0 0 08 8 8 8    49   f# f# f# f# f# f# f# f#R	 $IO%%     .  ""')-+/#'#!,0MQ MQ MQ MQ MQ` 
 L L L L 
 L 
 -2) ) ) ) 
) */$ $ $ $ $N 8 8 8 8
 
u{3	5	u}ej1	5>+	u{n	uzm	u{EJ/	Oek59-	U[1	uzm        ( #:"9";"; " " " ", , , ,
 !bj J	 	 0 0 0 0 0* 0 0 0   	 	 	 	? ? ? ?
G G G G         	 	 	 	       B 
 A A A 
 A 
   
 
   

 
   

 
$ $ $ 
$
 
) ) ) 
)
 
. . . 
. 
3 3 3 
3 
8 8 8 
8 
= = = 
=  
C C C 
C" 
   

& & & & 27  8 8 8 8w 	%!    w!fl_4!   $   &    
5 5 5 
5
 
7 7 7 
7
    
  ! !"            kkkkkk        43 3 3 3   % % % %$ $ $ $, , , , T       
 
 
 
$0 0 0 05 5 5 5   @" @" @" @" @" @" @" @"F %' ! ?Pk>O,? ? ?     
- - - -`!3 !3 !3 !3H3 3 3 30- - - -`O3 O3 O3 O3d% % % %! ! ! !
& & & &Q Q Q Q Q Q Q Qh
 26  5 5 5 5   * * * *
  %*"'' ' ' ' '@ ( ( ( ( ( ( ( ((         +       ).**       @ @ @ @ AE I I I I I IX 
   

 
 = = = 
 =   &
 
 
 
& 7 7 7 7*   &    $   ( ( ( (
   < Q1 1 1 12   0 =>    &X X X X 2    		DJJ	N	L	K		L	K	K	H!	J$	K#	K"	K"/$     4  5MMMfo3444   $          >   2   W 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  
 ONCL$7$7$9$9NNN "+2244    M	RRen&;&;&=&=RRRPPT]%9%9%;%;PPPNNCL$7$7$9$9NNN di188::    GCLLGCLL   G G G G9 9 9 9$   1 1 1 1   
: : : : 	   .        : : : :   
   8 8 8 8(   M M M M 	    2 #(	* * * * *ZM M M M9 9 9 9   B       
 !  = < < < < <7 7 7 7    <A( ( ( ( ( ($
 
 
 
"? ? ? ?; ; ; ;      !&%(Y5:!&"LF LF LF LF LF^
H H H H
 = = = =  '&(( ;R+:QRV:W:W W W W W LN  M M M M '&(( , , , ,& & & &
    $   
/ / / /.   B2 2 2 2 "'	U 	U 	U 	U 	U "'T T T T Tn  	!!1 1 1 1 " " " "P P P Pf/ / / /d
 
 
 
*2 2 2 2   G G G G/ / / /   	 	 	 	 27
 
 
 
 
       5 5 5 5   D   *W W W W    -.?@     $&99<QQ    !&!&#(	    F !&!&#(	    $   
 
 
 

$ 
$ 
$ 
$ 
$gb!en 
$ 
$ 
$$ $ $ $$ $ $ $ $ $ $ $&$ $ $ $ $WRU^ $ $ $*   <   4 4 4 4. . . .      &   $	C 	C 	C 	C        } } } }@A A A AH   ) ) ) )"      0 0 0 0   <       O O O O AE    (& & & &   1 1 1 1 1 1 1 1. . . .b4 4 4 41 1 1 1         CEE ! ! ! !	2 	2 	2 	2 	2% % % % C C C CK K K K   0 0 0 0
, , , ,$ $ $ $
4 4 4 4
   
 
 
 
"               
 
 
 
, , , ,+ + + +# # # # # # # #L9 9 9 9 9 9 9 9
M M M M+        "    . 
 
 
 
N N N N
  ! ! ! !" " " "
     " " " ", , , ,
 
 
 
$     s%   F F"!F"&A/H HH