
    bi                         d dl Z d dlZd dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZmZ  G d dej                        Z G d dej                  j                        Zd	 Zy)
    N)hf_hub_download)EntryNotFoundError)	CLIPModelis_torch_npu_availableis_torch_xpu_availablec                   $     e Zd Z fdZd Z xZS )MLPc                    t         |           t        j                  t        j                  dd      t        j
                  d      t        j                  dd      t        j
                  d      t        j                  dd      t        j
                  d      t        j                  dd      t        j                  dd            | _        y )	Ni   i   g?   @   g?      )super__init__nn
SequentialLinearDropoutlayers)self	__class__s    W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/trl/models/auxiliary_modules.pyr   zMLP.__init__   s    mmIIc4 JJsOIIdC JJsOIIc2JJsOIIb"IIb!	
    c                 $    | j                  |      S )N)r   )r   embeds     r   forwardzMLP.forward'   s    {{5!!r   )__name__
__module____qualname__r   r   __classcell__r   s   @r   r	   r	      s    
"r   r	   c                   (     e Zd ZdZ fdZd Z x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                   t         |           t        j                  d      | _        t
        j                  j                  g dg d      | _        d| _	        t               | _        	 t        ||      }t#        j$                  |t#        j&                  d      d      }| j                  j)                  |       || _        | j-                          y # t        $ r# t        j                  j!                  ||      }Y w xY w)	Nzopenai/clip-vit-large-patch14)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?)meanstd   cpuT)map_locationweights_only)r   r   r   from_pretrainedcliptorchvision
transforms	Normalize	normalizetarget_sizer	   mlpr   r   ospathjointorchloaddeviceload_state_dictdtypeeval)r   r:   model_idmodel_filenamecached_path
state_dictr   s         r   r   zAestheticScorer.__init__2   s    --.MN	$//994:^ : 
 5	A)(NCK ZZ%,,u:M\`a
  ,
		 " 	A'',,x@K	As   +C )D Dc                    t        | j                               j                  }t        j                  j                  | j                        |      }| j                  |      j                  | j                        j                  |      }| j                  j                  |      }|t        j                  j                  |dd      z  }| j                  |      j!                  d      }|S )N)pixel_valuesT)dimkeepdimr   )next
parametersr8   r-   r.   Resizer1   r0   tor:   r,   get_image_featuresr6   linalgvector_normr2   squeeze)r   imagesr8   r   rewards        r   __call__zAestheticScorer.__call__C   s    doo'(//''..t/?/?@H'**4::699&A		,,&,A00B0MM%((+r   )r   r   r   __doc__r   rO   r    r!   s   @r   r#   r#   +   s    "r   r#   c                     t        | |t        j                        t               rj	                         n+t               rj                         nj                         fd}|S )N)r<   r=   r:   c                 @    | j                  dd      }  |       }|i fS )Nr   r   )clamp)rM   promptsmetadatascoresscorers       r   _fnzaesthetic_scorer.<locals>._fn[   s&    1%rzr   )r#   r6   float32r   npur   xpucuda)hub_model_idr=   rX   rW   s      @r   aesthetic_scorerr^   N   sU    %mmF
 		!
 Jr   )r3   r6   torch.nnr   r-   huggingface_hubr   huggingface_hub.utilsr   transformersr   r   r   Moduler	   r#   r^    r   r   <module>re      sI    
    + 4 R R"")) "$ ehhoo  Fr   