
    Z ji"              	          S SK r S SKJr  SSKJr  SSKJr  SSKJ	r	J
r
  SSKJrJr  SSKJrJrJr  S	S
KJrJr  \R*                  " \5      rSSS\SS4S jrSSSSS\S\S   4S jr " S S\SS9r\" SS9 " S S\5      5       rS/rg)    N)
functional   )TorchvisionBackend)BatchFeature)
ImageInputPILImageResampling)ImagesKwargsUnpack)
TensorTypeauto_docstringlogging   )_SUPPORTED_SOFT_TOKENS get_aspect_ratio_preserving_sizeimagetorch.Tensor
patch_sizereturnc                     U R                   u  p#nX1-  nXA-  nU R                  X%XU5      nUR                  SSSSS5      nUR                  XV-  S5      nU$ )z
Convert 3D tensor image of shape (num_channels, image_height, image_width) into 2D tensor of patches of shape
(num_patches_height * num_patches_width, patch_size * patch_size * num_channels).
r   r         r   )shapereshapepermute)r   r   num_channelsimage_heightimage_widthnum_patches_heightnum_patches_widthpatched_images           ڃ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/gemma4/image_processing_gemma4.pyconvert_image_to_patchesr#      sl    
 /4kk+L%3#1MM,JcmnM!))!Q1a8M!))*<*PRTUM    	positionstarget_length)r   r   c                    U R                   S   nX#-
  nUS:  aq  SS/U R                  S-
  -  SU/-   nSSSU4n[        R                  R                  R                  XSSS9n [        R                  R                  R                  XSSS9nX4$ )z+
Pad the tensor along the first dimension.
r   r   constant)modevaluer   )r   ndimtorchnnr   pad)r   r%   r&   current_lengthpadding_lengthpaddingpos_paddings          r"   pad_along_first_dimr3   .   s     [[^N"3Na&EJJN+q..AA!Q/##''Zq'QHH''++I[]+^	r$   c                   8    \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)Gemma4ImageProcessorKwargs>   a9  
patch_size (`int`, *optional*):
    Size of each image patch in pixels.
max_soft_tokens (`int`, *optional*):
    Maximum number of soft (vision) tokens per image.
    Must be one of {70, 140, 280, 560, 1120}.
pooling_kernel_size (`int`, *optional*):
    Spatial pooling kernel size applied after patchification.
r   max_soft_tokenspooling_kernel_size N)__name__
__module____qualname____firstlineno____doc__int__annotations____static_attributes__r9   r$   r"   r5   r5   >   s     Or$   r5   F)totalz$Constructs a Gemma4 image processor.)custom_introc                     ^  \ rS rSr\R
                  r/ SQr/ SQrSr	Sr
SrSrSrSrSrSrS	r\r/ S
QrS\\   4U 4S jjrU 4S jrS\R2                  S\S\S\S\R8                  S\R2                  4S jrS\S\\   S\4U 4S jjr    S#S\!S   S\"SSS\"S\#S\"S\#\!\#   -  S-  S\#\!\#   -  S-  S\$\%-  S-  S\S-  S \S-  S\S-  S\4S! jjr&S"r'U =r($ )$Gemma4ImageProcessorN   )        rG   rG   )      ?rH   rH   NTF   i  r   pixel_valuesimage_position_idsnum_soft_tokens_per_imagekwargsc                    > [         TU ]  " S0 UD6  U R                  [        ;  a   [	        S[         SU R                   S35      eg )N!`max_soft_tokens` must be one of , got .r9   )super__init__r7   r   
ValueErrorselfrN   	__class__s     r"   rT   Gemma4ImageProcessor.__init___   sN    "6"'==@AW@XX^_c_s_s^ttuvww >r$   c                 0   > SUS'   [         TU ]  " S0 UD6  g )NF	do_resizer9   )rS   _validate_preprocess_kwargsrV   s     r"   r\   0Gemma4ImageProcessor._validate_preprocess_kwargse   s    
 ${+5f5r$   r   r   max_patchesr8   resampler   c                     UR                   S   UR                   S   pv[        UUUUUS9u  pX:X  a  X:X  a  U$ [        R                  " UX/USS9$ )Nr   )heightwidthr   r^   r8   T)sizeinterpolation	antialias)r   r   Fresize)
rW   r   r   r^   r8   r_   rb   rc   target_heighttarget_widths
             r"   aspect_ratio_preserving_resize3Gemma4ImageProcessor.aspect_ratio_preserving_resizem   si     BR&F!# 3'
# "|'<Lxx."	
 	
r$   imagesc                 &   > [         TU ]  " U40 UD6$ )N)rS   
preprocess)rW   rm   rN   rX   s      r"   ro   Gemma4ImageProcessor.preprocess   s    
 w!&3F33r$   r   r[   z5PILImageResampling | F.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsr7   c           	      F   U[         ;  a  [        S[          SU S35      eXS-  -  n/ n/ n/ nU GH4  nU(       a  U R                  UU
UUUS9nU R                  UXEXgU5      nUR                  S   U
-  nUR                  S   U
-  n[        UU
5      nUR                  UR                  S   US-  -  5        UR                  n[        R                  " [        R                  " UUS	9[        R                  " UUS	9S
S9n[        R                  " USS9nUR                  UR                  S   S5      n[        UUU5      u  nnUR                  U5        UR                  U5        GM7     [        R                  " USS9n[        R                  " USS9nUUUS.n[        UU	S9$ )NrP   rQ   rR   r   )r   r   r^   r8   r_   ra   r   r   )devicexy)indexing)dimrJ   )datatensor_type)r   rU   rk   rescale_and_normalizer   r#   appendrx   r,   meshgridarangestackr   r3   r   )rW   rm   r[   r_   rq   rr   rs   rt   ru   rv   r   r7   r8   rN   r^   rK   position_idsrM   r   patch_heightpatch_widthpatchesrx   
patch_gridstacked_gridreal_positionsr%   r|   s                               r"   _preprocess Gemma4ImageProcessor._preprocess   s     "88@AW@XX^_n^oopqrr &Q(>>
 $&!E;;) +(;% <  ..ujR^luvE !;;r?j8L++b/Z7K.ujAG%,,W]]1-=ATVWAW-WX \\F[8\&9J
 !;;zr:L)11'--2BAFN "5Wnk!ZGY(	*E J {{<Q7{{<Q7 )".)B

 >BBr$   r9   )NNN))r:   r;   r<   r=   r   BICUBICr_   rt   ru   rd   default_to_squaredo_convert_rgbr[   rq   rs   r   r7   r8   r5   valid_kwargsmodel_input_namesr
   rT   r\   r,   Tensorr?   rg   InterpolationModerk   r   r   ro   listboolfloatstrr   r   rA   __classcell__)rX   s   @r"   rE   rE   N   s   !))H JIDNIJLJO-L[x(B!C x6
||
 
 	

 !
 %%
 

644 344 
	4$ "&&**.JC^$JC JC J	JC
 JC JC JC DK'$.JC 4;&-JC j(4/JC $JJC tJC !4ZJC 
JC JCr$   rE   )r,   torchvision.transforms.v2r   rg   image_processing_backendsr   image_processing_utilsr   image_utilsr   r   processing_utilsr	   r
   utilsr   r   r   image_processing_pil_gemma4r   r   
get_loggerr:   loggerr?   r#   tupler3   r5   rE   __all__r9   r$   r"   <module>r      s      5 ; 2 9 4 8 8 a 
		H	%N   &4EH
)* U   CDJC- JC EJCZ "
"r$   