
    j                         d dl Z d dlmZmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZ dd	lmZ dd
lmZmZ deddfdZ G d dee          ZdS )    N)ABCabstractmethod   )AttrDict)check_supported_deviceset_env_for_deviceupdate_device_num)DISABLE_CINN_MODEL_WLFLAGS_json_format_model)AutoRegisterABCMetaClass   )build_model)CINN_WHITELISTenable_cinn_backendconfigreturnBaseTrainerc                     | j         j        }	 n# t          $ r Y nw xY wt                              |          |           S )zbuild model trainer

    Args:
        config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.

    Returns:
        BaseTrainer: the trainer, which is subclass of BaseTrainer.
    )GlobalmodelModuleNotFoundErrorr   get)r   
model_names     l/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddlex/modules/base/trainer.pybuild_trainerr      sP     $J   ??:&&v...s   
c                   ~     e Zd ZdZdZdef fdZd ZddefdZ	dd	e
d
efdZed             Zed             Z xZS )r   zBase Model TrainerTr   c                 Z   t                                                       || _        |j        | _        |j        | _        |j        | _        |	                    dd          | _
        | j        	                    dd          }t          | j        j        |          \  | _        | _        dS )zInitialize the instance.

        Args:
            config (AttrDict):  PaddleX pipeline config, which is loaded from pipeline yaml file.
        	BenchmarkNbasic_config_path)config_path)super__init__r   r   global_configTraintrain_configEvaluateeval_configr   benchmark_configr   r   
pdx_config	pdx_model)selfr   r    	__class__s      r   r"   zBaseTrainer.__init__4   s     	#]"L!? &

; = ='++,?FF*5$++
 +
 +
'    c                    t          j        | j        j        d           |                                  |                                  |                                 }| j        |                    d| j        i           | j        	                    dd          pt          }|                    | j        	                    dd          || j        	                    dd          d	           t          s<| j        	                    d
d          r!| j        j        t          v rt                        | j        j        di |}|j        dk    sJ d|j         d            dS )zexecute model trainingT)exist_okN	benchmarkexport_with_pirFuniform_output_enableddist_ips)r2   r1   ipsdy2str   z Encountered an unexpected error(z) in training! )osmakedirsr#   outputupdate_configdump_configget_train_kwargsr(   updater   r   r%   r
   r   r   r   r*   train
returncode)r+   argskwargs
train_argsr1   train_results         r   r>   zBaseTrainer.trainF   s   
D&-====**,,
 ,{D,ABCCC""#4e<<W@W 	 	*.*;*?*?,d+ + $3(,,Z>> 	
 	
 	
 &	"!%%gu55	" "(N::!!!+t~+99j99#q(((l.E    )((((r-   Nconfig_file_pathc                     |*t           j                            | j        j        d          }| j                            |           dS )zdump the config

        Args:
            config_file_path (str, optional): the path to save dumped config. Defaults to None,
                means that save in `Global.output` as `config.yaml`.
        Nzconfig.yaml)r7   pathjoinr#   r9   r)   dump)r+   rD   s     r   r;   zBaseTrainer.dump_configi   sC     #!w||D,>,E}UU-.....r-   using_device_numberr   c                     t          | j        j        | j        j                   t	          | j        j                   |rt          | j        j        |          n| j        j        }|                    dd          }|S )a"  get device setting from config

        Args:
            using_device_number (int, optional): specify device number to use. Defaults to None,
                means that base on config setting.

        Returns:
            str: device setting, such as: `gpu:0,1`, `npu:0,1` `cpu`.
        dcugpu)r   r#   devicer   r   r	   replace)r+   rI   device_settings      r   
get_devicezBaseTrainer.get_devicet   s     	t18$:L:RSSS4-4555 #+d079LMMM#* 	 (//u==r-   c                     t           )zupdate training configNotImplementedErrorr+   s    r   r:   zBaseTrainer.update_config   
     "!r-   c                     t           )z2get key-value arguments of model training functionrR   rT   s    r   r<   zBaseTrainer.get_train_kwargs   rU   r-   )N)__name__
__module____qualname____doc___BaseTrainer__is_baser   r"   r>   strr;   intrP   r   r:   r<   __classcell__)r,   s   @r   r   r   /   s        I
x 
 
 
 
 
 
$! ! !F	/ 	/C 	/ 	/ 	/ 	/ c S    * " " ^" " " ^" " " " "r-   )	metaclass)r7   abcr   r   utils.configr   utils.devicer   r   r	   utils.flagsr
   r   
utils.miscr   r   utils.cinn_settingr   r   r   r   r6   r-   r   <module>rf      s,   
			 # # # # # # # # $ $ $ $ $ $         
 J I I I I I I I 2 2 2 2 2 2 $ $ $ $ $ $ C C C C C C C C/( /} / / / /"b" b" b" b" b"#!9 b" b" b" b" b" b"r-   