
    9i6                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrSSKJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SS/r " S S\ R2                  S9r " S S\5      rg)    N   )logging)parse_device)CalledProcessErrorraise_unsupported_api_error)DRY_RUN)abspath   )CLIArgument)CompletedProcess)run_cmd
BaseRunnerInferOnlyRunnerc                   :  ^  \ rS rSrSrU 4S jrS r\R                  SS j5       r	\R                  S 5       r
\R                  S 5       r\R                  S 5       r\R                  S	 5       r\R                  S
 5       rSS jr      SS jrS rS rSrU =r$ )r   $   zn
Abstract base class of Runner.

Runner is responsible for executing training/inference/compression commands.
c                 l   > [         TU ]  5         [        U5      U l        [        R
                  U l        g)zm
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/land-doc-ocr/venv/lib/python3.13/site-packages/paddlex/repo_apis/base/runner.pyr   BaseRunner.__init__+   s)     	 '(8 9nn    c                     g)zq
Make preparations for the execution of commands.

For example, download prerequisites and install dependencies.
N )r   s    r   prepareBaseRunner.prepare7   s    r   c                     [         e)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   trainBaseRunner.train?   s
    ( "!r   c                     [         e)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   evaluateBaseRunner.evaluateU   s
    " "!r   c                     [         e)ad  
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   predictBaseRunner.predicth   
     "!r   c                     [         e)af  
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#   r1   s       r   exportBaseRunner.exporty   r4   r   c                     [         e)ai  
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#   r1   s       r   inferBaseRunner.infer   r4   r   c                     [         e)ax  
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   compressionBaseRunner.compression   s
    * "!r   c                    U R                   /nUc  US4$ [        U5      u  pUb  [        U5      S:X  a  US4$ [        U5      nSR                  U Vs/ s H  n[	        U5      PM     sn5      nUS:  az  UR                  SS/5        UR                  SU/5        Ub  UR                  SU/5        Uc  [        R                  " 5       nUR                  S	U R                  U5      /5        US4$ US:X  af  [        R                  R                  5       nUS
:X  a  XXS'   XH4$ US:X  a  XXS'   XH4$ US:X  a  XXS'   XH4$ US:X  a  XXS'   XH4$ US:X  a  XXS'   XH4$ XXS'   XH4$ US4$ s  snf )distributedNr   ,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strextendosgetcwd_get_dist_train_log_direnvironcopy)	r   r'   r(   log_dirargsdev_idsnum_devicesnnew_envs	            r   rB   BaseRunner.distributed   s    }>:&v.?c'la/:g,Khh81A89G?KK:;<KKg./WcN+))+KKd&B&B7&KLM  Tz Ajjoo'G18-. =  57>34 =  518-. =  529./
 = 	 ;&29./ =  3:./= Tz1  9s   Ec                   ^ S nU" U5      n[         (       aA  [        R                  " SR                  S U 5       5      5        [	        US[        U5      SS9$ U(       aM  [        U[
        5      (       a+  [        R                  R                  U R                  U5      n	OU R                  n	OSn	U(       d_  Ub  [        R                  " S5        [        UUU	UUSSS	S
9n
[	        U
R                  U
R                  U
R                  U
R                  S9n
GORS mU4S jn[         R"                  " 5        n[         R"                  " 5        n[        UUU	UUS	S	SS	S9	nU/nU/nUb  [        R                  R%                  U5      n[        R&                  " US	S9  [)        USSS9n[        R                  " S[        R                  R+                  U5       S35        UR-                  U5        UR-                  U5         [.        R0                  " U" XU5      5      nUb  WR3                  5         [	        UUUR5                  5       UR5                  5       5      n
SSS5        SSS5        W
R                  S:w  a4  [7        U
R                  U
R                  U
R                  U
R                  S9eU
$ ! Ub  WR3                  5         f f = f! , (       d  f       Nu= f! , (       d  f       N~= f)r   c                     / nU  HF  n[        U[        5      (       a  UR                  UR                  5        M5  UR	                  U5        MH     U$ N)
isinstancer   rR   lstappend)cmdouteles      r   _trans_args'BaseRunner.run_cmd.<locals>._trans_args   s@    Cc;//JJsww'JJsO	 
 Jr    c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fra   )shlexquote).0xs     r   	<genexpr>%BaseRunner.run_cmd.<locals>.<genexpr>   s     !>#Q%++a..#s   #%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)rs   rt   c                 4  #    [         R                  " S5      n/ n[        US5      n SnU R                  S5      I S h  vN nUS:X  a  g U(       a  UR                  R                  U5        UR                  U5        US:X  a  SnOUS:X  a  SnU(       a  SR                  U5      nUR                  U5      nU(       d  UR                  U5        OUR                  R                  5         U H  n	U	R                  U5        M     UR                  5         M   N7f)NFbufferTr
   r      
   )localegetpreferredencodinghasattrreadr}   writerd   rP   decodeflushclear)
	in_stream
out_streamfiles	_ENCODINGcharsout_stream_is_bufferedr   charlinefs
             r   $_read_display_and_record_from_stream@BaseRunner.run_cmd.<locals>._read_display_and_record_from_stream  s     
 #77>	)0X)F&!E!*!22Ds{-"))//5LL&u} $ !%"xx#{{955&,,T2&--335!&AGGDM "'1 2s   <DDCDc                   >#    U I S h  vN n[         R                  " T" UR                  [        R                  U5      T" UR                  [        R                  U5      5      I S h  vN   UR                  5       I S h  vN nU$  N} N  N
7fra   )asynciogatherrs   r   rt   wait)	proc_call	out_files	err_filesprocretcoder   s        r   _tee_proc_call*BaseRunner.run_cmd.<locals>._tee_proc_call5  sw     &nn8SZZ 9SZZ	   !%		+ ' ,s4   BBAB(B	)B BB	BB)ru   rv   rx   rw   ry   rz   r{   	async_run)exist_okwzutf-8)encodingz
Log path: z 
)outputrt   )r   r   inforP   r   rQ   rb   rS   pathr   warning_run_cmdrY   rr   rs   rt   ioStringIOdirnamemakedirsopenr	   rd   r   runclosegetvaluer   )r   re   ru   switch_wdirrw   rx   capture_outputlog_pathrh   rv   cpr   
stdout_buf
stderr_bufr   r   r   rX   log_filer   r   s                       @r   r   BaseRunner.run_cmd   sk   	 #7LL!>#!>>?#CAc#htTT+s++ggll4#8#8+F++C#P !!	B "ryyB &D *bkkmz$! $ $""
	 (L	'L	' ggooh7GKK$7#HcGDHLL<0I/J#!NO$$X.$$X.)%kk&yYGG  + (%*"5"5"79L9L9N9 /<@ ==A$rwwryy  	  + ( ,5 /<ms=   3K	B3K=J-=KK-KK
K	K
K%c                 B    [         R                  R                  US5      $ )rU   distributed_train_logsrS   r   rP   r   rX   s     r   rU   "BaseRunner._get_dist_train_log_dirj  s    ww||G%=>>r   c                 B    [         R                  R                  US5      $ )_get_train_log_pathz	train.logr   r   s     r   r   BaseRunner._get_train_log_pathn  s    ww||G[11r   )r   r   )T)NN)NTFTFN)__name__
__module____qualname____firstlineno____doc__r   r    abcabstractmethodr+   r.   r2   r6   r9   r?   rB   r   rU   r   __static_attributes____classcell__)r   s   @r   r   r   $   s    
% 	" "* 	" "$ 	" "  	" "  	" "  	" ",#P Qf?2 2r   )	metaclassc                   6    \ rS rSrS rS rS rS rS rS r	Sr
g)	r   is  c                 0    [        U R                  S 5        g)r+   Nr   r   r   rY   kwargss      r   r+   InferOnlyRunner.trainv  s    #DNNG<r   c                 0    [        U R                  S5        g)r.   evalauteNr   r   s      r   r.   InferOnlyRunner.evaluatez  s    #DNNJ?r   c                 0    [        U R                  S 5        g)r2   Nr   r   s      r   r2   InferOnlyRunner.predict~  s    #DNNI>r   c                 0    [        U R                  S 5        g)r6   Nr   r   s      r   r6   InferOnlyRunner.export  s    #DNNH=r   c                 0    [        U R                  S 5        g)r?   Nr   r   s      r   r?   InferOnlyRunner.compression  s    #DNNMBr   r   N)r   r   r   r   r   r+   r.   r2   r6   r?   r   r   r   r   r   r   s  s     =@?>Cr   )r   r   r   r   rS   rl   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 " ! " . 1*
+L23;; L2^
Cj Cr   