a
    h                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZ erTd dl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Ze	d	Zejd
dddZejddddZG dd dejjZdS )    )annotationsN)nullcontext)AnyCallableTYPE_CHECKINGTypeVar)	ParamSpec)Sequence)torch_function_passthrough_P_Rzdict[Any, Any])returnc               *   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*v rt jj+,||t.t j|< qt jj/j*D ]4}t.t j|dpt.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
functionalZ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(   B/var/www/auris/lib/python3.9/site-packages/torch/_prims/context.pytorch_to_refs_map   sN    	
 r*   zset[Any]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>W       zall_prims.<locals>.<setcomp>)r   r   r    r(   r(   r(   r)   	all_primsR   s    r.   c                   @  sH   e Zd ZdZddd efddddd	d
dZdddddd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>l   r-   zTorchRefsMode.<lambda>boolzCallable[..., bool]typeNone)strictshould_fallback_fnprims_mode_clsr   c                 C  s   || _ || _|| _d S )N)r5   r6   r7   )selfr5   r6   r7   r(   r(   r)   __init__i   s    zTorchRefsMode.__init__r(   NzCallable[_P, _R]zSequence[type]zSequence[Any]zdict[str, Any] | Noner   )	orig_functypesargskwargsr   c                 C  st  |d u ri }|t v s|t v rT|   ||i |W  d    S 1 sJ0    Y  t }||d }|d u rt|tjjrtj	j
|d }nZ|d u rt|tjjrt|dd }|d u r|jrt||jd d }|d urtj	j
|d }|d urH| | ||||r||i |S |  ||i |W  d    S 1 s>0    Y  | jrftdtj| ||i |S )Ndefaultr   zno _refs support for )r
   r.   r7   r*   r"   
isinstancer   Z_opsZ
OpOverload_decompZdecomposition_tableZOpOverloadPacketr$   Z_dirr6   r5   RuntimeError	overridesresolve_name)r8   r:   r;   r<   r=   mappingfuncr>   r(   r(   r)   __torch_function__s   s2    
,
.z TorchRefsMode.__torch_function__)r(   N)__name__
__module____qualname____doc__r   r9   rF   r(   r(   r(   r)   r/   Z   s     r/   ) 
__future__r   	functools
contextlibr   typingr   r   r   r   Ztyping_extensionsr   collections.abcr	   r   Ztorch._decompZtorch._primsZtorch._refsZtorch._refs.nnZtorch._refs.nn.functionalZtorch._refs.specialZtorch.overridesZtorch._prims_commonr
   r   r   cacher*   r.   rB   ZTorchFunctionModer/   r(   r(   r(   r)   <module>   s,   6