
    Αi9V                    Z   S SK Jr  S SKrS SKJr  S SKrS SKqS SKJr  S SK	J
r
  SSKJr  SSKJr  \(       a  S S	KJr  S S
KJr  S SKJr  S SKJrJrJr  \R.                  R0                  R2                  \R.                  R0                  R4                  \R.                  R0                  R6                  \R.                  R0                  R8                  \R.                  R0                  R:                  \R.                  R0                  R<                  /r/ SQr \R.                  R0                  RB                  \R.                  R0                  RD                  /r#Sq$0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S SS S!.Er%        S&S" jr& " S# S$\'5      r(S% r)g)'    )annotationsN)TYPE_CHECKING)_C_ops)size_args_decorator_patch   )core)convert_np_dtype_to_dtype_)Any)NDArray)Tensor)	DTypeLike	PlaceLike	ShapeLike)__add____radd____sub____rsub____mul____rmul____div____truediv____rdiv____rtruediv__
__matmul__Ffloat16halfbfloat16float32floatfloat64doubleint8charint16shortint32intint64longbool	complex64
complex128)r,   cfloatcdoublec                4    [         R                  " U UUU5      $ N)paddletensor)datadtypedevicerequires_grads       a/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/base/dygraph/math_op_patch.py_rebuild_tensorr8   k   s!     ==	     c                  >   ^  \ rS rSr% S\S'   U 4S jrSS jrSrU =r$ )
TensorSizey   z	list[int]as_shapec                n   > [         TU ]  U [        [        R                  " U5      5      5      nXl        U$ r0   )super__new__r'   npprodr=   )clsshapeinstance	__class__s      r7   r@   TensorSize.__new__|   s+    7?3BGGEN(;<!r9   c                T    [         R                  " U R                  5      nUc  U$ X!   $ r0   )r1   Sizer=   )selfdimrD   s      r7   __call__TensorSize.__call__   s&    DMM*;Lzr9    r0   )	__name__
__module____qualname____firstlineno____annotations__r@   rL   __static_attributes____classcell__)rF   s   @r7   r;   r;   y   s    
 r9   r;   c                   ^$^% S?S jm%S@U%4S jjn U%4S jnSAS jn        SBS jm$SCU$4S jjnSCS jnSDS jnSES	 jnSFS
 jnSFS jnSFS jn	SFS jn
[         SFS j5       nSFS jnSFS jn[         SFS j5       n[         SCS j5       n[         SCS j5       nSSSSS.               SGS jjn[        SSSSS.             SHS jj5       n[        SSSSS.             SHS jj5       n[        SSSSS.             SHS jj5       n[         SIS j5       nUR                  SJS j5       nSKSLS  jjn[         SMS! j5       nSNS" jnS#U4S$U4S%U4S&U4S'U4S(U4S)U	4S*U
4S+T%4S,U 4S-U 4S.U4S/U4S0U4S1U4SU4S2U4S3U4S4U4S5U4S6U4S7U4SU4S8U4S9S:U4S;U4/nU" 5       nUR                  U5        / S<Qn[        nSq[
        R                  R                  nU(       dM  U H&  n[        UUS5      nU(       d  M  [        UUU5        M(     U H  n U S=   nU S>   n[        UUU5        M     gS=SK
q[        R                  R                   HG  n[        UU5      (       a  M  [        [        R                  US5      nU(       d  M:  [        UUU5        MI     [        R                  R                   H7  u  n!n"[        [        R                  U"S5      n#U#(       d  M*  [        UU!U#5        M9     g)Oz
Similar to monkey_patch_variable.
The difference is, in dygraph mode, use auto-generated op functions for better performance.
c                    [        U[        R                  R                  [        R                  45      (       d  [        U5      nU R                  U:X  a  U $ [        R                  " X5      $ )a  

Cast a Tensor to a specified data type if it differs from the current dtype;
otherwise, return the original Tensor.

Args:
    dtype: The target data type.

Returns:
    Tensor: a new Tensor with target dtype

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import numpy as np

        >>> original_tensor = paddle.ones([2, 2])
        >>> print("original tensor's dtype is: {}".format(original_tensor.dtype))
        original tensor's dtype is: paddle.float32
        >>> new_tensor = original_tensor.astype('float32')
        >>> print("new tensor's dtype is: {}".format(new_tensor.dtype))
        new tensor's dtype is: paddle.float32
)	
isinstancer   VarDescVarTypeDataTyper	   r4   r   cast)rJ   r4   s     r7   astype(monkey_patch_math_tensor.<locals>.astype   sN    2 %$,,"6"6!FGG.u5E::K{{4''r9   c                   > U R                  5       (       a  T" U S5      nT" US5      $ U R                  5       (       a6  T" U R                  5       S5      n[        R                  " S5        T" US5      $ T" U S5      $ )Nr"   uint8z:Casting complex values to real discards the imaginary part)is_floating_point
is_complexrealloggingwarning)rJ   r2   rc   r]   s      r7   byte&monkey_patch_math_tensor.<locals>.byte   su    !!##D&)F&'**__$))+v.DOOL $(($((r9   c                    > / n [         R                  5        H9  u  pU4S jnU" U5      nXl        SU SU S3Ul        U R	                  X45        M;     U $ )z/
Batch create all data type conversion methods
c                   >^  SUU 4S jjnU$ )Nc                   > T" U T5      $ r0   rN   )rJ   r]   r4   s    r7   conversion_method}monkey_patch_math_tensor.<locals>._create_dtype_conversion_methods.<locals>.make_conversion_method.<locals>.conversion_method   s    !$..r9   rJ   r   returnr   rN   )r4   rk   r]   s   ` r7   make_conversion_methodbmonkey_patch_math_tensor.<locals>._create_dtype_conversion_methods.<locals>.make_conversion_method   s    / / )(r9   z
            Cast a Tensor to z data type if it differs from the current dtype;
            otherwise, return the original Tensor.
            Returns:
                Tensor: a new Tensor with z dtype
            )_supported_dtype_conversionsitemsrO   __doc__append)methodsmethod_nametarget_dtypero   method_implr]   s        r7    _create_dtype_conversion_methodsBmonkey_patch_math_tensor.<locals>._create_dtype_conversion_methods   su     )E)K)K)M%K) 1>K#. '*^ ,+ ,8. 9	#K NNK56# *N& r9   c                8    U R                  UR                  5      $ r0   )r]   r4   )rJ   others     r7   type_as)monkey_patch_math_tensor.<locals>.type_as   s    {{5;;''r9   c                F    [         R                  " U [        U5      US5      $ )NT)r   scaler   )varr   biass      r7   _scalar_elementwise_op_9monkey_patch_math_tensor.<locals>._scalar_elementwise_op_   s     ||CutT::r9   c                   > T" U SS5      $ )Ng      g        rN   )r   r   s    r7   _neg_'monkey_patch_math_tensor.<locals>._neg_   s    &sD#66r9   c                "    U R                  5       $ r0   )absr   s    r7   _abs_'monkey_patch_math_tensor.<locals>._abs_   s    wwyr9   c                   [         R                  " U R                  5      nUS:X  d   S5       eU R                  5       (       d   S5       eU R	                  5       (       d  U R                  S5      n [        [         R                  " U 5      5      $ )N   z6only one element variable can be converted to complex.$variable's tensor is not initializedr+   )rA   rB   rD   _is_initializedrb   r]   complexarrayr   numels     r7   	_complex_+monkey_patch_math_tensor.<locals>._complex_   su    		"z 	
D	
z ""$$L&LL$~~**[)Crxx}%%r9   c                   [         R                  " U R                  5      nUS:X  d   S5       eU R                  5       (       d   S5       eU R                  [
        R                  R                  R                  :X  d(  U R                  [
        R                  R                  :X  a  U R                  S5      n [        [         R                  " U 5      5      $ )Nr   z4only one element variable can be converted to float.r   r   )rA   rB   rD   r   r4   r   rY   rZ   BF16r[   BFLOAT16r]   r   r   r   s     r7   _float_)monkey_patch_math_tensor.<locals>._float_   s    		"z 	
B	
z ""$$L&LL$II--222yyDMM222**Y'CRXXc]##r9   c                   [         R                  " U R                  5      nUS:X  d   S5       eU R                  5       (       d   S5       eU R                  [
        R                  R                  R                  :X  d(  U R                  [
        R                  R                  :X  a  U R                  S5      n [        [         R                  " U 5      5      $ )Nr   z3only one element variable can be converted to long.r   r   rA   rB   rD   r   r4   r   rY   rZ   r   r[   r   r]   r'   r   r   s     r7   _long_(monkey_patch_math_tensor.<locals>._long_   s    		"zPPPz""$$L&LL$II--222yyDMM222**Y'C288C=!!r9   c                   [         R                  " U R                  5      nUS:X  d   S5       eU R                  5       (       d   S5       eU R                  [
        R                  R                  R                  :X  d(  U R                  [
        R                  R                  :X  a  U R                  S5      n [        [         R                  " U 5      5      $ )Nr   z2only one element variable can be converted to int.r   r   r   r   s     r7   _int_'monkey_patch_math_tensor.<locals>._int_	  s    		"zOOOz""$$L&LL$II--222yyDMM222**Y'C288C=!!r9   c                   U R                   S:  d   S5       eU R                  [        R                  R                  R
                  :X  a'  [        U R                  5       R                  5       5      $ U R                  [        R                  R                  R                  :X  a'  [        U R                  5       R                  5       5      $ U R                  S   $ )Nr   zlen() of a 0-D tensor is wrong)ndimtyper   rY   rZ   VOCABlenvalueget_map_tensorSTRINGSget_string_tensorrD   r   s    r7   _len_'monkey_patch_math_tensor.<locals>._len_  s    xx!|===|88t||++111syy{11344XX--555syy{4467799Q<r9   c                   [         R                  " U R                  5      nUS:X  d   S5       eU R                  5       (       d   S5       eU R                  [
        R                  R                  R                  :X  d(  U R                  [
        R                  R                  :X  a  U R                  S5      n [        [         R                  " U 5      5      $ )Nr   z;only one element variable can be converted to python index.r   r   r   r   s     r7   _index_)monkey_patch_math_tensor.<locals>._index_  s    		"z 	
I	
z ""$$L&LL$II--222yyDMM222**Y'C288C=!!r9   c                ,    [        U R                  5      $ r0   r   rD   r   s    r7   _ndim'monkey_patch_math_tensor.<locals>._ndim*  s    399~r9   c                ,    [        U R                  5      $ r0   r   r   s    r7   
ndimension,monkey_patch_math_tensor.<locals>.ndimension.      399~r9   c                ,    [        U R                  5      $ r0   r   r   s    r7   rK   %monkey_patch_math_tensor.<locals>.dim1  r   r9   c                ,    [        U R                  5      $ r0   )r;   rD   r   s    r7   _size_(monkey_patch_math_tensor.<locals>._size_4  s    #))$$r9   c           	         [        U R                  5      S:X  a  U $ [        [        [	        [        U R                  5      5      5      5      n[
        R                  " X5      nU$ )Nr   )r   rD   listreversedranger   	transposer   permouts      r7   _T_%monkey_patch_math_tensor.<locals>._T_8  sH    syy>QJHU3syy>234s)
r9   c                   [        U R                  5      S:  a  [        SU R                   S35      e[	        [        [        U R                  5      5      5      nUS   US   sUS'   US'   [        R                  " X5      nU$ )a  
Return the last two dimensions of a Tensor transposed.

Args:
    var (Tensor): The input Tensor, which must have at least 2 dimensions.

Returns:
    Tensor: A new Tensor with its last two dimensions swapped.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> x = paddle.randn([2, 3, 4])
        >>> x_transposed = x.mT
        >>> x_transposed.shape
        paddle.Size([2, 4, 3])
r   zTensor.ndim(z/) is required to be greater than or equal to 2.)r   rD   
ValueErrorr   r   r   r   r   r   s      r7   _mT_&monkey_patch_math_tensor.<locals>._mT_@  s|    ( syy>Asxxj(WX  E#cii.)*!"XtBxR$r(s)
r9   NFr4   r5   r6   
pin_memorysizer6   c          	     p    Uc  U R                   nUc  U R                  n[        R                  " UUUUUUS9$ )a  
Create a new Tensor of specified shape and fill it with a given value.

Args:
    var (Tensor): A reference Tensor for default dtype and device.
    size (ShapeLike): Shape of the new Tensor.
    fill_value (bool | float | Tensor): Value to fill the Tensor with.
    dtype (DTypeLike, optional): Desired data type of the new Tensor. Defaults to `var.dtype`.
    device (PlaceLike, optional): Device on which to place the new Tensor. Defaults to `var.place`.
    requires_grad (bool, optional): Whether to track gradients. Default: False.
    pin_memory (bool, optional): Whether to pin memory. Default: False.

Returns:
    Tensor: A new Tensor filled with `fill_value`.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.ones([2, 2])
        >>> y = x.new_full([3, 3], 5.0)
        >>> y.numpy()
        array([[5., 5., 5.],
               [5., 5., 5.],
               [5., 5., 5.]], dtype=float32)
r   r4   placer1   full)r   r   
fill_valuer4   r5   r6   r   s          r7   
_new_full_,monkey_patch_math_tensor.<locals>._new_full_]  sD    J =IIE>YYF{{'!
 	
r9   c               n    Uc  U R                   nUc  U R                  n[        R                  " UUUUUS9$ )a   
Create a new uninitialized Tensor of the specified shape.

Args:
    var (Tensor): A reference Tensor for default dtype and device.
    size (ShapeLike): Shape of the new Tensor.
    dtype (DTypeLike, optional): Desired data type of the new Tensor. Defaults to `var.dtype`.
    device (PlaceLike, optional): Device on which to place the new Tensor. Defaults to `var.place`.
    requires_grad (bool, optional): Whether to track gradients. Default: False.
    pin_memory (bool, optional): Whether to pin memory. Default: False.

Returns:
    Tensor: A new uninitialized Tensor with the specified shape.

Examples:
    .. code-block:: pycon

        >>> import paddle
        >>> x = paddle.ones([2, 2])
        >>> y = x.new_empty(3, 3)  # type: ignore
        >>> y.shape
        paddle.Size([3, 3])
r5   r6   r   )r4   r   r1   emptyr   r   r4   r5   r6   r   s         r7   _new_empty_-monkey_patch_math_tensor.<locals>._new_empty_  sA    D =IIE>YYF||'!
 	
r9   c          	     p    Uc  U R                   nUc  U R                  n[        R                  " USUUUUS9$ )a[  
Create a new Tensor of the specified shape filled with ones.

Args:
    var (Tensor): A reference Tensor for default dtype and device.
    size (ShapeLike): Shape of the new Tensor.
    dtype (DTypeLike, optional): Desired data type of the new Tensor. Defaults to `var.dtype`.
    device (PlaceLike, optional): Device on which to place the new Tensor. Defaults to `var.place`.
    requires_grad (bool, optional): Whether to track gradients. Default: False.
    pin_memory (bool, optional): Whether to pin memory. Default: False.

Returns:
    Tensor: A new Tensor filled with ones.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.zeros([2, 2])
        >>> y = x.new_ones(3, 3)  # type: ignore
        >>> y.numpy()
        array([[1., 1., 1.],
               [1., 1., 1.],
               [1., 1., 1.]], dtype=float32)
r   r   r   r   s         r7   
_new_ones_,monkey_patch_math_tensor.<locals>._new_ones_  D    H =IIE>YYF{{'!
 	
r9   c          	     p    Uc  U R                   nUc  U R                  n[        R                  " USUUUUS9$ )aA  
Create a new Tensor of the specified shape filled with zeros.

Args:
    var (Tensor): A reference Tensor for default dtype and device.
    size (ShapeLike): Shape of the new Tensor.
    dtype (DTypeLike, optional): Desired data type of the new Tensor. Defaults to `var.dtype`.
    device (PlaceLike, optional): Device on which to place the new Tensor. Defaults to `var.place`.
    requires_grad (bool, optional): Whether to track gradients. Default: False.
    pin_memory (bool, optional): Whether to pin memory. Default: False.

Returns:
    Tensor: A new Tensor filled with zeros.

Examples:
    .. code-block:: python

    >>> import paddle
    >>> x = paddle.ones([2, 2])
    >>> y = x.new_zeros(3, 3)  # type: ignore
    >>> y.numpy()
    array([[0., 0., 0.],
           [0., 0., 0.],
           [0., 0., 0.]], dtype=float32)
r   r   r   r   s         r7   _new_zeros_-monkey_patch_math_tensor.<locals>._new_zeros_  r   r9   c                $    U R                   (       + $ )a  
Whether this Tensor requires gradient computation.

This is a convenience property that returns the opposite of stop_gradient.
Setting requires_grad=True is equivalent to setting stop_gradient=False.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.randn([2, 3])
        >>> print(x.requires_grad)  # False by default
        >>>
        >>> x.requires_grad = False
        >>> print(x.stop_gradient)  # True
)stop_gradientrJ   s    r7   r6   /monkey_patch_math_tensor.<locals>.requires_grad#  s    $ %%%%r9   c                t    [        U[        5      (       d  [        S[        U5       35      eU(       + U l        g)z
Set whether this Tensor requires gradient computation.

Args:
    value (bool): True to enable gradient computation, False to disable.
$requires_grad must be bool, but got NrX   r*   	TypeErrorr   r   )rJ   r   s     r7   r6   r   7  s8     %&&6tE{mD  "'Yr9   Tc                v    [        U[        5      (       d  [        S[        U5       35      eU(       + U l        U $ )z
Set whether this Tensor requires gradient computation.

Args:
    requires_grad (bool): True to enable gradient computation, False to disable.
r   r   )rJ   r6   s     r7   requires_grad_0monkey_patch_math_tensor.<locals>.requires_grad_E  s@     -..6tM7J6KL  "/.r9   c                "    U R                  5       $ )z
Returns the number of bytes allocated on the machine for a single element of the Tensor.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.randn((2,3),dtype=paddle.float64)
        >>> x.itemsize
        8
)element_sizer   s    r7   itemsize*monkey_patch_math_tensor.<locals>.itemsizeS  s       ""r9   c                    U R                  5       n[        U R                  5      SS n[        U R                  5      SS  nU R                  n[
        UUUU44$ )N   r      )numpystrr   r4   r6   r8   )rJ   proto
data_numpyr   r4   r6   s         r7   _reduce_ex_-monkey_patch_math_tensor.<locals>._reduce_ex_b  s^    ZZ\
DJJ"%DJJ#**	!
 
 	
r9   __neg____abs____complex__	__float____long____int____len__	__index__r]   rf   r`   r}   rK   r   r   TmTnew_full	new_emptynew_ones	new_zerosr   )__array_ufunc__Nr   __reduce_ex__)r   r   r   r   r   r   r   r   r   r   __mod____rmod__r   __rmatmul____gt____ge____lt____le____floordiv____rfloordiv____pow____rpow____eq____ne__r   r   )rJ   r   r4   r   rn   r   rm   )rJ   r   r|   r   rn   r   )r   r   r   r   r   r   rn   r   )r   r   rn   r   )r   r   rn   r   )r   r   rn   r   )r   r   rn   r'   )r   r   r   r   r   zbool | float | paddle.Tensorr4   DTypeLike | Noner5   PlaceLike | Noner6   r*   r   r*   rn   r   )r   r   r   r   r4   r  r5   r  r6   r*   r   r*   rn   r   )rJ   r   rn   r*   )rJ   r   r   r*   rn   None)T)r6   r*   rn   r   )rJ   r   rn   r'   )rJ   r   )propertyr   setterextend_already_patch_eager_tensorr   eagerr   getattrsetattrpaddle.tensorr1   r2   tensor_method_funchasattrmagic_method_func)&rf   ry   r}   r   r   r   r   r   r   r   r   r   r   rK   r   r   r   r   r   r   r   r6   r   r   r   eager_methodsdtype_conversion_methodseager_cpp_level_patchlocal_already_patchlocal_tensorrv   rx   methodmagic_methodorigin_methodimplr   r]   s&                                       @@r7   monkey_patch_math_tensorr1     s   (B)6(;;!;).;	;
7&$	"	" "   % %    B #'#'# 1
1
1
 11

  1
 !1
 1
 1
 
1
f 
 #'#'# ,
,
,
  	,

 !,
 ,
 ,
 
,
 ,
\ 
 #'#'# /
/
/
  	/

 !/
 /
 /
 
/
 /
b 
 #'#'# /
/
/
  	/

 !/
 /
 /
 
/
 /
b & && ' ' # #

 
E	E		"	g	V	E	E	g	6		$	G		z"			c
	t	Z 	k"	Z 	k"	-(	>*!	X	+&9M>  @A12: 6"&::$$L0K!,TBK{k;? 1
 $F )K )KL+{; $
 	 "==;;K|[11!&--dCK{k;? < ,2==+J+J'L-6==->DtlD9 ,Kr9   )r3   zNDArray[Any]r4   r   r5   r   rn   r   )*
__future__r   rd   typingr   r   rA   r1   r   paddle.utils.decorator_utilsr    r   	frameworkr	   r
   numpy.typingr   r   paddle._typingr   r   r   rY   rZ   UINT8INT8INT16INT32INT64BOOL_supported_int_dtype_!_supported_promote_complex_types_	COMPLEX64
COMPLEX128_complex_dtypesr   rq   r8   r'   r;   r1  rN   r9   r7   <module>rD     s   #        2$>> 	LLLLLLLLLLLL  % ! 	LL""LL##
 $  y  I  
	 
 y  Y  y  i  F  F  W   W! " W# $ 
7% & W' ( G) , F- . / 0 5  <
 
  A	:r9   