o
    ,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r*d 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Zej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_Rreturndict[Any, Any]c               	   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} i 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 ]\}}|j*D ]}|j+,|||j+,|< qqt-t jD ]}|t jj*v rt jj+,||t.t j|< qt jj/j*D ]}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torch_refsnn
functionalspecialfftlinalgTensor
__invert__bitwise_not__xor__bitwise_xor__and__bitwise_and__or__
bitwise_or__eq__eq__rsub__rsub__rtruediv__rtruediv__floordiv__floor_divide__rfloordiv__	rfloordiv__pow__pow__rpow__rpow	new_emptynew_full	new_zerosnew_onesfill_zero_tosum_to_sizecopy__primscopy_toresize__all____dict__getdirgetattr_conversions)modulesr	mod_torchmod_refsstensor_attr rE   O/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/_prims/context.pytorch_to_refs_map   sv   	

rG   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 rE   )r   r6   r:   r;   ).0rC   rE   rE   rF   	<setcomp>W   s    zall_prims.<locals>.<setcomp>)r   r6   r9   rE   rE   rE   rF   	all_primsR   s   rK   c                   @  s6   e Zd ZdZddd efdddZ		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 )NFrE   )_rE   rE   rF   <lambda>l   s    zTorchRefsMode.<lambda>strictboolshould_fallback_fnCallable[..., bool]prims_mode_clstyper   Nonec                 C  s   || _ || _|| _d S )N)rO   rQ   rS   )selfrO   rQ   rS   rE   rE   rF   __init__i   s   
zTorchRefsMode.__init__rE   N	orig_funcCallable[_P, _R]typesSequence[type]argsSequence[Any]kwargsdict[str, Any] | Noner   c                 C  sl  |d u ri }|t v s|t v r*|   ||i |W  d    S 1 s%w   Y  t }||d }|d u rGt|tjjrGtj	j
|d }n-|d u rtt|tjjrtt|dd }|d u rh|jrht||jd d }|d urttj	j
|d }|d ur| | ||||r||i |S |  ||i |W  d    S 1 sw   Y  | jrtdtj| ||i |S )Ndefaultr   zno _refs support for )r
   rK   rS   rG   r;   
isinstancer   _ops
OpOverload_decompdecomposition_tableOpOverloadPacketr=   _dirrQ   rO   RuntimeError	overridesresolve_name)rV   rX   rZ   r\   r^   mappingfuncr`   rE   rE   rF   __torch_function__s   s6   
  z TorchRefsMode.__torch_function__)rO   rP   rQ   rR   rS   rT   r   rU   )rE   N)
rX   rY   rZ   r[   r\   r]   r^   r_   r   r   )__name__
__module____qualname____doc__r   rW   rm   rE   rE   rE   rF   rL   Z   s    rL   )r   r   )r   rH   ) 
__future__r   	functools
contextlibr   typingr   r   r   r   typing_extensionsr   collections.abcr	   r   torch._decomptorch._primstorch._refstorch._refs.nntorch._refs.nn.functionaltorch._refs.specialtorch.overridestorch._prims_commonr
   r   r   cacherG   rK   ri   TorchFunctionModerL   rE   rE   rE   rF   <module>   s.    6