
    ϑi                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  / rSS jrS	 rS
 rS r " S S5      rS rS rS rSS jrg)    N)_C_ops)core)check_variable_and_dtype)EagerParamBase)LayerHelper)in_dynamic_or_pir_modec                    [        5       (       a  [        R                  " X5      $ [        U S/ SQS5        [	        S0 [        5       D6nUR                  U R                  U R                  S9nUR                  SSU /0SU/0SU0S9  U$ )	a
  

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attrs)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.13/site-packages/paddle/incubate/tensor/manipulation.py_npu_identityr"      s    6 ""1-- 
 	
" 8vx877'' 8 
 	!:SENV$	 	 	
 
    c                 V   [        U [        5      (       aL  [        R                  " U R                  5      n[        U R
                  U R                  40 UD6nU" X05      nX44$ [        U [        R                  5      (       a)  [        R                  R                  5       nU" XP5      nXT4$ g)zh
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_implr3   Z   s    
 *n--j112":#3#3Z5E5EOO	I*	J	.	.jj'')K,   
/r#   c                      [         R                  R                  5       n [         R                  " 5       (       a  [        R
                  " 5       $ [        S U  5       5      (       a  g[        R                  " 5       $ )zZ
Constructs a new AsyncLoad object.
It is used to load/reload data asynchronously on GPU.
c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fNr*   is_compiled_with_custom_device.0devs     r!   	<genexpr>$create_async_load.<locals>.<genexpr>r   s!      >Ls--c22n   #%N)r*   deviceget_all_custom_device_typeis_compiled_with_xpur   XpuAsyncLoadany	AsyncLoad)custom_devicess    r!   create_async_loadrF   j   s`    
 ]]==?N""$$  ""	 >L 
 
 ~~r#   c                  ,    [         R                  " 5       $ )zZ
Constructs a new AsyncLoad object.
It is used to load/reload data asynchronously on XPU.
)r   rB    r#   r!   create_xpu_async_loadrI   z   s    
 r#   c                   *    \ rS rSrSrS rS rS rSrg)_NoopAsyncTask   u(   A dummy Task for sync‐fallback on XPU.c                     g)NTrH   selfs    r!   is_completed_NoopAsyncTask.is_completed   s    r#   c                     g r6   rH   rN   s    r!   cpu_wait_NoopAsyncTask.cpu_wait       r#   c                     g r6   rH   rN   s    r!   xpu_wait_NoopAsyncTask.xpu_wait   rU   r#   rH   N)	__name__
__module____qualname____firstlineno____doc__rP   rS   rW   __static_attributes__rH   r#   r!   rK   rK      s    2r#   rK   c                 L   [         R                  " 5       =(       a-    [        U S5      =(       a    U R                  R	                  5       n[         R
                  R                  5       n[        S U 5       5      =(       a0    [        U S5      =(       a    U R                  R                  5       U;   nU(       d  U(       aR  U R                  5       n[         R                  " UR                  5       [         R                  " 5       S9nU[        5       4$ [        XR                  5      $ )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   N   #    U  H  n[         R                  " U5      v   M     g 7fr6   r7   r9   s     r!   r<    async_offload.<locals>.<genexpr>   s!      
BP3F11#66.r>   r`   )r*   rA   hasattrr`   is_xpu_placer?   r@   rC   custom_device_typecpu	to_tensornumpyCPUPlacerK   r3   offload)r-   
async_loadis_xpu_tensorrE   is_custom_tensorhost_tensorr   s          r!   async_offloadrp      s     	##% 	,J(	,))+  ]]==?N 
BP
 	
 	D J(	D //1^C  ( nn&{002&//:KLN$$$Z););<<r#   c                 J   [         R                  " 5       (       au  [        U S5      (       ad  U R                  R	                  5       (       aE  U R                  5       n[         R                  " U[         R                  " S5      S9nU[        5       4$ [        XR                  5      $ )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.
r`   r   rc   )r*   rA   rd   r`   is_cpu_placeri   rh   XPUPlacerK   r3   reload)r-   rl   arrxpus       r!   async_reloadrw      s}     	##%%J(())++ s&//!*<=N$$$Z):):;;r#   c                     [        U R                  5      S::  d   S5       e[        UR                  5      S::  d   S5       eU R                  UR                  :X  d   S5       eUR                  XX2U5      $ )a7  
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_offsetr      s|    " z A%@'@@%z A%@'@@%z///J1JJ/++
 r#   c                    U(       ad  [         R                  " SU05        [         R                  R                  5         [         R                  R	                  U R                  5       5        g[         R                  " SS05        [         R                  R                  5         [         R                  R	                  / 5        g)z
Enable activation offload
FLAGS_offload_retry_timesN)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--/55e6F6F6HI5r:;**,55b9r#   )r   )Try   )r&   r*   r   paddle.baser   paddle.base.data_feederr   paddle.base.frameworkr   paddle.base.layer_helperr   paddle.frameworkr   __all__r"   r3   rF   rI   rK   rp   rw   r   r   rH   r#   r!   <module>r      sX        < 0 0 3

9x!   
 
#=L<44:r#   