o
    "j.                     @   s   d dl Zd dlmZmZ ddlmZmZ ddlmZ ej	j
jej	j
jej	j
jej	j
jej	j
jej	j
jgZg dZej	j
jej	j
jgZdadd	 ZdS )
    N)_C_ops_legacy_C_ops   )core	framework)convert_np_dtype_to_dtype_)__add____radd____sub____rsub____mul____rmul____div____truediv____rdiv____rtruediv__
__matmul__Fc                     s  dd } dd   fdd}dd }d	d
 }dd }dd }dd }t dd }dd }dd }	t dd }
t dd }d|fd|fd|fd|fd|fd |fd!| fd"|	fd#|fd$|fd%|
fd&|fd'g}g d(}t}d)atjj}|s|D ]}t||d*}|rt||| qs|D ]}|d+ }|d, }t||| qd*S d+d*l}|jj	D ]}t
||rqt|j|d*}|rt||| q|jjD ]\}}t|j|d*}|rt||| qd*S )-z
    Similar to monkey_patch_variable.
    The difference is, in dygraph mode, use auto-generated op functions for better performance.
    c                 S   s"   t |tjjst|}t| |S )a  

        Cast a Tensor to a specified data type.

        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VarTyper   r   cast)selfdtype r   b/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/base/dygraph/math_op_patch.pyastypeB   s   z(monkey_patch_math_tensor.<locals>.astypec                 S   s.   t  rt| t||dS t| d|d|S )NTscalebias)r   Zin_dygraph_moder   r   floatr   )varr   r   r   r   r   _scalar_elementwise_op_^   s   z9monkey_patch_math_tensor.<locals>._scalar_elementwise_op_c                    s    | ddS )Ng      g        r   r   r    r   r   _neg_d   s   z'monkey_patch_math_tensor.<locals>._neg_c                 S   `   t | j}|dksJ d|   }| sJ d| jtjj	j
kr)| d} tt | S )N   z4only one element variable can be converted to float.$variable's tensor is not initializedfloat32)npprodshapevalue
get_tensor_is_initializedr   r   r   r   BF16r   r   arrayr   Znumeltensorr   r   r   _float_g      

z)monkey_patch_math_tensor.<locals>._float_c                 S   r$   )Nr%   z3only one element variable can be converted to long.r&   r'   r(   r)   r*   r+   r,   r-   r   r   r   r   r.   r   intr/   r0   r   r   r   _long_r      
z(monkey_patch_math_tensor.<locals>._long_c                 S   r$   )Nr%   z2only one element variable can be converted to int.r&   r'   r4   r0   r   r   r   _int_{   r7   z'monkey_patch_math_tensor.<locals>._int_c                 S   s\   | j dks	J d| jtjjjkrt|   S | jtjjj	kr)t|  
 S | jd S )Nr   zlen() of a 0-D tensor is wrong)ndimtyper   r   r   ZVOCABlenr+   Zget_map_tensorZSTRINGSZget_string_tensorr*   r!   r   r   r   _len_   s   
z'monkey_patch_math_tensor.<locals>._len_c                 S   r$   )Nr%   z;only one element variable can be converted to python index.r&   r'   r4   r0   r   r   r   _index_   r3   z)monkey_patch_math_tensor.<locals>._index_c                 S   
   t | jS Nr;   r*   r!   r   r   r   _ndim   s   
z'monkey_patch_math_tensor.<locals>._ndimc                 S   r>   r?   r@   r!   r   r   r   
ndimension      
z,monkey_patch_math_tensor.<locals>.ndimensionc                 S   r>   r?   r@   r!   r   r   r   dim   rC   z%monkey_patch_math_tensor.<locals>.dimc                 S   s   t t| jS r?   )r5   r(   r)   r*   r!   r   r   r   _size_   s   z(monkey_patch_math_tensor.<locals>._size_c                 S   sF   t | jdkr	| S g }tt | jD ]}|d| qt| |}|S )Nr%   r   )r;   r*   rangeinsertr   Z	transpose)r   permioutr   r   r   _T_   s   z%monkey_patch_math_tensor.<locals>._T___neg__	__float__Z__long____int____len__	__index__r   rD   rB   r9   sizeT)Z__array_ufunc__N)r   r	   r
   r   r   r   r   r   r   r   __mod__r   __gt____ge____lt____le____floordiv____pow____rpow____eq____ne__TNr   r%   )property_already_patch_eager_tensorr   eagerZTensorgetattrsetattrZpaddle.tensorr1   Ztensor_method_funchasattrZmagic_method_func)r   r#   r2   r6   r8   r<   r=   rA   rB   rD   rE   rK   Zeager_methodsZeager_cpp_level_patchZlocal_already_patchZlocal_tensormethod_nameZmethod_implmethodpaddleZmagic_methodZorigin_methodimplr   r"   r   monkey_patch_math_tensor<   sv   			




rg   )numpyr(   re   r   r    r   r   r   r   r   ZUINT8ZINT8ZINT16ZINT32ZINT64ZBOOLZ_supported_int_dtype_Z!_supported_promote_complex_types_Z	COMPLEX64Z
COMPLEX128Z_complex_dtypesr^   rg   r   r   r   r   <module>   s"   