
    3j\*                     8   S SK r 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  \R&                  " \5      rS	rS
\S\S\S\S\S\\\4   4S j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)F        i0  i`  heightwidth
patch_sizemax_patchespooling_kernel_sizereturnc                 h   X-  nX2S-  -  n[         R                  " Xe-  5      nXp-  nXq-  n	XB-  n
[        [         R                  " X-  5      5      U
-  n[        [         R                  " X-  5      5      U
-  nUS:X  a  US:X  a  [	        SXB-   S35      eX4S-  -  U
-  nUS:X  a1  U
n[        [        [         R                  " X-  5      5      U
-  U5      nO6US:X  a0  U
n[        [        [         R                  " X-  5      5      U
-  U5      nX-  U:  a  [	        SU  SU SU SU SU S	U 35      eX4$ )
a  
Image is resized to preserve aspect ratio so it fits within the patch budget.
Target dimensions are the largest that:
1) Produce at most `max_patches` patches when patchified with `patch_size`
2) Have height and width divisible by `pooling_kernel_size * patch_size`
   r   zoAttempting to resize to a 0 x 0 image. Resized height should be divisble by `pooling_kernel_size * patch_size`=.z
Resizing [xz] to [z] but this exceeds z patches with patch_size )mathsqrtintfloor
ValueErrormin)r   r   r   r   r   total_px	target_pxfactorideal_heightideal_width	side_multtarget_heighttarget_widthmax_side_lengths                 l/home/wildlama/miniconda3/lib/python3.13/site-packages/transformers/models/gemma4/image_processing_gemma4.py get_aspect_ratio_preserving_sizer+   !   s{    ~H1}-IYYy+,F?L.K#0I 

<#;<=	IMtzz+"9:;iGL la/22E2R1SSTV
 	

 #1&<<	IO!

5>*+i7
 
	 

6>*+i7

 #i/%}oQ|n M  +},Ej\S
 	

 &&    imagetorch.Tensorc                     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           r*   convert_image_to_patchesr<   X   sl    
 /4kk+L%3#1MM,JcmnM!))!Q1a8M!))*<*PRTUMr,   	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   r0   constant)modevaluer2   )r3   ndimtorchnnr   pad)r-   r=   r>   current_lengthpadding_lengthpaddingpos_paddings          r*   pad_along_first_dimrK   g   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)Gemma4ImageProcessorKwargsw   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_tokensr    N)__name__
__module____qualname____firstlineno____doc__r   __annotations____static_attributes__rP   r,   r*   rM   rM   w   s     Or,   rM   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($ )$Gemma4ImageProcessor   )        r]   r]   )      ?r^   r^   NTF   r   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 r   rP   )super__init__rO   _SUPPORTED_SOFT_TOKENSr   selfrd   	__class__s     r*   ri   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_resizerP   )rh   _validate_preprocess_kwargsrk   s     r*   rq   0Gemma4ImageProcessor._validate_preprocess_kwargs   s    
 ${+5f5r,   r-   r   r   r   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$ )Nr2   )r   r   r   r   r   T)sizeinterpolation	antialias)r3   r+   tvFresize)
rl   r-   r   r   r   rs   r   r   r'   r(   s
             r*   aspect_ratio_preserving_resize3Gemma4ImageProcessor.aspect_ratio_preserving_resize   si     BR&F!# 3'
# "|'<Lzz."	
 	
r,   imagesc                 &   > [         TU ]  " U40 UD6$ )N)rh   
preprocess)rl   r}   rd   rm   s      r*   r   Gemma4ImageProcessor.preprocess   s    
 w!&3F33r,   r.   rp   z7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsrO   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$ )Nrf   rg   r   r   )r-   r   r   r   rs   ru   r2   r   )devicexy)indexing)dimr`   )datatensor_type)rj   r   r{   rescale_and_normalizer3   r<   appendr   rD   meshgridarangestackr4   rK   r   )rl   r}   rp   rs   r   r   r   r   r   r   r   rO   r   rd   r   ra   position_idsrc   r-   patch_heightpatch_widthpatchesr   
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,   rP   )NNN))rQ   rR   rS   rT   r   BICUBICrs   r   r   rv   default_to_squaredo_convert_rgbrp   r   r   r   rO   r   rM   valid_kwargsmodel_input_namesr
   ri   rq   rD   Tensorr   ry   InterpolationModer{   r   r   r   listboolfloatstrr   r   rW   __classcell__)rm   s   @r*   r[   r[      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 L	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,   r[   )r   rD   torchvision.transforms.v2r   ry   image_processing_backendsr   image_processing_utilsr   image_utilsr   r   processing_utilsr	   r
   utilsr   r   r   
get_loggerrQ   loggerrj   r   tupler+   r<   rK   rM   r[   __all__rP   r,   r*   <module>r      s	     7 ; 2 9 4 8 8 
		H	% 3 3'3'3' 3' 	3'
 3' 38_3'nN   &4EH
)* U   CDJC- JC EJCZ "
"r,   