
    Vh                     "   d dl Z d dlmZ d dlmZ d dl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 j&                  d      d        Z e j&                  d      d        Z G d d	e	j,                  j.                        Zy)
    N)Sequence)nullcontext)AnyCallableOptional)torch_function_passthroughc            	         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                  j0                  t         j                  j2                  t         j                  j4                  t         j                  j6                  t         j                  j8                  t         j                  j:                  t         j                  j<                  t         j                  j<                  t         j                  j>                  t         j                  j>                  t         j                  j@                  t         j                  j@                  t         j                  jB                  t         j                  jB                  t         j                  jD                  t         j                  jD                  t         j                  jF                  t         j                  jF                  t         j                  jH                  t         j                  jH                  t         j                  jJ                  t         j                  jJ                  t         j                  jL                  t         jN                  jP                  t         j                  jR                  t         jN                  jR                  i}| D ]M  \  }}|jT                  D ]9  }|jV                  jY                  |      ||jV                  jY                  |      <   ; O t[        t         j                        D ]c  }|t         j                  jT                  v s t         j                  jV                  jY                  |      |t]        t         j                  |      <   e t         j                  j^                  jT                  D ]e  }t]        t         j                  |d      xs t]        t         |      }t         j                  j^                  jV                  jY                  |      ||<   g |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_attrs         D/home/dcms/DCMS/lib/python3.12/site-packages/torch/_prims/context.pytorch_to_refs_maprA      s0    
	5;;>>"			ekknn778	++,	EKKOO$	u{{))*G!8!8ekk55 	ekk55 	U[[33	
 	U[[^^ 	u{{// 	!!5;;#7#7 	!!5;;#;#; 	""EKK$9$9 	ekkoo 	u{{// 	 5 5 	u{{33 	 5 5 	u{{33  	EKK--!" 	EKK--#$ 	  %++"9"9ELL00U\\00-A0  ' D	8!! 	DA+3+<+<+@+@+CAi  $$Q'(	DD
  F###*/++*>*>*B*B1*EAgellA&'F
 [[%%-- BellAt4Iq8I11::>>qA+B H    c                      t         j                  j                  D  ch c]+  } t         j                  j                  j	                  |       - c} S c c} w )zJ
    Set of all prim functions, e.g., torch._prims.add in all_prims()
    )r
   r1   r4   r5   r6   )r>   s    r@   	all_primsrD   I   s7    
 38,,2F2FGQELL!!%%a(GGGs   0Ac            
       J    e Zd ZdZdd efdZ	 	 ddededee   d	e	e
   fd
Zy)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                       y)NF )_s    r@   <lambda>zTorchRefsMode.<lambda>c   s    rB   c                 .    || _         || _        || _        y )N)strictshould_fallback_fnprims_mode_cls)selfrL   rM   rN   s       r@   __init__zTorchRefsMode.__init__`   s     "4,rB   N	orig_functypesargskwargsc                 t   |i }|t         v s|t               v r"| j                         5   ||i |cd d d        S t               }|j	                  |d       }|Ot        |t        j                  j                        r+t        j                  j                  j	                  |d       }n|t        |t        j                  j                        rat        |dd       }|&|j                  rt        ||j                  d   d       }|*t        j                  j                  j	                  |d       }|1| j                  | ||||      r ||i |S | 5   ||i |cd d d        S | j                  r+t!        dt        j"                  j%                  |              ||i |S # 1 sw Y   pxY w# 1 sw Y   UxY w)Ndefaultr   zno _refs support for )r   rD   rN   rA   r6   
isinstancer
   _ops
OpOverload_decompdecomposition_tableOpOverloadPacketr8   _dirrM   rL   RuntimeError	overridesresolve_name)rO   rQ   rR   rS   rT   mappingfuncrV   s           r@   __torch_function__z TorchRefsMode.__torch_function__j   s    >F 22i9;6N$$& 2 $1&12 2#%{{9d+ <Jy%**2G2GH==4488DID\jEJJ4O4OPiD9G9>>!)Y^^A->E"}}88<<WdK&&tYdFK $1&11 -T,V,- -;;'(D(DY(O'PQ  $)&))?2 22- -s   F!F.!F+.F7)rH   N)__name__
__module____qualname____doc__r   rP   r   r   r   r   dictrc   rH   rB   r@   rF   rF   Q   sR      +"	- !!%+*+* +* sm	+*
 +*rB   rF   )	functoolscollections.abcr   
contextlibr   typingr   r   r   r
   torch._decomptorch._primstorch._refstorch._refs.nntorch._refs.nn.functionaltorch._refs.specialtorch.overridestorch._prims_commonr   	lru_cacherA   rD   r_   TorchFunctionModerF   rH   rB   r@   <module>rw      s     $ " * *          : T3 3l TH HD*EOO55 D*rB   