o
    *jj                     @   s   d dl mZ d dlmZmZ d dl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 ejejdG dd deZdS )    )Mapping)OptionalUnionN)nn)Trainers)Model
TorchModel)
OutputKeys)EpochBasedTrainer)TRAINERS)func_receive_dict_inputs)module_namec                       sD   e Zd Z fddZdeejef fddZdd Z	dd	 Z
  ZS )
MPlugTrainerc                    s$   | dd | _t j|i | d S )Ntask)popr   super__init__)selfargskwargs	__class__ t/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/trainers/multi_modal/mplug/mplug_trainer.pyr      s   zMPlugTrainer.__init__returnc                 C   s   t j| j| j| jdS )N)r   Zcfg_dict)r   Zfrom_pretrainedZ	model_dirr   cfgr   r   r   r   build_model   s   zMPlugTrainer.build_modelc                 C   s   | j j}|j|ddS )NT)Zskip_special_tokens)Zeval_preprocessor	tokenizerdecode)r   tokensr   r   r   r   _decode   s   zMPlugTrainer._decodec                    s    j r jjn j}|  t   t|tr&t|j	s&|j	di |}n|	|}W d    n1 s5w   Y   fdd|d D |t
j<  fdd|d D |t
j< |S )Nc                       g | ]}  |qS r   r!   .0seqr   r   r   
<listcomp>,       
z0MPlugTrainer.evaluation_step.<locals>.<listcomp>	sequencesc                    r"   r   r#   r$   r   r   r   r'   /   r(   Zanswer_input_idsr   )_distmodelmoduleevaltorchZno_grad
isinstancer   r   forwardr	   ZTEXTZLABELS)r   datar+   resultr   r   r   evaluation_step    s(   



zMPlugTrainer.evaluation_step)__name__
__module____qualname__r   r   r   Moduler   r   r!   r3   __classcell__r   r   r   r   r      s
    r   )collections.abcr   typingr   r   r.   r   Zmodelscope.metainfor   Zmodelscope.modelsr   r   Zmodelscope.outputsr	   Zmodelscope.trainersr
   Zmodelscope.trainers.builderr   Zmodelscope.utils.file_utilsr   Zregister_moduleZmplugr   r   r   r   r   <module>   s   