o
    ॵij                     @   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__ g/home/ubuntu/.local/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   cfg_dict)r   from_pretrained	model_dirr   cfgr   r   r   r   build_model   s   zMPlugTrainer.build_modelc                 C   s   | j j}|j|ddS )NT)skip_special_tokens)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-   answer_input_idsr   )_distmodelmoduleevaltorchno_grad
isinstancer   r   forwardr	   TEXTLABELS)r   datar1   resultr   r   r   evaluation_step    s(   



zMPlugTrainer.evaluation_step)__name__
__module____qualname__r   r   r   Moduler   r    r&   r<   __classcell__r   r   r   r   r      s
    r   )collections.abcr   typingr   r   r4   r   modelscope.metainfor   modelscope.modelsr   r   modelscope.outputsr	   modelscope.trainersr
   modelscope.trainers.builderr   modelscope.utils.file_utilsr   register_modulemplugr   r   r   r   r   <module>   s   