
    /j                   R"   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/m0Z0m1Z1m2Z2m3Z3m4Z4 dd
l5m6Z6m7Z7 ddl8Z8ddl9Z8ddl:Z8ddl;m<c m=Z> ddl8m?Z? ddl@mAZAmBZBmCZCmDZD ddlEmFZF ddlGmHZHmIZI ddlJmKZKmLZLmMZM ddlNmOZO ddlPmQZQmRZRmSZSmTZTmUZU ddlVmWZWmXZX ddlYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z` ddlambZbmcZc ddldmeZe ddlfmgZg ejh        r>ddlimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZs ddltmuZu ddlvmwZw ddlxmyZymzZz ddl{m|Z| dd l}m~Z~ dd!lmZ 	 ddlZn# e$ r dZY nw xY w	 ddlZ8ddlmZ dd"lJmZ 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 e3d)          Z e3d*          Z e6d+          Ze8j        j        j        Ze8j        j        j        Ze8j        j        j        Z ej        ej!                  Zd,ed-<   i Zd.ed/<   d0Zd1Zd2e d3Z ej        e          Zi Zd4ed5<    ej        e          Zd6ed7<    ej                    Z G d8 d9e
j                  Z G d: d;          ZdgdBZdadhdDZdhdEZdadFedG<   didJZdjdKZdhdLZdadkdOZdldPZdhdQZ ej                    ZdmdSZdndUZ G dV dWe
j                  Z G dX dY          Z ej                    Ze$dod\            Ze$	 	 	 	 	 	 	 	 dpdqdl            Ze0drdsdp            Ze0	 drdtds            Z	 dudvduZej        dhdv            Ze8j        e8j        e8j        fe8j        e8j        e8j        fe8j        e8j        e8j        fe8j        e8j        fe8j        e8j        fe8j        e8j        fe8j        e8j        e8j        fe8j        e8j        e8j        fe8j        e8j        e8j        fe8j        e8j        fi
Z G dw dx          Z e            ZdwdzZdhd{Z ej        d|ej                  Z G d} d~ej                  ZdwdZdwdZdxdZdydZdzdZd{dZd|dZd}dZ G d d          Ze0d~d            Ze0dd            Ze0dd            ZddZ e            Zded<   ej        dk    r&ej        ej        ej6        ej3        ej        ej        fZddZej        dk    reej4        fz  ZddZ ddZddZe0dd            Ze0dd            ZddZe4ej        ej        ej        ej        f         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#ded<   ddZ$ddZ%ddZ&ddZ'ddZ(ddZ)dhdZ*ddZ+ G d d          Z,da-ded<   ddZ.ddZ/e$	 	 ddd            Z0ej         G d d                      Z1 G d de          Z2 e2            e2_3        dd Z4ddZ5ddZ6dddd	Z7e0dd            Z8e0dd            Z8ddZ8ddZ9e$dd            Z:ddZ;drddZ<ddZ=ddZ>ddZ? ej&        d          dd             Z@dd#ZA	 ddd'ZBdd(ZC e&d)          dd,            ZDeeeEeeFeG eHd          eIjJ        eKjJ        ej'        e8jL        e8jM        e8jN        e8jO        e8jP        e8jQ        e8j        jR        jS        e8jT        jU        hZVd-ed.<    ec            r*ddlWZWeVX                    eWjY        jM                   	 dd/ZZej[        dd1            Z\dd3Z]dd5Z^dd7Z_dd8Z`dd:Zadd;Zbdd<Zcdd=Zd eHi e                                          Zfd>ed?<    eHi g                                          Zhd@edA<    eHi i                                          ZjdBedC<    eH e"            g                                          Zkd@edD<    eH eld'                    ZmdEedF<    eH el end                              ZodEedG<   emjp        Zqerjs        Zteujs        ZvdH  ejw        eujx        g                                e"jx        g                                          D             ZydI ezjx        g                                D             Z{dJ e|jx        g                                D             Z}ejs        Z~dK ejx        g                                D             ZdL ejx        g                                D             Zej        ZdM eFjx        g                                D             ZdN  eHe
j                  jx        g                                D             Z e3dO          Z e3dP          ZddTZddVZddWZddZZdd\Zdd]ZeZdd`ZddbZeuj        Ze8j?        j        ddd            ZddgZddiZddmZddpZddsZ	 	 ddÐdyZ	 dr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Zd]ddːdZd̐dZd͐dZdΐdZdrdϐdZdd]dd]dd]d]ej        d]d]d]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j[        dҐd            ZdӐdZdԐdՐdZd֐dZdאdZdؐdZdِdĄZ	 drdڐdńZ	 dr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	 drddۄZ G d܄ de
j                  Zǐdd߄ZȐddZɐddZʐddZg dZ̐ddg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 de,eef                   ZՐddZ G d d          Z G d de,eef                   ZؐddZِddZڐddZېddZܐddZݐddZސdd
ZߐddZej         G 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ZddZej$        	 ddd#            Zdd%Zdd&Z G d' d(e8j        j                  Zdd+Zd d-Zdd/Z G d0 d1          Z ez            Zd2ed3<   ddd5Zdd7Zej$        dd8            Zdd9Zdd:Zdd<Zdd>Zdhd?ZddCZddDZddEZ d	dGZe8j        d
dI            Ze8j        d
dJ            Ze8j        ddK            ZddLZddNZddPZ G dQ dR          Z	 G dS dTeަ          Z
ddWZdXZdedY<   dӐdZZe$dd[            Ze$dd\            Zdd]Zd]add^Zdd_Ze8j        dda            Ze8j        ddc            ZddeZddfZdS (  a  
Utility functions and classes used throughout the TorchDynamo system.

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

This is an internal module that provides shared functionality used across the Dynamo codebase.
    )annotationsN)CounterOrderedDict)AbstractContextManagercontextmanager)is_dataclass)	lru_cache)CodeTypeMethodWrapperType)AnycastClassVarGenericLiteralNoReturnOptionaloverload	TypeAlias	TypeGuardTypeVarUnion)	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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 )ReInplaceTriggerr4         N)__name__
__module____qualname__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPSrN       X/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/_dynamo/utils.pyr\   r\      s        LLJJJre   r\   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triggerr\   bytesintreturnNonec                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_bytes_ri   name)clsrj   rk   s      rf   add_missed_bytesz"ReinplaceCounters.add_missed_bytes   sA    A::K6667775@77777 :re   countc                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_tensors_rr   )rt   rj   rv   s      rf   add_missed_opportunitiesz*ReinplaceCounters.add_missed_opportunities   sA    A::K8',88999UB99999 :re   c                8    | j                                          d S N)ri   clearrt   s    rf   r}   zReinplaceCounters.clear   s    re   c                j    d}t           D ](}|| j                            d|j         d          z  })|S rx   r\   ri   getrs   rt   sumrj   s      rf   get_total_missedz"ReinplaceCounters.get_total_missed   sD    ' 	H 	HG3;??#CW\#C#CQGGGCC
re   c                j    d}t           D ](}|| j                            d|j         d          z  })|S rp   r   r   s      rf   get_total_missed_bytesz(ReinplaceCounters.get_total_missed_bytes   sD    ' 	F 	FG3;??#A7<#A#A1EEECC
re   c                D    | j         rt          dd| j                    d S d S )Ninductorreinplace_counters)ri   r*   r~   s    rf   logzReinplaceCounters.log   s6     ; 	J:';S[IIIII	J 	Jre   N)rj   r\   rk   rl   rm   rn   )rj   r\   rv   rl   rm   rn   rm   rn   rm   rl   )r_   r`   ra   collectionsdefaultdictrl   ri   __annotations__classmethodru   rz   r}   r   r   r   rN   re   rf   rh   rh      s        1H1H1M1MGMMMM A A A [A
 C C C [C    [    [    [ J J J [J J Jre   rh   rows-Union[list[tuple[str, Any]], list[list[Any]]]headers!Union[tuple[str, ...], list[str]]rm   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     rf   	<genexpr>ztabulate.<locals>.<genexpr>   sG       
 
),DIIc#smm$$
 
 
 
 
 
re   )tabulateImportErrorr   	itertoolschain)r   r   r   s      rf   r   r      s    
  w 777 
 
 
yy 
 
09	40P0P
 
 
 
 
 	
 	
 	

s    =AArn   c                     t           dz   a d S Nr4   )
curr_framerN   re   rf   increment_framer     s    aJJJre   c                 n    t                                            t                                           dad S Nr   )rZ   r}   rX   r   rN   re   rf   reset_frame_countr     s/    ""$$$""$$$JJJre   !Optional[list[Callable[[], str]]]_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    rf   (register_hook_for_recompile_user_contextr     s*      '#% ##D)))))re   c                     t           S r|   r   rN   re   rf   #get_hook_for_recompile_user_contextr     s    ##re   c                 
    da dS )z@Clear any registered recompile user-context hooks (test helper).Nr   rN   re   rf   reset_recompile_user_contextsr   #  s      $re   cntrl   c                    t           | z  a d S r|   )op_count)r   s    rf   increment_op_countr   ,  s    OHHHre   c                     i } t                                           D ]\  }}|dz  | |<   |                     dd          |                     dd          z   | d<   | S )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rZ   itemsr   )total_by_keyphasetimings      rf   calculate_time_spentr   3  sx    L17799 + +v$slU&2&6&6' '2A66'7L"# re   c            	         t                      } d}|                                 D ]\  }}| d| dt          |d           }t          |           d S )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       rf   print_time_reportr   E  se    '))L
C"((** / /
U..s..U5!__..	#JJJJJre   r    c                     t          t          d          st          t                    t          _        t          j        S )Nmetrics_contexton_exit)hasattr_metrics_context_tlsr    record_compilation_metricsr   rN   re   rf   get_metrics_contextr   g  s<    '):;; 
/=.0
 0
 0
,  //re   r!   c                     t          t          d          st          t                    t          _        t          j        S )Nruntime_metrics_contextr   )r   r   r!   r   r   rN   re   rf   get_runtime_metrics_contextr   o  s<    ')BCC 
7L.8
 8
 8
4  77re   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
    r4   r]   r^   N)r_   r`   ra   __doc__CHROMIUMPT2_COMPILECOMPILATION_METRICrN   re   rf   r   r   w  s,          HKre   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   metadatarV   time_nsOptional[int]	log_levelr   rm   rn   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         rf   r   z$CompileEventLogger.log_instant_event  s     ?lnnG022,555$)!!.:::$(!!|   	&&+@	
 	
 	
 	
 	
re   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           rf   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?????re   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       rf   add_toplevelzCompileEventLogger.add_toplevel  s[     .//CCEE	i   	#Iy)PPxPPPPPre   r   r   rl   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          rf   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:::::re   r4   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        rf   increment_toplevelz%CompileEventLogger.increment_toplevel  s]     122 4466	q   	$$Y	3FFFFFre   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          rf   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;;;;;re   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        rf   add_to_set_toplevelz&CompileEventLogger.add_to_set_toplevel?  s]     122 4466	q   	%%iCGGGGGre   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     rf   chromiumzCompileEventLogger.chromiumS  sA     	#,5	
 	
AF	
JR	
 	
 	
 	
 	
re   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     rf   pt2_compilezCompileEventLogger.pt2_compile]  sA     	#,8	
 	
DI	
MU	
 	
 	
 	
 	
re   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 )=NrN   r   kvs      rf   r   z>CompileEventLogger.add_record_function_data.<locals>.<genexpr>w  s0      $M$MDAqZZAZZ$M$M$M$M$M$Mre   z_data: N)torchautogradprofiler_is_profiler_enabledr   r   record_function)r   r   metadata_strs      rf   add_record_function_dataz+CompileEventLogger.add_record_function_dataj  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     rf   compilation_metricz%CompileEventLogger.compilation_metric}  s8     	' 3Y	
 	
BJ	
 	
 	
 	
 	
re   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      rf   instantzCompileEventLogger.instant  s3     	,,'+?+H	
 	
 	
 	
 	
re   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      rf   try_add_pt2_compilez&CompileEventLogger.try_add_pt2_compile  sC     )** 	F022''
??h?????re   	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       rf   try_zCompileEventLogger.try_  sV    
 )** 	F-//**,, 	F	4"6"""""re   )
r   r   r   rV   r   r   r   r   rm   rn   F)
r   r   r   r   r   r   r   r   rm   rn   )r   r   r   r   r   r   rm   rn   )
r   r   r   r   r   r   r   rl   rm   rn   )r   r   r   rl   r   r   rm   rn   )
r   r   r   r   r   r   r   r   rm   rn   )r   r   r   r   r   r   rm   rn   )r   r   r   r   rm   rn   )r   r   r   r   rm   rn   r|   )r   r   r   rV   r   r   rm   rn   )r"  r#  r$  r%  r&  r'  rm   rn   )r_   r`   ra   r   staticmethodr   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r!  r)  rN   re   rf   r   r     sC        0  "&*>*G	
 
 
 
 \
*   +@ +@ +@ +@ \+@Z ;@Q Q Q Q \Q ; ; ; \;<  *>*QG G G G \G  < < < \<<  +?*QH H H H \H& 
 
 
 \
 

 

 

 \

    \$ 	
 	
 	
 	
 \	
 LP	
 	
 	
 	
 \	
 	@ 	@ 	@ \	@ 	# 	# 	# \	# 	# 	#re   r   rs   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__)rs   rfs     rf   compile_time_record_functionr4    s       ~3 X33D99
	*EEEKKdD)))))BKKdD))))s   A/ /BFr   
phase_nameOptional[str]r   r   r   Optional[dict[str, object]]dynamo_compile_column_us
compile_idOptional[CompileId]is_backwardOptional[bool]log_waitcounterwaitcounter_name_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_namer;  z (dynamo_timed)zpytorch.wait_counter._usdepthr   r4   z#pytorch.wait_counter.dynamo_compilez.pytorch.wait_counter.compile_runtime_overheadsr     duration_usT)r9  
is_runtime
is_forward)rX   r   r   r   r   log_event_startr4  r   r-   guardr  _guardsCompileContextcurrent_compile_idendswithr   _dynamo_timed_tlsrB  
contextlib	ExitStackenter_contextlog_event_endrZ   r   r   r   r   )r   r5  r   r   r8  r9  r;  r=  r>  r   r@  metricsevent_metadatar   start_nscx_mgrswc_nameis_compile_time
runtime_wcstackcxend_nstime_spent_nsis_outer_eventrD  r   runtime_contextextras                               rf   dynamo_timedr`    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|   rN   ra  rc  s     rf   compile_timesrf  Z  s    ILre   Literal['csv']tuple[list[str], list[object]]c                    d S r|   rN   re  s     rf   rf  rf  ^  s	     &)Sre   -Union[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]rm   r   c                ~    r |t          |                     S d                    t          ||                     S )Nr   )r   r   r   )rl  rn  rc  s     rf   fmt_fnzcompile_times.<locals>.fmt_fnu  s=     	(73v;;'''yyWf--...re   c                F    g | ]}| t           |         d            fS )c                
    | dS )Nz.4frN   xs    rf   <lambda>z*compile_times.<locals>.<listcomp>.<lambda>|  s
    jj re   rn  )rX   )r   r  rq  s     rf   
<listcomp>z!compile_times.<locals>.<listcomp>{  sG     
 
 
 /2<P<PQQQR
 
 
re   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                ,    g | ]} |d            S )c                
    | dS )Nz.6frN   rt  s    rf   rv  z*compile_times.<locals>.<listcomp>.<lambda>  s
    1

 re   rw  rN   )r   r  rq  s     rf   rx  z!compile_times.<locals>.<listcomp>  s<     
 
 
 F122333
 
 
re   N)rl  rm  rn  ro  rm   r   )r   rX   r   rl  listkeys)ra  rc  r   r   rl  r   rq  s    `    @rf   rf  rf  e  s      GJ / / / / / / /
 u}}
 
 
 
-
 
 
 3x&BCCCC
	
 
 
 
-4466
 
 
 /4466774re   c                 X    t                               t          dd                     d S )Nr   Tre  )r   inforf  rN   re   rf   dump_compile_timesr    s'    HH]66677777re   c                  (    e Zd ZdddZddZddZdS )DuplicateWarningChecker   maxsizerl   rm   rn   c                <    || _         |                                  d S r|   )r  reset)selfr  s     rf   __init__z DuplicateWarningChecker.__init__  s    

re   c                ,    t                      | _        d S r|   )r   setr  s    rf   r  zDuplicateWarningChecker.reset  s    *5--re   r   !Union[str, tuple[object, object]]r   c                ,   || j         v r+| j                             |d           t          j        sdS n_d | j         |<   t	          | j                   | j        k    r8| j                             d           t	          | j                   | j        k    8dS )NT)lastF)r  move_to_endrL   verboselenr  popitemr  r   s     rf   addzDuplicateWarningChecker.add  s    $(??H  4 000> u !DHSMdh--$,..  e ,,, dh--$,..tre   N)r  )r  rl   rm   rn   r   )r   r  rm   r   )r_   r`   ra   r  r  r  rN   re   rf   r  r    sU            8 8 8 8	 	 	 	 	 	re   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_handlerrN  rO  )compile_debugs    rf   setup_compile_debugr    s@    JNN#8#>>#EM "!!!!!!re   c                 8    t                                            d S r|   )graph_break_dup_warning_checkerr  rN   re   rf   reset_graph_break_dup_checkerr    s    #))+++++re   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.LogRecordrm   r   c                |    t                                          |          }t                              d|          S )N )superformatANSI_ESCAPE_PATTERNsub)r  r  msg	__class__s      rf   r  zStripAnsiFormatter.format  s.    ggnnV$$"&&r3///re   )r  r  rm   r   )r_   r`   ra   r   r  __classcell__r  s   @rf   r  r    sC        ::0 0 0 0 0 0 0 0 0 0re   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   rf   rv  z"add_file_handler.<locals>.<lambda>  s    v334DEE re   )r  pathr   get_debug_dirmakedirsloggingFileHandlersetFormatterr  	getLogger
addHandlerrN  rO  callback)log_path	exitstackr  r  s     @@rf   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re   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   rf   rv  z setup_log_file.<locals>.<lambda>  s    v';';<L'M'M re   )rN  rO  rL   log_file_namer  r  r  _logging	_internalget_loggersr  r  )r  r  r  s    @@rf   setup_log_filer    s    $&&I'".v/CDDn.::<< 	O 	OF.///MMMMMNNNNre   exc	Exceptioncoder
   c                n    t                       d|j         dt          |           j         d|j         dS )Nz/error_recordings/_z.rec)r  co_nametyper_   co_firstlineno)r  r  s     rf   gen_record_file_namer    sR    oo ? ?? ?S		"? ?%)%8? ? ? ?re   filenameexec_recordrA   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      rf   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   callr4   )nodesop)r  cns      rf   count_callsr    s2    	AW  QT>>FAHre   ru  c                    | S r|   rN   rt  s    rf   identityr    s    Hre   r   c                `    	 t          |            dS # t          $ r Y dS t          $ r Y dS w xY wNTF)hash	TypeError
ValueErrorrt  s    rf   hashabler    sR    Qt   uu   uus    
-	--r$  r&  c                     d S r|   rN   r$  r&  s     rf   nothingr    s    Dre   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 equalityrm   rn   c                "    i | _         i | _        d S r|   rl  refsr  s    rf   r  zExactWeakKeyDictionary.__init__#  s    &(;=			re   r   r   c                6    | j         t          |                   S r|   )rl  idr  s     rf   __getitem__z"ExactWeakKeyDictionary.__getitem__'  s    {2c77##re   Ndefaultc                R    | j                             t          |          |          S r|   )rl  r   r  )r  r   r  s      rf   r   zExactWeakKeyDictionary.get*  s    {r#ww000re   r   c                .    t          |          | j        v S r|   )r  rl  r  s     rf   __contains__z#ExactWeakKeyDictionary.__contains__-  s    #ww$+%%re   r   c                     t          |           j        vr!t          j        | fd           j        <   | j        <   d S )Nc                .                                   S r|   )
_remove_id)refidxr  s    rf   rv  z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>3  s    $//#:N:N re   )r  r  weakrefr  rl  )r  r   r   r  s   `  @rf   __setitem__z"ExactWeakKeyDictionary.__setitem__0  sR    ggdi$[.N.N.N.N.NOODIcN Cre   r  rl   c                N    || j         v r| j         |= || j        v r
| j        |= d S d S r|   r  )r  r  s     rf   r  z!ExactWeakKeyDictionary._remove_id6  s;    $+C $)	# re   c                j    | j                                          | j                                         d S r|   )r  r}   rl  r  s    rf   r}   zExactWeakKeyDictionary.clear<  s.    	re   r   )r   r   rm   r   r|   )r   r   r  r   rm   r   )r   r   rm   r   )r   r   r   r   rm   rn   r  rl   rm   rn   )r_   r`   ra   r   r  r  r   r  r  r  r}   rN   re   rf   r  r     s        ff> > > >$ $ $ $1 1 1 1 1& & & &! ! ! !        re   r  objr   allowed_typestype[T]	TypeIs[T]c                    d S r|   rN   r  r  s     rf   istyper  A  s    >Acre   )tuple[type[list[T]], type[tuple[T, ...]]]c                    d S r|   rN   r  s     rf   r  r  E  s	     re   Iterable[type]c                    d S r|   rN   r  s     rf   r  r  K  s    @Cre   c                    t          |t          t          t          f          rt	          |           |v S t	          |           |u S )zisinstance() without subclasses)
isinstancetupler}  r  r  r  s     rf   r  r  O  s=    -%s!344 *CyyM))99%%re   ztuple[Any, ...]_builtin_final_typing_classesr^      inputs collections.abc.Sequence[object]modeltorch.fx.GraphModulelist[Optional[torch.device]]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     rf   rx  z&get_inputs_devices.<locals>.<listcomp>j  s2     3 3 3!@R@R	%@R@R@Rre   c                <    g | ]}t          |d           |j        S device)r   r*  r   is     rf   rx  z&get_inputs_devices.<locals>.<listcomp>n  s)    IIIGAx4H4HIAHIIIre   c                \    g | ])}t          |t          j                  r|j        d k    '|*S r%  )r  r  r*  r  r+  s     rf   rx  z&get_inputs_devices.<locals>.<listcomp>p  s?       z!U\::?@v?O?O?O?O?Ore   )pytreetree_flattenr}  graphr  r/   )r  r  
all_inputsdevicess       rf   get_inputs_devicesr3  f  s     $V,,Q/ 3 3%)%+*;%<%<3 3 3 J -1IIjIIIJJ- -G   	 re   )r^      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   r   r   s    rf   	is_typingr=  y  s`     7""z%9V'W'W"t55?FM:;; 	!FN"	!FL re   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    rf   is_numpy_int_typerI    sQ     uGHHHHIII		
  re   c                v    t           sdS t          | t           j        t           j        t           j        f          S r?  )r@  r  float16float32float64r<  s    rf   is_numpy_float_typerN    s8     uJJJ	
  re   Callable[..., T]*TypeGuard[functools._lru_cache_wrapper[T]]c                    d S r|   rN   r<  s    rf   is_lru_cache_wrapped_functionrR    s	     25re   ,TypeGuard[functools._lru_cache_wrapper[Any]]c                    d S r|   rN   r<  s    rf   rR  rR    s	     473re   c                z    t          | t          j                  o!t          t	          j        | d                    S )N__wrapped__)r  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr<  s    rf   rR  rR    s<     eY9:: {um44@ @ re   r   
_FuncTypesMTypeIs[Union[_FuncTypes, torch._ops.OpOverloadPacket, torch._ops.OpOverload]]c                    t          |           p/t          | t          j        j        t          j        j        f          S r|   )rY  r  r  _opsOpOverloadPacket
OpOverloadr<  s    rf   is_function_or_wrapperrb    s9     u 
+UZ-BC" " re   TypeIs[_FuncTypes]c                z    t          | t          j        t          j        t          j        t          j        f          S r|   )r  r8  FunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper<  s    rf   rY  rY    s6     %&'		
  re   )__eq____ne____lt____le____gt____ge__z==z!=<z<=>z>=TypeIs[Union[types.GetSetDescriptorType, types.MethodDescriptorType, types.WrapperDescriptorType, types.MemberDescriptorType, types.MethodWrapperType]]c                    t          | t          j        t          j        t          j        t          j        t          j        f          S r|   )r  r8  GetSetDescriptorTyperg  rh  MemberDescriptorTyper   r<  s    rf   is_wrapper_or_member_descriptorru    s>      &&'&#	
  re   fnc                ,    t          |           d         S r   ) unwrap_with_attr_name_if_wrapper)rv  s    rf   unwrap_if_wrapperry    s    +B//22re   tuple[Any, Optional[str]]c                    t          |           r/t          j        | dd          rt          j        | d|           } d}nd }| |fS )N_torchdynamo_inlineF)rY  rZ  r[  )rv  	attr_names     rf   rx  rx    sT     2 71"6KUSS #B(=rBB)			y=re   TypeGuard[np.ndarray]c                H    t           sdS t          | t           j                  S r?  )r@  r  ndarrayr<  s    rf   is_numpy_ndarrayr    s!     u%$$$re   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	ParameterrL   traceable_tensor_subclasses_subclassesrO   r  )r  tensor_lists     rf   istensorr  $  sO     	% 
	+% %K
 !2!= ??K#{###re   modc                ,    t          | t                    S r|   )r  r.   )r  s    rf   is_lazy_moduler  /  s    c?+++re   r  c                     t          |   d S r|   )r   r$  s    rf   
print_oncer  3  s    	4LLLLre   r$  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 closurerm   r   c                      S r|   rN   rt  s   rf   r  zmake_cell.<locals>.f<  s    re   Nr4   r   )rm   r   )__closure__r  )r$  r  ru  s     @rf   	make_cellr  8  sW    A      =$Q]););q)@)@)@@=re   &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     rf   r   z$proxy_args_kwargs.<locals>.<genexpr>E  s*      ::c3<<>>::::::re   c                >    i | ]\  }}||                                 S rN   r  )r   r   r  s      rf   
<dictcomp>z%proxy_args_kwargs.<locals>.<dictcomp>F  s&    KKKSS\\^^KKKre   r4   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}  rl  )r$  r&  
proxy_argsproxy_kwargser  r  s          rf   proxy_args_kwargsr  C  s    
::T:::::
KKFLLNNKKK<'' 

 

 

&&&&&&++++++<]77D>]]GGT&--//EZEZ<[]]Q	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	

s   := 
BABBr  Optional[float]r   c                .    | d nt          | dz            S NrC  rl   r  s    rf   	to_int_msr  U  s    944#a$h--/re   c                .    | d nt          | dz            S )Ni@B r  r  s    rf   	to_int_usr  [  s    944#a)m"4"44re   r^   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CompilationMetricsNr6  r9  	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Optional[set[str]]non_compliant_opscompliant_custom_opsrestart_reasonsdynamo_time_before_restart_szOptional[list[str]]stack_traceexception_stack_tracegraph_node_shapesr<  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_configrF  num_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usrD  !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_usrl   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Optional[dict[str, bool]]feature_usagecompile_time_autotune_time_usFrE  
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_provenancerR  rV   rm   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   rm   r  c                    | | dz  nd S )Ng    .ArN   r  s    rf   us_to_sz*CompilationMetrics.create.<locals>.us_to_s  s    #)#56C<<4?re   c                    | | dz  nd S r  rN   r  s    rf   us_to_msz+CompilationMetrics.create.<locals>.us_to_ms  s    %+%76T>>TAre   Optional[Any]r6  c                    d	d| d S t          | t          t          f          sdS d                    fdt	          |           D                       S )
Nitemr   rm   r   c                D    	 t          |           S # t          $ r Y dS w xY wN	<unknown>)r   r  )r  s    rf   safe_strzFCompilationMetrics.create.<locals>.collection_to_str.<locals>.safe_str  s5    't99$  ' ' '&;;'s    
r  ,c              3  .   K   | ]} |          V  d S r|   rN   )r   r  r   s     rf   r   zGCompilationMetrics.create.<locals>.collection_to_str.<locals>.<genexpr>  s+      FFtHHTNNFFFFFFre   )r  r   rm   r   )r  r  r}  r   sorted)r  r   s    @rf   collection_to_strz4CompilationMetrics.create.<locals>.collection_to_str  sk    ' ' ' ' ~tfsDk22 #"{88FFFFvf~~FFFFFFre   c                p    | d S 	 t          j        t          |                     S # t          $ r Y dS w xY wr  )jsondumpsr}  r  r  s    rf   collection_to_json_strz9CompilationMetrics.create.<locals>.collection_to_json_str  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  r9  N)r  r   rm   r  )r  r   rm   r   )r  r  rm   r6  rN   )r   r   )	rt   rR  r  r  r$  r(  legacy_metricsall_metricsr9  s	            rf   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!![!!!re   )rR  rV   rm   r  )ir_   r`   ra   r9  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  rF  r  r  r  r  rD  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  rE  r  r   r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  r  r  r  r  r   r+  rN   re   rf   r  r  d  s         $J$$$$#I####!G!!!!!%K%%%%$(N(((( $J$$$$,00000!%K%%%%+/////$(N((((&*****'+++++"&J&&&&377777.22222/33333'+O++++#I####!%K%%%%.22222,00000,00000/33333*.O....48 8888'+K++++155555'+++++ (,++++15555559!9999-111117;$;;;;'+++++#'M''''%)O))))!%J%%%%(,,,,,7;%;;;;7;%;;;;#'M''''!%K%%%%7;%;;;;=A+AAAA9='====BF0FFFF,0000026 666659#9999;?)????6:$::::48"88887;%;;;;7;%;;;;9='====!%K%%%%+/////,00000.2222200000%)O))))*.....89&99999:'::::;?)????7;%;;;;8<&<<<<78%888889&999989&99999:'::::78%888889&9999"&L&&&&$(N((((/3M333337!7777!&J&&&& $J$$$$.22222.22222266666(,,,,,&*****&*****48"8888"H""""+/////$(N((((7;%;;;;7;%;;;; "&K%%%% "&K%%%% "&K%%%%266666:?'????%)O)))).22222R" R" R" [R" R" R"re   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      rf   #add_compilation_metrics_to_chromiumr3  0  s    -..L1133J +	M'< 7M5'+-+M!"!; 7 *+)<)HD$%%%d -.,B,ND'(((TX ()'8'DD"###$%&%C+o;       re   c                 n    dd}  | t          j                              }t          j        |d          S )NdrV   rm   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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 rN   r  r  r#  r   r   r   	blocklists      rf   r  zJ_get_dynamo_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>y  M     
 
 
U)## *UC"8"8Ce###re   r   r5  rK  s    @rf   clean_for_jsonz6_get_dynamo_config_for_logging.<locals>.clean_for_jsona  sG    
 
 
	.
 
 
 
ggii
 
 
 	
re   T	sort_keysr5  rV   rm   rV   )rL   get_config_copyr&  r'  )rO  config_dicts     rf   _get_dynamo_config_for_loggingrU  `  sE    
 
 
 
< !.!7!9!9::K:kT2222re   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 )	Nr5  rV   rm   c                J    dhfd|                                  D             S )Nr:  c                l    i | ]0\  }}|v	|t          |t                    rt          |          n|1S rN   rI  rJ  s      rf   r  zH_compiler_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>  rL  re   rM  rN  s    @rf   rO  z4_compiler_config_for_logging.<locals>.clean_for_json  s@    
	
 
 
 
ggii
 
 
 	
re   z!Compiler Config cannot be pickledTrP  rR  )r  compilerrL   rS  r  AttributeErrorr&  r'  )rO  compiler_config_copyrT  s      rf   _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   rm   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     rf   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   rm   r   )r_   r`   ra   r  r  r  s   @rf   TypeSafeSerializerr_    s=        	8 	8 	8 	8 	8 	8 	8 	8 	8 	8re   rb  Nz!Inductor Config cannot be pickledT)rt   skipkeysrQ  z(Inductor Config is not JSON serializable)r&  JSONEncoderr  r  	_inductorrL   rS  r  rZ  r   AssertionErrorr   r  r   r  r}  r'  r  )rb  keys_to_scrubinductor_conf_strinductor_config_copyr   r$  s         rf   %_scrubbed_inductor_config_for_loggingrj    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_nsrR  exc_typeOptional[type[BaseException]]	exc_valueOptional[BaseException]c                @   t           j        j                                        r 	 ddlm} |}d}n# t          $ r d }d }Y nw xY wd }d }|                    d          }|s#t           j        j	        
                                }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t"          j        dt)                      dt+                      dt           j        j        dt1                      rt2          j        ndd|d|dt6          j        dt           j        }	t8                              i |	|          t<                                         d}
j         du rd|
z   }
j!        du r|
dz   }
t           j        "                    |
fdd|           tG                     t"          j$        rtK                     d S d S )Nr   )REMOTE_CACHE_VERSION_ManifoldCacher9  r  rC  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  compilation_metricsFbwd_T_runtimec                 d    d t          j                                                   D             S )Nc                b    i | ],\  }}|t          |t                    rt          |          n|-S rN   )r  r  r}  r  s      rf   r  z@record_compilation_metrics.<locals>.<lambda>.<locals>.<dictcomp>  sG     
 
 
1 *Q,,3tAwww!
 
 
re   )dataclassesasdictr   )rt  s   rf   rv  z,record_compilation_metrics.<locals>.<lambda>  s9     
 
#*+>??EEGG
 
 
 re   )record_logging_overheadr9  )&r  re  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cacherr  ModuleNotFoundErrorr   rI  rJ  rK  ra   r   r  r  get_structured_logging_overheadrU  rL   suppress_errorsinline_inbuilt_nn_modulesrj  r\  versioncudar1   triton__version__r6  r  r+  r0  r   rF  rE  trace_structuredr3  rB  r(   )rk  rl  rR  rm  ro  rr  r  r  r9  common_metricsrs   rt  s              @rf   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 	+F,L 	@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 )Nr.  )r  r0  popleftr   deque)r  	new_deques     rf   set_compilation_metrics_limitr    s`    
"
#
#h
.
.$$&&& "
#
#h
.
.!"6xHHHI$re   c                 8    t                                            d S r|   )r0  r}   rN   re   rf   clear_compilation_metricsr  %  s         re   list[CompilationMetrics]c                 *    t          t                    S r|   )r}  r0  rN   re   rf   get_compilation_metricsr  *  s    $%%%re   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.
    rm   	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.
        rY  )r   tlsrY  r  s    rf   	get_stackzChromiumEventLogger.get_stack7  s5    
 48W%% 	"8>!DHN8>!re   r6  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  rY  s     rf   r   z'ChromiumEventLogger.get_outermost_eventB  s&    
    *uQxxd*re   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    rf   r   z,ChromiumEventLogger.get_pt2_compile_substackJ  s9    
 48344 	1800,.DH)800re   rV   c                \    t          | j        d          si | j        _        | j        j        S )N
event_data)r   r  r  r  s    rf   get_event_dataz"ChromiumEventLogger.get_event_dataU  s+    tx.. 	%"$DHx""re   'dict[str, AbstractContextManager[None]]c                \    t          | j        d          si | j        _        | j        j        S )Nrecord_functions)r   r  r  r  s    rf   get_record_functionsz(ChromiumEventLogger.get_record_functionsZ  s,    tx!344 	+(*DH%x((re   rn   c                    t          j                    | _        ddlm} |j        r#|j         dt          j                     | _        n%t          t          j                              | _        t                              d| j                   d S )Nr4   rK   -z*ChromiumEventLogger initialized with id %s)	threadinglocalr  r  rL   pt2_compile_id_prefixuuiduuid4id_r   r   r  )r  rL   s     rf   r  zChromiumEventLogger.__init___  s    ?$$ ' 	) 6GGGGDHH4:<<((DH 	=txHHHHHre   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      rf   r   z&ChromiumEventLogger.try_add_event_datan  s?     T^^----FJ11&11111re   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   ra  r  r   )r  r   r&  r  s       rf   r   z"ChromiumEventLogger.add_event_datav  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz":%%f-----re   r   r   rl   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   ra  r  r  r   r   r   r  s        rf   r   zChromiumEventLogger.increment  s     T^^----Kj)) K K4>>3C3C K K K   ((**
Z''%'Jz"j,,,*+Jz"3':s###u,#####re   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   ra  r  r  r  r  s        rf   r  zChromiumEventLogger.add_to_set  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz"j,,,*-%%Jz"3':s#''.....re   FNr   r   r   r   r9  r:  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)
        r9  BN)r  rI  rJ  rK  r   _log_timed_eventr  r   r   r   r  r  r  r.  r/  r0  r1  r  r   r  )r  r   r   r   r   r9  r3  s          rf   rG  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re   c                   |                                  }|                                 }|                                 |                                 |                                 }|                                 |                                 }|rD|                                D ]}|                    d d d            |                                 d S d S r|   )r  r   r}   r  r  rl  r2  )r  rY  substackr  r  r3  s         rf   r  zChromiumEventLogger.reset  s       0022((**
4466 	%&--// . .D$----""$$$$$	% 	%re   rk  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)
        r9  ErY  r  rm   rn   c                    | d         k    r<t                               d           |                                  | d         k    :d S d S )Nz>ChromiumEventLogger: Detected overlapping events, fixing stack)r   r  pop)rY  r   s    rf   	pop_stackz4ChromiumEventLogger.log_event_end.<locals>.pop_stack  sY    b	)) T   		 b	))))))re   z7ChromiumEventLogger: Start event not in stack, ignoringN)rY  r  rm   rn   )r  rI  rJ  rK  r   r  r   r  r  r   r  r   r'   r  r  r  r2  )r  r   r   r   rk  r   r9  all_event_datarS  eventr  event_stackr   r  r3  s    `             rf   rQ  z!ChromiumEventLogger.log_event_end  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))))) *)re   r   Optional[dict[str, Any]]c                    ||dz  ||ddddt           j                            dfddd           t                     S )	za
        Logs a timed event in chromium format. See log_event_start, log_event_end, etc.
        rC  r`  r   )rs   tsr$  phcattidpidchromium_eventc                      S r|   rN   r  s   rf   rv  z6ChromiumEventLogger._log_timed_event.<locals>.<lambda>P      u re   F
payload_fnsuppress_contextexpect_trace_id)r  r  r  r)   )r  r   r   r   r   r  s        @rf   r  z$ChromiumEventLogger._log_timed_event:  sr     D.!	
 	
 	''$}}}"!	 	( 	
 	
 	
 	'u---re   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
        Nr9  rC  r,  r`  r   p)rs   r  r$  r  r  r  r  sr  c                      S r|   rN   r  s   rf   rv  z7ChromiumEventLogger.log_instant_event.<locals>.<lambda>w  r  re   FTr  )
r   r  rI  rJ  rK  r  r  r'   r   r  )r  r   r   r   r   r9  r  s         @rf   r   z%ChromiumEventLogger.log_instant_eventW  s     H5HHJJKK
!+D.!

 

 	''$}}}" 	 	( 	
 	
 	
 ! 	't4466'    	 	re   )rm   r  rm   r6  )rm   rV   )rm   r  r   )r   r   r&  r   rm   rn   )r   r   r   r   r   rl   rm   rn   )r   r   r   r   r   r   rm   rn   FN)r   r   r   rl   r   rV   r   r   r9  r:  rm   rn   r|   )r   r   r   rl   r   rV   rk  rl   r   r   r9  r:  rm   rn   )
r   r   r   rl   r   r   r   r  rm   rV   r?  )
r   r   r   rl   r   r  r   r   rm   rn   )r_   r`   ra   r   r  r   r   r  r  r  r   r   r   r  rG  r  rQ  r  r   rN   re   rf   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 .2    B .2&+( ( ( ( ( ( (re   r  zOptional[ChromiumEventLogger]CHROMIUM_EVENT_LOGc                 :    t           t                      a t           S r|   )r  r  rN   re   rf   r   r     s    !022re   c                     t           d uS r|   )r  rN   re   rf   r  r    s    T))re   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   rG  rQ  r  )r   r  r   chromium_event_logchromium_start_times        rf   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 calledrV   scoper   rs   r$  r   rm   rn   c                X    t           t           xj        dz  c_        | j        | j        = d S r   )CleanupManagerrv   r  rs   r  r$  s     rf   __call__zCleanupHook.__call__  s.    %  A%  Jty!!!re   r$  c                b    || vsJ t           xj        dz  c_        || |<   t          | |          S r   )r  rv   r  )r  rs   r$  s      rf   r+  zCleanupHook.create  s@    5    !d5$'''re   Nr$  r   rm   rn   )r  rV   rs   r   r$  r   rm   r  )r_   r`   ra   r   r   r  r+  r+  rN   re   rf   r  r    se         66III" " " " ( ( ( \( ( (re   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  rl   rm   rn   c                ~    | j         |         D ]} |             t                                          |           d S r|   )rl  r  r  )r  r  r   r  s      rf   r  zCleanupManager._remove_id  sD    K$ 	 	DDFFFF3re   r  )r_   r`   ra   rv   r   r  r  r  s   @rf   r  r    sP         E&&&&                   re   r  torch.Tensorc                    |                                                      | j                  }| j        r%| j        | j                                         |_        |S )z!Clone the tensor and its gradient)clonerequires_grad_requires_gradis_leafgrad)ru  ys     rf   clone_tensorr    sF    			  11Ay  QV'Hre   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      rf   _copy_dynamo_attrr    st    sD T73--224455555	d		 T re   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     rf   copy_dynamo_tensor_attributesr    s|     c3 9:::c3 :;;;c3 8999c3 3444c3 ABBBc3 >?????re   dtyper  Optional[torch.dtype]c          	     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 stridesru  r  rm   c                    t          j        |           }| j        r|                    | j                   | j        r"| j        t          | j                  |_        t          | |           |S )Nr  )r  r  r  r  r  r  clone_inputr  )ru  r  r  s     rf   torch_clonez clone_input.<locals>.torch_clone  sl    KNN9 	.Q_---9 	6+ u555AF%a+++re   xlaN)is_coalesced)layoutc              3  ,   K   | ]\  }}|d z
  |z  V  dS )r4   NrN   )r   shapestrides      rf   r   zclone_input.<locals>.<genexpr>%	  s@       
 
%2UFUQY& 
 
 
 
 
 
re       )r  r*  r  ru  r  rm   r  )*rP   r  no_gradr*  r  r  
sparse_coosparse_coo_tensor_indicesri   r  r  r%   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorrl  r0   r   zipsizer  is_quantizedempty_quantizedemptyr  data_ptrelement_sizeas_strided_copy_r  r  r  r  r  r	  r   r  )ru  r  r
  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      rf   r	  r	    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, Union[T, tuple[T, ...]]]dict[str, list[T]]c                    d S r|   rN   r,  s    rf   clone_inputsr1  A	  s	     re   Sequence[T]list[T]c                    d S r|   rN   r0  s    rf   r1  r1  G	  s    :=#re   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  r1  r  r  r	  r}  ranger  )r,  resr   r   r,  s        rf   r1  r1  K	  s    Nt##>""))++ 	. 	.JC%'' .'..C!%66CCUCC6&u--C

~

C3s88__ ) )c!fel++ 	) Q((CFJre   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)
Nr4   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)r#  r   r  r  rer  ra  )r$  r  r  functorch_subclass_names       rf   skip_frame_if_in_functorch_moder=  ^	  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  r.  _DisableFuncTorchr|  _python_dispatch_disable_current_modesr  randomget_rng_stater=  r  is_availablexpuset_rng_state)disable_functorchdisable_current_modes	rng_statecuda_rng_statexpu_rng_states        rf   preserve_rng_staterM  q	  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[Union[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)rN  s    rf   is_jit_modelrX  	  sD     I1I3I$I"		
  re   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)	rX  r  rQ  tracer  scriptr   r  error)r  r,  r  s      rf   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|   )rZ  getfiler  OSErrorr  s    rf   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  ra  s    rf   is_namedtuplerd  	  s    T#YY'''re   rt   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-namedtupler`   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  ,   K   | ]}|t           u|V  d S r|   )r   )r   ts     rf   r   z$is_namedtuple_cls.<locals>.<genexpr>	  s,      KKA!7:J:Ja:J:J:J:JKKre   c              3  R   K   | ]!}t          |          oj        |j        u V  "d S r|   )rc  __new__)r   rj  rt   s     rf   r   z$is_namedtuple_cls.<locals>.<genexpr>	  sR       ! ! 	 *!,, 5K194! ! ! ! ! !re   F)
issubclassr  r  r  callable	__bases__anyr  )rt   modulebasess   `  rf   rc  rc  	  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steprg  c                      e Zd ZU ded<   dS )!namedtuple_fields.<locals>.Markerrl   indexNr_   r`   ra   r   rN   re   rf   Markerry  	  s         




re   r|  rf  r   r  )r   )slicerm  r  r   rg  ry  	dataclassr`   r   r7  n_fieldsdirr  r  rz  r  r#  r   )rt   r|  r  fieldsrs   s        rf   namedtuple_fieldsr  	  s>    e||((c5!!!!!sI {        >11111
#c&%--..
/
/CFC 4 47c>>jd););VDD>"3--3F4Lv;;#,&&&&FJ///000re   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 rN   )_versionr  r  )r   params     rf   rx  z%checkpoint_params.<locals>.<listcomp>	  s=     
 
 
  ENEK$6$67
 
 
re   rm   rn   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  r  rC  rG  r  rE  r  r&  )r  r  original_valuerK  rJ  saved_states      rf   restorez"checkpoint_params.<locals>.restore	  s    ]__ 	0 	0L&&y111z&&(( 9
((8882= 0 0.w>W,,KK///0		0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   BB))B-0B-r   )r  r  r  rC  rD  r  rE  r   r   
parametersbuffers)r  r  rK  rJ  r  s     @@@rf   checkpoint_paramsr  	  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  rE  synchronizer  gccollectmanual_seedr   perf_counterr7  )r  r,  r  r  t0r  r*  t1s           rf   timedr  

  s     z   j,KMMMJLLL	d				B5\\  '				B27?re   c           	         t          d t          j        ||                     d                    D                       S )Nc              3  $   K   | ]}|j         V  d S r|   )is_cudar   ru  s     rf   r   z check_is_cuda.<locals>.<genexpr>
  s$      WWQqyWWWWWWre   T)allr   r   r  )r  r,  s     rf   check_is_cudar  
  s8    WW)/."--PTBUBU"V"VWWWWWWre   r  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 )
Nr4   c                    g | ]}d | S r  rN   r+  s     rf   rx  z rot_n_helper.<locals>.<listcomp>$
  s    &&&GGG&&&re   r  zlambda r!  z: ()rot__helper)r7  reversedevalr   r_   )r  varsrotatedrv  s       rf   rot_n_helperr  !
  s    q5555&&U1XX&&&DtBCCy49,--G	???388G+<+<???	@	@B####BKIre   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:  _GenericAliasr8  GenericAliasr  r}  r  s    rf   r  r  N
  s    a%#$$ -3'++,,,	IJ 	
	 	  
	'
 
re   set[int]c                 
    ddhS Nr   r4   rN   rN   re   rf   common_constantsr  `
  s    
 	
		 re   -TypeGuard[Union[torch.SymBool, torch.SymInt]]c                    t          | t          j        t          j        f          o.t          | j        t          j        j        j        j                   S r|   )	r  r  SymBoolSymIntr'  nestedr  
nested_intNestedIntNoder<  s    rf   is_torch_symr  j
  sH    eemU\:;; J
EL*5CE E A re   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 )Nr4   )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_modulerL   allow_unspec_int_on_nn_module"is_unspecialized_builtin_nn_moduleis_unspecialized_nn_moduler&   r  )r   r  r  s      rf   is_int_specialization_caser  p
  s    ((((((!##GG ((*** 	

 88:: 988	
 BBDD 988	
 ::<< 988	
  F##!	
(   WXXX ,)+++/re   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 )Nr4   )ConstantVariableLazyVariableTrackerSymNodeVariable)	variablesr  r  r  r  is_realizedoriginal_sourcer  r  rL   specialize_intr  rl   r  r+  evaluate_expr)r  r  r  r  r  r   is_symnode_vts          rf   specialize_symnoder  
  s    QQQQQQQQQQ #*++ COO4E4E  $$&&""$$$U++ 
%% >Us">.uf=== 	  	J#'' <&&s'8'8':':;;;Jre   c                    ddl m} t          |           } t          | |          r(|                                 r|                                 S | S )Nr4   rD   )r  rE   r  r  is_python_constantas_python_constant)r  rE   s     rf   guard_if_dynr  
  s]    ******
S
!
!C#'' (C,B,B,D,D (%%'''Jre   Mapping[Any, Any]c                ~    t          d t          j        | |                                          D                       S )Nc              3  >   K   | ]}|                                 V  d S r|   )r  r  s     rf   r   z&check_constant_args.<locals>.<genexpr>
  s.      VV!q##%%VVVVVVre   )r  r   r   rl  r  s     rf   check_constant_argsr  
  s3    VVytV]]__/U/UVVVVVVre   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 )Nr4   rD   UnspecializedPythonVariabler   F)	r  rE   variables.tensorr  r   r   rl  r  r  )r$  r&  rE   r  unspec_countru  s         rf   check_unspec_python_argsr  
  s    ******======L_T6==??33  a455 	ALLQ00 	Q5I5I5K5K 	55	!re   c                    ddl m} t          j        | |                                          D ])}|                                st          ||          s dS *dS )Nr4   r  FT)r  r  r   r   rl  r  r  )r$  r&  r  ru  s       rf   check_unspec_or_constant_argsr  
  sl    
 >=====_T6==??33  $$&& 	*Q8S*T*T 	554re   c                    ddl m t          fdt          j        | |                                          D                       S )Nr4   )NumpyNdarrayVariablec              3  8   K   | ]}t          |          V  d S r|   )r  )r   ru  r  s     rf   r   z+check_numpy_ndarray_args.<locals>.<genexpr>
  sB         	1*++     re   )r  r  rp  r   r   rl  )r$  r&  r  s     @rf   check_numpy_ndarray_argsr  
  sa    666666    v}}77     re   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 rN   rn  r   methods     rf   	<setcomp>r  
  s6       
  re   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  %    NNN&Xf=M=MNvNNNre   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  s5       8H8H
  re   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  s%    RRRF&AQAQRRRRre   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  s%    PPP6x?O?OPPPPre   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  r  re   c                0    h | ]}t          |          |S rN   r  r  s     rf   r  r  
  s5       hv>N>N
  re   KVr5  
dict[K, V]KeysView[K]c                d    t          | t                    sJ t                              |           S r|   )r  r6  r~  )r5  s    rf   builtin_dict_keysr  
  s(    a99Q<<re   !Iterable[tuple[K, Union[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 rN   )r   r  r   r  r  s     rf   rx  z'get_items_from_dict.<locals>.<listcomp>  s+    TTTK+C334TTTre   c                J    g | ]}|t                               |          f S rN   )r6  r  r  s     rf   rx  z'get_items_from_dict.<locals>.<listcomp>  s.    FFF!D$$S!,,-FFFre   )r  r6  r  r   r   r~  ra  s   `rf   get_items_from_dictr	    s    c4     cD+&'' Gyy{{	C	%	% GTTTTk>Ns>S>STTTT GFFFtyy~~FFFFre   c                l    t          |           }t          j        j                            |           |S r|   )
object_newr  r  Moduler  )rt   r  s     rf   nn_module_newr    s*    
S//C	HOS!!!Jre   itIterable[T]c                B    t          j        t          j        | d          S r   )rW  reduceoperatormul)r  s    rf   productr    s    HL"a000re   rz  c                L    |                                  \  }\  }}|||z            S r|   )
__reduce__)r  rz  r  r  ru  s        rf   tuple_iterator_getitemr    s'    }}Avuuu}re   c                d    t          j                            t          j                  |           S r|   )r  _dynamodisablery  r  r~   s    rf   dataclass_fieldsr  "  s#    =  !344S999re   
range_itertuple[int, int, int]c                    |                                  \  }\  }}|j        |pd|j        z  z   }|j        }|j        }|||fS r   )r  ru  rw  rv  )r  r  	range_obj	maybe_idxru  rv  rw  s          rf   normalize_range_iterr!  )  sP    !+!6!6!8!8A|	Y Oy~A??E>D>D4re   rj  c                ,    |                      |          S r|   )as_subclass)rj  rt   s     rf   to_subclassr$  7  s    ==re   dict[Any, Any]c                    t           }t          | t                    rt          }t          t	          j        |                    |           ||dz                       S r   )r6  r  r   nextr   islicer~  )r5  r  
dict_classs      rf   dict_keys_getitemr*  >  sM     J![!! ! 
	 !3!3QA>>???re   r  set[T]c                ,    t          |           |         S r|   r}  )r  r  s     rf   set_getitemr.  H  s    771:re   r  c                L    | j         j        }| j        }|rdnd}| d| d| }|S )NLGz["z"].)r  r_   rs   )r   r  rs   r$  r  
local_names         rf   	enum_reprr3  M  sD     ?#D
*C!CCcE++T++c++Jre   r'  torch.fx.Nodeexample_valuec                    || j         d<   t          j                    j        }|J |j        }t
          j        j        j        	                    ||          x}r|| j         d<   d S d S )Nr5  unbacked_bindings)
r&  r$   r   	fake_mode	shape_envr  r   experimentalsymbolic_shapescompute_unbacked_bindings)r'  r5  r8  r9  symbol_to_paths        rf   set_example_valuer>  W  s     "/DIo"$$.I   #I 8 0JJ}
 
	
8 *8	%&&&8 8re   vtrE   c                    |                                  j        j                            d          }t	          |          s/ddlm} ddlm}  |dt          |          dg |j
                   |S )Nr5  r4   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  r'  r&  r   rP   r  rB  r  r  r   
DYNAMO_BUG)r?  fake_tensorrB  r  s       rf   _get_fake_tensorrF  k  s    ++--$)--o>>K; 	
''''''&&&&&&P$$I1%01		
 	
 	
 	
 re   r}  seq_lenc                |    |                      |          \  }}}t          d||z
  ||dk    rdndz
  z   |z            S )Nr   r4   r  )indicesmax)r  rG  ru  rv  rw  s        rf   slice_lengthrK  z  sI    		'**E4q4%<4q11b#ABtKLLLre   r  txrC   expectactualc                n    ddl m} ddlm} d| d| d| } |t          |  ||          g           d S )Nr   )raise_observed_exceptionr  z3wrong number of arguments or keyword arguments for z() call.
  Expect: z
  Actual: r  )torch._dynamo.excrP  torch._dynamo.variablesr  r  )rL  rs   rM  rN  rP  r  msg_strs          rf   raise_args_mismatchrU    s     ;:::::888888	d 	 		 		 	  
w''(     re   r   searchrB   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 )Nr4   rQ  c              3     K   | ]B}|                                 o)|                                                                k    V  Cd S r|   )r  r  )r   ru  rV  s     rf   r   z iter_contains.<locals>.<genexpr>  sk       
 
    "" F$$&&&*C*C*E*EE
 
 
 
 
 
re   FTr   )SourcelessBuilder)r  r  r  rp  r+  	is_tensorrF  torch._dynamo.variables.builderrZ  r  eqcall_functionor_)r   rV  rL  rW  r  found_constmust_check_tensor_idfoundru  rZ  checks    `         rf   iter_containsrd    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  } ''..Lre   r  ?TypeIs[Union[torch.Tensor, torch.nn.Module, MethodWrapperType]]c                d    t          | t          j        t          j        j        t
          f          S )z4Returns whether it indexes dictionaries using its id)r  r  r  r  r  r   )r  s    rf   	key_is_idrg    s"     a%,9JKLLLre   	list[Any]c                    d | D             S )Nc                N    g | ]"}t          |          rt          |          n|#S rN   )rg  r  )r   r  s     rf   rx  zkey_to_id.<locals>.<listcomp>  s-    888QYq\\(BqEEEq888re   rN   r<  s    rf   	key_to_idrk    s    88%8888re   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 )Nr4   )is_builtin_callabler!  c              3  :   K   | ]}t          |           V  dS r  N
const_reprr   r  r  s     rf   r   zconst_repr.<locals>.<genexpr>  s0      DDQj%888DDDDDDre   [](z,)r  rp  'r  r`  r   rm   r   c                R    | j         }|j        }|dk    r|j        S |dz   |j        z   S )Nbuiltins.)r  r`   ra   )r`  klassrq  s      rf   fullnamezconst_repr.<locals>.fullname  s7    KE%F##))C<%"444re   )r`  r   rm   r   )trace_rulesrm  r  r}  r  r   r  r  r  r3  replacer_   r  )ru  r  rm  
elems_reprr|  s    `   rf   rr  rr    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re   
const_keysc               V    d                     fd| D                       }d|z   dz   S )Nr!  c              3  :   K   | ]}t          |           V  dS ro  rq  rs  s     rf   r   z!dict_keys_repr.<locals>.<genexpr>  s0      GGq
1E222GGGGGGre   rt  ru  r   )r  r  keys_strs    ` rf   dict_keys_reprr    s8    xxGGGGJGGGGGH>Cre   
__dict_key)UnsupportedFakeTensorExceptionrootc                D    | dt          |           d| j        j         S )Nr  _c)r  outputr9  )rL  r  r  s      rf   get_safe_global_namer    s+     66RWW66	 4666re   r  
containersContainer[T]c                    |D ]	}| |v r dS 
dS r  rN   )r  r  	containers      rf   is_inr    s-      	944 5re   )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   rv   rf  )r  r  r  r,  	candidates        rf   get_unique_name_wrtr  
  s      5#=*#=#=#= _  OOOO	Y,,,, 		 
'
''re   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)Nr4   r  zEncountered exception (z!) during fake tensor propagation.z!Fake tensor propagation exceptionr  )r  r  r  reasonr   r  r   )rv  r  r  r  s       rf   wrap_fake_exceptionr    s    
rtt) 
 
 
&&&&&&SSSSC7MM	
 	
 	
 	
 	
 	
 	
 	
 	
 	

s   	 
A.AA))A.r8  ,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  deepcopyra  s   rf   rv  z)deepcopy_to_fake_tensor.<locals>.<lambda>1  s    4=+=+= re   )r  r  rE  FakeCopyModer  )r  r8  s   ` rf   deepcopy_to_fake_tensorr  -  s     
		&	3	3I	>	> ? ?"#=#=#=#=>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   AA	A	r  r8  c                x    t          j        t          j        t          j        | |z
                                S )z+
    Calculate root mean squared error
    )r  sqrtmeansquare)r  r8  s     rf   rmser  4  s,     :ejcCi!8!899:::re   	equal_nanc                (    t          | |d|          S )Ng        )tolr  same)r  r8  r  s      rf   bitwise_samer  ;  s%    	   re   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       rf   r   zsame.<locals>.<genexpr>`  sx       ,
 ,
" "B	! $#9a%9!1+  ,
 ,
 ,
 ,
 ,
 ,
re   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 rj  r   rm   r  c                b    t          | t          j                  r| nt          j        |           S r|   )r  r  r  tensorrj  s    rf   	to_tensorzsame.<locals>.to_tensor  s%    "1el33H11aHre   c              3  .   K   | ]} |          V  d S r|   rN   )r   r$  r  s     rf   r   zsame.<locals>.<genexpr>  s+      MMiinnMMMMMMre   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       @rC     r  r4   g{Gz?)r  r  rK  bfloat16numelndimr  )
multiplierr  r  r  r8  r  r  s    rf   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(??
 &)
%%re   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  r8  r  r  s     rf   r   zsame.<locals>.<genexpr>  s       
 
  S!!S!!#&&-#'%9+#9a  
 
 
 
 
 
re   zunsupported type: )rj  r   rm   r  )rm   r  )=r  r}  r  r   r  r  r  ParameterListr  r  r  r  r  r_   r  lossr6  r  r~  r#  r  r  r  rO   	is_sparseto_denser  r   r   r  rl   r  allclosetorE  flattenrL  
functionalcosine_similarityr   r  detachcpurM  isnanrp  r  mathre  rL   cppinject_relu_bug_TESTING_ONLYr  r   r*  rI  rN  r  	as_tensor__dict__r   )r  r8  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   ``````````````            @rf   r  r  D  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Ere   c                n    | j                             d          d         }d| j         d| d| j         dS )N/r  rw  z' (r   r  )r  splitr  r  )r  short_filenames     rf   format_func_infor    sC    %++C004NGt|GGGG1DGGGGre   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|   )rL   recompile_limitr6  r  accumulated_recompile_limit)priorprior_acc_limits     rf   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     rf   _get_debug_dirr    sm     	




!
!
*
*+A
B
B	C 	 bikk

		  7<<(+++re   c                 8    t           j        } t          |           S r|   )rL   r;  r  )
debug_roots    rf   r  r    s    &J*%%%re   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 )
Nr5  r   r  r4   rA  z Missing FakeTensor example valuez,`FakeTensor` example value was required for z but not available.rC  )r&  rP   rR  r  r  rB  r   rD  )r'  r  r  rB  s       rf   extract_fake_example_valuer    s    $)##	/0J(K(K#y))	 333333''''''6II`t```1%01		
 	
 	
 	
 	
 	
 tre   r  c                6    t          |           |j        u sJ | S r|   )rQ   r8  )r  rL  s     rf   ensure_graph_faker    s"    q!!R\1111Hre   r  allow_non_graph_fakec                ^     d fd}t           j        j                            ||          S )Nr  r4  rm   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^  r5  get_attr)r  r&  get_fake_valuetargetr  
nn_modulesr  r  r   GraphModuler  r  )r  r  r   r  rL  s      rf   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"---
re   )r  r4  rm   r   )r  r   r'  map_arg)rL  r  r  r   s   ` ` rf   get_fake_values_from_nodesr"    sC          " 8=  ...re   r  Optional[FakeTensorMode]c                    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   rm   r   c                   d|                      d           t          fd                                D             d           }t          |t          f          rd dt          |           dS |                      d          S )Nr  r4   c              3  8   K   | ]\  }}|j         k    |V  d S r|   )rs   )r   r  r  sym_names      rf   r   zGget_concrete_sizes_from_symints.<locals>.replace_sym.<locals>.<genexpr>  s3      KK41a(8J8JQ8J8J8J8JKKre   rv  z	: hint = r  r   )groupr'  r   r  rl   r   )r&  r$  r)  r%  backed_var_to_vals     @rf   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~~re   )r&  r   rm   r   )r;  sympy.core.numbersr%  r9  r+  r  )r  r8  r;  patternr9  r,  r%  r+  s         @@rf   get_concrete_sizes_from_symintsr/    s     III******
G***#I!3       "&+s
+
+CJre   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          )Nr4   )TorchRuntimeErrorUnsupported
real_stackzshould be unreachable)r  r3  r4  r   r  with_traceback__traceback__rf  )r0  r3  r4  r  r  s        rf   (_wrap_graph_break_with_torch_runtime_errr8    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)r'  rL  r  _t0s       rf   r  r  $  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           nnt/          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   )ValueRangeErrorr4   rA  )r  r4  	UserErrorUserErrorTyper5  c                V    i | ]&}t          |          t          |          |j        'S rN   )rP   r  r  r  s     rf   r  z(_get_fake_value_impl.<locals>.<dictcomp>S  s=     !
 !
 !
&)!
sGGS\!
 !
 !
re   Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  @   K   | ]}t          |t                    V  d S r|   )r  complex)r   as     rf   r   z'_get_fake_value_impl.<locals>.<genexpr>p  sE       M M#$
1gM M M M M Mre   c              3     K   | ];}t          |t          j                  r|j        j        t          |          n|V  <d S r|   )r  r  SymFloatr'  hintr  r  s     rf   r   z'_get_fake_value_impl.<locals>.<genexpr>t  sc       
 
  c5>227:x}7P c


	
 
 
 
 
 
re   c                 4    t          j                   S r|   )run_noder  )r$  r&  nnmoduler'  rL  s   rf   rv  z&_get_fake_value_impl.<locals>.<lambda>  s    D$II re   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)causerB  r  r  s   rf   rv  z&_get_fake_value_impl.<locals>.<lambda>  s0    MMC5)45   re   zshould not reachable)rL  )Mtorch.utils._sympy.value_rangesr?  r  rB  r  r  r4  r@  rA  r  r&  rP   r"  r$  r&  r  r  rL   use_graph_deduplicationtrack_nodes_for_deduplicationr5   r8  r  r  r  r  r  r  r  r  r  r  r   _infer_parametersrs   rp  r   r  r   	__cause__r  rE  DataDependentOutputExceptionfuncopsatenr  r  _local_scalar_denser   DynamicOutputShapeException capture_dynamic_output_shape_opsUnsupportedOperatorExceptionr_  ra  r.  _dispatch_pystub_schemaoverload_namer   r:  r;  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr  rW  r/  r8  rf  r.  tree_map_onlyr  rW  partialr  region_trackertrack_node_mutations)r'  rL  r  r?  r4  r@  rA  r  flat_args_kwargsid_to_initial_versionr8  ret_valr  r  import_suggestionmaybe_pystubrq  ctxr  r$  rX  rB  r&  r  rQ  r  s   ``                 @@@@@@@rf   r:  r:  0  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Optional[torch.fx.Node]c                 .    t          t          dd           S )Nr   )r  _current_noderN   re   rf   get_current_noderx  
  s    ='4000re   c              #     K   t                      }| t          _        	 d V  |t          _        d S # |t          _        w xY wr|   )rx  rw  r   )r'  olds     rf   set_current_noder{    sI      


CM"!c!!!!s   0 >tracerrQ  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   rm   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 )r  ra  )r  r$  r&  r'  r  s    rf   make_error_messagez$run_node.<locals>.make_error_message-  s@    p2ppppW[ppagpppq''re   r4   )r4  r^  NrC  r   rA  r  z/Missing attribute when running call_method noder  zattribute not definedrC  rD  r  placeholderr5  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   rm   r   )r  r{  r  r4  r  r   r  rB  r  rW  r  output_graphget_submoduler&  r  r  r  r  r   r6  r7  rf  )r|  r'  r$  r&  rQ  r  r4  rB  r  r  r  r  s    ```       @rf   rP  rP    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.
    r4   rA  r  c                $    t          |           S r|   )get_real_value)r  r|  s    rf   rv  z get_real_value.<locals>.<lambda>z  s    .F++ re   r  graphargrD  Nc                 2     dddg j                    S )Nz7RuntimeError when trying to get real value from fx.Noder  r  rV  )exnrB  r  s   rf   rv  z get_real_value.<locals>.<lambda>  s0    MMQ5)45   re   zshould not be reachable)r  rB  r  r  real_value_cacher  r  r   r'  r!  r$  r&  r&  exampler  r  r  r  r  r  rP  r   r8  rf  )r'  r|  cacher  r$  r&  	nn_module
real_valuer  r  rB  r  s    `       @@@rf   r  r  k  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   )FakeTensorConfigrP   rj  r   rm   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)rj  r  r  s     rf   stack_or_hintz7assert_no_fake_params_or_buffers.<locals>.stack_or_hint  s?    ! 	d`9N9Nq~9^9^```ccre   zUnexpected fake buffer r   zUnexpected fake param )rj  r   rm   r   )torch._subclasses.fake_tensorr  rP   named_buffersnamed_parameters)r  rP   r  rs   bufferr  r  s         @rf    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	
 	
! 	

 
re   c                $    | j          d| j         S )z9
    Returns the fully qualified name of the object.
    rz  )r`   ra   ra  s    rf   fqnr    s     n11s/111re   count1count2c                6    t           j        j        j        r| S |S r|   )r  r  rL   assume_static_by_default)r  r  s     rf   ifdynstaticdefaultr    s    }4 re   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  rz  N)r#  r  listdirr  r  r   r   __file__rL  	importlibimport_moduler_   )r  r  s     rf   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re   c                :    t          t          |                     S r|   )class_has_getattributer  r<  s    rf   object_has_getattributer    s    !$u++...re   c                F    t                               | d          }|||<   d S )Nr  )r   __getattribute__)r  rs   r   r5  s       rf    object_setattr_ignore_descriptorr    s$    Z00AAdGGGre   c                    	 t          t          j        | d          t          j                  rdS n# t
          $ r Y nw xY wdS )Nr  TF)r  rZ  r[  r8  re  rZ  r~   s    rf   r  r    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__)rZ  r[  r  rZ  r  r  r  r  )r   r  
getattr_fns      rf   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 )TensorStaticReasonr]   r  r   N)r_   r`   ra   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYrN   re   rf   r  r    s         IJre   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  rf  )r  s    rf   tensor_static_reason_to_messager    sX    #---OO#...CC#666EE
3633
4
44re   r  Union[torch.Tensor, Any]r[  tensor_sourcer#   )tuple[bool, Optional[TensorStaticReason]]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.
    r4   ))is_from_unspecialized_param_buffer_sourceTr  )r  r  r  r  r  rL   &force_nn_module_property_static_shapesr  r  r  r  r  r  force_parameter_static_shapesr  r  )r  r[  r  r  s       rf   tensor_always_has_static_shaper    s      BAAAAA 	";;==;%HHJJ; 
7; '::: 	V***44]CC 	+

. 	+ '111 3'222;re   r@  c                2     d fd}t          |          S )Nrm   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 rN   )r  rs   r  r$  r&  )r   r  s     rf   rx  z<lazy_format_graph_tabular.<locals>.inner.<locals>.<listcomp>(  s:     
 
 
;<QT1618QVQX6
 
 
re   )opcoders   r  r$  r&  r   )
r   r   r   r,   r0  r  r+   forward__code__r  )r   
node_specs	graph_strr@  r  s      rf   innerz(lazy_format_graph_tabular.<locals>.inner  s    	))))))) 	 	 	~,Wb99::;  	
 
@B
 
 

 H N N N
 
 
	 "'2:+>+JIVVVs   
 *77rm   r   rI   )r@  r  r  s   `` rf   lazy_format_graph_tabularr    s>    W W W W W W W" ere   line_noc                l    |  d| d| d| dt          j        |                                            d
S )Nr   z line z 
r   )disBytecode)r  rs   r  r  r  s        rf   format_bytecoder  3  sI     VVtVVhVVgVV#,t:L:L:P:P:R:RVVVVre   )_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  modulesrq  _global_backward_hooks_global_backward_pre_hooksrN   re   rf   nn_module_has_global_hooksr  I  sH     EH#:;; 	Cux&ABB  re   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              rf   nn_module_get_all_hooksr  R  s      	'$$	'&& 
  7o 7""#5666 8 8""#6777 :""#8999I- # #^R00 	# 	#I#DT""""	# re   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        rf   nnmodule_has_hooksr  s  s2     $/15	  E ;;re   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     rf   r   z"to_numpy_helper.<locals>.<genexpr>  *      AAC?3//AAAAAAre   )rP   r  tnpr  r  r  r  r  numpyr  r}  r  r<  s    rf   r  r    s    u~~ %%% u|,,,	E5<	(	( {{{&&&	EE4=	)	) tE{{AA5AAAAAAre   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_tensorr  s     rf   r   z"numpy_to_tensor.<locals>.<genexpr>  r  re   )
r@  r  r  r  r  r  r  r  r}  r  r<  s    rf   r   r     s    >>>%$$ &u%%%%%% |	EE4=	)	) tE{{AA5AAAAAAre   c                  &    e Zd ZddZddZddZdS )numpy_to_tensor_wrapperr  Callable[_P, R]rm   rn   c                <    || _         d| j         j        z   | _        d S Nwrapped_r  r_   )r  r  s     rf   r  z numpy_to_tensor_wrapper.__init__  s    "TV_4re   r   c                "    d| j         j         dS )Nz<Wrapped function <original >>r  r  s    rf   __repr__z numpy_to_tensor_wrapper.__repr__  s    AdfoAAAAre   r$  r%  r&  r'  r   c                :     | j         |i |}t          |          S r|   )r  r   r  r$  r&  r   s       rf   r  z numpy_to_tensor_wrapper.__call__  s&    dfd%f%%s###re   N)r  r  rm   rn   r  r$  r%  r&  r'  rm   r   )r_   r`   ra   r  r
  r  rN   re   rf   r  r    sT        5 5 5 5B B B B$ $ $ $ $ $re   r  c                   t          | t          j                  rt          | |          }t	          |          S t          | t
          j                  r1t          t          j        |           |          }t	          |          S d S r|   )r  r  r  r  r   r  r  )r  rs   r   s      rf   numpy_attr_wrapperr    sx    #s{## $c4  s###	C	&	& $ck#&&--s###$ $re   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   rm   rn   c                2    || _         d| j         z   | _        d S r  )r  r_   )r  r  s     rf   r  znumpy_method_wrapper.__init__  s    "T[0re   c                    d| j          dS )Nz<Wrapped method <original r	  )r  r  s    rf   r
  znumpy_method_wrapper.__repr__  s    ;DK;;;;re   r$  r   r&  c                    |d         }t          |t          j                  rt          j        |          }t          || j                  } ||dd          i |}t          |          S r  )r  r  r  r  r  r  r  r   )r  r$  r&  r  method_callabler   s         rf   r  znumpy_method_wrapper.__call__  si    1gc5<(( 	#+c""C!#t{33otABBx2622s###re   N)r  r   rm   rn   r  )r$  r   r&  r   rm   r   r_   r`   ra   r   r  r
  r  rN   re   rf   r  r    sV        qq1 1 1 1< < < <$ $ $ $ $ $re   r  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  rm   rn   c                2    || _         d|j         | _        d S r  )r  r_   )r  r  s     rf   r  znumpy_operator_wrapper.__init__  s    02;00re   r   c                    d| j          dS )Nz<Wrapped operator <original r	  )r_   r  s    rf   r
  znumpy_operator_wrapper.__repr__  s    ?dm????re   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  r  r  r  s     rf   r   z2numpy_operator_wrapper.__call__.<locals>.<genexpr>  sS       
 
KN
3 = =FCK3
 
 
 
 
 
re   )r  r   r  s       rf   r  znumpy_operator_wrapper.__call__  sH    z
 
RV
 
 
 dgtns###re   N)r  r  rm   rn   r  r  r  rN   re   rf   r  r    sZ        [[1 1 1 1@ @ @ @$ $ $ $ $ $re   r  c                "   t          | t                    s| S | j        rg }|                                 D ]n}t          |t          j                  r=|                    |j        j        	                    |j        j
                             Y|                    |           og }|                                 D ]n}t          |t          j                  r=|                    |j        j        	                    |j        j
                             Y|                    |           on(|                                 }|                                 }t	          j        ||| j        | j        | j                  }|                                 |S )N)r  r*  r  )r  rO   _has_symbolic_sizes_stridesr  r  r  r   r'  r9  	size_hintexprr  empty_stridedr  r*  r  zero_)ru  r  r  r  r  s        rf   defaker#    se   a$$  	$  	 	A!U\** AF,66qv{CCDDDDA 	! 	!A!U\** !af.88EEFFFFa    		! vvxxgxo	 	 	A GGIIIHre   r  r%  r'  c                     | |i |S r|   rN   )rv  r$  r&  s      rf   8_disable_side_effect_safety_checks_for_current_subtracerr%    s     2tvre   c                0    dd l }| |j        j        j        u S r   )torch.utils.checkpointr|  
checkpoint)r  r  s     rf   is_utils_checkpointr)    s!    !!!!%+(333re   c                    ddl m} | |u S )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphr+  )r  r+  s     rf   is_invoke_subgraphr-    s     SSSSSS---re   optionsc                 H    ddl m}  |j        t          j        j        fi | S )Nr4    TorchHigherOrderOperatorVariable)variables.higher_order_opsr1  maker  _higher_order_opsinvoke_subgraph)r.  r1  s     rf   build_invoke_subgraph_variabler6    sB    LLLLLL0+0/ 
  re   c                     dd l mc m} ddlm} |j        }t          j        j        j	        r|j
        } |j        |fi | S )Nr   r4   r0  )torch._higher_order_ops.wrapr4  wrapr2  r1  tag_activation_checkpointr  
_functorchrL   functionalize_rng_opswrap_activation_checkpointr3  )r.  higher_order_opsr1  activation_checkpoint_ops       rf   build_checkpoint_variabler@    s    ;;;;;;;;;LLLLLL
 	2  4 O#3#N 0+0  
  re   device_typerF   c                    ddl m} t          j        |           j        } |            }|dk    rn|dv r|rt                      }nd}|S )Nr4   )is_dynamo_supportedr  )r  rF  mtiaF)
eval_framerC  r  r*  r  r1   )rA  rC  r  compile_supporteds       rf   is_compile_supportedrG  -  sl    //////<$$)D++--u}}	(	(	(->	(&LL!re   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   rH  as_utf8s      rf   _fix_offsetrN  ?  s?     jj!!Gwww&&wy&AABBBre   c                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )_Anchorsrl   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetNr{  rN   re   rf   rP  rP  K  sD          re   rP  segmentOptional[_Anchors]c                   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
    )r^      r   Nz(
z
)r4   r   linenorl   rH  rm   c                0    t          |          |          S r|   )rN  )rY  rH  liness     rf   	normalizez-_extract_anchors_from_expr.<locals>.normalizeq  s    5=&111re   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  )rY  r]  r[  s     rf   next_valid_charz3_extract_anchors_from_expr.<locals>.next_valid_charv  s    s5zz!!cSv-?-?&?&?CaKF s5zz!!cSv-?-?&?&? E

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

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

""sSv-?-?'?'?'??s{re   r]   z)\#z\#rt  rv  )rY  rl   rH  rl   rm   rl   )rY  rl   r]  rl   rm   r^  )r6  r7  astparseSyntaxErrorr  bodyr  r  Exprr   BinOpr   rl   left
end_linenoend_col_offsetisspacerP  	SubscriptCallr_  )rU  rf  treer\  r   re  	statementr   
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor[  ra  s                  @@rf   _extract_anchors_from_exprr{  U  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).
    )r^      )rm  colno	end_colnor   r   r4   Nc                h    g | ].}t          j        j        |                                          /S rN   )	linecachegetliner  rstrip)r   rY  r  s     rf   rx  z.get_instruction_source_311.<locals>.<listcomp>  sE     
 
 
 d.77>>@@
 
 
re   	positionsr  r   ~c                :    g | ]}|                     d d          S )r  ^)r~  r   markers     rf   rx  z.get_instruction_source_311.<locals>.<listcomp>0  s&    BBB6>>#s++BBBre   c                ,    g | ]}t          |          S rN   r-  r  s     rf   rx  z.get_instruction_source_311.<locals>.<listcomp>3  s    +O+O+OVDLL+O+O+Ore   r  c                8    g | ]}d                      |          S )r  r  r  s     rf   rx  z.get_instruction_source_311.<locals>.<listcomp>M  s"    AAAv2776??AAAre   ) r6  r7  r  FrameSummaryr  r  rY  r  rm  
col_offsetrn  r  r   
splitlinesr7  textwrapdedentr  indentr   r  r  r  rN  r   lstripr{  rf  rQ  rR  rS  rT  )r  r}  frame_summaryr*  
orig_linesorig_lines_dedent
indent_lenr  
first_linestart_offset
end_offsetrU  markers	last_linerY  line
num_spacesanchorsmutable_markersr]  r,  s   `                    rf   get_instruction_source_311r    s    7""!.N!L~0.+n3
 
 
 &77:6,,..qrr233
 
 
 
 5t~7PST7TUU
 
 

 %ODIIj,A,ABBMMOOA''#.?.B*C*CC
A{
{+!8!8&AA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    rf   get_static_address_typer  Y  s.    !U\"" =q5t<<<4re   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  r.  r8   	get_statedefault_generatorrD  r  	set_staterG  )r   getterssetterss      rf   is_rng_state_getter_or_setterr  `  ss     	$)
 G 	$)
 	G (g(((((re   c                    t          | t          j                  o'| j        dk    o| j        j        t          j        j        u S )N__get__)	r  r8  r   r_   __self____objclass__r  r.  _TensorBaser<  s    rf   is_tensor_base_attr_getterr  q  s?    5%122 	@Ni'	@N'58+??re   c                    	 t          j        t          j        |           }t	          |          t
          j        u S # t          $ r Y dS w xY wr?  )rZ  r[  r  r  r  r8  rs  rZ  )rs   r  s     rf   is_tensor_getset_descriptorr  y  sQ    %elD99DzzU777   uus   9< 
A
	A
c                "    t          | d          S )N__torch_function__)r   r<  s    rf   is_torch_function_objectr    s    5.///re   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)rS  r  &torch._dynamo.variables.torch_functionr  r  r   r  r  r  
class_typer   r  r.  _disabled_torch_function_impl)r?  r  r  r_  s       rf   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5re   c                    d }d }t           j        j                                        x}r| |j        v r|j        |          }|j        }|                    | d||          S )NF)static_shapessymbolic_contextr  )r  rI  r$   try_gettensor_to_contextr  from_tensor)rj  r8  r  r  tracing_contexts        rf   to_fake_tensorr    sx     F-6>>@@@ 4111.@C%3F  	1A& !   re   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    rf   is_frozen_dataclassr    st    #E*** 	.&u---	.	. E122	. E.99		.
 &-re   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  rf  )r  r  r  s      rf   get_first_attrr    s^      & &3 	&3%%%%%	& CMMeMM
N
NNre   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   rm   c                <    dd}t          j        | |          S )Ngm_r   example_inputs_rm   c                    t           j        j        j        d         dxx         dz  cc<   t           j                            | |          S )Ncompiled_autogradcompilesr4   )r  r  r|  rU   re  compile)r  r  s     rf   inner_compilerzKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compiler  sE    #,-@A*MMMQRRMMM..sODDDre   )backendr  r  )r  r   r  r   rm   r   )r  r  )r  r  r  r  s     rf   compiler_fnz3maybe_enable_compiled_autograd.<locals>.compiler_fn  s?    E E E E =Ni   re   )r  r   rm   r   )r  r  r  _enable)r  r  r  r  rt  s    ``  rf   maybe_enable_compiled_autogradr    s        	 	 	 	 	 	 	 ],44[AA 	SIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A

AAr3   c                 V     G d dt                     } t           |                       S )Nc                      e Zd ZdS )*invalid_removeable_handle.<locals>.InvalidN)r_   r`   ra   rN   re   rf   Invalidr    s        re   r  )r6  r3   )r  s    rf   invalid_removeable_handler    sB        $    7799%%%re   c                    t          | t          j        j                  s| S t          | t          j        j                  r| S | j                            | j                  }| j        |_        |S r|   )	r  r  r  r  r   r  r  rl  r  )r  proxys     rf   nn_module_proxyr    s^    c58?++ 
#ux+,, 
M!!#-00E\ENLre   c                  (     e Zd Zd fdZdd
Z xZS )	GmWrapperr  r   unflatten_fnCallable[[list[Any]], Any]rm   rn   c                d    t                                                       || _        || _        d S r|   )r  r  r  r  )r  r  r  r  s      rf   r  zGmWrapper.__init__  s0     	(re   r$  r   c                Z    t          |          } | j        |                     |           S r|   )r}  r  r  r  s     rf   r  zGmWrapper.forward  s*    t**tw))$//00re   )r  r   r  r  rm   rn   r$  r   rm   r   )r_   r`   ra   r  r  r  r  s   @rf   r  r    sQ        ) ) ) ) ) )1 1 1 1 1 1 1 1re   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  r&  r   )r   r,  r'  s      rf   rx  z(flatten_graph_inputs.<locals>.<listcomp>  sH       At7m##	k5(I(I# 	
###re   r   r$  r   rm   c                B    | d         t          | dd                    z   S r  r-  r  s    rf   
flatten_fnz(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qrr(^^++re   	flat_argsc                0    | d          g| d          R S r|   rN   )r  boxed_inputs_counts    rf   r  z*flatten_graph_inputs.<locals>.unflatten_fn  s,    1112TY?Q?R?R5STTTre   )treespecc                 n     |           }D ]}| |                                            |          S r|   )r}   )r$  r  r,  compiled_fnr  inputs_idx_to_clears      rf   wrapperz%flatten_graph_inputs.<locals>.wrapper'  sH    Jt$$	 % 	 	AGMMOOOO {9%%%re   r  )r  r   rm   r   )	enumerater0  r  r  r  r  in_compiled_autograd_regionr  r}  r  r  r.  r/  rW  rl  tree_unflattenarg_tree_leaves)r  r  r  r  flat_inputsspecr  r  r  r  r  s          @@@@rf   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+
& & & & & & & & Nre   maybe_gmc                    t          | t          j        j                  rt	          | d          sg S | j                            dg           S )Nr&  locals_to_steal)r  r  r   r  r   r&  r   )r  s    rf   get_locals_to_stealr  4  sH    h 455 WXv=V=V 	=.333re   r  c                    || j         d<   d S )Nr  r%  )r  r  s     rf   set_locals_to_stealr	  :  s    !0BGre   c                      e Zd ZddZd	dZdS )
Litr  r   rm   rn   c                    || _         d S r|   r  )r  r  s     rf   r  zLit.__init__?  s    re   c                    | j         S r|   r  r  s    rf   r
  zLit.__repr__B  s	    vre   N)r  r   rm   rn   r  )r_   r`   ra   r  r
  rN   re   rf   r  r  >  s<                re   r  zset[str]warn_once_cache
stacklevelc                    | t           v rd S t                               |            t          j        | |dz              d S )Nr4   )r  )r  r  warningswarn)r  r  s     rf   	warn_oncer  I  sG     oM#*q.111111re   textc                V    t          j        d          }|                    d|           S )Nz\x1B[@-_][0-?]*[ -/]*[@-~]r  )r;  r  r  )r  ansi_escapes     rf   strip_color_from_stringr  U  s&    *:;;K??2t$$$re   c               #     K   	 t           j        j                            d          } d V  t           j        j                            |            d S # t           j        j                            |            w xY w)NT)r  r.  	_autograd _saved_tensors_hooks_set_tracing)r  s    rf   +_disable_saved_tensors_hooks_during_tracingr  [  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  re  rL   freezingis_grad_enabledrN   re   rf   is_parameter_freezingr   e  s!    ?!*J53H3J3J/JJre   c                 L    d t          t                                D             S )Nc                ,    g | ]}t          |          S rN   ) get_torch_function_mode_stack_atr+  s     rf   rx  z1get_torch_function_mode_stack.<locals>.<listcomp>j  s.       01(++  re   )r7  r   rN   re   rf   get_torch_function_mode_stackr$  i  s2     5:;T;V;V5W5W   re   indc                t    | t                      k     r| dk    sJ t          j                            |           S r   )r   r  r.  _get_function_stack_at)r%  s    rf   r#  r#  o  s7    *,,,,98**3///re   rY  c                    t          t                                D ]}t                       | D ]}t          |           d S r|   )r7  r   r   r   )rY  r  modes      rf   set_torch_function_mode_stackr*  t  sY    ,..// $ $!#### , ,%d++++, ,re   c                 ^    t          t                                D ]} t                       d S r|   )r7  r   r   )r  s    rf   clear_torch_function_mode_stackr,  |  s9    ,..// $ $!####$ $re   r*  torch.devicetorch.Streamc                @    t           j                            |           S r|   )r  acceleratorcurrent_streamr)  s    rf   get_current_streamr2    s    ++F333re   c                 "    t                       d S r|   )
breakpointr  s    rf   _breakpoint_for_c_dynamor5    s    LLLLLre   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 )Nr]   r4   )InternalTorchDynamoErrorz[Tensor subclass method __metadata_guard__ must take exactly two subclass metadata argumentsz?Tensor subclass method __metadata_guard__ must be a classmethod)	__metadata_guard__rZ  	signaturer  r  r  r7  r  r  )r   rv  sigr7  s       rf   verify_guard_fn_signaturer;    s    		!B

B

C
3>a111111&&i
 
 	
 
{eo%%111111&&M
 
 	
 &%re   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   r6  r   rl  r~  __iter__)r<  s    rf   #does_not_override_dict_iter_methodsr?    sl    4:{'899 	GO[-?@@	GMdi)9::	G $-1E!FF	re   r,  c                ,    |                      |          S r|   )r  ru  r,  s     rf   	call_sizerB    s    66!99re   c                ,    |                      |          S r|   )r  rA  s     rf   call_striderD    s    88A;;re   c                *    |                                  S r|   )storage_offsetrt  s    rf   call_storage_offsetrG    s    re   c                ,     ddg} fd|D             }|S )Nr  tagc                b    i | ]+}|j         v |t          j        j         |                   ,S rN   )r  r  )r   r   rj  s     rf   r  z(_extract_tensor_dict.<locals>.<dictcomp>  s>       ,/3!*CTCTTYqz#''CTCTCTre   rN   )rj  KEYS_TO_COPYtensor_dicts   `  rf   _extract_tensor_dictrM    s@    #L
   3?  K re   
tuple[Any]c                .    t          j        j        | i |S r|   )r  r.  Streamr  s     rf   build_streamrQ    s    8?D+F+++re   torch.Eventc                .    t          j        j        | i |S r|   )r  r.  Eventr  s     rf   build_eventrU    s    8>4*6***re   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   rl   _counterr  _idrm   rn   c                n    | j         dz   | _         | j         dk    rt          j                    | _        d S d S r   )_depthr   ru  rY  r~   s    rf   ru  z#CompileTimeInstructionCounter.start  s5    Z!^
:??*022CGGG ?re   c                    | j         dz
  | _         | j         dk    r0| xj        t          j        | j                  z  c_        d| _        d S d S )Nr4   r   r  )r[  rX  r   endrY  r~   s    rf   r]  z!CompileTimeInstructionCounter.end  sJ    Z!^
:??LL04SW===LLCGGG ?re   c                    d| _         d S r   rX  r~   s    rf   r}   z#CompileTimeInstructionCounter.clear  s    re   c                    | j         S r|   r_  r~   s    rf   r   z#CompileTimeInstructionCounter.value  s
    |re   r>  c              #     K   	 t           j        r|                                  d V  t           j        r|                                  d S d S # t           j        r|                                  w w xY wr|   )rL   %record_compile_time_instruction_countru  r]  r~   s    rf   r  z$CompileTimeInstructionCounter.record  st      	; 		EEE; 					 v; 				s   $A #A/Nr   r   rm   r>  )r_   r`   ra   rX  r   rY  r[  r   ru  r]  r}   r   r   r  rN   re   rf   rW  rW    s         HCMMMMF3 3 3 [3
    [    [    [    ^ [  re   rW  c                        e Zd Zd fdZ xZS )CompileCounterIntotherr   rm   c                `    t          t                                          |                    S r|   )re  r  __add__)r  rf  r  s     rf   rh  zCompileCounterInt.__add__  s!     !7!7888re   )rf  r   rm   re  )r_   r`   ra   rh  r  r  s   @rf   re  re    s=        9 9 9 9 9 9 9 9 9 9re   re  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)ri  rj  s     rf   set_feature_userm    sO     ((** M++OWeLLLLLM Mre   )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 )Nrn  rq  z;Invalid dynamo config optimize_ddp type type(optimize_ddp)=z.Invalid dynamo config optimize_ddp value mode=)rL   optimize_ddpr  r   r   r  r  rr  )rt  r)  s     rf   get_optimize_ddp_moderu  
  s    &L,%% 
".E4E	L#	&	& 
LtL7I7ILL
 
 	
 ))));D;; *)) Kre   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)rL   "fake_tensor_disable_inference_moder  is_inference_mode_enabledinference_moder  )is_inference_mode_ons    rf   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)rL   rw  r  r  
meta_utils$disable_inference_mode_for_fake_proprN   re   rf   *maybe_disable_inference_mode_for_fake_propr  3  s       0 )NNPP 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAc                .    | d u pd| j         v pd| j         v S )Nr5  r$  r%  )r'  s    rf   is_node_meta_validr  A  s$    4<M?di7M5DI;MMre   c                     t           S r|   _error_on_graph_breakrN   re   rf   _get_error_on_graph_breakr  I  s      re   c                
    | a d S r|   r  r<  s    rf   _set_error_on_graph_breakr  M  s    !re   AbstractContextManager[None]c                     t           j        j        j        r$t           j        j                            d          nt          j                    } | 	                                 | S )NzPregraph bytecode)
r  r  r  r  r.  r/  r0  rN  nullcontextr1  cms    rf   record_pregraph_bytecode_enterr  R  sR     >"7	&../BCCC#%% 
 LLNNNIre   r  c                4    |                      d d d            d S r|   )r2  r  s    rf   record_pregraph_bytecode_exitr  ]  s    KKdD!!!!!re   Optional[list[CodeType]]c                 ,    ddl m}   | j                    S )Nr   r$   )torch._guardsr$   get_traced_coder  s    rf   r  r  d  s%    ,,,,,,)>)+++re   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 )Nr4   rA  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__rC  )r  rB  r  r  r  r  r   SUPPORTABLE)r  r?  rB  r  is_overriddens        rf   raise_on_overridden_hashr  j  s    ######""""""II&**:u==M 	
BSd3iiSSrSSorooo_".		
 	
 	
 	
 	
 	
	
 	
re   )r   r   r   r   rm   r   r   )r   r   rm   rn   )rm   r   )r   rl   rm   rn   )rm   rY   )rm   r    )rm   r!   )rs   r   rm   r,  )NFNNNNFN)r   r   r5  r6  r   r   r   r7  r8  r6  r9  r:  r;  r<  r=  r   r>  r6  rm   r,  r*  )ra  rb  rc  r   rm   r   )ra  rg  rc  r   rm   rh  )r   F)ra  r   rc  r   rm   rj  )rm   r  )r  r  r  r
   rm   r   )r  r   r  rA   rm   rn   )r  r  rm   rl   )ru  rR   rm   rR   )ru  r   rm   r   )r$  r   r&  r   rm   rn   )r  r   r  r  rm   r  )r  r   r  r  rm   r  )r  r   r  r  rm   r   )r  r   r  r   rm   r   )r  r  r  r   rm   r!  )r   r   rm   r   )r   rO  rm   rP  )r   r   rm   rS  )r   r   rm   r]  )r   r   rm   rc  )r   r   rm   rq  )rv  r   rm   r   )rv  r   rm   rz  )r   r   rm   r~  )r  r   rm   r   )r  r   rm   r   r  r|   )r$  r   rm   r  )r$  r   r&  r   rm   r  )r  r  rm   r   )r  r  rm   rn   r  )rk  rl   rl  rl   rR  rV   rm  rn  ro  rp  rm   rn   )r  rl   rm   rn   )rm   r  )rm   r  )rm   r   )FF)r   r   r  r   r   r   rm   r,  r  )r  r  r  r  r  r   rm   rn   )r  r  r  r  rm   rn   )ru  r  r  r  rm   r  )r,  r-  rm   r.  )r,  r2  rm   r3  )r,  r   rm   r   )r$  r  rm   rn   rc  )rN  r   rm   rO  )r  r   r,  r   r  r   rm   r   )r  r   rm   r6  )rt   r   rm   r   )rt   r  rm   rs  )r  r   rm   r  )r4   )r  r   r,  r  r  rl   rm   r  )r  r   r,  r  rm   r   )r  rl   rm   r  )r  r   rm   r   )rm   r  )r   r   rm   r  )r   r   r  r   rm   r   )r  r   rm   r   )r$  r  r&  r  rm   r   )r5  r   rm   r  )r  r   rm   r  )rt   r   rm   r   )r  r  rm   rl   )r  r   rz  rl   rm   r   )r  r   rm   r  )rj  r   rt   r  rm   r   )r5  r%  r  rl   rm   r   )r  r+  r  rl   rm   rR   )r   r   r  r   rm   r   )r'  r4  r5  r   rm   rn   )r?  rE   rm   r   )r  r}  rG  rl   rm   rl   )r  r  )
rL  rC   rs   r   rM  r   rN  r   rm   rn   )
r   r  rV  r   rL  rB   rW  r   rm   r   )r  r   rm   re  )r   r   rm   rh  )ru  r   r  r   rm   r   )r  r   r  r   rm   r   )rL  rC   r  r   r  r   rm   r   )r  rR   r  r  rm   r   )r  r   r  r   r  r   rm   r   )rv  r  rm   r   )r  r   r8  r  rm   r   )r  r  r8  r  rm   r  )r  r   r8  r   r  r   rm   r   )r  r   r8  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  rm   r   )r  r
   rm   r   )r  r   rm   r   r  )T)r'  r4  r  r   rm   r   )r  r   rL  rC   rm   r   )rL  rC   r  r   r  r   rm   r   )r  r   r8  r#  rm   r   )r0  r1  rm   r   )r'  r4  rL  rC   r  r   rm   r   )rm   ru  )r'  r4  rm   r>  )r|  r   r'  r4  r$  r   r&  r   rQ  r   rm   r   )r'  r4  r|  r   rm   r   )r  r  rm   rn   )r  r   rm   r   )r  r   r  r   rm   r   )r  r  rm   rn   )r  r   rs   r   r   r   rm   rn   )rt   r  rm   r   )r   r   r  r   rm   r  )r  r  rm   r   )r  r  r[  r   r  r#   rm   r  )r@  r   r  r   rm   r   )r  r   rs   r   r  r   r  rl   r  r   rm   r   )FFF)
r  r  r  r   r  r   r  r   rm   rh  )
r  r  r  r   r  r   r  r   rm   r   )r   r   rm   r   )r  r   rs   r   rm   r   )ru  r   rm   r   )rv  r  r$  r%  r&  r'  rm   rS   )r.  r   rm   r   )rA  rF   rm   r   )r   r   rH  rl   rm   rl   )rU  r   rm   rV  )r  r|  r}  r@   rm   r   )rj  r   rm   r   )rs   r   rm   r   )r?  rE   rm   r   )rj  r  r8  r  rm   r   )r  r   r  r   rm   r   )TT)r  r   r  r   r  r   rm   r,  )rm   r3   )r  r   rm   r   )r  r   r  r   r  r  rm   r  )r  r   rm   rh  )r  r   r  rh  rm   rn   )r  r   r  rl   rm   rn   )r  r   rm   r   )rm   rh  )r%  rl   rm   r   )rY  rh  rm   rn   )r*  r-  rm   r.  )r   r   rm   rn   )r<  r   rm   r   )ru  r   r,  rl   rm   rl   )ru  r   rm   rl   )rj  r  rm   rV   )r$  rN  r&  r%  rm   r.  )r$  rN  r&  r%  rm   rR  )ri  r   rj  r   rm   rn   )r'  ru  rm   r   )r   r   rm   rn   )rm   r  )r  r  rm   rn   )rm   r  )r  r   r?  rE   rm   rn   (  r   
__future__r   atexitr   rN  r  ry  r  r  r  rW  r  r  rZ  r   r&  r  r  r  r  r  r;  r6  r  r  r   r  r8  r:  r  r  r  r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r  torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreer|  _pytreer.  r   torch._Cr   r   r   r   torch._dispatch.pythonr   torch._dynamo.metrics_contextr    r!   r  r"   r#   r$   torch._subclasses.meta_utilsr%   torch._utils_internalr&   r'   r(   r)   r*   torch.fx._utilsr+   r,   torch.monitorr-   torch.nn.modules.lazyr.   torch.utils._ordered_setr/   torch.utils._python_dispatchr0   torch.utils._tritonr1   r2   torch.utils.hooksr3   graph_utilsr5   r:  collections.abcr6   r7   r8   r9   r:   r;   r<   r=   r>   r?   %torch._dynamo.bytecode_transformationr@   torch._dynamo.replay_recordrA   torch._dynamo.symbolic_convertrB   rC   torch._dynamo.variables.baserE   torch._prims_commonrF   torch._subclassesrG   r  r@  r  torch._loggingtorch._numpy_numpyr  rH   rJ   r  rL   fftlinalgrC  rM   r   NP_TO_TNP_MODULEr  rO   rP   rQ   r   rR   rS   rT   r  r  r  unpatched_nn_module_getattrr  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implr   rU   rW   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgr  r_   r   rX   r  rZ   rv   timer_counterr  r\   rh   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   rM  r4  r`  rf  registerr  FloatTensorrL  DoubleTensorrM  double
HalfTensorrK  halfBFloat16Tensorr  
ByteTensorrE  
CharTensorrA  
LongTensorrD  long	IntTensorrC  rl   ShortTensorrB  short
BoolTensorr   tensortype_to_dtyper  r  r  r  r  VERBOSEr  	Formatterr  r  r  r  r  r  r  r  r  r  r  r  r  r7  ParamSpecArgsParamSpecKwargsTypeVarTupleTypeAliasTyper3  r=  rI  rN  rR  re  rf  rg  rh  r\  rb  rY  r]  neltlegtgecmp_name_to_op_mappingcmp_name_to_op_str_mappingru  ry  rx  r  r  r  r  r  r  r  r  r,  r~  r  !DEFAULT_COMPILATION_METRICS_LIMITr  r0  r3  rU  r\  rj  r   r  r  r  r  r  r   r  r  r  r  r  r  r  r  r	  r1  r=  rM  rX  r]  r_  rd  rc  r  r  r  r  r  rJ  r   rk   r  Ellipsisr  NotImplementedr*  r  memory_formatr  finfoiinfo	attention
SDPBackendr  _CudaDevicePropertiesr  r  r  languager  r  r  r  r  r  r  r  r  r  r  r~  r  rl  r  r   r  r  iterr  r7  r  __length_hint__tuple_iterator_lenr   rl  r  r6  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	  r  r  r  r  r'  	iter_nextr!  r$  dict_getitemr9  r*  r.  r3  r>  rF  rK  rU  rd  rg  rk  rr  r  GLOBAL_KEY_PREFIXr  r  r  r  r  r  r  r  r\  r  r  r  orig_code_mapr  r  seen_code_mapr  r  r  r  r"  r/  r8  r  r:  rw  rx  r{  rP  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  all_hook_namesr  r  r  r  r   r  r  r  r  r#  r%  r)  r-  r6  r@  rG  rN  rP  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,  r2  r5  r;  r?  _disable_dynamorB  rD  rG  rM  rQ  rU  rW  re  rm  rr  ru  r{  r  r  r  r  r  r  r  r  r  rN   re   rf   <module>r     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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
 
 
 
 
           ?C  B B B B* * * *$ $ $ $$ $ $ $    
 
 
 
$   > 'y(( 0 0 0 08 8 8 8    49   f# f# f# f# f# f# f# f#R	 $IO%%     .  !%"',0.2&*"&!/3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 
   

 
 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        83 3 3 3   % % % %$ $ $ $, , , , T       
 
 
 
$0 0 0 05 5 5 5   @" @" @" @" @" @" @" @"F %' ! ?Pk>O,? ? ?     
- - - -` 3  3  3  3F3 3 3 30- - - -`O3 O3 O3 O3d% % % %! ! ! !
& & & &Q Q Q Q Q Q Q Qh
 59  8 8 8 8   * * * *
  %*"'' ' ' ' '@ ( ( ( ( ( ( ( ((         +       ).**       @ @ @ @  8<K K K K K K\ 
   

 
 = = = 
 =   &
 
 
 
& 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$'$     ,  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   1 1 1 1   
: : : : 	        @ @ @ @   
   8 8 8 8(   M M M M 	    4 #(	* * * * *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 '&(( , , , ,& & & &
    $   
/ / / /.       F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"