Ë
    ÜÓ^jƒ&  ã                   óp  — d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZmZ dd	lmZmZ dd
lmZmZ deeef   deeef   defd„Z	 ddedeeeef   z  ee   z  ee   z  deeeef   z  ee   z  ee   z  deez  dz  def
d„Z G d„ ded¬«      Z e G d„ de«      «       Z!dgZ"y)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                 ó~   — | \  }}t        |t        t        f«      r|n||f\  }}|dz
  |z  dz   }|dz
  |z  dz   }||fS )aP  
    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           ú/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/pixtral/image_processing_pixtral.pyÚ_num_image_tokensr"      sa   € ð �M€FˆEÜ.8¸ÄeÌTÀ]Ô.S¡
ÐZdÐfpÐYqÑ€L�+Ø ™	 kÑ1°AÑ5ÐØ !™¨Ñ4°qÑ8ÐØÐ.Ð.Ð.ó    Úinput_imageÚsizeÚinput_data_formatc                 óˆ  — t        |t        t        f«      r|n||f\  }}t        |t        t        f«      r|n||f\  }}t        | |«      \  }}	t	        ||z  |	|z  «      }
|
dkD  rBt        t        j                  ||
z  «      «      }t        t        j                  |	|
z  «      «      }	t        ||	f||f«      \  }}||z  ||z  fS )aN  
    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™DÈ$ÐPTÈÑ€J�	Ü.8¸ÄeÌTÀ]Ô.S¡
ÐZdÐfpÐYqÑ€L�+Ü" ;Ð0AÓB�M€FˆEä�˜Ñ# U¨YÑ%6Ó7€Eàˆq‚yô ”T—Z‘Z ¨¡Ó/Ó0ˆÜ”D—J‘J˜u u™}Ó-Ó.ˆä*;¸VÀU¸OÈlÐ\gÐMhÓ*iÑ'ÐÐ'Ø˜|Ñ+Ð-=ÀÑ-KÐKÐKr#   c                   ó,   — e Zd ZU dZeeef   ez  ed<   y)ÚPixtralImageProcessorKwargsz½
    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__Ú__doc__ÚdictÚstrr)   Ú__annotations__© r#   r!   r1   r1   Y   s   … ñð
 �S˜#�X‘ Ñ$Ô$r#   r1   F)Útotalc                   ó”  ‡ — e Zd Zej                  Zg d¢Zg d¢ZdddœZddiZ	dZ
dZdZdZdZeZdd	gZd
ee   fˆ fd„Zeded
ee   defˆ fd„«       Z	 d#dddededdddf
ˆ fd„Zded   d	eeeef      ddfd„Z	 d#ded   dededddedededededeee   z  dz  deee   z  dz  d edz  d!e e!z  dz  de"e ef   ez  dz  defd"„Z#ˆ xZ$S )$ÚPixtralImageProcessor)g3<Í4'ÐÞ?gwgí¶MÝ?gy{Îå Ú?)g�‡Bô91Ñ?g•wÝt.¹Ð?g�Ý	U¦Ñ?é   ©r   r   Úlongest_edgei   TÚpixel_valuesÚimage_sizesÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr9   )ÚsuperÚ__init__)ÚselfrB   Ú	__class__s     €r!   rE   zPixtralImageProcessor.__init__r   s   ø€ Ü‰ÑÑ"˜6Ó"r#   Úimagesr   c                 ó$   •— t        ‰| �  |fi |¤ŽS ©N)rD   Ú
preprocess)rF   rH   rB   rG   s      €r!   rK   z PixtralImageProcessor.preprocessu   s   ø€ ä‰wÑ! &Ñ3¨FÑ3Ð3r#   NÚimageztorch.Tensorr%   r   Úresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 ó¢  •— |j                   r|j                   |j                   f}n<|j                  r%|j                  r|j                  |j                  f}nt        d«      ‚|j                  r%|j                  r|j                  |j                  f}nt        d«      ‚t	        |||¬«      }t        ‰	| �  |ft        |d   |d   ¬«      |dœ|¤ŽS )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   r>   )r%   rM   )r?   r   r   Ú
ValueErrorr/   rD   Úresizer   )
rF   rL   r%   r   rM   rB   Ú
size_tupleÚpatch_size_tupleÚoutput_sizerG   s
            €r!   rP   z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¸zÐVfÔgˆÜ‰w‰~Øð
Ü ¨°A©¸kÈ!¹nÔMÐX`ñ
Ødjñ
ð 	
r#   c                 ó:  — t        d„ |D «       «      t        d„ |D «       «      f}t        ||«      D ��cg c]F  \  }}t        j                  j                  j                  |d|d   |d   z
  d|d   |d   z
  f¬«      ‘ŒH }}}t        j                  |«      S c c}}w )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   ó&   K  — | ]	  }|d    –— Œ y­w)r   Nr9   ©Ú.0Úss     r!   ú	<genexpr>z:PixtralImageProcessor._pad_for_batching.<locals>.<genexpr>±   s   è ø€ Ò3 !˜˜1�Ñ3ùó   ‚c              3   ó&   K  — | ]	  }|d    –— Œ y­w)r   Nr9   rV   s     r!   rY   z:PixtralImageProcessor._pad_for_batching.<locals>.<genexpr>±   s   è ø€ Ò8SÀ!¸¸1½Ñ8SùrZ   r   r   )Úpad)r(   ÚzipÚtorchÚnnr   r\   Ústack)rF   r@   rA   Ú	max_shapeÚimgr%   Úpaddeds          r!   Ú_pad_for_batchingz'PixtralImageProcessor._pad_for_batching    s¥   € ô" Ñ3 {Ô3Ó3´SÑ8SÀ{Ô8SÓ5SÐTˆ	ô ! ¨{Ó;÷
á��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           	      óZ  — t        |xs | j                  d¬«      }t        di |¤Ž}t        ||¬«      \  }}i }|j	                  «       D ]!  \  }}|r| j                  ||||¬«      }|||<   Œ# t        ||«      }t        ||¬«      \  }}t        t        |«      «      D �cg c]
  }||   d   ‘Œ }}i }|j	                  «       D ]4  \  }}|r| j                  ||«      }| j                  ||||	|
|«      }|||<   Œ6 t        ||«      }| j                  ||¬«      }t        ||dœ|¬«      S c c}w )	NT)Údefault_to_square)rm   )rL   r%   r   rM   r   )r@   rA   )ÚdataÚtensor_typer9   )r   r   r   r   ÚitemsrP   r	   ÚrangeÚlenÚcenter_cropÚrescale_and_normalizerd   r   )rF   rH   re   r%   rM   rf   rg   rh   ri   rj   rk   rl   rm   rn   r   rB   Ú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!   Ú_preprocessz!PixtralImageProcessor._preprocess¸   s‡  € ô$ # :Ò#@°·±ÐTXÔYˆ
Ü Ñ. :Ñ.ˆä/DÀVÐ^nÔ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ó%;ò 	;Ñ!ˆE�>ÙØ!%§¡Ø(¨tÀÐX`ð "-ó "�ð -;Ð" 5Ò)ð	;ô (Ð(>Ð@TÓUˆä/DÀ^ÐfvÔ/wÑ,ˆÐ,ÜAFÄsÐK_ÓG`ÓAaÖb¸AÐ1°!Ñ4°QÓ7ÐbÐÐbà#%Ð Ø%3×%9Ñ%9Ó%;ò 	=Ñ!ˆE�>ÙØ!%×!1Ñ!1°.À)Ó!L�Ø!×7Ñ7Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð	=ô *Ð*BÐDXÓYÐØ×.Ñ.Ø)Ø)ð /ó 
ˆô
 Ø"/Ð@QÑRÐ`nô
ð 	
ùò# cs   ÂD(rJ   )%r2   r3   r4   r   ÚBICUBICrM   rk   rl   r   r%   rp   re   rh   rj   Údo_convert_rgbr1   Úvalid_kwargsÚmodel_input_namesr   rE   r   r   r   rK   r   rP   r   r   r)   rd   ÚboolÚfloatr7   r   r6   r„   Ú__classcell__)rG   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
r#   r<   rJ   )#r5   r*   r^   Ú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   r7   r/   r1   r<   Ú__all__r9   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#   