
    x-j                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ g Zdd	Zd
 Zd Zd Z G d d          Zd Zd Zd ZddZdS )    N)_C_ops)core)check_variable_and_dtype)EagerParamBase)LayerHelper)in_dynamic_or_pir_modec                 (   t                      rt          j        | |          S t          | dg dd           t	          d	i t                      }|                    | j        | j                  }|	                    dd| gid|gid|i           |S )
aV  

    This OP takes in the Tensor :attr:`x` and change it to output with
    aclFormat with int value. This API is only used for Ascend NPU.

    Args:
        x(Tensor): An input N-D Tensor with data type bool, float16,
                   float32, float64, int32, int64, int16, int8, uint8.
        format(int): Storage data format of the output in aclFormat,
                     default value is -1.

    Returns:
        Tensor: A Tensor with acl storage format on Ascend NPU.

    Examples:
        .. code-block:: pycon

            >>> # doctest: +REQUIRES(env:NPU)
            >>> import paddle
            >>> paddle.device.set_device('npu')

            >>> x = paddle.ones(shape=[6])
            >>> y = paddle.incubate._npu_identity(x, 3)  # ACL_FORMAT_NC1HWC0 = 3
            >>> print(y.shape)
            paddle.Size([1, 1, 1, 1, 16])
    x)	boolint8uint8int16int32int64float16float32float64npu_identity)dtypestop_gradientoutformat)typeinputsoutputsattrsN)r   )
r   r   r   r   r   locals"create_variable_for_type_inferencer   r   	append_op)r   r   helperr   s       c/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/incubate/tensor/manipulation.py_npu_identityr#      s    6  "1f--- 
 
 
 	
 	
 	
" 88vxx8877' 8 
 
 	!:SENV$	 	 	
 	
 	
 
    c                 B   t          | t                    rAt          j        | j                  }t          | j        | j        fi |} |||           }||fS t          | t          j                  r.t          j
                                        } |||           }||fS dS )zt
    Helper to create a new destination tensor and call 'func(dst, src)'
    which is either offload or reload.
    N)
isinstancer   copydeepcopy__dict__shaper   paddleTensorr   eager)
src_tensorfuncstate	new_paramtasknew_varbases         r"   _load_reload_implr4   Z   s    
 *n-- !j122":#3Z5EOOOO	tIz**$	J	.	. !j''))tK,,D  ! !r$   c                      t           j                                        } t          j                    rt	          j                    S t          d | D                       rdS t	          j                    S )zf
    Constructs a new AsyncLoad object.
    It is used to load/reload data asynchronously on GPU.
    c              3   >   K   | ]}t          j        |          V  d S Nr+   is_compiled_with_custom_device.0devs     r"   	<genexpr>z$create_async_load.<locals>.<genexpr>r   s>        7:-c22     r$   N)r+   deviceget_all_custom_device_typeis_compiled_with_xpur   XpuAsyncLoadany	AsyncLoad)custom_devicess    r"   create_async_loadrE   j   sz    
 ]==??N"$$   """	  >L   
 
   t~r$   c                  (    t          j                    S )zf
    Constructs a new AsyncLoad object.
    It is used to load/reload data asynchronously on XPU.
    )r   rA    r$   r"   create_xpu_async_loadrH   z   s    
 r$   c                   $    e Zd ZdZd Zd Zd ZdS )_NoopAsyncTasku(   A dummy Task for sync‐fallback on XPU.c                     dS )NTrG   selfs    r"   is_completedz_NoopAsyncTask.is_completed   s    tr$   c                     d S r7   rG   rL   s    r"   cpu_waitz_NoopAsyncTask.cpu_wait       r$   c                     d S r7   rG   rL   s    r"   xpu_waitz_NoopAsyncTask.xpu_wait   rQ   r$   N)__name__
__module____qualname____doc__rN   rP   rS   rG   r$   r"   rJ   rJ      sG        22        r$   rJ   c                 *   t          j                    o(t          | d          o| j                                        }t           j                                        }t          d |D                       o*t          | d          o| j                                        |v }|s|r]| 	                                }t          j
        |                                t          j                              }|t                      fS t          | |j                  S )a  
    Loads the source tensor into the destination tensor asynchronously.

    Args:
        src_tensor (EagerParamBase|paddle.Tensor): The source tensor.
        async_load (core.AsyncLoad): The AsyncLoad object.

    Returns:
        tuple: A tuple containing two elements:
         - dest_tensor (EagerParamBase|paddle.Tensor): The destination tensor.
         - task (Task): The task that loads the source tensor into the destination tensor.
    placec              3   >   K   | ]}t          j        |          V  d S r7   r8   r:   s     r"   r=   z async_offload.<locals>.<genexpr>   s>       
 
;>F1#66
 
 
 
 
 
r$   rY   )r+   r@   hasattrrY   is_xpu_placer>   r?   rB   custom_device_typecpu	to_tensornumpyCPUPlacerJ   r4   offload)r.   
async_loadis_xpu_tensorrD   is_custom_tensorhost_tensorr   s          r"   async_offloadrh      s#    	#%% 	,J((	,))++  ]==??N 
 
BP
 
 
 	
 	
 	D J((	D //11^C   %( % nn&&{0022&/:K:KLLLN$$$$Z);<<<r$   c                 <   t          j                    rut          | d          re| j                                        rL|                                 }t          j        |t          j        d                    }|t                      fS t          | |j
                  S )a  
    Reloads the source tensor into the destination tensor asynchronously.

    Args:
        src_tensor (EagerParamBase|paddle.Tensor): The source tensor.
        async_load (core.AsyncLoad): The AsyncLoad object.

    Returns:
        tuple: A tuple containing two elements:
         - dest_tensor (EagerParamBase|paddle.Tensor): The destination tensor.
         - task (Task): The task that reloads the source tensor into the destination tensor.
    rY   r   r[   )r+   r@   r\   rY   is_cpu_placera   r`   XPUPlacerJ   r4   reload)r.   rd   arrxpus       r"   async_reloadro      s     	#%%%J((% ))++%
   s&/!*<*<===N$$$$Z):;;;r$   c                     t          | j                  dk    s
J d            t          |j                  dk    s
J d            | j        |j        k    s
J d            |                    || |||          S )ac  
    Offloading the source tensor into the destination tensor asynchronously with offset and size customized.

    Args:
        src_tensor (EagerParamBase|paddle.Tensor): The source tensor.
        dst_tensor (EagerParamBase|paddle.Tensor): The destination tensor.
        src_offset (int): The element offset of the source tensor.
        dst_offset (int): The element offset of the destination tensor.
        offload_size (int): The size of the data to be loaded.
        async_loader (core.AsyncLoad): The AsyncLoad object.

    Returns:
        task (Task): The task that operates partial offloading.
       zOnly support 1-D tensorzOnly support same dtype)lenr*   r   offload_with_offset)r.   
dst_tensor
src_offset
dst_offsetoffload_sizeasync_loaders         r"   async_offload_with_offsetry      s    " z  A%%%'@%%%z  A%%%'@%%%z////1J///++J
J  r$   Trq   c                 ~   |rgt          j        d|i           t           j                                         t           j                            |                                            dS t          j        ddi           t           j                                         t           j                            g            dS )z#
    Enable activation offload
    FLAGS_offload_retry_timesr	   N)r+   	set_flagsr   register_offload_callback!set_skip_offload_callback_tensors
parametersclear_offload_callback)modelenableretry_timess      r"   enable_activation_offloadr      s      :5{CDDD--///55e6F6F6H6HIIIII5r:;;;**,,,55b99999r$   )r	   )Trq   )r'   r+   r   paddle.baser   paddle.base.data_feederr   paddle.base.frameworkr   paddle.base.layer_helperr   paddle.frameworkr   __all__r#   r4   rE   rH   rJ   rh   ro   ry   r   rG   r$   r"   <module>r      s@                 < < < < < < 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3

9 9 9 9x! ! !         
 
 
 
 
 
 
 
#= #= #=L< < <4  4: : : : : :r$   