o
    H!i                     @   s   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dZG dd deed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                 C   s.   | j j}zW n	 ty   Y n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   Z
model_name r   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddlex/modules/base/trainer.pybuild_trainer   s   	r   c                       sn   e Zd ZdZdZdef fddZdd Zdd	efd
dZ	dde
defddZedd Zedd Z  ZS )r   zBase Model TrainerTr   c                    s`   t    || _|j| _|j| _|j| _|	dd| _
| j	dd}t| jj|d\| _| _dS )zInitialize the instance.

        Args:
            config (AttrDict):  PaddleX pipeline config, which is loaded from pipeline yaml file.
        Z	BenchmarkNZbasic_config_path)config_path)super__init__r   r   global_configZTraintrain_configZEvaluateZeval_configr   benchmark_configr   r   
pdx_config	pdx_model)selfr   r   	__class__r   r   r   4   s   
zBaseTrainer.__init__c                 O   s   t j| jjdd |   |   |  }| jdur"|d| ji | j	ddp*t
}|| j	dd|| j	ddd	 tsP| j	d
drP| jjtv rPt  | jjdi |}|jdksgJ d|j ddS )zexecute model trainingT)exist_okNZ	benchmarkexport_with_pirFuniform_output_enabledZdist_ips)r'   r&   ipsZdy2str   z Encountered an unexpected error(z) in training!r   )osmakedirsr   outputupdate_configdump_configget_train_kwargsr   updater   r
   r   r	   r   r   r   r!   train
returncode)r"   argskwargsZ
train_argsr&   Ztrain_resultr   r   r   r0   F   s4   
zBaseTrainer.trainNconfig_file_pathc                 C   s*   |du r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)r)   pathjoinr   r+   r    dump)r"   r4   r   r   r   r-   i   s   zBaseTrainer.dump_configusing_device_numberr   c                 C   sH   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`.
        ZdcuZgpu)r   r   Zdevicer   r   r   replace)r"   r8   Zdevice_settingr   r   r   
get_devicet   s   
zBaseTrainer.get_devicec                 C      t )zupdate training configNotImplementedErrorr"   r   r   r   r,         zBaseTrainer.update_configc                 C   r;   )z2get key-value arguments of model training functionr<   r>   r   r   r   r.      r?   zBaseTrainer.get_train_kwargs)N)__name__
__module____qualname____doc__Z_BaseTrainer__is_baser   r   r0   strr-   intr:   r   r,   r.   __classcell__r   r   r#   r   r   /   s    #
)	metaclass)r)   abcr   r   Zutils.configr   Zutils.devicer   r   r   Zutils.flagsr	   r
   Z
utils.miscr   r   Zutils.cinn_settingr   r   r   r   r   r   r   r   <module>   s   