
    h                         S SK r S SKrS SKJr  S SKrS SKJr  S SKJr  S SK	J
r
JrJr   " S S\R                  5      r " S S\R                  R                  5      rS	 rg)
    N)hf_hub_download)EntryNotFoundError)	CLIPModelis_torch_npu_availableis_torch_xpu_availablec                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )MLP   c                   > [         TU ]  5         [        R                  " [        R                  " SS5      [        R
                  " S5      [        R                  " SS5      [        R
                  " S5      [        R                  " SS5      [        R
                  " S5      [        R                  " SS5      [        R                  " SS5      5      U l        g )	Ni   i   g?   @   g?      )super__init__nn
SequentialLinearDropoutlayers)self	__class__s    V/home/james-whalen/.local/lib/python3.13/site-packages/trl/models/auxiliary_modules.pyr   MLP.__init__   s    mmIIc4 JJsOIIdC JJsOIIc2JJsOIIb"IIb!	
    c                 $    U R                  U5      $ )Nr   )r   embeds     r   forwardMLP.forward'   s    {{5!!r   r   )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__r   s   @r   r	   r	      s    
" "r   r	   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )AestheticScorer+   z
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U ]  5         [        R                  " S5      U l        [
        R                  R                  / SQ/ SQS9U l        SU l	        [        5       U l         [        X#5      n["        R$                  " U["        R&                  " S5      SS9nU R                  R)                  U5        Xl        U R-                  5         g ! [         a"    [        R                  R!                  X#5      n Nf = f)	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   rA   model_idmodel_filenamecached_path
state_dictr   s         r   r   AestheticScorer.__init__2   s    --.MN	$//994:^ : 
 5	A)(CK ZZ%,,u:M\`a
  ,
		 " 	A'',,x@K	As   )C )C<;C<c                    [        U R                  5       5      R                  n[        R                  R                  U R                  5      " U5      nU R                  U5      R                  U R                  5      R                  U5      nU R                  R                  US9nU[        R                  R                  USSS9-  nU R                  U5      R!                  S5      nU$ )N)pixel_valuesT)dimkeepdimr   )next
parametersr?   r4   r5   Resizer8   r7   torA   r3   get_image_featuresr=   linalgvector_normr9   squeeze)r   imagesr?   r   rewards        r   __call__AestheticScorer.__call__C   s    doo'(//''..t/?/?@H'**4::699&A		,,&,A00B0MM%((+r   )r3   rA   r9   r7   r8   )	r!   r"   r#   r$   __doc__r   rW   r%   r&   r'   s   @r   r)   r)   +   s    " r   r)   c                    ^ [        U U[        R                  S9m[        5       (       a  TR	                  5       mO0[        5       (       a  TR                  5       mOTR                  5       mU4S jnU$ )N)rC   rD   rA   c                 @   > U R                  SS5      n T" U 5      nU0 4$ )Nr   r   )clamp)rU   promptsmetadatascoresscorers       r   _fnaesthetic_scorer.<locals>._fn[   s&    1%rzr   )r)   r=   float32r   npur   xpucuda)hub_model_idrD   ra   r`   s      @r   aesthetic_scorerrh   N   s[    %mmF
 		!	!
 Jr   )r:   r=   torch.nnr   r4   huggingface_hubr   huggingface_hub.utilsr   transformersr   r   r   Moduler	   r)   rh    r   r   <module>ro      sI    
    + 4 R R"")) "$ ehhoo  Fr   