o
    ॵi%                     @   s   d dl Z d dlZd dlmZmZ d dlmZ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mZ d	d
lmZ G dd deZejddG dd deZdS )    N)ABCabstractmethod)CallableDictOptional)check_local_model_is_latest)snapshot_download)TRAINERS)Config)Invoke
ThirdParty   )	LogBufferc                   @   s`   e Zd ZdZddedee fddZ		dddZe	d	d
 Z
e	dedeeef fddZdS )BaseTrainerz Base class for trainer which can not be instantiated.

    BaseTrainer defines necessary interface
    and provide default implementation for basic initialization
    such as parsing config file and parsing commandline args.
    Ncfg_filearg_parse_fnc                 C   sL   t || _|r| j|| _nd| _t | _t | _t	dt
 | _dS )z Trainer basic init, should be called in derived class

        Args:
            cfg_file: Path to configuration file.
            arg_parse_fn: Same as ``parse_fn`` in :obj:`Config.to_args`.
        Nz%Y%m%d_%H%M%S)r
   	from_filecfgto_argsargsr   
log_buffervisualization_buffertimestrftime	localtime	timestamp)selfr   r    r   L/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/trainers/base.py__init__   s   zBaseTrainer.__init__c                 C   sf   t j|r#t j|r|nt j|}t|tjtjt	j|id |S t
||tjtjt	j|id}|S )aH   Get local model directory or download model if necessary.

        Args:
            model (str): model id or path to local model directory.
            model_revision  (str, optional): model version number.
            third_party (str, optional): in which third party library
                this function is called.
        )
user_agent)revisionr    )ospathexistsisdirdirnamer   r   KEYLOCAL_TRAINERr   r   TRAINER)r   modelmodel_revisionthird_partymodel_cache_dirr   r   r   get_or_download_model_dir(   s*   
z%BaseTrainer.get_or_download_model_dirc                 O      dS ) Train (and evaluate) process

        Train process should be implemented for specific task or
        model, related parameters have been initialized in
        ``BaseTrainer.__init__`` and should be used in this function
        Nr   )r   r   kwargsr   r   r   trainG   s   zBaseTrainer.traincheckpoint_pathreturnc                 O   r/   ) Evaluation process

        Evaluation process should be implemented for specific task or
        model, related parameters have been initialized in
        ``BaseTrainer.__init__`` and should be used in this function
        Nr   )r   r3   r   r1   r   r   r   evaluateQ   s   	zBaseTrainer.evaluateN)NN)__name__
__module____qualname____doc__strr   r   r   r.   r   r2   r   floatr6   r   r   r   r   r      s    

	
r   dummy)module_namec                       sH   e Zd Zdef fddZdd Z	ddedeeef fd	d
Z  Z	S )DummyTrainerr   c                    s   t  | dS )zY Dummy Trainer.

        Args:
            cfg_file: Path to configuration file.
        N)superr   )r   r   r   r1   	__class__r   r   r   `   s   zDummyTrainer.__init__c                 O   s   | j j}td|  dS )r0   z
train cfg N)r   r2   print)r   r   r1   r   r   r   r   r2   h   s   zDummyTrainer.trainNr3   r4   c                 O   s(   | j j}td|  td|  dS )r5   z	eval cfg zcheckpoint_path N)r   
evaluationrD   )r   r3   r   r1   r   r   r   r   r6   r   s   
zDummyTrainer.evaluater7   )
r8   r9   r:   r<   r   r2   r   r=   r6   __classcell__r   r   rB   r   r@   ]   s    
r@   )r"   r   abcr   r   typingr   r   r   modelscope.hub.check_modelr    modelscope.hub.snapshot_downloadr   modelscope.trainers.builderr	   modelscope.utils.configr
   modelscope.utils.constantr   r   utils.log_bufferr   r   register_moduler@   r   r   r   r   <module>   s   
M