o
    	Ti                     @   s  d Z ddlmZmZ ddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ eG dd dZg d	Zd
d Zdd Zedkre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j"rej"ej#d dS dS dS )a  
Total Batch size = 128 = 4 (num_gpus) * 8 (per_device_batch) * 4 (accumulation steps)
Feel free to reduce batch size or increasing truncated_rand_backprop_min to a higher value to reduce memory usage.

CUDA_VISIBLE_DEVICES=0,1,2,3 python examples/scripts/alignprop.py     --num_epochs=20     --train_gradient_accumulation_steps=4     --sample_num_steps=50     --train_batch_size=8     --tracker_project_name="stable_diffusion_training"     --log_with="wandb"

    )	dataclassfieldN)HfArgumentParser)AlignPropConfigAlignPropTrainer"DefaultDDPOStableDiffusionPipeline)aesthetic_scorerc                   @   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 )ScriptArgumentsay  
    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 `"alignprop-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$alignprop-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   N/home/ubuntu/.local/lib/python3.10/site-packages/examples/scripts/alignprop.pyr	   -   s$   
 r	   )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 )N)nprandomchoiceanimalsr   r   r   r   	prompt_fns   s   r<   c           
      C   sh   i }| d | d | d g\}}}t |d d D ]\}}|| }	|d ||	 < q|j||d d S )Nimagespromptsrewards   r   )step)	enumerate	unsqueezefloat
log_images)
image_pair_dataglobal_stepaccelerate_loggerresultr=   r>   _iimagepromptr   r   r   image_outputs_loggerw   s   
rN   __main__z./logsT   z./save)logging_dirautomatic_checkpoint_namingtotal_limitproject_dir)pretrained_model_revisionr   )image_samples_hook)dataset_name)$r   dataclassesr   r   numpyr8   transformersr   trlr   r   r   trl.models.auxiliary_modulesr   r	   r;   r<   rN   r   parserparse_args_into_dataclassesscript_argstraining_argsproject_kwargsr   r   r   pipeliner   r   trainertrain
save_model
output_dirpush_to_hubrW   r   r   r   r   <module>   sJ   &