o
    #j%                     @   sh   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 )    Nc                  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   [/var/www/html/Deteccion_Ine/venv/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_available4      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_availableD   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Adamr4   step)use_cudause_xpu
use_customcustom_device_namer0   r1   r   Zinput_npZinput_tensorr   r   optr   r   r   _run_dygraph_singleT   s4   

r@   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 i|j
|	d j
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_backwardr   Z	CUDAPlaceZXPUPlaceZCustomPlaceZCPUPlaceZExecutorrunr    r5   )r;   r<   r=   r>   Z
train_progZstartup_progr   r   r   Zparam_gradsZplaceexer   r   r   _run_static_singley   s:   



rD   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   r6   _linear1_linear2)self	__class__r   r   rG      s   
z2train_for_run_parallel.<locals>.LinearNet.__init__c                 S   s   |  | |S )z!
            forward
            )rI   rH   )rJ   r   r   r   r   forward   s   z1train_for_run_parallel.<locals>.LinearNet.forward)__name__
__module____qualname____doc__rG   rM   __classcell__r   r   rK   r   	LinearNet   s    rS   r2   r3   rE   r   r   N)r   r   ZLayerdistributedZinit_parallel_envZDataParallelZMSELossr8   r9   r4   Zrandnr7   r:   Z
clear_grad)	rS   layerZdp_layerZloss_fnZadamZinputsZoutputslabelsZlossr   r   r   train_for_run_parallel   s   



rW   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   rT   ZspawnrW   r"   )device_listr   r   r   _run_parallel   s   	rY   c            	   
   C   s  t d d} d}d}d}t rt } n,t rt }n$ttjj	 dkr@d}ttjj	 dkr@t
dtjj	 d  | rJd}tj }n+|rTd	}tj }n!|rlt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

            >>> paddle.utils.run_check()
            >>> # doctest: +SKIP('the output will change in different run')
            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   zZMore 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&   formatr   r#   r+   listrangeZget_custom_device_countZ
cpu_placesrD   r@   osenvironrY   r$   )	r;   r<   r=   r>   Z
device_strrX   rZ   rb   r(   r   r   r   	run_check   sx   


rd   )r%   numpyr   r   __all__r   r    r)   r,   r@   rD   rW   rY   rd   r   r   r   r   <module>   s   
%&*