
    Ǆg.@                        d dl 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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mZ  ed      Z ed	      Z ed
edejB                  defd       Z"ed
edejB                  defd       Z"ed
e	dejB                  de	fd       Z"ed
ddejB                  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'	 d-dededeejP                     fdZ)dede*fdZ+ddd ed!ed"e*fd#Z,dddd$d%e-d"e*d&e*d'e*deee ef   gee ef   f   f
d(Z.d)efd*Z/d+ Z0d)edefd,Z1y).    N)wraps)Callable
NamedTupleOptionaloverloadSequenceTupleTypeVar)	ParamSpec)CustomOutParamAnnotationELEMENTWISE_TYPE_PROMOTION_KINDNumber
NumberType	ShapeType
TensorLikeTensorLikeType)_pytree)tree_flattentree_unflatten_T_Padtypereturnc                      y N r   r   s     d/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/_prims_common/wrappers.py_maybe_convert_to_dtyper               c                      y r   r   r   s     r   r    r        r!   r"   c                      y r   r   r   s     r   r    r    %   r!   r"   c                      y r   r   r   s     r   r    r    *   r!   r"   c                 <   t        | t              r"| j                  k7  r| j                        S | S t        | t              r t        j                        |       S t        | t              rt        fd| D              S | y t        dt        |        d      )Nc              3   6   K   | ]  }t        |        y wr   r    ).0xr   s     r   	<genexpr>z*_maybe_convert_to_dtype.<locals>.<genexpr>8   s     B1,Q6B   zReceived type z& that is neither a tensor or a number!)
isinstancer   r   tor   utilsdtype_to_type_ctorr   tuple
ValueErrortyper   s    `r   r    r    0   s    !Z 77e44;!V.u''.q11!XBBBB 	y
~d1gY.TU
VVr"   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   r3   r2   r/   is_weakly_lesser_type)r   r4   msgs      r   _maybe_convert_to_typer9   A   sp    a #DG9,LMo&&tAw4s)DG9,KC5PQRoq6Mr"   c                 d    t        |d      r!|j                  D ]  }t        | |      s y y| |u S )N__args__)r4   
annotationTF)hasattrr;   _annotation_has_type)r4   r<   r   s      r   r>   r>   L   s>    z:&$$ 	A#:	 *r"   c                   B    e Zd ZdZdddedeee      fdZde	de	fd	Z
y)
"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 specifiying 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_kindrA   c                     || _         || _        y r   )type_promoting_arg_namesrB   )selfrB   rA   s      r   __init__z+elementwise_type_promotion_wrapper.__init__k   s     )<%#6 r"   fnr   c                 n     t        j                        t               fd       }|_        |S )Nc                    	
  j                   | i |	t        	fdj                  D              }t        j                  | }t        j                  |dj                  i\  }
j                  D ci c]8  }|	j                  j                         v r|t        	j                  |   |      : }}	j                  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!        |             c c}w )Nc              3   t   K   | ]/  }|j                   j                         v rj                   |    1 y wr   )	argumentskeys)r)   r*   bounds     r   r+   zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>z   s7      (,,.. "(s   58rB   r   c              3   6   K   | ]  }t        |        y wr   r(   )r)   r*   result_dtypes     r   r+   zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>   s     V!4QEVr,   zUnhandled result type: r   )bindr1   rD   pytreearg_tree_leavesr/   elementwise_dtypesrB   rK   rL   r    updater-   r   r   AssertionErrorr3   )argskwargsrA   flattened_type_promoting_argscompute_dtyper*   promoted_argsresultmaybe_dtyperM   rO   rG   rE   sigs            @@r   _fnz8elementwise_type_promotion_wrapper.__call__.<locals>._fnw   sW   CHHd-f-E"' (66( # -3,B,BDW,X)*/*B*B.+$($<$<+'M< 66,,.. *5??1+=}MMM 
 OO""=1*%//*F %//)#oog6#.L&*-.v|DD&(+VvVVV #:4<.!IJJ's   6=Einspect	signaturer   __signature__)rE   rG   r^   r]   s   `` @r   __call__z+elementwise_type_promotion_wrapper.__call__t   s;    #	r!	K 
!	KF  
r"   )__name__
__module____qualname____doc__r   r   r   strrF   r   rc   r   r"   r   r@   r@   V   sA    0 8<	7 =7 &hsm4	7(8 ( (r"   r@   outshapec                     t        j                  | j                  |      ry| j                         dk7  r.dt	        | j                         d}t        j                  |       y)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).T)r/   
same_shaperj   numelrh   warningswarn)ri   rj   r8   s      r   _resize_output_checkrp      s_    		5)
yy{aQRUVYV_V_R`Qa bt t 	 	cr"   memory_formatc                 D    t        | |      r| j                  ||      S | S )N)rq   )rp   resize_)ri   rj   rq   s      r   _maybe_resize_outrt      s&    
 C'{{5{>>
r"   r*   c                 ^    | j                         dk(  xr | j                  j                  dk(  S )Nr   cpu)dimdevicer3   )r*   s    r   is_cpu_scalarry      s$    557a<2AHHMMU22r"   F)exact_dtype	copy_fromcopy_torz   c                     t               sA j                  j                  k7  r(d j                   dj                   d}t        |      |r2t        j                   j
                  j
                  k(   fd       nCt        j                  t        j                   j
                  j
                         fd       j                         S )NzAttempting to copy from device z to device z*, but cross-device copies are not allowed!c                  >    d j                    dj                    dS )Nz"Expected out tensor to have dtype z	 but got z insteadr   r{   r|   s   r   <lambda>z _safe_copy_out.<locals>.<lambda>   s'    88I J}}oX/ 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.??YZaZgZgYh i= = r"   )	ry   rx   RuntimeErrortorch_checkr   r/   can_safe_cast_tocopy_)r{   r|   rz   r8   s   ``  r   _safe_copy_outr      s     #	(8(8GNN(J-i.>.>-? @ ((RT 	 3 OOw}},/	
 	""Y__gmmT=	
 ==##r"   )rz   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                 6    rt        j                  |       S d S r   )r/   suggest_memory_format)tr   s    r   maybe_compute_memory_formatz0out_wrapper.<locals>.maybe_compute_memory_format   s    1Gu**1-QTQr"   rG   r   c                    	 rt         n*t        t        d t        t	                    D                 }rt         n,t        d j                   D cg c]
  }|t         f c}      t        j                         	dt        	fdD              t               dddt        j                  dt        j                  f f	d	       }t        j                  d
t        j                  j                  d|      }t!        	j"                  t$              s	j"                  	j&                  |fv sJ g 	j(                  j+                         |}t-        |d       }t        j.                  |      |_        t3        t5         di             |_        ||j6                  d
<   |j6                  d<   r
k7  rd   |j6                  t8        <   dt:        j<                   d|_        |S c c}w )z?
        Adds the out parameter to a Python reference.
        c              3   (   K   | ]
  }t           y wr   )r   )r)   _s     r   r+   z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>   s     K^^Ks   return_types_)rx   r   c              3   :   K   | ]  }|j                   v   y wr   )
parameters)r)   pr]   s     r   r+   z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>  s     HAA/Hs   Nr   rV   rW   c                   	  r 	D ]  }t         |      }||vs|||<    r 
|d d ui|n 
|i |t        t              rs)t        t              rt	              t	              k(  sJ  r>t         t              sJ t         j                                t                nt         t              sJ t        j                  t	               t	              k(   fd       t               D ]0  \  }}t        ||j                   |             t        ||       2 n r S    S )Nis_out)r{   r|   rz   c                  8    dt               dt                S )Nzexpected tuple of z elements but got )len)ri   r[   s   r   r   z@out_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.<lambda>4  s"    "4S[MASTWX[T\S] ^ r"   )getattrr-   r   r	   r   rt   rj   r   r   _check_typezip)ri   rV   rW   kout_attrror[   rz   factory_kwargsrG   is_factory_fn	is_tensorr   r   r   return_types   `      @r   r^   z.out_wrapper.<locals>._out_wrapper.<locals>._fn  s_   ' -A&sAH$,q	- TF3d?FvFT,V,6:.fe,K3y>1	2
 " %c:666%V\\+Fv+N #VSkZ%c5111%%CCK/^ !$FC 0 X1)!QWW6QRS6TU&A;WX
 #3:c)::r"   ri   )kinddefaultr<   c                     | j                   S r   )r   )r   s    r   r   z3out_wrapper.<locals>._out_wrapper.<locals>.<lambda>O  s
    aff r"   )key)r   return_annotation__annotations__r   r   zThis function is wrapped by z.out_wrapper) r   r	   r1   ranger   r   rd   r`   ra   allr   r   rV   rW   	ParameterKEYWORD_ONLYr-   r   rh   emptyr   valuessorted	Signaturerb   dictr   r   r   out_wrapperre   !_torch_decompositions_out_wrapper)rG   out_typer   r^   	out_paramparamsr   r   r   r]   default_out_namesrz   r   r   r   r   s   `     @@@@r   _out_wrapperz!out_wrapper.<locals>._out_wrapper   s     uKU3y>5JKKL 	  }-Y/WN0C/W 	 #,HHH	r$( 5	;rww 5	;BII 5	; 5	; 
5	;n %%""//	
	 #//59N9NIIS
 :
 	
 
 5#..'')494
 $45#--
 #72/@"#EF%-E"(3H% &77<EaLC 89
 3O{OeOeNffr0s-
I 0Xs   G:
)r   r   r   r   )rz   r   r   r   r   r   r   r   s   ```` @@@r   r   r      sg     !
9~%	I!#IRq"b&) qhr2v.> q qf r"   rG   c                 2    t        j                  | d       S )Nc                     t        | d       S )Nr   )r=   )fs    r   r   z+_maybe_remove_out_wrapper.<locals>.<lambda>k  s    71&IJJ r"   )stop)r`   unwrap)rG   s    r   _maybe_remove_out_wrapperr   h  s    >>
J r"   c                       fd G fddt         j                  j                        t               fd       }|S )Nc                     t         j                  j                         5  t         j                  j                  t         j                  j                  j
                        } | i |cd d d        S # 1 sw Y   y xY wr   )r   _C_AutoDispatchBelowAutograd&_dispatch_tls_is_dispatch_key_excludedDispatchKeyADInplaceOrView)rV   rW   oldprims      r   redispatch_primz0backwards_not_supported.<locals>.redispatch_primp  s^    XX002 	)((AA$$44C ((		) 	) 	)s   A	A33A<c                   2    e Zd Ze fd       Zed        Zy)6backwards_not_supported.<locals>.BackwardsNotSupportedc                 4    t        ||      \  }} ||      S r   )r   )ctx	args_spec	flat_argsrV   rW   r   s        r   forwardz>backwards_not_supported.<locals>.BackwardsNotSupported.forwardx  s     ))Y?LD&"4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Ar"   N)rd   re   rf   staticmethodr   r   )r   s   r   BackwardsNotSupportedr   w  s+    		1 
	1 
	B 
	Br"   r   c                      t        | |f      \  }}t        j                         r%t        d |D              r j                  |g| S  | |      S )Nc              3   j   K   | ]+  }t        |t        j                        s|j                   - y wr   )r-   r   Tensorrequires_grad)r)   r   s     r   r+   zBbackwards_not_supported.<locals>._autograd_impl.<locals>.<genexpr>  s&      +
 !*Q2MAOO+
s   33)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  "s +
%.+
 (
 /(..yE9EE"400r"   )r   autogradFunctionr   )r   r   r   r   s   ` @@r   backwards_not_supportedr   o  s@    )B 7 7 B 4[1 1& r"   c                 f     t        j                         }t                fd       }||_        |S )zQ
    Allows unary operators that accept tensors to work with Python numbers.
    c                  P   t        |       dkD  rt        | d   t              r}t        j                  t        | d               }t        |       }t        j                  | d   |      |d<    |i |}t        |t        j                        sJ |j                         S  | i |S )Nr   r   )r   r-   r   r/   type_to_dtyper3   listr   tensorr   item)rV   rW   r   args_r[   rG   s        r   r^   z-elementwise_unary_scalar_wrapper.<locals>._fn  s    t9q=ZQ8''T!W6EJE||DG59E!H)&)Ffell333;;= 4"6""r"   r_   )rG   r]   r^   s   `  r    elementwise_unary_scalar_wrapperr     s:     

B
C
2Y	# 	# CJr"   r   )2r`   rn   	functoolsr   typingr   r   r   r   r   r	   r
   typing_extensionsr   r   torch._prims_common_prims_commonr/   r   r   r   r   r   r   r   torch.utilsr   rQ   torch.utils._pytreer   r   r   r   r   r    r3   r9   r>   r@   rp   rq   rt   boolry   r   rh   r   r   r   r   r   r"   r   <module>r      s3      U U U '  #   * < T]t_ 
	~ 	ekk 	n 	 
	 
	z 	%++ 	* 	 
	 
	x 	 	 	 
	 
	t 	EKK 	D 	 
	
W"j t 
 F FTn Y ( 48	 E//03^ 3 3
 PU$ $+9$HL$< #(	III I !	I
 xB (2r6"223IX( &X h r"   