o
    *j'                     @   sn  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	m
Z
mZmZ d dlZd dl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
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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/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6 e&j7ej8dG dd de$Z9dS )    N)partial)ignore_patterns)CallableDictOptionalTupleUnion)distributed)nn)Dataset)model_file_download)Trainers)Model
TorchModel)	MsDataset)Preprocessor)OfaPreprocessor)
collate_fn)EpochBasedTrainer)TRAINERS)build_optimizer)is_parallel)Config)DEFAULT_MODEL_REVISION
ConfigKeysInvokeModeKeys	ModelFile   )(AdjustLabelSmoothedCrossEntropyCriterionget_schedulerecursive_overwrite)module_namec                       s   e Zd ZdZdddddddddedfdeeeej	e
f  dee
 dee dee d	eeeee
ef f  d
eeeef  deeeef  deeeee
ef f  deejjejjjf dee
 def fddZdefddZde
fddZdd Z  ZS )
OFATrainera  
    OFA trainer for MaaS.

    Args:
        model (`str`): A model dir or a model id to be loaded
        cfg_file (`str`, **optional**, default to `None`):
            A config dir
        cfg_modify_fn (`Callable`, **optional**, default to `None`):
            A function which can rebuild the config file.
        arg_parse_fn (`Callable`, **optional**, default to `None`):
            Same as ``parse_fn`` in :obj:`Config.to_args`.
        data_collator (`Callable`, **optional**, default to `None`):
            The function to use to form a batch from a list of elements
            of `train_dataset` or `eval_dataset`.
        train_dataset (:obj:`MsDataset` or :obj:`Dataset`, **optional**, default to `None`):
            Dataset for training.
        eval_dataset (:obj:`MsDataset` or :obj:`Dataset`, **optional**, default to `None`):
            Dataset for evaluation.
        preprocessor (:obj:`Preprocessor`, **optional**, default to `None`):
            The optional preprocessor.
            NOTE: If the preprocessor has been called before the dataset fed into this trainer by user's custom code,
            this parameter should be None, meanwhile remove the 'preprocessor' key from the cfg_file.
            Else the preprocessor will be instantiated from the cfg_file or assigned from this parameter and
            this preprocessing action will be executed every time the dataset's __getitem__ is called.
        model_revision (`str`, **optional**, default to `None`):
            The revision used when the model_name_or_path is
                a model id of the remote hub. default `None`.
        seed (`int`, **optional**, default to `42`):
            The optional random seed for torch, cuda, numpy and random.
    N)NN*   modelcfg_filecfg_modify_fnarg_parse_fndata_collatortrain_dataseteval_datasetpreprocessor
optimizersmodel_revisionseedc                    s  t j||
tjd}|j}|| _|dd}tj|dd t	 }|d urJ| 
|}tjtj|tj}tj|}||krDt|| |tj t||t| d tj|tj}| t|}|d ur| |}t|d}tjt||dd	 W d    n1 sw   Y  |d u rtjt|tj dd
tj!t|tj"dd
i}t#tj$dd}t%&t'||jj(j)|  }||jj* |jj+_,|j-|jj._-t/|jj.| _.|	d d u rt0||jj1d}n|	d }|	d d u rt2|jj+\}}|d ur|di d|i|}nd }n|	d }||f}	|d u rt3t4|j-j5|j-j6d}d|vr1|jdd r1|jj7|d< d|vrD|jddrD|jj8|d< d|d< t9 j:d|||||||||	|d
| d S )N)revisionZ
invoked_bywork_dirZ	workspaceT)exist_ok)ignorew   )fpindent)	model_dirmodeZ
no_collateZ
WORLD_SIZEr   r   )cfg	optimizer)Zpad_idxZeos_idxlauncheruse_fp16FZ	to_tensor)
r%   r&   r(   r'   r)   r*   r+   r,   r-   r/    );r   Zfrom_pretrainedr   ZTRAINERr8   r'   getosmakedirssetget_config_filepathabspathjoinr   CONFIGURATIONshutilcopyaddr!   r   rebuild_configr   	from_fileopenjsondumpdictr   trainr   r   ZTRAINvalZEVALintenvironmathceillenZ
dataloaderZbatch_size_per_gpuZ
max_epochslr_schedulerZnum_train_steps	tokenizer	criterionr   r   r;   r    r   r   Zpad_token_idZeos_token_idr<   r=   super__init__)selfr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   kwargsr8   r1   Zignore_file_setdstsrcr:   writerZ
world_sizeZepoch_stepsr;   Zscheduler_classZscheduler_argsrX   	__class__r>   p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/trainers/multi_modal/ofa/ofa_trainer.pyr\   D   s   


	


zOFATrainer.__init__r:   c                 C   s   | j dur
|  |}|S )zB
        rebuild config if `cfg_modify_fn` is not `None`.
        N)r'   )r]   r:   r>   r>   rd   rK      s   

zOFATrainer.rebuild_configconfig_filec                 C   sr   t j|r|S t j}t|ddkr*|dd }|dd dd }n|}t}t	|t
j||d}|S )zC
        support local file/ url or model_id with revision
        #   r   =)	file_pathr0   	cache_dir)r@   rD   existstempfileTemporaryDirectorynamerW   splitr   r   r   rG   )r]   re   Z	temp_nameZmodel_idr0   	file_namer>   r>   rd   rC      s   
zOFATrainer.get_config_filec                    s   | j st|r
|jn|}|  | ||\}}}d|i}d|vrsdg}tg }|D ] | fdd| D  q*i }	|D ],}
||
d}|durkt	
 rbt	 rb|j }t	|t	  |	|
| i q?| j|	 n| j|d  || _dS )a  
        A single training step.

        step 1. Let the model in a trainable state.
        step 2. Execute the criterion function.
        step 3. Update the logging variable's value.
        step 4. Update the training result.

        Args:
            model (:obj:`torch.nn.Module` or :obj:`TorchModel`): The model to be run.
            inputs (`dict`): model inputs.
        losslog_varsc                    s   g | ]} |v r|qS r>   r>   ).0keyZkey_pr>   rd   
<listcomp>   s    z)OFATrainer.train_step.<locals>.<listcomp>N)_distr   modulerQ   rZ   rB   updatekeysr?   distZis_availableZis_initializeddatacloneZ
all_reduceZdiv_Zget_world_sizeitemZ
log_buffertrain_outputs)r]   r%   Zinputsrr   Zsample_sizeZlogging_outputr   Zdefault_keys_patternZ
match_keysrs   ru   valuer>   rv   rd   
train_step   s.   

zOFATrainer.train_step)__name__
__module____qualname____doc__r   r   r   r   r
   Modulestrr   r   r   r   r   r   torchZoptimZ	OptimizerrX   Z_LRSchedulerrS   r\   r   rK   rC   r   __classcell__r>   r>   rb   rd   r#   #   sZ    !

	

dr#   ):rU   r@   rH   rm   	functoolsr   r   typingr   r   r   r   r   rN   r   r	   r|   r
   Ztorch.utils.datar   Zmodelscope.hub.file_downloadr   Zmodelscope.metainfor   Zmodelscope.models.baser   r   Z modelscope.msdatasets.ms_datasetr   Zmodelscope.preprocessors.baser   Z$modelscope.preprocessors.multi_modalr   Z*modelscope.preprocessors.ofa.utils.collater   Zmodelscope.trainersr   Zmodelscope.trainers.builderr   Z%modelscope.trainers.optimizer.builderr   Z"modelscope.trainers.parallel.utilsr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   r   r   r   Zofa_trainer_utilsr   r    r!   Zregister_moduleZofar#   r>   r>   r>   rd   <module>   s8   