o
    pi%                     @  sv   d dl m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dZdS )    )annotationsNc                  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   a/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/utils/install_check.py_simple_network   s   

r   c                  C  s*   t jddgddggt jd} | dddS )zK
    Prepare feeding data for simple network. The shape is [1, 2, 2].

          ?g       @g      @g      @)r      r   )nparrayr   Zreshape)Znp_input_singler   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 available.
    r   TzYou 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er   r   r   _is_cuda_available6      r*   c               
   C  r"   )z)
    Check whether XPU is available.
    r   TzYou 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   _is_xpu_availableF   r+   r-   c                 C  s   t   | rt d n|rt d n|rt | 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 }t 	|}||}	t j
|	}
|
  t jjd| d}|  dS )z
    Testing the simple network in dygraph mode using one CPU/GPU/XPU.

    Args:
        use_cuda (bool): Whether running with CUDA.
        use_xpu (bool): Whether running with XPU.
    ZgpuZxpucpur   g      ?)value)r   r
   r   r   r      )weight_attr	bias_attrMbP?Zlearning_rate
parametersN)r   disable_staticZ
set_devicer   r   r
   r   Linearr!   Z	to_tensorr   r   backward	optimizerAdamr5   step)use_cudause_xpu
use_customcustom_device_namer1   r2   r   Zinput_npZinput_tensorr   r   optr   r   r   _run_dygraph_singleV   s4   

rA   c              	   C  s   t   t jt j u t j }t j }d|_t j|| t \}}}t jj	||gdd }	W d   n1 s>w   Y  | rKt 
d}
n|rSt d}
n|r\t |d}
nt  }
t j|
}|| |j||jt i||	d gd W d   n1 sw   Y  t   dS )z
    Testing the simple network with executor running directly, using one CPU/GPU/XPU.

    Args:
        use_cuda (bool): Whether running with CUDA.
        use_xpu (bool): Whether running with XPU.
    r   )Zparameter_listr   N)feedZ
fetch_list)r   Zenable_staticr   Zscope_guardZScopeZProgramZrandom_seedZprogram_guardr   Zappend_backwardZ	CUDAPlaceZXPUPlaceZCustomPlaceZCPUPlaceExecutorrunr   r!   r6   )r<   r=   r>   r?   Z
train_progZstartup_progr   r   r   Zparam_gradsZplaceexer   r   r   _run_static_single{   s:   




rF   c            	      C  s   G dd dt jj} t j  |  }t |}t j }t jjd|	 d}t 
ddgd}||}t 
ddgd}|||}|  |  |  dS )	z2
    train script for parallel training check
    c                      s(   e Zd ZdZ fddZdd Z  ZS )z)train_for_run_parallel.<locals>.LinearNetz?
        simple fc network for parallel training check
        c                   s.   t    tjdd| _tjdd| _d S )N
   r   )super__init__r   r   r7   _linear1_linear2)self	__class__r   r   rI      s   
z2train_for_run_parallel.<locals>.LinearNet.__init__c                 S  s   |  | |S )z!
            forward
            )rK   rJ   )rL   r   r   r   r   forward   s   z1train_for_run_parallel.<locals>.LinearNet.forward)__name__
__module____qualname____doc__rI   rO   __classcell__r   r   rM   r   	LinearNet   s    rU   r3   r4   rG   r   r   N)r   r   ZLayerdistributedZinit_parallel_envZDataParallelZMSELossr9   r:   r5   Zrandnr8   r;   Z
clear_grad)	rU   layerZdp_layerZloss_fnZadamZinputsZoutputslabelsZlossr   r   r   train_for_run_parallel   s   



rY   c                 C  s   t jjtt| d dS )z
    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.
        device_list (int): The specified devices.
    )ZnprocsN)r   rV   ZspawnrY   r#   )device_listr   r   r   _run_parallel   s   	r[   returnNonec            	   
   C  s  t d d} d}d}d}t rt } n-t rt }n%ttjj	 dkrAd}ttjj	 dkrAt
dtjj	 d  d | rKd	}tj }n+|rUd
}tj }n!|rmtjj	 d }|}tttjj|}n	d}tjjdd}t|}t| ||| t| ||| t d| d z't|dkr|rddl}d|jd< t| t d| d| d t d W dS  ty } zt
d| d| d t
d|  t d| d |d}~ww )a  
    Check whether PaddlePaddle is installed correctly and running successfully
    on your system.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> # doctest: +SKIP('the output will change in different run')
            >>> paddle.utils.run_check()
            Running verify PaddlePaddle program ...
            I0818 15:35:08.335391 30540 program_interpreter.cc:173] New Executor is Running.
            I0818 15:35:08.398319 30540 interpreter_util.cc:529] Standalone Executor is Used.
            PaddlePaddle works well on 1 CPU.
            PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
    z(Running verify PaddlePaddle program ... FNr   Tr   zWMore than one kind of custom devices detected, but run check would only be executed on .ZGPUZXPUZCPU)device_countzPaddlePaddle works well on 1 ZxcclZPADDLE_DISTRI_BACKENDzPaddlePaddle works well on  zs.zXPaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.z%PaddlePaddle meets some problem with aq  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: z7PaddlePaddle is installed successfully ONLY for single z2! Let's start deep learning with PaddlePaddle now.)printr   Zis_compiled_with_cudar*   Zis_compiled_with_xpur-   r#   Z	frameworkcoreZget_all_custom_device_typer&   r'   r   r$   r,   listrangeZget_custom_device_countZ
cpu_placesrF   rA   osenvironr[   r%   )	r<   r=   r>   r?   Z
device_strrZ   r_   re   r)   r   r   r   	run_check   st   


rg   )r\   r]   )
__future__r   r&   numpyr   r   __all__r   r!   r*   r-   rA   rF   rY   r[   rg   r   r   r   r   <module>   s   
%&*