U
    ªÊ¹j.  ã                   @   sÔ  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mZ d dlmZmZ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*ee+ƒZ,ee-eeg df  f e.d< e-eg df ddœdd„Z/e0ddœdd„Z1dd„ Z2d d!„ Z3d"d#„ Z4d$d%„ Z5d&d'„ Z6d(d)„ Z7d*d+„ Z8dS ),é    N)Údefaultdict)ÚCallableÚcastÚDictÚListÚ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/bwave/python/venv/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_nameJ   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"   N   s    z/make_function_id_set.<locals>.FunctionIdSet.addc                 S   s   || ƒ kr| j  |¡ d S r   )r   Úremover#   r   r   r   r$   R   s    
z2make_function_id_set.<locals>.FunctionIdSet.removec                 S   s
   || ƒ kS r   r   r#   r   r   r   Ú__contains__V   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   ÚFunctionIdSet;   s   
r,   r   )r   r,   r   r   r   Úmake_function_id_set1   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& '¡ r¢d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/   r0   ZFloatStorager1   r   r   r   r4   …   s   þc                 S   s   h | ]}t |ƒ’qS r   )Úid©r2   Úxr   r   r   Ú	<setcomp>’   s     z+_disallowed_function_ids.<locals>.<setcomp>)2ÚcollectionsÚOrderedDictÚcopyÚdeepcopyÚinspectÚ	signatureÚmathÚ__package__r0   Ú__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_ids\   sX    
å
ÿþ
ÿrP   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„ˆ|= 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 kr0dS |j‰ t‡ fdd„|D ƒƒrPdS ˆ |kp`ˆ  |¡S )N)r0   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   r6   r   r   r   r4   °   s     zL_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<listcomp>Fc                 3   s   | ]}ˆ   |¡V  qd S r   )Ú
startswith)r2   Úm©Úmod_namer   r   Ú	<genexpr>·   s     zK_allowed_function_ids.<locals>._is_allowed_module_prefix.<locals>.<genexpr>)Útupler=   Ú	getmoduler&   ÚanyrU   )r3   Zallowed_modulesZdisallowed_modulesZallowed_modules_dotrS   r   rW   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r‚q8t	|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&   rU   )r2   rX   ©rS   r   r   rY   ½   s   ÿzE_allowed_function_ids.<locals>._find_torch_objects.<locals>.<genexpr>r   ztorch.rT   )r\   r   Z*allowed_functions_module_string_ignorelistr&   r5   ÚlistrM   ÚitemsZ
torch._opsr   Ú_opsZHigherOrderOperatorÚfuncrB   Údeprecated_funcZvmapÚtypesÚ
ModuleTyperU   r=   r[   r   )rS   Únamer3   r0   ©Ú_find_torch_objectsr]   Ztorch_object_idsr^   r   rh   ¼   s6    þüÿ
z2_allowed_function_ids.<locals>._find_torch_objectsztorch.Tensor.rT   )rG   ÚfilterwarningsÚUserWarningr   r0   r?   ÚdirrJ   Úgetattrr   rd   ÚMethodDescriptorTypeÚWrapperDescriptorTyper5   rP   r	   r   r'   r&   )rf   Úmethodr   Úextrar   rg   r   Ú_allowed_function_ids•   s&    ( 
ÿ
rq   c                  C   s   i } | S r   r   ©Úrvr   r   r   Ú"_allowed_user_defined_function_idsù   s    rt   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.©rU   Úcallabler5   ©r2   ÚkÚvr   r   r   Ú
<dictcomp>  s
   
 þ z)_builtin_function_ids.<locals>.<dictcomp>c                 S   s2   i | ]*\}}|  d ¡st|ƒrt|ƒd|› �“qS )rv   z	operator.rw   ry   r   r   r   r|     s
   
 þ c                 S   s   i | ]}t |ƒd |j› �“qS )z
functools.)r5   r&   )r2   r{   r   r   r   r|     s      ztyping.castzfunctools.reduce)ÚbuiltinsrM   r`   ÚupdateÚoperatorÚ	itertoolsÚchainÚislicer5   r   Ú	functoolsÚreducerr   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'   NrT   )rx   rl   r&   r5   ry   ©Úmodr   r   r|     s
   ý z'_numpy_function_ids.<locals>.<dictcomp>)r   r   r~   rM   r`   rr   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 ru   rw   ry   r   r   r   r|   )  s
   
 þ z)_builtin_constant_ids.<locals>.<dictcomp>)r}   rM   r`   rr   r   r   r   Ú_builtin_constant_ids$  s    þr‰   Ú_lazy_module_init)rf   Ú	init_funcÚreturnc                 C   sD   d| kst d| › �ƒ‚| tjkr&|ƒ  | tks2t ‚t|   |¡ dS )z.Register a module without eagerly importing itrT   z%Expected a root module name, but got N)r   ÚsysÚmodulesrŠ   Úappend)rf   r‹   r   r   r   Úadd_module_init_func4  s
    
r�   )r3   rŒ   c                 C   sN   t | dd ƒ}|d krd S | d¡d }t |d ¡}|d k	rJ|D ]
}|ƒ  q>d S )Nr'   rT   r   )rl   ÚsplitrŠ   Úpop)r3   rS   Úbase_moduleZ
init_funcsÚfnr   r   r   Ú_maybe_init_lazy_module@  s    r•   c                 C   sD   t | ƒ t| ƒtkrdS t| ƒtkr(dS t| tjjtjjtjj	fƒS )z&Is this safe to trace like torch.add ?FT)
r•   r5   rP   rq   r   r0   ra   ZOpOverloadPacketZ
OpOverloadZ_OpNamespace©r3   r   r   r   Ú
is_allowedL  s    þr—   c                 C   s   t | ƒ t| ƒtkS r   )r•   r5   rt   r–   r   r   r   Úis_user_defined_allowed_  s    r˜   c                 C   s   t | ƒ t| ddƒS )NZ_dynamo_forbiddenF)r•   rl   r–   r   r   r   Úis_forbiddend  s    r™   c                 C   s   t  t| ƒ|¡S )z&Convert a torch.* function to a string)rq   r!   r5   )r3   r   r   r   r   Útorch_get_namei  s    rš   c                 C   s   t | ƒtkS r   )r5   r…   r–   r   r   r   Úis_builtin_callablen  s    r›   c                 C   s   t | ƒtkS r   )r5   r‰   r–   r   r   r   Úis_builtin_constantr  s    rœ   c                 C   s$   t d krdS t| t jƒp"t| ƒtkS )NF)Únpr   Zndarrayr5   rˆ   r–   r   r   r   Úis_numpyv  s    rž   )9r}   r9   r;   rƒ   r=   r€   r?   r   r�   rd   rG   r   Útypingr   r   r   r   r   r   Únumpyr�   ÚModuleNotFoundErrorr0   Ztorch._functorch.deprecatedZ
_functorchÚ
deprecatedrc   Ztorch.fx._symbolic_tracer	   Ú r   Zexternal_utilsr   Úutilsr   r   r-   rP   rq   rt   r…   rˆ   r‰   r_   rŠ   r+   r*   r�   Úobjectr•   r—   r˜   r™   rš   r›   rœ   rž   r   r   r   r   Ú<module>   s\     
+
8
c



$