
    Αiw                       % S SK Jr  S SKrS SKJrJrJr  S SKJ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JrJrJr  \(       a  S S
K	JrJr  S SKJr  \\\\\4   rS\S'   SSKJr  / SQr\" SSSSS9S0S1S jj5       r \" SSSSS9S0S2S jj5       r!S3S jr"S4S jr#S5S jr$S0S6S jjr%S0S6S jjr&S0S2S jjr'S0S2S jjr(S0S6S jjr)S0S6S  jjr*S7S! jr+\" SS"SS#S9\S8S$ j5       5       r, S0   S9S% jjr-S0S:S& jjr. S0   S;S' jjr/S0S<S( jjr0 S0     S=S) jjr1S>S* jr2S0S6S+ jjr3S0S2S, jjr4 S?     S@S- jjr5 S?     S@S. jjr6\SAS/ j5       r7g)B    )annotationsN)TYPE_CHECKINGNoReturnUnion)	TypeAlias)core)signature_safe_contextmanager)
deprecated   )EventStreamcreate_eventcreate_stream)	CUDAPlaceCustomPlace)_gpuDevicePropertiesr   _CudaPlaceLike)MemoryAnalysisTool)r   r   current_streamsynchronizedevice_countempty_cachemax_memory_allocatedmax_memory_reservedmemory_allocatedmemory_reservedstream_guardget_device_propertiesget_device_nameget_device_capabilityreset_max_memory_allocatedreset_max_memory_reservedmemory_summaryvmm_compactz2.5.0zpaddle.device.current_streamz>current_stream in paddle.device.cuda will be removed in future)since	update_tolevelreasonc                h   SnU b  [        U [        5      (       a  U nO[        U [        R                  5      (       a  U R	                  5       nOP[        U [
        5      (       a0  [        R                  R                  U 5      nUR	                  5       nO[        S5      e[        R                  " U5      $ )a  
Return the current CUDA stream by the device.

Args:
    device(paddle.CUDAPlace()|int|None, optional): The device or the ID of the device which want to get stream from.
            If device is None, the device is the current device. Default: None.

Returns:
        CUDAStream: the stream to the device.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> s1 = paddle.device.cuda.current_stream()

        >>> s2 = paddle.device.cuda.current_stream(0)

        >>> s3 = paddle.device.cuda.current_stream(paddle.CUDAPlace(0))

z+device type must be int or paddle.CUDAPlace)
isinstanceintr   r   get_device_idstrpaddledevice_convert_to_place
ValueError_get_current_streamr0   	device_idplaces      [/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/device/cuda/__init__.pyr   r   ?   s    @ Ifc""I//,,.I$$MM33F;E++-IJKK##I..    zpaddle.device.synchronizez;synchronize in paddle.device.cuda will be removed in futurec                R   U b  [        U [        5      (       a  U nO[        U [        R                  5      (       a  U R	                  5       nO[        U [
        5      (       a=  U R                  S5      (       a  [        U SS 5      nOU S:X  a  SnO[        SU  S35      e[        S5      e[        R                  R                  5       n[        R                  " 5       (       a0  [        U[        R                  5      (       a  UR	                  5       nOS	n[        R                  " U5      $ )
a  
Wait for the compute on the given CUDA device to finish.

Args:
    device(paddle.CUDAPlace()|int|None, optional): The device or the ID of the device.
            If device is None, the device is the current device. Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle

        >>> paddle.device.cuda.synchronize()
        >>> paddle.device.cuda.synchronize(0)
        >>> paddle.device.cuda.synchronize(paddle.CUDAPlace(0))

Ngpu:   gpur   The current string z is not expected. Because paddle.device.cuda.synchronize only support string which is like 'gpu:x' or 'gpu'. Please input appropriate string again!z0device type must be int, str or paddle.CUDAPlacer*   )r+   r,   r   r   r-   r.   
startswithr2   r/   	framework_current_expected_placeis_compiled_with_cuda_device_synchronizer4   s      r7   r   r   o   s    2 fc""I//,,.I$$  ((qr
O	5	 )& 2= =  OPP  88:''))j6##/
 /
 ++-II##I..r8   c                 b    [        [        S5      (       a  [        R                  " 5       n U $ Sn U $ )z
Return the number of GPUs available.

Returns:
    int: the number of GPUs available.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> paddle.device.cuda.device_count()

get_cuda_device_countr   )hasattrr   rD   )num_gpuss    r7   r   r      s=    $ 4011 	""$  O   Or8   c                 d    [         R                  " 5       (       a  [         R                  " 5         gg)a~  
Releases idle cached memory held by the allocator so that those can be used in other GPU
application and visible in `nvidia-smi`. In most cases you don't need to use this function,
Paddle does not release the memory back to the OS when you remove Tensors on the GPU,
Because it keeps gpu memory in a pool so that next allocations can be done much faster.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> tensor = paddle.randn([512, 512, 512], "float64")
        >>> del tensor
        >>> paddle.device.cuda.empty_cache()
N)r   rA   cuda_empty_cache r8   r7   r   r      s$    & !!## $r8   c           	        U c  [         R                  " 5       $ [        U [        5      (       a  U n[         R                  " 5       (       a  SnGOSn[         R
                  " 5       n[        U5      S:X  a!  U S:X  a  US   nOD[        SU  SUS    S35      eU H)  nUR                  S5      u  pg[        U5      U :X  d  M'  UnM+     Uc  [        SU  S	U 35      eO[        U [         R                  5      (       a  SnU R                  5       nO[        U [         R                  5      (       a!  U R                  5       nU R                  5       nO[        U [        5      (       a]  U R                  S
5      (       a  [        U SS 5      nOJSU ;   a   U R                  SS5      u  p8[        U5      nO$[        SU  SU S35      e[        SU  SU S35      eUS:  d   SU S35       e[         R                  " 5       (       a&  U[        5       :  d   SU S[        5        35       e U$ U[         R                   " W5      :  d$   SU SU S[         R                   " U5       35       eU$ )a~  
Return the id of the given device. It is just a utility that will not be exposed to users.

Args:
    device(paddle.CUDAPlace|paddle.CustomPlace|int|str): The device, the id of the device or
        the string name of device like 'gpu:x' or 'custom_device:x'.
        Default: None.

Return:
    int: The id of the given device. If device is None, return the id of current device.
Nr<   r   r   z
Device id z) not found in available_custom_devices: [z:0]:z( not found in available_custom_devices: r:   r;   r=   z is not expected. Because zi only support string which is like 'gpu:x' or '<custom_device>:x'. Please input appropriate string again!The device type z only support int, str (format 'gpu:x' or '<custom_device>:x'), paddle.CUDAPlace or paddle.CustomPlace. Please input appropriate device again!z4The device id must be not less than 0, but got id = .zThe device id z exceeds gpu card number z	 exceeds z device card number )r   get_cuda_current_device_idr+   r,   rA   get_available_custom_devicelenr2   splitr   r-   r   get_device_typer.   r>   r   get_custom_device_count)	r0   op_namer5   device_typeavailable_custom_devicesddev_typedev_iddevice_id_strs	            r7   extract_cuda_device_idr[      s    ~..00&#	%%''KK'+'G'G'I$+,1Q;":1"=K$$VH,UVnopVqUrruv  2A'(wws|$H6{f,&. 2 "  (PQiPjk  # 
FDNN	+	+((*		FD,,	-	-,,.((*		FC	 	 V$$F12JI6M)/c1)=&KM*I%fX-Gy Q9 9 
 vh&@	 J5 5
 	

 > 
>ykK> !!##<>) 	
YK'@@PQ	
)  477DD 	
YKy=QRVRnRnozR{Q|}	
D r8   c                   Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5      $ )as  
Return the peak size of memory that is allocated to tensor of the given device.

Note:
    The size of memory allocated to tensor is 256-byte aligned in Paddle, which may larger than the memory size that tensor actually need.
    For instance, a float32 0-D Tensor with shape [] will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

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

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> max_memory_allocated_size = paddle.device.cuda.max_memory_allocated(paddle.CUDAPlace(0))
        >>> max_memory_allocated_size = paddle.device.cuda.max_memory_allocated(0)
        >>> max_memory_allocated_size = paddle.device.cuda.max_memory_allocated("gpu:0")
z'paddle.device.cuda.max_memory_allocatedr   The API } is not supported in CPU-only PaddlePaddle. Please reinstall PaddlePaddle with GPU or custom device support to call this API.rT   	Allocated	r/   r0   get_all_custom_device_typer   rA   is_compiled_with_custom_devicer2   r[   device_memory_stat_peak_valuer0   namecustom_devicesr5   s       r7   r   r   #  s    6 5D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I--k9EEr8   c                   Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5      $ )aQ  
Return the peak size of memory that is held by the allocator of the given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

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

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> max_memory_reserved_size = paddle.device.cuda.max_memory_reserved(paddle.CUDAPlace(0))
        >>> max_memory_reserved_size = paddle.device.cuda.max_memory_reserved(0)
        >>> max_memory_reserved_size = paddle.device.cuda.max_memory_reserved("gpu:0")
z&paddle.device.cuda.max_memory_reservedr   r]   r^   r_   Reservedra   re   s       r7   r   r   N  s    . 4D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I--j)DDr8   c                    Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5        g)a  
Reset the peak size of memory that is allocated to tensor of the given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> paddle.device.cuda.reset_max_memory_allocated(paddle.CUDAPlace(0))
        >>> paddle.device.cuda.reset_max_memory_allocated(0)
        >>> paddle.device.cuda.reset_max_memory_allocated("gpu:0")
z-paddle.device.cuda.reset_max_memory_allocatedr   r]   r^   r_   r`   N	r/   r0   rb   r   rA   rc   r2   r[   #device_memory_stat_reset_peak_valuere   s       r7   r!   r!   u  s    * ;D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I,,[)Dr8   c                    Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5        g)a  
Reset the peak size of memory that is held by the allocator of the given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> paddle.device.cuda.reset_max_memory_reserved(paddle.CUDAPlace(0))
        >>> paddle.device.cuda.reset_max_memory_reserved(0)
        >>> paddle.device.cuda.reset_max_memory_reserved("gpu:0")
z,paddle.device.cuda.reset_max_memory_reservedr   r]   r^   r_   ri   Nrk   re   s       r7   r"   r"     s    * :D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I,,ZCr8   c                   Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5      $ )ad  
Return the current size of memory that is allocated to tensor of the given device.

Note:
    The size of memory allocated to tensor is 256-byte aligned in Paddle, which may be larger than the memory size that tensor actually need.
    For instance, a float32 0-D Tensor with shape [] will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

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

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> memory_allocated_size = paddle.device.cuda.memory_allocated(paddle.CUDAPlace(0))
        >>> memory_allocated_size = paddle.device.cuda.memory_allocated(0)
        >>> memory_allocated_size = paddle.device.cuda.memory_allocated("gpu:0")
z#paddle.device.cuda.memory_allocatedr   r]   r^   r_   r`   	r/   r0   rb   r   rA   rc   r2   r[    device_memory_stat_current_valuere   s       r7   r   r     s    6 1D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I00iHHr8   c                   Sn[         R                  R                  5       n[        R                  " 5       (       d4  U(       a  [        R
                  " US   5      (       d  [        SU S35      e[        XS9n[        R                  " SU5      $ )a?  
Return the current size of memory that is held by the allocator of the given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

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

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> memory_reserved_size = paddle.device.cuda.memory_reserved(paddle.CUDAPlace(0))
        >>> memory_reserved_size = paddle.device.cuda.memory_reserved(0)
        >>> memory_reserved_size = paddle.device.cuda.memory_reserved("gpu:0")
z"paddle.device.cuda.memory_reservedr   r]   r^   r_   ri   ro   re   s       r7   r   r     s    . 0D]]==?N""$$33N14EFF tf  Z  [
 	
 'v<I00YGGr8   c                    [        U [        R                  R                  R                  5      (       d  [        S5      e[        5       n[        U 5      [        U5      :X  a  U $ [        R                  " U 5      $ )z
Set the current stream.

Parameters:
    stream(paddle.device.cuda.Stream): The selected stream.

Returns:
    CUDAStream: The previous stream.

/stream type should be paddle.device.cuda.Stream)
r+   r/   r0   cudar   	TypeErrorr   idr   _set_current_stream)stream
cur_streams     r7   rw   rw     s[     ffmm007788IJJ!J	&zR
^###F++r8   zpaddle.device.stream_guardz<stream_guard in paddle.device.cuda will be removed in futurec              #  8  #    U b>  [        U [        R                  R                  R                  5      (       d  [        S5      e[        5       nU b  [        U 5      [        U5      :X  a  Sv   g[        U 5      n Sv   [        U5      n g! [        U5      n f = f7f)as  
Notes:
    This API only supports dynamic graph mode currently.

A context manager that specifies the current stream context by the given stream.

Parameters:
    stream(paddle.device.cuda.Stream): the selected stream. If stream is None, just yield.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> s = paddle.device.cuda.Stream()
        >>> data1 = paddle.ones(shape=[20])
        >>> data2 = paddle.ones(shape=[20])
        >>> with paddle.device.cuda.stream_guard(s):
        ...     data3 = data1 + data2

Nrs   )	r+   r/   r0   rt   r   ru   r   rv   rw   )rx   ry   
pre_streams      r7   r   r   &  s|     @ *VV]]5G5G5N5N"O"OIJJ!J~Fr*~5(0
	5(4F(4Fs   A7B:B
 >B
BBc                   [         R                  " 5       (       d  [        S5      eU b  [        U [        5      (       a  U nO[        U [         R
                  5      (       a  U R                  5       nOc[        U [        5      (       a=  U R                  S5      (       a  [	        U SS 5      nO)U S:X  a  SnO [        SU  S35      e[        S	U  S
35      eSn[         R                  " U5      $ )a  
Return the properties of given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x' which to get the properties of the
        device from. If device is None, the device is the current device.
        Default: None.

Returns:
    _gpuDeviceProperties: The properties of the device which include ASCII string
    identifying device, major compute capability, minor compute capability, global
    memory available and the number of multiprocessors on the device.

Examples:

    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)

        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> paddle.device.cuda.get_device_properties()
        >>> # _gpuDeviceProperties(name='A100-SXM4-40GB', major=8, minor=0, total_memory=40536MB, multi_processor_count=108)

        >>> paddle.device.cuda.get_device_properties(0)
        >>> # _gpuDeviceProperties(name='A100-SXM4-40GB', major=8, minor=0, total_memory=40536MB, multi_processor_count=108)

        >>> paddle.device.cuda.get_device_properties('gpu:0')
        >>> # _gpuDeviceProperties(name='A100-SXM4-40GB', major=8, minor=0, total_memory=40536MB, multi_processor_count=108)

        >>> paddle.device.cuda.get_device_properties(paddle.CUDAPlace(0))
        >>> # _gpuDeviceProperties(name='A100-SXM4-40GB', major=8, minor=0, total_memory=40536MB, multi_processor_count=108)

zThe API paddle.device.cuda.get_device_properties is not supported in CPU-only PaddlePaddle. Please reinstall PaddlePaddle with GPU support to call this API.Nr:   r;   r<   r   r=   z is not expected. Because paddle.device.cuda.get_device_properties only support string which is like 'gpu:x' or 'gpu'. Please input appropriate string again!rL   z is not expected. Because paddle.device.cuda.get_device_properties only support int, str or paddle.CUDAPlace. Please input appropriate device again!r*   )
r   rA   r2   r+   r,   r   r-   r.   r>   r   r0   r5   s     r7   r   r   T  s    N %%'' 
 	
 fc""I//,,.I$$  ((qr
O	5	 )& 2= =  "6( +9 9  	%%i00r8   c                ,    [        U 5      R                  $ )a  
Return the name of the device which is got from CUDA function `cudaDeviceProp <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0>`_.

Parameters:
    device(paddle.CUDAPlace|int|None, optional): The device or the ID of the device. If device is None (default), the device is the current device.

Returns:
    str: The name of the device.

Examples:

    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> paddle.device.cuda.get_device_name()

        >>> paddle.device.cuda.get_device_name(0)

        >>> paddle.device.cuda.get_device_name(paddle.CUDAPlace(0))

)r   rf   )r0   s    r7   r   r     s    4 !(---r8   c                H    [        U 5      nUR                  UR                  4$ )aq  
Return the major and minor revision numbers defining the device's compute capability which are got from CUDA function `cudaDeviceProp <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0>`_.

Parameters:
    device(paddle.CUDAPlace|int|None, optional): The device or the ID of the device. If device is None (default), the device is the current device.

Returns:
    tuple(int,int): the major and minor revision numbers defining the device's compute capability.

Examples:

    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)

        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> paddle.device.cuda.get_device_capability()

        >>> paddle.device.cuda.get_device_capability(0)

        >>> paddle.device.cuda.get_device_capability(paddle.CUDAPlace(0))

)r   majorminor)r0   props     r7   r    r      s!    6 !(D::tzz!!r8   c                (   [         R                  R                  U 5      n[        U[        R
                  5      (       a#  [        R                  " 5       R                  5       $ [        R                  " UR                  5       5      R                  5       $ )z
Get the random state for the default generator.

Returns:
    Tensor: The random state tensor.

Examples:

    .. code-block:: python

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

)
r/   r0   device_to_placer+   r   CPUPlacedefault_cpu_generator	get_statedefault_cuda_generatorr-   )r0   r6   s     r7   get_rng_stater     sd      MM))&1E%''))+5577&&u':':'<=GGIIr8   c                0   [         R                  R                  U5      n[        U[        R
                  5      (       a%  [        R                  " 5       R                  U 5        g[        R                  " UR                  5       5      R                  U 5        g)aP  
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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> 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)
r/   r0   r   r+   r   r   r   	set_stater   r-   )	new_stater0   r6   s      r7   set_rng_stater     sb    8 MM))&1E%''""$..y9##E$7$7$9:DDYOr8   c                D   [        U 5      n [        R                  R                  5       n[	        U[
        R                  5      (       a%  [
        R                  " 5       R                  U 5        g[
        R                  " UR                  5       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)
        >>> paddle.cuda.manual_seed(102)

N)r,   r/   r?   _current_expected_place_r+   r   r   r   manual_seedr   r-   )seedr6   s     r7   r   r     sk    2 t9D557E%''""$006##E$7$7$9:FFtLr8   c                    Sn[         R                  " 5       (       d  [        SU S35      e[        XS9n[         R                  " U5      $ )a  
Defragment the free memory blocks managed by the Virtual Memory Management (VMM)
allocator of the given device.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

Returns:
    int: The amount of memory (in bytes) that was moved during the compaction.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> moved_bytes = paddle.device.cuda.vmm_compact(0)
        >>> print(f"Bytes moved during compaction: {moved_bytes}")
zpaddle.device.cuda.vmm_compactr]   zl is not supported in CPU-only PaddlePaddle. Please reinstall PaddlePaddle with GPU support to call this API.r_   )r   rA   r2   r[   r$   )r0   rf   r5   s      r7   r$   r$   4  sU    . ,D&&((tf  I  J
 	
 'v<II&&r8   c                D    [        U SS9n[        R                  " U5        g)a"  
Get detailed summary of the CUDA memory usage
for the specified device, printed in three distinct sections: Global Summary,
Allocator Summary, and Distribution. This function prints the summary directly
to the terminal.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

The summary includes:
1. Global Summary: GPU utilization rates and physical memory information (similar to nvidia-smi).
2. Allocator Summary: Memory allocated by the PaddlePaddle's allocator (Total, Used, Free),
   including a Weighted Fragmentation Rate.
3. Distribution: A wide pivot table showing the size distribution of allocated blocks
   (split by common sizes like 1M, 10M, ... 3G).

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> paddle.device.cuda.memory_summary(0)
r#   r_   N)r[   r   r#   r}   s     r7   r#   r#   T  s    8 'v7GHI%%i0r8   c                   [        U SS9n[        R                  R                  U5      nUnUb  US:X  a8  [        R
                  R                  [        R                  " 5       SU S35      nO[        R
                  R                  U5      n[        R
                  R                  U5      n[        R
                  R                  U5      u  pxU SU U 3n	[        R
                  R                  XY5      n[        R
                  R                  U5      nU(       a:  [        R
                  R                  U5      (       d  [        R                  " U5        [        R                  " X45        g)a{  
Retrieve recorded Allocate events on the specified device and prints the events directly
to the terminal; these events are only counted when FLAGS_record_alloc_event is set to true.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> paddle.device.cuda.allocate_record_table(0)
allocate_record_tabler_   N memory_analysis_idz.txt_id)r[   r/   r   get_allocate_recordospathjoingetcwddirnamebasenamesplitextexistsmakedirsr   r   
r0   	save_pathr5   dataupdated_save_pathdir_name	base_namefile_name_without_extextnew_file_names
             r7   r   r   t  s	   * 'v7NOI;;**95D!IOGGLLIIK-i[=
 77??9-GG$$Y/	%'WW%5%5i%@"01YKuEGGLLAww01Hx00
H,,TEr8   c                   [        U SS9n[        R                  R                  U5      nUnUb  US:X  a8  [        R
                  R                  [        R                  " 5       SU S35      nO[        R
                  R                  U5      n[        R
                  R                  U5      n[        R
                  R                  U5      u  pxU SU U 3n	[        R
                  R                  XY5      n[        R
                  R                  U5      nU(       a:  [        R
                  R                  U5      (       d  [        R                  " U5        [        R                  " X45        g)a  
Retrieve recorded Allocate events on the specified device and plot the events, default name is 'memory_analysis.png', saved at current working directory;
these events are only counted when FLAGS_record_alloc_event is enabled.

Args:
    device(paddle.CUDAPlace|int|str|None, optional): The device, the id of the device or
        the string name of device like 'gpu:x'. If device is None, the device is the current device.
        Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')  # or '<custom_device>'

        >>> paddle.device.cuda.allocate_record_plot(0)
allocate_record_plotr_   Nr   r   z.pngr   )r[   r/   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s
             r7   r   r     s	   * 'v7MNI;;**95D!IOGGLLIIK-i[=
 77??9-GG$$Y/	%'WW%5%5i%@"01YKuEGGLLAww01Hx00
H++DDr8   c              #  L  #    [         R                  R                  S5      n[        R                  " S5      S   n U (       a,  S[         R                  S'   [        R
                  " SS05        O+S[         R                  S'   [        R
                  " SS05        Sv   Uc  [         R                  S	 OU[         R                  S'   [        R
                  " SU05        g! Uc  [         R                  S	 OU[         R                  S'   [        R
                  " SU05        f = f7f)a  
Notes:
    This API only supports dynamic graph mode currently.

A context manager that enables/disables allocate record guard.

Parameters:
    flag(bool): whether to record allocate events.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('gpu')

        >>> data1 = paddle.ones(shape=[20])
        >>> data2 = paddle.ones(shape=[20])
        >>> with paddle.device.cuda.allocate_record_guard(True):
        ...     data3 = data1 + data2

FLAGS_record_alloc_eventTrueTFalseFN)r   environgetr/   	get_flags	set_flags)flagtmp_envtmp_cpps      r7   allocate_record_guardr     s     0 jjnn78G9:"G@5;BJJ128$?@5<BJJ128%@A?

565<BJJ124g>?	 ?

565<BJJ124g>?s   9D$A"C AD$AD!!D$)N)r0   _CudaPlaceLike | Nonereturncore.CUDAStream)r0   r   r   None)r   r,   )r   r   )r0   r   rT   r.   r   r,   )r0   r   r   r,   )rx   r   r   r   )rx   r   r   r   )r0   r   r   r   )r0   r   r   r.   )r0   r   r   ztuple[int, int])r0   r   r   core.GeneratorState)r   r   r0   r   r   r   )r   r,   r   r   )NN)r0   r   r   z
str | Noner   r   )r   boolr   r   )8
__future__r   r   typingr   r   r   typing_extensionsr   r/   paddle.baser   paddle.base.wrapped_decoratorr	   paddle.utilsr
   streamsr   r   r   r   r   r   paddle.base.libpaddler   r.   r,   r   __annotations__memory_analyzerr   __all__r   r   r   r   r[   r   r   r!   r"   r   r   rw   r   r   r   r    r   r   r   r$   r#   r   r   r   rI   r8   r7   <module>r      s   # 	 1 1 '   G # ? ?-: %	!NI  0, 
,
K	'/'/T 
)
H	-/-/`2 .K\(FV$EN"EJ"DJ(IV$HN,* 
*
I	 $5 $5P %)G1!G1G1T.< %)"!"">J. EI P" P,A P	 PFMB'@1B CG&F!&F5?&F	&FT CG&E!&E5?&E	&ER (@ (@r8   