o
    *jq,                     @   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   u/var/www/html/Deteccion_Ine/venv/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)Z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lenZ	schedulerfrom_configconfigZsave_pretrained)r   ZtrainerZcheckpoint_path_prefix
output_dirmetaZsave_optimizersinstance_prompt	token_numpipeZtext_inputs_originZtext_inputs_origin_idsindexZprompt_embeds_newZprompt_embeds_origintemplatetext_inputsZtext_input_idsZindex_templateZprompt_embeds_nowZprompt_embedspipelineZscheduler_argsr   r   r   save_checkpoints<   sl   





z)ConesCheckpointProcessor.save_checkpoints)NT)__name__
__module____qualname__r   r<   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_preservationFr5   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   )Ztorch_dtypeZsafety_checkerrevisionTr   r   )r   r   r   r    )superr   poprB   r5   rC   rD   rE   rG   rH   listfilterhooksZset_processorr   r   r   r#   r,   float32r$   r%   r"   r&   r'   r(   detachtarget_embed)r   argskwargsZ	ckpt_hookr;   	__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)rY   rZ   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   rY   rZ   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)ZdtypetextTr   r   )r   r   r   r    r   )r%   epsilonZv_predictionzUnknown prediction type g{Gz?N   gMbP?mean)Z	reductionz$"model.forward()" must return a dictlog_varslossc                    s   g | ]} |v r|qS r   r   ).0keyZkey_pr   r   
<listcomp>  s    z4ConesDiffusionTrainer.train_step.<locals>.<listcomp>r!   ):trainr.   r"   r   ZTRAIN_modeZvaeencoder$   r%   r,   rR   Zlatent_distsampler2   Zscaling_factorr&   r'   Zsqueezer(   Z
randn_likeshaperandintZnoise_schedulerZnum_train_timestepslongZ	add_noiseZ	unsqueezeZprediction_typeZget_velocity
ValueErrorZunetZnormrT   FZmse_lossfloatr
   ZLOSSrI   r	   to_dictdict	TypeErrorsetupdatekeysgetr   dataclonedistZ
all_reduceZdiv_Zget_world_sizeitemZ
log_buffertrain_outputs)r   r.   Zinputsr6   Zlatentsr:   r(   noiseZbszZ	timestepsZnoisy_latentsZencoder_hidden_statesZtarget_priorZmodel_pred_priorZloss_embedding_headZloss_embedding_tailZloss_embeddingrg   r   Zdefault_keys_patternZ
match_keysrf   ri   valuer   rj   r   
train_step   s   






z ConesDiffusionTrainer.train_stepr   )	r=   r>   r?   r   r   rw   r   r   __classcell__r   r   rW   r   rA   t   s    -rA   )+pathlibr   r,   Ztorch.nn.functionalnnZ
functionalrt   Z	diffusersr   ZPILr   ZPIL.ImageOpsr   Ztorch.utils.datar   Ztorchvisionr   Zmodelscope.metainfor   Zmodelscope.outputsr	   r
   Zmodelscope.trainers.builderr   Z4modelscope.trainers.hooks.checkpoint.checkpoint_hookr   Z9modelscope.trainers.hooks.checkpoint.checkpoint_processorr   Z%modelscope.trainers.optimizer.builderr   Zmodelscope.trainers.trainerr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   Zmodelscope.utils.torch_utilsr   r)   r   Zregister_moduleZcones2_inferencerA   r   r   r   r   <module>   s,   =