U
    ©Ê¹jS  ã                   @   s¸   d dl Z d dlmZ d dlmZmZmZmZm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 e  d¡dd„ ƒZe  d¡dd„ ƒZG d	d
„ d
e	jjƒZdS )é    N)Únullcontext)ÚAnyÚCallableÚDictÚOptionalÚSequence)Útorch_function_passthroughc               *   C   s   t t jft jt jjft jjt jjjft jt jjft jt jjft jt jjfg} t jjt jj	t jj
t jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jjt jj t jj t jj!t jj!t jj"t jj"t jj#t jj#t jj$t jj$t jj%t jj%t jj&t j'j(t jj)t j'j)i}| D ]0\}}|j*D ]}|j+ ,|¡||j+ ,|¡< �q^�qPt-t jƒD ].}|t jj*k�rŒt jj+ ,|¡|t.t j|ƒ< �qŒt jj/j*D ]4}t.t j|dƒ�pât.t |ƒ}t jj/j+ ,|¡||< �qÆ|S )z}
    Mapping of torch API functions to torch._refs functions.
    E.g. torch_to_refs_map()[torch.add] == torch._refs.add
    N)0ÚtorchZ_refsÚnnZ
functionalÚspecialZfftZlinalgZTensorÚ
__invert__Zbitwise_notÚ__xor__Zbitwise_xorÚ__and__Zbitwise_andÚ__or__Z
bitwise_orÚ__eq__ÚeqÚ__rsub__ZrsubÚ__rtruediv__ZrtruedivÚ__floordiv__Zfloor_divideÚ__rfloordiv__Z	rfloordivÚ__pow__ÚpowÚ__rpow__ZrpowZ	new_emptyZnew_fullZ	new_zerosZnew_onesZfill_Zzero_ÚtoZsum_to_sizeZcopy_Ú_primsZcopy_toÚresizeÚ__all__Ú__dict__ÚgetÚdirÚgetattrZ_conversions)ÚmodulesÚrZ	mod_torchZmod_refsÚsZtensor_attr© r$   úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/_prims/context.pyÚtorch_to_refs_map   sx    ú	                     ê
 r&   c                   C   s   dd„ t jjD ƒS )zJ
    Set of all prim functions, e.g., torch._prims.add in all_prims()
    c                 S   s   h | ]}t jj |¡’qS r$   )r	   r   r   r   )Ú.0r#   r$   r$   r%   Ú	<setcomp>O   s     zall_prims.<locals>.<setcomp>)r	   r   r   r$   r$   r$   r%   Ú	all_primsJ   s    r)   c                   @   sB   e Zd ZdZddd„ efdd„Zdeeee e	e
 d	œd
d„ZdS )ÚTorchRefsModeaB  
    Switches the interpretation of torch.* functions and Tensor methods to
    use PrimTorch refs in torch._refs.  (Direct calls to _refs are unaffected.)

    >>> # xdoctest: +SKIP
    >>> with TorchRefsMode():
    ...     torch.add(x, y)  # calls torch._refs.add(x, y)

    By default, this context manager will fall back on the torch.* if the
    ref does not exist; set strict=True to error if this occurs.
    If the ref exists we still would like to fall back on the torch.* sometimes,
    this behavior can be customized by passing a function to should_fallback_fn.
    Fc                  G   s   dS )NFr$   )Ú_r$   r$   r%   Ú<lambda>d   ó    zTorchRefsMode.<lambda>c                 C   s   || _ || _|| _d S )N)ÚstrictÚshould_fallback_fnÚprims_mode_cls)Úselfr.   r/   r0   r$   r$   r%   Ú__init__a   s    zTorchRefsMode.__init__r$   N)Ú	orig_funcÚtypesÚargsÚkwargsc              
   C   sè   |d kri }|t ks|tƒ krD|  ¡ � |||ŽW  5 Q R £ S Q R X tƒ }| |d ¡}|d kr|t|tjjƒr|tj	j
 |d ¡}|d k	rÂ|  | ||||¡r |||ŽS | � |||ŽW  5 Q R £ S Q R X | jrÞtdtj |¡› �ƒ‚|||ŽS )Nzno _refs support for )r   r)   r0   r&   r   Ú
isinstancer	   Z_opsZ
OpOverloadÚ_decompZdecomposition_tabler/   r.   ÚRuntimeErrorÚ	overridesÚresolve_name)r1   r3   r4   r5   r6   ÚmappingÚfuncr$   r$   r%   Ú__torch_function__k   s&    

ÿz TorchRefsMode.__torch_function__)r$   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r2   r   r   r   r   r   r>   r$   r$   r$   r%   r*   R   s   ü
  ûûr*   )Ú	functoolsÚ
contextlibr   Útypingr   r   r   r   r   r	   Ztorch._decompZtorch._primsZtorch._refsZtorch._refs.nnZtorch._refs.nn.functionalZtorch._refs.specialZtorch.overridesZtorch._prims_commonr   Ú	lru_cacher&   r)   r:   ZTorchFunctionModer*   r$   r$   r$   r%   Ú<module>   s    
6
