a
    d"                     @   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 d dl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 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S )    N)DictOptionalSet)is_fx_tracing   )config)is_compiling)	HAS_NUMPYis_safe_constantnpc                    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 u rD  }t|tr0t| | _ || _nt|ts>J || _ | j S N)r   
isinstancedictsetkeysr   )selfvaluelazy_initializer h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_dynamo/allowed_functions.py__call__5   s    

z4make_function_id_set.<locals>.FunctionIdSet.__call__)idxdefaultc                 S   s   |   | j ||S r   )r   get)r   r   r   r   r   r   get_name@   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   D   s    z/make_function_id_set.<locals>.FunctionIdSet.addc                 S   s   ||  v r| j | d S r   )r   remover   r   r   r   r    H   s    
z2make_function_id_set.<locals>.FunctionIdSet.removec                 S   s
   ||  v S r   r   r   r   r   r   __contains__L   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   FunctionIdSet1   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jjtjj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j!j"t#j$tj%j&j'j(g} dd tj)* D }| |7 } dd tj)* D }| |7 } dd | D S )NTFc                 S   s    g | ]}t |ttjr|qS r   )r   typetorchfloat32.0objr   r   r   
<listcomp>s   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   r0   w   s   c                 S   s   h | ]}t |qS r   )idr.   xr   r   r   	<setcomp>}       z+_disallowed_function_ids.<locals>.<setcomp>)+collectionsOrderedDictcopydeepcopyinspect	signaturemath__package__r+   __builtins__Zautocast_decrement_nestingZautocast_increment_nestingZautogradZgradZclear_autocast_cacheZcudaZcurrent_deviceampZautocast_modeZautocastcpu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_dtypeZprofilerZprofilewarningswarnZ_CZ_dynamoZ
eval_frameunsupported__dict__values)r    ZdtypesZstorager   r   r   _disallowed_function_idsR   sL    


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rHd|  t|< qHt D ]}|v r~|= q~t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 u r0dS |j t fdd|D rPdS  |v p` |S )N)r+   r<   )ztorch.optim.z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.c                 S   s   g | ]}|d  qS ).r   r2   r   r   r   r0      r5   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>   r5   zK_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<genexpr>)tupler:   	getmoduler"   anyrN   )r/   Zallowed_modulesZdisallowed_modulesZ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 ]\}}t|vr8t|t	j
r|jdrވ|rވ j d| t|< | q8|r j d| t|< q8t|d u r8t|s8 j d| t|< q8d S )Nc                 3   s   | ]} j |V  qd S r   )r"   rN   )r.   rQ   rL   r   r   rR      s   zE_allowed_function_ids.<locals>._find_torch_objects.<locals>.<genexpr>ztorch.rM   )rU   r   Z*allowed_functions_module_string_ignorelistr"   r1   listrG   itemsr   types
ModuleTyperN   r:   rT   r
   )rL   namer/   _find_torch_objectsrV   Ztorch_object_idsrW   r   r^      s"    
z2_allowed_function_ids.<locals>._find_torch_objectsztorch.Tensor.rM   )rD   filterwarningsUserWarningr   r+   r<   dirZTensorgetattrr   rZ   MethodDescriptorTyper1   rI   r   r   r#   r"   )r\   methodr   extrar   r]   r   _allowed_function_ids   s     
rf   c                  C   s\   dd t j D } | dd tj D  | dd tjtjfD  d| tt	j
< | S )Nc                 S   s2   i | ]*\}}| d st|rt|d| qS _z	builtins.rN   callabler1   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 )rh   z	operator.ri   rk   r   r   r   rn      s   c                 S   s   i | ]}t |d |j qS )z
functools.)r1   r"   )r.   rm   r   r   r   rn      r5   zfunctools.reduce)builtinsrG   rY   updateoperator	itertoolschainislicer1   	functoolsreducervr   r   r   _builtin_function_ids   s    ry   c                     s<   t  } tr8ttjf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   )rj   rb   r"   r1   rk   modr   r   rn      s   z'_numpy_function_ids.<locals>.<dictcomp>)r   r	   r   randomrp   rG   rY   rw   r   rz   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 rg   ri   rk   r   r   r   rn      s   z)_builtin_constant_ids.<locals>.<dictcomp>)ro   rG   rY   rw   r   r   r   _builtin_constant_ids   s    r~   c                 C   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 ?)r1   rf   r   r+   Z_opsZOpOverloadPacketZ
OpOverloadZ_OpNamespacer/   r   r   r   
is_allowed   s    r   c                 C   s   t t| |S )z%Convert a torch.* funcion to a string)rf   r   r1   )r/   r   r   r   r   torch_get_name	  s    r   c                 C   s   t | tv S r   )r1   ry   r   r   r   r   is_builtin_callable  s    r   c                 C   s   t | tv S r   )r1   r~   r   r   r   r   is_builtin_constant  s    r   c                 C   s$   t rt| tjpt| tv S dS d S )NF)r	   r   r   Zndarrayr1   r}   r   r   r   r   is_numpy  s    r   )$ro   r6   r8   ru   r:   rr   r<   rq   rZ   rD   typingr   r   r   r+   Ztorch.fx._symbolic_tracer    r   Zexternal_utilsr   utilsr	   r
   r   r)   rI   rf   ry   r}   r~   r   r   r   r   r   r   r   r   r   <module>   s>   +
-
K


