o
    Qeà(  ã                   @   s|   d dl mZ d dlZd dlZd dlZd dlZg 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 )é    )Úabsolute_importNc                  C   sv   t jjdg d¢dd} t jddgdt jt jj d¡dd	}t jdgdd
}t jjj	| ||d}t j
 |¡}| ||fS )zD
    Define a simple network composed by a single linear layer.
    Úinput)Né   r   Úfloat32)ÚnameÚshapeÚdtyper   é   gš™™™™™¹?)Úinitializer)r   r   Úattr)r   r   )ÚxÚweightÚbias)ÚpaddleÚstaticÚdataZcreate_parameterÚ	ParamAttrÚnnr
   ÚConstantZ
functionalÚlinearÚtensorÚsum)r   r   r   Ú
linear_outÚout© r   úJD:\Projects\ConvertPro\env\Lib\site-packages\paddle/utils/install_check.pyÚ_simple_network   s   þý
r   c                 C   sj   t jddgddggt jd}| dkr| | dd¡S g }t| ƒD ]}| |¡ qt  |¡}| | dd¡}|S )z“
    Prepare feeding data for simple network. The shape is [device_count, 2, 2].

    Args:
        device_count (int): The number of devices.
    ç      ð?g       @g      @g      @)r   é   r   )ÚnpÚarrayr   ZreshapeÚrangeÚappend)Údevice_countZnp_input_singleZ
input_listÚiZnp_input_mutir   r   r   Ú_prepare_data+   s   
r%   c               
   C   óT   zt tj ¡ ƒdksJ ‚W dS  ty) }  zt d | ¡¡ W Y d} ~ dS d} ~ ww )z)
    Check whether CUDA is avaiable.
    r   Tz“You are using GPU version PaddlePaddle, but there is no GPU detected on your machine. Maybe CUDA devices is not set properly.
 Original Error is {}NF)Úlenr   r   Úcuda_placesÚ	ExceptionÚloggingÚwarningÚformat©Úer   r   r   Ú_is_cuda_available?   ó   ý€ûr/   c               
   C   r&   )z(
    Check whether NPU is avaiable.
    r   Tz’You are using NPU version PaddlePaddle, but there is no NPU detected on your machine. Maybe NPU devices is not set properly.
 Original Error is {}NF)r'   r   r   Ú
npu_placesr)   r*   r+   r,   r-   r   r   r   Ú_is_npu_availableN   r0   r2   c               
   C   r&   )z(
    Check whether XPU is avaiable.
    r   Tz’You are using XPU version PaddlePaddle, but there is no XPU detected on your machine. Maybe XPU devices is not set properly.
 Original Error is {}NF)r'   r   r   Ú
xpu_placesr)   r*   r+   r,   r-   r   r   r   Ú_is_xpu_available]   r0   r4   c                 C   sØ   t  ¡  | rt  d¡ n|rt  d¡ n|rt  d¡ nt  d¡ t jdt jjjddd}t jd	t jjjd
dd}t jjdd||d}tdƒ}t  	|¡}||ƒ}t j
 |¡}	|	 ¡  t jjd| ¡ d}
|
 ¡  dS )zò
    Testing the simple network in dygraph mode using one CPU/GPU/XPU/NPU.

    Args:
        use_cuda (bool): Whether running with CUDA.
        use_xpu (bool): Whether running with XPU.
        use_npu (bool): Whether running with NPU.
    ZgpuZxpuZnpuÚcpur   g      à?)Úvalue)r   r
   r   r   r   é   )Úweight_attrÚ	bias_attrr   gü©ñÒMbP?)Úlearning_rateÚ
parametersN)r   Údisable_staticZ
set_devicer   r   r
   r   ZLinearr%   Z	to_tensorr   r   ZbackwardÚ	optimizerZAdamr;   Ústep)Úuse_cudaÚuse_xpuÚuse_npur8   r9   r   Zinput_npZinput_tensorr   r   Úoptr   r   r   Ú_run_dygraph_singlel   s8   	
ÿÿý
ÿrC   c              	   C   s&  t  ¡  t j t j ¡ ¡x t j ¡ }t j ¡ }d|_t j ||¡ tƒ \}}}t jj	||j
gdd }W d  ƒ n1 s?w   Y  | rLt  d¡}	n|rTt  d¡}	n|r\t  d¡}	nt  ¡ }	t j |	¡}
|
 |¡ |
j||j
tdƒi|j
|d j
gd W d  ƒ n1 sˆw   Y  t  ¡  dS )a  
    Testing the simple network with executor running directly, using one CPU/GPU/XPU/NPU.

    Args:
        use_cuda (bool): Whether running with CUDA.
        use_xpu (bool): Whether running with XPU.
        use_npu (bool): Whether running with NPU.
    r   )Zparameter_listr   N©ÚfeedZ
fetch_list)r   Úenable_staticr   Úscope_guardÚScopeÚProgramZrandom_seedÚprogram_guardr   Zappend_backwardr   Ú	CUDAPlaceÚXPUPlaceÚNPUPlaceÚCPUPlaceÚExecutorÚrunr%   r<   )r?   r@   rA   Ú
train_progÚstartup_progr   r   r   Zparam_gradsÚplaceÚexer   r   r   Ú_run_static_single   s8   	

ÿÿþ
þìrU   c              	   C   sJ  t  ¡  t j t j ¡ ¡Š t j ¡ }t j ¡ }t j ||¡! tƒ \}}}t j 	|¡}	d|	_
t jjdd |	¡ W d  ƒ n1 sCw   Y  t j |¡j|	j|d}
| r\t  d¡}n|rft  d¡}|}
n|rpt  d¡}|}
nt  ¡ }t j |¡}| |¡ |j|
|jtt|ƒƒi|	jgd W d  ƒ n1 sšw   Y  t  ¡  dS )a(  
    Testing the simple network in data parallel mode, using multiple CPU/GPU.

    Args:
        use_cuda (bool): Whether running with CUDA.
        use_xpu (bool): Whether running with XPU.
        use_npu (bool): Whether running with NPU.
        device_list (int): The specified devices.
    Tg{®Gáz„?)r:   N)Z	loss_nameZplacesr   rD   )r   rF   r   rG   rH   rI   rJ   r   r   ÚmeanZpersistabler=   ZSGDZminimizeZCompiledProgramZwith_data_parallelr   rK   rL   rM   rN   rO   rP   r%   r'   r<   )r?   r@   rA   Údevice_listrQ   rR   r   r   Ú_ZlossZcompiled_progrS   rT   r   r   r   Ú_run_static_parallel´   sB   


üÿþ


þærY   c               
   C   sB  t dƒ d} d}d}t ¡ rtƒ } nt ¡ rtƒ }nt ¡ r!tƒ }| r+d}tj 	¡ }n|r5d}tj 
¡ }n|r?d}tj ¡ }n	d}tjjdd}t|ƒ}t| ||ƒ t| ||ƒ t d	 |¡ƒ zt| |||ƒ t d
 ||¡ƒ t dƒ W dS  ty  } zt d ||¡¡ t d |¡¡ t d |¡ƒ W Y d}~dS d}~ww )a×  
    Check whether PaddlePaddle is installed correctly and running successfully
    on your system.

    Examples:
        .. code-block:: python

            import paddle

            paddle.utils.run_check()
            # Running verify PaddlePaddle program ...
            # W1010 07:21:14.972093  8321 device_context.cc:338] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 11.0, Runtime API Version: 10.1
            # W1010 07:21:14.979770  8321 device_context.cc:346] device: 0, cuDNN Version: 7.6.
            # PaddlePaddle works well on 1 GPU.
            # PaddlePaddle works well on 8 GPUs.
            # PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
    z(Running verify PaddlePaddle program ... FZGPUZXPUZNPUZCPUr   )r#   z PaddlePaddle works well on 1 {}.z"PaddlePaddle works well on {} {}s.zXPaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.a›  PaddlePaddle meets some problem with {} {}s. This may be caused by:
 1. There is not enough GPUs visible on your system
 2. Some GPUs are occupied by other process now
 3. NVIDIA-NCCL2 is not installed correctly on your system. Please follow instruction on https://github.com/NVIDIA/nccl-tests 
 to test your NCCL, or reinstall it following https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.htmlz
 Original Error is: {}zkPaddlePaddle is installed successfully ONLY for single {}! Let's start deep learning with PaddlePaddle now.N)Úprintr   Zis_compiled_with_cudar/   Zis_compiled_with_xpur4   Zis_compiled_with_npur2   r   r(   r3   r1   Z
cpu_placesr'   rU   rC   r,   rY   r)   r*   r+   )r?   r@   rA   Z
device_strrW   r#   r.   r   r   r   Ú	run_checkß   s\   ÿ
ÿúÿÿ€ör[   )Ú
__future__r   Úosr*   Únumpyr   r   Ú__all__r   r%   r/   r2   r4   rC   rU   rY   r[   r   r   r   r   Ú<module>   s   $$+