U
    9%e)                     @   s`  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZmZmZ zd dlZW n ek
r   dZY nX d dlZd dlm  mZ d dlmZ ddlmZ ddlmZ ddlmZmZ dd	 Z e d
d Z!e dd Z"e dd Z#e dd Z$e dd Z%e dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+d d! Z,dS )"    N)castDictOptionalSet)is_fx_tracing   )config)is_compiling)is_safe_constantNP_SUPPORTED_MODULESc                    s   G  fddd}| S )a2  
    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.
    c                       s   e Zd ZU dZeee  ed< dZee	ee
f  ed<  fddZee
dddZed	d
dZed	ddZed	ddZdS )z+make_function_id_set.<locals>.FunctionIdSetNfunction_idsfunction_namesc                    sJ   | j d krD  }t|tr0t| | _ || _nt|ts>t|| _ | j S N)r   
isinstancedictsetkeysr   AssertionError)selfvaluelazy_initializer ^/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_dynamo/allowed_functions.py__call__=   s    

z4make_function_id_set.<locals>.FunctionIdSet.__call__)idxdefaultc                 S   s   |   | j ||S r   )r   get)r   r   r   r   r   r   get_nameH   s    z4make_function_id_set.<locals>.FunctionIdSet.get_name)r   c                 S   s   |   | j | d S r   )r   addr   r   r   r   r   r   L   s    z/make_function_id_set.<locals>.FunctionIdSet.addc                 S   s   ||  kr| j | d S r   )r   remover    r   r   r   r!   P   s    
z2make_function_id_set.<locals>.FunctionIdSet.removec                 S   s
   ||  kS r   r   r    r   r   r   __contains__T   s    z8make_function_id_set.<locals>.FunctionIdSet.__contains__)__name__
__module____qualname__r   r   r   int__annotations__r   r   strr   r   r   r!   r"   r   r   r   r   FunctionIdSet9   s   
r)   r   )r   r)   r   r   r   make_function_id_set/   s    
r*   c                  C   s  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jtjtjtjtjtjtjtj j!j"j#tj$j%g} tj&' rddl(m)} t*j+,| dd tj-. D }| |7 } dd tj-. D }| |7 } tj&' r| /tj&j0j1 dd	 | D S )
NTFr   )_functional_collectivesc                 S   s    g | ]}t |ttjr|qS r   )r   typetorchZfloat32.0objr   r   r   
<listcomp>   s     z,_disallowed_function_ids.<locals>.<listcomp>c                 S   s    g | ]}t |ttjr|qS r   )r   r,   r-   ZFloatStorager.   r   r   r   r1      s   c                 S   s   h | ]}t |qS r   )idr/   xr   r   r   	<setcomp>   s     z+_disallowed_function_ids.<locals>.<setcomp>)2collectionsOrderedDictcopydeepcopyinspect	signaturemath__package__r-   __builtins__Zautocast_decrement_nestingZautocast_increment_nestingZautogradgradZclear_autocast_cachecudaZcurrent_deviceZ
set_devicedistributionsconstraintsZis_dependentnormalZNormalZinference_modeZset_anomaly_enabledZset_autocast_cache_enabledZset_autocast_cpu_dtypeZset_autocast_cpu_enabledZset_autocast_enabledZset_autocast_gpu_dtypewarningswarnZ_CZ_dynamoZ
eval_frameunsupportedTensor__init__distributedZis_availableZtorch.distributedr+   r   Z!skipfiles_inline_module_allowlistr   __dict__valuesextendZdistributed_c10dZ'dynamo_unsupported_distributed_c10d_ops)r!   r+   ZdtypesZstorager   r   r   _disallowed_function_idsZ   sX    


rM   c                     s   t jdtdd t dd  fdd  t  t ttjD ]4} ttj| }t	|t
jt
jfrHd|  t|< qHt D ]}|kr|= qttfD ]}|j d	|j t|< qS )
z=
    Walk torch.* and get the ids of all the stuff in it
    ignoreztorch.distributed)categorymodulec                    sb   d}d}t dd |D }t| }|d kr0dS |j t fdd|D rPdS  |kp` |S )N)r-   r<   )
ztorch.optim.ztorch.utils._foreach_utilsztorch.nn.modules.rnn.ztorch._dynamo.ztorch._C._dynamo.ztorch._inductor.ztorch._C.inductor.z	torch.fx.ztorch.distributed.fsdp.ztorch.distributed._tensor.c                 S   s   g | ]}|d  qS ).r   r3   r   r   r   r1      s     zL_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<listcomp>Fc                 3   s   | ]}  |V  qd S r   )
startswith)r/   mmod_namer   r   	<genexpr>   s     zK_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<genexpr>)tupler:   	getmoduler#   anyrR   )r0   Zallowed_modulesZdisallowed_modulesZallowed_modules_dotrP   r   rT   r   _is_allowed_module_prefix   s    
z8_allowed_function_ids.<locals>._is_allowed_module_prefixc                    s  t  fddtjD rd S  jt < t j D ]\}}t|kr8dd l}t	||j
jrdq8||jjtj|jjtjfkrq8t	|tjr|jdr|r j d| t|< | q8|r j d| t|< q8t|d kr8t|s8 j d| t|< q8d S )Nc                 3   s   | ]} j |V  qd S r   )r#   rR   )r/   rU   rP   r   r   rV      s   zE_allowed_function_ids.<locals>._find_torch_objects.<locals>.<genexpr>r   ztorch.rQ   )rY   r   Z*allowed_functions_module_string_ignorelistr#   r2   listrJ   itemsZ
torch._opsr   _opsZHigherOrderOperatorfuncr?   deprecated_funcZvmaptypes
ModuleTyperR   r:   rX   r
   )rP   namer0   r-   _find_torch_objectsrZ   Ztorch_object_idsr[   r   re      s6    
z2_allowed_function_ids.<locals>._find_torch_objectsztorch.Tensor.rQ   )rD   filterwarningsUserWarningr   r-   r<   dirrG   getattrr   ra   MethodDescriptorTypeWrapperDescriptorTyper2   rM   r   r	   r$   r#   )rc   methodr   extrar   rd   r   _allowed_function_ids   s&    ( 

rn   c                  C   s   i } | S r   r   rvr   r   r   "_allowed_user_defined_function_ids   s    rq   c                  C   sn   dd t j D } | dd tj D  | dd tjtjfD  | tt	di d| tt
j< | S )Nc                 S   s2   i | ]*\}}| d st|rt|d| qS _z	builtins.rR   callabler2   r/   kvr   r   r   
<dictcomp>   s
   
  z)_builtin_function_ids.<locals>.<dictcomp>c                 S   s2   i | ]*\}}| d st|rt|d| qS )rs   z	operator.rt   rv   r   r   r   ry     s
   
  c                 S   s   i | ]}t |d |j qS )z
functools.)r2   r#   )r/   rx   r   r   r   ry     s      ztyping.castzfunctools.reduce)builtinsrJ   r]   updateoperator	itertoolschainislicer2   r   	functoolsreducero   r   r   r   _builtin_function_ids   s    r   c                     s2   t  } tD ]" |  fdd j D  q
| S )Nc                    sF   i | ]>\}}t |rt|d dp$ j jkrt| j d| qS )r$   NrQ   )ru   ri   r#   r2   rv   modr   r   ry     s
    z'_numpy_function_ids.<locals>.<dictcomp>)r   r   r{   rJ   r]   ro   r   r   r   _numpy_function_ids  s    
r   c                  C   s   dd t j D } | S )zC
    Collects constant builtins by eliminating callable items.
    c                 S   s2   i | ]*\}}| d st|st|d| qS rr   rt   rv   r   r   r   ry   '  s
   
  z)_builtin_constant_ids.<locals>.<dictcomp>)rz   rJ   r]   ro   r   r   r   _builtin_constant_ids"  s    r   c                 C   s8   t | tkrdS t | tkp6t| tjjtjjtjjfS )z&Is this safe to trace like torch.add ?F)	r2   rM   rn   r   r-   r^   ZOpOverloadPacketZ
OpOverloadZ_OpNamespacer0   r   r   r   
is_allowed/  s    r   c                 C   s   t | tkS r   )r2   rq   r   r   r   r   is_user_defined_allowed=  s    r   c                 C   s   t t| |S )z&Convert a torch.* function to a string)rn   r   r2   )r0   r   r   r   r   torch_get_nameA  s    r   c                 C   s   t | tkS r   )r2   r   r   r   r   r   is_builtin_callableF  s    r   c                 C   s   t | tkS r   )r2   r   r   r   r   r   is_builtin_constantJ  s    r   c                 C   s$   t d krdS t| t jp"t| tkS )NF)npr   Zndarrayr2   r   r   r   r   r   is_numpyN  s    r   )-rz   r6   r8   r   r:   r}   r<   r|   ra   rD   typingr   r   r   r   numpyr   ModuleNotFoundErrorr-   Ztorch._functorch.deprecatedZ
_functorch
deprecatedr`   Ztorch.fx._symbolic_tracer    r   Zexternal_utilsr	   utilsr
   r   r*   rM   rn   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sN   
+
8
c



