o
    	Ti                     @   sl  d Z ddlZddl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mZmZmZmZ ddlmZmZmZ eG dd	 d	ZG d
d de	jZG dd dej	jZdd Zg dZdd Zdd Ze dkreeefZ!e!" \Z#Z$ddddde$_%ee#j&e#j'e#j(dZ)ee$ee#j*e#j+ee)edZ,e,-  e,.e$j/ e$j0re,j0e#j1d dS dS dS )ar  
python examples/scripts/ddpo.py     --num_epochs=200     --train_gradient_accumulation_steps=1     --sample_num_steps=50     --sample_batch_size=6     --train_batch_size=3     --sample_num_batches_per_epoch=4     --per_prompt_stat_tracking=True     --per_prompt_stat_tracking_buffer_size=32     --tracker_project_name="stable_diffusion_training"     --log_with="wandb"
    N)	dataclassfield)hf_hub_download)EntryNotFoundError)	CLIPModelCLIPProcessorHfArgumentParseris_torch_npu_availableis_torch_xpu_available)
DDPOConfigDDPOTrainer"DefaultDDPOStableDiffusionPipelinec                   @   s   e Zd ZU dZedddidZeed< edddidZeed	< ed
ddidZ	eed< edddidZ
eed< edddidZeed< edddidZeed< dS )ScriptArgumentsat  
    Arguments for the script.

    Args:
        pretrained_model (`str`, *optional*, defaults to `"runwayml/stable-diffusion-v1-5"`):
            Pretrained model to use.
        pretrained_revision (`str`, *optional*, defaults to `"main"`):
            Pretrained model revision to use.
        hf_hub_model_id (`str`, *optional*, defaults to `"ddpo-finetuned-stable-diffusion"`):
            HuggingFace repo to save model weights to.
        hf_hub_aesthetic_model_id (`str`, *optional*, defaults to `"trl-lib/ddpo-aesthetic-predictor"`):
            Hugging Face model ID for aesthetic scorer model weights.
        hf_hub_aesthetic_model_filename (`str`, *optional*, defaults to `"aesthetic-model.pth"`):
            Hugging Face model filename for aesthetic scorer model weights.
        use_lora (`bool`, *optional*, defaults to `True`):
            Whether to use LoRA.
    zrunwayml/stable-diffusion-v1-5helpzPretrained model to use.)defaultmetadatapretrained_modelmainz!Pretrained model revision to use.pretrained_revisionzddpo-finetuned-stable-diffusionz*HuggingFace repo to save model weights to.hf_hub_model_idz trl-lib/ddpo-aesthetic-predictorz9Hugging Face model ID for aesthetic scorer model weights.hf_hub_aesthetic_model_idzaesthetic-model.pthz?Hugging Face model filename for aesthetic scorer model weights.hf_hub_aesthetic_model_filenameTzWhether to use LoRA.use_loraN)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   r   r   bool r    r    I/home/ubuntu/.local/lib/python3.10/site-packages/examples/scripts/ddpo.pyr   0   s$   
 r   c                       s,   e Zd Z fddZe dd Z  ZS )MLPc                    sb   t    ttddtdtddtdtddtdtddtdd| _d S )	Ni   i   g?   @   g?      )super__init__nn
SequentialLinearDropoutlayers)self	__class__r    r!   r(   W   s   






zMLP.__init__c                 C   s
   |  |S N)r-   )r.   embedr    r    r!   forwardd   s   
zMLP.forward)r   r   r   r(   torchno_gradr3   __classcell__r    r    r/   r!   r"   V   s    r"   c                       s0   e Zd ZdZ fddZe dd Z  ZS )AestheticScorera  
    This model attempts to predict the aesthetic score of an image. The aesthetic score
    is a numerical approximation of how much a specific image is liked by humans on average.
    This is from https://github.com/christophschuhmann/improved-aesthetic-predictor
    c                   s   t    td| _td| _t | _zt	||}W n t
y,   tj||}Y nw tj|tddd}| j| || _|   d S )Nzopenai/clip-vit-large-patch14cpuT)map_locationweights_only)r'   r(   r   from_pretrainedclipr   	processorr"   mlpr   r   ospathjoinr4   loaddeviceload_state_dictdtypeeval)r.   rE   model_idmodel_filenamecached_path
state_dictr/   r    r!   r(   p   s   
zAestheticScorer.__init__c                    sl   t  j j|dd} fdd| D }jjd	i |}|tjj	|ddd }
|dS )
Npt)imagesreturn_tensorsc                    s$   i | ]\}}|| j  qS r    )torE   ).0kvrC   r.   r    r!   
<dictcomp>   s   $ z,AestheticScorer.__call__.<locals>.<dictcomp>T)dimkeepdimr&   r    )next
parametersrC   r=   itemsr<   get_image_featuresr4   linalgvector_normr>   squeeze)r.   rL   inputsr2   r    rR   r!   __call__~   s   zAestheticScorer.__call__)	r   r   r   r   r(   r4   r5   r_   r6   r    r    r/   r!   r7   i   s
    r7   c                    sH   t | |tjd t r   nt r   n    fdd}|S )N)rG   rH   rE   c                    s,   | d   ddtj}  | }|i fS )N   r   )roundclamprN   r4   uint8)rL   promptsr   scoresscorerr    r!   _fn   s   zaesthetic_scorer.<locals>._fn)r7   r4   float32r	   npur
   xpucuda)hub_model_idrH   rh   r    rf   r!   aesthetic_scorer   s   

rn   )catdoghorsemonkeyrabbitzebraspiderbirdsheepdeercowgoatlionfrogchickenduckgoosebeepigturkeyflyllamacamelbatgorillahedgehogkangarooc                   C   s   t jti fS r1   )nprandomchoiceanimalsr    r    r    r!   	prompt_fn   s   r   c                 C   sn   i }| d \}}}}}t |D ]\}}	|| }
||  }|	d ||
dd|d< q|j||d d S )NrT   r   z.25z | z.2f)step)	enumerateitem	unsqueezefloat
log_images)
image_dataglobal_stepaccelerate_loggerresultrL   rd   _rewardsiimagepromptrewardr    r    r!   image_outputs_logger   s   "
r   __main__z./logsT   z./save)logging_dirautomatic_checkpoint_namingtotal_limitproject_dir)pretrained_model_revisionr   )image_samples_hook)dataset_name)2r   r?   dataclassesr   r   numpyr   r4   torch.nnr)   huggingface_hubr   huggingface_hub.utilsr   transformersr   r   r   r	   r
   trlr   r   r   r   Moduler"   r7   rn   r   r   r   r   parserparse_args_into_dataclassesscript_argstraining_argsproject_kwargsr   r   r   pipeliner   r   trainertrain
save_model
output_dirpush_to_hubr   r    r    r    r!   <module>   sX   % 