ó
    qyüiƒ&  ã                   óp  • S r SSKrSSK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JrJr  SS	KJrJr  SS
KJrJr  S\\\4   S\\\4   S\4S jr SS\S\\\\4   -  \\   -  \\   -  S\\\\4   -  \\   -  \\   -  S\\-  S-  S\4
S jjr " S S\SS9r \ " S S\5      5       r!S/r"g)z"Image processor class for Pixtral.é    N)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeatureÚget_size_dict)Úgroup_images_by_shapeÚreorder_images)ÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDictÚget_image_size)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚ
image_sizeÚ
patch_sizeÚreturnc                 ó€   • U u  p#[        U[        [        45      (       a  UOX4u  pEUS-
  U-  S-   nUS-
  U-  S-   nXv4$ )a,  
Calculate the number of image tokens given the image size and patch size.

Args:
    image_size (`tuple[int, int]`):
        The size of the image as `(height, width)`.
    patch_size (`tuple[int, int]`):
        The patch size as `(height, width)`.

Returns:
    `int`: The number of image tokens.
é   )Ú
isinstanceÚtupleÚlist)r   r   ÚheightÚwidthÚpatch_heightÚpatch_widthÚnum_width_tokensÚnum_height_tokenss           Úq/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/pixtral/image_processing_pixtral.pyÚ_num_image_tokensr"      sZ   € ð �M€FÜ.8¸ÄeÌTÀ]×.SÑ.S¡
ÐZdÐYqÑ€LØ ™	 kÑ1°AÑ5ÐØ !™¨Ñ4°qÑ8ÐØÐ.Ð.ó    Úinput_imageÚsizeÚinput_data_formatc                 ó„  • [        U[        [        45      (       a  UOX4u  pE[        U[        [        45      (       a  UOX"4u  pg[        X5      u  p‰[	        X„-  X•-  5      n
U
S:”  aB  [        [        R                  " XŠ-  5      5      n[        [        R                  " Xš-  5      5      n	[        X‰4Xg45      u  p¼X¶-  XÇ-  4$ )a  
Find the target (height, width) dimension of the output image after resizing given the input image and the desired
size.

Args:
    input_image (`ImageInput`):
        The image to resize.
    size (`int` or `tuple[int, int]`):
        Max image size an input image can be. Must be a dictionary with the key "longest_edge".
    patch_size (`int` or `tuple[int, int]`):
        The patch_size as `(height, width)` to use for resizing the image. If patch_size is an integer, `(patch_size, patch_size)`
        will be used
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If unset, will use the inferred format from the input.

Returns:
    `tuple`: The target (height, width) dimension of the output image after resizing.
r   )	r   r   r   r   ÚmaxÚintÚmathÚfloorr"   )r$   r%   r   r&   Ú
max_heightÚ	max_widthr   r   r   r   Úratior    r   s                r!   Úget_resize_output_image_sizer/   1   s¶   € ô0 %/¨t´e¼T°]×$CÑ$C™DÈ$ÈÑ€JÜ.8¸ÄeÌTÀ]×.SÑ.S¡
ÐZdÐYqÑ€LÜ" ;ÓB�M€Fä�Ñ# UÑ%6Ó7€Eàˆqƒyô ”T—Z’Z ¡Ó/Ó0ˆÜ”D—J’J˜u™}Ó-Ó.ˆä*;¸V¸OÈlÐMhÓ*iÑ'ÐØÑ+Ð-=Ñ-KÐKÐKr#   c                   ó4   • \ rS rSr% Sr\\\4   \-  \S'   Sr	g)ÚPixtralImageProcessorKwargséY   z±
patch_size (`Union[dict[str, int], int]` *optional*, defaults to `{"height": 16, "width": 16}`):
    Size of the patches in the model, used to calculate the output image size.
r   © N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚdictÚstrr)   Ú__annotations__Ú__static_attributes__r3   r#   r!   r1   r1   Y   s   ‡ ñð
 �S˜#�X‘ Ñ$Ö$r#   r1   F)Útotalc                   ó°  ^ • \ rS rSr\R
                  r/ SQr/ SQrSSS.r	SS0r
SrSrSrSrSr\rS	S
/rS\\   4U 4S jjr\S\S\\   S\4U 4S jj5       r S%SSS\S\SSSS4
U 4S jjjrS	\S   S
\\\\4      SS4S jr S%S\S   S\S\SSS\S\S\S\ S\S\ \\    -  S-  S \ \\    -  S-  S!\S-  S"\!\"-  S-  S\#\!\4   \-  S-  S\4S# jjr$S$r%U =r&$ )&ÚPixtralImageProcessoréb   )g3<Í4'ÐÞ?gwgí¶MÝ?gy{Îå Ú?)g�‡Bô91Ñ?g•wÝt.¹Ð?g�Ý	U¦Ñ?é   ©r   r   Úlongest_edgei   TÚpixel_valuesÚimage_sizesÚkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr3   )ÚsuperÚ__init__)ÚselfrF   Ú	__class__s     €r!   rI   ÚPixtralImageProcessor.__init__r   s   ø€ Ü‰ÒÑ"˜6Ó"r#   Úimagesr   c                 ó&   >• [         TU ]  " U40 UD6$ ©N)rH   Ú
preprocess)rJ   rM   rF   rK   s      €r!   rP   Ú PixtralImageProcessor.preprocessu   s   ø€ ä‰wÒ! &Ñ3¨FÑ3Ð3r#   NÚimageztorch.Tensorr%   r   Úresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 óÈ  >• UR                   (       a  UR                   UR                   4nOFUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        S5      eUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        S5      e[	        XUS9n[
        T	U ]  " U4[        US   US   S9US.UD6$ )a%  
Resize an image. The longest edge of the image is resized to size["longest_edge"], with the aspect ratio
preserved. Output dimensions are aligned to patch_size.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Dict containing the longest possible edge of the image.
    patch_size (`SizeDict`):
        Patch size used to calculate the size of the output image.
    resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*):
        Resampling filter to use when resizing the image.
z@size must contain either 'longest_edge' or 'height' and 'width'.z-patch_size must contain 'height' and 'width'.)r%   r   r   r   rB   )r%   rS   )rC   r   r   Ú
ValueErrorr/   rH   Úresizer   )
rJ   rR   r%   r   rS   rF   Ú
size_tupleÚpatch_size_tupleÚoutput_sizerK   s
            €r!   rV   ÚPixtralImageProcessor.resizey   sÆ   ø€ ð, ××Ø×+Ñ+¨T×->Ñ->Ð?‰JØ�[�[˜TŸZŸZØŸ+™+ t§z¡zÐ2‰JäÐ_Ó`Ð`à×× ×!1×!1Ø *× 1Ñ 1°:×3CÑ3CÐDÑäÐLÓMÐMä2°5ÐVfÑgˆÜ‰wŠ~Øð
Ü ¨°A©¸kÈ!¹nÑMÐX`ñ
Ødjñ
ð 	
r#   c                 ó8  • [        S U 5       5      [        S U 5       5      4n[        X5       VVs/ s HC  u  pE[        R                  R                  R                  USUS   US   -
  SUS   US   -
  4S9PME     nnn[        R                  " U5      $ s  snnf )a;  
Pads images to form a batch of same shape.

Args:
    pixel_values (`list[torch.Tensor]`):
        A list of pixel values, each of shape (channels, height, width).
    image_sizes (`list[tuple[int, int]]`):
        A list of (height, width) for each image.

Returns:
    `torch.Tensor`: Stacked and padded images.
c              3   ó*   #   • U  H	  oS    v •  M     g7f)r   Nr3   ©Ú.0Úss     r!   Ú	<genexpr>Ú:PixtralImageProcessor._pad_for_batching.<locals>.<genexpr>±   s   é € Ð3¢{ !˜1ž¢{ùó   ‚c              3   ó*   #   • U  H	  oS    v •  M     g7f)r   Nr3   r]   s     r!   r`   ra   ±   s   é € Ð8SÂ{À!¸1¾Â{ùrb   r   r   )Úpad)r(   ÚzipÚtorchÚnnr   rd   Ústack)rJ   rD   rE   Ú	max_shapeÚimgr%   Úpaddeds          r!   Ú_pad_for_batchingÚ'PixtralImageProcessor._pad_for_batching    s¤   € ô" Ñ3¡{Ó3Ó3´SÑ8SÁ{Ó8SÓ5SÐTˆ	ô ! Ô;ô
â;‘	�ô �H‰H×Ñ×#Ñ# C¨a°¸1±ÀÀQÁÑ1GÈÈIÐVWÉLÐ[_Ð`aÑ[bÑLbÐ-cÐ#ÓdÙ;ð 	ñ 
ô �{Š{˜6Ó"Ð"ùó	
s   ³A
BÚ	do_resizeÚdo_center_cropÚ	crop_sizeÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdisable_groupingÚreturn_tensorsc           	      ó\  • [        U=(       d    U R                  SS9n[        S0 UD6n[        XS9u  nn0 nUR	                  5        H$  u  nnU(       a  U R                  UUUUS9nUUU'   M&     [        UU5      n[        UUS9u  nn[        [        U5      5       Vs/ s H  nUU   S   PM     nn0 nUR	                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXšU5      nUUU'   M:     [        UU5      nU R                  UUS9n[        UUS.US9$ s  snf )	NT)Údefault_to_square)rv   )rR   r%   r   rS   r   )rD   rE   )ÚdataÚtensor_typer3   )r   r   r   r   ÚitemsrV   r	   ÚrangeÚlenÚcenter_cropÚrescale_and_normalizerl   r   )rJ   rM   rn   r%   rS   ro   rp   rq   rr   rs   rt   ru   rv   rw   r   rF   Úpatch_size_sdÚgrouped_imagesÚgrouped_images_indexÚresized_images_groupedÚshapeÚstacked_imagesÚresized_imagesÚiÚbatch_image_sizesÚprocessed_images_groupedÚprocessed_imagesÚpadded_imagess                               r!   Ú_preprocessÚ!PixtralImageProcessor._preprocess¸   sy  € ô$ # :×#@°·±ÐTXÑYˆ
Ü Ñ. :Ñ.ˆä/DÀVÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡Ø(¨tÀÐX`ð "-ð "�ð -;Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆä/DÀ^ÐfvÑ/wÑ,ˆÐ,ÜAFÄsÐK_ÓG`ÔAaÓbÒAa¸AÐ1°!Ñ4°QÔ7ÑAaÐÐbà#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�Ø!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐØ×.Ñ.Ø)Ø)ð /ð 
ˆô
 Ø"/Ð@QÑRÐ`nñ
ð 	
ùò# cs   ÂD)r3   rO   )'r4   r5   r6   r7   r   ÚBICUBICrS   rt   ru   r   r%   ry   rn   rq   rs   Údo_convert_rgbr1   Úvalid_kwargsÚmodel_input_namesr   rI   r   r   r   rP   r   rV   r   r   r)   rl   ÚboolÚfloatr:   r   r9   r�   r<   Ú__classcell__)rK   s   @r!   r?   r?   b   sô  ø† à!×)Ñ)€HÚ4€JÚ4€IØ¨Ñ,€JØ˜DÐ!€DØÐØ€IØ€JØ€LØ€NØ.€Là'¨Ð7Ðð# Ð(CÑ!D÷ #ð ð4 ð 4°vÐ>YÑ7Zð 4Ð_kö 4ó ð4ð OSñ%
àð%
ð ð%
ð ð	%
ð
 Lð%
ð 
÷%
ð %
ðN#à˜>Ñ*ð#ð ˜%  S ™/Ñ*ð#ð 
ô	#ðN 8<ñ3
à�^Ñ$ð3
ð ð3
ð ð	3
ð
 Lð3
ð ð3
ð ð3
ð ð3
ð ð3
ð ð3
ð ˜D ™KÑ'¨$Ñ.ð3
ð ˜4 ™;Ñ&¨Ñ-ð3
ð  ™+ð3
ð ˜jÑ(¨4Ñ/ð3
ð ˜˜c˜‘N XÑ-°Ñ4ð3
ð" 
÷#3
ó 3
r#   r?   rO   )#r8   r*   rf   Útorchvision.transforms.v2r   ÚtvFÚimage_processing_backendsr   Úimage_processing_utilsr   r   Úimage_transformsr   r	   Úimage_utilsr
   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r)   r"   r   r:   r/   r1   r?   Ú__all__r3   r#   r!   Ú<module>rŸ      s  ðñ )ã ã Ý 7å ;ß Aß Eß eÕ eß 4ß /ð/ %¨¨S¨¡/ð /¸uÀSÈ#ÀX¹ð /ÐSVô /ð0 8<ñ	%LØð%Là
��c˜3�h‘Ñ
 $ s¡)Ñ
+¨e°C©jÑ
8ð%Lð �e˜C ˜H‘oÑ%¨¨S©	Ñ1°E¸#±JÑ>ð%Lð Ð-Ñ-°Ñ4ð	%Lð
 õ%LôP% ,°eò %ð ôH
Ð.ó H
ó ðH
ðV #Ð
#�r#   