
    zj O                       U d dl mZ d dl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 erd dlmZ eeeef         Zd	ed
<    e	j                    Zered          ndZer e	j        e          s ed          eredv rddlmZ nddlmZ g dZd'd(dZd)dZd'd*dZd'd*dZ d'd+dZ!d'd+dZ"d'd*dZ#d'd*dZ$d'd,dZ%d'd+d Z&	 d'd-d"Z'	 d'd.d$Z(d/d&Z)dS )0    )annotations)TYPE_CHECKINGUnion)	TypeAliasN)core   )EventStreamcreate_eventcreate_stream)CustomPlacer   _CustomPlaceLikezLNo custom device available, please install paddle with custom device support)	metax_gpuiluvatar_gpu)get_device_properties)r
   r	   device_countr   empty_cachemax_memory_allocatedmax_memory_reservedreset_max_memory_allocatedreset_max_memory_reservedmemory_allocatedmemory_reservedcurrent_streamsynchronizedevice_type
str | Nonereturnintc                f    | rt          j        |           }nt          j        t                    }|S )a  
    Return the number of custom devices available.

    Args:
        device_type (str, optional): The type of custom device (e.g., 'npu', 'mlu', etc.).
            If None, returns the count of the first available custom device type.

    Returns:
        int: the number of custom devices available.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.device_count()
            >>> paddle.device.device_count('npu')
    )r   get_custom_device_countdev_type)r   nums     k/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/device/custom_device.pyr   r   E   s3    &  5*;77*844J    Nonec                 ,    t          j                     dS )a  
    Releases idle cached memory held by the allocator so that those can be used in other GPU
    application and visible in device-specific tools.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.empty_cache()
    N)r   device_empty_cache r%   r$   r   r   `   s     	r%   device_CustomPlaceLike | Nonec                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|          S )a  
    Return the peak size of memory that is allocated to tensor of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Returns:
        int: The peak size of memory that is allocated to tensor of the given device, in bytes.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.max_memory_allocated('npu:0')
            >>> paddle.device.max_memory_allocated('npu')
            >>> paddle.device.max_memory_allocated(0)
            >>> paddle.device.max_memory_allocated(Paddle.CustomPlace('npu',0))
    r   N:r   Invalid device ID '4'. After colon must be digits only. Example: 'npu:0'The input: z is not expected. Because paddle.device.max_memory_allocated only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'	Allocated

isinstancestrrfindisdigit
ValueErrorr   r   r   get_device_iddevice_memory_stat_peak_valuer*   	device_id	colon_idxdevice_id_strs       r$   r   r   n   s%   ( I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 -k9EEEr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|          S )a  
    Return the peak size of memory that is held by the allocator of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Returns:
        int: The peak size of memory that is held by the allocator of the given device, in bytes.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.max_memory_reserved('npu:0')
            >>> paddle.device.max_memory_reserved('npu')
            >>> paddle.device.max_memory_reserved(0)
            >>> paddle.device.max_memory_reserved(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.max_memory_reserved only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'Reservedr3   r;   s       r$   r   r      s%   ( I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 -j)DDDr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|           dS )a{  
    Reset the peak size of memory that is allocated to tensor of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.reset_max_memory_allocated('npu:0')
            >>> paddle.device.reset_max_memory_allocated('npu')
            >>> paddle.device.reset_max_memory_allocated(0)
            >>> paddle.device.reset_max_memory_allocated(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.reset_max_memory_allocated only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r2   
r4   r5   r6   r7   r8   r   r   r   r9   #device_memory_stat_reset_peak_valuer;   s       r$   r   r      s+   " I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 	,[)DDDDDr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|           dS )ay  
    Reset the peak size of memory that is held by the allocator of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.reset_max_memory_reserved('npu:0')
            >>> paddle.device.reset_max_memory_reserved('npu')
            >>> paddle.device.reset_max_memory_reserved(0)
            >>> paddle.device.reset_max_memory_reserved(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.reset_max_memory_reserved only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r@   rB   r;   s       r$   r   r     s+   " I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 	,ZCCCCCr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|          S )a  
    Return the current size of memory that is allocated to tensor of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Returns:
        int: The current size of memory that is allocated to tensor of the given device, in bytes.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.memory_allocated('npu:0')
            >>> paddle.device.memory_allocated('npu')
            >>> paddle.device.memory_allocated(0)
            >>> paddle.device.memory_allocated(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.memory_allocated only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r2   
r4   r5   r6   r7   r8   r   r   r   r9    device_memory_stat_current_valuer;   s       r$   r   r   8  s%   ( I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 0iHHHr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        d
|          S )a  
    Return the current size of memory that is held by the allocator of the given device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Returns:
        int: The current size of memory that is held by the allocator of the given device, in bytes.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.memory_reserved('npu:0')
            >>> paddle.device.memory_reserved('npu')
            >>> paddle.device.memory_reserved(0)
            >>> paddle.device.memory_reserved(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.memory_reserved only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r@   rF   r;   s       r$   r   r   l  s%   ( I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 0YGGGr%   core.CustomStreamc                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        t          |          S )
a]  
    Return the current stream by the device.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Returns:
        Stream: The stream to the device.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.current_stream('npu:0')
            >>> paddle.device.current_stream('npu')
            >>> paddle.device.current_stream(0)
            >>> paddle.device.current_stream(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.current_stream only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0')r4   r5   r6   r7   r8   r   r   r   r9   !_get_current_custom_device_streamr"   r;   s       r$   r   r     s%   ( I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 1(IFFFr%   c                   d}| d}nt          | t                    rb|                     d          }|dk    rd}n| |dz   d         }|                                st	          d| d          t          |          }nZt          | t
                    r| }nBt          | t          j                  r|                                 }nt	          d|  d	          t          j	        t          |           dS )
a$  
    Wait for the compute on the given device to finish.

    Args:
        device(_CustomPlaceLike, optional): Support input like 'npu:0', 'mlu', int, or CustomPlace.
            If None, the device is the first available custom device with index 0.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.device.synchronize('npu:0')
            >>> paddle.device.synchronize('npu')
            >>> paddle.device.synchronize(0)
            >>> paddle.device.synchronize(Paddle.CustomPlace('npu',0))
    r   Nr-   r.   r   r/   r0   r1   z is not expected. Because paddle.device.synchronize only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0')r4   r5   r6   r7   r8   r   r   r   r9   _synchronize_custom_devicer"   r;   s       r$   r   r     s+   " I~			FC	 	  
LL%%	??II"9q=??3M ((**  '- ' ' '  
 M**II	FC	 	  

			FD,	-	- 
((**		&   
 
 	
 	#Hi88888r%   core.GeneratorStatec                
   t           j                            |           }t          |t          j                  r%t	          j                                                    S t	          j        |                                          S )a%  
    Get the random state for the default generator.

    Returns:
        Tensor: The random state tensor.

    Examples:

        .. code-block:: python

            >>> # doctest: +REQUIRES(env:CUSTOM_DEVICE)
            >>> import paddle
            >>> paddle.device.get_rng_state()

    )	paddler*   device_to_placer4   r   CPUPlacedefault_cpu_generator	get_statedefault_custom_device_generator)r*   places     r$   get_rng_staterW     se    $ M))&11E%'' 8)++55777/66@@BBBr%   	new_statec                   t           j                            |          }t          |t          j                  r(t	          j                                        |            dS t	          j        |                              |            dS )ar  
    Set the random number generator state of the specified device.

    Args:
        new_state (core.GeneratorState): The desired RNG state to set.
            This should be a state object previously obtained from ``get_rng_state()``.
        device (DeviceLike, optional): The device to set the RNG state for.
            If not specified, uses the current default device (as returned by ``paddle.framework._current_expected_place_()``).
            Can be a device object, integer device ID, or device string.

    Returns:
        None

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> # Save RNG state
            >>> state = paddle.device.get_rng_state()
            >>> # Do some random operations
            >>> x = paddle.randn([2, 3])
            >>> # Restore RNG state
            >>> paddle.device.set_rng_state(state)
    N)	rP   r*   rQ   r4   r   rR   rS   	set_staterU   )rX   r*   rV   s      r$   set_rng_stater[     sv    6 M))&11E%'' I"$$..y99999,U33==iHHHHHr%   seedc                2   t          |           } t          j                                        }t	          |t
          j                  r(t          j                                        |            dS t          j	        |                              |            dS )a  Set the seed for generating random numbers for the current Device.

    .. warning::
        If you are working with a multi-Device model, this function is insufficient
        to get determinism.  To seed all Devices, use :func:`manual_seed_all`.
        If current Device is CPU, this function will set the seed of the default CPU generator.

    Sets the seed for global default generator, which manages the random number generation.

    Args:
        seed(int): The random seed to set.

    Returns:
        None

    Examples:
        .. code-block:: python
            >>> # doctest: +REQUIRES(env:CUSTOM_DEVICE)
            >>> import paddle
            >>> paddle.device.manual_seed(102)
            >>> # paddle.cuda.manual_seed(102) is equivalent to paddle.device.manual_seed(102)

    N)
r   rP   	framework_current_expected_placer4   r   rR   rS   manual_seedrU   )r\   rV   s     r$   r`   r`   ?  s    0 t99D4466E%'' F"$$0066666,U33??EEEEEr%   )N)r   r   r   r   )r   r&   )r*   r+   r   r   )r*   r+   r   r&   )r*   r+   r   rI   )r*   r+   r   rN   )rX   rN   r*   r+   r   r&   )r\   r   r   r&   )*
__future__r   typingr   r   typing_extensionsr   rP   paddle.baser   custom_streamsr	   r
   r   r   r   r5   r   r   __annotations__get_all_custom_device_type	dev_typesr"   is_compiled_with_custom_device	Exceptiongpgpu_backendr   default_backend__all__r   r   r   r   r   r   r   r   r   r   rW   r[   r`   r)   r%   r$   <module>rn      s   # " " " " " " ' ' ' ' ' ' ' ' ' ' ' ' ' '                    """""""'	#     ,D+--	$.9Q<<$ 7D7AA 
)V    79994444444666666  "    6   1F 1F 1F 1F 1Fh1E 1E 1E 1E 1Eh.E .E .E .E .Eb.D .D .D .D .Db1I 1I 1I 1I 1Ih1H 1H 1H 1H 1Hh1G 1G 1G 1G 1Gh.9 .9 .9 .9 .9d '+C C C C C2 GKI I I I IDF F F F F Fr%   