
    PhII                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZmZmZmZmZmZmZmZ dZee
j.                     ed<   	 d dlZd dlZd dlmc mZ d dl m!Z! ddl"m#Z# ddl$m%Z% dd	l&m'Z'm(Z(m)Z) 	  G d
 d      Z*e*dee+   fd       Z,d$dZ-ej\                   G d d             Z/d%de/fdZ0e*dee+e1f   fd       Z2e*dee+e1f   fd       Z3e*dee+e1f   fd       Z4e*dee+e1f   fd       Z5e*dee+e1f   fd       Z6 ee7      Z8ee1eeg df      f   ed<   de1deg df   ddfdZ9de:ddfdZ;de<fdZ=de<fdZ>de<fdZ?de1fd Z@de<fd!ZAde<fd"ZBde<fd#ZCy# e$ r Y w xY w)&    N)defaultdict)AnyCallablecastDictListOptionalSetUnionnp)is_fx_tracing   )config)is_compiling)hashableis_safe_constantNP_SUPPORTED_MODULESc                       e Zd ZU dZdZeee      ed<   dZ	ee
eef      ed<   deg ee
eef   ee   f   f   fdZd Zded	efd
ZdefdZdefdZdefdZy)FunctionIdSeta2  
    Track a set of `id()`s of objects which are either allowed or not
    allowed to go into the generated FX graph.  Use to test for torch.*,
    numpy.*, builtins.*, etc.

    Support user modification to permit customization of what can be
    added to the graph and what will cause a graph break.
    Nfunction_idsfunction_nameslazy_initializerc                     || _         y N)r   )selfr   s     jC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/allowed_functions.py__init__zFunctionIdSet.__init__@   s
     0    c                    | j                   j| j                         }t        |t              r1t	        |j                               | _         || _        | j                   S t        |t              sJ || _         | j                   S r   )r   r   
isinstancedictsetkeysr   )r   values     r   __call__zFunctionIdSet.__call__C   su    $))+E%&$'

$5!&+#     "%---$)!   r   idxdefaultc                 d     |         | j                   J | j                   j                  ||      S r   )r   get)r   r&   r'   s      r   get_namezFunctionIdSet.get_nameN   s2    ""...""&&sG44r   c                 4     |        }|j                  |       y r   )addr   r&   r   s      r   r,   zFunctionIdSet.addS   s    vr   c                 >     |        }||v r|j                  |       y y r   )remover-   s      r   r/   zFunctionIdSet.removeW   s$    v,$ r   c                     | |        v S r    )r   r&   s     r   __contains__zFunctionIdSet.__contains__\   s    df}r   )__name__
__module____qualname____doc__r   r	   r
   int__annotations__r   r   strr   r   r   r%   r*   r,   r/   r2   r1   r   r   r   r   3   s     (,L(3s8$+/3NHT#s(^,31"eDcNCPSH<T6U2U)V 1	!5C 5# 5
s %# %
 r   r   returnc                  z   ddd t         j                  t        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  j                  t        j                  t        j                  j                   t        j                  j"                  t        j$                  j&                  j(                  t        j$                  j*                  j,                  t        j.                  t        j0                  j2                  t        j4                  t        j6                  t        j8                  t        j:                  t        j<                  t        j>                  t@        jB                  t        jD                  jF                  jH                  jJ                  t        jL                  jN                  t        jP                  t        jR                  jT                  g} t        jV                  jY                         D cg c](  }t3        |t[        t        j\                              s'|* }}| |z  } t        jV                  jY                         D cg c]'  }t3        |t[        t        j^                              r|) }}| |z  } t        j`                  jc                         r3| je                  t        j`                  jf                  jh                         | D ch c]  }tk        |       c}S c c}w c c}w c c}w )NTF)6collectionsOrderedDictcopydeepcopyinspect	signaturemath__package__torch__builtins__autocast_decrement_nestingautocast_increment_nestingautogradgradclear_autocast_cachecudacurrent_device
set_devicedistributionsconstraintsis_dependentnormalNormalinference_modejitr    set_anomaly_enabledset_autocast_cache_enabledset_autocast_cpu_dtypeset_autocast_cpu_enabledset_autocast_enabledset_autocast_gpu_dtypewarningswarn_C_dynamo
eval_frameunsupportedTensorr   
resize_as__tensor_convert__dict__valuestypefloat32FloatStoragedistributedis_availableextenddistributed_c10d'dynamo_unsupported_distributed_c10d_opsid)r/   objdtypesstoragexs        r   _disallowed_function_idsrt   `   s;    			((((""

!!

''44""))		!!(($$&&""$$##//=FF ~~,,..*S$u}}BU2V.   fF >>((**Cc4 2 234 	*  
 gF %%'..VV	
 ""6aBqE6""# #s   =(L.&L.,L3L8c                      t        dd      j                  } | j                         D ]!  \  }}t        d| d|j                   d       # y )NTF)recordc_binding_only"z": ,)gen_allowed_objs_and_idsname_rule_mapitemsprintr3   )mkvs      r    dump_allowed_torch_name_rule_mapr      sC     UCQQA	1!C

|1%& r   c                   j    e Zd ZU dZeeef   ed<   ee	   ed<   ee	   ed<   ee	   ed<   eee	f   ed<   y)AllowedObjectsa  
    Track the objects, object id - name pairs, and name - dynamo wrapping rule pairs
    from the heuristic defined in `gen_allowed_objs_and_ids`.
    TODO: Remove the overalp/duplication between these fields
    after allowed_functions refactor is done.
    
object_idsctx_mamager_classesc_binding_in_graph_functions non_c_binding_in_graph_functionsr{   N)
r3   r4   r5   r6   r   r7   r9   r8   r
   r   r1   r   r   r   r      sC     S#XS!"%c(*&)#h.S>!r   r   c                    	
 ddl mm	 t        j                  dt
        d       t               t               t               t               t               fdd 	fdd	 
 fd

 
t                
t               t        j                  rfddlm} |j                  |j                  |j                   |j"                  |j$                  |j&                  fD ]  }t)        |      t+        |      <    t-        t        j.                        D ]X  }t1        t        j.                  |      }t3        |t4        j6                  t4        j8                  f      sHd| t+        |      <   Z t;               D ]
  }|v s|=  t<        t>        fD ])  }|j@                   d|jB                   t+        |      <   + tE              S )z=
    Walk torch.* and get the ids of all the stuff in it
    r   )TorchCtxManagerClassVariableTorchInGraphFunctionVariableignoretorch.distributed)categorymodulec                     t        t        |       t              r@t        | d      r3t        | d      r&|j                   d| <   j	                  |        y y y y )N	__enter____exit__.)
issubclassrg   hasattrr3   r,   )rp   r   namer   r   torch_name_rule_maps      r   heuristic_record_if_ctx_managerzAgen_allowed_objs_and_ids.<locals>.heuristic_record_if_ctx_manager   sf    tCy$'[)Z( -  ??#1TF+  ##C( ) * (r   c                     t        |       xr6 | t        j                  j                  t        j                  j                  hv S r   )r   rD   r]   _cuda_isCurrentStreamCapturing_graph_pool_handlerp   s    r   is_special_functionsz6gen_allowed_objs_and_ids.<locals>.is_special_functions   s9    } 
HH33HH'')
 "
 	
r   c                    	 t        | d      r| j                  } t        | t        j
                  t        j                  t        j                  t        j                  t        j                  f      s |       re|j                   d| <   rt        | d      sj                  |        y y t        | d      rj                  |        y j                  |        y y # t        $ r Y w xY w)N__wrapped__r   __code__)r   r   	Exceptionr    typesFunctionType
MethodTypeBuiltinFunctionTypeMethodDescriptorTypeWrapperDescriptorTyper3   r,   )	rp   r   r   r   r   rw   r   r   r   s	      r   %heuristic_record_if_in_graph_functionzGgen_allowed_objs_and_ids.<locals>.heuristic_record_if_in_graph_function   s    	sM*oo ""  ))**++	
 "#& -  ??#1TF+ sJ/044S9 0 3
+488=044S9 '  		s   C   	C,+C,c                 4   d}g d}t         j                  r|j                  d       t        |D cg c]  }|dz   	 c}      }t	        j
                  |       }|y|j                  t        fd|D              ry|v xs j                  |      S c c}w )N)rD   rB   )>ztorch.optimztorch.nn.modules.rnnztorch._dynamoztorch._C._dynamoztorch._inductorztorch._C.inductorztorch.fxztorch._C._autogradztorch._C._cudartztorch._C._distributed_autogradztorch._C._distributed_c10dztorch._C._distributed_rpcztorch._C._functorchztorch._C._monitorztorch._C._nvtxztorch._C._lazyztorch._C._profilerztorch.__config__ztorch._custom_opztorch._dispatchztorch._exportz torch._functorch.make_functionalztorch._functorch.compile_utilsztorch._functorch.partitionersztorch._functorch.aot_autogradztorch._functorch.compilersztorch._functorch.fx_minifierztorch.autograd.profiler_utilztorch.autograd.profilerztorch._jit_internalztorch._libraryztorch._lobpcgztorch._loggingztorch._meta_registrationsztorch._namedtensor_internalsztorch._numpyztorch._sourcesztorch._subclassesztorch._tensorztorch._tensor_strztorch._utilsztorch._utils_internalztorch._vmap_internalsztorch.compilerr   ztorch.exportz	torch.hubz	torch.jitztorch.libraryztorch.masked.maskedtensorztorch.nn.initztorch.nn.modules.moduleztorch.nn.parallelztorch.nn.utilsztorch.multiprocessingz
torch.onnxztorch.overridesztorch.packageztorch.profilerztorch.serializationztorch.storageztorch.utilsztorch.distributed.r   Fc              3   @   K   | ]  }j                  |        y wr   )
startswith).0r~   mod_names     r   	<genexpr>zNgen_allowed_objs_and_ids.<locals>._is_allowed_module_prefix.<locals>.<genexpr>B  s     B/A!x""1%/As   )	r   trace_distributedappendtupler@   	getmoduler3   anyr   )rp   allowed_modulesdisallowed_modulesrs   allowed_modules_dotr   r   s         @r   _is_allowed_module_prefixz;gen_allowed_objs_and_ids.<locals>._is_allowed_module_prefix   s    +?
@ ##%%&:;#o$FoQWo$FG""3'>??B/ABB?*Vh.A.ABU.VV %Gs   Bc                      t         fdt        j                  D              ry  j                  	t	               <   t         j                  j                               D ]  \  }}t	        |      	vsdd l}t        ||j                  j                        r:||j                  j                  t        j                  |j                  j                  t        j                  |j                   j"                  j$                  |j&                  fv rt        |t(        j*                        rL|j                  j-                  d      s |      s j                   d| 	t	        |      <    |        |      r5r | |        | |        j                   d| 	t	        |      <   Tt/        j0                  |      kt3        |      rxr | |        | |        j                   d| 	t	        |      <    y )Nc              3   T   K   | ]  }j                   j                  |       ! y wr   )r3   r   )r   r   r   s     r   r   zHgen_allowed_objs_and_ids.<locals>._find_torch_objects.<locals>.<genexpr>H  s(      
M OO&&x0Ms   %(r   ztorch.r   )r   r   *allowed_functions_module_string_ignorelistr3   ro   listre   r|   
torch._opsr    _opsHigherOrderOperatorfuncrI   deprecated_funcvmapnn
functional!triplet_margin_with_distance_losscondr   
ModuleTyper   r@   r   r   )
r   r   rp   rD   _find_torch_objectsr   r   r   rv   torch_object_idss
   `   r   r   z5gen_allowed_objs_and_ids.<locals>._find_torch_objectsG  s    
"MM
 
 '-F$foo3356ID##w.. "c5::#A#AB JJOO#((JJOO#((HH''IIJJ  c5#3#34||..x8=V> 8>6Gq4O(C1+C0.s37VTJ=c64P39??2C1TF0K$RW-&&s+3<LS<Q7VTJ=c64P39??2C1TF0K$RW-O 7r   r   )_functional_collectives_implztorch.Tensor.r   )#	variablesr   r   r[   filterwarningsUserWarningr!   r"   rD   rB   r   r   torch.distributedr   _all_gather_into_tensor_all_reduce_reduce_scatter_tensor_all_reduce_coalesced!_all_gather_into_tensor_coalesced _reduce_scatter_tensor_coalescedreprro   dirra   getattrr    r   r   r   rt   r   r   r4   r3   r   )rv   rw   fcifr   methodr&   extrar   r   r   r   r   r   r   r   r   r   r   r   s   ``      @@@@@@@@@@@@r   rz   rz      s    VH{CVWv%#&5 '*u$&	)
: :8TWl.L .L` I ''OO&&%%1100
A '+1gRU#
 ELL!t,U//1L1LM
 .;4&+ARZ( " ()"" % *  .).)9)9(:!ENN;K&LE# / $( r   c                  *    t               j                  S r   )rz   r   r1   r   r   _allowed_function_idsr     s    #%000r   c                  
    i } | S r   r1   )rvs    r   "_allowed_user_defined_function_idsr     s    BIr   c            	         t         j                  j                         D  ci c]0  \  } }| j                  d      st	        |      rt        |      d|  2 }} }|j                  t        j                  j                         D  ci c]0  \  } }| j                  d      st	        |      rt        |      d|  2 c}}        |j                  t        j                  t        j                  fD ci c]  }t        |      d|j                    c}       |j                  t        t              dt        t        j                        dt        t        j                         di       |S c c}} w c c}} w c c}w )N_	builtins.z	operator.z
functools.ztyping.castzfunctools.reducezcopy.deepcopy)builtinsre   r|   r   callablero   updateoperator	itertoolschainislicer3   r   	functoolsreducer>   r?   r   r   r   s      r   _builtin_function_idsr     sN    %%++-
-DAq||C Xa[ 	11#-  

 II !))//1	
11<<$! qEYqc?"1	
 II4=OOYEUEU3VW3VaA*QZZL)	)3VW IItHmy "4t}}	
 I-
	
 	Xs   5E+5E1
0 E7c                  L   t               } t        D ]  }| j                  |j                  j	                         D ci c]S  \  }}t        |      rCt        |dd       xs |j                  |j                  k(  rt        |      |j                   d| U c}}        | S c c}}w )Nr4   r   )	r!   r   r   re   r|   r   r   r3   ro   )r   modr   r   s       r   _numpy_function_idsr     s    	B#
		  LL..00DAqA;Qd3Cs||T 1#,,q,,0	
 $ Is   AB c                      t         j                  j                         D  ci c]0  \  } }| j                  d      st	        |      st        |      d|  2 }} }|S c c}} w )zC
    Collects constant builtins by eliminating callable items.
    r   r   )r   re   r|   r   r   ro   r   s      r   _builtin_constant_idsr     sd     %%++-
-DAq||C ! 	11#-  

 I
s   5A_lazy_module_initr   	init_funcc                     d| vs
J d|         | t         j                  v r |        | t        vsJ t        |    j                  |       y)z.Register a module without eagerly importing itr   z%Expected a root module name, but got N)sysmodulesr   r   )r   r   s     r   add_module_init_funcr     sR     d?JCD6JJ?s{{ ((((d""9-r   rp   c                     t        | dd       }|y |j                  d      d   }t        j                  |d       }||D ]	  } |         y y )Nr4   r   r   )r   splitr   pop)rp   r   base_module
init_funcsfns        r   _maybe_init_lazy_moduler     sW    S,-F~,,s#A&K"&&{D9JBD  r   c                    t        |        t        |       t        v ryt        |       t        v ryt	        | t
        j                  j                  t
        j                  j                  t
        j                  j                  f      S )z&Is this safe to trace like torch.add ?FT)
r   ro   rt   r   r    rD   r   OpOverloadPacket
OpOverload_OpNamespacer   s    r   
is_allowedr    sd    C 	#w**	#w''
 		$	$ejj&;&;UZZ=T=TU r   c                 :    t        |        t        |       t        v S r   )r   ro   r   r   s    r   is_user_defined_allowedr    s    C c7888r   c                 2    t        |        t        | dd      S )N_dynamo_forbiddenF)r   r   r   s    r   is_forbiddenr    s    C 3+U33r   c                 @    t         j                  t        |       |      S )z&Convert a torch.* function to a string)r   r*   ro   )rp   r'   s     r   torch_get_namer
    s     ))"S'7;;r   c                 $    t        |       t        v S r   )ro   r   r   s    r   is_builtin_callabler        c7+++r   c                 $    t        |       t        v S r   )ro   r   r   s    r   is_builtin_constantr  "  r  r   c                     t         yt        | t         j                  t         j                  f      xs t	        |       t
        v S )NF)r   r    ndarraygenericro   r   r   s    r   is_numpyr  &  s3    	zcBJJ

34V3CV8VVr   )r:   N)FT)Dr   r<   r>   dataclassesr   r@   r   rB   r   r   r   r[   r   typingr   r   r   r   r   r	   r
   r   r   r   r8   numpyModuleNotFoundErrorrD   torch._functorch.deprecated
_functorch
deprecatedr   torch.fx._symbolic_tracer    r   external_utilsr   utilsr   r   r   r   r7   rt   r   	dataclassr   rz   r9   r   r   r   r   r   r   r   r   objectr   boolr  r  r  r
  r  r  r  r1   r   r   <module>r"     sJ            
   # H H H!%HU %	
  5 5 2  ( C C&* *Z 4##c( 4# 4#r' " " "l> l^ 1tCH~ 1 1 DcN  
 tCH~  4 T#s(^   	tCH~ 	 	 :ET9J 4T(2t8"4556 J	.s 	.xD/A 	.d 	.	 	D 	t &9D 9
4 4
<C <
, ,, ,WT We  		s   !E7 7F ?F 