o
    "jJ                     @   s   d dl Z d dlmZ d dlmZ d dlmZ ddlmZm	Z	 g dZ
edd	dd
dd,ddZedddddd,ddZdd Zdd Zdd Zd,ddZd,ddZd,ddZd,ddZd d! Zedd"dd#ded$d% Zd,d&d'Zd,d(d)Zd,d*d+ZdS )-    N)core)signature_safe_contextmanager)
deprecated   )StreamEvent)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_capabilityz2.5.0zpaddle.device.current_streamz>current_stream in paddle.device.cuda will be removed in future)ZsinceZ	update_tolevelreasonc                 C   sD   d}| durt | tr| }nt | tjr|  }ntdt|S )a  
    Return the current CUDA stream by the device.

    Args:
        device(paddle.CUDAPlace()|int, 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))

    N+device type must be int or paddle.CUDAPlace)
isinstanceintr   	CUDAPlaceget_device_id
ValueErrorZ_get_current_streamdevice	device_id r    \/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/device/cuda/__init__.pyr   (   s    


r   zpaddle.device.synchronizez;synchronize in paddle.device.cuda will be removed in futurec                 C   sl   | durt | tr| }n%t | tjr|  }ntdtj }t	 r/t |tjr/| }nd}t
|S )a  
    Wait for the compute on the given CUDA device to finish.

    Args:
        device(paddle.CUDAPlace()|int, 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))

    Nr   r   )r   r   r   r   r   r   paddleZ	frameworkZ_current_expected_placeis_compiled_with_cudaZ_device_synchronize)r   r   Zplacer    r    r!   r	   U   s   





r	   c                  C   s   t tdrt } | S d} | S )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   r$   )Znum_gpusr    r    r!   r
      s   r
   c                   C   s   t  r
t   dS dS )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], "float")
            >>> del tensor
            >>> paddle.device.cuda.empty_cache()
    N)r   r#   Zcuda_empty_cacher    r    r    r!   r      s   r   c                 C   s   | du rt  S t| tr| }n.t| t jr|  }n#t| tr6| dr.t| dd }ntd	| |td	| ||dksJJ d| d|t
 k sZJ d	| d
t
  |S )a  
    Return the id of the given cuda device. It is just a utility that will not be exposed to users.

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

    Return:
        int: The id of the given device. If device is None, return the id of current device.
    Ngpu:   zThe current string {} is not expected. Because {} only support string which is like 'gpu:x'. Please input appropriate string again!zThe device type {} is not expected. Because {} only support int, str or paddle.CUDAPlace. Please input appropriate device again!r   z4The device id must be not less than 0, but got id = .zThe device id z exceeds gpu card number )r   Zget_cuda_current_device_idr   r   r   r   str
startswithr   formatr
   )r   op_namer   r    r    r!   extract_cuda_device_id   s0   





r-   c                 C   4   d}t  std| dt| |d}t d|S )a  
    Return the peak size of gpu memory that is allocated to tensor of the given device.

    Note:
        The size of GPU 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 tensor with shape [1] in GPU will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes.

    Args:
        device(paddle.CUDAPlace or int or str, 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 gpu 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')

            >>> 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_allocatedThe API l is not supported in CPU-only PaddlePaddle. Please reinstall PaddlePaddle with GPU support to call this API.r,   	Allocatedr   r#   r   r-   Zdevice_memory_stat_peak_valuer   namer   r    r    r!   r         
r   c                 C   r.   )a  
    Return the peak size of GPU memory that is held by the allocator of the given device.

    Args:
        device(paddle.CUDAPlace or int or str, 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 GPU 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')

            >>> 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/   r0   r1   Reservedr3   r4   r    r    r!   r         
r   c                 C   r.   )a  
    Return the current size of gpu memory that is allocated to tensor of the given device.

    Note:
        The size of GPU 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 tensor with shape [1] in GPU will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes.

    Args:
        device(paddle.CUDAPlace or int or str, 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 gpu 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')

            >>> 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/   r0   r1   r2   r   r#   r   r-   Z device_memory_stat_current_valuer4   r    r    r!   r     r6   r   c                 C   r.   )ap  
    Return the current size of GPU memory that is held by the allocator of the given device.

    Args:
        device(paddle.CUDAPlace or int or str, 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 GPU 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')

            >>> 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/   r0   r1   r7   r9   r4   r    r    r!   r   C  r8   r   c                 C   s<   t | tjjjstdt }t| t|kr| S t	| S )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"   r   cudar   	TypeErrorr   idr   _set_current_stream)stream
cur_streamr    r    r!   r>   c  s   
r>   zpaddle.device.stream_guardz<stream_guard in paddle.device.cuda will be removed in futurec                 c   sr    | durt | tjjjstdt }| du s t| t|kr%dV  dS t| }z
dV  W t|} dS t|} w )a  
    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

    Nr:   )	r   r"   r   r;   r   r<   r   r=   r>   )r?   r@   Z
pre_streamr    r    r!   r   x  s    
r   c                 C   s   t  std| durBt| tr| }n0t| t jr|  }n%t| tr:| dr2t| dd }ntd|  dtd|  dd	}t 	|S )
a  
    Return the properties of given device.

    Args:
        device(paddle.CUDAPlace or int or str, 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'   zThe current string z is not expected. Because paddle.device.cuda.get_device_properties only support string which is like 'gpu:x'. Please input appropriate string again!zThe device type 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   r#   r   r   r   r   r   r)   r*   r   r   r    r    r!   r     s(   %






r   c                 C   s
   t | jS )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, 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   r5   )r   r    r    r!   r     s   
r   c                 C   s   t | }|j|jfS )a  
    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, 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)r   propr    r    r!   r   	  s   r   )N)r"   Zpaddle.baser   Zpaddle.base.wrapped_decoratorr   Zpaddle.utilsr   streamsr   r   __all__r   r	   r
   r   r-   r   r   r   r   r>   r   r   r   r   r    r    r    r!   <module>   sN   '%
+
$
 
$ 
'
F