o
    Mea=                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 dd	lmZ g d
Zdadd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( ZdS ))    N)core)	framework)ParallelEnv)is_compiled_with_cinn)is_compiled_with_cuda)is_compiled_with_rocm   )cuda)get_cudnn_version
set_device
get_deviceXPUPlaceIPUPlaceMLUPlaceis_compiled_with_xpuis_compiled_with_ipur   r   r   is_compiled_with_npuis_compiled_with_mluget_all_device_typeget_all_custom_device_typeget_available_deviceget_available_custom_devicec                   C      t  S )a  
    Whether paddle was built with WITH_ASCEND_CL=ON to support Ascend NPU.

    Returns (bool): `True` if NPU is supported, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_npu = paddle.device.is_compiled_with_npu()
    )r   r    r   r   FD:\Projects\ConvertPro\env\Lib\site-packages\paddle/device/__init__.pyr   3      r   c                   C   r   )a  
    Whether paddle was built with WITH_IPU=ON to support Graphcore IPU.

    Returns (bool): `True` if IPU is supported, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_ipu = paddle.is_compiled_with_ipu()
    )r   r   r   r   r   r   r   B   r   r   c                   C   r   )z
    Return a Graphcore IPU Place

    Examples:
        .. code-block:: python

            # required: ipu

            import paddle
            place = paddle.device.IPUPlace()
    )r   r   r   r   r   r   r   Q   r   r   c                   C   r   )a  
    Whether paddle was built with WITH_XPU=ON to support Baidu Kunlun

    Returns (bool): whether paddle was built with WITH_XPU=ON

    Examples:
        .. code-block:: python

            import paddle
            support_xpu = paddle.device.is_compiled_with_xpu()
    )r   r   r   r   r   r   r   `   r   r   c                 C   
   t | S )a  
    Return a Baidu Kunlun Place

    Parameters:
        dev_id(int): Baidu Kunlun device id

    Examples:
        .. code-block:: python

            # required: xpu
            
            import paddle
            place = paddle.device.XPUPlace(0)
    )r   r   dev_idr   r   r   r   o      
r   c                   C   r   )a0  
    Whether paddle was built with WITH_MLU=ON to support Cambricon MLU

    Returns (bool): whether paddle was built with WITH_MLU=ON

    Examples:
        .. code-block:: python

            # required: mlu

            import paddle
            support_mlu = paddle.device.is_compiled_with_mlu()
    )r   r   r   r   r   r   r      s   r   c                 C   r   )z
    Return a Cambricon MLU Place

    Parameters:
        dev_id(int): MLU device id

    Examples:
        .. code-block:: python

            # required: mlu

            import paddle
            place = paddle.device.MLUPlace(0)
    )r   r   r   r   r   r   r      r   r   c                  C   s8   t  sdS tdu rtt  } | atdk rdS | S tS )a  
    This funciton return the version of cudnn. the retuen value is int which represents the 
    cudnn version. For example, if it return 7600, it represents the version of cudnn is 7.6.
    
    Returns:
        int: A int value which represents the cudnn version. If cudnn version is not installed, it return None.

    Examples:
        .. code-block:: python
            
            import paddle

            cudnn_version = paddle.device.get_cudnn_version()



    Nr   )r   r   _cudnn_versionintcudnn_version)r"   r   r   r   r
      s   r
   c              	   C   s:  |   }|dkrt }|S |dkr#t stdtt j}|S |dkrEt s/tdt	
ddd}t|d	 }t|}|S |d
krgt sQtdt	
ddd}t|d	 }t|}|S |dkryt sstdt }|S |dkrt stdt	
ddd}t|d	 }t|}|S | t v rt	
d| dd}t|d	 }t| |}|S td|}td|}	td|}
td|}|s|	s|
s|s| dd}|d	 }|t v r|d }t|}t||}ntdddd g dt  D |r5t s"td|| dd}|d }t|}t|}|	rWt sDtd|	| dd}|d }t|}t|}|
ryt sftd |
| dd}|d }t|}t|}|rt std!|| dd}|d }t|}t|}|S )"NcpugpuzLThe device should not be 'gpu', since PaddlePaddle is not compiled with CUDAxpuzKThe device should not be 'xpu', since PaddlePaddle is not compiled with XPUZFLAGS_selected_xpus0,r   npuzKThe device should not be 'npu', since PaddlePaddle is not compiled with NPUZFLAGS_selected_npusZipuzKThe device should not be 'ipu', since PaddlePaddle is not compiled with IPUmluzKThe device should not be 'mlu', since PaddlePaddle is not compiled with MLUZFLAGS_selected_mluszFLAGS_selected_{}szgpu:\d+zxpu:\d+znpu:\d+zmlu:\d+:r   z3The device must be a string which is like 'cpu', {}z, c                 s   s    | ]	}d  ||V  qdS )z'{}', '{}:x'N)format).0xr   r   r   	<genexpr>   s    z$_convert_to_place.<locals>.<genexpr>)r$   r%   r(   r)   zIThe device should not be {}, since PaddlePaddle is not compiled with CUDAzHThe device should not be {}, since PaddlePaddle is not compiled with XPUzHThe device should not be {}, since PaddlePaddle is not compiled with NPUzHThe device should not be {}, since PaddlePaddle is not compiled with mlu)lowerr   CPUPlacer   
ValueError	CUDAPlacer   r   r   osgetenvsplitr!   r   r   NPUPlacer   r   r   r   r   r+   CustomPlacerematchjoin)deviceZlower_deviceplaceZselected_xpus	device_idZselected_npusZselected_mlusZselected_devicesZavaliable_gpu_deviceZavaliable_xpu_deviceZavaliable_npu_deviceZavaliable_mlu_deviceZdevice_info_listdevice_typer   r   r   _convert_to_place   s   \W
P
IC
<7







r?   c                 C   s   t | }t| |S )a3  
    Paddle supports running calculations on various types of devices, including CPU, GPU, XPU, NPU, MLU and IPU.
    They are represented by string identifiers. This function can specify the global device
    which the OP will run.

    Parameters:
        device(str): This parameter determines the specific running device.
            It can be ``cpu``, ``gpu``, ``xpu``, ``npu``, ``mlu``, ``gpu:x``, ``xpu:x``, ``npu:x``, ``mlu:x`` and ``ipu``,
            where ``x`` is the index of the GPUs, XPUs, NPUs or MLUs.

    Examples:

     .. code-block:: python
            
        import paddle

        paddle.device.set_device("cpu")
        x1 = paddle.ones(name='x1', shape=[1, 2], dtype='int32')
        x2 = paddle.zeros(name='x2', shape=[1, 2], dtype='int32')
        data = paddle.stack([x1,x2], axis=1)
    )r?   r   Z_set_expected_place)r;   r<   r   r   r   r   &  s   
r   c                  C   s  d} t  }t|tjrd} | S t|tjr"| }dt| } | S t|tjr4| }dt| } | S t|tj	rF| }dt| } | S t|tj
rYt }d|d } | S t|tjrk| }dt| } | S t|tjr| }| }|d	 t| } | S td
|)a  
    This funciton can get the current global device of the program is running.
    It's a string which is like 'cpu', 'gpu:x', 'xpu:x', 'mlu:x' and 'npu:x'. if the global device is not
    set, it will return a string which is 'gpu:x' when cuda is avaliable or it 
    will return a string which is 'cpu' when cuda is not avaliable.

    Examples:

     .. code-block:: python
            
        import paddle
        device = paddle.device.get_device()

     r#   zgpu:zxpu:znpu:zipus:{{0-{}}}r   zmlu:r*   z&The device specification {} is invalid)r   Z_current_expected_place
isinstancer   r0   r2   Zget_device_idstrr   r6   r   Zget_ipu_device_countr+   r   r7   Zget_device_typer1   )r;   r<   r=   Znum_devicesr>   r   r   r   r   A  s>   r   c                   C   r   )a  
    Get all available device types.

    Returns:
        A list of all available device types.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_all_device_type()

            # Case 1: paddlepaddle-cpu package installed, and no custom device registerd.
            # Output: ['cpu']

            # Case 2: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: ['cpu', 'gpu']

            # Case 3: paddlepaddle-cpu package installed, and custom deivce 'CustomCPU' is registerd.
            # Output: ['cpu', 'CustomCPU']

            # Case 4: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['cpu', 'gpu', 'CustomCPU', 'CustomGPU']
    )r   r   r   r   r   r   r   m     r   c                   C   r   )a  
    Get all available custom device types.

    Returns: 
        A list of all available custom device types.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_all_custom_device_type()

            # Case 1: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: None

            # Case 2: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['CustomCPU', 'CustomGPU']
    )r   r   r   r   r   r   r        r   c                   C   r   )a=  
    Get all available devices.

    Returns:
        A list of all available devices.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_available_device()

            # Case 1: paddlepaddle-cpu package installed, and no custom device registerd.
            # Output: ['cpu']

            # Case 2: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: ['cpu', 'gpu:0', 'gpu:1']

            # Case 3: paddlepaddle-cpu package installed, and custom deivce 'CustomCPU' is registerd.
            # Output: ['cpu', 'CustomCPU']

            # Case 4: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['cpu', 'gpu:0', 'gpu:1', 'CustomCPU', 'CustomGPU:0', 'CustomGPU:1']
    )r   r   r   r   r   r   r     rC   r   c                   C   r   )a  
    Get all available custom devices.

    Returns:
       A list of all available custom devices.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_available_custom_device()

            # Case 1: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: None

            # Case 2: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['CustomCPU', 'CustomGPU:0', 'CustomGPU:1']
    )r   r   r   r   r   r   r     rD   r   )r8   r3   Zpaddle.fluidr   r   Zpaddle.fluid.dygraph.parallelr   Zpaddle.fluid.frameworkr   r   r   r@   r	   __all__r    r   r   r   r   r   r   r   r
   r?   r   r   r   r   r   r   r   r   r   r   <module>   s4    b,