
    Αi O                       % S SK Jr  S SKJrJr  S SKJr  S SKrS SKJ	r	  SSK
JrJrJrJr  \(       a  S SKJr  \\\\4   rS	\S
'   \	R(                  " 5       r\(       a  \S    OSr\(       a  \	R.                  " \5      (       d  \" S5      e\(       a  \S;   a  SSKJr  OSSKJr  / SQrSSS jjrSS jrSSS jjrSSS jjr SS S jjr!SS S jjr"SSS jjr#SSS jjr$SS!S jjr%SS S jjr& S   S"S jjr' S     S#S jjr(S$S jr)g)%    )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synchronizec                x    U (       a  [         R                  " U 5      nU$ [         R                  " [        5      nU$ )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)device_typenums     [/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/device/custom_device.pyr   r   E   s5    & **;7 J **84J    c                 .    [         R                  " 5         g)z
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"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5      $ )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
ValueErrorintr   r   get_device_iddevice_memory_stat_peak_valuedevice	device_id	colon_idxdevice_id_strs       r!   r   r   n   s    ( I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 --k9EEr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5      $ )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)   r*   r+   z is not expected. Because paddle.device.max_memory_reserved only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'Reservedr-   r6   s       r!   r   r      s    ( I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 --j)DDr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5        g)aK  
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)   r*   r+   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'r,   
r.   r/   r0   r1   r2   r3   r   r   r4   #device_memory_stat_reset_peak_valuer6   s       r!   r   r      s    " I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 	,,[)Dr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5        g)aI  
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)   r*   r+   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<   r>   r6   s       r!   r   r     s    " I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 	,,ZCr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5      $ )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)   r*   r+   z is not expected. Because paddle.device.memory_allocated only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r,   
r.   r/   r0   r1   r2   r3   r   r   r4    device_memory_stat_current_valuer6   s       r!   r   r   8  s    ( I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 00iHHr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " S
U5      $ )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)   r*   r+   z is not expected. Because paddle.device.memory_reserved only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0'r<   rB   r6   s       r!   r   r   l  s    ( I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 00YGGr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " [        U5      $ )
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)   r*   r+   z is not expected. Because paddle.device.current_stream only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0')r.   r/   r0   r1   r2   r3   r   r   r4   !_get_current_custom_device_streamr   r6   s       r!   r   r     s    ( I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 11(IFFr"   c                   SnU c  SnO[        U [        5      (       aQ  U R                  S5      nUS:X  a  SnOXS-   S nUR                  5       (       d  [	        SU S35      e[        U5      nOW[        U [
        5      (       a  U nO?[        U [        R                  5      (       a  U R                  5       nO[	        SU  S	35      e[        R                  " [        U5        g)
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)   r*   r+   z is not expected. Because paddle.device.synchronize only support str, int or CustomPlace. Please input appropriate device again! Example: 'npu:0')r.   r/   r0   r1   r2   r3   r   r   r4   _synchronize_custom_devicer   r6   s       r!   r   r     s    " I~		FC	 	 LL%	?I"q=?3M ((** )- 9' ' 
 M*I	FC	 	 		FD,,	-	-((*	& " 
 	
 	##Hi8r"   c                   [         R                  R                  U 5      n[        U[        R
                  5      (       a#  [        R                  " 5       R                  5       $ [        R                  " U5      R                  5       $ )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()

)	paddler7   device_to_placer.   r   CPUPlacedefault_cpu_generator	get_statedefault_custom_device_generator)r7   places     r!   get_rng_staterQ     s[    $ MM))&1E%''))+5577//6@@BBr"   c                   [         R                  R                  U5      n[        U[        R
                  5      (       a%  [        R                  " 5       R                  U 5        g[        R                  " U5      R                  U 5        g)a&  
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)	rJ   r7   rK   r.   r   rL   rM   	set_staterO   )	new_stater7   rP   s      r!   set_rng_staterU     sY    6 MM))&1E%''""$..y9,,U3==iHr"   c                (   [        U 5      n [        R                  R                  5       n[	        U[
        R                  5      (       a%  [
        R                  " 5       R                  U 5        g[
        R                  " U5      R                  U 5        g)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)
r3   rJ   	framework_current_expected_placer.   r   rL   rM   manual_seedrO   )seedrP   s     r!   rY   rY   ?  sb    0 t9D446E%''""$006,,U3??Er"   )N)r   z
str | Nonereturnr3   )r[   None)r7   _CustomPlaceLike | Noner[   r3   )r7   r]   r[   r\   )r7   r]   r[   zcore.CustomStream)r7   r]   r[   core.GeneratorState)rT   r^   r7   r]   r[   r\   )rZ   r3   r[   r\   )*
__future__r   typingr   r   typing_extensionsr   rJ   paddle.baser   custom_streamsr	   r
   r   r   r   r/   r3   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   rQ   rU   rY   r%   r"   r!   <module>rl      s/   # ' '    ""'	#i  ++-	$9Q<$D77AA
V  9946"61Fh1Eh.Eb.Db1Ih1Hh1Gh.9d '+C#CC2 GKI"I,CI	IDFr"   