
    Ph                     n   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlZd dlmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZm Z  dd	l!m"Z"m#Z#m$Z$m%Z% d
dl&m'Z'm(Z( d
dl)m*Z* d
dl+m,Z,m-Z-m.Z. d
dl/m0Z0  G d de(      Z1 G d de(      Z2 G d de2      Z3 G d de(      Z4 G d de2      Z5 G d de2      Z6 G d de(      Z7 G d de(      Z8 G d d e(      Z9 G d! d"e(      Z:d# Z;d$ Z<d% Z= G d& d'e(      Z>ej~                   G d( d)             Z@ G d* d+e0      ZA G d, d-e(      ZB G d. d/e(      ZC G d0 d1e(      ZD G d2 d3e(      ZE G d4 d5e(      ZF G d6 d7e(      ZG G d8 d9e(      ZH ej                  d
:      d;        ZJ G d< d=e(      ZK G d> d?e(      ZL G d@ dAe(      ZMy)B    N)DictList   )configpolyfill	variables)create_call_functioncreate_instructionunimplemented)GuardBuilderinstall_guard)
AttrSourceGetItemSourceODictGetItemSource
TypeSource)check_constant_argsidentityis_tensor_base_attr_getterproxy_args_kwargs   )MutableLocalVariableTracker)DefaultDictVariable)NestedUserFunctionVariableUserFunctionVariableUserMethodVariable)UserDefinedObjectVariablec                   X     e Zd Zd	 fd	Z fdZ fdZdeddfdZ	 	 	 	 	 	 d
dZ xZ	S )SuperVariablec                 N    t        |   di | || _        || _        || _        y N )super__init__typevarobjvarspecialized)selfr&   r'   r(   kwargs	__class__s        gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/variables/misc.pyr%   zSuperVariable.__init__#   s)    "6"&    c                      |t        j                  t                      || j                         | j                   || j                         t        dd      S t        dd      S )Nr   Tr   )r   BuiltinVariabler$   r&   r'   r	   )r)   codegenr+   s     r,   reconstructzSuperVariable.reconstruct)   sR    	))%01;;"DKK '400'400r-   c           	         | j                   sJ d       | j                  r$t        | j                  j	                         |      S | j                  j	                         }| j                   j                         }| j                   j                  rt        | j                   j                        nd }t        |t              r,| j                   j                  }| j                   j                  }d }| j                   j                  k|j                  }|j                  |      dz   }t        |t        |            D ]3  }	t        ||	   |      st!        t#        t!        |d      |	      |      } n t        t%        ||      |      |fS )Nz1-arg super not implementedr   __mro__)r'   r(   getattrr&   as_python_constantpython_typesourcer   
issubclasstypevaluer3   indexrangelenhasattrr   r   r$   )r)   txnamesearch_typetype_to_usetype_to_use_sourcer7   
search_mrostart_indexr;   r+   s             r,   _resolved_getattr_and_sourcez*SuperVariable._resolved_getattr_and_source2   sI   {{999{4<<::<dCCll557
 kk--/.2kk.@.@Jt{{))*d 	 k4(++++K!%!3!3;;) %,,J$**;7!;K{C
O<:e,d3'%j1CY&OQVWF  = u[+6=vEEr-   r@   returnr   c                 V   | j                  | |      \  }}t        j                  j                  |      st	        | |      S |rIt        |j                  t        j                               t        j                  j                  ||      S t        j                  j                  |      S Nr7   )
rF   r   ConstantVariable
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)r)   r?   r@   r:   r7   s        r,   var_getattrzSuperVariable.var_getattrU   s     99$Ev))44U;"4..&++L,G,GHI--44U64JJ))0077r-   c                    | j                  | |      \  }}|t        j                  u rt        t              S |t
        j                  j                  j                  u r| j                  }ddl	m
} t        |t        j                        r}t        |j                  |      rg|se|sc|j                  j                  j!                  |dt        j"                  j%                  d             t        j"                  j%                  d       S t'        d       y t        |t(        j*                        r6t        j,                  ||      j/                  || j                  g|z   |      S t        |t(        j0                        r=t        j2                  |j4                  | j                  |      j/                  |||      S |t6        j8                  j:                  u rt        | j                  t        j                        r| j                  j<                  rt?        |      dk(  rt?        |      dk(  r|d   jA                         rxdd	l!m"}	 |d   jG                         }
  |	|tI        | j                  j<                  |
            t6        j8                  j;                  | j                  jJ                  |
            S |t6        j8                  jL                  t        jN                  fv rt        | j                  t        jP                        r|rt        jR                  jU                  |d         r| j                  j                  r|st?        |      dk(  sJ t        jR                  jW                  |d         }tY        | j                  jZ                        }|d   ||<   |j]                  | j                  | j                  j_                  |            S t'        d
|        y )Nr   )AttributeMutationNew__call_nn_module_initTzsuper() nn.Module.__init__rJ   r   r   VariableBuilderznon-function or method super: )0rF   objectr%   LambdaVariabler   torchnnModuler'   side_effectsrS   
isinstancer   r   mutable_localoutput
store_attrrK   rP   r   typesFunctionTyper   call_function
MethodTyper   __func__collectionsOrderedDict__getitem__r7   r=   is_python_constantbuilderrV   r5   r   r:   __setitem____setattr__CustomizedDictVariableConstDictVariableis_valid_keyget_keydictitemsreplace_allmodifed)r)   r?   r@   argsr*   inner_fnr7   r'   rS   rV   keyknewvals                r,   call_methodzSuperVariable.call_methodf   s     <<T4H&v&!(++111[[F; 69#F#FGv335IJ		&&11+..55d;
 !1188>>:;%"4"4511mB 4f=> %"2"23//!!4;;vmBf-. //;;;4;;	(K(KL""D	QFq Q**,0q',,.CS?2'9$++:L:Lc'RS''33DKK4E4EsK  00<<f>P>PQQ4;;	(H(HI++88aA))#d)q.00++33DG<A$++++,FQF1I>>##F+ 
 :8*EFr-   )NFru   List[VariableTracker]r*   Dict[str, VariableTracker]rG   r   )
__name__
__module____qualname__r%   r1   rF   strrQ   rz   __classcell__r+   s   @r,   r    r    "   sS    '1!FF8C 8,= 8"CG &	CG
 -CG 
CGr-   r    c                       e Zd ZdZy)UnknownVariablez
    It could be anything!
    Nr~   r   r   __doc__r#   r-   r,   r   r          r-   r   c                       e Zd ZdZy)DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nr   r#   r-   r,   r   r      r   r-   r   c                   :    e Zd ZdZd ZdeddfdZ	 	 	 	 	 	 d	dZy)
ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    c                     t        d      )Nzcomptime is special form)NotImplementedErrorr)   r0   s     r,   r1   zComptimeVariable.reconstruct   s    !"<==r-   r@   rG   r   c                 j    ddl m } ddlm}  |t        ||      t	        | j
                  |            S )Nr   )comptimer   )r   rJ   )r   	functionsr   r4   r   r7   )r)   r?   r@   r   r   s        r,   rQ   zComptimeVariable.var_getattr   s.    ' 	4#Hd#Jt{{D,I
 	
r-   c                    ddl m} |rJ t        |      dk(  sJ |d   }t        |t              r |j                          ||             nt        |t              r|j                         }|j                  rJ d|j                          t        j                  ||j                  |j                  j                         |j                  rt!        |j                  j"                        nd t!                     } | ||             nt%        dt'        |             t(        j*                  j-                  d       S )Nr   )ComptimeContextr   r   zOcomptime function must not have free variables, but these variables were free: z"unsupported argument to comptime: )r   r   r=   r]   r   get_functionr   get_codeclosureco_freevarsra   rb   	f_globalsfn_namer5   defaultstuplerr   RuntimeErrorr9   r   rK   rP   )r)   r?   ru   r*   r   fncodefuncs           r,   rc   zComptimeVariable.call_function   s    	/ z4yA~~!Wb./BOOob1267;;=Dzz 2262B2B1CE> %%

--/,.KKbkk''(T D $%!CDH:NOO))0066r-   Nr{   )r~   r   r   r   r1   r   rQ   rc   r#   r-   r,   r   r      s=    
>
C 
,= 
#7/#79U#7	#7r-   r   c                   $     e Zd Z fdZd Z xZS )ClosureVariablec                 2    t        |   di | || _        y r"   r$   r%   r@   r)   r@   r*   r+   s      r,   r%   zClosureVariable.__init__       "6"	r-   c                 :    |j                  | j                        gS Ncreate_load_closurer@   r   s     r,   r1   zClosureVariable.reconstruct       ++DII677r-   r~   r   r   r%   r1   r   r   s   @r,   r   r          8r-   r   c                   $     e Zd Z fdZd Z xZS )InlinedClosureVariablec                 2    t        |   di | || _        y r"   r   r   s      r,   r%   zInlinedClosureVariable.__init__   r   r-   c                 :    |j                  | j                        gS r   r   r   s     r,   r1   z"InlinedClosureVariable.reconstruct   r   r-   r   r   s   @r,   r   r      r   r-   r   c                        e Zd Z fdZ xZS )NewCellVariablec                 $    t        |   di | y r"   r$   r%   r)   r*   r+   s     r,   r%   zNewCellVariable.__init__      "6"r-   r~   r   r   r%   r   r   s   @r,   r   r         # #r-   r   c                        e Zd Z fdZ xZS )NewGlobalVariablec                 $    t        |   di | y r"   r   r   s     r,   r%   zNewGlobalVariable.__init__
  r   r-   r   r   s   @r,   r   r   	  r   r-   r   c                   L     e Zd ZdZed        Zdef fdZdeddf fdZ	 xZ
S )	InspectSignatureVariablez!represents inspect.signature(...)c                 8    |rt        d|        t        |       S )Nzinspect.signature with )r   r   )callabler*   s     r,   rP   zInspectSignatureVariable.create  s     3F8<='11r-   	inspectedc                 2    t        |   di | || _        y r"   )r$   r%   r   )r)   r   r*   r+   s      r,   r%   z!InspectSignatureVariable.__init__  s    "6""r-   r@   rG   r   c                     |dk(  rJt        j                  | j                  j                         D ci c]  }|t	                c}t
              S t        |   ||      S c c}w )N
parameters)user_cls)r   rn   r   inspect_parameter_namesInspectParameterVariablerq   r$   rQ   r)   r?   r@   r+   s      r,   rQ   z$InspectSignatureVariable.var_getattr  so    <.. !% F F H H 244 H   w"2t,,s   A!)r~   r   r   r   staticmethodrP   r   r%   r   rQ   r   r   s   @r,   r   r     s=    +2 2
#/ #	-C 	-,= 	- 	-r-   r   c                       e Zd ZdZy)r   z2This is not implemented, if used will graph break.Nr   r#   r-   r,   r   r   '  s    <r-   r   c                 (      fd}t         |_        |S )Nc                  (     j                   | i |S r   )forwardru   r*   fn_clss     r,   trampoline_autograd_fwdz@produce_trampoline_autograd_fwd.<locals>.trampoline_autograd_fwd.  s    v~~t.v..r-   )produce_trampoline_autograd_fwd_origin)r   r   s   ` r,   r   r   -  s    / 'F#""r-   c                 (      fd}t         |_        |S )Nc                  (     j                   | i |S r   )backwardr   s     r,   trampoline_autograd_bwdz@produce_trampoline_autograd_bwd.<locals>.trampoline_autograd_bwd6  s    v///r-   )produce_trampoline_autograd_bwdr   )r   r   s   ` r,   r   r   5  s    0 'F#""r-   c                 (      fd}t         |_        |S )Nc                  (     j                   | i |S r   )applyr   s     r,   trampoline_autograd_applyzDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply>  s    v||T,V,,r-   )!produce_trampoline_autograd_applyr   )r   r   s   ` r,   r   r   =  s    - )J%$$r-   c                   >     e Zd ZdZ fdZd Zd Z	 	 	 	 ddZ xZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassc                 2    t        |   di | || _        y r"   )r$   r%   r   )r)   r   r*   r+   s      r,   r%   z!AutogradFunctionVariable.__init__H  s    "6"r-   c                    dfd}t        j                  |||f       t        j                        }|g|}rt	        j
                         rt        j                  r| j                  j                  t        j                  j                  j                  j                  k7  rt        d       | j                  j                  }|t        j                  j                  j                  urt        d       | j                  j                   }|t        j                  j                  j                   urt        d       ddlm}m}	 t)        | j                        }
t+        | j                        }t-        | j                        }j.                  j0                  j3                         }}t5        j7                  | j                  j8                        | j                  j:                        }t        j<                  j>                  jA                  j.                  j.                  jB                  d	      }|	jE                  |t5        |d
      |      }|jG                  ||      }tI        |tJ        jL                        r|g|jN                  }n||g} ||||	jE                  |t5        |d      |      |       |dd  }|	jE                  |
d       jG                  ||      S | jP                  r!t5        t5        | jP                  d      d
      }nd }| j                  jR                  }tI        |tT        jV                        r(tK        jX                  ||      jG                  ||      S tI        |tT        jZ                        rPtK        j\                  |j^                  tK        j`                  | j                        |      jG                  ||      S t        d|        y )NFc                     t        | t        j                        r| j                  durdt        | t        j                        r| j                        rd| S )NFT)r]   r   TensorVariablerequires_gradNNModuleVariableis_training)noder   r?   s    r,   visitz2AutogradFunctionVariable.call_apply.<locals>.visitO  sN    $	 8 89%%U2$(M$	 : :;##B'$(MKr-   z8NYI - autograd.Function with custom setup_context methodzNYI - User defind vjpzNYI - User defind jvpr   )safe_or_raise_always_restore TorchHigherOrderOperatorVariablezautograd.Function)parentsource_targetr   )r7   fwd_bwd_tracerr   )r   r+   rJ   z?non-function or method in subclass of torch.autograd.Function: )1r   r   AutogradFunctionContextVariablerP   rY   is_grad_enabledr   capture_autograd_functionr   setup_contextautogradfunction_SingleLevelFunctionr   vjpFunctionjvphigher_order_opsr   r   r   r   r   r_   graphcopy_graphstater   import_sourcer   r~   _dynamooutput_graphSubgraphTracercurrent_tracermakerc   r]   r   TupleVariablerr   r7   r   ra   rb   r   rd   r   re   UserDefinedClassVariable)r)   r?   ru   r*   r   ctxvjp_fnjvp_fnr   r   r   r   r   graph_checkpoint
checkpointmodule_sourcer   higher_order_autograd_fnspeculated_fwd_resultbwd_argsr7   r   r   s    `                    @r,   
call_applyz#AutogradFunctionVariable.call_applyL  sf   	 	edF^4-44R8|d| %%'00
 ))>>**??MMN N [[__FU^^4488856[[__FU^^4488856
 )J$++(V%&Edkk&R#&Edkk&R#( ,.99??B<N<N<Pj&  !7!78$++:N:NM #]]77FF		yy//1 G N
 (H'L'L'!-;- (M ($
 %=$J$JD&%! /1H1HI>"7"="=>!67( 055+%mZ@#1 6 
 
 8D388)# 9  mBf-.
 ;;
4;; DiPFF[[  b%,,-11"VDRRD&  E,,-//224;;? mBf-	. QRTQUVr-   c                 ,    t        | j                        S r   )r   r   r)   r?   ru   r*   s       r,   rc   z&AutogradFunctionVariable.call_function  s    '44r-   c           
         ddl m} ddlm} |dk(  rj || j                        rEt        | j                        } || |j                  j                  d|gt        ||             S | j                  |||      S |dk(  r$|j                         5  t        | j                  j                  t        j                        r t        | j                  j                        }nt        | j                  j                  t        j                         rXt#        | j                  j                  j$                  t'        j(                  | j                              }|j*                  g|z   }n"t-        d	| j                  j                          |j/                  |||      cd d d        S |d
k(  r
t        | j                  j0                  t        j                        r t        | j                  j0                        }	nt        | j                  j0                  t        j                         rXt#        | j                  j0                  j$                  t'        j(                  | j                              }	|	j*                  g|z   }n"t-        d| j                  j0                          |j/                  |	||      S t-        d|        y # 1 sw Y   y xY w)Nr   )is_user_defined_allowedr   )wrap_fx_proxyr   rc   )r?   proxyr   z&backward is a non-function or method: r   z%forward is a non-function or method: zUnsupported method: )allowed_functionsr  rj   r  r   r   r_   create_proxyr   r  strict_translation_moder]   r   ra   rb   r   rd   r   re   r   r  objr   inline_callr   )
r)   r?   r@   ru   r*   r  r  r   r   r   s
             r,   rz   z$AutogradFunctionVariable.call_method  s;    	@*7?&t{{3,MKK-) %0"))00'1 +48  r488Z++-dkk22E4F4FG3DKK4H4HIH 4 4e6F6FG1,,55!::4;;G H %LL>D0D!@AUAU@VW ~~b(D&A .-  Y$++--u/A/AB.t{{/B/BCDKK//1A1AB,KK''0066t{{C  }t+;DKK<O<O;PQ >>"gtV<< 078C .-s   D
KK)ru   r|   r*   r}   )	r~   r   r   r   r%   r  rc   rz   r   r   s   @r,   r   r   E  s0    7@D5;9 &	;9
 -;9r-   r   c                   D    e Zd ZU  ej                  e      Zee   e	d<   y)SavedTensorBox)default_factorytensorsN)
r~   r   r   dataclassesfieldlistr  r   r   __annotations__r#   r-   r,   r  r    s    %6[%6%6t%LGT/"Lr-   r  c                        e Zd ZdZddhej
                  Z	 	 	 	 d	 fd	Zed        Zd Z		 	 	 	 	 	 d
dZ
 fdZ xZS )r   zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r  	inferencec                 T    t        |   d||d| || _        || _        || _        y )N)r:   
value_typer#   )r$   r%   r   r  saved_tensors)r)   r:   r"  r   r  r#  r*   r+   s          r,   r%   z(AutogradFunctionContextVariable.__init__  s2     	FuFvF"
*r-   c                    | j                   j                  dt        j                  j                  j
                  t               i       }| j                   j                  j                  d t        j                  j                  j
                  t        j                  t        d|t                     i       }|j                  |j                  j                  d<   |S )Nrc   T)r   r  r#  example_value)r_   r  rY   r   r   FunctionCtxr   r\   track_object_new	functoolspartialr   r  r:   r   meta)r?   r  outs      r,   rP   z&AutogradFunctionContextVariable.create-  s    		&&U^^44@@%'2
 ii$$55NN##///,.	 

 ,/99

(
r-   c                 H    | j                   t        d       | j                   S )Nzproxy not set)r  r   r)   s    r,   as_proxyz(AutogradFunctionContextVariable.as_proxy@  s    ::/*zzr-   c                 p   |dk7  rt        d|        | j                  t        d       | j                  s6| j                  r|rJ |j                  j
                  j                  | |       |D ]'  }| j                  j                  j                  |       ) t        j                  j                  d       S )Nsave_for_backwardz"autograd.Function context method: zCsave_for_backward only supported on a newly constructed FunctionCtx)r   r#  r   r7   r_   r\   track_save_for_backwardr  appendr   rK   rP   )r)   r?   r@   ru   r*   args         r,   rz   z+AutogradFunctionContextVariable.call_methodE  s     &&>tfEF%U ~~;;v--II""::4FC&&--c2 ))0066r-   c                      dk(  rt         fd      S dk(  r> j                  2t        j                  t	         j                  j
                              S t                 S )Nr0  c                  ,    j                  | |      S r   )rz   )ru   r*   r@   r)   r?   s     r,   <lambda>z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>^  s    (8(8T4(Pr-   r#  )rX   r#  r   r   r  r  r$   rQ   r   s   ```r,   rQ   z+AutogradFunctionContextVariable.var_getattr[  se    &&!P  ?"t'9'9'E**40B0B0J0J+KLLw"2t,,r-   )NFNNr{   )r~   r   r   r   r   _nonvar_fieldsr%   r   rP   r.  rz   rQ   r   r   s   @r,   r   r     s    
 	 
#	1	1N +  $
7 &	7
 -7 
7,- -r-   r   c                   2     e Zd Z fdZ	 	 	 	 	 	 ddZ xZS )rX   c                 2    t        |   di | || _        y r"   )r$   r%   r   )r)   r   r*   r+   s      r,   r%   zLambdaVariable.__init__f  s    "6"r-   c                 &     | j                   |i |S r   )r   r  s       r,   rc   zLambdaVariable.call_functionj  s     tww'''r-   r{   )r~   r   r   r%   rc   r   r   s   @r,   rX   rX   e  s%    (/(9U(	(r-   rX   c                        e Zd Z fdZd Zedej                  j                  fd       Z	d Z
d Zd Z	 	 	 	 	 	 d	dZ xZS )
rM   c                     t        |   di | t        |t              sJ t        |t              sJ || _        || _        y r"   )r$   r%   r]   r   r   r  r@   )r)   r  r@   r*   r+   s       r,   r%   zGetAttrVariable.__init__q  sA    "6"#///$$$$	r-   c                 h    | j                   j                   d| j                   d| j                   dS )N(z, ))r+   r~   r  r@   r-  s    r,   __str__zGetAttrVariable.__str__x  s.    ..))*!DHH:R		{!DDr-   
base_proxyc                     t        | |      S r   )r4   )rA  attrs     r,   create_getattr_proxyz$GetAttrVariable.create_getattr_proxy{  s    z4((r-   c                 r    t         j                  | j                  j                         | j                        S r   )rM   rD  r  r.  r@   r-  s    r,   r.  zGetAttrVariable.as_proxy  s&    33DHH4E4E4GSSr-   c                    t        | j                  t        j                        s
t	               |j
                  j                  | j                  j                        }| j                  |j                  vr
t	               t        j                  || j                        }||j                  vr
t	               t        j                  ||      S r   )r]   r  r   r   r   r_   get_submodule
module_keyr@   __dict__inspectgetattr_static)r)   r?   r@   step1step2s        r,   const_getattrzGetAttrVariable.const_getattr  s    $((I$>$>?%''		''(;(;<99ENN*%''&&udii8u~~%%''%%eT22r-   c                 \     || j                          |j                  | j                        S r   )r  create_load_attrsr@   r   s     r,   r1   zGetAttrVariable.reconstruct  s#    ((33r-   c                 R    | j                   j                  || j                  ||      S r   )r  rz   r@   r  s       r,   rc   zGetAttrVariable.call_function  s#     xx##B		4@@r-   r{   )r~   r   r   r%   r@  r   rY   fxProxyrD  r.  rN  r1   rc   r   r   s   @r,   rM   rM   p  sb    E ) ) )T	34A/A9UA	Ar-   rM   c                   B     e Zd Z fdZ	 	 	 	 	 	 d fdZd Zd Z xZS )MethodWrapperVariablec                 2    t        |   di | || _        y r"   )r$   r%   method_wrapper)r)   rW  r*   r+   s      r,   r%   zMethodWrapperVariable.__init__  s    "6",r-   c                 .   t        | j                        rnt        |d   t        j                        rQt        |      dk(  rt        |      dk(  sJ |d   j                  || j                  j                  j                        S t        | )  |||       y )Nr   r   )r   rW  r]   r   r   r=   rQ   __self__r~   r$   rc   )r)   r?   ru   r*   r+   s       r,   rc   z#MethodWrapperVariable.call_function  s     &d&9&9:zGY--@
 t9>c&kQ&6667&&r4+>+>+G+G+P+PQQb$/r-   c                      yNTr#   r-  s    r,   ri   z(MethodWrapperVariable.is_python_constant      r-   c                     | j                   S r   )rW  r-  s    r,   r5   z(MethodWrapperVariable.as_python_constant  s    """r-   r{   )r~   r   r   r%   rc   ri   r5   r   r   s   @r,   rU  rU    s/    -
0/
09U
0	
0#r-   rU  c                   4     e Zd Z fdZ fdZd Zd Z xZS )GetSetDescriptorVariablec                 2    t        |   di | || _        y r"   )r$   r%   desc)r)   ra  r*   r+   s      r,   r%   z!GetSetDescriptorVariable.__init__  r   r-   c                     |dk(  rI| j                   r=ddlm}   ||t        | j                   d            | j                  j
                        S t        |   ||      S )N__get__r   rU   )r7   rj   rV   r   ra  rc  r$   rQ   )r)   r?   r@   rV   r+   s       r,   rQ   z$GetSetDescriptorVariable.var_getattr  sV    90J?2z$++y'IJ		!!  7&r400r-   c                      yr[  r#   r-  s    r,   ri   z+GetSetDescriptorVariable.is_python_constant  r\  r-   c                     | j                   S r   )ra  r-  s    r,   r5   z+GetSetDescriptorVariable.as_python_constant  s    yyr-   )r~   r   r   r%   rQ   ri   r5   r   r   s   @r,   r_  r_    s    1r-   r_  c                   >     e Zd Zdej                  f fdZd Z xZS )PythonModuleVariabler:   c                 2    t        |   di | || _        y r"   r$   r%   r:   r)   r:   r*   r+   s      r,   r%   zPythonModuleVariable.__init__      "6"
r-   c                 "    t         j                  S r   )ra   
ModuleTyper-  s    r,   r6   z PythonModuleVariable.python_type  s    r-   )r~   r   r   ra   rm  r%   r6   r   r   s   @r,   rg  rg    s    e..  r-   rg  c                        e Zd Zd	 fd	Zd Zd Zed        Ze e	j                  d      d               Z	 	 	 	 	 	 d
dZ	 	 	 	 	 	 d
 fdZ xZS )SkipFilesVariableNc                 @    t        |   di | || _        || _        y r"   )r$   r%   r:   reason)r)   r:   rq  r*   r+   s       r,   r%   zSkipFilesVariable.__init__  s!    "6"
r-   c                 ,    t        | j                        S r   r9   r:   r-  s    r,   r6   zSkipFilesVariable.python_type      DJJr-   c                     | j                   S r   r:   r-  s    r,   r5   z$SkipFilesVariable.as_python_constant      zzr-   c                 f    t        |j                  t        j                                | ||      S rI   )r   rN   r   FUNCTION_MATCH)clsr:   r7   s      r,   create_with_sourcez$SkipFilesVariable.create_with_source  s.    f''(C(CDE
 	
r-   c                  B    t         j                  t        j                  iS r   )rf   
namedtupler   r  r#   r-   r,    fold_through_function_to_wrapperz2SkipFilesVariable.fold_through_function_to_wrapper  s     ""I$F$F
 	
r-   c                    ddl m} t        j                  | j                  dd      rt        d| j                          y | j                  t        j                  u r% |j                  t        j                  gi S | j                  t        j                  u rNt              dk  r@t        j                  d         r(t        i t        j                  d   t                     S | j                  | j                         j                         v rt!              r | j                  D cg c]  }|j#                          c}i j%                         D ci c]  \  }}||j#                          c}}} | j                         j'                  | j                        |t                     S | j                  t(        j*                  u rst-        fdD              rD 	cg c]  }	|	j/                         }
}	g }t)        j*                  |
 D ]/  }|j1                  t3        j4                  t7        |                   1 t3        j8                  |t                     S | j                  t(        j:                  u r}s{t-        fd	D              rgD 	cg c]  }	|	j/                         }
}	g }t)        j:                  |
 D ]  }|j1                  |        t3        j8                  |t                     S | j                  t(        j<                  u rddl m} t?        d
 j                         D              r9t        ddjA                  tC        j                               ddhz
                j'                  d      }t              dv rd   jE                        rd   j/                        }dv rt              dk(  rd   jF                  }ndt              dk(  rd   jF                  }nFt              dk(  r! |tH        jJ                        jF                  }nt        d       nt        d       g }||j1                  |       D ]%  }||}n	  ||gi       }|j1                  |       ' t3        j8                  |t                     S | j                  t(        jN                  u rǉst              dk(  rd   jE                        rd   jQ                         rd   j/                        }d   j#                         }g }t)        jN                  ||      D ]/  }|j1                  t3        j4                  t7        |                   1 t3        j8                  |t                     S | j                  t(        jR                  u rt?        d j                         D              r8t        ddjA                  tC        j                               dhz
                d t              dk(  r6d   jE                        r"d   j/                        }dv rfdnd }nt        d       g }	 t)        jR                        D ]  \  }}|j1                  t3        j4                  t2        jT                  jW                  |      rt2        jT                  jY                  |      n|t3        j8                  t7        |      t                     gt                             	 t3        j8                  |t                     S | j                  tZ        j\                  u ras_t              dk(  rQd   j^                  (d   ja                  jb                  jd                        rfd}t3        jf                  |      S | j                  t        jh                  u r~s|t              dk(  rg }nBt              dk(  r)d   jE                        rd   j/                        }nt        d       t2        jj                  jm                  t                     S | j                  tZ        jn                  u r9st        d        d   }dd  }t2        jp                  js                  ||!      S | j                  t(        jt                  u r^t              dk  rt3        jv                  d"t               iS dd#l<m=} j}                    |       t~        jt                              S | j                  t(        j                  u rt3        j                  d"t               iS | j                  t(        j                  u rt3        j                  d"t               iS 	 t        j                  | j                        }d%| j                  j                   d&| d'}|| j                  rd(| j                   d'nd)z  }t        |       y c c}w c c}}w c c}	w c c}	w # tL        $ r t        d d| | d      w xY w# tL        $ r t        d      w xY w# t        $ r d$| j                  j                   }Y w xY w)*Nr   r/   _torchdynamo_disableFz.call torch._dynamo.disable() wrapped function r   )r^   c              3   @   K   | ]  }|j                          y wr   has_unpack_var_sequence.0r3  r?   s     r,   	<genexpr>z2SkipFilesVariable.call_function.<locals>.<genexpr>       DtC//3t   c              3   @   K   | ]  }|j                          y wr   r  r  s     r,   r  z2SkipFilesVariable.call_function.<locals>.<genexpr>  r  r  c              3   $   K   | ]  }|d v 
 yw))initialr   Nr#   )r  rw   s     r,   r  z2SkipFilesVariable.call_function.<locals>.<genexpr>$  s     K]c311]s   z-Unsupported kwargs for itertools.accumulate: ,r  r   )r   r   r   zDitertools.accumulate can only accept one of: `func` kwarg, pos 2 argz.Unsupported arguments for itertools.accumulatezOUnexpected failure in invoking function during accumulate. Failed running func r>  r?  c              3   &   K   | ]	  }|d k7    yw)rw   Nr#   )r  kws     r,   r  z2SkipFilesVariable.call_function.<locals>.<genexpr>\  s     722;s   z*Unsupported kwargs for itertools.groupby: rw   c                     t        | t        j                        r| j                         S t        | t        j                        r| j                         S t        dt        t        |             z         )Nz,Unsupported key type for itertools.groupby: )	r]   r   SymNodeVariableevaluate_exprrK   r5   r   r   r9   rw   s    r,   retrieve_const_keyz;SkipFilesVariable.call_function.<locals>.retrieve_const_keyb  s^    c9#<#<=,,..Y%?%?@1133'FTRUYW r-   c                 V     j                  d      j                  | gi             S )Nrw   )getrc   )xr*   r  r?   s    r,   r6  z1SkipFilesVariable.call_function.<locals>.<lambda>p  s(    . &

5 1 ? ?QC Lr-   z+Unsupported arguments for itertools.groupbyr  z1Unexpected failure when calling itertools.groupbyc                     t        | t        j                        r6d   j                  rd   j                  }nd   }| j	                  |      S t        d|  d       y )Nr   )wrapped_reconstructiblezfunctools.wraps(r?  )r]   r   r   r7   cloner   )r   reconstructibleru   s     r,   wrapsz.SkipFilesVariable.call_function.<locals>.wraps  sW    b)"F"FGAw~~*.q'..*.q'88O8LL 0A67r-   z*deque() with more than 1 arg not supportedzfunctools.partial malformed)ru   keywordsr^   )SourcelessBuilderzBuiltin z'skip function z	 in file 'z',  )Ibuiltinr/   rJ  rK  r:   r   rf   rg   call_custom_dictdefaultdictr=   r   is_supported_argr   r~  keysr   r5   rr   r  	itertoolsproductallunpack_var_sequencer2  r   r   r  ListIteratorVariablechain
accumulateanyjoinsetr  rc   operatoradd	Exceptioncombinationsri   groupbyrK   rL   rP   r(  r  r7   can_reconstructr_   root_txrX   dequelistsDequeVariabler)  r   FunctoolsPartialVariablerepeatRepeatIteratorVariablerj   r  inline_user_function_returnr   countCountIteratorVariablecycleCycleIteratorVariablegetfile	TypeErrorr~   r   rq  )r)   r?   ru   r*   r/   r  rx   vr:   r3  seqsrr   itemaccseqr   iterablerkeyfuncresultr  r   	rest_argsr  pathmsgr  s    ```                      @r,   rc   zSkipFilesVariable.call_function  s	    	-!!$**.DeLJ4::,WXZZ;2223?33K++.26<  JJ+111D	Q#44T!W=&''Q*n	  JJ$??AFFHH#D&1DJJ267$Q!&&($79?HA1a**,,HE K488:>>tzzJ\^  JJ)+++DtDD;?@4CC++B/4D@E!))40Y44T$Z@A 111%|~VVJJ)//)DtDD;?@4CC++B/4D@E!.T" /11%|~VVZZ9///0KV[[]KKCxxFKKM 2i5H HIJL
 **Y'C4yF"tAw'F'Fr'J1g11"5V#D	Q!&>77DY!^700DY!^*8<<8FFD!^ NOES!;C"2T{B7
 S!  11%|~VVJJ)000D	QQ//3Q**,Aw2226HQ**,AE!..x;Y44T$Z@A <11%|~VVZZ9,,,777@xxFKKM 2eW <=>@
 4yA~$q'"A"A""E1g11"5    KLF%--cw?DAqMM!// $-#=#=#H#H#K !* : : A A! D%& ) > >$(G<>!"	 +7.
 @$ 11&WWJJ)//)D	QQ*d1g.E.EbiiFWFW.X8 ++E22ZZ;,,,V4yA~TaDG$C$CB$GQ33B7JK??00ln0UUZZ9,,,;<aBQRI &&??V @   ZZ9+++4y1} 77)5  311#!#B8$  ZZ9??*22DWWWZZ9??*22DWWW8tzz2 $DJJ$;$;#<IdV1MC4;;SQ'B>C#M 8H A AT % +mnrmsstuytz{~z  @A  B L  #G z  8!$**"5"5!678sC   6g3#g83g>(h3hB3h) 
i h&)h>"i&%i&c                     | j                   t        j                  t        j                  hv r,|dk(  r'ddlm}  |j                  || j                   g|i |S t        | !  ||||      S )Nfromkeysr   r  )	r:   rf   rg   r  r  r/   call_custom_dict_fromkeysr$   rz   )r)   r?   r@   ru   r*   r/   r+   s         r,   rz   zSkipFilesVariable.call_method  ss     JJ;22K4K4KLL
"0<?<<DJJ!%)/  w"2tT6::r-   r   r{   )r~   r   r   r%   r6   r5   classmethodr{  r   r(  	lru_cacher~  rc   rz   r   r   s   @r,   ro  ro    s    
  
 
 Y
  

d/d9Ud	dL; &	;
 -; 
; ;r-   ro  c                   >     e Zd Z fdZ	 	 	 	 	 	 ddZd Zd Z xZS )TypingVariablec                 2    t        |   di | || _        y r"   ri  rj  s      r,   r%   zTypingVariable.__init__  rk  r-   c                     |dk(  rKt        |      dk(  r=t        j                  j                  | j                  |d   j                                  S t        d       y )Nrh   r   r   typing)r=   r   rK   rP   r:   r5   r   r)   r?   r@   ru   r*   s        r,   rz   zTypingVariable.call_method  sR     = SY!^--44

475578  	hr-   c                 ,    t        | j                        S r   rs  r-  s    r,   r6   zTypingVariable.python_type  rt  r-   c                     | j                   S r   rv  r-  s    r,   r5   z!TypingVariable.as_python_constant  rw  r-   r{   )r~   r   r   r%   rz   r6   r5   r   r   s   @r,   r  r    s5      &	 
 -  
  r-   r  )maxsizec                      ddl m}  i }| j                         D ]H  \  }}|j                  j                         D ]&  \  }}t	        |      st        ||d       x}s"|||<   ( J |S )Nr   )NP_TO_TNP_MODULE)utilsr  rr   rI  r   r4   )r  np_fn_to_tnp_fnnp_modtnp_modr   tnp_fnnp_fns          r,   get_np_to_tnp_mapr    sn    (O+113&//557OGV $FGT::5:-3OE*  8 4 r-   c                   `     e Zd ZdZ fdZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd Zd Z fdZ	 xZ
S )	NumpyVariablezz
    Wrapper around `numpy.*`. Currently, is able to trace a small subset of numpy functions as well as numpy dtypes.
    c                 2    t        |   di | || _        y r"   ri  rj  s      r,   r%   zNumpyVariable.__init__  rk  r-   c                    t         j                  st        d| j                   d       ddlm} ddlm} | j                  j                  dk(  r#t        dt        | j                         d	       y t               j                  | j                        }|t        d
| j                   d       |j                  dk(  rAt         j                  r1d|j                   d}|dt         j                   z  }t        |        |j                  j                   d ||      gt#        ||       }|j%                  ||      S )Nznumpy.z()r   )numpy_to_tensor_wrapperr   )NumpyNdarrayVariabledtypez4numpy dtype function is not supported yet. Got type .zCan't find numpy function zM in torch._numpy.  Please file an issue to request support for this function.ztorch._numpy.randomz
delegate 'z' to NumPy itself via zconfg.use_numpy_random_stream=rc   )r   trace_numpyr   r:   r  r  tensorr  r~   r9   r  r  r   use_numpy_random_streamr   r_   r  r   rP   )	r)   r?   ru   r*   r  r  r   r  r  s	            r,   rc   zNumpyVariable.call_function  s0    !!F4::,b1230 ::')FtDJJGWFXXYZ %&**4::6D|0 =R R #8822"4#4#4"55KL78V8V7WXXc" +BII**'- #40E
 (..r599r-   c                     t        d       y )Nnumpyr   r  s        r,   rz   zNumpyVariable.call_method=  s     	gr-   c                 ,    t        | j                        S r   rs  r-  s    r,   r6   zNumpyVariable.python_typeF  rt  r-   c                     | j                   S r   rv  r-  s    r,   r5   z NumpyVariable.as_python_constantI  rw  r-   c                     t         j                  rQt        | j                  t              r7| j                  j
                  }t        j                  |      }|j                  S t        | )         S r   )r   r  r]   r:   r9   r~   tnpr  r@   r$   r.  )r)   rC  	tnp_dtyper+   s      r,   r.  zNumpyVariable.as_proxyL  sS     *TZZ"> ::&&D		$I >>!w!!r-   r{   )r~   r   r   r   r%   rc   rz   r6   r5   r.  r   r   s   @r,   r  r    sb    &:/&:9U&:	&:P &	
 - 
 " "r-   r  c                   *     e Zd Z fdZd Zd Z xZS )NullVariablec                 $    t        |   di | y r"   r   r   s     r,   r%   zNullVariable.__init___  r   r-   c                      y)Nr  r#   r-  s    r,   r@  zNullVariable.__str__b  s    r-   c                 V    t         j                  dk  rt        d       t        d      gS )N)      z0cannot reconstruct NullVariable in < Python 3.11	PUSH_NULL)sysversion_infor   r
   r   s     r,   r1   zNullVariable.reconstructe  s'    g%LM";/00r-   )r~   r   r   r%   r@  r1   r   r   s   @r,   r  r  ^  s    #1r-   r  c                       e Zd ZdZy)DeletedVariablez"Marker used to implement delattr()Nr   r#   r-   r,   r  r  k  s    ,r-   r  )Nrf   r  r(  rJ  r  r  r  ra   r  r   r   torch._CrY   torch._numpy_numpyr  r  r   r   r   bytecode_transformationr	   r
   excr   guardsr   r   r7   r   r   r   r   r  r   r   r   r   baser   r   dictsr   r   r   r   r   user_definedr   r    r   r   r   r   r   r   r   r   r   r   r   r   r   	dataclassr  r   rX   rM   rU  r_  rg  ro  r  r  r  r  r  r  r#   r-   r,   <module>r     s         
     * * N  0 N N  0 & 
 4GGO GGTo o 67 67r8o 88_ 8#o #
# #
- -2	 	##%G9 G9T M M MN-&? N-b(_ ($Ao $AN#O #0 , ?  Q; Q;h_ 2 Q   N"O N"d
1? 
1-o -r-   