o
    h)                     @   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yE   dZY nw 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dede
fdd	Zdefd
dZdefddZdefddZdS )z+make_function_id_set.<locals>.FunctionIdSetNfunction_idsfunction_namesc                    sN   | j d u r$  }t|trt| | _ || _| j S t|ts!J || _ | j S N)r   
isinstancedictsetkeysr   )selfvaluelazy_initializer U/var/www/html/ai/venv/lib/python3.10/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_namec                 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   ||  v r| j | d S d S r   )r   remover   r   r   r   r    P   s   
z2make_function_id_set.<locals>.FunctionIdSet.removec                 S   s
   ||  v 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&' rQddl(m)} t*j+,| dd tj-. D }| |7 } dd tj-. D }| |7 } tj&' rz| /tj&j0j1 dd	 | D S )
NTFr   )_functional_collectivesc                 S       g | ]}t |ttjr|qS r   )r   typetorchfloat32.0objr   r   r   
<listcomp>   s
    z,_disallowed_function_ids.<locals>.<listcomp>c                 S   r+   r   )r   r,   r-   FloatStorager/   r   r   r   r2      s    c                 S   s   h | ]}t |qS r   )idr0   xr   r   r   	<setcomp>       z+_disallowed_function_ids.<locals>.<setcomp>)2collectionsOrderedDictcopydeepcopyinspect	signaturemath__package__r-   __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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Tensor__init__distributedis_availabletorch.distributedr*   r   !skipfiles_inline_module_allowlistr   __dict__valuesextenddistributed_c10d'dynamo_unsupported_distributed_c10d_ops)r    r*   dtypesstorager   r   r   _disallowed_function_idsZ   sX   


ri   c                     s   t jdtdd t dd  fdd  t  t ttjD ]} ttj| }t	|t
jt
jfr>d|  t|< q$t D ]	}|v rK|= qBttfD ]}|j d	|j t|< qP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 u rdS |j t fdd|D r(dS  |v p0 |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   r5   r   r   r   r2      r8   zL_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<listcomp>Fc                 3   s    | ]}  |V  qd S r   )
startswith)r0   mmod_namer   r   	<genexpr>   s    zK_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<genexpr>)tupler=   	getmoduler"   anyrn   )r1   allowed_modulesdisallowed_modulesallowed_modules_dotrl   r   rp   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 ]n\}}t|vrdd l}t	||j
jr2q||jjtj|jjtjfv rAqt	|tjrb|jdra|ra j d| t|< | q|rs j d| t|< qt|d u rt|s j d| t|< qd S )Nc                 3   s    | ]	} j |V  qd S r   )r"   rn   )r0   rq   rl   r   r   rr      s
    

zE_allowed_function_ids.<locals>._find_torch_objects.<locals>.<genexpr>r   ztorch.rm   )ru   r   *allowed_functions_module_string_ignorelistr"   r4   listrb   items
torch._opsr   _opsHigherOrderOperatorfuncrE   deprecated_funcvmaptypes
ModuleTypern   r=   rt   r
   )rl   namer1   r-   _find_torch_objectsry   torch_object_idsrz   r   r      s<   z2_allowed_function_ids.<locals>._find_torch_objectsztorch.Tensor.rm   )rV   filterwarningsUserWarningr   r-   r?   dirr\   getattrr   r   MethodDescriptorTypeWrapperDescriptorTyper4   ri   r   r	   r#   r"   )r   methodr   extrar   r   r   _allowed_function_ids   s(   (
r   c                  C   s   i } | S r   r   rvr   r   r   "_allowed_user_defined_function_ids   s   r   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   2   i | ]\}}| d st|rt|d| qS _z	builtins.rn   callabler4   r0   kvr   r   r   
<dictcomp>       z)_builtin_function_ids.<locals>.<dictcomp>c                 S   r   )r   z	operator.r   r   r   r   r   r     r   c                 S   s   i | ]}t |d |j qS )z
functools.)r4   r"   )r0   r   r   r   r   r     s    ztyping.castzfunctools.reduce)builtinsrb   r}   updateoperator	itertoolschainislicer4   r   	functoolsreducer   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#   Nrm   )r   r   r"   r4   r   modr   r   r     s    z'_numpy_function_ids.<locals>.<dictcomp>)r   r   r   rb   r}   r   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 r   r   r   r   r   r   r   '  r   z)_builtin_constant_ids.<locals>.<dictcomp>)r   rb   r}   r   r   r   r   _builtin_constant_ids"  s   r   c                 C   s8   t | tv rdS t | tv pt| tjjtjjtjjfS )z&Is this safe to trace like torch.add ?F)	r4   ri   r   r   r-   r   OpOverloadPacket
OpOverload_OpNamespacer1   r   r   r   
is_allowed/  s   r   c                 C      t | tv S r   )r4   r   r   r   r   r   is_user_defined_allowed=     r   c                 C   s   t t| |S )z&Convert a torch.* function to a string)r   r   r4   )r1   r   r   r   r   torch_get_nameA  s   r   c                 C   r   r   )r4   r   r   r   r   r   is_builtin_callableF  r   r   c                 C   r   r   )r4   r   r   r   r   r   is_builtin_constantJ  r   r   c                 C   s$   t d u rdS t| t jpt| tv S )NF)npr   ndarrayr4   r   r   r   r   r   is_numpyN  s   r   )-r   r9   r;   r   r=   r   r?   r   r   rV   typingr   r   r   r   numpyr   ModuleNotFoundErrorr-   torch._functorch.deprecated
_functorch
deprecatedr   torch.fx._symbolic_tracer    r   external_utilsr	   utilsr
   r   r)   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sT    +
8
c



