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

    Runner is responsible for executing training/inference/compression commands.
    c                     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__s     ]/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/repo_apis/base/runner.pyr   zBaseRunner.__init__+   s9     	 '(8 9 9n    c                     dS )z
        Make preparations for the execution of commands.

        For example, download prerequisites and install dependencies.
        N )r   s    r   preparezBaseRunner.prepare7   s      r   Tc                     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_dirdo_evals          r   trainzBaseRunner.train?   s
    ( "!r   c                     t           )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%   s        r   evaluatezBaseRunner.evaluateU   s
    " "!r   c                     t           )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$   s       r   predictzBaseRunner.predicth   
     "!r   c                     t           )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,   s       r   exportzBaseRunner.exporty   r.   r   c                     t           )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,   s       r   inferzBaseRunner.infer   r.   r   c                     t           )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"   train_cli_argsexport_cli_argsr$   train_save_dirs         r   compressionzBaseRunner.compression   s
    * "!r   Nc                    | j         g}||dfS t          |          \  }}|t          |          dk    r|dfS t          |          }d                    d |D                       }|dk    r|                    ddg           |                    d|g           ||                    d	|g           |t          j                    }|                    d
|                     |          g           ni|dk    rct
          j        	                                }|dk    r||d<   n5|dk    r||d<   n)|dk    r||d<   n|dk    r||d<   n|dk    r||d<   n||d<   ||fS |dfS )distributedNr   ,c                 ,    g | ]}t          |          S r   )str).0ns     r   
<listcomp>z*BaseRunner.distributed.<locals>.<listcomp>   s    8881A888r   r
   z-mzpaddle.distributed.launchz	--devicesz--ipsz	--log_dirxpuXPU_VISIBLE_DEVICESnpuASCEND_RT_VISIBLE_DEVICESmluMLU_VISIBLE_DEVICESgcuTOPS_VISIBLE_DEVICES	metax_gpuMACA_VISIBLE_DEVICESCUDA_VISIBLE_DEVICES)
r   r   lenjoinextendosgetcwd_get_dist_train_log_direnvironcopy)r   r$   r%   log_dirargsdev_idsnum_devicesnew_envs           r   r9   zBaseRunner.distributed   s    }>:&v..?c'lla//:g,,Khh8888899G??KK:;<<<KKg.///WcN+++)++KKd&B&B7&K&KLMMMMAjoo''G18-..57>344518-..529.//;&&29.//29./= Tzr   Fc                    d } ||          }t           rQt          j        d                    d |D                                  t	          |dt          |          d          S |rCt          |t
                    r&t          j                            | j	        |          }	n
| j	        }	nd}	|sV|t          j
        d           t          |||	||ddd	
          }
t	          |
j        |
j        |
j        |
j                  }
nd fd}t!          j                    5 }t!          j                    5 }t          |||	||d	d	dd		  	        }|g}|g}|t          j                            |          }t          j        |d	           t)          |dd          }t          j        dt          j                            |           d           |                    |           |                    |           	 t/          j         ||||                    }||                                 n# ||                                 w w xY wt	          |||                                |                                          }
ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |
j        dk    r't7          |
j        |
j        |
j        |
j                  |
S )r   c                     g }| D ]G}t          |t                    r|                    |j                   2|                    |           H|S N)
isinstancer   rM   lstappend)cmdouteles      r   _trans_argsz'BaseRunner.run_cmd.<locals>._trans_args   sU    C $ $c;// $JJsw''''JJsOOOOJr    c              3   >   K   | ]}t          j        |          V  d S rZ   )shlexquote)r=   xs     r   	<genexpr>z%BaseRunner.run_cmd.<locals>.<genexpr>   s*      !>!>Q%+a..!>!>!>!>!>!>r   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)ri   rj   c                 :  K   t          j        d          }g }t          |d          }	 d}|                     d           d {V }|dk    rd S |r|j                            |           |                    |           |dk    rd}n|dk    rd}|rd                    |          }|                    |          }|s|                    |           n|j        	                                 |D ]}	|	                    |           |
                                 )NFbufferTr
   r      
   )localegetpreferredencodinghasattrreadrs   writer]   rL   decodeflushclear)
	in_stream
out_streamfiles	_ENCODINGcharsout_stream_is_bufferedr|   charlinefs
             r   $_read_display_and_record_from_streamz@BaseRunner.run_cmd.<locals>._read_display_and_record_from_stream  sG     
 #7>>	)0X)F)F&&!E!*!2!2222222Ds{{- 6")//555LL&&&u}} $ !% 
&"xx#{{9555 6&,,T2222&-33555!& * *AGGDMMMM1&r   c                    K   |  d {V }t          j         |j        t          j        |           |j        t          j        |                     d {V  |                                 d {V }|S rZ   )asynciogatherri   r   rj   wait)	proc_call	out_files	err_filesprocretcoder   s        r   _tee_proc_callz*BaseRunner.run_cmd.<locals>._tee_proc_call5  s      &n88SZ  98SZ 	         !%		++++++r   )rk   rl   rn   rm   ro   rp   rq   	async_run)exist_okwzutf-8)encodingz
Log path: z 
)outputrj   )r   r   inforL   r   r<   r[   rN   pathr   warning_run_cmdrT   rh   ri   rj   ioStringIOdirnamemakedirsopenr	   r]   r   runclosegetvaluer   )r   r^   rk   switch_wdirrm   rn   capture_outputlog_pathra   rl   cpr   
stdout_buf
stderr_bufr   r   r   rS   log_filer   r   s                       @r   r   zBaseRunner.run_cmd   s   	 	 	 k# 	UL!>!>#!>!>!>>>???#CAc#hhtTTTT 	+s++ ,gll4#8+FF+C e	#P   !!	 	 	B "ry  BB &  &  &D      *bkmm z$! $ $""
 
 
	 (L	'L	' gooh77GK$7777#HcGDDDHL!N0I0I!N!N!NOOO$$X...$$X...)%k&y)YGG G  + (((  + (((( ,%*"5"5"7"79L9L9N9N 9                             @ =A$rwry    	sO   "J)6CJ>H4J4I9JJ)J	J)J	J))J-0J-c                 B    t           j                            |d          S )rP   distributed_train_logsrN   r   rL   r   rS   s     r   rP   z"BaseRunner._get_dist_train_log_dirj  s    w||G%=>>>r   c                 B    t           j                            |d          S )_get_train_log_pathz	train.logr   r   s     r   r   zBaseRunner._get_train_log_pathn  s    w||G[111r   )T)NN)NTFTFN)__name__
__module____qualname____doc__r   r   abcabstractmethodr(   r*   r-   r0   r2   r7   r9   r   rP   r   __classcell__)r   s   @r   r   r   $   sp        
% 
% 
% 
% 
%   	" " " "* 	" " "$ 	" " "  	" " "  	" " "  	" " ",# # # #P Q Q Q Qf? ? ?2 2 2 2 2 2 2r   )	metaclassc                   0    e Zd Zd Zd Zd Zd Zd Zd ZdS )r   c                 0    t          | j        d            dS )r(   Nr   r   r   rT   kwargss      r   r(   zInferOnlyRunner.trainv  s    #DNG<<<<<r   c                 0    t          | j        d           dS )r*   evalauteNr   r   s      r   r*   zInferOnlyRunner.evaluatez  s    #DNJ?????r   c                 0    t          | j        d            dS )r-   Nr   r   s      r   r-   zInferOnlyRunner.predict~  s    #DNI>>>>>r   c                 0    t          | j        d            dS )r0   Nr   r   s      r   r0   zInferOnlyRunner.export  s    #DNH=====r   c                 0    t          | j        d            dS )r7   Nr   r   s      r   r7   zInferOnlyRunner.compression  s    #DNMBBBBBr   N)	r   r   r   r   r(   r*   r-   r0   r7   r   r   r   r   r   s  sm        = = =@ @ @? ? ?> > >C C C C Cr   )r   r   r   rv   rN   rd   r   utilsr   utils.devicer   utils.errorsr   r   utils.flagsr   
utils.miscr	   	utils.argr   utils.subprocessr   r   r   __all__ABCMetar   r   r   r   r   <module>r      s`    


  				  				  



       ( ( ( ( ( ( K K K K K K K K " " " " " " ! ! ! ! ! ! " " " " " " . . . . . . 1 1 1 1 1 1*
+L2 L2 L2 L2 L23; L2 L2 L2 L2^
C C C C Cj C C C C Cr   