o
    ॵis,                     @   s  d dl mZ d dlZd dlm  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 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% g dZ&G dd deZ'ej(ej)dG dd deZ*dS )    )PathN)DiffusionPipeline)Image)exif_transpose)Dataset)
transforms)Trainers)ModelOutputBase
OutputKeys)TRAINERS)CheckpointHook)CheckpointProcessor)build_optimizer)EpochBasedTrainer)
ConfigDict)ModeKeys)is_dist)za photo of a {}za rendering of a {}za cropped photo of the {}zthe photo of a {}za photo of a clean {}za photo of a dirty {}za dark photo of the {}za photo of my {}za photo of the cool {}za close-up photo of a {}za bright photo of the {}za cropped photo of a {}za photo of the {}za good photo of the {}za photo of one {}za close-up photo of the {}za rendition of the {}za photo of the clean {}za rendition of a {}za photo of a nice {}za good photo of a {}za photo of the nice {}za photo of the small {}za photo of the weird {}za photo of the large {}za photo of a cool {}za photo of a small {}c                   @   s"   e Zd Zdd Z		dddZdS )ConesCheckpointProcessorc                 C   s
   || _ d S N)	model_dir)selfr    r   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/trainers/multi_modal/cones2/cones_trainer.py__init__9   s   
z!ConesCheckpointProcessor.__init__NTc                 C   s`  d}d}t | j|j}|j|d|jjddd}	|	j}
|
d d }d}d}tD ]X}|j|	|d|jjddd}|j}t
t|d |kd d }|jj|dd	d
}|d d |||  }|j|dd	d
}|d d |||  }||7 }||7 }q)t|| tt |d  t j| j|jjd}i }|jj|jjfi ||_|| d	S )z-Save the state dict for Cones model.
        dog   
max_lengthTpt)paddingr   
truncationreturn_tensorsr   cudaN)attention_maskz/emb.pt)text_encoder)r   from_pretrainedr   todevice	tokenizermodel_max_length	input_idsPROMPT_TEMPLETEformatinttorchwheremodelr#   savelen	schedulerfrom_configconfigsave_pretrained)r   trainercheckpoint_path_prefix
output_dirmetasave_optimizersinstance_prompt	token_numpipetext_inputs_origintext_inputs_origin_idsindexprompt_embeds_newprompt_embeds_origintemplatetext_inputstext_input_idsindex_templateprompt_embeds_nowprompt_embedspipelinescheduler_argsr   r   r   save_checkpoints<   sl   





z)ConesCheckpointProcessor.save_checkpoints)NT)__name__
__module____qualname__r   rK   r   r   r   r   r   7   s
    r   )module_namec                       s8   e Zd Z fddZd
dedefddZdd	 Z  ZS )ConesDiffusionTrainerc              	      s   t  j|i | 	 |dd| _|dd| _|dd| _|dd| _|d	d
| _|dd| _|dd| _	t
tdd | jd }|t| j tj| jtjd d d}|| j ||j| jdd|jjddj| jd  | _d S )Nwith_prior_preservationFr;   r   class_promptza photo of dogclass_data_dirz/tmp/class_datanum_class_images   
resolutioni   prior_loss_weightg      ?c                 S   s
   t | tS r   )
isinstancer   )hookr   r   r   <lambda>   s   
 z0ConesDiffusionTrainer.__init__.<locals>.<lambda>r   )torch_dtypesafety_checkerrevisionTr   r   )r   r   r   r    )superr   poprQ   r;   rR   rS   rT   rV   rW   listfilterhooksset_processorr   r   r   r$   r-   float32r%   r&   r#   r'   r(   r)   detachtarget_embed)r   argskwargs	ckpt_hookrI   	__class__r   r   r   w   sP   
zConesDiffusionTrainer.__init__Ncfgdefault_argsc              
   C   sR   zt | jj ||dW S  ty( } z| jd| dtj d |d }~ww )N)rl   rm   z%Build optimizer error, the optimizer zG is a torch native component, please check if your torch with version: z matches the config.)	r   r/   r#   
parametersKeyErrorloggererrorr-   __version__)r   rl   rm   er   r   r   r      s   
z%ConesDiffusionTrainer.build_optimizerc                    s  |   d}| jj   tj| _| jj|d | j	jt
jdj }|| jjjj }| jj|d | jjjdddd}t
|j| j	}t
|}|jd	 }t
jd	| jjj|f|j	d
}	|	 }	| jj|||	}
| j|d	d	 }| jjjjdkr|}n| jjjjdkr| jj|||	}ntd| jjjj | j|
|	|j}dt
 t
| j!dd t
|dd   d }dt
 t
| j!d| d t
|d| d  d }|| }t"j#|$ |$ dd}t%j&|| i}t'|t(r|) }t'|t*s	t+dd|vrbdg}t,g }|D ] |- fdd|. D  qi }|D ]-}|/|d}|durYt0 rP|j12 d}t34|5t36  |-||7 i q-| j8-| n| j8-|d  || _9dS )aZ   Perform a training step on a batch of inputs.

        Subclass and override to inject custom behavior.

        Args:
            model (`TorchModel`): The model to train.
            inputs (`Dict[str, Union[torch.Tensor, Any]]`):
                The inputs and targets of the model.

                The dictionary will be unpacked before being fed to the model. Most models expect the targets under the
                argument `labels`. Check your model's documentation for all accepted arguments.

        Return:
            `torch.Tensor`: The tensor with training loss on this batch.
        r   target)dtypetextTr   r   )r   r   r   r    r   )r&   epsilonv_predictionzUnknown prediction type g{Gz?N   gMbP?mean)	reductionz$"model.forward()" must return a dictlog_varslossc                    s   g | ]} |v r|qS r   r   ).0keykey_pr   r   
<listcomp>  s    z4ConesDiffusionTrainer.train_step.<locals>.<listcomp>r!   ):trainr/   r#   r   TRAIN_modevaeencoder%   r&   r-   rd   latent_distsampler4   scaling_factorr'   r(   squeezer)   
randn_likeshaperandintnoise_schedulernum_train_timestepslong	add_noise	unsqueezeprediction_typeget_velocity
ValueErrorunetnormrf   Fmse_lossfloatr
   LOSSrX   r	   to_dictdict	TypeErrorsetupdatekeysgetr   dataclonedist
all_reducediv_get_world_sizeitem
log_buffertrain_outputs)r   r/   inputsr<   latentsrD   r)   noisebsz	timestepsnoisy_latentsencoder_hidden_statestarget_priormodel_pred_priorloss_embedding_headloss_embedding_tailloss_embeddingr}   r   default_keys_pattern
match_keysr|   r   valuer   r   r   
train_step   s   






z ConesDiffusionTrainer.train_stepr   )	rL   rM   rN   r   r   r   r   r   __classcell__r   r   rj   r   rP   t   s    -rP   )+pathlibr   r-   torch.nn.functionalnn
functionalr   	diffusersr   PILr   PIL.ImageOpsr   torch.utils.datar   torchvisionr   modelscope.metainfor   modelscope.outputsr	   r
   modelscope.trainers.builderr   4modelscope.trainers.hooks.checkpoint.checkpoint_hookr   9modelscope.trainers.hooks.checkpoint.checkpoint_processorr   %modelscope.trainers.optimizer.builderr   modelscope.trainers.trainerr   modelscope.utils.configr   modelscope.utils.constantr   modelscope.utils.torch_utilsr   r*   r   register_modulecones2_inferencerP   r   r   r   r   <module>   s,   =