
    BjL                        d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZ 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mZmZmZ d dlmZ d d	lmZ d d
lm Z m!Z!  ed          Z" ed          Z#ededej$        defd            Z%ededej$        defd            Z%ededej$        defd            Z%edddej$        ddfd            Z%d Z%dede&defdZ'd Z( G d d          Z)dedefdZ*	 d1dededej+        dz  fdZ,dede-fd Z.d!ed"eddfd#Z/d$d%d!ed"ed&e-fd'Z0d$d$d$d(d)e1d&e-d*e-d+e-deee#e"f         gee#e"f         f         f
d,Z2d-efd.Z3d/ Z4d-ee#e"f         dee#e"ez  f         fd0Z5dS )2    N)CallableSequence)wraps)GenericAlias)
NamedTupleoverloadTypeVar)	ParamSpec)CustomOutParamAnnotationELEMENTWISE_TYPE_PROMOTION_KINDNumber
NumberType	ShapeType
TensorLikeTensorLikeType)_pytree)
_fast_bind)tree_flattentree_unflatten_T_Padtypereturnc                     d S N r   r   s     _/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/torch/_prims_common/wrappers.py_maybe_convert_to_dtyper       	     	D    c                     d S r   r   r   s     r   r    r    %   r!   r"   c                     d S r   r   r   s     r   r    r    +   r!   r"   c                     d S r   r   r   s     r   r    r    1   s    Dr"   c                 ~   t          | t                    r"| j        k    r|                               S | S t          | t                    r t          j                  |           S t          | t                    rt          fd| D                       S | d S t          dt          |            d          )Nc              3   8   K   | ]}t          |          V  d S r   r    ).0xr   s     r   	<genexpr>z*_maybe_convert_to_dtype.<locals>.<genexpr>?   s.      BB1,Q66BBBBBBr"   zReceived unsupported type z+. Expected TensorLike, Number, or Sequence.)
isinstancer   r   tor   utilsdtype_to_type_ctorr   tuple
ValueErrortyper   s    `r   r    r    7   s    !Z   7e44;;!V 2.u'..q111!X CBBBBBBBBBB 	yt
YT!WWYYY  r"   typc                    t          | t                    s"dt          |            d}t          |          t	          j        t          |           |          s(d|  dt          |            d| d}t          |           ||           S )NzFound unknown type z  when trying to convert scalars!zScalar z	 of type z cannot be safely cast to type !)r,   r   r2   r1   r.   is_weakly_lesser_type)r   r3   msgs      r   _maybe_convert_to_typer8   J   s    a   MDGGMMMoo&tAww44 RRRDGGRRCRRRoo3q66Mr"   c                 j    t          |d          r |j        D ]}t          | |          r dS dS | |u S )N__args__)r3   
annotationTF)hasattrr:   _annotation_has_type)r3   r;   r   s      r   r=   r=   U   sW    z:&& $ 	 	A#::: ttu*r"   c                   J    e Zd ZdZdddedee         dz  fdZdedefd	Z	dS )
"elementwise_type_promotion_wrappera  
    Adds elementwise type promotion to a Python reference implementation.

    Takes two kwargs, type_promoting_args and type_promotion_kind.

    type_promoting_args must be a string Sequence specifying the argument names of all
    arguments that participate in type promotion (and should be type promoted). If the
    arg specifies a Sequence-type then every element of the Sequence will participate in
    type promotion.

    type_promotion_kind must be one of the kinds specified by ELEMENTWISE_TYPE_PROMOTION_KIND.
    See its documentation for details.

    The return_dtype will be coerced to the wrapped function's dtype arg if it is available and
    not None.

    Other type promotion behavior, like validating the Python type of scalar arguments, must
    be handled separately.
    N)type_promoting_argstype_promotion_kindr@   c                "    || _         || _        d S r   )type_promoting_arg_namesrA   )selfrA   r@   s      r   __init__z+elementwise_type_promotion_wrapper.__init__t   s     )<%#6   r"   fnr   c                      t          j                  t          j        t	                     fd                        }|_        |S )Nc                  J  	 t          g| R i |t          fdj        D                       }t          j        | }t          j        |dj        i\  	fdj        D             }j        	                    |            
di j        }dj        v rj        d         }|r|	t          |t                    rt          |	          S t          |t                    rt          	fd|D                       S t          dt          |                     )Nc              3   D   K   | ]}|j         v j         |         V  d S r   )	arguments)r)   r*   bounds     r   r+   zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>   sC       ( ('' "''''( (r"   rA   c                 Z    i | ]'}|j         v |t          j         |                   (S r   )rJ   r    )r)   r*   rK   compute_dtypes     r   
<dictcomp>zLelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<dictcomp>   sD       '' *5?1+=}MM'''r"   r   c              3   8   K   | ]}t          |          V  d S r   r(   )r)   r*   result_dtypes     r   r+   zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>   s.      VV!4QEEVVVVVVr"   zUnhandled result type: r   )r   r0   rC   pytreearg_tree_leavesr.   elementwise_dtypesrA   rJ   updater,   r   r    r   AssertionErrorr2   )argskwargsr@   flattened_type_promoting_argspromoted_argsresultmaybe_dtyperK   rM   rP   rF   rD   sigs          @@@r   _fnz8elementwise_type_promotion_wrapper.__call__.<locals>._fn   s    s4T444V44E"' ( ( ( (6( ( ( # # -3,BDW,X)*/*B.+$($<+ +'M<
    6  M
 O""=111R**%/**F %/))#og6 /#.L&*-- E.v|DDD&(++ WVVVVvVVVVVV !I4<<!I!IJJJr"   )inspect	signaturetorch_disable_dynamor   __signature__)rD   rF   r]   r\   s   `` @r   __call__z+elementwise_type_promotion_wrapper.__call__}   so    ## 
		r!	K !	K !	K !	K !	K !	K 
 
	!	KF  
r"   )
__name__
__module____qualname____doc__r   r   strrE   r   rc   r   r"   r   r?   r?   _   s         0 59	7 7 7 =7 &c]T1	7 7 7 7+8 + + + + + + +r"   r?   outshapec                     t          j        | j        |          rdS |                                 dk    r.dt	          | j                   d}t          j        |d           dS )NFr   zCAn output with one or more elements was resized since it had shape a   which does not match the required output shape {str(shape)}. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0).   )
stacklevelT)r.   
same_shaperj   numelrh   warningswarn)ri   rj   r7   s      r   _resize_output_checkrr      sy    	5)) u
yy{{atRUVYV_R`R` t t t 	 	ca((((4r"   memory_formatc                 T    t          | |          r|                     ||          S | S )N)rs   )rr   resize_)ri   rj   rs   s      r   _maybe_resize_outrv      s1    
 C'' {{5{>>>
r"   r*   c                 R    |                                  dk    o| j        j        dk    S )Nr   cpu)dimdevicer2   )r*   s    r   is_cpu_scalarr{      s"    5577a<2AHMU22r"   	copy_fromcopy_toc                 j    | j         |j         k    r"d| j          d|j          d}t          |          d S )NzAttempting to copy from device z to device z*, but cross-device copies are not allowed!)rz   RuntimeError)r|   r}   r7   s      r   check_copy_devicesr      s]    7>))Ti.> T T T T T 	 3 *)r"   F)exact_dtyper   c                 2    t                     st                      |r(t          j         j        j        k     fd           n7t          j        t          j         j        j                   fd                                          S )Nr|   r}   c                  *    d j          dj          dS )Nz"Expected out tensor to have dtype z	 but got z insteadr   r   s   r   <lambda>z _safe_copy_out.<locals>.<lambda>   s,     / / /}/ / / r"   )	cast_fromcast_toc                  *    d j          dj          dS )NzAttempting to cast from z to out tensor with dtype z0, but this can't be cast because it is not safe!r   r   s   r   r   z _safe_copy_out.<locals>.<lambda>   s-     =y = =ZaZg = = = r"   )r{   r   r`   _checkr   r.   can_safe_cast_tocopy_r|   r}   r   s   `` r   _safe_copy_outr      s     ## AY@@@@  
Ow},/ / / / /	
 	
 	
 	
 	"Y_gmTTT= = = = =	
 	
 	
 ==###r"   )r   pass_is_outpreserve_memory_format	out_namesr   r   c                      dt                    dk    rt                    dk    fddt          t          t          f         dt          t          t          f         f fd}|S )N)ri   r      c                 4    rt          j        |           nd S r   )r.   suggest_memory_format)tr   s    r   maybe_compute_memory_formatz0out_wrapper.<locals>.maybe_compute_memory_format   s    1GQu*1---TQr"   rF   r   c                    	 rt           nEt          t          t          d t          t	                              D                                 }rt           nJt          j        t          t          d t          t	                              D                                 }rt           n!t          d j         d D                       t          j	                   	dt          	fdD                       t                     dt          j        dt          j        f f	d	            }t          j        d
t          j        j        d|          }t#          	j        t&          t(          f          s.	j        	j        ||fv st-          d	j         d| d|           g 	j                                        |R }t3          |d           }t          j        |          |_        t9          t;           di                     |_        ||j        d
<   |j        d<   r
k    rd         |j        t>          <   dt@          j!         d|_"        |S )z?
        Adds the out parameter to a Python reference.
        c              3   $   K   | ]}t           V  d S r   r   r)   _s     r   r+   z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>  "      KK^KKKKKKr"   c              3   $   K   | ]}t           V  d S r   r   r   s     r   r+   z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>  r   r"   return_types_c                      g | ]}|t           fS r   r   )r)   os     r   
<listcomp>z5out_wrapper.<locals>._out_wrapper.<locals>.<listcomp>  s    888!^$888r"   )rz   r   c              3   *   K   | ]}|j         v V  d S r   )
parameters)r)   pr\   s     r   r+   z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>#  s*      HHAA/HHHHHHr"   rV   rW   c                  h  	  |                     dd           r 
D ]}t          |          }||vr|||<    fd}t          t          t          f          rD ]} ||           n |           r  dd ui|n  i |t
          u rt
          S t          t                    rst          t                    r t                    t                    k    sLj        dk    rt          t          t          f          s%t          dt                     d d           mrqj        dk    rft          t                    st          dt                               t          j                              t          		           nj        dk    r5t          t                    st          d
t                               n;t          t          t          f          st          dt                               t          j        t                    t                    k    fd           t!                    D ]6\  }}t          ||j         |                     t          ||		           7nrn  S )Nri   c                     t          | t                    r4t          d         t                    rt          d         |            d S d S d S )Nr   r   )r,   r   r   )ri   rV   s    r   maybe_check_copy_deviceszPout_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.maybe_check_copy_devices.  sa    c:.. G:d1gz3R3R G&a#FFFFFFG G G Gr"   is_outunbindzUnexpected result type: z, is_tensor=z, out_names=zout must be TensorLike, got r   zout must be tuple, got zout must be list or tuple, got c                  H    dt                     dt                      S )Nzexpected tuple of z elements but got )len)ri   rZ   s   r   r   z@out_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.<lambda>y  s(     ^S[[ ^ ^TWX[T\T\ ^ ^ r"   )popgetattrr,   r0   listNotImplementedr   r   rd   rU   r2   rv   rj   r   r`   _check_typezip)rV   rW   kout_attrr   r   rri   rZ   r   factory_kwargsrF   is_factory_fn	is_tensorr   r   r   return_types   `      @@r   r]   z.out_wrapper.<locals>._out_wrapper.<locals>._fn%  s   **UD))C -' - -A&sAH$,q	G G G G G #t}-- . 0 0A,,Q////0 )(--- -TF3d?FvFFT,V,,''%%FJ//4= vu-- Fs9~~55 K8++
6D%=0Q0Q+ %-tF|| - - - -!*- -  
 "   X!8!8%c:66 ,F499FF   &33F;;  
 #"( #$/     {h..)#u55 X"01V4PS991V1V"W"WWX  *#e}== "0 M$s)) M M# #  %CCKK/^^^^^   !$FC 0 0 X X1)!QW6Q6QRS6T6TUUU&A;WWWWWX
 #:33c)::r"   ri   N)kinddefaultr;   zUnexpected return annotation: z , expected str, TypeVar, empty, z, or c                     | j         S r   )r   )r   s    r   r   z3out_wrapper.<locals>._out_wrapper.<locals>.<lambda>  s    af r"   )key)r   return_annotation__annotations__r   r   zThis function is wrapped by z.out_wrapper)#r   r   r0   ranger   typesr   rd   r^   r_   allr   r   rV   rW   	ParameterKEYWORD_ONLYr,   r   rh   r	   emptyrU   r   valuessorted	Signaturerb   dictr   r   r   out_wrapperre   !_torch_decompositions_out_wrapper)rF   out_typebc_out_typer]   	out_paramparamsr   r   r   r\   default_out_namesr   r   r   r   r   s   `     @@@@r   _out_wrapperz!out_wrapper.<locals>._out_wrapper  s    NNuKKU3y>>5J5JKKKKK  	 NN#uKKU3y>>5J5JKKKKK  	 NN---88i888  	 ##,HHHHHHHHH	r\	;rw \	;") \	; \	; \	; \	; \	; \	; \	; \	; \	; \	; \	; \	; \	; 
\	;| %"/	
 
 
	 s,sGn==	$Hk(JJJ N1F N N19N N@KN N   5#.''))4944
 $4$4555#-)
 
 

 #72/@"#E#EFF%-E"(3H%  	I&777<EaLC 89 P;+AOOO 	- 
r"   )r   r   r   r   )r   r   r   r   r   r   r   r   s   ```` @@@r   r   r      s     !
9~~%	I!#IR R R R Rl"b&) lhr2v.> l l l l l l l l l l l\ r"   rF   c                 0    t          j        | d           S )Nc                 $    t          | d           S )Nr   )r<   )fs    r   r   z+_maybe_remove_out_wrapper.<locals>.<lambda>  s    71&IJJJ r"   )stop)r^   unwrap)rF   s    r   _maybe_remove_out_wrapperr     s$    >
JJ   r"   c                       fd G fddt           j        j                  t                     fd            }|S )Nc                     t           j                                        5   | i |cd d d            S # 1 swxY w Y   d S r   )r`   _C_AutoDispatchBelowAutograd)rV   rW   prims     r   redispatch_primz0backwards_not_supported.<locals>.redispatch_prim  s    X0022 	) 	)4(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   599c                   @    e Zd Ze fd            Zed             ZdS )6backwards_not_supported.<locals>.BackwardsNotSupportedc                 B    t          ||          \  }} ||          S r   )r   )ctx	args_spec	flat_argsrV   rW   r   s        r   forwardz>backwards_not_supported.<locals>.BackwardsNotSupported.forward  s*     *)Y??LD&"?4000r"   c                      t          d          )Nzbackwards not supported on prim)r   )r   rV   s     r   backwardz?backwards_not_supported.<locals>.BackwardsNotSupported.backward  s    @AAAr"   N)rd   re   rf   staticmethodr   r   )r   s   r   BackwardsNotSupportedr     sX        		1 	1 	1 	1 
	1 
	B 	B 
	B 	B 	Br"   r   c                      t          | |f          \  }}t          j                    r't          d |D                       r j        |g|R  S  | |          S )Nc              3   X   K   | ]%}t          |t          j                  |j        V  &d S r   )r,   r`   Tensorrequires_grad)r)   r   s     r   r+   zBbackwards_not_supported.<locals>._autograd_impl.<locals>.<genexpr>  sI       +
 +
 !*Q2M2M+
O+
 +
 +
 +
 +
 +
r"   )r   r`   is_grad_enabledanyapply)rV   rW   r   r   r   r   s       r   _autograd_implz/backwards_not_supported.<locals>._autograd_impl  s    +T6N;;	9 "" 	1s +
 +
%.+
 +
 +
 (
 (
 	1 /(.yE9EEEE"?4000r"   )r`   autogradFunctionr   )r   r   r   r   s   ` @@r   backwards_not_supportedr     s    ) ) ) ) )	B 	B 	B 	B 	B 	B 	B 7 	B 	B 	B 4[[1 1 1 1 1 [1& r"   c                 r     t          j                   }t                      fd            }||_        |S )zQ
    Allows unary operators that accept tensors to work with Python numbers.
    c                     t          |           dk    rt          | d         t                    rt          j        t          | d                             }t          |           }t          j        | d         |          |d<    |i |}t          |t          j	                  st          dt          |                     |                                S  | i |S )Nr   r   zExpected torch.Tensor, got )r   r,   r   r.   type_to_dtyper2   r   r`   tensorr   rU   item)rV   rW   r   args_rZ   rF   s        r   r]   z-elementwise_unary_scalar_wrapper.<locals>._fn  s    t99q==ZQ88='T!W66EJJE|DG5999E!HR)&))Ffel33 S$%Q4<<%Q%QRRR;;==  r4"6"""r"   )r^   r_   r   rb   )rF   r\   r]   s   `  r    elementwise_unary_scalar_wrapperr     sM     
B

C
2YY# # # # Y# CJr"   r   )6r^   r   rp   collections.abcr   r   	functoolsr   r   typingr   r   r	   typing_extensionsr
   r`   torch._prims_common_prims_commonr.   r   r   r   r   r   r   r   torch.utilsr   rQ   torch.utils._inspectr   torch.utils._pytreer   r   r   r   r   r    r2   r8   r=   r?   rr   rs   rv   boolr{   r   r   rh   r   r   r   r   r   r"   r   <module>r     s      . . . . . . . .             0 0 0 0 0 0 0 0 0 0 ' ' ' ' ' '  # # # # # #                  * ) ) ) ) ) + + + + + + < < < < < < < < WT]]Yt__ 
	~ 	ek 	n 	 	 	 
	 
	z 	%+ 	* 	 	 	 
	 
	x 	 	 	 	 	 
	 
	t 	EK 	D 	 	 	 
	
  &j t 
      I I I I I I I IZn Y    ( 15 	 &-   3^ 3 3 3 3 3 ^  n  QU         PU$ $ $ $+9$HL$ $ $ $4 #(	D D DDD D !	D
 xB (2r6"223D D D DN(    $ $ $TRb"z/!"     r"   