
    IЦi                   t   % S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKJ r   S SKJ!r!  S SK
J"r"  S SKJ#r#  S SKJ$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9  S SK:J;r;J<r<  S SK=r=S SK>r=S SK?r=S SK@JAs  JBrC  S S	K=JDrD  S S
KEJFrFJGrGJHrHJIrI  S SKJJKrK  S SKLJMrM  S SKNJOrOJPrP  S SKQJRrR  S SKSJTrTJUrUJVrVJWrW  S SKXJYrYJZrZ  S SK[J\r\  S SK]J^r^  S SK_J`r`Jara  S SKbJcrc   S SKdre S SKgr=S SKhJirj  S SKNJkrk  S SKgJlrl  SSKmJnrn  \e(       ap  \e\eR                  \eR                  \eR                  4rrS\sS'   \e\j\eR                  \jR                  \eR                  \jR                  \eR                  \jR                  0rtOSrr0 rtS SKuJvrvJwrwJxrx  \7" S5      rz\=R                  R                  R                  r~\=R                  R                  R                  r\=R                  R                  GR                  r\GR                  " \RP                  5      rS\sS'   0 rS \sS!'   S"rS#rS$\ S%3r\GR                  " \5      r0 rS&\sS''   \GR                  " \5      rS(\sS)'   \GR                  " 5       r " S* S+\	GR"                  5      r " S, S-5      r      GSS. jrS qGSS/ jrGSS0 jrS qGSS1 jrGSS2 jrGSS3 jrS4\sS5'   GSS6 jr\      GS             GSS8 jj5       r\3GSGSS9 jj5       r\3 GS     GSS: jj5       rGS GS!S; jjr\GR>                  GSS< j5       r\=GRB                  \=GRD                  \=GR                  4\=GRF                  \=GRH                  \=GRJ                  4\=GRL                  \=GRN                  \=GRP                  4\=GRR                  \=GRT                  4\=GRV                  \=GRX                  4\=GRZ                  \=GR\                  4\=GR^                  \=GR`                  \=GRb                  4\=GRd                  \=GRf                  \=GRh                  4\=GRj                  \=GRl                  \=GRn                  4\=GRp                  \=GRr                  40
r " S= S>5      r\" 5       rS? rGSS@ jrSA rSB rGS"SC jrGS#SD jrGS$SE jrGS%SF jrSG rSH r " SI SJ5      r\3GS&SK j5       r\3      GS'SL j5       r\3GS(SM j5       rSN r\GR                  SO:  aI  \GR                  \GR                  \GR                  \Rn                  \GR                  \GR                  4rSP rSQ rSR rSS rST rSU rSV rSW rSX rSY rSZ rS[ r\
RD                  " S\5      S] 5       rGS)S^ jrS_ rGS*S` jrGS*Sa jrSbr\GR                   " Sc Sd5      5       rSer\GR                  " \Sf9qSg\sSh'   GS+Si jrGS,Sj jr          GS-Sk jr\M" \Sl9rGS.Sm jrGSSn jrGS/So jr " Sp Sq5      rSqSr\sSs'   GS0St jr\   GS1       GS2Su jj5       r\GR                   " Sv Sw5      5       r " Sx Sy\5      r\" 5       \l        Sz rSS{.S| jrS} rGS3S~ jr\ S 5       rS rGSS jrS rS rS r\
RD                  " S5      GS4S j5       rS Gr GS5S jGrS Gr\"" S5      S 5       Gr\\G\\G\G\G\" S5      G\GR                  G\
GR                  \GR                  \=GR                  \=GR                  \=GR                  \=GR                  \=GR                   \=GR"                  \=R                  GR$                  GR&                  \=GR(                  GR*                  1GrS\sS'   \a" 5       (       a0  S SGKGrG\GR1                  G\GR2                  GR                  5         S GrS GrS GrS GrS GrS GrS Gr G\" 0 GRC                  5       5      Gr"S\sS'   G\" 0 GRG                  5       5      Gr$S\sS'   G\" \GRJ                  " 5       GRG                  5       5      Gr&S\sS'   G\" G\'" S5      5      Gr(S\sS'   G\" G\'" G\)" S 5      5      5      Gr*S\sS'   G\(GRV                  Gr,G\-GR\                  Gr/S Gr0S Gr1S Gr2G\3Gr4GS6S jGr5S Gr6S Gr7S Gr8S Gr9S Gr:GSS jGr;S Gr<S Gr=GS"S jGr>GS"S jGr?SGr@S SGKAGJBGrB  S GrCS GrDS GrES GrFSS7SS7SS7S7\GR                  S74	S jGrHS GrI\R@                  S 5       GrJ\" 5       GrK\GR                  " G\L5      GrMS\sS'   / GrNS\sS'   \" 5       GrO\
RD                  " S5      S 5       GrPS GrQGS7S jGrRS GrSS GrTGSS jGrU\GR                  " 5       GrWS GrX\ S 5       GrYS GrZS Gr[S Gr\GS8S jGr]S Gr^GS9S jGr_GS:S jGr`GS;S jGraGSGS<S jjGrb " S S\	GR"                  5      GrcGS=S jGrd        GS>S jGreS GrfS GrgSS/GrhSS/Gri/ SQGrjG\hG\i-   G\j-   GrkS Grl   GS?S jGrm   GS?S jGrnS GroS Grp " S S5      GrqS Grr " S S5      Grs " S S5      GrtS GruS GrvS GrwS GrxS GryS GrzGS@S jGr{\GR                   " S S5      5       Gr|GSAS jGr}GSBS jGr~S GrS GrS GrS GrGSCS jGrS GrS GrS Gr\R@                  GSDS j5       GrS GrS Gr " S S\=R                  R                  5      GrGSES jGrS GrS Gr " S S5      GrG\" 5       GrS\sS'   GS5GS  jGrGS Gr\R@                  GS 5       GrGS GrGS GrGS GrGS GrGS GrGS GrGS	 GrGS
 GrGS GrGS GrGS GrGS Gr0 GrGS\sGS'   GS GrGS Gr " GS GS5      GrGSFGS jGrg! \f a    Sre G
Nf = f! \y a     G
NPf = f(G      )annotationsN)contextmanager)is_dataclass)	lru_cache)MethodWrapperType)AnyCallablecastClassVarCounterDefaultDictDequeDict	GeneratorGenericIterableIteratorKeysViewListOptionaloverloadSetTupleTypeTypeVarUnion
ValuesView)LiteralTypeIs)fx)_instruction_counter_len_torch_function_stack_pop_torch_function_stack_push_on_torch_function_stack)enable_python_dispatcher)MetricsContext)SourceTracingContext)is_sparse_compressed)log_chromium_event_internallog_compilation_eventrecord_chromium_event_internalsignpost_event)_format_graph_codelazy_format_graph_code)_WaitCounter)LazyModuleMixin)
has_tritonhas_triton_package)RemovableHandle)detect_fake_mode
LazyString   )configzTuple[types.ModuleType, ...]NP_SUPPORTED_MODULES )
FakeTensoris_fakemaybe_get_fake_modeTzDefaultDict[str, Counter[str]]countersDict[str, Any]optimus_scuba_logzAhttps://pytorch.org/docs/main/torch.compiler_troubleshooting.htmlz;https://pytorch.org/docs/main/torch.compiler_nn_module.htmlzSee z& for more information and limitations.zDict[str, List[float]]compilation_time_metricsDict[str, float]cumulative_time_spent_nsc                       \ rS rSrSrSrSrSrg)ReInplaceTrigger   r8         r;   N)__name__
__module____qualname____firstlineno__AUTO_FUNC_V1AUTO_FUNC_V2
TRITON_OPS__static_attributes__r;       R/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_dynamo/utils.pyrG   rG      s    LLJrS   rG   c                      \ rS rSr% \R
                  " \5      rS\S'   \	SS j5       r
\	SS j5       r\	S 5       r\	S 5       r\	S 5       r\	S	 5       rS
rg)ReinplaceCounters   zDefaultDict[str, int]_valuesc                Z    US:w  a%  U R                   SUR                   3==   U-  ss'   g g Nr   missed_bytes_)rX   name)clstriggerbytess      rT   add_missed_bytes"ReinplaceCounters.add_missed_bytes   s-    A:KK-~675@7 rS   c                F    US:w  a  U R                   SU 3==   U-  ss'   g g Nr   missed_tensors_)rX   )r]   r^   counts      rT   add_missed_opportunities*ReinplaceCounters.add_missed_opportunities   s)    A:KK/'34=4 rS   c                8    U R                   R                  5         g N)rX   clearr]   s    rT   rj   ReinplaceCounters.clear   s    rS   c                f    Sn[          H$  nXR                  R                  SU 3S5      -  nM&     U$ rc   )rG   rX   getr]   sumr^   s      rT   get_total_missed"ReinplaceCounters.get_total_missed   s4    'G;;??_WI#>BBC (
rS   c                z    Sn[          H.  nXR                  R                  SUR                   3S5      -  nM0     U$ rZ   )rG   rX   rn   r\   ro   s      rT   get_total_missed_bytes(ReinplaceCounters.get_total_missed_bytes   s8    'G;;??]7<<.#A1EEC (
rS   c                V    U R                   (       a  [        SSU R                   5        g g )Ninductorreinplace_counters)rX   r-   rk   s    rT   logReinplaceCounters.log   s!     ;;:';S[[I rS   r;   N)r^   rG   r_   int)r^   rG   re   r{   )rK   rL   rM   rN   collectionsdefaultdictr{   rX   __annotations__classmethodr`   rf   rj   rq   rt   ry   rR   r;   rS   rT   rV   rV      s    %0%<%<S%AG"A A A
 > >       J JrS   rV   c                     SS K nUR                  XS9$ ! [         a1    SR                  S [        R                  " U/U 5       5       5      s $ f = f)Nr   headers
c              3  `   #    U  H$  nS R                  [        [        U5      5      v   M&     g7f), N)joinmapstr).0rows     rT   	<genexpr>tabulate.<locals>.<genexpr>   s&      
0PDIIc#sm$$0Ps   ,.)tabulateImportErrorr   	itertoolschain)rowsr   r   s      rT   r   r      sX    
   77 
yy 
09	40P
 
 	

s    8AAc                     [         S-   q g Nr8   )
curr_framer;   rS   rT   increment_framer      s    aJrS   c                 X    [         R                  5         [        R                  5         Sqg Nr   )rE   rj   rC   r   r;   rS   rT   reset_frame_countr      s    ""$""$JrS   c                    [         U -  q g ri   )op_count)cnts    rT   increment_op_countr      s    OHrS   c                     0 n [         R                  5        H  u  pUS-  X'   M     U R                  SS5      U R                  SS5      -   U S'   U $ )N    eAentire_frame_compiler   entire_backward_compiletotal_wall_time)rE   itemsrn   )total_by_keyphasetimings      rT   calculate_time_spentr      sc    L1779$sl : '3&6&6'2A6'7L"# rS   c            	         [        5       n SnU R                  5        H  u  p#U SU S[        US5       3nM     [        U5        g )NzTIMING: :   )r   r   roundprint)r   outkeyvalues       rT   print_time_reportr     sJ    ')L
C"((*
Qse1U5!_-. + 
#JrS   r&   _METRICS_CONTEXTc                     [         $ ri   )r   r;   rS   rT   get_metrics_contextr   %  s    rS   Fc           	   #    #    Ub  UR                  S5      (       d   eU(       a  UnU nOU nSnU [        ;  a	  / [        U '   0 nU(       a  UR                  U5        U(       a  UR                  SU05        [        5       n	[        R
                  " 5       n
U	R                  XjX5         [        R                  R                  U  S35         U(       a*  [        SU  35      R                  5          Sv   SSS5        OSv   SSS5        [        R
                  " 5       nX-
  n[        U    R                  US-  5        U	R                  Xk0 X5        U(       aE  [        5       nUR                  5       (       a  UR!                  XLS-  5        ["        U==   U-  ss'   gg! , (       d  f       N= f! , (       d  f       N= f! [        R
                  " 5       nX-
  n[        U    R                  US-  5        U	R                  Xk0 X5        U(       aE  [        5       nUR                  5       (       a  UR!                  XLS-  5        ["        U==   U-  ss'   f f = f7f)a  
dynamo_timed is a context manager
By wrapping a function in dynamo_timed, we can get a few things:

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

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

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

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

Params:
- key: key into compile_time_metrics. If phase_name is not provided, this is
  also the event name used for pt2_compile_events logs and chromium events.
- phase_name: Optional override for the event name.
- log_pt2_compile_event: Whether to log a pt2 compile event internally.
- metadata: Extra metadata to put in pt2_compile_events.
- dynamo_compile_column_us: If provided, updates the specified CompilationMetrics
  field to be logged to dyname_compile column. We expect all columns to be _us;
  therefore, the field name must end with "_us".
- log_waitcounter: If set, we'll log a waitcounter of the form "pytorch.dynamo_timed.{key}"
N_usfn_namez (dynamo_timed)zpytorch.dynamo_timed.r     )endswithrC   updateget_chromium_event_loggertimetime_nslog_event_starttorchprofilerrecord_functionr0   guardappendlog_event_endr   in_progress	incrementrE   )r   
phase_namelog_pt2_compile_eventmetadatadynamo_compile_column_uslog_waitcounter
event_namer   event_metadatachromium_logstart_nsend_nstime_spent_nsmetrics_contexts                 rT   dynamo_timedr   )  s    Z  +'007777


**(* %Nh'y'23(A(CL||~H  nB^^++se?,CD!$9#"?@FFH IH  E ) %,,]S-@A""H	
 $13O**,,)),t.C %Z0MA0 $ IH ED ) %,,]S-@A""H	
 $13O**,,)),t.C %Z0MA0 $sP   B$I'"F= 	$F,-F2F,?F= BI
F)	%F,,
F:6F= =BIIc                    g ri   r;   repr	aggregates     rT   compile_timesr         rS   c                    g ri   r;   r   s     rT   r   r          rS   c           	     D  ^ S 4U4S jjnU S:X  a5  [          Vs/ s H  nX2" [         U   S S94PM     nnSnU[        USS9-  nU$ U S	:X  aJ  [         R                  5        Vs/ s H  nU" US
 S9PM     nn[        [         R	                  5       5      nX4$ gs  snf s  snf )a  
Get metrics about torchdynamo frontend/backend compilation times.

Accumulates information from functions tagged with `dynamo_timed`.

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

aggregate causes values from multiple compilations (e.g. split graphs)
to be accumulated into one value.  If false, expect more than one value
per metric.
c                    U $ ri   r;   xs    rT   <lambda>compile_times.<locals>.<lambda>  s    rS   c                h   > T(       a  U" [        U 5      5      $ SR                  [        X5      5      $ )Nr   )rp   r   r   )valuesitem_fnr   s     rT   fmt_fncompile_times.<locals>.fmt_fn  s)    3v;''yyW-..rS   r   c                
    U S $ )Nz.4fr;   r   s    rT   r   r     s
    3jrS   )r   z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                
    U S $ )Nz.6fr;   r   s    rT   r   r     s
    1S'
rS   N)rC   r   r   listkeys)	r   r   r   kr   r   vr   r   s	    `       rT   r   r     s      + /
 u} .
- /2<PQR- 	 
 3x&BCC
	 .446
6 1236 	 
 /4467

s   B#Bc                 >    [         R                  [        SSS95        g )Nr   Tr   )ry   infor   r;   rS   rT   dump_compile_timesr     s    HH]67rS   c                  2    \ rS rSrSSS jjrS rS	S jrSrg)
DuplicateWarningCheckeri  c                0    Xl         U R                  5         g ri   )maxsizereset)selfr   s     rT   __init__ DuplicateWarningChecker.__init__  s    

rS   c                8    [         R                  " 5       U l        g ri   )r|   OrderedDictsetr   s    rT   r   DuplicateWarningChecker.reset  s    **,rS   c                f   XR                   ;   a2  U R                   R                  USS9  [        R                  (       d  g gS U R                   U'   [	        U R                   5      U R
                  :  a>  U R                   R                  SS9  [	        U R                   5      U R
                  :  a  M>  g)NT)lastF)r   move_to_endr9   verboselenr   popitemr   r   s     rT   addDuplicateWarningChecker.add  s    ((?HH  4 0>> "  !DHHSMdhh-$,,.  e , dhh-$,,.rS   )r   r   N)   )r   r{   returnNone)r   z!Union[str, Tuple[object, object]]r  bool)rK   rL   rM   rN   r   r   r  rR   r;   rS   rT   r   r     s    -	rS   r   c                     [         R                  R                  SS5      S:H  n U (       a
  [        5       $ [        R
                  " 5       $ )NTORCH_COMPILE_DEBUG01)osenvironrn   add_file_handler
contextlib	ExitStack)compile_debugs    rT   setup_compile_debugr    s7    JJNN#8#>#EM!!!!rS   c                 ,    [         R                  5         g ri   )graph_break_dup_warning_checkerr   r;   rS   rT   reset_graph_break_dup_checkerr    s    #))+rS   c                   ^^ [         R                  R                  [        5       S5      n [         R                  " U SS9  [
        R                  " [         R                  R                  U S5      5      m[
        R                  " S5      mTR                  T5        [        R                  " 5       nUR                  UU4S j5        U$ )NtorchdynamoTexist_okz	debug.logztorch._dynamoc                 &   > TR                  T 5      $ ri   removeHandlerlog_file_handlerloggers   rT   r   "add_file_handler.<locals>.<lambda>  s    v334DErS   )r  pathr   get_debug_dirmakedirsloggingFileHandler	getLogger
addHandlerr  r  callback)log_path	exitstackr!  r"  s     @@rT   r  r    s    ww||MO];HKK4(**277<<++NO/F
&'$$&IEFrS   c                 N  ^^ [         R                  " 5       n [        R                  b|  [        R
                  " [        R                  5      m[        R                  R                  R                  5        H*  mTR                  T5        U R                  UU4S j5        M,     U $ U $ )Nc                 &   > TR                  T 5      $ ri   r  r   s   rT   r    setup_log_file.<locals>.<lambda>  s    v';';<L'MrS   )r  r  r9   log_file_namer'  r(  r   _logging	_internalget_loggersr*  r+  )r-  r!  r"  s    @@rT   setup_log_filer5     s}    $$&I'"..v/C/CDnn..::<F./MN = rS   c                |    [        5        SUR                   S[        U 5      R                   SUR                   S3$ )Nz/error_recordings/_z.rec)r%  co_nametyperK   co_firstlineno)exccodes     rT   gen_record_file_namer=    sB    o aS	""#1T%8%8$9? ?rS   c                    [         R                  R                  U 5      (       a  [        R	                  SU 5        g [         R
                  " [         R                  R                  U 5      SS9  [        U S5       nUR                  U5        S S S 5        g ! , (       d  f       g = f! [         a    [        R                  SU 5         g f = f)Nz9Unable to write execution record %s; file already exists.Tr  wbz#Unable to write execution record %s)r  r$  existsry   warningr&  dirnameopendump	Exception	exception)filenameexec_recordfs      rT   write_record_to_filerJ    s    
G77>>(##KKKX KK1DAh%  # &%% G;XFGs4   :B' >B' ;BB' 
B$ B' $B' ' C
	C
c                ^    SnU R                    H  nSUR                  ;   d  M  US-  nM     U$ )Nr   callr8   )nodesop)gcns      rT   count_callsrR    s0    	AWWQTT>FA  HrS   c                    U $ ri   r;   r   s    rT   identityrT  '  s    HrS   c                R     [        U 5        g! [         a     g[         a     gf = f)NTF)hash	TypeError
ValueErrorr   s    rT   hashablerY  +  s.    Q  s    
&	&&c                     g ri   r;   argskwargss     rT   nothingr^  6  s    rS   c                  F    \ rS rSrSrS rS rSS jrS rS r	S	 r
S
 rSrg)ExactWeakKeyDictionaryi:  z\Similar to weakref.WeakKeyDictionary, but use `is`/`id` rather than `==` to compare equalityc                     0 U l         0 U l        g ri   r   refsr   s    rT   r   ExactWeakKeyDictionary.__init__=  s    	rS   c                2    U R                   [        U5         $ ri   )r   idr  s     rT   __getitem__"ExactWeakKeyDictionary.__getitem__A  s    {{2c7##rS   Nc                L    U R                   R                  [        U5      U5      $ ri   )r   rn   rf  )r   r   defaults      rT   rn   ExactWeakKeyDictionary.getD  s    {{r#w00rS   c                2    [        U5      U R                  ;   $ ri   )rf  r   r  s     rT   __contains__#ExactWeakKeyDictionary.__contains__G  s    #w$++%%rS   c                   ^ ^ [        U5      mTT R                  ;  a)  [        R                  " UUU 4S j5      T R                  T'   UT R                  T'   g )Nc                &   > TR                  T5      $ ri   )
_remove_id)refidxr   s    rT   r   4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>M  s    $//#:NrS   )rf  rc  weakrefrr  r   )r   r   r   rs  s   `  @rT   __setitem__"ExactWeakKeyDictionary.__setitem__J  s?    gdii$[[.NODIIcN CrS   c                v    XR                   ;   a  U R                   U	 XR                  ;   a  U R                  U	 g g ri   rb  )r   rs  s     rT   rq  !ExactWeakKeyDictionary._remove_idP  s2    ++C ))		# rS   c                l    U R                   R                  5         U R                  R                  5         g ri   )rc  rj   r   r   s    rT   rj   ExactWeakKeyDictionary.clearV  s     		rS   )rc  r   ri   )rK   rL   rM   rN   __doc__r   rg  rn   rm  rv  rq  rj   rR   r;   rS   rT   r`  r`  :  s(    f$1&!rS   r`  c                    g ri   r;   objallowed_typess     rT   istyper  [  r   rS   c                    g ri   r;   r~  s     rT   r  r  `  r   rS   c                    g ri   r;   r~  s     rT   r  r  g  r   rS   c                x    [        U[        [        [        45      (       a  [	        U 5      U;   $ [	        U 5      UL $ )zisinstance() without subclasses)
isinstancetupler   r   r9  r~  s     rT   r  r  l  s4    -%s!344CyM))9%%rS   rJ      c                    [         R                  S:  a  [        U [        5      (       a  g[        U [        R
                  5      =(       d    U [        R                  L $ )Nr  T)sysversion_infor  _builtin_final_typing_classestyping_Finalr   r   s    rT   	is_typingr    sA     7"z%9V'W'WeV]]+Fu/FFrS   c                "   [         (       d  g[        U [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  [         R                  [         R                  45      $ NF)
npr  int8int16int32int64uint8uint16uint32uint64r  s    rT   is_numpy_int_typer    sW    2GGHHHHHHHHIIIIII		
 rS   c                    [         (       d  g[        U [         R                  [         R                  [         R                  45      $ r  )r  r  float16float32float64r  s    rT   is_numpy_float_typer    s4    2JJJJJJ	
 rS   c                    [        U [        R                  5      =(       a     [        [        R
                  " U S5      5      $ )N__wrapped__)r  	functools_lru_cache_wrapperis_functioninspectgetattr_staticr  s    rT   is_lru_cache_wrapped_functionr    s4    eY99: {um4@ rS   c                    [        U 5      =(       d>    [        U [        R                  R                  [        R                  R
                  45      $ ri   )r  r  r   _opsOpOverloadPacket
OpOverloadr  s    rT   is_function_or_wrapperr    s9    u 

++UZZ-B-BC" rS   c                    [        U [        R                  [        R                  [        R                  [        R
                  45      $ ri   )r  typesFunctionTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper  s    rT   r  r    s;    %%&&''		
 rS   c                    [        U [        R                  [        R                  [        R                  [        R
                  [        R                  45      $ ri   )r  r  GetSetDescriptorTyper  r  MemberDescriptorTyper   r  s    rT   is_wrapper_or_member_descriptorr    sF     &&&&''&&##	
 rS   c                    [        U 5      S   $ r   ) unwrap_with_attr_name_if_wrapper)fns    rT   unwrap_if_wrapperr    s    +B/22rS   c                    [        U 5      (       a:  [        R                  " U SS5      (       a  [        R                  " U SU 5      n SnX4$ S nX4$ )N_torchdynamo_inlineF)r  r  r  )r  	attr_names     rT   r  r    sU     2711"6KUSS##B(=rB)	 = 	=rS   c                N    [         (       d  g[        U [         R                  5      $ r  )r  r  ndarrayr  s    rT   is_numpy_ndarrayr    s    2%$$rS   c                    [         R                  [         R                  R                  /[        R
                  Q7nU[         R                  R                  4-   n[        X5      $ )zCheck of obj is a tensor)	r   Tensornn	Parameterr9   traceable_tensor_subclasses_subclassesr<   r  )r  tensor_lists     rT   istensorr    sU     	% 
	+	+%K
 !2!2!=!= ??K###rS   c                "    [        U [        5      $ ri   )r  r1   )mods    rT   is_lazy_moduler    s    c?++rS   r  c                     [        U 6   g ri   )r   r\  s    rT   
print_oncer    s	    	4LrS   c                   ^ U mU4S jnUR                   b  [        UR                   5      S:X  d   eUR                   S   $ )zNSome black magic to create a cell object that usually only exists in a closurec                    > T $ ri   r;   r   s   rT   rI  make_cell.<locals>.f  s    rS   r8   r   )__closure__r  )valrI  r   s     @rT   	make_cellr    s>    A ==$Q]]);q)@@@==rS   c                8    [        S U  5       5      nUR                  5        VVs0 s H  u  p4X4R                  5       _M     nnnX%4$ s  snnf ! [         a@  nSSKJn  SSKJn  U" SU" U 6  SU" [        UR                  5       5      6  3US9   S nAg S nAff = f)Nc              3  @   #    U  H  oR                  5       v   M     g 7fri   )as_proxyr   args     rT   r   $proxy_args_kwargs.<locals>.<genexpr>  s     :Tc<<>>T   r8   unimplemented)typestrzcall_function args: r   from_exc)
r  r   r  NotImplementedErrorr;  r  variables.baser  r   r   )	r\  r]  
proxy_argsr   r  proxy_kwargser  r  s	            rT   proxy_args_kwargsr    s    
:T::
<BLLNKN\\^+NK'' L 
&+"7D>"2!GT&--/=R4S3TU	
	
s(   %A A	A 	A 
B6BBc                (    U c  S $ [        U S-  5      $ Nr   r{   r   s    rT   	to_int_msr    s    94/#a$h-/rS   c                (    U c  S $ [        U S-  5      $ )Ni@B r  r  s    rT   	to_int_usr  #  s    944#a)m"44rS   rJ   c                     \ rS rSr% SrS\S'   SrS\S'   SrS\S'   SrS\S'   Sr	S\S	'   Sr
S\S
'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS \S!'   SrS\S"'   Sr S\S#'   Sr!S \S$'   Sr"S \S%'   Sr#S \S&'   Sr$S\S''   Sr%S \S('   Sr&S\S)'   Sr'S\S*'   Sr(S\S+'   Sr)S\S,'   Sr*S\S-'   Sr+S\S.'   Sr,S\S/'   Sr-S\S0'   Sr.S\S1'   Sr/S\S2'   Sr0S\S3'   Sr1S\S4'   Sr2S\S5'   Sr3S\S6'   Sr4S\S7'   Sr5S\S8'   Sr6S\S9'   Sr7S\S:'   Sr8S\S;'   Sr9S\S<'   Sr:S\S='   Sr;S\S>'   Sr<S\S?'   \=r>S@\SA'   Sr?S\SB'   Sr@S\SC'   SrAS\SD'   SrBS\SE'   SrCS\SF'   SrDS\SG'   SrES\SH'   SrFS\SI'   SrGS\SJ'   SrHSK\SL'   SMrIg)NCompilationMetricsi,  NOptional[str]
compile_id	frame_keyr8  co_filenameOptional[int]r:  
cache_sizeaccumulated_cache_sizeguard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_countOptional[float]
start_timeentire_frame_compile_time_sbackend_compile_time_sinductor_compile_time_scode_gen_time_s	fail_typefail_reasonfail_user_frame_filenamefail_user_frame_linenozOptional[Set[str]]non_compliant_opscompliant_custom_opsrestart_reasonsdynamo_time_before_restart_szOptional[bool]has_guarded_coderemote_cache_time_saved_sstructured_logging_overhead_sconfig_suppress_errors config_inline_inbuilt_nn_modulesspecialize_floatdynamo_config
is_forwardnum_triton_bundles!remote_fx_graph_cache_get_time_ms!remote_fx_graph_cache_put_time_msstart_time_usduration_us!dynamo_cumulative_compile_time_us'aot_autograd_cumulative_compile_time_us#inductor_cumulative_compile_time_us,inductor_code_gen_cumulative_compile_time_ustriton_compile_time_usruntime_cudagraphify_time_usruntime_triton_autotune_time_us%dynamo_compile_time_before_restart_uscuda_synchronize_time_us distributed_ephemeral_timeout_usstructured_logging_overhead_us!remote_fx_graph_cache_get_time_us!remote_fx_graph_cache_put_time_us#backward_cumulative_compile_time_usend_time_uspre_grad_pass_time_uspost_grad_pass_time_usjoint_graph_pass_time_usr{   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_usager;   )JrK   rL   rM   rN   r  r~   r  r8  r  r:  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  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/  r0  r1  r2  r3  r4  r5  rR   r;   rS   rT   r  r  ,  s    $J$#I}#!G]!!%K%$(NM( $J$,0M0!%K%+/=/$(NM(&*m*'+}+"&J&377.2O2/3_3'+O_+#I}#!%K%.2m2,0M0,0)0/3,3*.O'.48 /8 (,n+15559!?9-1N17;$n;'+n+#'M='!%J%(,,7;%};7;%};#'M='!%K%7;%};=A+]A9='=BF0-F,0M026 -659#]9;?)=?.2m26:$m:48"M87;%};7;%};9='=!%K%+/=/,0M0.2m200%)O])*.-.8<&<9='=;?)=?7;%};8<&<"&L-&$(NM(/3M,3rS   r  @   maxlenzDeque[CompilationMetrics]_compilation_metricsc                   [        5       nUR                  5       nU(       d  g UR                  UU R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                   U R"                  U R$                  b  ['        U R$                  5      OS U R(                  b  ['        U R(                  5      OS U R*                  b  ['        U R*                  5      OS U R,                  U R.                  U R0                  S9  g )N)r   r  r8  r  r:  r  r  r  r  r  r  r  r  r  r  r  r  r	  r
  r  r  r  )r   get_topadd_event_datar  r8  r  r:  r  r  r  r  r  r  r  r  r  r  r  r  r   r	  r
  r  r  r  )rP  event_loggerr   s      rT   #add_compilation_metrics_to_chromiumr?  ~  s%   ,.L%%'J++		MM''<< 77MM55''++--++MM!"!;!; 77 * q223!!- "!"8"89( Q../%&%C%C++oo;   rS   c                 T    " S S[         R                  5      n Sn[        5       nSn[        R                  R
                  (       a(  [        R                  R
                  R                  5       OSnUb   UR                  5        Hj  u  pV[        U[        5      (       a  [        R                  " X5      (       a  UR                  U5        [        U[        5      (       d  M]  [        U5      XE'   Ml     U H  nXE	 M     [         R                  " X@SS9nU$ U$ ! [         a    Sn U$ f = f)zF
Method to parse and scrub uninteresting configs from inductor config
c                  (   ^  \ rS rSrU 4S jrSrU =r$ )A_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializeri  c                D   >  [         TU ]  U5      $ ! [         a     gf = f)NzValue is not JSON serializable)superrj  rE  )r   o	__class__s     rT   rj  I_scrubbed_inductor_config_for_logging.<locals>.TypeSafeSerializer.default  s)    8wq)) 878s    
r;   )rK   rL   rM   rN   rj  rR   __classcell__rF  s   @rT   TypeSafeSerializerrB    s    	8 	8rS   rJ  zV((^TYPE_CHECKING$)|(.*_progress$)|(.*TESTING.*)|(.*(rocm|halide).*)|(^trace\..*)|(^_))NT)r]   skipkeysz(Inductor Config is not JSON serializable)jsonJSONEncoderr   r   	_inductorr9   get_config_copyr   r  r   researchr  r   dumpsrE  )rJ  configs_to_scrub_rekeys_to_scrubinductor_conf_strinductor_config_copyr   r  s          rT   %_scrubbed_inductor_config_for_loggingrW    s   8T-- 8 tEM49OO4J4J..0PT  '	K0668!#s++ryy9L/R/R!%%c*c3''04S	(- 9 %(- % !%

$t!   	K J	Ks   5A)D "1D D'&D'c                j  ^^ U4S jnU4S jnSU4S jjn[         R                  R                  5       n[         R                  R                  R                  5       (       a1   SSKJn	Jn
  U
nU	R                  5       n[        U5      R                  nOS nS n[        [         R                  R                  R!                  5       5      U S-  US-  X-
  S-  U(       a  UR"                  OS U(       a  [        U5      OS [%        U5      ['        5       [         R(                  R*                  [-        5       (       a  [.        R0                  OSU" S5      U" S	5      UUS
.nU S-  U" S5      U" S5      U" S5      U" S5      U" S5      U" S5      U" S5      US.	n[3        S0 0 UEUETED6m[4        R7                  T5        TR8                  (       a  SnOSn[         R                  R;                  UU4S jSS9  [=        T5        [>        R@                  (       a  [C        T5        g g ! [         a    S nS n GNf = f)Nc                <   > TR                  U S 5      nUb  US-  $ S $ )Ng    .Arn   fieldmetricmetricss     rT   us_to_s+record_compilation_metrics.<locals>.us_to_s  s'    UD)%1v|;t;rS   c                <   > TR                  U S 5      nUb  US-  $ S $ r  rZ  r[  s     rT   us_to_ms,record_compilation_metrics.<locals>.us_to_ms  s'    UD)!'!3v~==rS   c                   >^ SS jmTR                  U S 5      nUc  g TU 	 [        U[        5      (       d  [        U[        5      (       d  g SR	                  U4S jU 5       5      $ )Nc                P     [        U 5      $ ! [         a    [        S 5      s $ f = fri   )r   rE  )items    rT   safe_strPrecord_compilation_metrics.<locals>._convert_collection_to_str.<locals>.safe_str  s*    !4y  !4y !s   
 %%,c              3  4   >#    U  H  nT" U5      v   M     g 7fri   r;   )r   rf  rg  s     rT   r   Qrecord_compilation_metrics.<locals>._convert_collection_to_str.<locals>.<genexpr>  s     :646   )rf  r   r  r   )rn   r  r   r   r   )r\  r]  rg  r^  s     @rT   _convert_collection_to_str>record_compilation_metrics.<locals>._convert_collection_to_str  s\    	! UD)> EN&#&&z&$/G/Gxx:6:::rS   r   )FbRemoteFxGraphCacheREMOTE_CACHE_VERSIONr    r1  r2  )r  r  r'  r  r  r  r#  r,  r3  r4  r1  r2  r-  r0  r   r  r  r  r  r"  r$  r%  )	r  r   r  r  r  r  r  r  r  compilation_metricsbwd_compilation_metricsc            	        > [         R                  " T5      R                  5        V Vs0 s H(  u  pU [        U[        5      (       a  [        U5      OU_M*     snn $ s  snn f ri   )dataclassesasdictr   r  r   r   )r   r   rr  s     rT   r   ,record_compilation_metrics.<locals>.<lambda>2  sQ    #**+>?EEG
G *Q,,tAw!3G
 
s   /AF)record_logging_overhead)r\  r   r  r  r;   )"r   r2  get_structured_logging_overheadrN  utils should_use_remote_fx_graph_cachetorch._inductor.fb.remote_cachero  rp  get_remote_backendr9  rK   ModuleNotFoundErrorr   _guardsCompileContextcurrent_compile_idrM   r  rW  versioncudar2   triton__version__r  r:  r   r  trace_structuredr?  r9   log_compilation_metricsr+   )start_time_nsend_time_nsr^  exc_type	exc_valuer_  rb  rm  r  ro  rp  r-  backendr0  common_metricslegacy_metricsr\   rr  s     `              @rT   record_compilation_metricsr    s-   <>;" %*NN$R$R$T!==??	9
 $8 *==?G48M4J4J1
 15-# %--66IIKL&$."d*#3<.6X**D)2s9~*34Q*R@B**0:&,,"-G/.
 /I0/
 !51V%N0 $c)'./R'S")*S"T#*+P#Q"#QR%,-O%P-5/.
 .6/.
 *GN  - 
9^
9~
9
9  34%%$(	NN##	
 !& $  ((;< %%12 &Q # 	9#' 481	9s   #/H   H21H2)on_exitc                    [        [        5      U :  a)  [        R                  5         [        [        5      U :  a  M)  [        R                  " [        U S9nUqg )Nr8  )r  r:  popleftr|   deque)new_size	new_deques     rT   set_compilation_metrics_limitr  I  sE    
"
#h
.$$& "
#h
.!!"6xHI$rS   c                 ,    [         R                  5         g ri   )r:  rj   r;   rS   rT   clear_compilation_metricsr  Q  s     rS   c                      [        [        5      $ ri   )r   r:  r;   rS   rT   get_compilation_metricsr  V  s    $%%rS   c                      \ rS rSrSrSS jrSS jrS rSS jrS r	SS jr
    SS	 jr S         SS
 jjrSS jr            SS jr S         SS jjr  S         SS jjrSrg)ChromiumEventLoggeriZ  a  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.
c                    [        U R                  S5      (       a  U R                  R                  $ / U R                  l        U R                  R                  $ )zE
The main event stack, with every chromium event.
Logged to tlparse.
stack)hasattrtlsr  r   s    rT   	get_stackChromiumEventLogger.get_stacka  s<    
 488W%%88>>!DHHN88>>!rS   c                >    U R                  5       nU(       a  US   $ S$ )z7
Get the top event name or None if the stack is empty.
N)r  )r   r  s     rT   r<  ChromiumEventLogger.get_topl  s!      !uRy+t+rS   c                    [        U R                  S5      (       a  U R                  R                  $ / U R                  l        U R                  R                  $ )zY
A smaller subset of the main stack that gets used to log
PT2 Compile Events internally.
pt2_compile_substack)r  r  r  r   s    rT   get_pt2_compile_substack,ChromiumEventLogger.get_pt2_compile_substacks  sB    
 48834488000,.DHH)88000rS   c                    [        U R                  S5      (       d  0 U R                  l        U R                  R                  $ )N
event_data)r  r  r  r   s    rT   get_event_data"ChromiumEventLogger.get_event_data~  s/    txx.."$DHHxx"""rS   c                    [         R                  " 5       U l        [        [        R
                  " 5       5      U l        [        R                  SU R                  5        g )Nz*ChromiumEventLogger initialized with id %s)		threadinglocalr  r   uuiduuid4id_ry   r   r   s    rT   r   ChromiumEventLogger.__init__  s9    ??$ tzz|$ 	=txxHrS   c                R    XR                  5       ;  a  gU R                  " U40 UD6  g)zT
Same as add_event_data, but will silently not log if the event isn't in the stack.
N)r  r=  )r   r   r]  s      rT   try_add_event_data&ChromiumEventLogger.try_add_event_data  s'     ^^--J1&1rS   c                    XR                  5       ;  a)  [        S[        U5       SU R                  5        S35      eU R                  5       nX;  a  0 X1'   X1   R	                  U5        g)zb
Adds additional metadata info to an in-progress event
This metadata is recorded in the END event
zEvent z not in zq. Cannot add metadata to events that aren't in progress. Please make sure the event has started and hasn't ended.N)r  RuntimeErrorr   r  r   )r   r   r]  r  s       rT   r=  "ChromiumEventLogger.add_event_data  su     ^^--j)*(4>>3C2D EK K 
 ((*
'%'J"%%f-rS   c                H   [        [        R                  R                  R	                  5       5      nXSS'   U R                  UUSU5        U R                  5       R                  U5        U R                  " U40 UD6  U(       a   U R                  5       R                  U5        gg)z
Logs the start of a single event.
:param str event_name Name of event to appear in trace
:param time_ns Timestamp in nanoseconds
:param metadata: Any extra metadata associated with this event
r  BN)
r   r   r  r  r  _log_timed_eventr  r   r=  r  )r   r   r   r   r   r  s         rT   r   #ChromiumEventLogger.log_event_start  s     55HHJK
!+		
 	
+J3(3 ))+22:> !rS   c                    U R                  5       nU R                  5       nUR                  5         UR                  5         U R                  5       nUR                  5         g ri   )r  r  rj   r  )r   r  substackr  s       rT   r   ChromiumEventLogger.reset  sK      002((*
rS   c                  ^ [        [        R                  R                  R	                  5       5      nXcS'   U R                  5       nTU;   a	  UT   nUT	 O0 nUR                  U5        U R                  TUSU5      n	U4S jn
U R                  5       nTU;  a  [        R                  S5        gU
" U5        U(       a?  U R                  5       nU
" U5        [        XU R                  U5        UR                  5         UR                  5         g)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
r  Ec                z   > TU S   :w  a1  [         R                  S5        U R                  5         TU S   :w  a  M0  g g )Nr  z>ChromiumEventLogger: Detected overlapping events, fixing stack)ry   rA  pop)r  r   s    rT   	pop_stack4ChromiumEventLogger.log_event_end.<locals>.pop_stack  s9    b	) T 		 b	)rS   z7ChromiumEventLogger: Start event not in stack, ignoringN)r   r   r  r  r  r  r   r  r  ry   rA  r  r*   r  r  )r   r   r   r   r  r   r  all_event_datar   eventr  event_stackr  s    `           rT   r   !ChromiumEventLogger.log_event_end  s    55HHJK
!+ ,,.'+J7Nz*Nh'%%	
	 nn& [( KKQR+ #'#@#@#B *+'TXX} !$$& 	rS   Nc                   ^ UUS-  UUSSSS.m[         R                  R                  SU4S jSSS9  [        T5        T$ )	zQ
Logs a timed event in chromium format. See log_event_start, log_event_end, etc.
r   r   r   )r\   tsr\  phcattidpidchromium_eventc                    > T $ ri   r;   r  s   rT   r   6ChromiumEventLogger._log_timed_event.<locals>.<lambda>(      urS   F
payload_fnsuppress_contextexpect_trace_id)r   r2  r  r,   )r   r   r   r   r   r  s        @rT   r  $ChromiumEventLogger._log_timed_event  sY     D.!	
 	''$"!	 	( 	
 	'u-rS   c           	     :  ^ Uc  0 n[        [        R                  R                  R	                  5       5      nXSS'   UUS-  USSSSSS.m[        R
                  R                  S	U4S
 jSSS9  U(       a'  [        TU R                  5       U R                  U5        gg)a*  
Log an instant event with no associated duration.
:param str event_name: Name of event to appear in trace
:param int time_ns Timestamp in nanoseconds
:param Optional[Dict[str, Any]] metadata: Any extra metadata associated with this event
:param str cname optional color for the arrow in the trace
Nr  r   ir   r   p)r\   r  r\  r  r  r  r  sr  c                    > T $ ri   r;   r  s   rT   r   7ChromiumEventLogger.log_instant_event.<locals>.<lambda>O  r  rS   FTr  )
r   r   r  r  r  r2  r  r*   r  r  )r   r   r   r   r   r  r  s         @rT   log_instant_event%ChromiumEventLogger.log_instant_event/  s     H55HHJK
!+D.!

 	''$" 	 	( 	
 !'t446' !rS   )r  r  )r  z	List[str]r  r  )r  rA   )r   r   r  r	  F)
r   r   r   r{   r   rA   r   r
  r  r	  r  r	  )r   r   r   r{   r   rA   r  r{   r   r
  r  r	  ri   )
r   r   r   r{   r   r   r   Optional[Dict[str, Any]]r  rA   r  )
r   r   r   r{   r   r  r   r
  r  r	  )rK   rL   rM   rN   r|  r  r<  r  r  r   r  r=  r   r   r   r  r  rR   r;   rS   rT   r  r  Z  s@   	",	1#
I2.. 
	.2 ',?? ? !	?
  $? 
?6CC C !	C
 C  $C 
CT .2  	
 + 
B .2&+(( ( +	(  $( 
( (rS   r  zOptional[ChromiumEventLogger]CHROMIUM_EVENT_LOGc                 0    [         c
  [        5       q [         $ ri   )r  r  r;   rS   rT   r   r   ]  s    !02rS   c           	   #  T  #    [        5       nU(       a  UR                  5         [        R                  " 5       nUR	                  U U0 U5         Sv   UR                  U [        R                  " 5       0 UU5        g! UR                  U [        R                  " 5       0 UU5        f = f7f)z
Context manager that creates a chromium start and end event. Chromium event
logging is integrated with dynamo_timed, so you probably want to use that
instead. Use this context manager only if you want to avoid dynamo_timed.
N)r   r   r   r   r   r   )r   reset_event_logr   chromium_event_logchromium_start_times        rT   chromium_event_timedr  d  s      34  ",,.&&
		
((LLN!	
((LLN!	
s   AB(A; )B(;*B%%B(c                  D    \ rS rSr% SrS\S'   S\S'   S r\S 5       rS	r	g
)CleanupHooki  z,Remove a global variable when hook is calledrA   scoper   r\   c                r    [         b  [         =R                  S-  sl        U R                  U R                  	 g r   )CleanupManagerre   r  r\   r   r\  s     rT   __call__CleanupHook.__call__  s)    %  A% JJtyy!rS   c                `    X;  d   e[         =R                  S-  sl        X U'   [        X5      $ r   )r  re   r  )r  r\   r  s      rT   createCleanupHook.create  s1       !d5''rS   r;   N)
rK   rL   rM   rN   r|  r~   r  staticmethodr  rR   r;   rS   rT   r  r    s)    6
I" ( (rS   r  c                  8   ^  \ rS rSr% SrS\S'   U 4S jrSrU =r$ )r  i  r   zClassVar[CleanupManager]instancec                ^   > U R                   U    H
  nU" 5         M     [        TU ]	  U5        g ri   )r   rD  rq  )r   rs  hookrF  s      rT   rq  CleanupManager._remove_id  s(    KK$DF %3rS   r;   )	rK   rL   rM   rN   re   r~   rq  rR   rH  rI  s   @rT   r  r    s    E&&   rS   r  c                    U R                  5       R                  U R                  5      nU R                  (       a,  U R                  b  U R                  R                  5       Ul        U$ )z!Clone the tensor and its gradient)clonerequires_grad_requires_gradis_leafgrad)r   ys     rT   clone_tensorr    sB    		  1AyyQVV'HrS   dtypec          	     6  ^ [        U 5      (       a  U $ U4S jn[        R                  " 5          U R                  R                  S:X  a  U" U 5      sSSS5        $ U R
                  [        R                  L a`  [        R                  " U" U R                  5       5      U" U R                  5       5      U R                  U R                  5       S9sSSS5        $ [        U 5      (       a  U R
                  [        R                  [        R                  1;   a!  U R                  5       nU R!                  5       nO U R#                  5       nU R%                  5       n[        R&                  " U" U5      U" U5      U" U R)                  5       5      U R                  U R
                  S9sSSS5        $ [+        S [-        U R/                  5       U R1                  5       5       5       5      nU R2                  (       a  [        R4                  " US-   4U 5      nO6[        R6                  " US-   T=(       d    U R8                  U R                  S9nU R;                  5       UR;                  5       -
  S-  U R=                  5       -  nUR?                  U R/                  5       U R1                  5       U5         URA                  U RC                  5       5        U RD                  (       a  URG                  U RH                  5        U RD                  (       a&  U RJ                  b  [M        U RJ                  TS	9Ul%        [Q        U S
5      (       a  U RR                  RU                  5       Ul)        UsSSS5        $ ! [N         a    U" U 5      s sSSS5        $ f = f! , (       d  f       g= f)zcopy while preserving stridesc                Z  > [         R                  " U 5      nU R                  (       a  UR                  U R                  5        U R                  (       a&  U R
                  b  [        U R
                  TS9Ul        [        U S5      (       a  U R                  R                  5       Ul        U$ )Nr  _dynamo_dynamic_indices)
r   r  r  r  r  r  clone_inputr  r  copy)r   r  r	  s     rT   torch_clone clone_input.<locals>.torch_clone  su    KKN99Q__-99+ u5AF1/00()(A(A(F(F(HA%rS   xlaN)is_coalesced)layoutc              3  6   #    U  H  u  pUS -
  U-  v   M     g7f)r8   Nr;   )r   shapestrides      rT   r   clone_input.<locals>.<genexpr>  s      
6O]UUQY& 6Os       )r	  devicer  r  )+r=   r   no_gradr  r9  r  
sparse_coosparse_coo_tensor_indicesrX   r  r  r)   
sparse_csr
sparse_bsrcrow_indicescol_indicesccol_indicesrow_indicessparse_compressed_tensorr   rp   zipsizer  is_quantizedempty_quantizedemptyr	  data_ptrelement_sizeas_strided_copy_r  r  r  r  r  r  r  r  r  r  )r   r	  r  compressed_indicesplain_indicesneeded_sizeresultcache_line_offsets    `      rT   r  r    s    qzz 
88==E!q> 
 88u'''**AJJL)AIIK(^^-	 
 "!$$xxE,,e.>.>??%&^^%5" !%&^^%5" !11./M*AHHJ'xx) 
8  
69!&&(AHHJ6O
 
 >>**K",<+>BF[[b (8F ZZ\FOO--3^^ 	1668QXXZ1BC
	"LL#yy%%aoo6yyQVV/)!&&> 1/00-.-F-F-K-K-MF*m 
^  	" q>!g 
^	"_ 
sE   "N
A3N
C
N
,C?N
,BM).1N
)N;N
NN


Nc                   [        U 5      [        L a  [        U 5      nUR                  5        He  u  p#[        U[        5      (       a  [        U5      X'   M)  [        U[        R                  5      (       d   [        U5      5       e[        U5      X'   Mg     U$ [        U 5      n[        [        U5      5       H5  n[        X   [        R                  5      (       d  M&  [        X   5      X'   M7     U$ ri   )r9  dictr   r  r  clone_inputsr   r  r  r   ranger  )example_inputsresr   r   r  s        rT   r5  r5    s    Nt#>"))+JC%'''.!%66CUC6&u- & 

~
C3s8_cfell++ (CF  JrS   c                     U R                  5         g ! [         a8  nSSKJn  [        R
                  " SS[        U 5      5      nU" SU 35      UeS nAff = f)Nr8   )	SkipFramez\(.*rq  z(torch.compile cannot be run in context: )r*  r  r;  r:  rP  subr   )r  r  r:  functorch_subclass_names       rT   skip_frame_if_in_functorch_moder=    sX    	 " #%&&"d3i"@67N6OP
	s    
A3AAc               #    #    [         R                  R                  n [         R                  R                  R
                  nU" 5          U " 5          [         R                  " [         R                  R                  5       5      n[        U5        [         R                  R                  5       (       a2  [         R                  " [         R                  R                  5       5      nS S S 5        S S S 5         S v   [         R                  R                  R                  5          [         R                  R                  W5        [         R                  R                  5       (       a  [         R                  R                  W5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f! [         R                  R                  R                  5          [         R                  R                  W5        [         R                  R                  5       (       a  [         R                  R                  W5        S S S 5        f ! , (       d  f       f = f= f7fri   )r   _C_DisableFuncTorchrz  _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       rT   preserve_rng_staterK    s`    22!KK88OO		 "3"5KK : : <=	'	2::""$$"[[)A)A)CDN	 #6	 
9[[))@@BLL&&y1zz&&((

((8 CB #6"5	 	  CBU[[))@@BLL&&y1zz&&((

((8 CBBs   AI&FBF#F+I&4F> 8(I& A"F-	I&
F	F
F*&I&-
F;7I&>)I#'A"I		I#
I I##I&c                
   [        U [        R                  R                  R                  [        R                  R
                  R                  [        R                  R                  [        R                  R                  45      $ ri   )	r  r   jit_traceTopLevelTracedModule_scriptRecursiveScriptModuleScriptFunctionScriptModule)model0s    rT   is_jit_modelrU  +  sW    II11II33II$$II""		
 rS   c                R   [        U 5      (       a  U $  [        R                  R                  X5      $ ! [         ai     [        R                  R                  U 5      s $ ! [         a7    U(       a  [        R                  S5         O[        R                  S5          g f = f g f = f)Nz	jit errorz0Both torch.jit.trace and torch.jit.script failed)	rU  r   rM  tracerE  scriptry   rF  error)modelr7  r  s      rT   torchscriptr[  7  s    E	Nyyu55 N	N99##E** 	Nk*		LM	N* Ns>   3 
B&AB&&B B&B B&B  B&%B&c                \     [         R                  " U 5      $ ! [        [        4 a     g f = fri   )r  getfilerW  OSErrorr  s    rT   r]  r]  I  s-    s##w s    ++c                *    [        [        U 5      5      $ )zLTest if an object is a namedtuple or a torch.return_types.* quasi-namedtuple)is_namedtuple_clsr9  r_  s    rT   is_namedtuplerb  P  s    T#Y''rS   c                  ^   [        T [        5      (       a  [        T SS5      nUS;   a  g[        [        T SS5      [        5      (       ae  [	        [        T SS5      5      (       aJ  [        S T R
                   5       5      nU[        4:X  a  gU(       a  [        U 4S jU 5       5      (       a  gg	! [         a     g	f = f)
zhTest if an object is a namedtuple or a (torch.return_types|torch.autograd.forward_ad).* quasi-namedtuplerL   N)torch.return_typesztorch.autograd.forward_adT_fields_makec              3  :   #    U  H  o[         Ld  M  Uv   M     g 7fri   )r   )r   ts     rT   r   $is_namedtuple_cls.<locals>.<genexpr>`  s     KA7:Jaas   	c              3  x   >#    U  H/  n[        U5      =(       a    TR                  UR                  L v   M1     g 7fri   )ra  __new__)r   rh  r]   s     rT   r   ri  d  s;      ! #	 *!, 5KK19945 #s   7:F)
issubclassr  getattrr  callable	__bases__anyrW  )r]   modulebasess   `  rT   ra  ra  U  s    c5!!S,5FLL'#y$7??HWd+E E KKKUH$S ! #!       s   (B2 A"B2 !B2 2
B?>B?c                8   U [         L a  g[        U [        5      (       d   e[        U S5      (       a  U R                  $ [
        R                   " S S5      5       nU R                  S:X  d   eU " [        U[        U R                  5      5      5      n0 n[        U5       HA  nUS   S:w  d  M  [        [        X$5      U5      (       d  M*  [        X$5      R                  X4'   MC     [        U5      U R                  :X  d   e[        [!        X3R"                  S95      $ )	zIGet the fields of a namedtuple or a torch.return_types.* quasi-namedtuple)startstopstepre  c                       \ rS rSr% S\S'   Srg)!namedtuple_fields.<locals>.Markeri~  r{   indexr;   NrK   rL   rM   rN   r~   rR   r;   rS   rT   Markerrx  ~  s    
rS   r{  rd  r   r7  )r   )slicerl  r  r  re  ru  	dataclassrL   r   r6  n_fieldsdirr  rm  ry  r  sortedrn   )r]   r{  r  fieldsr\   s        rT   namedtuple_fieldsr  s  s     e|(c5!!!!sI{{   >>1111
c&%-.
/CFC7c>j);VDD"3-33FL  v;#,,&&&JJ/00rS   c           	     Z  ^^^ [         R                  " 5          [         R                  " [         R                  R	                  5       5      m[         R
                  R                  5       (       a2  [         R                  " [         R
                  R	                  5       5      m[        R                  " U R                  5       U R                  5       5       Vs/ s H%  nXR                  [         R                  " U5      4PM'     snmS S S 5        UUU4S jnU$ s  snf ! , (       d  f       N= f)Nc                   > [         R                  " 5          [         R                  R                  T5        [         R                  R                  5       (       a  [         R                  R                  T5        T H)  u  pnU R                  U:w  d  M  U R                  U5        M+     S S S 5        g ! , (       d  f       g = fri   )r   r  rC  rF  r  rE  _versionr-  )paramr  original_valuerJ  rI  saved_states      rT   restore"checkpoint_params.<locals>.restore  st    ]]_LL&&y1zz&&((

((82=.>>W,KK/ 3>	 __s   A8B1B11
B?)r   r  r  rC  rD  r  rE  r   r   
parametersbuffersr  )gmr  r  rJ  rI  r  s      @@@rT   checkpoint_paramsr    s    	KK : : <=	::""$$"[[)A)A)CDN #"**,G
G NNEKK$67G
	 
0 N
	 
s   B=D,DDD
D*c                   [         R                  R                  5       (       a  [         R                  R                  nO[        nU" 5         [
        R                  " 5         [         R                  " S5        [        R                  " 5       n[        U5       H  nU " U6 nU" 5         M     [        R                  " 5       nWXt-
  4$ )Ni9  )r   r  rE  synchronizer^  gccollectmanual_seedr   perf_counterr6  )rZ  r7  timesr  t0r7  r1  t1s           rT   timedr    s    zz  jj,,MJJL	d				B5\'  
			B27?rS   c           	     l    [        S [        R                  " XR                  S5      5       5       5      $ )Nc              3  8   #    U  H  oR                   v   M     g 7fri   )is_cudar   r   s     rT   r    check_is_cuda.<locals>.<genexpr>  s     W"VQyy"V   T)allr   r   r  )r  r7  s     rT   check_is_cudar    s&    W)//.--PTBU"VWWWrS   r  c                    U S:  d   e[        U 5       Vs/ s H  nSU 3PM
     nn[        USS  US S -   5      n[        SSR                  U5       SSR                  U5       S35      nSU  S	3Ul        U$ s  snf )
Nr8   r   r  zlambda ri  z: ()rot__helper)r6  reversedevalr   rK   )rQ  r  varsrotatedr  s        rT   rot_n_helperr    s    q5L5"1X&XasGXD&tBCy49,-G	's388G+<*=Q?	@B7#BKI	 's   A8z	Set[type]common_constant_typesc                   [        U [        [        45      (       a  [        [	        [
        U 5      5      $ [        U [        R                  [        [        R                  45      =(       d    [        U [        [        1-  5      $ ri   )r  r  	frozensetr  r   is_safe_constantr  enumEnumr9  r   Sizer  r|  r  s    rT   r  r    s^    a%#$$3'+,,a$))T5::67 6	'< rS   c                t    SSK JnJn  [        X5      (       a  UR	                  U R                  5       5      $ U $ )Nr8   )ConstantVariableSymNodeVariable)	variablesr  r  r  r  evaluate_expr)r  r  r  s      rT   specialize_symnoder    s1    < #''&&s'8'8':;;JrS   c                h    SSK Jn  [        U 5      n [        X5      (       a  U R	                  5       $ U $ )Nr8   r  )r  r  r  r  as_python_constant)r  r  s     rT   guard_if_dynr    s.    +
S
!C#((%%''JrS   c                j    [        S [        R                  " XR                  5       5       5       5      $ )Nc              3  @   #    U  H  oR                  5       v   M     g 7fri   )is_python_constantr  s     rT   r   &check_constant_args.<locals>.<genexpr>  s     V/U!##%%/Ur  )r  r   r   r   r[  s     rT   check_constant_argsr    s"    Vyt]]_/UVVVrS   c                    SSK Jn  SSKJn  Sn[        R
                  " XR                  5       5       H,  n[        XS5      (       a  US-  nM  [        XR5      (       a  M,    g   US:  $ )Nr8   r  UnspecializedPythonVariabler   F)variables.constantr  variables.tensorr  r   r   r   r  )r\  r]  r  r  unspec_countr   s         rT   check_unspec_python_argsr    sV    4=L__T==?3a55ALA00	 4
 !rS   c                    SSK Jn  [        R                  " XR	                  5       5       H,  nUR                  5       (       a  M  [        X25      (       a  M,    g   g)Nr8   r  FT)r  r  r   r   r   r  r  )r\  r]  r  r   s       rT   check_unspec_or_constant_argsr    sA     >__T==?3$$&&*Q*T*T 4 rS   c                ~   ^ SSK Jm  [        U4S j[        R                  " XR                  5       5       5       5      $ )Nr8   )NumpyNdarrayVariablec              3  <   >#    U  H  n[        UT5      v   M     g 7fri   )r  )r   r   r  s     rT   r   +check_numpy_ndarray_args.<locals>.<genexpr>"  s#      7A 	1*++7s   )r  r  rp  r   r   r   )r\  r]  r  s     @rT   check_numpy_ndarray_argsr    s/    6 }}7  rS   zType[KeysView[Any]]	dict_keyszType[ValuesView[Any]]dict_valuesodict_valueszType[Iterator[Any]]tuple_iteratorrange_iteratorc                n    [        U 5      n[        R                  R                  R	                  U5        U$ ri   )
object_newr   r  Moduler   )r]   r  s     rT   nn_module_newr  1  s&    
S/C	HHOOS!JrS   c                N    [         R                  " [        R                  U S5      $ r   )r  reduceoperatormul)its    rT   productr  7  s    HLL"a00rS   c                :    U R                  5       u  nu  p4X4U-      $ ri   )
__reduce__)r  ry  r7  r  rt  s        rT   tuple_iterator_getitemr  ;  s!    }}Avu}rS   c                    U R                  5       u  nu  p#UR                  U=(       d    S-   nUR                  nUR                  nXEU4$ r   )r  rt  ru  rv  )
range_iterr7  	range_obj	maybe_idxrt  ru  rv  s          rT   normalize_range_iterr  C  sH    !+!6!6!8A|	 OOy~A.E>>D>>DrS   c                $    U R                  U5      $ ri   )as_subclass)rh  r]   s     rT   to_subclassr  M  s    ==rS   c                Z    [        [        R                  " [        U 5      XS-   5      5      $ r   )nextr   isliceiter)drQ  s     rT   dict_keys_getitemr  Q  s"    	  a!U344rS   c                t    U R                   R                  nU R                  nU(       a  SOSnU SU SU 3nU$ )NLGz["z"].)rF  rK   r\   )r   r  r\   r  r  
local_names         rT   	enum_reprr  U  sA     ??##D
**CCcE7"TF#cU+JrS   c                   XR                   S'   [        R                  " 5       R                  R                  n[
        R                  R                  R                  R                  X!5      =n(       a  X0R                   S'   g g )Nexample_valueunbacked_bindings)
metar(   rn   	fake_mode	shape_envr   r    experimentalsymbolic_shapescompute_unbacked_bindings)noder  r  symbol_to_paths       rT   set_example_valuer  _  sk     "/IIo""$..88I..>>XX ~  *8		%&rS   c                    U R                  5       R                  R                  R                  S5      n[	        U5      (       d  SSKJn  U" S5        U$ )Nr  r8   r  z:Cannot check Tensor object identity without its fake value)r  r  r  rn   r=   r;  r  )vtfake_tensorr  s      rT   _get_fake_tensorr  n  sA    ++-$$))--o>K;&RSrS   c                H  ^ SSK JnJnJnJn  TR                  5       (       a&  [        U4S jU  5       5      nUR                  U5      $ Sn	U(       a  [        TU5      (       a  Sn	[        T5      mS n
U  H  nU	(       a5  [        X5      (       a#  T[        U5      L a  UR                  S5      s  $ M=  M?  U" [        R                  5      R                  X+T/0 5      nU
c  Un
Mn  U" [        R                  5      R                  X,U
/0 5      n
M     U
c  UR                  S5      n
U
$ )Nr8   )BuiltinVariabler  TensorVariableVariableTrackerc              3     >#    U  H=  nUR                  5       =(       a!    UR                  5       TR                  5       :H  v   M?     g 7fri   )r  r  )r   r   rQ  s     rT   r    iter_contains.<locals>.<genexpr>  sI      
    " F$$&&*C*C*EEFs   AAFT)r  r  r  r  r  r  rp  r  r  r  r  eqcall_functionor_)r   rQ  txcheck_tensor_identityr  r  r  r  found_constmust_check_tensor_idfoundr   checks    `           rT   iter_containsr  w  s       "" 
 
 

  &&{33 FN!C!C#!&)'+E!,,-a00+22488 1 - $HKK0>>rv;PRSE}'5CC  } ''.LrS   c                t    [        U [        R                  [        R                  R                  [
        45      $ )z4Returns whether it indexes dictionaries using its id)r  r   r  r  r  r   )r   s    rT   	key_is_idr    s#    a%,,9JKLLrS   c                    U R                  5        Vs/ s H   n[        U5      (       a  [        U5      OUPM"     sn$ s  snf ri   )r   r  rf  )r   r   s     rT   	key_to_idr    s1    27**,?,QYq\\BqEq(,???s   '=c                 ^ SSK Jn  [        U [        [        45      (       ah  SR                  U4S jU  5       5      n[        U [        5      (       a  SU S3$ [        U [        5      (       d   e[        U 5      S:X  a  SU S3$ SU S	3$ [        U [        R                  5      (       a  [        U TS
9R                  SS5      $ U" U 5      (       a  U R                  $ [        U [        5      (       a  S nU" U 5      $ U < $ )Nr8   )is_builtin_callableri  c              3  8   >#    U  H  n[        UTS 9v   M     g7fr  N
const_reprr   r  r  s     rT   r   const_repr.<locals>.<genexpr>  s     D!Qj%8!   [](z,)r  r  'rq  c                z    U R                   nUR                  nUS:X  a  UR                  $ US-   UR                  -   $ )Nbuiltins.)rF  rL   rM   )rE  klassrq  s      rT   fullnameconst_repr.<locals>.fullname  s?    KKE%%F#)))C<%"4"444rS   )trace_rulesr  r  r   r  r   r  r  r  r  replacerK   r9  )r   r  r  
elems_reprr)  s    `   rT   r  r    s    0!dE]##XXD!DD
azl!$$a''''1v{:,b)):,a((	Atyy	!	! %(00b99	Q		zz	At			5 {rS   c               J   ^ SR                  U4S jU  5       5      nSU-   S-   $ )Nri  c              3  8   >#    U  H  n[        UTS 9v   M     g7fr  r  r  s     rT   r   !dict_keys_repr.<locals>.<genexpr>  s     GJq
1E2Jr   r!  r"  )r   )
const_keysr  keys_strs    ` rT   dict_keys_reprr3    s&    xxGJGGH>CrS   
__dict_key)UnsupportedFakeTensorExceptionc                P    U S[        U5       SU R                  R                   3$ )Nr7  _c)rf  outputr  )r  rootr  s      rT   get_safe_global_namer:    s*     V1RWIR		 4 4566rS   c                     U " 5       $ ! [          a;  nSSKJn  SUR                   S3n[        R                  U5        U" X1S9   S nAg S nAff = f)Nr8   r  zUnsupported: z with fake tensor propagation.r  )r5  r;  r  reasonry   rA  )r  r  r  msgs       rT   wrap_fake_exceptionr>    sH    't) '&ahhZ'EFCc&'s   	 
A1A		Ac                   ^  [         R                  R                  R                  U5         [	        U 4S j5      sS S S 5        $ ! , (       d  f       g = f)Nc                 0   > [         R                  " T 5      $ ri   )r  deepcopyr_  s   rT   r   )deepcopy_to_fake_tensor.<locals>.<lambda>  s    4==+=rS   )r   r  r  FakeCopyModer>  )r  r  s   ` rT   deepcopy_to_fake_tensorrD    s2    				&	&	3	3I	>"#=> 
?	>	>s   A
Ac                    [         R                  " [         R                  " [         R                  " X-
  5      5      5      $ )z#
Calculate root mean squared error
)r   sqrtmeansquare)rr  r8  s     rT   rmserI    s'     ::ejjci!89::rS   g-C6?Tc                  ^ ^^^^^^^^^	^
^ Tc  T m[        T [        [        [        R                  [
        R                  R                  [
        R                  45      (       a  [        T[        [        [        R                  45      (       d   S[        T 5       S[        T5       35       e[        T 5      [        T5      :w  a	  T	" S5        g[        T 5      [        T5      :H  =(       a'    [        UUUUU	UUU
4S j[        T TT5       5       5      $ [        T 5      R                  S:X  a1  [        T R                  TR                  TR                  TTTTTTT	T
S9$ [        T [         5      (       a  [        T[         5      (       d   e[#        T R%                  5       5      [#        TR%                  5       5      :X  d;   S	[#        T R%                  5       5       S
[#        TR%                  5       5       35       e['        T R%                  5       5       H/  n[        T U   TU   TU   TTTTTTT	T
S9(       a  M&  T	" SU5          g   g[        T ["        5      (       aO  [        T["        5      (       d   e[#        T 5      [#        T5      :X  d   S[#        T 5       S
[#        T5       35       eg[        T [
        R(                  [*        45      (       Ga  [        T [
        R,                  R.                  5      (       a   e[        T[
        R,                  R.                  5      (       a   eS mU4S jT TT4 5       u  m mmT R0                  (       a3  TR0                  (       d   eT R3                  5       m TR3                  5       m[        T[
        R(                  5      (       d   S[        T 5       S[        T5       35       eT(       a  T R4                  TR4                  :w  a  T	" ST R4                  TR4                  5        gT R4                  [
        R6                  :X  ag  T(       a  g[
        R8                  " T R;                  [
        R<                  S9TR;                  [
        R<                  S9TTTS9nU(       d  T	" S5        U$ T(       a  T R?                  5       R;                  [
        R@                  5      m TR?                  5       R;                  [
        R@                  5      m[
        R8                  " T TTTSS9(       a  g[
        R                  RB                  RE                  T TSSS9nUS:  a@  [F        RI                  SURK                  5       RM                  5       RO                  5       5        US:  $ T(       d  T R;                  TR4                  5      m [
        R8                  " T TTTTS9(       a  gTR4                  [
        RP                  :X  Ga  SnTRS                  5       RU                  5       (       d{  TRS                  5       RU                  5       (       dX  T RS                  5       RU                  5       (       a5  [
        R8                  " TR;                  TR4                  S9TUUTS9(       a  g[W        TT 5      RO                  5       n[X        RR                  " U5      (       a  [F        RI                  S5        [W        TT5      RO                  5       nTR4                  [
        RZ                  [
        R\                  4;   a  SOSnT
(       a  TR_                  5       S::  a	  TS:  a  SnOyT
(       a  TR_                  5       S ::  a	  TS:  a  S!nOUTR_                  5       S":  d?  T R`                  S#:X  a)  T Rb                  S$   T Rb                  S%   s=:X  a  S&:X  d  O  TS':  a  SnUUU-  TS-  -   :*  nU(       dr  T(       ak  [X        RR                  " U5      (       aP  [X        RR                  " U5      (       a5  [
        Rd                  Rf                  Rh                  Rj                  (       d  SnU(       d'  T	" S(UUTRm                  5       TR4                  UTT
5        U$ T(       a  gT	" S)T5        g[        T [n        [p        [        S5      [6        [
        Rr                  45      (       a*  T(       a  gT T:H  nU(       d  T	" S*[        T 5      T T5        U$ [u        T 5      (       d  [w        T 5      (       al  T(       a0  [u        T5      (       d   [w        T5      (       d  T RO                  5       m [        T 5      [        T5      L =(       a    T T:H  nU(       d
  T	" S+T T5        U$ [y        T 5      (       aX  [        T 5      [        T5      L =(       a;    [        [
        Rz                  " T 5      [
        Rz                  " T5      TTTTTTTT	T
S9$ [        T 5      R                  S,;   aP  [        T 5      [        T5      L d   e[        UUUUUU	U UUUU
4S- jT R|                  R%                  5        5       5      $ [        S.[        T 5      R                   35      e)/z-Check correctness to see if ref and res matchNztype mismatch r   zLength mismatchFc              3  P   >#    U  H  u  pn[        UUUTT
TTT	TTTS 9v   M     g7f)	log_error(use_larger_multiplier_for_smaller_tensorN)same)r   aibi	fp64_reficos_similarity	equal_nanexact_dtypeignore_non_fprM  relax_numpy_equalitytolrN  s       rT   r   same.<locals>.<genexpr>  sG      ,
 &=!	 $#9a &=s   #&QuestionAnsweringModelOutputrL  zkeys mismatch z == rS  rX  rT  rU  rW  rV  rM  rN  zAccuracy failed for key name %sTzelements mismatch c                p    [        U [        R                  5      (       a  U $ [        R                  " U 5      $ ri   )r  r   r  tensorrh  s    rT   	to_tensorsame.<locals>.to_tensorP  s%    "1ell331HaHrS   c              3  4   >#    U  H  nT" U5      v   M     g 7fri   r;   )r   r  r_  s     rT   r   rY  S  s     M8Linn8Lrl  zdtype mismatch %s, %sr  )atolrtolrT  z+Accuracy failed: uint8 tensor did not matchr   gư>)dimepsgGz?zSimilarity score=%sg{Gz?z=Found nan in reference. Consider running in higher precision.g      @g       @
   g      $@i  g      @r      r  r8   g{Gz?zRMSE (res-fp64): %.5f, (ref-fp64): %.5f and shape=%s. res.dtype: %s, multiplier: %f, tol: %f, use_larger_multiplier_for_smaller_tensor: %dz+Accuracy failed: allclose not within tol=%szAccuracy failed (%s): %s != %sz!Accuracy failed (numpy): %s != %s)MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsLongformerMaskedLMOutput	InstancesSquashedNormalBoxesNormalTanhTransformFooVariablec              3     >#    U  H6  n[        [        TU5      [        T
U5      [        TU5      TTTTT	TTTS 9v   M8     g7f)r[  N)rO  rm  )r   r   rS  rT  rU  fp64_refrV  rM  rr  rW  r8  rX  rN  s     rT   r   rY  	  sW      
 + S!S!#&-#'%9+#9a +s   >Azunsupported type: )@r  r   r  r|   r  r   r  ParameterListr  r9  r  r  r%  rK   rO  lossr4  r   r   r  r  floatr  r<   	is_sparseto_denser	  r
  allclosetor  flattenr  
functionalcosine_similarityry   rA  cpudetachrf  r  isnanrp  rI  mathr  bfloat16numelndimr  rN  r9   cppinject_relu_bug_TESTING_ONLYr&  r   r{   r  r  r  r  	as_tensor__dict__r  )rr  r8  ru  rS  rX  rT  rU  rW  rV  rM  rN  r   rscore	loose_tol	ref_error	res_error
multiplierpasses_testr_  s   ```````````        @rT   rO  rO    sP    dE;,,ehh.D.DejjQ  ${001
 
 	4DI;aS	{3	4 
 s8s3x'(3x3s8# 
 ,
 ,
 &)c8%<,
 )
 	
  
c		=	= HHHHMM 5]
 	
 
C		#t$$$$388:#HHJ#
 
 	CC
O,DSXXZ0AB	C 
 
#AFFQK#1' +)="/'=e  ;Q?# $$ 	C		#s####3x3s8#R'9#c(4Cz%RR#	C%,,.	/	/c5#4#4#?#?@@@@c5#4#4#?#?@@@@	I Nc88LMS(==== =,,.C,,.C#u||,,VtCyk4PS9+.VV,yyCII%1399ciiHyyEJJ& NNFFF-FFF-' KL++-""5==1C++-""5==1C~~c3SsdK HH''99#st9TEt|1599;3E3E3G3L3L3NOD= ffSYY' ~~c3SsiP ~~. %	 (,,..IIKOO--		)) #))4&&"+   3/446	::i((KKW !3/446	 99(GGCS  <NN$*sh!%J=NN$+x!$JNN$t+A#))B-399R=*MA*Mh
 "%J'J,BS4Z,OP#!

9--

9-- "OO2266SS"&K"I!!
		"@
 #"CSI	C#sDJellC	D	D3J6S	3L	3		#6s#;#;c""&9#&>&>((*C#Y$s)#5#*93D	#		S	T#Y& 
DOOC OOC )#!5'5]-
 	
 
c		   
 CyDI%%% 
 
 ||((*
 
 	
" /S	0B0B/CDEErS   c                ~    U R                   R                  S5      S   nSU R                   SU SU R                   S3$ )N/r  r$  z' (r   r  )r  splitr8  r:  )r<  short_filenames     rT   format_func_infor  	  sD    %%++C04Nt||nC/q1D1D0EQGGrS   c               #    #    [         R                  n [        R                  [         l        [         R                  n[        R                  [         l         S v   U [         l        U[         l        g ! U [         l        U[         l        f = f7fri   )r9   cache_size_limitr  r   accumulated_cache_size_limit)priorprior_acc_limits     rT   disable_cache_limitr  	  s^     ##E!kkF99O*-++F'>"'.=+ #(.=+s   ABA1 B1B		BzDefaultDict[Any, List[Any]]guard_failuresz3List[torch._dynamo.output_graph.GraphCompileReason]graph_break_reasonsc                    S[         R                   R                  5       R                  S5      -   S-   [        [        R
                  " 5       5      -   n[        R                  R                  X5      $ )Nrun_z%Y_%m_%d_%H_%M_%S_%fz-pid_)datetimenowstrftimer   r  getpidr$  r   )root_dirdir_names     rT   _get_debug_dirr  ;	  sd     	





!
*
*+A
B	C 	 biik
		  77<<++rS   c                 8    [         R                  n [        U 5      $ ri   )r9   debug_dir_rootr  )
debug_roots    rT   r%  r%  G	  s    &&J*%%rS   c                    SU R                   ;   a,  [        U R                   S   5      (       a  U R                   S   $ U(       a  SSKJn  U" S5        g g )Nr  r   r  z9`FakeTensor` example value was required but not available)r  r=   torch._dynamo.excr  )r  requiredr  s      rT   extract_fake_example_valuer  L	  sB    $))#		/0J(K(Kyy))	3QRrS   c                :    [        U 5      UR                  L d   eU $ ri   )r>   r  )r  r  s     rT   ensure_graph_faker  W	  s    q!R\\111HrS   c                j   ^ ^ SUU 4S jjn[         R                  R                  R                  X5      $ )Nc                   > U R                   S:X  a  SU R                  ;  a  [        U TT5      $ U R                  S   nT(       d+  [        U[        R
                  5      (       a  [        UT5      $ U$ )Nr
  r  )rN  r  get_fake_valuer  r   r  r  )rQ  r   allow_non_graph_faker  s     rT   visit)get_fake_values_from_nodes.<locals>.visit]	  s_    44?"aff'D "!R)=>>ff_%#
3(E(E$S"--
rS   )rQ  ztorch.fx.Node)r   r    r  map_arg)r  rM  r  r  s   ` ` rT   get_fake_values_from_nodesr  \	  s(    	 	 88==  ..rS   c           	     <
  ^ ^^^^ SSK Jn  SSKJnJnJnJnJn  T R                  n	ST R                  ;   a,  [        T R                  S   5      (       a  T R                  S   $ [        TT R                  T R                  4U5      u  mmSmU	S:X  ad  [        T5      S:  aU  [        TS   [         R"                  R$                  5      (       a)  ['        TS   TR(                  5      4[+        TSS 5      -   mU	S:X  al  TR,                  R.                  T R0                     m[3        T5      (       a#  [5        TS	5      (       a  TR7                  TT5        ['        TTR(                  5      mT R8                  S
;   d  [;        S T 5       5      (       a  [+        S T 5       5      m TR(                     [=        5          [?        UUUU U4S j5      n
SSS5        SSS5        U(       d=  [t        Rv                  " [         Rx                  [z        R|                  " [~        TS 9W
5      nW
$ ! , (       d  f       N\= f! , (       d  f       Ne= f! U a    e [@         Ga  nUnURB                  b  URB                  n[        U[         RD                  RF                  RH                  5      (       a  U" SURJ                   S35        GO1[        U[         RD                  RF                  RL                  5      (       aY  [         RN                  RP                  RR                  (       d  U" SURJ                   S35        GOU" SURJ                   S35        GO[        U[         RD                  RF                  RT                  5      (       a  URJ                  n	Sn[        U	[         RV                  RX                  5      (       a\  [         RZ                  R]                  U	R^                  R8                  U	R^                  R`                  5      nUb  Uu  nnSU SU SU S3nU" SURJ                   SU S35        O[        U[         Rb                  Rd                  Rf                  Rh                  5      (       a  U" URj                  [m        U5      SS9e[        X5      (       a!  U" URj                  UR                  S   5      Ue[        U[n        5      (       a'  S[m        U5      ;   a  U" ST R0                   SU 35        U" [m        U5      5      Rq                  URr                  5      SeSnAff = f)!ab  
Run the computation represented by `node` using fake tensors and return the result.

allow_non_graph_fake: whether to allow the return result to be:
    1. non-fake or 2. fake that is not created by this instance of Dynamo.
    If `True`, you must be prepared to deal with such return values, ideally
    by further wrapping them as this graph's fakes.
r   )ValueRangeErrorr8   )TorchRuntimeErrorr  Unsupported	UserErrorUserErrorTyper  Ncall_methodcall_module_initialize_hook)interpolate
is_integerwrapped_gradientc              3  B   #    U  H  n[        U[        5      v   M     g 7fri   )r  complex)r   as     rT   r   !get_fake_value.<locals>.<genexpr>	  s      M(,1
1gs   c              3     #    U  HH  n[        U[        R                  5      (       a"  UR                  R                  b  [        U5      OUv   MJ     g 7fri   )r  r   SymFloatr  hintrx  r  s     rT   r   r  	  sG      
  #u~~..388==3L #J s   AAc                 6   > [        TR                  TT TT5      $ ri   )run_noder8  )r\  r]  nnmoduler  r  s   rT   r    get_fake_value.<locals>.<lambda>	  s    D$IrS   zdata dependent operator: zC; to enable, set torch._dynamo.config.capture_scalar_outputs = Truezdynamic shape operator: zM; to enable, set torch._dynamo.config.capture_dynamic_output_shape_ops = Truezm; Operator does not have a meta kernel that supports dynamic output shapes, please report an issue to PyTorchrq  z:It's possible that the support was implemented in module `z` and you may need to `import z`(z), otherwise zunsupported operator: z (z~see https://docs.google.com/document/d/1GgvOe7C8_NVOMLOCwDaYV1mXXyHMXY7ExoewHqooxrs/edit#heading=h.64r4npvq0w0 for how to fix)constrain_as_size_example)	case_nameargumentz
TypeError z: )r  )@torch.utils._sympy.value_rangesr  r;  r  r  r  r  r  rN  r  r=   r  r\  r]  r  r  r   r  r  rD  r  r  r8  
nn_modulestargetr  r  _infer_parametersr\   rp  r%   r>  r  	__cause__r  r  DataDependentOutputExceptionfuncDynamicOutputShapeException_dynamor9    capture_dynamic_output_shape_opsUnsupportedOperatorExceptionr  r  r?  _dispatch_pystub_schemaoverload_namer    r  r  GuardOnDataDependentSymNodeCONSTRAINT_VIOLATIONr   rW  with_traceback__traceback__pytreetree_map_onlyr  r  partialr  )r  r  r  r  r  r  r  r  r  rN  ret_valr  causeimport_suggestionmaybe_pystubrq  ctxr7  r\  r]  r  s   ``                @@@rT   r  r  k	  sp    @  
B $))#		/0J(K(Kyy))-
TYY$&:LD& H	]s4y1}DGUXX__1U1U'Q>@5ab?R	]99''4(##:L(M(M
 &&x6 +8R\\ByyEE M(,M J J  
 	
 
CR\\35)IIG 6\H    LL)++,="Ew
 NQ 65\\   <R ;;"KKE5$$00MM
 
 +EJJ< 8T T 5$$00LL
 
 ==''HH.uzzl ;b b
 .uzzl ;8 8
 5$$00MM
 
 B ""ejj3344$xx88JJOORZZ%=%=   +".KFC##)(*H Q5/ &
 (B7H6I J# #
 588((88TT
 
 22E
5 
 //M>>q	JPQQy))jCJ.FJt{{m2eW=>A'66qGTQy<RsO    I ,H27H!H2I !
H/	+H22
I <I  I TKTTc                 $    [        [        SS 5      $ )Nr   )rm  _current_noder;   rS   rT   get_current_noder  	  s    ='400rS   c              #  v   #    [        5       nU [        l         S v   U[        l        g ! U[        l        f = f7fri   )r  r  r   )r  olds     rT   set_current_noder  	  s-     

CM"!cs   9) 969c                  ^^^^ TR                   m[        T5         UUUU4S jn TS:X  a  TR                  " T0 TD6sSSS5        $ TS:X  a+  [        TS   TR                  5      " TSS 0 TD6sSSS5        $ TS:X  a  Uc   eU" T0 TD6sSSS5        $ TS:X  a.  U R                  R                  TR                  5      sSSS5        $ TS	:X  a*  S
TR                  ;   d   eTR                  S
   sSSS5        $  SSS5        [        T5      e! [        [        4 a  nSSK	J
n  U" U" U5      US9   SnAN;SnAf[         a0  n[        U" U5      5      R                  UR                  5      UeSnAff = f! , (       d  f       N= f)a  
Runs a given node, with the given args and kwargs.

Behavior is dictated by a node's op.

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

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

Nodes that are not call_function, call_method, call_module, or get_attr will
raise an AssertionError.
c           	     N   > ST STR                    ST ST S3	[        U 5      -   $ )NzFailed running r   z(*z, **z):
)r  r   )r  r\  r]  r  rN  s    rT   make_error_message$run_node.<locals>.make_error_message
  s4    $RD$++bd6($ORUVWRXXXrS   r
  Nr  r   r8   r  get_attrplaceholderr  r  r  )rN  r  r  rm  output_graphget_submoduler  r  r5  r;  r  rE  r  r  r  AssertionError)	tracerr  r\  r]  r  r  r  r  rN  s	    ```    @rT   r  r  
  s}    
B	$		Y 	Y	_${{D3F3 
 	 }$tAw4d12hI&I 
 	 }$+++00 
 	 z!**88E 
 	 }$&$))333yy1# 
 	 % 
 : 
 $%CD 	=*,Q/!< 	1!45DD	1 
 	sX   
E5D'D9D*D	&D9E5E2D50E55E2+E--E22E55
Fc                  ^ SSK Jn  TR                  nX;   a  X0   $ U R                  n[        R
                  R                  R                  U R                  U R                  4U4S j5      u  pVUS:X  a)  SU R                  ;   a  U R                  S   R                  $ US:X  aS  TR                  R                  U R                     n[        U5      (       d  [         R"                  " U5      nOU" U0 UD6  OSn [%        TXXg5      nXU '   U$ ! [&         a0  n	U" [)        U	5      5      R+                  U	R,                  5      SeSn	A	ff = f)z
Run the actual computation represented by `node` and return the result.
This will execute any dependent nodes in the graph as well.
r8   )r  c                   > [        U T5      $ ri   )get_real_value)rQ  r  s    rT   r    get_real_value.<locals>.<lambda>A
  s    .F+rS   r  graphargr  N)r;  r  real_value_cacherN  r   r    r  r  r\  r]  r  exampler  r  r  r  r  rA  r  r  r   r  r  )
r  r  r  cacherN  r\  r]  	nn_module
real_valuer  s
    `        rT   r  r  3
  s&   
 '##E}{	B88==((	DKK +LD
 
]zTYY6yy$,,,	]''224;;?	i((i0I t&v&	Rfd&D
 d   RA'66qGTQRs   :D 
E+EEc                  ^ SSK JmJn  U4S jnU R                  5        H%  u  p4U" U5      (       d  M   SU SU" U5       35       e   U R	                  5        H%  u  p5U" U5      (       d  M   SU SU" U5       35       e   g )Nr   )FakeTensorConfigr=   c                n   > TR                   (       a#  SS KnSUR                  " U R                  5       3$ g)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)rh  r  r  s     rT   stack_or_hint7assert_no_fake_params_or_buffers.<locals>.stack_or_hint]
  s/    !!89N9Nq~~9^8_``crS   zUnexpected fake buffer r   zUnexpected fake param )torch._subclasses.fake_tensorr  r=   named_buffersnamed_parameters)r  r=   r  r\   bufferr  r  s         @rT    assert_no_fake_params_or_buffersr  Z
  s    Gd ((*
 
 	D$TF!M&,A+BC	D 
 + **,
 
 	B#D6=+?*@A	B 
 -rS   c                8    U R                    SU R                   3$ )z1
Returns the fully qualified name of the object.
r'  )rL   rM   r_  s    rT   fqnr  o
  s!     nnQs//011rS   c                \    [         R                  R                  R                  (       a  U $ U$ ri   )r   r  r9   assume_static_by_default)count1count2s     rT   ifdynstaticdefaultr  v
  s    }}44rS   c           
     P   [        [        R                  " [        R                  R	                  [        [        U R                  5      5      5      5       HN  nUR                  S5      (       d  M  US   S:w  d  M&  [        R                  " U R                   SUSS  35        MP     g)z8
Ensure all the files in a given submodule are imported
z.pyr   r7  r'  N)r  r  listdirr$  rB  r
   r   __file__r   	importlibimport_modulerK   )r  rG  s     rT   import_submoduler"  }
  st     2::bggood36M&NOPU##s(:##s||nAhsm_$EF QrS   c                *    [        [        U 5      5      $ ri   )class_has_getattributer9  r  s    rT   object_has_getattributer%  
  s    !$u+..rS   c                     [        [        R                  " U S5      [        R                  5      (       a  g g! [
         a     gf = f)N__getattribute__TF)r  r  r  r  r  AttributeErrorrk   s    rT   r$  r$  
  sT    ""3(:;
 
 	
   s   49 
AAc                     [         R                  " [        U 5      S5      nU(       a)  U[        R
                  R                  R                  L a  S nU$ ! [         a    S n N@f = f)N__getattr__)r  r  r9  r(  r   r  r  r*  )r   ignore_nn_module_getattr
getattr_fns      rT   get_custom_getattrr-  
  sX    ++DKG
  J%((//2M2M$M
  
s    A A#"A#c                       \ rS rSrSrSrSrSrg)TensorStaticReasoni
  rI   rg  r   r;   N)rK   rL   rM   rN   	PARAMETER
NOT_TENSORNN_MODULE_PROPERTYrR   r;   rS   rT   r/  r/  
  s    IJrS   r/  c                    U [         R                  :X  a  gU [         R                  :X  a  gU [         R                  :X  a  g[	        SU  35      e)Nz>mark_dynamic on parameter, parameters are always static today.z2mark_dynamic on a non tensor, how did this happen?z4tensor is static because it is nn module associated.zIllegal reason )r/  r0  r1  r2  r  )r<  s    rT   tensor_static_reason_to_messager4  
  sJ    #---O#...C#666E
?6(3
44rS   c                   SSK Jn  UR                  5       R                  5       (       d#  UR                  5       R	                  5       (       a'  [
        R                  (       a  S[        R                  4$ [        U 5      [        R                  R                  L d  U" U5      (       a'  [
        R                  (       a  S[        R                  4$ U(       d  S[        R                  4$ g)a   
Given a tensor, source, and is_tensor flag, determine if a shape should be static.

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

Returns a tuple, where the first element is the bool of whether or not this tensor should have a static shape.
The second element is a TensorStaticReason, useful for passing to tensor_static_reason_to_message if needed.
r8   ))is_from_unspecialized_param_buffer_sourceT)FN)sourcer6  guard_sourceis_specialized_nn_module"is_unspecialized_builtin_nn_moduler9   &force_nn_module_property_static_shapesr/  r2  r9  r   r  r  force_parameter_static_shapesr0  r1  )r]  	is_tensortensor_sourcer6  s       rT   tensor_always_has_static_shaper?  
  s      B 	""$==??%%'JJLL

7
7':::: 	V***4]CC

.
.'1111'2222rS   c                *   ^ ^ U U4S jn[        U5      $ )Nc                   >  SSK J n   TR                  R
                   Vs/ s H;  oR                  UR                  UR                  UR                  UR                  /PM=     nnU " U/ SQS9n[        TTR                  R                  R                  U5      $ ! [         a    S[        [        TT5      5      -   s $ f = fs  snf )Nr   )r   zkTabulate module missing, please install tabulate to log the graph in tabular format, logging code instead:
)opcoder\   r  r\  r]  r   )r   r   r   r/   graphrM  rN  r\   r  r\  r]  r.   forward__code__r  )r   rQ  
node_specs	graph_strr   r  s       rT   inner(lazy_format_graph_tabular.<locals>.inner
  s    	) AC
@N1TT166188QVVQXX6 	 
  N
	 "'2::+>+>+J+JIVV  	~,Wb9:;	
s   B AC"B>=B>r6   )r   r  rH  s   `` rT   lazy_format_graph_tabularrJ  
  s    W" erS   c                h    U  SU SU SU S[         R                  " U5      R                  5        S3
$ )Nr   z line z 
r   )disBytecode)prefixr\   rG  line_nor<  s        rT   format_bytecoderP  
  s;    XQtfAhZvgYc#,,t:L:P:P:R9SSUVVrS   _forward_pre_hooks_forward_hooks_backward_pre_hooks_backward_hooks)_state_dict_pre_hooks_state_dict_hooks_load_state_dict_pre_hooks_load_state_dict_post_hooksc                     [        [        R                  R                  R                  R
                  5      =(       d7    [        [        R                  R                  R                  R                  5      $ ri   )r  r   r  modulesrq  _global_backward_hooks_global_backward_pre_hooksr;   rS   rT   nn_module_has_global_hooksr]  
  sM     uxx&&==> #::C rS   c                ~   / nU(       + =(       a    U(       + =(       a    U(       + nU(       d  U(       a  UR                  [        5        U(       d  U(       a  UR                  [        5        U(       a  UR                  [        5        / nU H-  n[	        X/ 5      nU H  n	X   n
UR                  U
5        M     M/     U$ )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_namesrm  r   )r  check_forward_hookscheck_backward_hookscheck_state_dict_hookshook_dicts_to_checkcheck_all_hooks	all_hookshook_dict_namehooks	hook_namer  s              rT   nn_module_get_all_hooksrl  
  s      	'$$	'&& 
 o""#56""#67""#89I-R0I#DT"  . rS   c                0    [        U UUUS9n[        U5      $ )zD
Helper function to check if a module has any hooks attached to it.
)rc  rd  re  )rl  r
  )r  rc  rd  re  rj  s        rT   nnmodule_has_hooksrn     s&     $/15	E ;rS   c                T   [        U 5      (       a  U $ [        U [        R                  5      (       a  [	        U R
                  5      $ [        U [        R                  5      (       a  U R                  SS9$ [        U [        [        45      (       a  [        U 5      " S U  5       5      $ U $ )z0Convert tensor and tnp.ndarray to numpy.ndarray.T)forcec              3  8   #    U  H  n[        U5      v   M     g 7fri   )to_numpy_helperr   r  s     rT   r   "to_numpy_helper.<locals>.<genexpr>;       A5C?3//5r  )r=   r  tnpr  rr  r]  r   r  numpyr  r   r9  r  s    rT   rr  rr  2  s}    u~~%%%u||,,	E5<<	(	({{{&&	EE4=	)	)E{A5AAArS   c                >   [         c   e[        U [         R                  5      (       a  [        R                  " U 5      $ [        U [
        R                  5      (       a  U R                  $ [        U [        [        45      (       a  [        U 5      " S U  5       5      $ U $ )zeConvert tnp.ndarray to tensor, leave other types intact. If a list/tuple, loop through it to convert.c              3  8   #    U  H  n[        U5      v   M     g 7fri   )numpy_to_tensorrs  s     rT   r   "numpy_to_tensor.<locals>.<genexpr>H  ru  r  )
r  r  r  r   r  rv  r]  r  r   r9  r  s    rT   rz  rz  @  sr    >>%$$u%%%%%||	EE4=	)	)E{A5AAArS   c                  *    \ rS rSrS rSS jrS rSrg)numpy_to_tensor_wrapperiM  c                L    Xl         SU R                   R                  -   U l        g Nwrapped_rI  rK   )r   rI  s     rT   r    numpy_to_tensor_wrapper.__init__N  s    "TVV__4rS   c                6    SU R                   R                   S3$ )Nz<Wrapped function <original >>r  r   s    rT   __repr__ numpy_to_tensor_wrapper.__repr__R  s    -dffoo->bAArS   c                <    U R                   " U0 UD6n[        U5      $ ri   )rI  rz  r   r\  r]  r   s       rT   r   numpy_to_tensor_wrapper.__call__U  s     ffd%f%s##rS   )rK   rI  Nr  r   )rK   rL   rM   rN   r   r  r  rR   r;   rS   rT   r}  r}  M  s    5B$rS   r}  c                   [        U [        R                  5      (       a  [        X5      n[	        U5      $ [        U [
        R                  5      (       a+  [        [        R                  " U 5      U5      n[	        U5      $ g ri   )r  rv  r  rm  rz  r   r  )r  r\   r   s      rT   numpy_attr_wrapperr  Z  s^    #s{{##c s##	C	&	&ckk#&-s## 
'rS   c                  2    \ rS rSrSrSS jrS	S jrS rSrg)
numpy_method_wrapperic  zgConvert obj from torch.Tensor to tnp.ndarray and call method. Then convert result back to torch.Tensor.c                8    Xl         SU R                   -   U l        g r  )methodrK   )r   r  s     rT   r   numpy_method_wrapper.__init__f  s    "T[[0rS   c                "    SU R                    S3$ )Nz<Wrapped method <original r  )r  r   s    rT   r  numpy_method_wrapper.__repr__j  s    +DKK=;;rS   c                    US   n[        U[        R                  5      (       a  [        R                  " U5      n[        X0R                  5      nU" USS  0 UD6n[        U5      $ Nr   r8   )r  r   r  rv  r  rm  r  rz  )r   r\  r]  r  method_callabler   s         rT   r  numpy_method_wrapper.__call__m  sX    1gc5<<((++c"C!#{{3tABx262s##rS   )rK   r  N)r  r   r  	rK   rL   rM   rN   r|  r   r  r  rR   r;   rS   rT   r  r  c  s    q1<$rS   r  c                  2    \ rS rSrSrSS jrS	S jrS rSrg)
numpy_operator_wrapperiv  zQImplements dunder methods for tnp.ndarray via functions from the operator libraryc                8    Xl         SUR                   3U l        g r  )rN  rK   )r   rN  s     rT   r   numpy_operator_wrapper.__init__y  s    "2;;-0rS   c                "    SU R                    S3$ )Nz<Wrapped operator <original r  )rK   r   s    rT   r  numpy_operator_wrapper.__repr__}  s    -dmm_B??rS   c                Z    U(       a   eS U 5       nU R                   " U6 n[        U5      $ )Nc              3     #    U  H<  n[        U[        R                  5      (       a  [        R                  " U5      OUv   M>     g 7fri   )r  r   r  rv  r  r  s     rT   r   2numpy_operator_wrapper.__call__.<locals>.<genexpr>  s2      
RV3
3 = =CKK3FRVs   AA)rN  rz  r  s       rT   r  numpy_operator_wrapper.__call__  s3    z
RV
 ggtns##rS   )rK   rN  N)rN  zCallable[..., Any]r  r  r;   rS   rT   r  r  v  s    [1@$rS   r  c                r   [        U [        5      (       d  U $ U R                  (       Ga'  / nU R                  5        H}  n[        U[        R
                  5      (       aJ  UR                  UR                  R                  R                  UR                  R                  5      5        Ml  UR                  U5        M     / nU R                  5        H}  n[        U[        R
                  5      (       aJ  UR                  UR                  R                  R                  UR                  R                  5      5        Ml  UR                  U5        M     O U R                  5       nU R                  5       n[        R                  " UUU R                  U R                  U R                  S9nUR!                  5         U$ )N)r	  r  r  )r  r<   _has_symbolic_sizes_stridesr&  r   SymIntr   r  r  	size_hintexprr  empty_stridedr	  r  r  zero_)r   r&  r  r  r  s        rT   defaker    s,   a$$ 	$$$A!U\\**AFF,,66qvv{{CDA	 
 A!U\\**aff..88EFa 	  vvxggxxoo	A GGIHrS   c                L    SS K nXR                  R                  R                  L $ r   )torch.utils.checkpointrz  
checkpoint)r  r   s     rT   is_utils_checkpointr    s    !++((3333rS   c                    SSK Jn  XL $ )Nr   )invoke_subgraph_placeholder)'torch._higher_order_ops.invoke_subgraphr  )r  r  s     rT   is_invoke_subgraphr    s    S--rS   c                 d    SSK Jn  UR                  " [        R                  R
                  40 U D6$ )Nr8    TorchHigherOrderOperatorVariable)variables.higher_order_opsr  maker   _higher_order_opsinvoke_subgraph)optionsr  s     rT   build_invoke_subgraph_variabler    s/    L+00//
 rS   c                     SS K Js  Jn  SSKJn  UR
                  n[        R                  R                  R                  (       a  UR                  nUR                  " U40 U D6$ )Nr   r8   r  )torch._higher_order_ops.wrapr  wrapr  r  tag_activation_checkpointr   
_functorchr9   functionalize_rng_opswrap_activation_checkpointr  )r  higher_order_opsr  activation_checkpoint_ops       rT   build_checkpoint_variabler    s\    ;;L
 	22  44#3#N#N +00 
 rS   c                h    SSK Jn  U" 5       nU S:X  a   U$ U S:X  a  U(       a  [        5       nU$ SnU$ )Nr8   )is_dynamo_supportedr  r  F)
eval_framer  r2   )device_typer  compile_supporteds      rT   is_compile_supportedr    sL    /+-e
 	 
	#4&L  "rS   c                \    U R                  S5      n[        USU R                  SSS95      $ )z
Convert byte offset `offset` of `str` into character offset.
Byte offset is used for 3.11+ instruction column data.
Takes things like unicode characters into consideration.

Unchanged from CPython implementation.
zutf-8Nr,  )errors)encoder  decode)r   offsetas_utf8s      rT   _fix_offsetr    s4     jj!Gww&&wy&ABBrS   c                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S'   Srg)	_Anchorsi  r{   left_end_linenoleft_end_offsetright_start_linenoright_start_offsetr;   Nrz  r;   rS   rT   r  r    s     rS   r  c                  ^^ [         R                  S:  d   eSSKn UR                  SU -   S-   5      n[        UR                  5      S:w  a  gU R                  S5      mU4S jnU4S	 jmUU4S
 jnUU4S jnUR                  S   n[        XaR                  5      (       Ga  UR                  n[        XqR                  5      (       a  [        [        UR                  R                  5      S-
  nU" XR                  R                   5      n	T" X5      u  pTU   U	   =n
R#                  5       (       d  U
S;   aB  U
S;   a  U" X5      u  pO
U" X5      u  pTU   U	   =n
R#                  5       (       a  M:  U
S;   a  MB  U	S-   nU[        TU   5      :  a(  TU   U   =n
R#                  5       (       d  U
S;  a  US-  n[%        XX5      $ [        XqR&                  5      (       a  [        [        UR                  R                  5      S-
  nU" XR                  R                   5      nT" X5      u  pTU   U   S:w  a  U" X5      u  pTU   U   S:w  a  M  [        [        UR                  5      S-
  nU" XR                   5      n[%        XX5      $ [        XqR(                  5      (       a  [        [        UR*                  R                  5      S-
  nU" XR*                  R                   5      nT" X5      u  pTU   U   S:w  a  U" X5      u  pTU   U   S:w  a  M  [        [        UR                  5      S-
  nU" XR                   5      n[%        XX5      $ g! [         a     gf = f)z
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
)rJ      r   Nz(
z
)r8   r   c                "   > [        TU    U5      $ ri   )r  )linenor  liness     rT   	normalize-_extract_anchors_from_expr.<locals>.normalize  s    5=&11rS   c                   > U [        T5      :  a<  U[        TU    5      :  a*  SnU S-  n U [        T5      :  a  U[        TU    5      :  a  M*  U [        T5      :  a  U[        TU    5      :  d   eX4$ r  r  )r  colr  s     rT   next_valid_char3_extract_anchors_from_expr.<locals>.next_valid_char  sw    s5z!cSv-?&?CaKF s5z!cSv-?&? E
"sSv-?'???{rS   c                n   > US-  nT" X5      u  pU [        T5      :  a  U[        TU    5      :  d   eX4$ r   r  r  r  r  r  s     rT   r   -_extract_anchors_from_expr.<locals>.increment!  sB    q%f2E
"sSv-?'???{rS   c                r   > SnU S-  n T" X5      u  pU [        T5      :  a  U[        TU    5      :  d   eX4$ r  r  r  s     rT   nextline,_extract_anchors_from_expr.<locals>.nextline(  sG    !%f2E
"sSv-?'???{rS   rI   z)\#z\#r!  r#  )r  r  astparseSyntaxErrorr  bodyr  r  Exprr   BinOpr
   r{   left
end_linenoend_col_offsetisspacer  	SubscriptCallr  )segmentr  treer  r   r  	statementr  
cur_linenocur_colch	right_colleft_linenoleft_colright_linenor  r  s                  @@rT   _extract_anchors_from_exprr    s.    w&&& yy501 499~MM$E2
 		!I)XX&&dII&& c499#7#781<J
II,D,DEG"1*"FJ z*7332<<>>",;*2:*G'J*3J*H'J	 z*7332<<>>",  !ICj 122$Z0;;DDFFeOQ	 JGGmm,, sDJJ$9$9:Q>K jj.G.GHH$3K$J!K$X.#5(1+(H% $X.#5  T__59L!,0C0CDIK<KKhh''
 sDII$8$89A=K ii.F.FGH$3K$J!K$X.#5(1+(H% $X.#5  T__59L!,0C0CDIK<KKK  s   M 
M! M!c           	     d   [         R                  S:  Ga  [        R                  " U R                  UR
                  R                  U R                  UR
                  R                  UR
                  R                  UR
                  R                  S9n[        R                  " U/5      S   nSR                  UR                  5       SS 5      n[        UR
                  R                  UR
                  R                  S-   5       Vs/ s H2  n[        R                   " U R                  U5      R#                  5       PM4     nn[$        R&                  " SR                  U5      5      R                  5       n[)        US   5      [)        US   5      -
  nUS   SU n[$        R*                  " [$        R&                  " U5      U5      nU$ UR
                  c   eUR
                  R                  c  g[        R                   " U R                  UR
                  R                  5      R#                  5       n	UR
                  R                  c  U	$ UR
                  R                  b  UR
                  R                  c  U	$ [-        XR
                  R                  5      n
SnSn/ nUR
                  R                  UR
                  R                  :X  aA  [-        XR
                  R                  5      nXU nUR/                  SU
-  S	X-
  -  -   5        GOXS S-   nUR/                  SU
-  S	[)        U	5      U
-
  -  -   5        [        R                   " U R                  UR
                  R                  5      R#                  5       n[-        XR
                  R                  5      n[        UR
                  R                  S-   UR
                  R                  5       H  n[        R                   " U R                  U5      R#                  5       nXS-   -  n[)        U5      [)        UR1                  5       5      -
  nUR/                  SU-  S	[)        U5      U-
  -  -   5        M     XSU -  n[)        U5      [)        UR1                  5       5      -
  nUR/                  SU-  S	UU-
  -  -   5        Sn [3        U5      nUc$  U Vs/ s H  nUR7                  S	S
5      PM     nnGO=U Vs/ s H  n[9        U5      PM     nnUR:                  S:X  a  U=R<                  U
-  sl        UR>                  S:X  a  U=R@                  U
-  sl         [        [)        U5      5       H  n[        [)        UU   5      5       H  nUUR:                  :  a  M  UUR:                  :X  a  UUR<                  :  a  M7  UUR>                  :X  a  UUR@                  :  a  MY  UUR>                  :  a  Mk  UU   U   S	:X  d  My  S
UU   U'   M     M     U Vs/ s H  nSR                  U5      PM     nnSn[        [)        U5      5       HY  nU[        R                   " U R                  UR
                  R                  U-   5      R#                  5       S-   -  nX=U   S-   -  nM[     U$ s  snf ! [4         a     GNf = fs  snf s  snf s  snf )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).
)rJ      )r  colno	end_colnor   r   r8   Nrq  r   ~^)!r  r  r  FrameSummaryr  	positionsr  r8  r  
col_offsetr  r  r   
splitlinesr6  	linecachegetlinerstriptextwrapdedentr  indentr  r   lstripr  r  r,  r   r  r  r  r  )r<  instframe_summaryr1  r  
orig_linesorig_lines_dedent
indent_lenr  
first_linestart_offset
end_offsetr  markers	last_lineline
num_spacesanchorsmarkermutable_markersr  r  s                         rT   get_instruction_source_311r"  s  s    7"!..NN!!LL~~00..++nn33
 &&7:6,,.qr23   5 5t~~7P7PST7TU
U d..7>>@U 	 
 %OODIIj,ABMMOA'#.?.B*CC
A{
+!8&A>>%%%~~$ ""4#3#3T^^5J5JKRRTJ~~  (~~  (DNN,I,I,Q z>>+D+DEL JGG ~~  DNN$9$99 ^^-J-JK
*5s\)C:3L,MMN]+d2s\)C3z?\3Q,RRS%%dnn77

&( 	 !NN,I,IJ
DNN11A5t~~7P7PQF$$T%5%5v>EEGDd{"GTS%77JNN3+cSY5K.LLM R 	[j))^c)*:*:*<&==
sZ'#j1H*IIJ"&G,W5
 :AB'6>>#s+'B HO+OwVDLw+O ""a'##|3#%%*&&,6& CL)FS!89:G333W444w?V?V9Vg888w999G666"6*3/3636OF+C0 ; *" 2AAv2776?AF3w< d..0E0E0IJQQS	
 	!*t## ! MI
r  
 C ,P4 Bs*   9Z)Z ;Z#Z(;Z-
Z Z c                \    [        U [        R                  5      (       a  [        U SS 5      $ g )N_dynamo_static_input_type)r  r   r  rm  r^  s    rT   get_static_address_typer%    s&    !U\\""q5t<<rS   c                   [         R                  R                  R                  [         R                  R                  [         R
                  [         R                  R
                  4n[         R                  R                  R                  [         R                  R                  [         R                  [         R                  R                  4nU / UQUQ7;   $ ri   )	r   r?  r   	get_statedefault_generatorrD  r  	set_staterF  )r   getterssetterss      rT   is_rng_state_getter_or_setterr,    s     	$$))

  G 	$$))

  	G (g((((rS   c                    [        U [        R                  5      =(       aF    U R                  S:H  =(       a0    U R                  R
                  [        R                  R                  L $ )N__get__)	r  r  r   rK   __self____objclass__r   r?  _TensorBaser  s    rT   is_tensor_base_attr_getterr2    sK    5%112 	@NNi'	@NN''588+?+??rS   c                    [        U S5      $ )N__torch_function__)r  r  s    rT   is_torch_function_objectr5    s    5.//rS   c                   SSK Jn  SSKJn  U R	                  5       (       d0  [        U S5      (       aX  [        U R                  5       S5      (       a9  [        X5      (       a  g[        X5      =(       a    [        U R                  S5      $ g)Nr   )UserDefinedObjectVariable)TensorWithTFOverrideVariable
peek_valuer4  TF)	torch._dynamo.variablesr7  &torch._dynamo.variables.torch_functionr8  is_realizedr  r9  r  r   )r   r7  r8  s      rT   has_torch_functionr=    sm    AS 
~~L!!gbmmo?S&T&Tb77"8 
WHH*>
 	
 rS   c                    S nS n[         R                  R                  R                  5       =n(       a*  XR                  ;   a  UR                  U    nUR
                  nUR                  U SX#S9$ )NF)static_shapessymbolic_contextr7  )r   r  r(   try_gettensor_to_contextr>  from_tensor)rh  r  r@  r7  tracing_contexts        rT   to_fake_tensorrE  5  ss    F--66>>@@@111.@@C%33F  	1A !  rS   c                   [        U 5      (       + =(       ao    [        U 5      (       + =(       aX    [        U 5      =(       aF    [        U S5      =(       a3    [        U R                  S5      =(       a    U R                  R
                  $ )N__dataclass_params__frozen)r%  r$  r   r  rG  rH  r  s    rT   is_frozen_dataclassrI  C  sn    #E** 	.&u--	.	. E12	. E..9		.
 &&--rS   c                n    U H   n[        X5      (       d  M  [        X5      s  $    [        U  SU 35      e)zP
Return the first available attribute or throw an exception if none is present.
z% does not has any of the attributes: )r  rm  r  )r  attrsattrs      rT   get_first_attrrM  N  s=     33%%  C5 EeWM
NNrS   c              #     ^^#    U (       d  S v   g UU4S jn[         R                  R                  R                  U5       nUv   S S S 5        g ! , (       d  f       g = f7f)Nc                6   > S n[         R                  " XTTS9$ )Nc                    [         R                  R                  R                  S   S==   S-  ss'   [         R                  R                  X5      $ )Ncompiled_autogradcompilesr8   )r   r  rz  r@   rN  compile)gm_example_inputs_s     rT   inner_compilerKmaybe_enable_compiled_autograd.<locals>.compiler_fn.<locals>.inner_compiler`  s>    ##,,-@A*MQRRM..sDDrS   )r  	fullgraphdynamic)r   rS  )r  rV  rY  rX  s     rT   compiler_fn3maybe_enable_compiled_autograd.<locals>.compiler_fn_  s#    E ==i rS   )r   r  rQ  _enable)should_enablerX  rY  rZ  r  s    ``  rT   maybe_enable_compiled_autogradr^  Y  s>     	 ]],,44[ASI BAAs   =A A	A 
AA c                 @     " S S[         5      n [        U " 5       5      $ )Nc                      \ rS rSrSrg)*invalid_removeable_handle.<locals>.Invalidin  r;   N)rK   rL   rM   rN   rR   r;   rS   rT   Invalidra  n  s    rS   rb  )r4  r4   )rb  s    rT   invalid_removeable_handlerc  l  s    $  79%%rS   c                   [        U [        R                  R                  5      (       d  U $ [        U [        R                  R
                  5      (       a  U $ U R                  R                  U R                  5      nU R                  Ul        U$ ri   )	r  r   r  r  r    GraphModulerF  rk  r  )r  proxys     rT   nn_module_proxyrg  x  s`    c588??++
#uxx++,,
MM!!#--0E\\ENLrS   c                  .   ^  \ rS rSrU 4S jrS rSrU =r$ )	GmWrapperi  c                :   > [         TU ]  5         Xl        X l        g ri   )rD  r   r  unflatten_fn)r   r  rk  rF  s      rT   r   GmWrapper.__init__  s    (rS   c                T    [        U5      nU R                  " U R                  U5      6 $ ri   )r   r  rk  r  s     rT   rD  GmWrapper.forward  s%    t*ww))$/00rS   )r  rk  )rK   rL   rM   rN   r   rD  rR   rH  rI  s   @rT   ri  ri    s    )
1 1rS   ri  c                  ^	^
^^ [        U R                  R                  5       VVs/ s H<  u  p4UR                  S:X  d  M  UR                  R                  SS5      (       d  M:  UPM>     snnm[        R                  R                  R                  (       aS  TS/:X  d   e[        US   [        5      (       d   e[        US   5      m	S mU	4S jnU" [        X5      T" U5      5      m
O][        R                  " U5      u  pg[         R"                  " [        R$                  US9nU" [        X5      U5      m
[        R&                  mU
UU4S jnU$ s  snnf )	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.
r  	steal_argFr   c                *    U S   [        U SS  5      -   $ r  )r   r  s    rT   
flatten_fn(flatten_graph_inputs.<locals>.flatten_fn  s    7T$qr(^++rS   c                   > U S T /U TS  Q7$ ri   r;   )	flat_argsboxed_inputs_counts    rT   rk  *flatten_graph_inputs.<locals>.unflatten_fn  s#    112TY?Q?R5STTrS   )treespecc                 Z   > T" U 5      nT H  nX   R                  5         M     T" U5      $ ri   )rj   )r\  ru  r  compiled_fnrr  inputs_idx_to_clears      rT   wrapper%flatten_graph_inputs.<locals>.wrapper  s1    t$	 %AGMMO % 9%%rS   )	enumeraterC  rM  rN  r  rn   r   r  rQ  in_compiled_autograd_regionr  r   r  ri  r  tree_flattenr  r  tree_unflattenarg_tree_leaves)r  inputs
compile_gmr  r  rk  flat_inputsspecr|  rv  rz  rr  r{  s            @@@@rT   flatten_graph_inputsr    s    !00GA77m# 	
(,		k5(I 	
0 }}&&BB #qc)))&)T**** ^	,	U !2!<j>PQ #//7 (()>)>N 2!<kJ++
& NMs   EE$Ec                    [        U [        R                  R                  5      (       a  [	        U S5      (       d  / $ U R
                  R                  S/ 5      $ )Nr  locals_to_steal)r  r   r    re  r  r  rn   )maybe_gms    rT   get_locals_to_stealr    sB    h 4 455WXv=V=V	==.33rS   c                     XR                   S'   g )Nr  )r  )r  r  s     rT   set_locals_to_stealr    s    !0GGrS   c                  $    \ rS rSrS rSS jrSrg)Liti  c                    Xl         g ri   r  )r   r  s     rT   r   Lit.__init__  s    rS   c                    U R                   $ ri   r  r   s    rT   r  Lit.__repr__  s    vvrS   r  Nr  )rK   rL   rM   rN   r   r  rR   r;   rS   rT   r  r    s    rS   r  zSet[str]warn_once_cachec                r    U [         ;   a  g [         R                  U 5        [        R                  " XS-   S9  g )Nr8   )
stacklevel)r  r  warningswarn)r=  r  s     rT   	warn_oncer    s-     oMM#q.1rS   c                R    [         R                  " S5      nUR                  SU 5      $ )Nz\x1B[@-_][0-?]*[ -/]*[@-~]rq  )rP  rS  r;  )textansi_escapes     rT   strip_color_from_stringr    s"    **:;K??2t$$rS   c               #    #     [         R                  R                  R                  S5      n S v   [         R                  R                  R                  U 5        g ! [         R                  R                  R                  W 5        f = f7f)NT)r   r?  	_autograd _saved_tensors_hooks_set_tracing)r  s    rT   +_disable_saved_tensors_hooks_during_tracingr    sX     C""CCDI;;EB;;EBs   B	-A *B	+BB	c                     [         R                  R                  R                  =(       a    [         R                  " 5       (       + $ ri   )r   rN  r9   freezingis_grad_enabledr;   rS   rT   is_parameter_freezingr    s)    ??!!**J53H3H3J/JJrS   c                 f    [        [        5       5       V s/ s H  n [        U 5      PM     sn $ s  sn f ri   )r6  r"    get_torch_function_mode_stack_atr  s    rT   get_torch_function_mode_stackr    s3    5:;T;V5W5W(+5W  s   .c                l    U [        5       :  a  U S:  d   e[        R                  R                  U 5      $ r   )r"   r   r?  _get_function_stack_at)inds    rT   r  r    s/    *,,9988**3//rS   c                t    [        [        5       5       H  n[        5         M     U  H  n[        U5        M     g ri   )r6  r"   r#   r$   )r  r  modes      rT   set_torch_function_mode_stackr    s/    ,./!# 0 %d+ rS   c                 L    [        [        5       5       H  n [        5         M     g ri   )r6  r"   r#   r  s    rT   clear_torch_function_mode_stackr    s    ,./!# 0rS   c                     [        5         g ri   )
breakpointr  s    rT   _breakpoint_for_c_dynamor    s    LrS   c                    U R                   n[        R                  " U5      n[        UR                  5      S:w  a  SSKJn  U" S5      eUR                  U R                  :w  a  SSKJn  U" S5      eg )NrI   r8   )InternalTorchDynamoErrorz[Tensor subclass method __metadata_guard__ must take exactly two subclass metadata argumentsz?Tensor subclass method __metadata_guard__ must be a classmethod)	__metadata_guard__r  	signaturer  r  r;  r  r/  rF  )r   r  sigr  s       rT   verify_guard_fn_signaturer    sk    		!	!B


B
C
3>>a1&i
 	
 
{{eoo%1&M
 	
 &rS   c                   U R                   [        R                   [        R                  R                   4;   =(       a    U R                  [        R                  [        R                  R                  4;   =(       au    U R
                  [        R
                  [        R                  R
                  4;   =(       a7    U R                  [        R                  [        R                  R                  4;   $ ri   )r   r4  r|   r   r   r   __iter__)user_clss    rT   #does_not_override_dict_iter_methodsr     s    4::{'>'>'D'DEE 	SOO[-D-D-K-KLL	SMMdii)@)@)E)EFF	S $--1H1H1Q1Q!RR	rS   c                X    [         R                  R                  SS9S 5       nU" X5      $ )NT	recursivec                $    U R                  U5      $ ri   )r&  r   r  s     rT   r  call_size.<locals>.fn.  s    vvayrS   r   r  disabler   r  r  s      rT   	call_sizer  -  s1    
]]T* + a8OrS   c                X    [         R                  R                  SS9S 5       nU" X5      $ )NTr  c                $    U R                  U5      $ ri   )r  r  s     rT   r  call_stride.<locals>.fn6  s    xx{rS   r  r  s      rT   call_strider  5  s1    
]]T* + a8OrS   c                X    [         R                  R                  SS9S 5       nU" U 5      $ )NTr  c                "    U R                  5       $ ri   )storage_offsetr   s    rT   r  call_storage_offset.<locals>.fn>  s    !!rS   r  )r   r  s     rT   call_storage_offsetr  =  s1    
]]T*" +" a5LrS   c                    SS/nU Vs0 s H8  o"U R                   ;   d  M  U[        R                  " U R                   U   5      _M:     nnU$ s  snf )Nr$  tag)r  r  )rh  KEYS_TO_COPYr   tensor_dicts       rT   _extract_tensor_dictr  G  s]    #L 4@3?C!**CT'TYYqzz#''<   	s
   A(Az(Dict[int, weakref.ReferenceType[object]]user_obj_id_to_weakrefc                6    [         U    " 5       nUc   S5       eU$ )NzUser object is no longer alive)r  )obj_idr  s     rT   get_user_object_from_idr  Z  s$    
 
(
*C?<<<?JrS   c                T    [        U 5      n[        R                  " U 5      [        U'   g ri   )rf  ru  rr  r  )r  r  s     rT   store_user_object_weakrefr  `  s    WF%,[[%56"rS   c                      \ rS rSr% SrS\S'   SrS\S'   Sr\SS j5       r	\SS j5       r
\SS	 j5       r\SS
 j5       r\\S 5       5       rSrg)CompileTimeInstructionCounterie  r   r{   _counterr  _idc                    U R                   S-   U l         U R                   S:X  a  [        R                  " 5       U l        g g r   )_depthr!   rt  r  rk   s    rT   rt  #CompileTimeInstructionCounter.startj  s1    ZZ!^
::?*002CG rS   c                    U R                   S-
  U l         U R                   S:X  a;  U =R                  [        R                  " U R                  5      -  sl        SU l        g g )Nr8   r   r  )r  r  r!   endr  rk   s    rT   r  !CompileTimeInstructionCounter.endp  sF    ZZ!^
::?LL044SWW==LCG rS   c                    SU l         g r   r  rk   s    rT   rj   #CompileTimeInstructionCounter.clearw  s	    rS   c                    U R                   $ ri   r  rk   s    rT   r   #CompileTimeInstructionCounter.value{  s    ||rS   c              #    #     [         R                  (       a  U R                  5         S v   [         R                  (       a  U R                  5         g g ! [         R                  (       a  U R                  5         f f = f7fri   )r9   %record_compile_time_instruction_countrt  r  rk   s    rT   record$CompileTimeInstructionCounter.record  sM     	;;		;;	 <v;;	 <s   A?)A 'A?(A<<A?r;   Nr  )r  r{   )rK   rL   rM   rN   r  r~   r  r  r   rt  r  rj   r   r   r  rR   r;   rS   rT   r  r  e  s    HcCMF3 3
         rS   r  c                t    [        5       R                  5       (       a  [        5       R                  SX5        gg)zE
Records whether we are using a feature
Generally a feature is a JK.
r5  N)r   r   set_key_value)featureusages     rT   set_feature_user    s.     ((**++OWL +rS   )r   z3Union[List[Tuple[str, object]], List[List[object]]]r   z!Union[Tuple[str, ...], List[str]]r  r   r  )r   r{   r  r	  )r  rD   )r  r&   )NFNNF)r   r   r   r  r   r
  r   zOptional[Dict[str, object]]r   r  r   r
  r  Generator[Any, None, None]r  )r   zLiteral['str']r   r
  r  r   )r   zLiteral['csv']r   r
  r  zTuple[List[str], List[object]])r   F)r   r
  r  )rG  r   r  r	  )rO  zfx.Graphr  r{   )r   r?   r  r?   )r  objectr  zType[T]r  	TypeIs[T])r  r  r  z)Tuple[Type[List[T]], Type[Tuple[T, ...]]]r  r  )r  r  r  zIterable[type]r  r
  ri   )r   r  r  r  )rP  r  r  r	  r  )
r  r{   r  r{   r^  rA   r  zOptional[Type[BaseException]]r  zOptional[BaseException])r  r{   r  r	  )r  zList[CompilationMetrics])r  r  )FF)r   r   r  r
  r   r
  r  r  )r  ztorch.Tensor)r  zTuple[str, ...])r8   )r  zTuple[int, int, int])T)r  r   )r  ztypes.ModuleType)r   r   )r]   r9  )r   r   r+  r
  )r<  r/  )r]  zUnion[torch.Tensor, Any]r=  r
  r>  r'   r  z)Tuple[bool, Optional[TensorStaticReason]])FFF)r   r   r  r{   r  r{   )r  r   r  zOptional[_Anchors])r<  ztypes.CodeTyper  zdis.Instructionr  r   )r   z,torch._dynamo.variables.base.VariableTrackerr  r
  )TT)r  ztorch.fx.GraphModule)r  r   r  r
  (  
__future__r   atexitr|   r  r  ru  r  rL  r  r  r  r   r  r   rL  r  r'  r  r  r  rP  r  r  r  r   r  r  r  r  r  ru  r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   torch._functorch.config%torch.fx.experimental.symbolic_shapestorch.utils._pytreerz  _pytreer  r    torch._Cr!   r"   r#   r$   torch._dispatch.pythonr%   torch._dynamo.metrics_contextr&   torch._guardsr'   r(   torch._subclasses.meta_utilsr)   torch._utils_internalr*   r+   r,   r-   torch.fx._utilsr.   r/   torch.monitorr0   torch.nn.modules.lazyr1   torch.utils._tritonr2   r3   torch.utils.hooksr4   rw  r  r~  torch._loggingtorch._numpy_numpyrv  r5   r7   rq  r9   fftlinalgrC  r:   r~   NP_TO_TNP_MODULEr  r<   r=   r>   r   r?   r  r  r*  unpatched_nn_module_getattrr  unpatched_nn_module_call
_call_implunpatched_nn_module_call_implr}   r@   rB   troubleshooting_urlnnmodule_doc_urlnnmodule_doc_url_msgr)  rK   ry   rC   rx  rE   re   timer_counterr  rG   rV   r   r   r   r   r   r   r   r   r   r   r   registerr   FloatTensorr  DoubleTensorr  double
HalfTensorr  halfBFloat16Tensorr  
ByteTensorr  
CharTensorr  
LongTensorr  long	IntTensorr  r{   ShortTensorr  short
BoolTensorr
  tensortype_to_dtyper   r  r  r  r  r5  r=  rJ  rR  rT  rY  r^  r`  r  r  ParamSpecArgsParamSpecKwargs	ParamSpecTypeVarTupleTypeAliasTyper  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r}  r  !DEFAULT_COMPILATION_METRICS_LIMITr  r:  r?  rW  r  r   r  r  r  r  r  r   r  r  r  r  r  r  r5  r=  rK  rU  r[  r]  rb  ra  r  r  r  r  r  r  r   r_   r9  EllipsisrF  NotImplementedCodeTyper  r	  memory_formatr  finfoiinfo	attention
SDPBackendr  _CudaDevicePropertiesr  r  r  languager  r  r  r  r  r  r  r   r  r   r  r   r  r  r  r6  r  __length_hint__tuple_iterator_lenr  rk  r  r  r  r  r  	iter_nextr  r  r  r  r  r  r  r  r  r  r3  GLOBAL_KEY_PREFIXtorch._subclassesr5  r:  r>  rD  rI  rY  rO  r  r  orig_code_mapr   r  r  seen_code_mapr  r%  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r%  r$  r-  r/  r4  r?  rJ  rP  r`  ra  rb  all_hook_namesr]  rl  rn  rr  rz  r}  r  r  r  r  r  r  r  r  r  r  r  r  r"  r%  r,  r2  r5  r=  rE  rI  rM  r^  rc  rg  ri  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;   rS   rT   <module>rA     s   "       
   	         	 	 
          % $  #     0 .   , $ $   < 8 0 =  G & 1 > -	.) 
FFIIII	>
: 	
 FFCGGIIszzIIszz	
  "VV
 CL#hhoo99  88??33  % : : +6+B+B;CVCV+W
( W$& > &G  Q .//UV ! 46 0 5
 .9-D-DU-K * K!tyy %J %JP

=
.
 	
 
   > !    !%"',0.2!^B	^B ^B  	^B
 *^B ,^B ^B  ^B ^BB 
 
 
,1
%)# 
" "J 8 8
 
u{{3	5	u}}ejj1	5>>+	u{{n	uzzm	u{{EJJ/	OOekk599-	U[[1	uzzm  ( #:"; ",
	?
G	 B 
 
 
	 I 
 
 
& w 	%!G&	$3%$, T 
05   F4 F4 F4R %' ! 3>2C2C,3 / 
#L&Ru3u3u3 u3 ,	u3
 'u3r "*DE %!
&} }@ 59 1 8  ""'


  
  	
 
@ ( ( (( +   )*  ! GT&
 9 9"	$(
< Q1 12,"X 2  		J	NN	LL	KK		LL	KK	KK	HH!!	JJ$$'$ y , foo334W
 "&bggi	 0%)"))+%6" 6&*;+B+B+D+K+K+M&N# N&*48n# 4&*4a>&:# :#33 ^^
1
 	58&RM
@B 
 !  = <7'?
; ii-2`FF	H
 
> 
> '( /:.E.Ed.K+ K LN H M '( T, ,&

/CL !1 " ".b$NB*2G/	  5$  /	D*W +,<= ,.?@   $&99<QQ  	F  	$

$ 
$$$ $&$ $(@4.&
"	C   vrAH)"04
O  $&1 1,^41   E !	2% C CK0
,$
"
  DF @ E6
# #LM M[q  	B>  		s%    p B!p- p*)p*-p76p7