
    %Vji|                   >    U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm!Z!m"Z" ddlm#Z#m$Z$ ddlm%Z% ddlm&Z& ddlm'Z'm(Z( dd	lm)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 dd
l2m3Z3m4Z4m5Z5m6Z6m7Z7 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^m_Z_ ddl`maZa ddlbmcZc ejd        r0ddlemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZn ddlompZp ddlqmrZrmsZs ddltmuZu ddlvmwZw 	 ddlxZyn# ez$ r dZyY nw xY w	 ddl{Z8ddl|m}Z~ ddlJmZ ddl{mZ dd lmZ eyrCeyeyj        eyj        eyj        fZd!ed"<   eye~eyj        e~j        eyj        e~j        eyj        e~j        iZnd#Zi Zdd$lmZmZmZ n# e$ r Y nw xY w e0d%          Z e0d&          Z e4d'          Ze8j        j        j        Ze8j        j        j        Ze8j        j        j        Z ej        ej!                  Zd(ed)<   i Zd*ed+<   d,Zd-Zd.e d/Z ej        e          Zi Zd0ed1<    ej        e          Zd2ed3<    ej                    Z G d4 d5e
j                  Z G d6 d7          ZdCd>ZdadDd@ZdDdAZdadBedC<   dEdFZdFdGZdadGdJZdHdKZdDdLZdMedN<   dOedP<   dIdQZdJdRZ G dS dTe
j                  Z G dU dV          Z ej                    Ze$	 	 	 	 	 	 	 	 dKdLdg            Ze/dMdNdk            Ze/	 dMdOdn            Z	 dPdQdpZej        dDdq            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 dr ds          Z e٦            ZڐdRduZېdDdvZܐdRdwZݐdRdxZސdSd}ZߐdTdZdUdZdVdZdWdZdXdZ G d d          Ze/dYd            Ze/dZd            Ze/d[d            Zd\dZej        dk    r&ej        ej        ej4        ej0        ej        ej        fZd]dZd]dZd]dZe/d^d            Ze/d_d            Zd]dZe1ej        ej        ej        ej        f         Zded<   d`dZdadZej        ej        ej        ej        ej        ej        dZdddddddZdbdZ dcdZdddZdedZdfdZdgdZ ej&        d          dhd            ZdidjdZdkdZdldƄZ	dldǄZ
dȐZej         G dɄ dʦ                      ZdːZ ej        e̦          aded<   dmdЄZdndфZdnd҄ZdodڄZ eHeۦ          Z eIeۦ          Zdpd݄ZdDdބZdqdZ G d d          Zdaded<   drdZdsdZe$	 	 dtdud            Zej         G d d                      Z G d de          Z  e             e _!        dvdZ"dddwdZ#e/dxd            Z$e/dyd            Z$dzdZ$d{dZ%e$d|d            Z&d}dZ'dMd~dZ(ddZ)dfdZ*ddZ+ ej&        d          dd            Z,ddZ-	 dddZ.ddZ/ e&d          dd            Z0eee1eאe2e3 e4d          e5j6        e7j6        ej'        e8j8        e8j9        e8j:        e8j;        e8j<        e8j=        e8j        j>        j?        e8j@        jA        hZBded<    e_            r*ddlCZCeBD                    eCjE        j9                   	 ddZFejG        dd            ZHddZIdd!ZJdd#ZKdd$ZLdd&ZMdd'ZNdd(ZOdd)ZP e4i Q                                          ZRd*ed+<    e4i S                                          ZTd,ed-<    e4i U                                          ZVd.ed/<    e4 e"            S                                          ZWd,ed0<    e4 eXd#                    ZYd1ed2<    e4 eX eZd                              Z[d1ed3<   eYj\        Z]e^j_        Z`eaj_        Zbd4  ejc        eajd        S                                e"jd        S                                          D             Zed5 efjd        S                                D             Zgd6 ehjd        S                                D             Ziejj_        Zkd7 ejjd        S                                D             Zld8 emjd        S                                D             Znemjo        Zpd9 e2jd        S                                D             Zq e0d:          Zr e0d;          Zsdd?ZtddAZuddBZvddEZwddGZxddHZyezZ{ddKZ|ddMZ}eajo        Z~ddOZddRZddTZddXZdd[Zdd^ZddbZ	 dMddgZddjZddlZddmZddoZdpZddqlmZ ddsZddwZdWdxdd{Zdd}ZddZddZddWddWddWdWej        dWdWf
ddZddZej$        d|d            Z e            Z ej        em          Zded<   g Zded<    e            ZejG        dd            ZddZdddZddZddZ	 dM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	 dMddZ G d de
j                  ZddZddZddĄZddƄZdǐdgZdɐdgZg dˢZeez   ez   Zds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dfdZÐdf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 d d          Z ef            Zݐded<   dd֐dZސdאdZej$        d|d            ZdsdZdؐdZdِdZdڐdZdDdZdhd Zdېd!Zdܐd#Ze8j        dݐd%            Ze8j        dݐd&            Ze8j        dސd'            Zdߐd(Zi Zd)ed*<   dd,Zdd-Z G d. d/          Z G d0 d1eҦ          Zdd4Zd5Zd
ed6<   dd7Ze$d|d8            Ze$d|d9            Zdd:ZdWadsd;Zdd<Ze8j        dd>            Ze8j        dd@            ZddBZdS (  a  
Utility functions and classes used throughout the TorchDynamo system.

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

This is an internal module that provides shared functionality used across the Dynamo codebase.
    )annotationsN)CounterOrderedDict)AbstractContextManagercontextmanager)is_dataclass)	lru_cache)CodeTypeMethodWrapperType)	AnyCallablecastClassVarGenericOptionaloverloadTypeVarUnion)Literal	ParamSpec	TypeAlias	TypeGuardTypeIs)fx)_instruction_counter_len_torch_function_stack_pop_torch_function_stack_push_on_torch_function_stack)enable_python_dispatcher)MetricsContextRuntimeMetricsContext)	CompileIdSourceTracingContext)is_sparse_compressed)justknobs_checklog_chromium_event_internallog_compilation_eventrecord_chromium_event_internalsignpost_event)_format_graph_codelazy_format_graph_code)_WaitCounter)LazyModuleMixin)
has_tritonhas_triton_package)RemovableHandle   )_get_flat_args)		Container	Generator	ItemsViewIterableIteratorKeysViewMappingSequence
ValuesView)ExecutionRecord)InstructionTranslatorInstructionTranslatorBase)VariableTracker)DeviceLikeType)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_logzAhttps://pytorch.org/docs/main/torch.compiler_troubleshooting.htmlz;https://pytorch.org/docs/main/torch.compiler_nn_module.htmlzSee z& for more information and limitations.zdict[str, list[float]]compilation_time_metricsdict[str, float]cumulative_time_spent_nsc                      e Zd ZdZdZdZdS )ReInplaceTriggerr2         N)__name__
__module____qualname__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPSrH       M/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_dynamo/utils.pyrV   rV      s        LLJJJr_   rV   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triggerrV   bytesintreturnNonec                P    |dk    r| j         d|j         xx         |z  cc<   d S d S Nr   missed_bytes_)rc   name)clsrd   re   s      r`   add_missed_bytesz"ReinplaceCounters.add_missed_bytes   sA    A::K6667775@77777 :r_   countc                F    |dk    r| j         d| xx         |z  cc<   d S d S Nr   missed_tensors_)rc   )rm   rd   ro   s      r`   add_missed_opportunitiesz*ReinplaceCounters.add_missed_opportunities   s?    A::K3'33444=44444 :r_   c                8    | j                                          d S N)rc   clearrm   s    r`   rv   zReinplaceCounters.clear   s    r_   c                `    d}t           D ]#}|| j                            d| d          z  }$|S rq   )rV   rc   getrm   sumrd   s      r`   get_total_missedz"ReinplaceCounters.get_total_missed   sB    ' 	C 	CG3;??#>W#>#>BBBCC
r_   c                j    d}t           D ](}|| j                            d|j         d          z  })|S rj   )rV   rc   ry   rl   rz   s      r`   get_total_missed_bytesz(ReinplaceCounters.get_total_missed_bytes   sD    ' 	F 	FG3;??#A7<#A#A1EEECC
r_   c                D    | j         rt          dd| j                    d S d S )Ninductorreinplace_counters)rc   r*   rw   s    r`   logzReinplaceCounters.log   s6     ; 	J:';S[IIIII	J 	Jr_   N)rd   rV   re   rf   rg   rh   )rd   rV   ro   rf   rg   rh   rg   rh   rg   rf   )rY   rZ   r[   collectionsdefaultdictrf   rc   __annotations__classmethodrn   rs   rv   r|   r~   r   rH   r_   r`   rb   rb      s         1H1H1M1MGMMMM A A A [A
 > > > [>    [    [    [ J J J [J J Jr_   rb   rows-Union[list[tuple[str, Any]], list[list[Any]]]headers!Union[tuple[str, ...], list[str]]rg   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     r`   	<genexpr>ztabulate.<locals>.<genexpr>   sG       
 
),DIIc#smm$$
 
 
 
 
 
r_   )tabulateImportErrorr   	itertoolschain)r   r   r   s      r`   r   r      s    
  w 777 
 
 
yy 
 
09	40P0P
 
 
 
 
 	
 	
 	

s    =AArh   c                     t           dz   a d S Nr2   )
curr_framerH   r_   r`   increment_framer      s    aJJJr_   c                 n    t                                            t                                           dad S Nr   )rT   rv   rR   r   rH   r_   r`   reset_frame_countr      s/    ""$$$""$$$JJJr_   !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    r`   (register_hook_for_recompile_user_contextr   
  s*      '#% ##D)))))r_   c                     t           S ru   )r   rH   r_   r`   #get_hook_for_recompile_user_contextr         ##r_   cntrf   c                    t           | z  a d S ru   )op_count)r   s    r`   increment_op_countr     s    OHHHr_   c                     i } t                                           D ]\  }}|dz  | |<   |                     dd          |                     dd          z   | d<   | S )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rT   itemsry   )total_by_keyphasetimings      r`   calculate_time_spentr   &  sx    L17799 + +v$slU&2&6&6' '2A66'7L"# r_   c            	         t                      } d}|                                 D ]\  }}| d| dt          |d           }t          |           d S )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       r`   print_time_reportr   6  se    '))L
C"((** / /
U..s..U5!__..	#JJJJJr_   r    _METRICS_CONTEXTr!   _RUNTIME_METRICS_CONTEXTc                     t           S ru   )r   rH   r_   r`   get_metrics_contextr   Y  s    r_   c                     t           S ru   )r   rH   r_   r`   get_runtime_metrics_contextr   ]  r   r_   c                      e Zd ZdZdZdZdZdS )CompileEventLogLevelz
    Enum that loosely corresponds with a "log level" of a given event.

    CHROMIUM_EVENT: Logs only to tlparse.
    COMPILE_EVENT: Logs to tlparse + PT2 Compile Events
    COMPILATION_METRIC: Logs to tlparse, PT2 Compile Events, and dynamo_compile
    r2   rW   rX   N)rY   rZ   r[   __doc__CHROMIUMPT2_COMPILECOMPILATION_METRICrH   r_   r`   r   r   a  s,          HKr_   r   c                     e Zd ZdZedej        fd)d            Ze	 d*d+d            Ze	 d*d,d            Z	ed-d            Z
edej        fd.d            Zed/d            Zeej        fd0d            Zed1d            Zed1d            Zed*d2d            Ze	 d3d4d             Zed1d!            Zed5d(            ZdS )6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   metadatarP   time_nsOptional[int]	log_levelr   rg   rh   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         r`   r   z$CompileEventLogger.log_instant_event  s     ?lnnG022,555$)!!.:::$(!!|   	&&+@	
 	
 	
 	
 	
r_   F	overwriteboolobjectc                :   t                      }|                                }|t          j        k    r |j        | fi | dS |t          j        k    r8|                                }| |vrt          d|            |j        | fi | dS |t          j        k    sJ |                                }| |k    rt          d          t                      }|
                                st          d          |                    ||            |j        | fi | dS )z
        Centralized API for adding data to various events
        Log an event to a toplevel "dynamo" event or metrics context
        depending on log level.
        zError: specified log level PT2_COMPILE, but the event %s is not logged to pt2_compile_events. Make sure the event is active and you passed log_pt2_compile_event=True to dynamo_timedzLog level is COMPILATION_METRIC, but event_name isn't the toplevel event. CompilationMetrics must be logged to the toplevel event. Consider using `log_toplevel_event_data` directly.z[No metrics context is in progress. Please only call this function within a metrics context.N)r   get_pt2_compile_substackr   r   add_event_datar   r   r   get_outermost_eventr   in_progressupdate)r   r   r   r   r   pt2_compile_substack	top_eventmetrics_contexts           r`   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?????r_   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       r`   add_toplevelzCompileEventLogger.add_toplevel  s[     .//CCEE	i   	#Iy)PPxPPPPPr_   r   r   rf   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          r`   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:::::r_   r2   c                    t                      }|                                }|t          d          t                              ||| |           dS )zX
        Increments a value on the toplevel metric. By default, logs to metric.
        N_No toplevel event active. Please only call this function within a metrics context/dynamo_timed.)r   r   r   r   r   r   r   r   r   r   s        r`   increment_toplevelz%CompileEventLogger.increment_toplevel  s]     122 4466	q   	$$Y	3FFFFFr_   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          r`   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;;;;;r_   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        r`   add_to_set_toplevelz&CompileEventLogger.add_to_set_toplevel)  s]     122 4466	q   	%%iCGGGGGr_   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     r`   chromiumzCompileEventLogger.chromium=  sA     	#,5	
 	
AF	
JR	
 	
 	
 	
 	
r_   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     r`   pt2_compilezCompileEventLogger.pt2_compileG  sA     	#,8	
 	
DI	
MU	
 	
 	
 	
 	
r_   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     r`   compilation_metricz%CompileEventLogger.compilation_metricT  s8     	' 3Y	
 	
BJ	
 	
 	
 	
 	
r_   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      r`   instantzCompileEventLogger.instant`  s3     	,,'+?+H	
 	
 	
 	
 	
r_   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      r`   try_add_pt2_compilez&CompileEventLogger.try_add_pt2_compilel  sC     )** 	F022''
??h?????r_   	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       r`   try_zCompileEventLogger.try_x  sV    
 )** 	F-//**,, 	F	4"6"""""r_   )
r   r   r   rP   r   r   r   r   rg   rh   F)
r   r   r   r   r   r   r   r   rg   rh   )r   r   r   r   r   r   rg   rh   )
r   r   r   r   r   r   r   rf   rg   rh   )r   r   r   rf   r   r   rg   rh   )
r   r   r   r   r   r   r   r   rg   rh   )r   r   r   r   r   r   rg   rh   )r   r   r   r   rg   rh   )r   r   r   r   rg   rh   ru   )r   r   r   rP   r   r   rg   rh   )r  r  r	  r
  r  r  rg   rh   )rY   rZ   r[   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  rH   r_   r`   r   r   o  s#        0  "&*>*G	
 
 
 
 \
*   +@ +@ +@ +@ \+@Z ;@Q Q Q Q \Q ; ; ; \;<  *>*QG G G G \G  < < < \<<  +?*QH H H H \H& 
 
 
 \
 

 

 

 \

 	
 	
 	
 	
 \	
 LP	
 	
 	
 	
 \	
 	@ 	@ 	@ \	@ 	# 	# 	# \	# 	# 	#r_   r   Fr   
phase_nameOptional[str]r   r   r   Optional[dict[str, object]]dynamo_compile_column_us
compile_idOptional[CompileId]is_backwardOptional[bool]log_waitcounterwaitcounter_name_overrideGenerator[Any, None, None]c	           
   #  `	  K   |r|}	| }
n| }	d}
| t           vr
g t           | <   i }|r|                    |           |
r|                    d|
i           ||                    d|i           t                      }t          j                    }|                    |	||||           t          j                            |  d          g}|r=|r|n| }|	                    t          d|                                                      t          j        j                                        du}|r|                    d          sJ t!          t"          d          sdt"          _        t"          xj        d	z  c_        t"          j        d	k    rl|	                    t          d
                                                     |s6d}|	                    t          |                                                     	 t'          j                    5 }|D ]}|                    |           dV  ddd           n# 1 swxY w Y   t          j                    }||z
  }t           |          	                    |dz             |                    |	|i |||           |rt.          |	xx         |z  cc<   t"          xj        d	z  c_        t"          j        dk    }|dz  }|rVt1                      }|                                r0|                    ||           |r|                    d|           dS dS dS t7                      }|                    ||           |r"|d| d}|                    d||           dS dS dS # t          j                    }||z
  }t           |          	                    |dz             |                    |	|i |||           |rt.          |	xx         |z  cc<   t"          xj        d	z  c_        t"          j        dk    }|dz  }|rSt1                      }|                                r/|                    ||           |r|                    d|           w w w t7                      }|                    ||           |r |d| d}|                    d||           w w w xY w)a  
    dynamo_timed is a context manager
    By wrapping a function in dynamo_timed, we can get a few things:

    1) Optionally log timings to pt2_compile_events.
    2) Optionally log timings to CompilationMetrics (dynamo_compile).
    3) Optionally log chromium events.
    4) Optionally increment a WaitCounter.
    5) Store a record in compilation_time_metrics
       For example:

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

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

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

    Params:
    - key: key into compile_time_metrics. If phase_name is not provided, this is
      also the event name used for pt2_compile_events logs and chromium events.
    - phase_name: Optional override for the event name.
    - log_pt2_compile_event: Whether to log a pt2 compile event internally.
    - metadata: Extra metadata to put in pt2_compile_events.
    - dynamo_compile_column_us: If provided, updates the specified CompilationMetrics
      field to be logged to dyname_compile column. We expect all columns to be _us;
      therefore, the field name must end with "_us".
    - compile_id: In the typical case, this parameter should not be needed. Use to
      supply the compile_id for those cases where we want to log a compile_id where
      it's not naturally available, e.g., for runtime autotuning.
    - is_backward: Specify forward/backward directly when not available in a
      CompileContext, e.g., during runtime autotuning.
      that support it.
    - log_waitcounter: If set, we'll log a waitcounter of the form "pytorch.dynamo_timed.{key}"
    Nfn_namer  z (dynamo_timed)zpytorch.wait_counter._usdepthr   r2   z#pytorch.wait_counter.dynamo_compilez.pytorch.wait_counter.compile_runtime_overheadsr     duration_usT)r  
is_runtime
is_forward)rR   r   r   r   r   log_event_starttorchprofilerrecord_functionr   r-   guard_guardsCompileContextcurrent_compile_idendswithhasattr_dynamo_timed_tlsr  
contextlib	ExitStackenter_contextlog_event_endrT   r   r   r   r   )r   r  r   r   r  r  r  r  r  r   r  event_metadatar   start_nscx_mgrswc_nameis_compile_time
runtime_wcstackcxend_nstime_spent_nsis_outer_eventr!  r   runtime_contextextras                              r`   dynamo_timedr@    s     l  


***(* %N (h''' 4y'2333}k:;;;(A(C(CL|~~H  Hn.CZ  
 	&&#'>'>'>??!G  P/HQ++c|$EG$E$EFFLLNNOOOm2EEGGtSO A'0077777 ('22 	(&'#1$ "a''NN<(MNNTTVVWWW" AM
|J77==??@@@(Q!## 	u ( (##B''''EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 ) %,,]S-@AAA""H.CZ	
 	
 	
 $ 	Q %Z000MA000 ##q(##.49N'4/K Q"5"7"7"..00 N#--.FTTT% N'11-MMMMMN NN N #>"?"?))*BKPPP! Q&0&**5o E
 $--m[%PPPPP9	Q 	Q,Q Q9 ) %,,]S-@AAA""H.CZ	
 	
 	
 $ 	Q %Z000MA000 ##q(##.49N'4/K Q"5"7"7"..00 N#--.FTTT% N'11-MMMMNN #>"?"?))*BKPPP! Q&0&**5o E
 $--m[%PPPP9	Q,Qs1   9M: H7+M: 7H;;M: >H;?M: :D3R-reprLiteral['str']	aggregatec                    d S ru   rH   rA  rC  s     r`   compile_timesrF    s    ILr_   Literal['csv']tuple[list[str], list[object]]c                    d S ru   rH   rE  s     r`   rF  rF    s	     &)Sr_   -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]rg   r   c                ~    r |t          |                     S d                    t          ||                     S )Nr   )r{   r   r   )rL  rN  rC  s     r`   fmt_fnzcompile_times.<locals>.fmt_fn4  s=     	(73v;;'''yyWf--...r_   c                F    g | ]}| t           |         d            fS )c                
    | dS )Nz.4frH   xs    r`   <lambda>z*compile_times.<locals>.<listcomp>.<lambda>;  s
    jj r_   rN  )rR   )r   krQ  s     r`   
<listcomp>z!compile_times.<locals>.<listcomp>:  sG     
 
 
 /2<P<PQQQR
 
 
r_   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                ,    g | ]} |d            S )c                
    | dS )Nz.6frH   rT  s    r`   rV  z*compile_times.<locals>.<listcomp>.<lambda>C  s
    1

 r_   rW  rH   )r   vrQ  s     r`   rY  z!compile_times.<locals>.<listcomp>B  s<     
 
 
 F122333
 
 
r_   N)rL  rM  rN  rO  rg   r   )r   rR   r   rL  listkeys)rA  rC  r   r   rL  r   rQ  s    `    @r`   rF  rF  $  s      GJ / / / / / / /
 u}}
 
 
 
-
 
 
 3x&BCCCC
	
 
 
 
-4466
 
 
 /4466774r_   c                 X    t                               t          dd                     d S )Nr   TrE  )r   inforF  rH   r_   r`   dump_compile_timesrc  K  s'    HH]66677777r_   c                  (    e Zd ZdddZddZddZdS )DuplicateWarningChecker   maxsizerf   rg   rh   c                <    || _         |                                  d S ru   )rg  reset)selfrg  s     r`   __init__z DuplicateWarningChecker.__init___  s    

r_   c                ,    t                      | _        d S ru   )r   setrj  s    r`   ri  zDuplicateWarningChecker.resetc  s    *5--r_   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)rm  move_to_endrF   verboselenrg  popitemrj  r   s     r`   addzDuplicateWarningChecker.addf  s    $(??H  4 000> u !DHSMdh--$,..  e ,,, dh--$,..tr_   N)rf  )rg  rf   rg   rh   r   )r   ro  rg   r   )rY   rZ   r[   rk  ri  rw  rH   r_   r`   re  re  ^  sU            8 8 8 8	 	 	 	 	 	r_   re  contextlib.ExitStackc                     t           j                            dd          dk    } | rt                      S t	          j                    S )NTORCH_COMPILE_DEBUG01)osenvironry   add_file_handlerr/  r0  )compile_debugs    r`   setup_compile_debugr  u  s@    JNN#8#>>#EM "!!!!!!r_   c                 8    t                                            d S ru   )graph_break_dup_warning_checkerri  rH   r_   r`   reset_graph_break_dup_checkerr  ~  s    #))+++++r_   c                    t           j                            t                      d          } t          j        | d           t          j        t           j                            | d                    t          j        d                                         t          j
                    }|                    fd           |S )NtorchdynamoTexist_okz	debug.logztorch._dynamoc                 .                                    S ru   removeHandlerlog_file_handlerloggers   r`   rV  z"add_file_handler.<locals>.<lambda>  s    v334DEE r_   )r}  pathr   get_debug_dirmakedirsloggingFileHandler	getLogger
addHandlerr/  r0  callback)log_path	exitstackr  r  s     @@r`   r  r    s    w||MOO];;HK4((((*27<<++N+NOO//F
&'''$&&IEEEEEFFFr_   c                 0   t          j                    } t          j        tt	          j        t          j                  t          j        j        	                                D ]0
                               |                     fd           1| S | S )Nc                 .                                    S ru   r  r  s   r`   rV  z setup_log_file.<locals>.<lambda>  s    v';';<L'M'M r_   )r/  r0  rF   log_file_namer  r  r%  _logging	_internalget_loggersr  r  )r  r  r  s    @@r`   setup_log_filer    s    $&&I'".v/CDDn.::<< 	O 	OF.///MMMMMNNNNr_   exc	Exceptioncoder
   c                n    t                       d|j         dt          |           j         d|j         dS )Nz/error_recordings/_z.rec)r  co_nametyperY   co_firstlineno)r  r  s     r`   gen_record_file_namer    sR    oo ? ?? ?S		"? ?%)%8? ? ? ?r_   filenameexec_recordr=   c                   	 t           j                            |           rt                              d|            d S t          j        t           j                            |           d           t          | d          5 }|                    |           d d d            d S # 1 swxY w Y   d S # t          $ r t          
                    d|            Y d S w xY w)Nz9Unable to write execution record %s; file already exists.Tr  wbz#Unable to write execution record %s)r}  r  existsr   warningr  dirnameopendumpr  	exception)r  r  fs      r`   write_record_to_filer    s*   
G7>>(## 	$KKKX     K11DAAAAh%% $  ###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ G G G;XFFFFFFGs;   :B1 AB1 B$B1 $B((B1 +B(,B1 1%CCgfx.Graphc                :    d}| j         D ]}d|j        v r|dz  }|S )Nr   callr2   )nodesop)r  cns      r`   count_callsr    s2    	AW  QT>>FAHr_   rU  c                    | S ru   rH   rT  s    r`   identityr    s    Hr_   r   c                `    	 t          |            dS # t          $ r Y dS t          $ r Y dS w xY wNTF)hash	TypeError
ValueErrorrT  s    r`   hashabler    sR    Qt   uu   uus    
-	--r	  r  c                     d S ru   rH   r	  r  s     r`   nothingr    s    Dr_   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 equalityrg   rh   c                "    i | _         i | _        d S ru   rL  refsrn  s    r`   rk  zExactWeakKeyDictionary.__init__  s    &(;=			r_   r   r   c                6    | j         t          |                   S ru   )rL  idrv  s     r`   __getitem__z"ExactWeakKeyDictionary.__getitem__  s    {2c77##r_   Ndefaultc                R    | j                             t          |          |          S ru   )rL  ry   r  )rj  r   r  s      r`   ry   zExactWeakKeyDictionary.get  s    {r#ww000r_   r   c                .    t          |          | j        v S ru   )r  rL  rv  s     r`   __contains__z#ExactWeakKeyDictionary.__contains__  s    #ww$+%%r_   r   c                     t          |           j        vr!t          j        | fd           j        <   | j        <   d S )Nc                .                                   S ru   )
_remove_id)refidxrj  s    r`   rV  z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>  s    $//#:N:N r_   )r  r  weakrefr  rL  )rj  r   r   r  s   `  @r`   __setitem__z"ExactWeakKeyDictionary.__setitem__  sR    ggdi$[.N.N.N.N.NOODIcN Cr_   r  rf   c                N    || j         v r| j         |= || j        v r
| j        |= d S d S ru   r  )rj  r  s     r`   r  z!ExactWeakKeyDictionary._remove_id  s;    $+C $)	# r_   c                j    | j                                          | j                                         d S ru   )r  rv   rL  rn  s    r`   rv   zExactWeakKeyDictionary.clear  s.    	r_   r   )r   r   rg   r   ru   )r   r   r  r   rg   r   )r   r   rg   r   )r   r   r   r   rg   rh   r  rf   rg   rh   )rY   rZ   r[   r   rk  r  ry   r  r  r  rv   rH   r_   r`   r  r    s        ff> > > >$ $ $ $1 1 1 1 1& & & &! ! ! !        r_   r  objr   allowed_typestype[T]	TypeIs[T]c                    d S ru   rH   r  r  s     r`   istyper    s    >Acr_   )tuple[type[list[T]], type[tuple[T, ...]]]c                    d S ru   rH   r  s     r`   r  r    s	     r_   Iterable[type]c                    d S ru   rH   r  s     r`   r  r    s    @Cr_   c                    t          |t          t          t          f          rt	          |           |v S t	          |           |u S )zisinstance() without subclasses)
isinstancetupler_  rm  r  r  s     r`   r  r    s=    -%s!344 *CyyM))99%%r_   rX      r   c                    t           j        dk    rt          | t                    rdS t          | t          j                  p| t          j        u S )Nr  T)sysversion_infor  _builtin_final_typing_classestyping_Finalr   r   s    r`   	is_typingr    sE     7""z%9V'W'W"teV]++Fu/FFr_   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    r`   is_numpy_int_typer    sQ     uGHHHHIII		
  r_   c                v    t           sdS t          | t           j        t           j        t           j        f          S r  )r  r  float16float32float64r  s    r`   is_numpy_float_typer  -  s8     uJJJ	
  r_   Callable[..., T]*TypeGuard[functools._lru_cache_wrapper[T]]c                    d S ru   rH   r  s    r`   is_lru_cache_wrapped_functionr  ;  s	     25r_   ,TypeGuard[functools._lru_cache_wrapper[Any]]c                    d S ru   rH   r  s    r`   r  r  A  s	     473r_   c                z    t          | t          j                  o!t          t	          j        | d                    S )N__wrapped__)r  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr  s    r`   r  r  G  s<     eY9:: {um44@ @ r_   r   
_FuncTypesMTypeIs[Union[_FuncTypes, torch._ops.OpOverloadPacket, torch._ops.OpOverload]]c                    t          |           p/t          | t          j        j        t          j        j        f          S ru   )r  r  r%  _opsOpOverloadPacket
OpOverloadr  s    r`   is_function_or_wrapperr  W  s9     u 
+UZ-BC" " r_   TypeIs[_FuncTypes]c                z    t          | t          j        t          j        t          j        t          j        f          S ru   )r  typesFunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper  s    r`   r  r  _  s6     %&'		
  r_   )__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 ru   )r  r  GetSetDescriptorTyper!  r"  MemberDescriptorTyper   r  s    r`   is_wrapper_or_member_descriptorr/    s>      &&'&#	
  r_   fnc                ,    t          |           d         S r   ) unwrap_with_attr_name_if_wrapper)r0  s    r`   unwrap_if_wrapperr3    s    +B//22r_   tuple[Any, Optional[str]]c                    t          |           r/t          j        | dd          rt          j        | d|           } d}nd }| |fS )N_torchdynamo_inlineF)r  r  r  )r0  	attr_names     r`   r2  r2    sT     2 71"6KUSS #B(=rBB)			y=r_   TypeGuard[np.ndarray]c                H    t           sdS t          | t           j                  S r  )r  r  ndarrayr  s    r`   is_numpy_ndarrayr;    s!     u%$$$r_   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	ParameterrF   traceable_tensor_subclasses_subclassesrI   r  )r  tensor_lists     r`   istensorrC    sO     	% 
	+% %K
 !2!= ??K#{###r_   modc                ,    t          | t                    S ru   )r  r.   )rD  s    r`   is_lazy_modulerF    s    c?+++r_   rf  c                     t          |   d S ru   )r   r	  s    r`   
print_oncerI    s    	4LLLLr_   val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 closurerg   r   c                      S ru   rH   rT  s   r`   r  zmake_cell.<locals>.f  s    r_   Nr2   r   )rg   r   )__closure__rt  )rJ  r  rU  s     @r`   	make_cellrO    sW    A      =$Q]););q)@)@)@@=r_   &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 ru   as_proxyr   args     r`   r   z$proxy_args_kwargs.<locals>.<genexpr>  s*      ::c3<<>>::::::r_   c                >    i | ]\  }}||                                 S rH   rS  )r   r   rV  s      r`   
<dictcomp>z%proxy_args_kwargs.<locals>.<dictcomp>  s&    KKKSS\\^^KKKr_   r2   unimplemented_v2)typestrz&Failed to convert args/kwargs to proxyzcall_function args: r   z7Missing `as_proxy()` implementation for some arg/kwarg.gb_typecontextexplanationhintsfrom_exc)	r  r   NotImplementedErrorr  rZ  variables.baser[  r_  rL  )r	  r  
proxy_argsproxy_kwargserZ  r[  s          r`   proxy_args_kwargsrg    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 Nr   rf   r^  s    r`   	to_int_msrm    s    944#a$h--/r_   c                .    | d nt          | dz            S )Ni@B rk  rl  s    r`   	to_int_usro    s    944#a)m"4"44r_   rX   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<   e>Z?dBedC<   dZ@dedD<   dZAdedE<   dZBdedF<   dZCdedG<   dZDdedH<   dZEdedI<   dZFdedJ<   dZGdedK<   dZHdedL<   dZIdMedN<   dZJdedO<   dPZKd#edQ<   dZLdedR<   dZMd#edS<   dZNd#edT<   dZOdedU<   dZPdedV<   dZQdedW<   dZRdedX<   dZSdedY<   dZTdedZ<   dZUded[<   dZVded\<   dZWded]<   dZXded^<   dZYded_<   dZZded`<   dZ[deda<   dZ\dedb<   dPZ]d#edc<   e^dhdg            Z_dS )iCompilationMetricsNr  r  	frame_keyr  co_filenamer   r  
cache_sizeaccumulated_cache_sizeguard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_countrh  
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_configr#  num_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usr!  !dynamo_cumulative_compile_time_us'aot_autograd_cumulative_compile_time_us#inductor_cumulative_compile_time_us,inductor_code_gen_cumulative_compile_time_ustriton_compile_time_usruntime_cudagraphify_time_usruntime_triton_autotune_time_us%dynamo_compile_time_before_restart_us distributed_ephemeral_timeout_usstructured_logging_overhead_us!remote_fx_graph_cache_get_time_us!remote_fx_graph_cache_put_time_us#backward_cumulative_compile_time_usend_time_uspre_grad_pass_time_uspost_grad_pass_time_usjoint_graph_pass_time_usrf   log_format_versioninductor_configremote_cache_version"inductor_fx_remote_cache_hit_count#inductor_fx_remote_cache_miss_count%inductor_fx_remote_cache_backend_type!inductor_fx_remote_cache_hit_keys"inductor_fx_remote_cache_miss_keyscuda_versiontriton_versionzOptional[dict[str, bool]]feature_usagecompile_time_autotune_time_usFr"  
gc_time_ustensorify_float_attempttensorify_float_successtensorify_float_failureguard_latency_usrecompile_reasonnum_graph_breakstriton_kernel_compile_times_usir_countcudagraph_skip_reasonpython_version!pgo_put_remote_code_state_time_us!pgo_get_remote_code_state_time_usparam_numelparam_bytesparam_countrecompile_user_contexts#inline_inbuilt_nn_modules_candidatemetricsrP   rg   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   rg   rh  c                    | | dz  nd S )Ng    .ArH   r  s    r`   us_to_sz*CompilationMetrics.create.<locals>.us_to_s`  s    #)#56C<<4?r_   c                    | | dz  nd S rj  rH   r  s    r`   us_to_msz+CompilationMetrics.create.<locals>.us_to_msc  s    %+%76T>>TAr_   Optional[Any]r  c                    d	d| d S t          | t          t          f          sdS d                    fdt	          |           D                       S )
Nitemr   rg   r   c                D    	 t          |           S # t          $ r Y dS w xY wN	<unknown>)r   r  )r  s    r`   safe_strzFCompilationMetrics.create.<locals>.collection_to_str.<locals>.safe_strg  s5    't99$  ' ' '&;;'s    
r  ,c              3  .   K   | ]} |          V  d S ru   rH   )r   r  r  s     r`   r   zGCompilationMetrics.create.<locals>.collection_to_str.<locals>.<genexpr>s  s+      FFtHHTNNFFFFFFr_   )r  r   rg   r   )r  rm  r_  r   sorted)r  r  s    @r`   collection_to_strz4CompilationMetrics.create.<locals>.collection_to_strf  sk    ' ' ' ' ~tfsDk22 #"{88FFFFvf~~FFFFFFr_   c                p    | d S 	 t          j        t          |                     S # t          $ r Y dS w xY wr  )jsondumpsr_  r  r  s    r`   collection_to_json_strz9CompilationMetrics.create.<locals>.collection_to_json_stru  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  r  N)r  r   rg   rh  )r  r   rg   r   )r  r  rg   r  rH   )ry   r   )	rm   r  r  r  r  r  legacy_metricsall_metricsr  s	            r`   createzCompilationMetrics.createX  s_   	@ 	@ 	@ 	@	B 	B 	B 	B	G 	G 	G 	G	# 	# 	# 	# "''++o">">??+27?@@, , '.gEFF' ' (/wABB( (  'wJKK    *1>??* * 2:?@@2 2 2:?@@2 2 .5W<==. ./
 
8 4373 <M;LOO?@@<
 <
78 =N<MOO@AA=
 =
89 9O8NOO<==9
 9
45 !__\22
7A$KC
OOOtL!s!![!!!r_   )r  rP   rg   rq  )`rY   rZ   r[   r  r   rr  r  rs  r  rt  ru  rv  rw  rx  ry  rz  r{  r|  r}  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r  r  r  r  r!  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  LOG_FORMAT_VERSIONr  r  r  r  r  r  r  r  r  r  r  r  r"  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  rH   r_   r`   rq  rq    sf         $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''''!%J%%%%(,,,,,7;%;;;;7;%;;;;#'M''''!%K%%%%7;%;;;;=A+AAAA9='====BF0FFFF,0000026 666659#9999;?)????6:$::::48"88887;%;;;;7;%;;;;9='====!%K%%%%+/////,00000.2222200000%)O))))*.....8<&<<<<9='====;?)????7;%;;;;8<&<<<<"&L&&&&$(N((((/3M333337!7777!&J&&&& $J$$$$.22222.22222266666(,,,,,&*****&*****48"8888"H""""+/////$(N((((7;%;;;;7;%;;;; "&K%%%% "&K%%%% "&K%%%%266666:?'????Q" Q" Q" [Q" Q" Q"r_   rq  @   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   rr  r  rs  r  rt  ru  rv  rw  rx  ry  rz  r  r  r  r  r  r  r  r  r  r  )r   r   r   rr  r  rs  r  rt  ru  rv  rw  rx  ry  rz  r  r  r  r  r  r_  r  r  r  r  r  )r  event_loggerr   s      r`   #add_compilation_metrics_to_chromiumr    s    -..L1133J +	M'< 7M5'+-+M!"!; 7 *+)<)HD$%%%d -.,B,ND'(((TX ()'8'DD"###$%&%C+o;       r_   c                 n    dd}  | t          j                              }t          j        |d          S )NdrP   rg   c                L    h dfd|                                  D             S )N>   rs  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 rH   )r  rm  r  )r   r   r   	blocklists      r`   rX  zJ_get_dynamo_config_for_logging.<locals>.clean_for_json.<locals>.<dictcomp>  sM     
 
 
U)## *UC"8"8Ce###r_   )r   )r  r  s    @r`   clean_for_jsonz6_get_dynamo_config_for_logging.<locals>.clean_for_json  sG    
 
 
	.
 
 
 
ggii
 
 
 	
r_   T)	sort_keys)r  rP   rg   rP   )rF   get_config_copyr  r  )r  config_dicts     r`   _get_dynamo_config_for_loggingr    sE    
 
 
 
< !.!7!9!9::K:kT2222r_   c                 :    G d dt           j                  } t                      }d}d}t          j        j        r>	 t          j        j                                        }n# 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   rg   c                j    	 t                                          |          S # t          $ r Y dS w xY w)NzValue is not JSON serializable)superr  r  )rj  r  	__class__s     r`   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   rg   r   )rY   rZ   r[   r  __classcell__r  s   @r`   TypeSafeSerializerr    s=        	8 	8 	8 	8 	8 	8 	8 	8 	8 	8r_   r  Nz!Inductor Config cannot be pickledT)rm   skipkeysr  z(Inductor Config is not JSON serializable)r  JSONEncoderrm  r%  	_inductorrF   r  r  AttributeErrorr   r  r   rw  r_  r  r  )r  keys_to_scrubinductor_conf_strinductor_config_copyr   rJ  s         r`   %_scrubbed_inductor_config_for_loggingr    s   8 8 8 8 8T- 8 8 8 "eeM D	D#(?#9#I#I#K#K  >* 	D 	D 	D C	D '	K06688 : :S!#s++ +!%%c***c3'' :04S		(-$ . .(-- $
$&	! ! !  	K 	K 	K J	K s$   #A! !A76A7=BD	 	DDstart_time_nsend_time_nsr  exc_typeOptional[type[BaseException]]	exc_valueOptional[BaseException]c                   t           j        j                                        r 	 ddlm} |}d}n# t          $ r d }d }Y nw xY wd }d }|                    d          }|s#t           j        j	        
                                }|| dz  |dz  |r|j        nd |rt          |          nd t          t           j                                                  t!                      t"          j        t"          j        t)                      t           j        j        t/                      rt0          j        nd||t4          j        d}	t6                              i |	|          t:                                         d}
j        d	u rd
|
z   }
j         du r|
dz   }
t           j        !                    |
fdd	|           tE                     t"          j#        rtI                     d S d S )Nr   )REMOTE_CACHE_VERSION_ManifoldCacher  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 rH   )r  rm  r_  )r   rX  r^  s      r`   rX  z@record_compilation_metrics.<locals>.<lambda>.<locals>.<dictcomp>q  sG     
 
 
1 *Q,,3tAwww!
 
 
r_   )dataclassesasdictr   )r  s   r`   rV  z,record_compilation_metrics.<locals>.<lambda>q  s9     
 
#*+>??EEGG
 
 
 r_   )record_logging_overheadr  )%r%  r  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cacher  ModuleNotFoundErrorry   r)  r*  r+  r[   r   ro  r  get_structured_logging_overheadr  rF   suppress_errorsinline_inbuilt_nn_modulesr  versioncudar/   triton__version__r  rq  r  r  r   r#  r"  trace_structuredr  r  r(   )r  r  r  r  r  r  r  r  r  common_metricsrl   r  s              @r`   record_compilation_metricsr3  8  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
 !&$."d*.6@X**D)2<s9~~~*3N::<<+
 +
 899"("8,2,L@BB*0:D&,," 41V+# N( -334Q~4Q4QRR 3444 D%..}%--j 	N##	
 	
 	
 	
 !&  $   $ ((;<<< % 31222223 3s   
1 AA)on_exitnew_sizec                    t          t                    | k    r1t                                           t          t                    | k    1t          j        t          |           }|ad S )Nr  )rt  r  popleftr   deque)r5  	new_deques     r`   set_compilation_metrics_limitr:    s`    
"
#
#h
.
.$$&&& "
#
#h
.
.!"6xHHHI$r_   c                 8    t                                            d S ru   )r  rv   rH   r_   r`   clear_compilation_metricsr<    s         r_   list[CompilationMetrics]c                 *    t          t                    S ru   )r_  r  rH   r_   r`   get_compilation_metricsr?    s    $%%%r_   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	 	 d.d/dZd*d Z	 d0d1d"Z	 d0d2d%Z	 	 d3d4d&ZdS )5ChromiumEventLoggera  Logs chromium events to structured logs. tlparse will concatenate these into a perfetto UI link.

    See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.yr4qxyxotyw for
    a specification of the Chromium Event JSON format.
    rg   	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.
        r9  )r-  tlsr9  rn  s    r`   	get_stackzChromiumEventLogger.get_stack  s5    
 48W%% 	"8>!DHN8>!r_   r  c                B    |                                  }|r|d         ndS )zv
        Get the outermost event name (i.e. the longest running event)
        or None if the stack is empty.
        r   N)rE  )rj  r9  s     r`   r   z'ChromiumEventLogger.get_outermost_event  s&    
    *uQxxd*r_   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-  rD  r   rn  s    r`   r   z,ChromiumEventLogger.get_pt2_compile_substack  s9    
 48344 	1800,.DH)800r_   rP   c                \    t          | j        d          si | j        _        | j        j        S )N
event_data)r-  rD  rI  rn  s    r`   get_event_dataz"ChromiumEventLogger.get_event_data  s+    tx.. 	%"$DHx""r_   rh   c                    t          j                    | _        ddlm} |j        r#|j         dt          j                     | _        n%t          t          j                              | _        t                              d| j                   d S )Nr2   rE   -z*ChromiumEventLogger initialized with id %s)	threadinglocalrD  r  rF   pt2_compile_id_prefixuuiduuid4id_r   r   rb  )rj  rF   s     r`   rk  zChromiumEventLogger.__init__  s    ?$$ ' 	) 6GGGGDHH4:<<((DH 	=txHHHHHr_   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)rE  r   )rj  r   r  s      r`   r  z&ChromiumEventLogger.try_add_event_data  s?     T^^----FJ11&11111r_   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)rE  r   rA  rJ  r   )rj  r   r  rI  s       r`   r   z"ChromiumEventLogger.add_event_data  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz":%%f-----r_   r   r   rf   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
        rU  rV  rW  r   N)rE  r   rA  rJ  rj  r   r   r   rI  s        r`   r   zChromiumEventLogger.increment  s     T^^----Kj)) K K4>>3C3C K K K   ((**
Z''%'Jz"j,,,*+Jz"3':s###u,#####r_   c                Z   ||                                  vr5t          dt          |           d|                                   d          |                                 }||vri ||<   |||         vrt	                      ||         |<   ||         |                             |           dS )zR
        Add a value to a set within a event_name's metadata if it exists
        rU  rV  rW  N)rE  r   rA  rJ  rm  rw  rY  s        r`   r   zChromiumEventLogger.add_to_set  s     T^^----Kj)) K K4>>3C3C K K K  
 ((**
Z''%'Jz"j,,,*-%%Jz"3':s#''.....r_   FNr   r   r   r   r  r  c                d   |p"t           j        j                                        }t	          |          |d<   |                     ||d|           |                                                     |            | j        |fi | |r)| 	                                                    |           dS dS )a  
        Logs the start of a single event.
        :param str event_name Name of event to appear in trace
        :param time_ns Timestamp in nanoseconds
        :param metadata: Any extra metadata associated with this event
        :param log_pt2_compile_event: If True, log to pt2_compile_events
        :param compile_id: Explicit compile_id (rather than using the current context)
        r  BN)
r%  r)  r*  r+  r   _log_timed_eventrE  r   r   r   )rj  r   r   r   r   r  s         r`   r$  z#ChromiumEventLogger.log_event_start  s       T5=#?#R#R#T#T
!$Z		
 	
 	
 	
+++J33(333  	?))++22:>>>>>	? 	?r_   c                    |                                  }|                                 }|                                 |                                 |                                 }|                                 d S ru   )rE  r   rv   rJ  )rj  r9  substackrI  s       r`   ri  zChromiumEventLogger.reset9  sj       0022((**
r_   r  c                f   |p"t           j        j                                        }t	          |          |d<   |                                 }|v r|         }|= ni }|                    |           |                     |d|          }	d
fd}
|                                 }|vrt          
                    d           d	S  |
|           |rJ|                                 } |
|           t          |	|| j        |           |                                 |                                 d	S )a&  
        Logs the end of a single event. This function should only be
        called after log_event_start with the same event_name.
        :param event_name: Name of event to appear in trace
        :param time_ns: Timestamp in nanoseconds
        :param metadata: Any extra metadata associated with this event
        :param start_time_ns: The start time timestamp in nanoseconds
        :param log_pt_compile_event: If True, log to pt2_compile_events
        :param compile_id: Explicit compile_id (rather than using the current context)
        r  Er9  rB  rg   rh   c                    | d         k    r<t                               d           |                                  | d         k    :d S d S )Nz>ChromiumEventLogger: Detected overlapping events, fixing stack)r   r  pop)r9  r   s    r`   	pop_stackz4ChromiumEventLogger.log_event_end.<locals>.pop_stackj  sY    b	)) T   		 b	))))))r_   z7ChromiumEventLogger: Start event not in stack, ignoringN)r9  rB  rg   rh   )r%  r)  r*  r+  r   rJ  r   r]  rE  r   r  r   r'   rR  rd  )rj  r   r   r   r  r   r  all_event_datar3  eventre  event_stackr   s    `           r`   r2  z!ChromiumEventLogger.log_event_endC  sx   &  T5=#?#R#R#T#T
!$Z ,,..''+J7Nz**Nh'''%%	
 
	 	 	 	 	 	 nn&& [(( KKQRRRF	+  	'#'#@#@#B#B I*+++'+TX}   !$$&&& 	r_   r   Optional[dict[str, Any]]c                    ||dz  ||ddddt           j                            dfddd           t                     S )	za
        Logs a timed event in chromium format. See log_event_start, log_event_end, etc.
        r   r@  r   )rl   tsr	  phcattidpidchromium_eventc                      S ru   rH   rg  s   r`   rV  z6ChromiumEventLogger._log_timed_event.<locals>.<lambda>      u r_   F
payload_fnsuppress_contextexpect_trace_id)r%  r  r1  r)   )rj  r   r   r   r   rg  s        @r`   r]  z$ChromiumEventLogger._log_timed_event  sr     D.!	
 	
 	''$}}}"!	 	( 	
 	
 	
 	'u---r_   c           	     <   |i }t          t          j        j                                                  }||d<   ||dz  |ddddddt          j                            d	fd
dd           |r+t          |                                 | j	        |           dS dS )aZ  
        Log an instant event with no associated duration.
        :param str event_name: Name of event to appear in trace
        :param int time_ns Timestamp in nanoseconds
        :param Optional[Dict[str, Any]] metadata: Any extra metadata associated with this event
        :param str cname optional color for the arrow in the trace
        Nr  r   ir@  r   p)rl   rk  r	  rl  rm  rn  ro  srp  c                      S ru   rH   rr  s   r`   rV  z7ChromiumEventLogger.log_instant_event.<locals>.<lambda>  rs  r_   FTrt  )
r   r%  r)  r*  r+  r  r1  r'   r   rR  )rj  r   r   r   r   r  rg  s         @r`   r   z%ChromiumEventLogger.log_instant_event  s     H5HHJJKK
!+D.!

 

 	''$}}}" 	 	( 	
 	
 	
 ! 	't4466'    	 	r_   )rg   rB  rg   r  )rg   rP   r   )r   r   r  r   rg   rh   )r   r   r   r   r   rf   rg   rh   )r   r   r   r   r   r   rg   rh   FN)r   r   r   rf   r   rP   r   r   r  r  rg   rh   ru   )r   r   r   rf   r   rP   r  rf   r   r   r  r  rg   rh   )
r   r   r   rf   r   r   r   ri  rg   rP   r  )
r   r   r   rf   r   ri  r   r   rg   rh   )rY   rZ   r[   r   rE  r   r   rJ  rk  r  r   r   r   r$  ri  r2  r]  r   rH   r_   r`   rA  rA    so        	" 	" 	" 	"+ + + +	1 	1 	1 	1# # # #
I I I I2 2 2 2. . . .(- - - -$/ / / /6 ',*.? ? ? ? ?<   " +/G G G G G\ .2    B .2&+( ( ( ( ( ( (r_   rA  zOptional[ChromiumEventLogger]CHROMIUM_EVENT_LOGc                 :    t           t                      a t           S ru   )r  rA  rH   r_   r`   r   r     s    !022r_   c                     t           d uS ru   )r  rH   r_   r`   r  r    s    T))r_   r   reset_event_log_on_exitc           	   #    K   t                      }t          j                    }|                    | |i |           	 dV  |                    | t          j                    i ||           |r|                                 dS dS # |                    | t          j                    i ||           |r|                                 w w xY w)z
    Context manager that creates a chromium start and end event. Chromium event
    logging is integrated with dynamo_timed, so you probably want to use that
    instead. Use this context manager only if you want to avoid dynamo_timed.
    N)r   r   r   r$  r2  ri  )r   r  r   chromium_event_logchromium_start_times        r`   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 calledrP   scoper   rl   r	  r   rg   rh   c                X    t           t           xj        dz  c_        | j        | j        = d S r   )CleanupManagerro   r  rl   rj  r	  s     r`   __call__zCleanupHook.__call__  s.    %  A%  Jty!!!r_   rJ  c                b    || vsJ t           xj        dz  c_        || |<   t          | |          S r   )r  ro   r  )r  rl   rJ  s      r`   r  zCleanupHook.create  s@    5    !d5$'''r_   Nr	  r   rg   rh   )r  rP   rl   r   rJ  r   rg   r  )rY   rZ   r[   r   r   r  r  r  rH   r_   r`   r  r    se         66III" " " " ( ( ( \( ( (r_   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  rf   rg   rh   c                ~    | j         |         D ]} |             t                                          |           d S ru   )rL  r  r  )rj  r  r   r  s      r`   r  zCleanupManager._remove_id  sD    K$ 	 	DDFFFF3r_   r  )rY   rZ   r[   ro   r   r  r	  r
  s   @r`   r  r    sP         E&&&&                   r_   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     r`   clone_tensorr  &  sF    			  11Ay  QV'Hr_   dtyper  Optional[torch.dtype]c          	     H   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+          d	 t-          |                                 |                                           D                       }| j        rt          j        |d
z   f|           }n&t          j        |d
z   p| j        | j                  }|                                 |                                z
  d
z  |                                 z  }|                    |                                 |                                 |           	 |                     | !                                           | j"        r|#                    | j$                   | j"        r"| j%        tM          | j%                  |_%        n'# tN          $ r  ||           cY cddd           S w xY wtQ          | d          r| j)        *                                |_)        |cddd           S # 1 swxY w Y   dS )zcopy while preserving stridesrU  r  rg   c                    t          j        |           }| j        r|                    | j                   | j        r"| j        t          | j                  |_        t          | d          r| j        	                                |_        |S )Nr  _dynamo_dynamic_indices)
r%  r  r  r  r  r  clone_inputr-  r  copy)rU  r  r  s     r`   torch_clonez clone_input.<locals>.torch_clone7  s    KNN9 	.Q_---9 	6+ u555AF1/00 	I()(A(F(F(H(HA%r_   xlaN)is_coalesced)layoutc              3  ,   K   | ]\  }}|d z
  |z  V  dS )r2   NrH   )r   shapestrides      r`   r   zclone_input.<locals>.<genexpr>]  s@       
 
%2UFUQY& 
 
 
 
 
 
r_       )r  devicer  r  rU  r  rg   r  )+rJ   r%  no_gradr  r  r  
sparse_coosparse_coo_tensor_indicesrc   r  r  r%   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorrL  r{   zipsizer  is_quantizedempty_quantizedemptyr  data_ptrelement_sizeas_strided_copy_r  r  r  r  r  r  r   r-  r  r  )rU  r  r  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      r`   r  r  .  s=   
 qzz       
 6 68=E!!;q>>6 6 6 6 6 6 6 6 8u'''*AJJLL))AIIKK((^^--	  6 6 6 6 6 6 6 6 "!$$ 	xE,e.>???%&^^%5%5" !%&^^%5%5" !1.//M**AHHJJ''x  )6 6 6 6 6 6 6 68  
 
69!&&((AHHJJ6O6O
 
 
 
 
 > 	*K",<+>BBFF[b (8  F ZZ\\FOO---3^^ 	16688QXXZZ1BCCC
	"LL###y 7%%ao666y ?QV/)!&>>> 	" 	" 	" ;q>>!!g6 6 6 6 6 6 6 6^	"
 1/00 	N-.-F-K-K-M-MF*m6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6sK   NA7NCN3DNA1L43N4M	NM2NNNexample_inputs"dict[str, Union[T, tuple[T, ...]]]dict[str, list[T]]c                    d S ru   rH   r  s    r`   clone_inputsr  z  s	     r_   Sequence[T]list[T]c                    d S ru   rH   r  s    r`   r  r    s    :=#r_   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 ru   )r  dictr   r  r  r  r%  r=  r  r_  rangert  )r  resr   r   ry  s        r`   r  r    s    Nt##>""))++ 	. 	.JC%'' .'..C!%66CCUCC6&u--C

~

C3s88__ ) )c!fel++ 	) Q((CFJr_   c                    	 |                                   d S # t          $ r=}ddlm} t	          j        ddt          |                     } |d|           |d }~ww xY w)Nr2   )	SkipFramez\(.*r  z(torch.compile cannot be run in context: )r  r   r  r  resubrA  )rJ  rf  r  functorch_subclass_names       r`   skip_frame_if_in_functorch_moder    s    	   """""" #%&"d3ii"@"@iP7NPP
 
	s    
A8AAGenerator[None, None, None]c               #  l  K   t           j        j        } t           j        j        j        } |            5   |             5  t          j        t           j                                                  }t          |           t           j
                                        r0t          j        t           j
                                                  }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   	 d V  t           j        j                                        5  t           j                            |           t           j
                                        rt           j
                            |           d d d            d S # 1 swxY w Y   d S # t           j        j                                        5  t           j                            |           t           j
                                        rt           j
                            |           d d d            w # 1 swxY w Y   w xY wru   )r%  _C_DisableFuncTorchr&  _python_dispatch_disable_current_modesr  randomget_rng_stater  r.  is_availableset_rng_state)disable_functorchdisable_current_modes	rng_statecuda_rng_states       r`   preserve_rng_stater    s     2!K8O			 	  E E"3"3"5"5 E EK : : < <==	'	222:""$$ 	E"[)A)A)C)CDDN	E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E
9[)@@BB 	9 	9L&&y111z&&(( 9
((888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9U[)@@BB 	9 	9L&&y111z&&(( 9
((888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9sy   C0BCC0C	C0 C	!C00C47C4<F #AFFF$H3>AH'H3'H++H3.H+/H3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 ru   )	r  r%  jit_traceTopLevelTracedModule_scriptRecursiveScriptModuleScriptFunctionScriptModule)r  s    r`   is_jit_modelr    sD     I1I3I$I"		
  r_   modelrs  c                \   t          |           r| S 	 t          j                            | |          S # t          $ rl 	 t          j                            |           cY S # t          $ r: |rt                              d           nt                              d           Y nw xY wY nw xY wd S )Nz	jit errorz0Both torch.jit.trace and torch.jit.script failed)	r  r%  r  tracer  scriptr   r  error)r  r  rs  s      r`   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ru   )r  getfiler  OSErrorr  s    r`   r  r    s>    s###w   tts    ++c                :    t          t          |                     S )zLTest if an object is a namedtuple or a torch.return_types.* quasi-namedtuple)is_namedtuple_clsr  r  s    r`   is_namedtupler     s    T#YY'''r_   rm   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-namedtuplerZ   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  ,   K   | ]}|t           u|V  d S ru   )r   )r   ts     r`   r   z$is_namedtuple_cls.<locals>.<genexpr>  s,      KKA!7:J:Ja:J:J:J:JKKr_   c              3  R   K   | ]!}t          |          oj        |j        u V  "d S ru   )r  __new__)r   r  rm   s     r`   r   z$is_namedtuple_cls.<locals>.<genexpr>  sR       ! ! 	 *!,, 5K194! ! ! ! ! !r_   F)
issubclassr  getattrr  callable	__bases__anyr  )rm   modulebasess   `  r`   r  r    s   c5!! 	 S,55FLLLt'#y$77??  HWd++E E   KKKKKKKUH$$4 	 S ! ! ! ! #! ! !   	   4   5s   *B= A,B= B= =
C
	C
r  tuple[str, ...]c                H   | t           u rdS t          | t                    sJ t          | d          r| j        S t
          j         G d d                      }| j        dk    sJ  | t          |t          | j
                                      }i }t          |          D ]D}|d         dk    r6t          t          ||          |          rt          ||          j        ||<   Et          |          | j
        k    sJ t          t!          ||j                            S )	zIGet the fields of a namedtuple or a torch.return_types.* quasi-namedtuple)startstopstepr  c                      e Zd ZU ded<   dS )!namedtuple_fields.<locals>.Markerrf   indexNrY   rZ   r[   r   rH   r_   r`   Markerr  	  s         




r_   r  r  r   r  )r   )slicer	  r  r-  r  r#  	dataclassrZ   r   r  n_fieldsdirr  r
  r  rt  r  ry   )rm   r  r  fieldsrl   s        r`   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0r_   gmtorch.fx.GraphModule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 rH   )_versionr%  r  )r   params     r`   rY  z%checkpoint_params.<locals>.<listcomp>&	  s;     
 
 
 ENEK$6$67
 
 
r_   rg   rh   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 ru   )r%  r  r  r  r.  r  r%  r  )r&  r-  original_valuer  r  saved_states      r`   restorez"checkpoint_params.<locals>.restore+	  s    ]__ 	0 	0L&&y111z&&(( 9
((8882= 0 0.w>W,,KK///0		0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   BB))B-0B-r   )r%  r  r  r  r  r.  r  r   r   
parametersbuffers)r   r*  r  r  r)  s     @@@r`   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.  r  synchronizer  gccollectmanual_seedr   perf_counterr  )r  r  r/  r2  t0r  r  t1s           r`   timedr9  7	  s     z   j,KMMMJLLL	d				B5\\  '				B27?r_   c           	         t          d t          j        ||                     d                    D                       S )Nc              3  $   K   | ]}|j         V  d S ru   )is_cudar   rU  s     r`   r   z check_is_cuda.<locals>.<genexpr>K	  s$      WWQqyWWWWWWr_   T)allr   r   r+  )r   r  s     r`   check_is_cudar?  J	  s8    WW)/."--PTBUBU"V"VWWWWWWr_   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 )
Nr2   c                    g | ]}d | S rl  rH   r   ry  s     r`   rY  z rot_n_helper.<locals>.<listcomp>Q	  s    &&&GGG&&&r_   rc  zlambda r  z: ()rot__helper)r  reversedevalr   rY   )r  varsrotatedr0  s       r`   rot_n_helperrK  N	  s    q5555&&U1XX&&&DtBCCy49,--G	???388G+<+<???	@	@B####BKIr_   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 ru   )r  r  	frozensetr>  r   is_safe_constantr  enumEnumr  r%  Sizer  _GenericAliasr  GenericAliasrL  r  rl  s    r`   rO  rO  {	  s    a%#$$ -3'++,,,	IJ 	
	 	  
	'
 
r_   set[int]c                 
    ddhS Nr   r2   rH   rH   r_   r`   common_constantsrX  	  s    
 	
		 r_   -TypeGuard[Union[torch.SymBool, torch.SymInt]]c                    t          | t          j        t          j        f          o.t          | j        t          j        j        j        j                   S ru   )	r  r%  SymBoolSymIntnodenestedr  
nested_intNestedIntNoder  s    r`   is_torch_symra  	  sH    eemU\:;; J
EL*5CE E A r_   sourcec                   ddl m} t          j                    j         o|                                                                 p|                                                                ot          j	         p|                                
                                ot          j	         p]|                                                                ot          j	         p* ||          pt          d           o| t                      v S )Nr2   )is_from_defaultsz5pytorch/dynamo:enable_unspecialize_zero_one_plain_int)rb  rd  r$   ry   #force_unspec_int_unbacked_size_likeguard_sourceis_localis_specialized_nn_modulerF   allow_unspec_int_on_nn_module"is_unspecialized_builtin_nn_moduleis_unspecialized_nn_moduler&   rX  )r   rb  rd  s      r`   is_int_specialization_caserl  	  s   ((((((!##GG !!**,,, 	

 !!::<< 988	
 !!DDFF 988	
 !!<<>> 988	
  F##!	
(   WXXX ,)+++/r_   rV  c                   ddl m}m}m} t	          | |          r|                                 so|                                 }|                                 }t          |          p3t          j
         o&t          |          t          u ot          ||           }|s| S t	          | |          r'|                    |                                           S | S )Nr2   )ConstantVariableLazyVariableTrackerSymNodeVariable)	variablesrn  ro  rp  r  is_realizedoriginal_sourcer(  ra  rF   specialize_intr  rf   rl  r  evaluate_expr)rV  rn  ro  rp  rb  r   is_symnode_vts          r`   specialize_symnoderw  	  s    QQQQQQQQQQ #*++ COO4E4E  $$&&""$$$U++ 
%% >Us">.uf=== 	  	J#'' <&&s'8'8':':;;;Jr_   c                x    ddl m} t          |           } t          | |          r|                                 S | S )Nr2   rn  )rq  rn  rw  r  as_python_constant)rV  rn  s     r`   guard_if_dynr{  	  sK    ++++++
S
!
!C#'(( (%%'''Jr_   Mapping[Any, Any]c                ~    t          d t          j        | |                                          D                       S )Nc              3  >   K   | ]}|                                 V  d S ru   )is_python_constantr=  s     r`   r   z&check_constant_args.<locals>.<genexpr>	  s.      VV!q##%%VVVVVVr_   )r>  r   r   rL  r  s     r`   check_constant_argsr  	  s3    VVytV]]__/U/UVVVVVVr_   c                    ddl m} ddlm} d}t	          j        | |                                          D ]+}t          ||          r|dz  }t          ||          s dS ,|dk    S )Nr2   ry  UnspecializedPythonVariabler   F)variables.constantrn  variables.tensorr  r   r   rL  r  )r	  r  rn  r  unspec_countrU  s         r`   check_unspec_python_argsr  	  s    444444======L_T6==??33  a455 	ALLA/00 	55	!r_   c                    ddl m} t          j        | |                                          D ])}|                                st          ||          s dS *dS )Nr2   r  FT)r  r  r   r   rL  r  r  )r	  r  r  rU  s       r`   check_unspec_or_constant_argsr  	  sl    
 >=====_T6==??33  $$&& 	*Q8S*T*T 	554r_   c                    ddl m t          fdt          j        | |                                          D                       S )Nr2   )NumpyNdarrayVariablec              3  8   K   | ]}t          |          V  d S ru   )r  )r   rU  r  s     r`   r   z+check_numpy_ndarray_args.<locals>.<genexpr>
  sB         	1*++     r_   )r  r  r  r   r   rL  )r	  r  r  s     @r`   check_numpy_ndarray_argsr  	  sa    666666    v}}77     r_   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 rH   r  r   methods     r`   	<setcomp>r  
  s6       
  r_   c                0    h | ]}t          |          |S rH   r  r  s     r`   r  r  
  %    NNN&Xf=M=MNvNNNr_   c                0    h | ]}t          |          |S rH   r  r  s     r`   r  r  
  s5       8H8H
  r_   c                0    h | ]}t          |          |S rH   r  r  s     r`   r  r  
  s%    RRRF&AQAQRRRRr_   c                0    h | ]}t          |          |S rH   r  r  s     r`   r  r  
  s%    PPP6x?O?OPPPPr_   c                0    h | ]}t          |          |S rH   r  r  s     r`   r  r  
  r  r_   KVr  
dict[K, V]KeysView[K]c                d    t          | t                    sJ t                              |           S ru   )r  r  r`  )r  s    r`   builtin_dict_keysr  %
  s(    a99Q<<r_   !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 rH   )r   r  r   rX  r  s     r`   rY  z'get_items_from_dict.<locals>.<listcomp>1
  s+    TTTK+C334TTTr_   c                J    g | ]}|t                               |          f S rH   )r  r  r  s     r`   rY  z'get_items_from_dict.<locals>.<listcomp>3
  s.    FFF!D$$S!,,-FFFr_   )r  r  r  r   r   r`  r  s   `r`   get_items_from_dictr  +
  s    c4     cD+&'' Gyy{{	C	%	% GTTTTk>Ns>S>STTTTFFFFtyy~~FFFFr_   c                l    t          |           }t          j        j                            |           |S ru   )
object_newr%  r>  Modulerk  )rm   r  s     r`   nn_module_newr  6
  s*    
S//C	HOS!!!Jr_   itIterable[T]c                B    t          j        t          j        | d          S r   )r  reduceoperatormul)r  s    r`   productr  <
  s    HL"a000r_   r  c                L    |                                  \  }\  }}|||z            S ru   )
__reduce__)r  r  r  r  r  s        r`   tuple_iterator_getitemr  @
  s'    }}Avuuu}r_   c                d    t          j                            t          j                  |           S ru   )r%  _dynamodisabler#  r  rw   s    r`   dataclass_fieldsr  E
  s#    =  !344S999r_   
range_itertuple[int, int, int]c                    |                                  \  }\  }}|j        |pd|j        z  z   }|j        }|j        }|||fS r   )r  r  r  r  )r  r  	range_obj	maybe_idxr  r  r  s          r`   normalize_range_iterr  L
  sP    !+!6!6!8!8A|	Y Oy~A??E>D>D4r_   r  c                ,    |                      |          S ru   )as_subclass)r  rm   s     r`   to_subclassr  Z
  s    ==r_   dict[Any, Any]c                    t           }t          | t                    rt          }t          t	          j        |                    |           ||dz                       S r   )r  r  r   nextr   islicer`  )r  r  
dict_classs      r`   dict_keys_getitemr  a
  sK    J![!! ! 
	 !3!3QA>>???r_   r{  set[T]c                ,    t          |           |         S ru   r_  )r{  r  s     r`   set_getitemr  i
  s    771:r_   rN  c                L    | j         j        }| j        }|rdnd}| d| d| }|S )NLGz["z"].)r  rY   rl   )r   rN  rl   rJ  r  
local_names         r`   	enum_reprr  n
  sD     ?#D
*C!CCcE++T++c++Jr_   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 )Nr  unbacked_bindings)
metar$   ry   	fake_mode	shape_envr%  r   experimentalsymbolic_shapescompute_unbacked_bindings)r]  r  r  r  symbol_to_paths        r`   set_example_valuer  x
  s     "/DIo"$$.I   #I 8 0JJ}
 
	
8 *8	%&&&8 8r_   vtr@   c                    |                                  j        j                            d          }t	          |          s/ddlm} ddlm}  |dt          |          dg |j
                   |S )Nr  r2   graph_break_hintsrY  z:Cannot check Tensor object identity without its fake valuez/TensorVariable is missing a fake example_value.r]  r^  r_  r`  )rT  r]  r  ry   rJ   r  r  r  rZ  r   
DYNAMO_BUG)r  fake_tensorr  rZ  s       r`   _get_fake_tensorr  
  s    ++--$)--o>>K; 	
''''''))))))P$$I1%01		
 	
 	
 	
 r_   r  seq_lenc                |    |                      |          \  }}}t          d||z
  ||dk    rdndz
  z   |z            S )Nr   r2   rc  )indicesmax)r{  r  r  r  r  s        r`   slice_lengthr  
  sI    		'**E4q4%<4q11b#ABtKLLLr_   txr?   rl   c           	     `    ddl m} ddlm}  |t          |  |d| d          g           d S )Nr   )raise_observed_exceptionry  zwrong number of arguments for z() callrH  )torch._dynamo.excr  torch._dynamo.variablesrn  r  )r  rl   r  rn  s       r`   raise_args_mismatchr  
  sm    ::::::888888
MMMMNNO     r_   r   searchr>   check_tensor_identityc                Z   ddl m}m}m}                                 r0t          fd| D                       }|                    |          S d}|r!t          |          rd}t                    d }	| D ]}
|r9t          |
|          r(t          |
          u r|                    d          c S = |t          j
                                      ||
gi           }|	|}	n |t          j                                      |||	gi           }	|	|                    d          }	|	S )Nr2   )BuiltinVariablern  TensorVariablec              3     K   | ]B}|                                 o)|                                                                k    V  Cd S ru   )r  rz  )r   rU  r  s     r`   r   z iter_contains.<locals>.<genexpr>
  sk       
 
    "" F$$&&&*C*C*E*EE
 
 
 
 
 
r_   FT)rq  r  rn  r  r  r  r  r  r  r  eqcall_functionor_)r   r  r  r  r  rn  r  found_constmust_check_tensor_idfoundrU  checks    `          r`   iter_containsr  
  s    MLLLLLLLLL  "" 4 
 
 
 
 
 
 
 
 

  &&{333  *FN!C!C *#!&))'+E   	!^,, 9-a0000+22488888#OHK00>>rAv;PRSSE}'55CC  } ''..Lr_   rX  ?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   )rX  s    r`   	key_is_idr  
  s"     a%,9JKLLLr_   	list[Any]c                >    d |                                  D             S )Nc                N    g | ]"}t          |          rt          |          n|#S rH   )r  r  )r   rX  s     r`   rY  zkey_to_id.<locals>.<listcomp>
  s-    ???QYq\\(BqEEEq???r_   )r`  r  s    r`   	key_to_idr  
  s    ??%**,,????r_   c                  ddl m} t          | t          t          f          rrd                    fd| D                       }t          | t                    rd| dS t          | t                    sJ t          |           dk    rd| dS d| d	S t          | t          j                  r%t          | 
          
                    dd          S  ||           r| j        S t          | t                    rdd} ||           S | S )Nr2   )is_builtin_callabler  c              3  :   K   | ]}t          |           V  dS rN  N
const_reprr   r{  rN  s     r`   r   zconst_repr.<locals>.<genexpr>
  s0      DDQj%888DDDDDDr_   [](z,)rD  r  'r  r  r   rg   r   c                R    | j         }|j        }|dk    r|j        S |dz   |j        z   S )Nbuiltins.)r  rZ   r[   )r  klassr  s      r`   fullnamezconst_repr.<locals>.fullname
  s7    KE%F##))C<%"444r_   )r  r   rg   r   )trace_rulesr
  r  r_  r  r   rt  rP  rQ  r  replacerY   r  )rU  rN  r
  
elems_reprr  s    `   r`   r  r  
  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r_   
const_keysc               V    d                     fd| D                       }d|z   dz   S )Nr  c              3  :   K   | ]}t          |           V  dS r  r  r  s     r`   r   z!dict_keys_repr.<locals>.<genexpr>  s0      GGq
1E222GGGGGGr_   r  r  r   )r  rN  keys_strs    ` r`   dict_keys_reprr"     s8    xxGGGGJGGGGGH>Cr_   
__dict_key)UnsupportedFakeTensorExceptionrootc                D    | dt          |           d| j        j         S )Nr  _c)r  outputr  )r  r%  r  s      r`   get_safe_global_namer)    s+     66RWW66	 4666r_   r  
containersContainer[T]c                    |D ]	}| |v r dS 
dS r  rH   )r  r*  	containers      r`   is_inr.    s-      	944 5r_   )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   ro   AssertionError)r0  r/  r*  ry  	candidates        r`   get_unique_name_wrtr5    s      5#=*#=#=#= _  OOOO	Y,,,, 		 
'
''r_   Callable[[], Any]c                    	  |             S # t           $ rX}ddlm} d|j         d}t                              |            |dt          |j                  |g |           Y d }~d S d }~ww xY w)Nr2   rY  zEncountered exception (z!) during fake tensor propagation.z!Fake tensor propagation exceptionr\  )r$  r  rZ  reasonr   r  r   )r0  rf  rZ  msgs       r`   wrap_fake_exceptionr:  .  s    
rtt) 
 
 
))))))SSSSC7MM	
 	
 	
 	
 	
 	
 	
 	
 	
 	

s   	 
A.AA))A.r  ,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 ru   )r  deepcopyr  s   r`   rV  z)deepcopy_to_fake_tensor.<locals>.<lambda>C  s    4=+=+= r_   )r%  rA  r  FakeCopyModer:  )r  r  s   ` r`   deepcopy_to_fake_tensorr@  ?  s     
		&	3	3I	>	> ? ?"#=#=#=#=>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   AA	A	r  r  c                x    t          j        t          j        t          j        | |z
                                S )z+
    Calculate root mean squared error
    )r%  sqrtmeansquare)r  r  s     r`   rmserE  F  s,     :ejcCi!8!899:::r_   g-C6?Tfp64_refcos_similaritytolfloat	equal_nanexact_dtyperelax_numpy_equalityignore_non_fp	log_errorCallable[..., None](use_larger_multiplier_for_smaller_tensorforce_max_multiplierc                b   	
  t           t          t          t          j        t
          j        j        t
          j        f          rt          t          t          t          j        f          s*J dt                      dt                                 t                     t                    k    r 	d           dS t                     t                    k    o1t          	
f	dt                     D                       S t                     j        dk    r*t           j        j        j        	
          S t           t                     rt          t                     sJ t#                                                     t#                                                    k    sNJ d	t#                                                      d
t#                                                                 t'                                                     D ]>}t           |         |         |         	
          s 	d|            dS ?dS t           t"                    rct          t"                    sJ t#                     t#                    k    s*J dt#                      d
t#                                 dS t           t
          j        t*          f          rt           t
          j        j                  rJ t          t
          j        j                  rJ d+dfd fD             \    j        r1j        sJ                                                                   t          t
          j                  s*J dt                      dt                                 r j        j        k    r 	d j        j                   dS  j        t
          j        k    rhrdS t          j                             t
          j                                      t
          j                            }|s 	d           |S r                                                     t
          j                                                                        t
          j                   t          j         d          rdS t
          j        j!        "                     dd          }|dk     rQtF          $                    d|%                                &                                '                                           t7          |dk              S s                     j                   t          j                   rdS j        t
          j(        k    rd})                                *                                s)                                *                                sZ )                                *                                r4t          j                            j                  ||          rdS tW                     '                                }tY          j)        |          rtF          $                    d           tW                    '                                }d, 
fd!} |            }|||z  d"z  z   k    }|sGrEtY          j)        |          r1tY          j)        |          rt
          j-        j.        j/        j0        sd}|s) 	d#||1                                j        |
           |S rdS  	d$           dS t           td          tf          t          d          t6          t
          j4        f          r)rdS  k    }|s 	d%t                                 |S tk                     stm                     rir2tk                    s#tm                    s '                                 t                     t                    u o k    }|s 	d&            |S to                     r\t                     t                    u o=t          t          j8                   t          j8                  	
'          S t                     j        d(v r\t                     t                    u sJ t          	 
fd) j9                                        D                       S tu          d*t                     j                   )-z-Check correctness to see if ref and res matchNztype mismatch r   zLength mismatchFc              3  V   	K   | ]#\  }}}t          |||
	           V  $dS )rN  rP  rQ  N)same)r   aibi	fp64_refirG  rJ  rK  rQ  rM  rN  rL  rH  rP  s       r`   r   zsame.<locals>.<genexpr>g  sr       ,
 ,
 "B	 $#9a%9  ,
 ,
 ,
 ,
 ,
 ,
r_   QuestionAnsweringModelOutputrT  zkeys mismatch z == )	rG  rH  rJ  rK  rL  rM  rN  rP  rQ  zAccuracy failed for key name %sTzelements mismatch r  r   rg   r  c                b    t          | t          j                  r| nt          j        |           S ru   )r  r%  r=  tensorr  s    r`   	to_tensorzsame.<locals>.to_tensor  s%    "1el33H11aHr_   c              3  .   K   | ]} |          V  d S ru   rH   )r   rJ  r]  s     r`   r   zsame.<locals>.<genexpr>  s+      MMiinnMMMMMMr_   zdtype mismatch %s, %sr  )atolrtolrJ  z+Accuracy failed: uint8 tensor did not matchr   gư>)dimepsgGz?zSimilarity score=%sg{Gz?z=Found nan in reference. Consider running in higher precision.rI  c                 j   rdS j         t          j        t          j        fv rdnd} r                                dk    rd} nmr                                dk    rd} nP                                dk     s6j        dk    r%j        d	         j        d
         cxk    rdk    sn dk    rd} | S )N      $@g      @g       @
   i  g       @r      rc  r2   g{Gz?)r  r%  r  bfloat16numelndimr  )
multiplierrQ  rF  r  r  rH  rP  s    r`   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(??
 &)
%%r_   rd  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rG  rH  rJ  rK  rL  rM  rN  rP  )MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsLongformerMaskedLMOutput	InstancesSquashedNormalBoxesNormalTanhTransformFooVariablec              3     K   | ]H}t          t          |          t          
|          t          |          	           V  IdS )rm  N)rU  r
  )r   r   rG  rJ  rK  rF  rM  rN  r  rL  r  rH  rP  s     r`   r   zsame.<locals>.<genexpr>i  s       
 
  S!!S!!#&&-#'%9+#9a  
 
 
 
 
 
r_   zunsupported type: )r  r   rg   r  )rg   rI  );r  r_  r  r   r8  r%  r>  ParameterListrR  r  rt  r>  r  rY   rU  lossr  rm  r`  r  r=  rI  rA  rI   	is_sparseto_denser  r   allclosetor  flattenr  
functionalcosine_similarityr   r  detachcpur  r  isnanr  rE  mathr  rF   cppinject_relu_bug_TESTING_ONLYr  r   rf   r  r  r  r;  	as_tensor__dict__r   )r  r  rF  rG  rH  rJ  rK  rL  rM  rN  rP  rQ  rX  rscore	loose_tol	ref_error	res_errorrl  rk  passes_testr]  s   ````````````         @r`   rU  rU  M  s
    dE;,eh.DejQ  \F #e[->?@@ 	
 	
4T#YY44c44	
 	
@ s88s3xxI'(((53xx3s88# 
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 &)c8%<%<,
 ,
 ,
 )
 )
 	
" 
c	=	=	= HHM 5]!5
 
 
 	
 
C		 pF#t$$$$$388::#chhjj//111CS__CC#chhjj//CC 211 

## 	 	AFFQK#1' +)="/'=e)=    	;Q???uu#$ t	C		 WF#s#####3xx3s88###%R#c((%R%RC%R%R###t	C%,.	/	/ SFc5#4#?@@@@@c5#4#?@@@@@	I 	I 	I 	I NMMMc88LMMMS(= 	!=  =,,..C,,..C#u|,,VV.VtCyy.V.V4PS99.V.VVV, 	yCI%%	139ciHHHuyEJ&&   4NFFF--FFF--'    MIKLLL 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Er_   c                n    | j                             d          d         }d| j         d| d| j         dS )N/rc  r  z' (r   rD  )rs  splitr  r  )r  short_filenames     r`   format_func_infor  }  sC    %++C004NGt|GGGG1DGGGGr_   c               #    K   t           j        } t          j        t           _        t           j        }t          j        t           _        	 d V  | t           _        |t           _        d S # | t           _        |t           _        w xY wru   )rF   recompile_limitr  rg  accumulated_recompile_limit)priorprior_acc_limits     r`   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     r`   _get_debug_dirr    sm     	




!
!
*
*+A
B
B	C 	 bikk

		  7<<(+++r_   c                 8    t           j        } t          |           S ru   )rF   r  r  )
debug_roots    r`   r  r    s    &J*%%%r_   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 )
Nr  r   rY  r2   r  z Missing FakeTensor example valuez,`FakeTensor` example value was required for z but not available.r  )r  rJ   r  rZ  r  r  r   r  )r]  r  rZ  r  s       r`   extract_fake_example_valuer    s    $)##	/0J(K(K#y))	 666666''''''6II`t```1%01		
 	
 	
 	
 	
 	
 tr_   rf  c                6    t          |           |j        u sJ | S ru   )rK   r  )rf  r  s     r`   ensure_graph_faker    s"    q!!R\1111Hr_   r  allow_non_graph_fakec                ^     d fd}t           j        j                            ||          S )Nr  r  rg   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  r  get_attr)r  r  get_fake_valuetargetr(  
nn_modulesr  r%  r   GraphModuler=  r  )r  r   r   r  r  s      r`   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"---
r_   )r  r  rg   r   )r%  r   r]  map_arg)r  r  r  r  s   ` ` r`   get_fake_values_from_nodesr    sC          " 8=  ...r_   c           	     Z    ddl m} ddlm}m}m}m}m}  j        }	d j	        v r't           j	        d                   r j	        d         S t           j         j        f|          \  t          j        j        j        st          j        j        j        r,t          t'           i           |          }
d |
D             }ng }
i }dj        }|J |	dk    rgt+                    dk    rTt-          d         t          j        j                  r/t3          d         |          ft5          dd                   z   |	d	k    r\j        j         j                 t=                    r&t?          d
          r                                t3          |           j!        dv stE          d D                       rt5          d D                       	 |5  tG                      5  tI           fd          }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   n# |$ r  tJ          $ r}|}|j&        |j&        }t-          |t          j'        j(        j)                  rp|j*        t          j+        j,        j-        j.        t          j+        j,        j/        j.        fv rdg}ndg} |dta          |j*                  d|j*         d|           nt-          |t          j'        j(        j1                  rpt          j        j        j2        s- |dta          |j*                  d|j*         ddg           n |dta          |j*                  d|j*         ddg           net-          |t          j'        j(        j3                  r|j*        }	d}t-          |	t          j4        j5                  rGt          j6        7                    |	j8        j!        |	j8        j9                  }||\  }}d| d| d| d} |d d!|j*         d| d"g           nt-          |t          j:        j;        j<        j=                  r  ||j>        ta          |          d#$          t-          ||          r ||j>        |j        d                   |t-          |t~                    r+d%ta          |          v r |d&d' j         d(| dg             |ta          |                    @                    |jA                  dd}~ww xY w|s9t          jC        t          jD        t          jF        t          )          |          }t          j        j        j        st          j        j        j        r!j        jH        I                     |
|           |S )*az  
    Run the computation represented by `node` using fake tensors and return the result.

    allow_non_graph_fake: whether to allow the return result to be:
        1. non-fake or 2. fake that is not created by this instance of Dynamo.
        If `True`, you must be prepared to deal with such return values, ideally
        by further wrapping them as this graph's fakes.
    r   )ValueRangeErrorr2   )TorchRuntimeErrorrZ  Unsupported	UserErrorUserErrorTyper  c                V    i | ]&}t          |          t          |          |j        'S rH   )rJ   r  r%  rU  s     r`   rX  z"get_fake_value.<locals>.<dictcomp>  s=     !
 !
 !
&)!
sGGS\!
 !
 !
r_   Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  @   K   | ]}t          |t                    V  d S ru   )r  complex)r   as     r`   r   z!get_fake_value.<locals>.<genexpr>!  sE       M M#$
1gM M M M M Mr_   c              3     K   | ];}t          |t          j                  r|j        j        t          |          n|V  <d S ru   )r  r%  SymFloatr]  hintrI  rU  s     r`   r   z!get_fake_value.<locals>.<genexpr>%  sc       
 
  c5>227:x}7P c


	
 
 
 
 
 
r_   c                 4    t          j                   S ru   )run_noder(  )r	  r  nnmoduler]  r  s   r`   rV  z get_fake_value.<locals>.<lambda>1  s    D$II r_   zkEnable tracing of data-dependent output operators with `torch._dynamo.config.capture_scalar_outputs = True`zConsider wrapping the operator into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html)zData dependent operatorz
Operator `zP` has a non-Tensor output whose value is dependent on the data of Tensor inputs.r  zDynamic shape operatorz.`'s output shape depends on input Tensor data.zmEnable tracing of dynamic shape operators with `torch._dynamo.config.capture_dynamic_output_shape_ops = True`z'Dynamic shape operator (no meta kernel)zA` does not have a meta kernel that supports dynamic output shapesz!Please report an issue to PyTorchr  z:It's possible that the support was implemented in module `z` and you may need to `import z`(z), otherwise z3Operator does not support running with fake tensorszunsupported operator: z}see https://docs.google.com/document/d/1GgvOe7C8_NVOMLOCwDaYV1mXXyHMXY7ExoewHqooxrs/edit#heading=h.64r4npvq0w0 for how to fixconstrain_as_size_example)	case_nameargumentz&TypeError when making fake tensor callz
TypeError z: )r  )Jtorch.utils._sympy.value_rangesr  r  r  rZ  r  r  r  r  r  rJ   r  r	  r  r%  r  rF   use_graph_deduplicationtrack_nodes_for_deduplicationr3   r  rt  r  r>  r  r@  r  r(  r  r  rF  r-  _infer_parametersrl   r  r   r:  r   	__cause__rA  r  DataDependentOutputExceptionfuncopsatenr  r  _local_scalar_denser   DynamicOutputShapeException capture_dynamic_output_shape_opsUnsupportedOperatorExceptionr  r  r  _dispatch_pystub_schemaoverload_namer   r  r  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr  with_traceback__traceback__pytreetree_map_onlyr=  r  partialr  region_trackertrack_node_mutations)r]  r  r  r  r  rZ  r  r  r  r  flat_args_kwargsid_to_initial_versionr  ret_valrf  causer`  import_suggestionmaybe_pystubr  ctxr  r	  r  r  s   ``                    @@@r`   r  r    s    @?????              
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  
 
 
 
 
 
 
hR 	 	022 	 	)IIIIIIII G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     aR aR aR ;"KE5$0M
 
 Z	
 z	#+	2:  K` 1EJI I I I     5$0L
 
 @	 ='H   4
OO gUZ g g gY	     ! E
OO zUZ z z z;	     5$0M
 
 *	 B ""ej344 
$x88JORZ%=     +".KFC/#)/ /IO/ // / / &
 M===( & & &		 	 	 	 	 58(8T
 
 	 )2E

5   
 // 	)M>q	JJPQQy)) 	jCJJ.F.F@;T[;;E;;	     A''66qGGTQCaRF   
 L)+,="EEEw
 

 	4
==
 		 55!	
 	
 	
 Nsa   %H= 'H06HH0H	H0 H	!H0$H= 0H44H= 7H48H= =T KTT Optional[torch.fx.Node]c                 .    t          t          dd           S )Nr   )r
  _current_noderH   r_   r`   get_current_noder    s    ='4000r_   c              #     K   t                      }| t          _        	 d V  |t          _        d S # |t          _        w xY wru   )r  r  r   )r]  olds     r`   set_current_noder    sI      


CM"!c!!!!s   0 >tracerr  c           	       
 j         
t                    5  d
fd}ddlm} 	 
dk    r j        i cd	d	d	           S 
d
k    rnt          d         j                  sddlm}  |dd |d          g             t          d         j                  dd	         i cd	d	d	           S 
dk    r|J  |i cd	d	d	           S 
dk    r+| j        	                    j                  cd	d	d	           S 
dk    r$dj
        v sJ j
        d         cd	d	d	           S n# t          t          f$ rC}ddlm} g }	t          |t                    rdg}	 |dd ||          |	|           Y d	}~nKd	}~w|$ r  t          $ r6}t           ||                                        |j                  |d	}~ww xY wd	d	d	           n# 1 swxY w Y   t%          
          )a  
    Runs a given node, with the given args and kwargs.

    Behavior is dictated by a node's op.

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

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

    Nodes that are not call_function, call_method, call_module, or get_attr will
    raise an AssertionError.
    rf  r   rg   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  )rf  r	  r  r]  r  s    r`   make_error_messagez$run_node.<locals>.make_error_message  s@    p2ppppW[ppagpppq''r_   r2   )r  r  Nr  r   rY  z/Missing attribute when running call_method noder  zattribute not definedr  r  r  placeholderr  z;If the op is a PyTorch op, please file an issue to PyTorch.zGNotImplementedError/UnsupportedFakeTensorException when running FX noder\  )rf  r   rg   r   )r  r  r  r  r  r-  rZ  r
  output_graphget_submoduler  rb  r$  r  r  r   r  r  r3  )r  r]  r	  r  r  r  r  rZ  rf  r`  r  s    ```      @r`   r  r    s   " 
B	$		 7 7	 	 	 	 	 	 	 	 	 	%$$$$$-	_$$"t{D3F337 7 7 7 7 7 7 7 }$$tAw44 555555$$ Q "$6$67N$O$O 	    5wtAw44d122hI&II/7 7 7 7 7 7 7 70 }$$+++x00057 7 7 7 7 7 7 76 z!!*88EE97 7 7 7 7 7 7 7: }$$&$)3333y1?7 7 7 7 7 7 7 7: % $%CD 	 	 	------E!011 Q a..q11          	 	 	 	 	 	11!4455DD 	i7 7 7 7 7 7 7 7 7 7 7 7 7 7 7r 

s_   GD9A'D9?D9$D9D98G9G
9FGG1GGGG#&G#c                J   ddl m} j        }| |v r||          S | j        }t          j        j                            | j        | j	        ffd          \  }}|dk    rd| j
        v r| j
        d         j        S |dk    rDj        j        | j                 }t          |          st!          j        |          }n ||i | nd}	 t%          | |||          }||| <   nC# t&          $ r6}	 |t)          |	                                        |	j                  dd}	~	ww xY w|S )z
    Run the actual computation represented by `node` and return the result.
    This will execute any dependent nodes in the graph as well.
    r2   )r  c                $    t          |           S ru   )get_real_value)r  r  s    r`   rV  z get_real_value.<locals>.<lambda>  s    .F++ r_   r  graphargr  N)r  r  real_value_cacher  r%  r   r]  r  r	  r  r  exampler  r  r  rF  r  r>  r  r   r   r  r  )
r]  r  r  cacher  r	  r  	nn_module
real_valuerf  s
    `        r`   r  r  
  sl   
 '&&&&&#Eu}}T{	B8=((	DK ++++ LD&
 
]zTY66y$,,	]'24;?	i(( 	'i00II It&v&&&&	RfdD&)DD
 d R R RA''66qGGTQRs   C   
D *1DD c                   ddl mm} dfd}|                                 D ])\  }} ||          rJ d| d	 ||                       *|                                 D ])\  }} ||          rJ d
| d	 ||                       *d S )Nr   )FakeTensorConfigrJ   r  r   rg   r   c                N    j         rdd l}d |j        | j                   S dS )Nr   z"FAKE TENSOR CREATION TRACEBACK: 
 zNEnable TORCH_FAKE_TENSOR_DEBUG=1 to get creation stack traces on fake tensors.)debug	tracebackformat_list_debug_trace)r  r  r
  s     r`   stack_or_hintz7assert_no_fake_params_or_buffers.<locals>.stack_or_hint4  s?    ! 	d`9N9Nq~9^9^```ccr_   zUnexpected fake buffer r   zUnexpected fake param )r  r   rg   r   )torch._subclasses.fake_tensorr
  rJ   named_buffersnamed_parameters)r   rJ   r  rl   bufferr&  r
  s         @r`    assert_no_fake_params_or_buffersr  1  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	
 	
! 	

 
r_   c                $    | j          d| j         S )z9
    Returns the fully qualified name of the object.
    r  )rZ   r[   r  s    r`   fqnr  F  s     n11s/111r_   count1count2c                6    t           j        j        j        r| S |S ru   )r%  r  rF   assume_static_by_default)r  r  s     r`   ifdynstaticdefaultr  M  s    }4 r_   types.ModuleTypec           
     F   t          t          j        t          j                            t          t          | j                                                D ]I}|                    d          r2|d         dk    r&t          j
        | j         d|dd                     JdS )z@
    Ensure all the files in a given submodule are imported
    z.pyr   r  r  N)r  r}  listdirr  r  r   r   __file__r,  	importlibimport_modulerY   )rD  r  s     r`   import_submoduler$  T  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r_   c                :    t          t          |                     S ru   )class_has_getattributer  r  s    r`   object_has_getattributer'  ]  s    !$u++...r_   c                F    t                               | d          }|||<   d S )Nr  )r   __getattribute__)r  rl   r   r  s       r`    object_setattr_ignore_descriptorr*  a  s$    Z00AAdGGGr_   c                    	 t          t          j        | d          t          j                  rdS n# t
          $ r Y nw xY wdS )Nr)  TF)r  r  r  r  r  r  rw   s    r`   r&  r&  g  sc    "3(:;;
 
 	 4		
    5s   -2 
??ignore_nn_module_getattrr  c                    	 t          j        t          |           d          }n# t          $ r d }Y nw xY w|r|t          j        j        j        u rd }|S )N__getattr__)r  r  r  r  r%  r>  r  r.  )r   r,  
getattr_fns      r`   get_custom_getattrr0  s  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 )TensorStaticReasonrW   rf  r   N)rY   rZ   r[   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYrH   r_   r`   r2  r2    s         IJr_   r2  r8  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 )r2  r3  r4  r5  r3  )r8  s    r`   tensor_static_reason_to_messager7    sX    #---OO#...CC#666EE
3633
4
44r_   r[  Union[torch.Tensor, Any]	is_tensortensor_sourcer#   )tuple[bool, Optional[TensorStaticReason]]c                   ddl m} |                                                                s&|                                                                rt
          j        rdt          j        fS t          |           t          j        j        u s ||          rt
          j        rdt          j        fS |sdt          j        fS dS )a@  
    Given a tensor, source, and is_tensor flag, determine if a shape should be static.

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

    Returns a tuple, where the first element is the bool of whether or not this tensor should have a static shape.
    The second element is a TensorStaticReason, useful for passing to tensor_static_reason_to_message if needed.
    r2   ))is_from_unspecialized_param_buffer_sourceTr~  )rb  r=  rf  rh  rj  rF   &force_nn_module_property_static_shapesr2  r5  r  r%  r>  r?  force_parameter_static_shapesr3  r4  )r[  r9  r:  r=  s       r`   tensor_always_has_static_shaper@    s      BAAAAA 	""$$==??;%%''JJLL; 
7; '::: 	V***44]CC 	+

. 	+ '111 3'222;r_   r  c                2     d fd}t          |          S )Nrg   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 rH   )r  rl   r  r	  r  )r   r  s     r`   rY  z<lazy_format_graph_tabular.<locals>.inner.<locals>.<listcomp>  s:     
 
 
;<QT1618QVQX6
 
 
r_   )opcoderl   r  r	  r  r   )
r   r   r   r,   graphr  r+   forward__code__rs  )r   
node_specs	graph_strr  r   s      r`   innerz(lazy_format_graph_tabular.<locals>.inner  s    	))))))) 	 	 	~,Wb99::;  	
 
@B
 
 

 H N N N
 
 
	 "'2:+>+JIVVVs   
 *77rg   r   rC   )r  r   rJ  s   `` r`   lazy_format_graph_tabularrL    s>    W W W W W W W" er_   line_noc                l    |  d| d| d| dt          j        |                                            d
S )Nr   z line z 
r   )disBytecode)r0  rl   r  rM  r  s        r`   format_bytecoderQ    sI     VVtVVhVVgVV#,t:L:L:P:P:R:RVVVVr_   _forward_pre_hooks_forward_hooks_backward_pre_hooks_backward_hooks)_state_dict_pre_hooks_state_dict_hooks_load_state_dict_pre_hooks_load_state_dict_post_hooksc                     t          t          t          j        j        j        j                  p't          t          j        j        j        j                            S ru   )r   rt  r%  r>  modulesr  _global_backward_hooks_global_backward_pre_hooksrH   r_   r`   nn_module_has_global_hooksr^    sH     EH#:;; 	Cux&ABB  r_   torch.nn.Module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   )rD  r`  ra  rb  hook_dicts_to_checkcheck_all_hooks	all_hookshook_dict_namehooks	hook_namer   s              r`   nn_module_get_all_hooksrn    s      	'$$	'&& 
  7o 7""#5666 8 8""#6777 :""#8999I- # #^R00 	# 	#I#DT""""	# r_   c                F    t          | |||          }t          |          S )zL
    Helper function to check if a module has any hooks attached to it.
    )r`  ra  rb  )rn  r   )rD  r`  ra  rb  rl  s        r`   nnmodule_has_hooksrp    s2     $/15	  E ;;r_   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 ru   )to_numpy_helperr   r  s     r`   r   z"to_numpy_helper.<locals>.<genexpr>  *      AAC?3//AAAAAAr_   )rJ   r  tnpr:  rt  r[  r%  r=  numpyr  r_  r  r  s    r`   rt  rt    s    u~~ %%% u|,,,	E5<	(	( {{{&&&	EE4=	)	) tE{{AA5AAAAAAr_   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 ru   )numpy_to_tensorru  s     r`   r   z"numpy_to_tensor.<locals>.<genexpr>*  rv  r_   )
r  r  r:  r%  r  rw  r[  r  r_  r  r  s    r`   r{  r{  "  s    >>>%$$ &u%%%%%% |	EE4=	)	) tE{{AA5AAAAAAr_   c                  &    e Zd ZddZddZddZdS )numpy_to_tensor_wrapperr  Callable[_P, R]rg   rh   c                <    || _         d| j         j        z   | _        d S Nwrapped_r  rY   )rj  r  s     r`   rk  z numpy_to_tensor_wrapper.__init__0  s    "TV_4r_   r   c                "    d| j         j         dS )Nz<Wrapped function <original >>r  rn  s    r`   __repr__z numpy_to_tensor_wrapper.__repr__4  s    AdfoAAAAr_   r	  r
  r  r  r   c                :     | j         |i |}t          |          S ru   )r  r{  rj  r	  r  r   s       r`   r  z numpy_to_tensor_wrapper.__call__7  s&    dfd%f%%s###r_   N)r  r~  rg   rh   rK  r	  r
  r  r  rg   r   )rY   rZ   r[   rk  r  r  rH   r_   r`   r}  r}  /  sT        5 5 5 5B B B B$ $ $ $ $ $r_   r}  c                   t          | t          j                  rt          | |          }t	          |          S t          | t
          j                  r1t          t          j        |           |          }t	          |          S d S ru   )r  rw  r:  r
  r{  r%  r=  )r  rl   r   s      r`   numpy_attr_wrapperr  <  sx    #s{## $c4  s###	C	&	& $ck#&&--s###$ $r_   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   rg   rh   c                2    || _         d| j         z   | _        d S r  )r  rY   )rj  r  s     r`   rk  znumpy_method_wrapper.__init__H  s    "T[0r_   c                    d| j          dS )Nz<Wrapped method <original r  )r  rn  s    r`   r  znumpy_method_wrapper.__repr__L  s    ;DK;;;;r_   r	  r   r  c                    |d         }t          |t          j                  rt          j        |          }t          || j                  } ||dd          i |}t          |          S rW  )r  r%  r=  rw  r:  r
  r  r{  )rj  r	  r  r  method_callabler   s         r`   r  znumpy_method_wrapper.__call__O  si    1gc5<(( 	#+c""C!#t{33otABBx2622s###r_   N)r  r   rg   rh   rK  )r	  r   r  r   rg   r   rY   rZ   r[   r   rk  r  r  rH   r_   r`   r  r  E  sV        qq1 1 1 1< < < <$ $ $ $ $ $r_   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@  rg   rh   c                2    || _         d|j         | _        d S r  )r  rY   )rj  r  s     r`   rk  znumpy_operator_wrapper.__init__[  s    02;00r_   r   c                    d| j          dS )Nz<Wrapped operator <original r  )rY   rn  s    r`   r  znumpy_operator_wrapper.__repr___  s    ?dm????r_   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 ru   )r  r%  r=  rw  r:  rU  s     r`   r   z2numpy_operator_wrapper.__call__.<locals>.<genexpr>e  sS       
 
KN
3 = =FCK3
 
 
 
 
 
r_   )r  r{  r  s       r`   r  znumpy_operator_wrapper.__call__b  sH    z
 
RV
 
 
 dgtns###r_   N)r  r@  rg   rh   rK  r  r  rH   r_   r`   r  r  X  sZ        [[1 1 1 1@ @ @ @$ $ $ $ $ $r_   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  rI   _has_symbolic_sizes_stridesr  r%  r\  r   r]  r  	size_hintexprr  empty_stridedr  r  r  zero_)rU  r  r{  r  r  s        r`   defaker  l  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Hr_   r~  r
  r  c                     | |i |S ru   rH   )r0  r	  r  s      r`   8_disable_side_effect_safety_checks_for_current_subtracerr    s     2tvr_   c                0    dd l }| |j        j        j        u S r   )torch.utils.checkpointr&  
checkpoint)r  r%  s     r`   is_utils_checkpointr    s!    !!!!%+(333r_   c                    ddl m} | |u S )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphr  )r  r  s     r`   is_invoke_subgraphr    s     SSSSSS---r_   optionsc                 H    ddl m}  |j        t          j        j        fi | S )Nr2    TorchHigherOrderOperatorVariable)variables.higher_order_opsr  maker%  _higher_order_opsinvoke_subgraph)r  r  s     r`   build_invoke_subgraph_variabler    sB    LLLLLL0+0/ 
  r_   c                     dd l mc m} ddlm} |j        }t          j        j        j	        r|j
        } |j        |fi | S )Nr   r2   r  )torch._higher_order_ops.wrapr  wrapr  r  tag_activation_checkpointr%  
_functorchrF   functionalize_rng_opswrap_activation_checkpointr  )r  higher_order_opsr  activation_checkpoint_ops       r`   build_checkpoint_variabler    s    ;;;;;;;;;LLLLLL
 	2  4 O#3#N 0+0  
  r_   device_typerA   c                    ddl m} t          j        |           j        } |            }|dk    rn|dv r|rt                      }nd}|S )Nr2   )is_dynamo_supportedr  )r.  xpumtiaF)
eval_framer  r%  r  r  r/   )r  r  r  compile_supporteds       r`   is_compile_supportedr    sl    //////<$$)D++--u}}	(	(	(->	(&LL!r_   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)encodert  decode)r   r  as_utf8s      r`   _fix_offsetr    s?     jj!!Gwww&&wy&AABBBr_   c                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )_Anchorsrf   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetNr  rH   r_   r`   r  r    sD          r_   r  segmentOptional[_Anchors]c                z   t           j        dk    sJ ddl}	 |                    d| z   dz             }n# t          $ r Y dS w xY wt          |j                  dk    rdS |                     d          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
    )rX      r   Nz(
z
)r2   r   linenorf   r  rg   c                0    t          |          |          S ru   )r  )r  r  liness     r`   	normalizez-_extract_anchors_from_expr.<locals>.normalize  s    5=&111r_   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 rW  rt  )r  r  r  s     r`   next_valid_charz3_extract_anchors_from_expr.<locals>.next_valid_char  s    s5zz!!cSv-?-?&?&?CaKF s5zz!!cSv-?-?&?&? E

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

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

""sSv-?-?'?'?'??s{r_   rW   z)\#z\#r  r  )r  rf   r  rf   rg   rf   )r  rf   r  rf   rg   r  )r  r  astparseSyntaxErrorrt  bodyr  r  Exprr   BinOpr   rf   left
end_linenoend_col_offsetisspacer  	SubscriptCallr  )r  r  treer  r   r  	statementr  
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor  r  s                  @@r`   _extract_anchors_from_exprr    s    w&&&&JJJ 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   6 
AAtypes.CodeTypeinstdis.Instructionc                Z    t           j        dk    rft          j         j        |j        j         j        |j        j        |j        j	        |j        j
                  }t          j        |g          d         }d                    |                                dd                   } fdt          |j        j        |j        j        dz             D             }t          j        d                    |                                                    }t#          |d                   t#          |d                   z
  }|d         d|         }t          j        t          j        |          |          }|S |j        J |j        j        dS t'          j         j        |j        j                                                  }|j        j        |S |j        j	        |j        j
        |S t-          ||j        j	                  }	d}
d}g }|j        j        |j        j        k    rGt-          ||j        j
                  }
||	|
         }|                    d	|	z  d
|
|	z
  z  z              n||	d         dz   }|                    d	|	z  d
t#          |          |	z
  z  z              t'          j         j        |j        j                                                  }t-          ||j        j
                  }
t          |j        j        dz   |j        j                  D ]}t'          j         j        |                                          }||dz   z  }t#          |          t#          |                                          z
  }|                    d	|z  d
t#          |          |z
  z  z              ||d|
         z  }t#          |          t#          |                                          z
  }|                    d	|z  d
|
|z
  z  z              d}	 t3          |          }n# t4          $ r Y nw xY w|d |D             }nd |D             }|j        dk    r|xj        |	z  c_        |j        dk    r|xj        |	z  c_        t          t#          |                    D ]}t          t#          ||                             D ]e}||j        k     r||j        k    r||j        k     r%||j        k    r||j        k    r<||j        k    rH||         |         d
k    rd||         |<   fd |D             }d}t          t#          |                    D ]O}|t'          j         j        |j        j        |z                                             dz   z  }|||         dz   z  }P|S )a  
    Python 3.11+ only. Returns lines of source code (from code object `code`)
    corresponding to `inst`'s location data, and underlines relevant code to `inst`.

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

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

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

&(( 	 !DN,IJJ
DN1A5t~7PQQ 	N 	NF$T%5v>>EEGGDtd{"GTS%7%77JNN3+cSYY5K.LLMMMM9[j[))^^c)*:*:*<*<&=&==
sZ'#j1H*IIJJJ"&G,W55    BB'BBB ,P+Ow+O+O+O "a''##|3##%**&&,6&& CLL)) 	7 	7FS!899:: 7 7G333W444w?V9V9Vg888w999G666"6*3/36636OF+C07  BAAAAF3w<<   $ $d.0E0IJJQQSS	
 	'!*t##Ms   P) )
P65P6c                \    t          | t          j                  rt          | dd           S d S )N_dynamo_static_input_type)r  r%  r=  r
  r\  s    r`   get_static_address_typer"    s.    !U\"" =q5t<<<4r_   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 ru   )	r%  r  r5   	get_statedefault_generatorr  r.  	set_stater  )r   getterssetterss      r`   is_rng_state_getter_or_setterr)    ss     	$)
 G 	$)
 	G (g(((((r_   c                    t          | t          j                  o'| j        dk    o| j        j        t          j        j        u S )N__get__)	r  r  r   rY   __self____objclass__r%  r  _TensorBaser  s    r`   is_tensor_base_attr_getterr/    s?    5%122 	@Ni'	@N'58+??r_   c                    	 t          j        t          j        |           }t	          |          t
          j        u S # t          $ r Y dS w xY wr  )r  r  r%  r=  r  r  r-  r  )rl   attrs     r`   is_tensor_getset_descriptorr2    sQ    %elD99DzzU777   uus   9< 
A
	A
c                "    t          | d          S )N__torch_function__)r-  r  s    r`   is_torch_function_objectr5    s    5.///r_   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_valuer4  F)r  r7  &torch._dynamo.variables.torch_functionr8  rr  r-  r9  r  r
  
class_typer   r%  r  _disabled_torch_function_impl)r  r7  r8  r  s       r`   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5r_   c                    d }d }t           j        j                                        x}r| |j        v r|j        |          }|j        }|                    | d||          S )NF)static_shapessymbolic_contextrb  )r%  r)  r$   try_gettensor_to_contextr:  from_tensor)r  r  r@  rb  tracing_contexts        r`   to_fake_tensorrE  /  sx     F-6>>@@@ 4111.@C%3F  	1A& !   r_   c                    t          |            oOt          |            o?t          |           o0t          | d          o t          | j        d          o| j        j        S )N__dataclass_params__frozen)r'  r&  r   r-  rG  rH  r  s    r`   is_frozen_dataclassrI  ?  st    #E*** 	.&u---	.	. E122	. E.99		.
 &-r_   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
  r3  )r  rJ  r1  s      r`   get_first_attrrL  J  s^      & &3 	&3%%%%%	& CMMeMM
N
NNr_   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   rg   c                <    dd}t          j        | |          S )Ngm_r   example_inputs_rg   c                    t           j        j        j        d         dxx         dz  cc<   t           j                            | |          S )Ncompiled_autogradcompilesr2   )r%  r  r&  rO   r  compile)rR  rS  s     r`   inner_compilerzKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compiler^  sE    #,-@A*MMMQRRMMM..sODDDr_   )backendrN  rO  )rR  r   rS  r   rg   r   )r%  rW  )r   rX  rO  rN  s     r`   compiler_fnz3maybe_enable_compiled_autograd.<locals>.compiler_fn]  s?    E E E E =Ni   r_   )r   r   rg   r   )r%  r  rU  _enable)rM  rN  rO  rZ  r  s    ``  r`   maybe_enable_compiled_autogradr\  U  s        	 	 	 	 	 	 	 ],44[AA 	SIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A

AAr1   c                 V     G d dt                     } t           |                       S )Nc                      e Zd ZdS )*invalid_removeable_handle.<locals>.InvalidN)rY   rZ   r[   rH   r_   r`   Invalidr_  l  s        r_   r`  )r  r1   )r`  s    r`   invalid_removeable_handlera  j  sB        $    7799%%%r_   c                    t          | t          j        j                  s| S t          | t          j        j                  r| S | j                            | j                  }| j        |_        |S ru   )	r  r%  r>  r  r   r  r  r  r  )rD  proxys     r`   nn_module_proxyrd  v  s^    c58?++ 
#ux+,, 
M!!#-00E\ENLr_   c                  (     e Zd Zd fdZdd
Z xZS )	GmWrapperr   r!  unflatten_fnCallable[[list[Any]], Any]rg   rh   c                d    t                                                       || _        || _        d S ru   )r  rk  r   rg  )rj  r   rg  r  s      r`   rk  zGmWrapper.__init__  s0     	(r_   r	  r   c                Z    t          |          } | j        |                     |           S ru   )r_  r   rg  r  s     r`   rF  zGmWrapper.forward  s*    t**tw))$//00r_   )r   r!  rg  rh  rg   rh   r	  r   rg   r   )rY   rZ   r[   rk  rF  r	  r
  s   @r`   rf  rf    sQ        ) ) ) ) ) )1 1 1 1 1 1 1 1r_   rf  inputs
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  ry   )r   ry  r]  s      r`   rY  z(flatten_graph_inputs.<locals>.<listcomp>  sH       At7m##	k5(I(I# 	
###r_   r   r	  r   rg   c                B    | d         t          | dd                    z   S rW  r  rH  s    r`   
flatten_fnz(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qrr(^^++r_   	flat_argsc                0    | d          g| d          R S ru   rH   )rt  boxed_inputs_counts    r`   rg  z*flatten_graph_inputs.<locals>.unflatten_fn  s,    1112TY?Q?R?R5STTTr_   )treespecc                 n     |           }D ]}| |                                            |          S ru   )rv   )r	  rt  ry  compiled_fnrs  inputs_idx_to_clears      r`   wrapperz%flatten_graph_inputs.<locals>.wrapper  sH    Jt$$	 % 	 	AGMMOOOO {9%%%r_   rk  )rt  r   rg   r   )	enumeraterE  r  r%  r  rU  in_compiled_autograd_regionr  r_  rt  rf  r  tree_flattenr  r  tree_unflattenarg_tree_leaves)r   rl  rm  rg  flat_inputsspecr{  rv  ry  rs  rz  s          @@@@r`   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+
& & & & & & & & Nr_   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  ry   )r  s    r`   get_locals_to_stealr    sH    h 455 WXv=V=V 	=.333r_   r  c                    || j         d<   d S )Nr  r  )r   r  s     r`   set_locals_to_stealr    s    !0BGr_   c                      e Zd ZddZd	dZdS )
Litr{  r   rg   rh   c                    || _         d S ru   r{  )rj  r{  s     r`   rk  zLit.__init__  s    r_   c                    | j         S ru   r  rn  s    r`   r  zLit.__repr__  s	    vr_   N)r{  r   rg   rh   rK  )rY   rZ   r[   rk  r  rH   r_   r`   r  r    s<                r_   r  zset[str]warn_once_cacher9  
stacklevelc                    | t           v rd S t                               |            t          j        | |dz              d S )Nr2   )r  )r  rw  warningswarn)r9  r  s     r`   	warn_oncer    sG     oM#*q.111111r_   textc                V    t          j        d          }|                    d|           S )Nz\x1B[@-_][0-?]*[ -/]*[@-~]r  )r  rW  r  )r  ansi_escapes     r`   strip_color_from_stringr    s&    *:;;K??2t$$$r_   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    r`   +_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 ru   )r%  r  rF   freezingis_grad_enabledrH   r_   r`   is_parameter_freezingr    s!    ?!*J53H3J3J/JJr_   c                 L    d t          t                                D             S )Nc                ,    g | ]}t          |          S rH   ) get_torch_function_mode_stack_atrC  s     r`   rY  z1get_torch_function_mode_stack.<locals>.<listcomp>  s.       01(++  r_   )r  r   rH   r_   r`   get_torch_function_mode_stackr    s2     5:;T;V;V5W5W   r_   indc                t    | t                      k     r| dk    sJ t          j                            |           S r   )r   r%  r  _get_function_stack_at)r  s    r`   r  r    s7    *,,,,98**3///r_   r9  c                    t          t                                D ]}t                       | D ]}t          |           d S ru   )r  r   r   r   )r9  r  modes      r`   set_torch_function_mode_stackr    sY    ,..// $ $!#### , ,%d++++, ,r_   c                 ^    t          t                                D ]} t                       d S ru   )r  r   r   )r  s    r`   clear_torch_function_mode_stackr    s9    ,..// $ $!####$ $r_   c                 "    t                       d S ru   )
breakpointrH  s    r`   _breakpoint_for_c_dynamor    s    LLLLLr_   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 )NrW   r2   )InternalTorchDynamoErrorz[Tensor subclass method __metadata_guard__ must take exactly two subclass metadata argumentsz?Tensor subclass method __metadata_guard__ must be a classmethod)	__metadata_guard__r  	signaturert  r+  r  r  r,  r  )r   r0  sigr  s       r`   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
 
 	
 &%r_   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 ru   )r   r  r   rL  r`  __iter__)r  s    r`   #does_not_override_dict_iter_methodsr  "  sl    4:{'899 	GO[-?@@	GMdi)9::	G $-1E!FF	r_   ry  c                ,    |                      |          S ru   )r  rU  ry  s     r`   	call_sizer  .  s    66!99r_   c                ,    |                      |          S ru   )r  r  s     r`   call_strider  3  s    88A;;r_   c                *    |                                  S ru   )storage_offsetrT  s    r`   call_storage_offsetr  8  s    r_   c                ,     ddg} fd|D             }|S )Nr!  tagc                b    i | ]+}|j         v |t          j        j         |                   ,S rH   )r  r  )r   r   r  s     r`   rX  z(_extract_tensor_dict.<locals>.<dictcomp>E  s>       ,/3!*CTCTTYqz#''CTCTCTr_   rH   )r  KEYS_TO_COPYtensor_dicts   `  r`   _extract_tensor_dictr  ?  s@    #L
   3?  K r_   z(dict[int, weakref.ReferenceType[object]]user_obj_id_to_weakrefobj_idc                F    t          |                      }|
J d            |S )NzUser object is no longer alive)r  )r  r  s     r`   get_user_object_from_idr  R  s'    
 
(
*
*C??<???Jr_   c                \    t          |           }t          j        |           t          |<   d S ru   )r  r  r  r  )r  r  s     r`   store_user_object_weakrefr  X  s(    WWF%,[%5%56"""r_   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   rf   _counterrc  _idrg   rh   c                n    | j         dz   | _         | j         dk    rt          j                    | _        d S d S r   )_depthr   r  r  rw   s    r`   r  z#CompileTimeInstructionCounter.startb  s5    Z!^
:??*022CGGG ?r_   c                    | j         dz
  | _         | j         dk    r0| xj        t          j        | j                  z  c_        d| _        d S d S )Nr2   r   rc  )r  r  r   endr  rw   s    r`   r  z!CompileTimeInstructionCounter.endh  sJ    Z!^
:??LL04SW===LLCGGG ?r_   c                    d| _         d S r   r  rw   s    r`   rv   z#CompileTimeInstructionCounter.clearo  s    r_   c                    | j         S ru   r  rw   s    r`   r   z#CompileTimeInstructionCounter.values  s
    |r_   r  c              #     K   	 t           j        r|                                  d V  t           j        r|                                  d S d S # t           j        r|                                  w w xY wru   )rF   %record_compile_time_instruction_countr  r  rw   s    r`   recordz$CompileTimeInstructionCounter.recordw  st      	; 		EEE; 					 v; 				s   $A #A/Nr   r   rg   r  )rY   rZ   r[   r  r   r  r  r   r  r  rv   r   r   r  rH   r_   r`   r  r  ]  s         HCMMMMF3 3 3 [3
    [    [    [    ^ [  r_   r  c                        e Zd Zd fdZ xZS )CompileCounterIntotherr   rg   c                `    t          t                                          |                    S ru   )r  r  __add__)rj  r  r  s     r`   r  zCompileCounterInt.__add__  s!     !7!7888r_   )r  r   rg   r  )rY   rZ   r[   r  r	  r
  s   @r`   r  r    s=        9 9 9 9 9 9 9 9 9 9r_   r  featureusagec                    t                                                      r%t                                          d| |           dS dS )zQ
    Records whether we are using a feature
    Generally a feature is a JK.
    r  N)r   r   set_key_value)r  r  s     r`   set_feature_user    sO     ((** M++OWeLLLLLM Mr_   )ddp_optimizerpython_reducer'python_reducer_without_compiled_forwardno_optimization_ddp_optimization_modec                     t           j        } t          | t                    r| rdnd}n7t          | t                    r| }nt          dt          |                     |t          v sJ d|            |S )Nr  r  z;Invalid dynamo config optimize_ddp type type(optimize_ddp)=z.Invalid dynamo config optimize_ddp value mode=)rF   optimize_ddpr  r   r   r  r  r  )r  r  s     r`   get_optimize_ddp_moder    s    &L,%% 
".E4E	L#	&	& 
LtL7I7ILL
 
 	
 ))));D;; *)) Kr_   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)rF   "fake_tensor_disable_inference_moder%  is_inference_mode_enabledinference_moder  )is_inference_mode_ons    r`   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)rF   r  r%  rA  
meta_utils$disable_inference_mode_for_fake_proprH   r_   r`   *maybe_disable_inference_mode_for_fake_propr    s       0 )NNPP 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAc                .    | d u pd| j         v pd| j         v S )Nr  rJ  r  )r]  s    r`   is_node_meta_validr    s$    4<M?di7M5DI;MMr_   c                     t           S ru   _error_on_graph_breakrH   r_   r`   _get_error_on_graph_breakr    s      r_   c                
    | a d S ru   r  r  s    r`   _set_error_on_graph_breakr    s    !r_   AbstractContextManager[None]c                     t           j        j        j        r$t           j        j                            d          nt          j                    } | 	                                 | S )NzPregraph bytecode)
r%  autogradr&  _is_profiler_enabledr  	_profiler_RecordFunctionFastr/  nullcontext	__enter__cms    r`   record_pregraph_bytecode_enterr    sR     >"7	&../BCCC#%% 
 LLNNNIr_   r  c                4    |                      d d d            d S ru   )__exit__r
  s    r`   record_pregraph_bytecode_exitr    s    KKdD!!!!!r_   Optional[list[CodeType]]c                 ,    ddl m}   | j                    S )Nr   r$   )torch._guardsr$   get_traced_coder  s    r`   r  r    s%    ,,,,,,)>)+++r_   )r   r   r   r   rg   r   r   )r   r   rg   rh   )rg   r   )r   rf   rg   rh   )rg   rS   )rg   r    )rg   r!   )NFNNNNFN)r   r   r  r  r   r   r   r  r  r  r  r  r  r  r  r   r  r  rg   r  r  )rA  rB  rC  r   rg   r   )rA  rG  rC  r   rg   rH  )r   F)rA  r   rC  r   rg   rJ  )rg   rx  )r  r  r  r
   rg   r   )r  r   r  r=   rg   rh   )r  r  rg   rf   )rU  rL   rg   rL   )rU  r   rg   r   )r	  r   r  r   rg   rh   )r  r   r  r  rg   r  )r  r   r  r  rg   r  )r  r   r  r  rg   r   )r  r   r  r   rg   r   )r   r   rg   r   )r   r  rg   r	  )r   r   rg   r  )r   r   rg   r  )r   r   rg   r  )r   r   rg   r+  )r0  r   rg   r   )r0  r   rg   r4  )r   r   rg   r8  )r  r   rg   r   )rD  r   rg   r   r  ru   )rJ  r   rg   rK  )r	  r   r  r   rg   rP  )r^  rh  rg   r   )r  rq  rg   rh   r}  )r  rf   r  rf   r  rP   r  r  r  r  rg   rh   )r5  rf   rg   rh   )rg   r=  )rg   rA  )rg   r   )FF)r   r   r  r   r   r   rg   r  r  )rU  r  r  r  rg   r  )r  r  rg   r  )r  r  rg   r  )r  r   rg   r   )rJ  r  rg   rh   r  )r  r   rg   r  )r  r   r  r   rs  r   rg   r   )r  r   rg   r  )rm   r   rg   r   )rm   r  rg   r  )r   r!  rg   r"  )r2   )r  r   r  r.  r/  rf   rg   r0  )r   r!  r  r.  rg   r   )r  rf   rg   r@  )r^  r   rg   r   )rg   rU  )r   r   rg   rY  )r   r   rb  r   rg   r   )rV  r   rg   r   )r	  r.  r  r|  rg   r   )r  r  rg   r  )r  r  rg   r  )rm   r   rg   r   )r  r  rg   rf   )r  r   r  rf   rg   r   )r  r   rg   r  )r  r   rm   r  rg   r   )r  r  r  rf   rg   r   )r{  r  r  rf   rg   rL   )r   r   rN  r   rg   r   )r]  r  r  r   rg   rh   )r  r@   rg   r   )r{  r  r  rf   rg   rf   )r  r?   rl   r   rg   rh   )
r   r.  r  r   r  r>   r  r   rg   r   )rX  r   rg   r  )r   r   rg   r  )rU  r   rN  r   rg   r   )r  r   rN  r   rg   r   )r  r?   r%  r   r  r   rg   r   )r  rL   r*  r+  rg   r   )r0  r   r*  r   r/  r   rg   r   )r0  r6  rg   r   )r  r   r  r;  rg   r   )r  r  r  r  rg   r  )r  r   r  r   rF  r   rG  r   rH  rI  rJ  r   rK  r   rL  r   rM  r   rN  rO  rP  r   rQ  r   rg   r   )r  r
   rg   r   )r  r   rg   r   rK  )T)r]  r  r  r   rg   r   )rf  r   r  r?   rg   r   )r  r?   r  r   r  r   rg   r   )r]  r  r  r?   r  r   rg   r   )rg   r  )r]  r  rg   r  )r  r   r]  r  r	  r   r  r   r  r   rg   r   )r]  r  r  r   rg   r   )r   r!  rg   rh   )r  r   rg   r   )r  r   r  r   rg   r   )rD  r  rg   rh   )r  r   rl   r   r   r   rg   rh   )rm   r  rg   r   )r   r   r,  r   rg   r  )r8  r2  rg   r   )r[  r8  r9  r   r:  r#   rg   r;  )r  r   r   r!  rg   r   )r0  r   rl   r   r  r   rM  rf   r  r   rg   r   )FFF)
rD  r_  r`  r   ra  r   rb  r   rg   r  )
rD  r_  r`  r   ra  r   rb  r   rg   r   )r   r   rg   r   )r  r   rl   r   rg   r   )rU  r   rg   r   )r0  r~  r	  r
  r  r  rg   rM   )r  r   rg   r   )r  rA   rg   r   )r   r   r  rf   rg   rf   )r  r   rg   r  )r  r  r  r  rg   r   )r  r   rg   r   )rl   r   rg   r   )r  r@   rg   r   )r  r  r  r;  rg   r   )r  r   rJ  r   rg   r   )TT)rM  r   rN  r   rO  r   rg   r  )rg   r1   )rD  r   rg   r   )r   r!  rl  r   rm  rn  rg   r@  )r  r   rg   r  )r   r!  r  r  rg   rh   )r9  r   r  rf   rg   rh   )r  r   rg   r   )rg   r  )r  rf   rg   r   )r9  r  rg   rh   )r   r   rg   rh   )r  r   rg   r   )rU  r   ry  rf   rg   rf   )rU  r   rg   rf   )r  r  rg   rP   )r  rf   rg   r   )r  r   rg   rh   )r  r   r  r   rg   rh   )r]  r  rg   r   )r   r   rg   rh   )rg   r  )r  r  rg   rh   )rg   r  (  r   
__future__r   atexitr   r/  r  r#  r  rO  rP  r  r3  r"  r  r   r  r   r  r  r  r}  r  r  r  rM  r   r  r  r  rP  r  r  r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   r%  torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreer&  _pytreer  r   torch._Cr   r   r   r   torch._dispatch.pythonr   torch._dynamo.metrics_contextr    r!   r  r"   r#   r$   torch._subclasses.meta_utilsr%   torch._utils_internalr&   r'   r(   r)   r*   torch.fx._utilsr+   r,   torch.monitorr-   torch.nn.modules.lazyr.   torch.utils._tritonr/   r0   torch.utils.hooksr1   graph_utilsr3   r  collections.abcr4   r5   r6   r7   r8   r9   r:   r;   r<   torch._dynamo.replay_recordr=   torch._dynamo.symbolic_convertr>   r?   torch._dynamo.variables.baser@   torch._prims_commonrA   rx  r  r)  torch._loggingtorch._numpy_numpyrw  rB   rD   r  rF   fftlinalgr  rG   r   NP_TO_TNP_MODULEr  rI   rJ   rK   r   rL   rM   rN   r>  r  r.  unpatched_nn_module_getattrr  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implr   rO   rQ   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgr  rY   r   rR   rI  rT   ro   timer_counterrQ  rV   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rN  r.  r@  rF  registerrc  FloatTensorr  DoubleTensorr  double
HalfTensorr  halfBFloat16Tensorrh  
ByteTensorr  
CharTensorr  
LongTensorr  long	IntTensorr  rf   ShortTensorr  short
BoolTensorr   tensortype_to_dtypere  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ParamSpecArgsParamSpecKwargsTypeVarTupleTypeAliasTyper  r  r  r  r  r  r   r!  r"  r  r  r  r  neltlegtgecmp_name_to_op_mappingcmp_name_to_op_str_mappingr/  r3  r2  r;  rC  rF  rI  rO  rg  rm  ro  r  r  rq  !DEFAULT_COMPILATION_METRICS_LIMITr8  r  r  r  r  r3  r   r   r:  r<  r?  rA  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r-  r9  r?  rK  r  r   re   r  Ellipsisr  NotImplementedr  r  memory_formatr  finfoiinfo	attention
SDPBackendr.  _CudaDevicePropertiesrL  r/  rw  languagerO  r  rX  ra  rl  rw  r{  r  r  r  r  r`  r  rL  r  r   r  r  iterr  r  r  __length_hint__tuple_iterator_lenr   r  r  r  dict_newr   r  dict_methodsrm  set_methodsrN  frozenset_methodsr  	tuple_newtuple_methodsr_  list_methodsr  list_getitemstr_methodsr  r  r  r  r  r  r  r  r  	iter_nextr  r  dict_getitemr  r  r  r  r  r  r  r  r  r  r  r"  GLOBAL_KEY_PREFIXtorch._subclassesr$  r)  r.  r5  r:  r@  rE  r  rU  r  r  orig_code_mapr  r  seen_code_mapr  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r'  r*  r&  r0  r2  r7  r@  rL  rQ  re  rf  rg  all_hook_namesr^  rn  rp  rt  r{  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r)  r/  r2  r5  r=  rE  rI  rL  r\  ra  rd  rf  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  _disable_dynamor  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rH   r_   r`   <module>rs     s3#     # " " " " "                



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



                , , , , , , , , = = = = = = = = $ $ $ $ $ $       - - - - - - - -
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 O N N N N N N N N N N N N N      , , , , $ $ $ $ $ $ $ $ $                  < ; ; ; ; ; O O O O O O O O ; ; ; ; ; ; ; ; ; ; = = = = = =              G F F F F F F F & & & & & & 1 1 1 1 1 1 > > > > > > > > - - - - - - ' ' ' ' ' ' 
 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 <;;;;;        =<<<<<222222   	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     %'  & & & &G  Q V.VVV g!! 46  5 5 5 5
 .E[-DU-K-K  K K K K	!!    ty   %J %J %J %J %J %J %J %JP
 
 
 
 
           ?C  B B B B* * * *$ $ $ $           > !       / / / /   $ $ $ $    49   S# S# S# S# S# S# S# S#l $IO%%   !%"',0.2&*"&!/3NQ NQ NQ NQ NQb 
 L L L L 
 L 
,1) ) ) ) 
) */$ $ $ $ $N 8 8 8 8
 
u{3	5	u}ej1	5>+	u{n	uzm	u{EJ/	Oek59-	U[1	uzm        ( #:"9";"; " " " ", , , ,
 
 
 
	 	 	 	? ? ? ?
G G G G         	 	 	 	       B 
 A A A 
 A 
   

 
 C C C 
 C& & & & w 	%!G G G G   &    
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   v" v" v" v" v" v" v" v"r %' ! ?Pk>O,? ? ?     
- - - -` 3  3  3  3F- - - -`M3 M3 M3 M3b ">*DEEE 009STTT % % % %! ! ! !
& & & &s s s s s s s sl	 59  8 8 8 8   * * * *
  %*"'' ' ' ' '@ ( ( ( ( ( ( ( ((         +       ).**     8<I I I I I IX 
   

 
 = = = 
 =   &
 
 
 
 9 9 9 9"   *    $   ( ( ( (
   < Q1 1 1 12   . =>    &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GCLLGCLL   G G G G   1 1 1 1   
: : : : 	       @ @ @ @   
   8 8 8 8(   M M M M
    #(	& & & & &RM M M M@ @ @ @   B       
 !  = < < < < <7 7 7 7    <A( ( ( ( ( ($
 
 
 
"? ? ? ?; ; ; ;  !&%(Y5:!&mF mF mF mF mF`	H H H H
 
= 
= 
= 
= '&(( ;R+:QRV:W:W W W W W LN  M M M M '&(( , , , ,& & & &
    $   
/ / / /4 "'I I I I IX  	!!1 1 1 1 " " " "L L L L^$ $ $ $N
 
 
 
*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
, , , ,$ $ $ $   
 
 
 
"               
 
 
 
  DF  E E E E   6 6 6 6
# # # # # # # #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