o
    1j6                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZddlmZ ddl	m
Z
 ddlmZ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gZG dd de jdZG dd deZdS )    N   )logging)parse_device)CalledProcessErrorraise_unsupported_api_error)DRY_RUN)abspath   )CLIArgument)CompletedProcess)run_cmd
BaseRunnerInferOnlyRunnerc                       s   e Zd ZdZ fddZdd ZejdddZejd	d
 Z	ejdd Z
ejdd Zejdd Zejdd ZdddZ						dddZdd Zdd Z  ZS ) r   zz
    Abstract base class of Runner.

    Runner is responsible for executing training/inference/compression commands.
    c                    s    t    t|| _tj| _dS )z
        Initialize the instance.

        Args:
            runner_root_path (str): Path of the directory where the scripts reside.
        N)super__init__r   runner_root_pathsys
executablepython)selfr   	__class__ ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/repo_apis/base/runner.pyr   +   s   

zBaseRunner.__init__c                 C   s   dS )z
        Make preparations for the execution of commands.

        For example, download prerequisites and install dependencies.
        Nr   )r   r   r   r   prepare7   s    zBaseRunner.prepareTc                 C      t )a  
        Execute model training command.

        Args:
            config_path (str): Path of the configuration file.
            cli_args (list[base.utils.arg.CLIArgument]): List of command-line
                arguments.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.
            ips (str|None): Paddle cluster node ips, e.g.,
                '192.168.0.16,192.168.0.17'.
            save_dir (str): Directory to save log files.
            do_eval (bool, optional): Whether to perform model evaluation during
                training. Default: True.

        Returns:
            paddlex.repo_apis.base.utils.subprocess.CompletedProcess
        NotImplementedError)r   config_pathcli_argsdeviceipssave_dirZdo_evalr   r   r   train?   s   zBaseRunner.trainc                 C   r   )a"  
        Execute model evaluation command.

        Args:
            config_path (str): Path of the configuration file.
            cli_args (list[base.utils.arg.CLIArgument]): List of command-line
                arguments.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.
            ips (str|None): Paddle cluster node ips, e.g.,
                '192.168.0.16,192.168.0.17'.

        Returns:
            paddlex.repo_apis.base.utils.subprocess.CompletedProcess
        r   )r   r   r   r    r!   r   r   r   evaluateU   s   zBaseRunner.evaluatec                 C   r   )a  
        Execute prediction command.

        Args:
            config_path (str): Path of the configuration file.
            cli_args (list[base.utils.arg.CLIArgument]): List of command-line
                arguments.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.

        Returns:
            paddlex.repo_apis.base.utils.subprocess.CompletedProcess
        r   r   r   r   r    r   r   r   predicth      zBaseRunner.predictc                 C   r   )a  
        Execute model export command.

        Args:
            config_path (str): Path of the configuration file.
            cli_args (list[base.utils.arg.CLIArgument]): List of command-line
                arguments.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.

        Returns:
            paddlex.repo_apis.base.utils.subprocess.CompletedProcess
        r   r%   r   r   r   exporty   r'   zBaseRunner.exportc                 C   r   )a  
        Execute model inference command.

        Args:
            config_path (str): Path of the configuration file.
            cli_args (list[base.utils.arg.CLIArgument]): List of command-line
                arguments.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.

        Returns:
            paddlex.repo_apis.base.utils.subprocess.CompletedProcess
        r   r%   r   r   r   infer   r'   zBaseRunner.inferc                 C   r   )a  
        Execute model compression (quantization aware training and model export)
            commands.

        Args:
            config_path (str): Path of the configuration file.
            train_cli_args (list[base.utils.arg.CLIArgument]): List of
                command-line arguments used for model training.
            export_cli_args (list[base.utils.arg.CLIArgument]): List of
                command-line arguments used for model export.
            device (str): A string that describes the device(s) to use, e.g.,
                'cpu', 'xpu:0', 'gpu:1,2'.
            train_save_dir (str): Directory to store model snapshots.

        Returns:
            tuple[paddlex.repo_apis.base.utils.subprocess.CompletedProcess]
        r   )r   r   Ztrain_cli_argsZexport_cli_argsr    Ztrain_save_dirr   r   r   compression   s   zBaseRunner.compressionNc                 C   sd  | j g}|du r|dfS t|\}}|du st|dkr |dfS t|}ddd |D }|dkra|ddg |d	|g |durK|d
|g |du rSt }|d| |g |dfS |dkrtj	 }|dkrv||d< ||fS |dkr||d< ||fS |dkr||d< ||fS |dkr||d< ||fS |dkr||d< ||fS ||d< ||fS |dfS )distributedNr   ,c                 S   s   g | ]}t |qS r   )str).0nr   r   r   
<listcomp>   s    z*BaseRunner.distributed.<locals>.<listcomp>r	   z-mzpaddle.distributed.launchz	--devicesz--ipsz	--log_dirZxpuZXPU_VISIBLE_DEVICESZnpuZASCEND_RT_VISIBLE_DEVICESZmluZMLU_VISIBLE_DEVICESZgcuZTOPS_VISIBLE_DEVICESZ	metax_gpuZMACA_VISIBLE_DEVICESZCUDA_VISIBLE_DEVICES)
r   r   lenjoinextendosgetcwd_get_dist_train_log_direnvironcopy)r   r    r!   log_dirargsZdev_idsZnum_devicesZnew_envr   r   r   r+      sJ   
	zBaseRunner.distributedFc                    s  dd }||}t r!tddd |D  t|dt|ddS |r5t|tr1tj| j	|}	n| j	}	nd}	|s[|durBt
d	 t|||	||d
d
dd}
t|
j|
j|
j|
jd}
ndd   fdd}t }t s}t|||	||ddd
dd	}|g}|g}|durtj|}tj|dd t|ddd}tdtj| d || || zt||||}W |dur|  n
|dur|  w w t||| | }
W d   n1 sw   Y  W d   n1 sw   Y  |
jdkr
t|
j|
j|
j|
jd|
S )r   c                 S   s4   g }| D ]}t |tr||j q|| q|S N)
isinstancer
   r3   lstappend)cmdoutZeler   r   r   _trans_args   s   
z'BaseRunner.run_cmd.<locals>._trans_args c                 s   s    | ]}t |V  qd S r;   )shlexquote)r.   xr   r   r   	<genexpr>   s    z%BaseRunner.run_cmd.<locals>.<genexpr>r   N)
returncodestdoutstderrz:`log_path` will be ignored when `capture_output` is False.FT)envcwdsilentechopipe_stdoutpipe_stderrblocking)rH   rI   c           
         s   t d}g }t|d}	 d}| dI d H }|dkrd S |r&|j| || |dkr2d}n|dkr8d}|r_d|}||}|sL|| n|j	  |D ]}	|	| qS|
  q)NFbufferTr	          
   )localegetpreferredencodinghasattrreadrQ   writer>   r2   decodeflushclear)
Z	in_streamZ
out_streamfilesZ	_ENCODINGcharsZout_stream_is_bufferedr[   charlinefr   r   r   $_read_display_and_record_from_stream  s4   





z@BaseRunner.run_cmd.<locals>._read_display_and_record_from_streamc                    sH   | I d H }t  |jtj| |jtj|I d H  | I d H }|S r;   )asyncioZgatherrH   r   rI   wait)	proc_call	out_files	err_filesprocretcoderb   r   r   _tee_proc_call5  s   



	z*BaseRunner.run_cmd.<locals>._tee_proc_call)rJ   rK   rM   rL   rN   rO   rP   Z	async_run)exist_okwzutf-8)encodingz
Log path: z 
)outputrI   )r   r   infor2   r   r-   r<   r4   pathr   warning_run_cmdr:   rG   rH   rI   ioStringIOdirnamemakedirsopenr   r>   rc   runclosegetvaluer   )r   r?   rJ   Zswitch_wdirrL   rM   capture_outputlog_pathrA   rK   cprk   Z
stdout_bufZ
stderr_bufre   rf   rg   r9   Zlog_fileri   r   rj   r   r      s   	

"



  zBaseRunner.run_cmdc                 C      t j|dS )r6   Zdistributed_train_logsr4   rq   r2   r   r9   r   r   r   r6   j     z"BaseRunner._get_dist_train_log_dirc                 C   r   )_get_train_log_pathz	train.logr   r   r   r   r   r   n  r   zBaseRunner._get_train_log_path)T)NN)NTFTFN)__name__
__module____qualname____doc__r   r   abcabstractmethodr#   r$   r&   r(   r)   r*   r+   r   r6   r   __classcell__r   r   r   r   r   $   s6    





(
 )	metaclassc                   @   s8   e Zd Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r   c                 O      t | jd  dS )r#   Nr   r   r   r:   kwargsr   r   r   r#   v     zInferOnlyRunner.trainc                 O   s   t | jd dS )r$   ZevalauteNr   r   r   r   r   r$   z  r   zInferOnlyRunner.evaluatec                 O   r   )r&   Nr   r   r   r   r   r&   ~  r   zInferOnlyRunner.predictc                 O   r   )r(   Nr   r   r   r   r   r(     r   zInferOnlyRunner.exportc                 O   r   )r*   Nr   r   r   r   r   r*     r   zInferOnlyRunner.compressionN)	r   r   r   r   r#   r$   r&   r(   r*   r   r   r   r   r   s  s    )r   rc   rt   rU   r4   rC   r   utilsr   Zutils.devicer   Zutils.errorsr   r   Zutils.flagsr   Z
utils.miscr   Z	utils.argr
   Zutils.subprocessr   r   rs   __all__ABCMetar   r   r   r   r   r   <module>   s(     Q